Skip to content

Hooks 指南

AgenticFORGE Hooks 用于在不改业务逻辑的前提下,扩展和观测 Agent 运行时生命周期。

为什么需要 Hooks

适用场景:

  • run / LLM / tool 阶段做结构化日志
  • 统计指标(耗时、次数、成功/失败)
  • 审计追踪(traceId 串联链路)
  • 在模型调用前后、工具调用前后注入策略

生命周期事件

事件含义
beforeRunAgent 开始执行前
afterRunAgent 成功结束后
onErrorAgent 执行发生异常时
beforeLLMCallLLM 请求前
afterLLMCallLLM 响应后
beforeToolCall工具调用前
afterToolCall工具调用后

快速开始

ts
import {SimpleAgent} from "@agenticforge/agents";
import {LLMClient, createConsoleLoggingHook, MetricsHook} from "@agenticforge/core";

const llm = new LLMClient({provider: "openai", model: "gpt-4o"});
const agent = new SimpleAgent({name: "assistant", llm});

const metrics = new MetricsHook();

agent
  .useHook(createConsoleLoggingHook({events: ["afterRun", "onError"]}))
  .useHook(metrics.hook);

const answer = await agent.run("请总结这份设计。");
console.log(answer);
console.log(metrics.getSnapshot());

Hook 配置项

通用字段

字段类型说明
namestringHook 名称
prioritynumber优先级(越大越先执行)
strictbooleantrue 时 Hook 报错会中断主流程
eventsAgentHookEvent[]仅订阅指定事件

上下文字段

handle(context) 可读取:

  • event, agentName, traceId, timestamp
  • 可选 inputText, outputText
  • 可选 llmRequest, llmResponse
  • 可选 toolName, toolInput, toolOutput
  • 可选 error, metadata

自定义 Hook 示例

ts
import type {AgentHook} from "@agenticforge/core";

const timingHook: AgentHook = {
  name: "timing-hook",
  events: ["beforeRun", "afterRun"],
  handle: (ctx) => {
    console.log(`[${ctx.event}] trace=${ctx.traceId} agent=${ctx.agentName}`);
  },
};

agent.useHook(timingHook);

内置 Hook

createConsoleLoggingHook

内置日志 Hook,支持事件过滤。

ts
agent.useHook(createConsoleLoggingHook({events: ["afterRun", "onError"]}));

MetricsHook

采集事件计数与 run/LLM/tool 的平均耗时。

ts
const metrics = new MetricsHook();
agent.useHook(metrics.hook);

// 后续读取
const snapshot = metrics.getSnapshot();

最佳实践

  • 生产环境观测 Hook 建议 strict: false
  • metadata 挂业务标签(租户、场景、请求类型)。
  • 日志落盘前注意对 prompt/tool 入参脱敏。
  • 需要控制执行依赖时,用 priority 编排 Hook 顺序。

相关文档

Released under the CC BY-NC-SA 4.0 License.