AutoContextHook
public class AutoContextHook implements Hook
io/agentscope/core/memory/autocontext/AutoContextHook.java:83–285Auto 上下文钩子,在多次调用之间保存和恢复会话与记忆数据。位于 io.agentscope.core.memory.autocontext 包,实现 Hook。核心成员包括 onEvent、priority、AutoContextHook。
Hook for automatically registering AutoContextMemory integration with ReActAgent.
This hook automatically performs the following setup when a ReActAgent with AutoContextMemory is first called:
-
Registers
ContextOffloadToolto the agent's toolkit -
Attaches the agent's PlanNotebook to AutoContextMemory (if available)
Additionally, this hook handles PreReasoningEvent to trigger memory
compression before LLM reasoning. This ensures compression happens at a
deterministic point in the execution flow, and the compressed messages are
used for reasoning.
This hook ensures that AutoContextMemory is properly integrated with the agent without requiring manual setup steps. It uses an internal flag to ensure setup is only performed once, even if the hook is called multiple times.
Usage Example:
`AutoContextMemory memory = new AutoContextMemory(autoContextConfig, model);
AutoContextHook hook = new AutoContextHook();
ReActAgent agent = ReActAgent.builder()
.name("Assistant")
.model(model)
.memory(memory)
.hook(hook) // Register the hook
.enablePlan()
.toolkit(toolkit)
.build();
// The hook will automatically:
// 1. Register ContextOffloadTool when agent is first called
// 2. Attach PlanNotebook to AutoContextMemory if available
// 3. Trigger compression before each LLM reasoning call
`
Priority: This hook has high priority (50) to ensure setup occurs early in the event chain, before other hooks process the event.
构造器摘要Constructor Summary
| 构造器Constructor | 说明Description |
|---|---|
public AutoContextHook() | 创建新的AutoContextHook。 Creates a new AutoContextHook. |
方法摘要Method Summary
| 方法Method | 说明Description |
|---|---|
public T extends HookEvent Mono<T> onEvent(T event) | 处理事件事件。 Performs the on event operation. |
public int priority() | 返回优先级。 Returns the priority. |
构造器详情Constructor Detail
AutoContextHook
public AutoContextHook()
创建新的AutoContextHook。
Creates a new AutoContextHook.
io/agentscope/core/memory/autocontext/AutoContextHook.java:95–97方法详情Method Detail
onEvent
public T extends HookEvent Mono<T> onEvent(T event)
处理事件事件。
Performs the on event operation.
| 名称Name | 类型Type | 说明Description |
|---|---|---|
event | T | 事件(类型 `T`)。The event. |
Mono<T>io/agentscope/core/memory/autocontext/AutoContextHook.java:99–112priority
public int priority()
返回优先级。
Returns the priority.
intio/agentscope/core/memory/autocontext/AutoContextHook.java:114–118