7171 ShellCallOutcome ,
7272 ShellCommandOutput ,
7373 Tool ,
74+ ToolOrigin ,
75+ get_function_tool_origin ,
7476 invoke_function_tool ,
7577 maybe_invoke_function_tool_failure_error_function ,
7678 resolve_computer ,
@@ -1050,6 +1052,7 @@ async def on_invoke_tool(_ctx: ToolContext[Any], value: Any) -> Any:
10501052 on_invoke_tool = on_invoke_tool ,
10511053 strict_json_schema = True ,
10521054 is_enabled = True ,
1055+ _emit_tool_origin = False ,
10531056 )
10541057
10551058
@@ -1649,6 +1652,7 @@ async def _maybe_execute_tool_approval(
16491652 tool_call ,
16501653 rejection_message = rejection_message ,
16511654 scope_id = self .tool_state_scope_id ,
1655+ tool_origin = get_function_tool_origin (func_tool ),
16521656 ),
16531657 )
16541658
@@ -1844,6 +1848,7 @@ def _build_function_tool_results(self) -> list[FunctionToolResult]:
18441848 output = result ,
18451849 raw_item = ItemHelpers .tool_call_output_item (tool_run .tool_call , result ),
18461850 agent = self .public_agent ,
1851+ tool_origin = get_function_tool_origin (tool_run .function_tool ),
18471852 )
18481853 else :
18491854 # Skip tool output until nested interruptions are resolved.
@@ -2060,14 +2065,22 @@ async def execute_approved_tools(
20602065 if isinstance (tool_name , str ) and tool_name :
20612066 tool_map [tool_name ] = tool
20622067
2063- def _append_error (message : str , * , tool_call : Any , tool_name : str , call_id : str ) -> None :
2068+ def _append_error (
2069+ message : str ,
2070+ * ,
2071+ tool_call : Any ,
2072+ tool_name : str ,
2073+ call_id : str ,
2074+ tool_origin : ToolOrigin | None = None ,
2075+ ) -> None :
20642076 append_approval_error_output (
20652077 message = message ,
20662078 tool_call = tool_call ,
20672079 tool_name = tool_name ,
20682080 call_id = call_id ,
20692081 generated_items = generated_items ,
20702082 agent = agent ,
2083+ tool_origin = tool_origin ,
20712084 )
20722085
20732086 async def _resolve_tool_run (
@@ -2095,14 +2108,25 @@ async def _resolve_tool_run(
20952108
20962109 call_id = extract_tool_call_id (tool_call )
20972110 if not call_id :
2111+ resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
2112+ if resolved_tool is None and tool_namespace is None :
2113+ resolved_tool = tool_map .get (tool_name )
20982114 _append_error (
20992115 message = "Tool approval item missing call ID." ,
21002116 tool_call = tool_call ,
21012117 tool_name = tool_name ,
21022118 call_id = "unknown" ,
2119+ tool_origin = (
2120+ get_function_tool_origin (resolved_tool )
2121+ if isinstance (resolved_tool , FunctionTool )
2122+ else None
2123+ ),
21032124 )
21042125 return None
21052126
2127+ resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
2128+ if resolved_tool is None and tool_namespace is None :
2129+ resolved_tool = tool_map .get (tool_name )
21062130 approval_status = context_wrapper .get_approval_status (
21072131 tool_name ,
21082132 call_id ,
@@ -2111,9 +2135,6 @@ async def _resolve_tool_run(
21112135 tool_lookup_key = tool_lookup_key ,
21122136 )
21132137 if approval_status is False :
2114- resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
2115- if resolved_tool is None and tool_namespace is None :
2116- resolved_tool = tool_map .get (tool_name )
21172138 message = REJECTION_MESSAGE
21182139 if isinstance (resolved_tool , FunctionTool ):
21192140 message = await resolve_approval_rejection_message (
@@ -2131,6 +2152,11 @@ async def _resolve_tool_run(
21312152 tool_call = tool_call ,
21322153 tool_name = tool_name ,
21332154 call_id = call_id ,
2155+ tool_origin = (
2156+ get_function_tool_origin (resolved_tool )
2157+ if isinstance (resolved_tool , FunctionTool )
2158+ else None
2159+ ),
21342160 )
21352161 return None
21362162
@@ -2140,12 +2166,15 @@ async def _resolve_tool_run(
21402166 tool_call = tool_call ,
21412167 tool_name = tool_name ,
21422168 call_id = call_id ,
2169+ tool_origin = (
2170+ get_function_tool_origin (resolved_tool )
2171+ if isinstance (resolved_tool , FunctionTool )
2172+ else None
2173+ ),
21432174 )
21442175 return None
21452176
2146- tool = tool_map .get (approval_key ) if approval_key is not None else None
2147- if tool is None and tool_namespace is None :
2148- tool = tool_map .get (tool_name )
2177+ tool = resolved_tool
21492178 if tool is None :
21502179 _append_error (
21512180 message = f"Tool '{ display_tool_name } ' not found." ,
0 commit comments