AS AgentScopeAPI 手册 / API Reference

HookEvent

@Deprecated(forRemoval = true, since = "2.0.0") public abstract sealed class HookEvent permits PreCallEvent, PostCallEvent, ReasoningEvent, ActingEvent, SummaryEvent, ErrorEvent
源码Source io/agentscope/core/hook/HookEvent.java:76–211

所有钩子事件的基类。位于 io.agentscope.core.hook 包。核心成员包括 getType、getAgent、getTimestamp、getMemory、getSystemMessage、setSystemMessage。

Base class for all hook events.

This is a sealed class - only the predefined event types are permitted. This enables exhaustive pattern matching in switch expressions.

All events provide access to common context:

  • #getAgent() - The agent instance

  • #getMemory() - Convenient access to agent's memory (may be null)

  • #getType() - The event type

  • #getTimestamp() - When the event occurred

System message lifecycle: Every event carries a unified systemMsg field that holds the single MsgRole#SYSTEM message visible to the LLM. ReActAgent manages this field across the event lifecycle as follows:

  • Seeded from sysPrompt at the start of each call() before PreCallEvent hooks run.

  • After PreCallEvent hooks complete, the resulting system message is frozen as the base for the entire call.

  • Before each PreReasoningEvent (and PreSummaryEvent), the frozen base is injected fresh into the event — hooks that run on these events always start from the same clean baseline and may append per-iteration content.

  • Before model.stream(...) is called: the event's final system message is prepended to PreReasoningEvent#getInputMessages() as the first element.

Because each PreReasoningEvent starts from a fresh copy of the frozen base, hooks that fire per-iteration (e.g. subagent guidance) can safely use #appendSystemContent(String) — content is added to that iteration's copy and never accumulates across iterations.

Hooks should modify the system message exclusively via #setSystemMessage(Msg), #appendSystemContent(String), or #appendSystemContent(ContentBlock). Injecting MsgRole#SYSTEM messages into inputMessages directly is forbidden and results in an IllegalStateException at runtime.

Modifiability: Whether an event allows modification is determined by the presence of setter methods in the concrete event class.

构造器摘要Constructor Summary

构造器Constructor说明Description
protected HookEvent(HookEventType type, Agent agent)
初始化 `HookEvent` 实例。
Constructor for HookEvent.

方法摘要Method Summary

方法Method说明Description
public final HookEventType getType()
final
获取事件类型。
Get the event type.
public final Agent getAgent()
final
获取智能体实例。
Get the agent instance.
public final long getTimestamp()
final
获取时间戳。
Get the timestamp when event was created.
public final Memory getMemory()
final
获取记忆。
Convenient access to the agent's conversation context wrapped as a read-only `Memory` view.
public final Msg getSystemMessage()
final
获取系统消息。
Returns the current unified system message, or `null` if none has been set.
public final void setSystemMessage(Msg systemMsg)
final
设置系统消息。
Replaces the entire system message with the given one.
public final void appendSystemContent(String text)
final
追加系统内容。
Appends the given text as a new `TextBlock` at the end of the system message.
public final void appendSystemContent(ContentBlock block)
final
追加系统内容。
Appends a `ContentBlock` at the end of the system message.

构造器详情Constructor Detail

HookEvent

protected HookEvent(HookEventType type, Agent agent)

初始化 HookEvent 实例。

Constructor for HookEvent.

参数Parameters
名称Name类型Type说明Description
typeHookEventType类型(类型 `HookEventType`)。The event type (must not be null)
agentAgent智能体(类型 `Agent`)。The agent instance (must not be null)
抛出Throws
  • NullPointerException 可能抛出 `NullPointerException`。if type or agent is null
源码Source io/agentscope/core/hook/HookEvent.java:98–102

方法详情Method Detail

getType

public final HookEventType getType()
final

获取事件类型。

Get the event type.

返回Returns HookEventType
事件类型。The event type
源码Source io/agentscope/core/hook/HookEvent.java:109–111

getAgent

public final Agent getAgent()
final

获取智能体实例。

Get the agent instance.

返回Returns Agent
返回智能体。The agent instance (never null)
源码Source io/agentscope/core/hook/HookEvent.java:118–120

getTimestamp

public final long getTimestamp()
final

获取时间戳。

Get the timestamp when event was created.

返回Returns long
返回数值结果。The timestamp (milliseconds since epoch)
源码Source io/agentscope/core/hook/HookEvent.java:127–129

getMemory

public final Memory getMemory()
final

获取记忆。

Convenient access to the agent's conversation context wrapped as a read-only Memory view.

返回Returns Memory
返回记忆。a read-only `Memory` view, or `null` if the event's agent is not a `ReActAgent`
源码Source io/agentscope/core/hook/HookEvent.java:139–144

getSystemMessage

public final Msg getSystemMessage()
final

获取系统消息。

Returns the current unified system message, or null if none has been set.

返回Returns Msg
返回消息。the system message, may be null
源码Source io/agentscope/core/hook/HookEvent.java:156–158

setSystemMessage

public final void setSystemMessage(Msg systemMsg)
final

设置系统消息。

Replaces the entire system message with the given one.

参数Parameters
名称Name类型Type说明Description
systemMsgMsg系统消息(类型 `Msg`)。the new system message (may be null to clear)
源码Source io/agentscope/core/hook/HookEvent.java:168–170

appendSystemContent

public final void appendSystemContent(String text)
final

追加系统内容。

Appends the given text as a new TextBlock at the end of the system message.

参数Parameters
名称Name类型Type说明Description
textString文本(类型 `String`)。the text to append (must not be null)
源码Source io/agentscope/core/hook/HookEvent.java:180–183

appendSystemContent

public final void appendSystemContent(ContentBlock block)
final

追加系统内容。

Appends a ContentBlock at the end of the system message.

参数Parameters
名称Name类型Type说明Description
blockContentBlock内容块(类型 `ContentBlock`)。the content block to append (must not be null)
源码Source io/agentscope/core/hook/HookEvent.java:193–210
基于官方固定版本源码静态生成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.