跳至内容

Lifecycle

运行钩子

基类: Generic[TContext]

一个接收代理运行过程中各类生命周期事件回调的类。通过子类化并重写您需要的方法来实现自定义处理。

on_agent_start async

on_agent_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
) -> None

在代理被调用前执行。每次当前代理变更时都会调用。

on_agent_end async

on_agent_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    output: Any,
) -> None

当代理生成最终输出时调用。

on_handoff async

on_handoff(
    context: RunContextWrapper[TContext],
    from_agent: Agent[TContext],
    to_agent: Agent[TContext],
) -> None

当发生转接时调用。

on_tool_start async

on_tool_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    tool: Tool,
) -> None

在调用工具之前执行。

on_tool_end async

on_tool_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    tool: Tool,
    result: str,
) -> None

在工具被调用后执行。

AgentHooks

基类: Generic[TContext]

一个接收特定代理各种生命周期事件回调的类。您可以在agent.hooks上设置此钩子来接收该特定代理的事件。

子类化并重写你需要的方法。

启动时 async

on_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
) -> None

在调用代理程序之前调用。每次运行中的代理程序切换为此代理程序时都会调用。

结束回调 async

on_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    output: Any,
) -> None

当代理生成最终输出时调用。

on_handoff async

on_handoff(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    source: Agent[TContext],
) -> None

当代理被移交时调用。source是正在移交给该代理的源代理。

工具启动时 async

on_tool_start(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    tool: Tool,
) -> None

在调用工具之前执行。

on_tool_end async

on_tool_end(
    context: RunContextWrapper[TContext],
    agent: Agent[TContext],
    tool: Tool,
    result: str,
) -> None

在工具被调用后执行。