AS AgentScopeAPI 手册 / API Reference

AutoContextHook

public class AutoContextHook implements Hook
源码Source io/agentscope/core/memory/autocontext/AutoContextHook.java:83–285

Auto 上下文钩子,在多次调用之间保存和恢复会话与记忆数据。位于 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 ContextOffloadTool to 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.

源码Source 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.

参数Parameters
名称Name类型Type说明Description
eventT事件(类型 `T`)。The event.
返回Returns Mono<T>
返回Mono。Returns mono.
源码Source io/agentscope/core/memory/autocontext/AutoContextHook.java:99–112

priority

public int priority()

返回优先级。

Returns the priority.

返回Returns int
返回数值结果。Returns a numeric result.
源码Source io/agentscope/core/memory/autocontext/AutoContextHook.java:114–118

继承成员Inherited Members

继承自Methods inherited from Hook
基于官方固定版本源码静态生成Generated from pinned official sources · Python 2.0.11 · Java 2.0.4
签名与文档为静态抽取;如有疑问请以源码为准。Signatures and docs are extracted statically; verify against the source when in doubt.