跳过内容

工具上下文

ToolContext dataclass

基础类: RunContextWrapper[TContext]

工具调用的上下文。

源代码位于 src/agents/tool_context.py
@dataclass
class ToolContext(RunContextWrapper[TContext]):
    """The context of a tool call."""

    tool_name: str = field(default_factory=_assert_must_pass_tool_name)
    """The name of the tool being invoked."""

    tool_call_id: str = field(default_factory=_assert_must_pass_tool_call_id)
    """The ID of the tool call."""

    tool_arguments: str = field(default_factory=_assert_must_pass_tool_arguments)
    """The raw arguments string of the tool call."""

    tool_call: Optional[ResponseFunctionToolCall] = None
    """The tool call object associated with this invocation."""

    @classmethod
    def from_agent_context(
        cls,
        context: RunContextWrapper[TContext],
        tool_call_id: str,
        tool_call: Optional[ResponseFunctionToolCall] = None,
    ) -> "ToolContext":
        """
        Create a ToolContext from a RunContextWrapper.
        """
        # Grab the names of the RunContextWrapper's init=True fields
        base_values: dict[str, Any] = {
            f.name: getattr(context, f.name) for f in fields(RunContextWrapper) if f.init
        }
        tool_name = tool_call.name if tool_call is not None else _assert_must_pass_tool_name()
        tool_args = (
            tool_call.arguments if tool_call is not None else _assert_must_pass_tool_arguments()
        )

        tool_context = cls(
            tool_name=tool_name,
            tool_call_id=tool_call_id,
            tool_arguments=tool_args,
            tool_call=tool_call,
            **base_values,
        )
        return tool_context

tool_name class-attribute instance-attribute

tool_name: str = field(
    default_factory=_assert_must_pass_tool_name
)

正在调用的工具的名称。

tool_call_id class-attribute instance-attribute

tool_call_id: str = field(
    default_factory=_assert_must_pass_tool_call_id
)

工具调用的 ID。

tool_arguments class-attribute instance-attribute

tool_arguments: str = field(
    default_factory=_assert_must_pass_tool_arguments
)

工具调用的原始参数字符串。

tool_call class-attribute instance-attribute

tool_call: Optional[ResponseFunctionToolCall] = None

与此次调用关联的工具调用对象。

context instance-attribute

context: TContext

您传递给 Runner.run() 的上下文对象(或 None)。

usage class-attribute instance-attribute

usage: Usage = field(default_factory=Usage)

到目前为止的代理运行的使用情况。对于流式响应,使用情况在流的最后一个块处理完毕之前将是陈旧的。

from_agent_context classmethod

from_agent_context(
    context: RunContextWrapper[TContext],
    tool_call_id: str,
    tool_call: Optional[ResponseFunctionToolCall] = None,
) -> ToolContext

从 RunContextWrapper 创建 ToolContext。

源代码位于 src/agents/tool_context.py
@classmethod
def from_agent_context(
    cls,
    context: RunContextWrapper[TContext],
    tool_call_id: str,
    tool_call: Optional[ResponseFunctionToolCall] = None,
) -> "ToolContext":
    """
    Create a ToolContext from a RunContextWrapper.
    """
    # Grab the names of the RunContextWrapper's init=True fields
    base_values: dict[str, Any] = {
        f.name: getattr(context, f.name) for f in fields(RunContextWrapper) if f.init
    }
    tool_name = tool_call.name if tool_call is not None else _assert_must_pass_tool_name()
    tool_args = (
        tool_call.arguments if tool_call is not None else _assert_must_pass_tool_arguments()
    )

    tool_context = cls(
        tool_name=tool_name,
        tool_call_id=tool_call_id,
        tool_arguments=tool_args,
        tool_call=tool_call,
        **base_values,
    )
    return tool_context