-
Notifications
You must be signed in to change notification settings - Fork 3.1k
feat: #2228 Add tool origin tracking to ToolCallItem and ToolCallOutputItem #2242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import enum | ||
| import inspect | ||
| import json | ||
| import weakref | ||
|
|
@@ -179,6 +180,42 @@ class ComputerProvider(Generic[ComputerT]): | |
| ] | ||
|
|
||
|
|
||
| class ToolOriginType(str, enum.Enum): | ||
| """The type of tool origin.""" | ||
|
|
||
| FUNCTION = "function" | ||
| """Regular Python function tool created via @function_tool decorator.""" | ||
|
|
||
| MCP = "mcp" | ||
| """MCP server tool converted via MCPUtil.to_function_tool().""" | ||
|
|
||
| AGENT_AS_TOOL = "agent_as_tool" | ||
| """Agent converted to tool via agent.as_tool().""" | ||
|
|
||
|
|
||
| @dataclass | ||
| class ToolOrigin: | ||
| """Information about the origin/source of a function tool.""" | ||
|
|
||
| type: ToolOriginType | ||
| """The type of tool origin.""" | ||
|
|
||
| mcp_server_name: str | None = None | ||
| """The name of the MCP server. Only set when type is MCP.""" | ||
|
|
||
| agent_as_tool_name: str | None = None | ||
|
||
| """The name of the agent. Only set when type is AGENT_AS_TOOL.""" | ||
|
|
||
| def __repr__(self) -> str: | ||
| """Custom repr that only includes relevant fields.""" | ||
| parts = [f"type={self.type.value!r}"] | ||
| if self.mcp_server_name is not None: | ||
| parts.append(f"mcp_server_name={self.mcp_server_name!r}") | ||
| if self.agent_as_tool_name is not None: | ||
| parts.append(f"agent_as_tool_name={self.agent_as_tool_name!r}") | ||
| return f"ToolOrigin({', '.join(parts)})" | ||
|
|
||
|
|
||
| @dataclass | ||
| class FunctionToolResult: | ||
| tool: FunctionTool | ||
|
|
@@ -235,6 +272,9 @@ class FunctionTool: | |
| tool_output_guardrails: list[ToolOutputGuardrail[Any]] | None = None | ||
| """Optional list of output guardrails to run after invoking this tool.""" | ||
|
|
||
| _tool_origin: ToolOrigin | None = field(default=None, init=False, repr=False) | ||
| """Private field tracking tool origin. Set by SDK when creating tools.""" | ||
|
|
||
| def __post_init__(self): | ||
| if self.strict_json_schema: | ||
| self.params_json_schema = ensure_strict_json_schema(self.params_json_schema) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems the code comment here is incorrect
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to
Set origin tracking on run_agent (the FunctionTool returned by @function_tool)for more clarity