AS AgentScopeAPI 手册 / API Reference

Hook

@Deprecated(forRemoval = true, since = "2.0.0") public interface Hook
源码Source io/agentscope/core/hook/Hook.java:121–191

定义钩子契约的接口。位于 io.agentscope.core.hook 包。核心成员包括 onEvent、tools、priority。

Hook interface for monitoring and intercepting agent execution.

All agent execution events are delivered through a single #onEvent(HookEvent) method. This unified event model provides a clean, type-safe way to intercept and modify agent behavior.

Hook Priority: Hooks are executed in priority order (lower value = higher priority). Default priority is 100. Hooks with the same priority execute in registration order.

Event Modifiability: Whether an event is modifiable is indicated by the presence of setter methods:

  • Events with setters (e.g., PreReasoningEvent#setInputMessages) allow modification

  • Events without setters are notification-only

Example Usage:

`// Basic hook with default priority
Hook loggingHook = new Hook() {
@Override
public <T extends HookEvent> Mono<T> onEvent(T event) {
return switch (event) {
case PreReasoningEvent e -> {
System.out.println("Reasoning with model: " + e.getModelName());
yield Mono.just(e);
}
case ReasoningChunkEvent e -> {
// Display streaming output
System.out.print(extractText(e.getIncrementalChunk()));
yield Mono.just(e);
}
default -> Mono.just(event);
};
}
};

// High priority hook (executes first)
Hook authHook = new Hook() {
@Override
public int priority() {
return 10; // High priority
}

@Override
public <T extends HookEvent> Mono<T> onEvent(T event) {
return switch (event) {
case PreActingEvent e -> {
// Inject auth token before any other hook
ToolUseBlock toolUse = e.getToolUse();
// ... add auth
e.setToolUse(toolUse);
yield Mono.just(e);
}
default -> Mono.just(event);
};
}
};

// Modifying events
Hook hintInjector = new Hook() {
@Override
public <T extends HookEvent> Mono<T> onEvent(T event) {
return switch (event) {
case PreReasoningEvent e -> {
// Modify messages before LLM reasoning
List<Msg> msgs = new ArrayList<>(e.getInputMessages());
msgs.add(0, Msg.builder()
.role(MsgRole.SYSTEM)
.content(new TextBlock("Think step by step"))
.build());
e.setInputMessages(msgs);
yield Mono.just(e);
}
case PostActingEvent e -> {
// Modify tool result
ToolResultBlock result = e.getToolResult();
// ... process result
e.setToolResult(result);
yield Mono.just(e);
}
default -> Mono.just(event);
};
}
};
`

方法摘要Method Summary

方法Method说明Description
public T extends HookEvent Mono<T> onEvent(T event)
处理钩子事件。
Handle a hook event.
default public List<Object> tools()
default
返回工具。
Optional tools installed together with this hook.
default public int priority()
default
返回优先级。
The priority of this hook (lower value = higher priority).

方法详情Method Detail

onEvent

public T extends HookEvent Mono<T> onEvent(T event)

处理钩子事件。

Handle a hook event.

参数Parameters
名称Name类型Type说明Description
eventT钩子事件。The hook event
返回Returns Mono<T>
返回Mono。Mono containing the potentially modified event
源码Source io/agentscope/core/hook/Hook.java:152

tools

default public List<Object> tools()
default

返回工具。

Optional tools installed together with this hook.

返回Returns List<Object>
返回列出。tool instances to register for this hook (may be immutable)
源码Source io/agentscope/core/hook/Hook.java:168–170

priority

default public int priority()
default

返回优先级。

The priority of this hook (lower value = higher priority).

返回Returns int
优先级值(默认:100)。The priority value (default: 100)
源码Source io/agentscope/core/hook/Hook.java:188–190
基于官方固定版本源码静态生成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.