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 ,
@@ -980,6 +982,7 @@ async def on_invoke_tool(_ctx: ToolContext[Any], value: Any) -> Any:
980982 on_invoke_tool = on_invoke_tool ,
981983 strict_json_schema = True ,
982984 is_enabled = True ,
985+ _emit_tool_origin = False ,
983986 )
984987
985988
@@ -1541,6 +1544,7 @@ async def _maybe_execute_tool_approval(
15411544 tool_call ,
15421545 rejection_message = rejection_message ,
15431546 scope_id = self .tool_state_scope_id ,
1547+ tool_origin = get_function_tool_origin (func_tool ),
15441548 ),
15451549 )
15461550
@@ -1735,6 +1739,7 @@ def _build_function_tool_results(self) -> list[FunctionToolResult]:
17351739 output = result ,
17361740 raw_item = ItemHelpers .tool_call_output_item (tool_run .tool_call , result ),
17371741 agent = self .agent ,
1742+ tool_origin = get_function_tool_origin (tool_run .function_tool ),
17381743 )
17391744 else :
17401745 # Skip tool output until nested interruptions are resolved.
@@ -1926,14 +1931,22 @@ async def execute_approved_tools(
19261931 if isinstance (tool_name , str ) and tool_name :
19271932 tool_map [tool_name ] = tool
19281933
1929- def _append_error (message : str , * , tool_call : Any , tool_name : str , call_id : str ) -> None :
1934+ def _append_error (
1935+ message : str ,
1936+ * ,
1937+ tool_call : Any ,
1938+ tool_name : str ,
1939+ call_id : str ,
1940+ tool_origin : ToolOrigin | None = None ,
1941+ ) -> None :
19301942 append_approval_error_output (
19311943 message = message ,
19321944 tool_call = tool_call ,
19331945 tool_name = tool_name ,
19341946 call_id = call_id ,
19351947 generated_items = generated_items ,
19361948 agent = agent ,
1949+ tool_origin = tool_origin ,
19371950 )
19381951
19391952 async def _resolve_tool_run (
@@ -1961,14 +1974,25 @@ async def _resolve_tool_run(
19611974
19621975 call_id = extract_tool_call_id (tool_call )
19631976 if not call_id :
1977+ resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
1978+ if resolved_tool is None and tool_namespace is None :
1979+ resolved_tool = tool_map .get (tool_name )
19641980 _append_error (
19651981 message = "Tool approval item missing call ID." ,
19661982 tool_call = tool_call ,
19671983 tool_name = tool_name ,
19681984 call_id = "unknown" ,
1985+ tool_origin = (
1986+ get_function_tool_origin (resolved_tool )
1987+ if isinstance (resolved_tool , FunctionTool )
1988+ else None
1989+ ),
19691990 )
19701991 return None
19711992
1993+ resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
1994+ if resolved_tool is None and tool_namespace is None :
1995+ resolved_tool = tool_map .get (tool_name )
19721996 approval_status = context_wrapper .get_approval_status (
19731997 tool_name ,
19741998 call_id ,
@@ -1977,9 +2001,6 @@ async def _resolve_tool_run(
19772001 tool_lookup_key = tool_lookup_key ,
19782002 )
19792003 if approval_status is False :
1980- resolved_tool = tool_map .get (approval_key ) if approval_key is not None else None
1981- if resolved_tool is None and tool_namespace is None :
1982- resolved_tool = tool_map .get (tool_name )
19832004 message = REJECTION_MESSAGE
19842005 if isinstance (resolved_tool , FunctionTool ):
19852006 message = await resolve_approval_rejection_message (
@@ -1994,6 +2015,11 @@ async def _resolve_tool_run(
19942015 tool_call = tool_call ,
19952016 tool_name = tool_name ,
19962017 call_id = call_id ,
2018+ tool_origin = (
2019+ get_function_tool_origin (resolved_tool )
2020+ if isinstance (resolved_tool , FunctionTool )
2021+ else None
2022+ ),
19972023 )
19982024 return None
19992025
@@ -2003,12 +2029,15 @@ async def _resolve_tool_run(
20032029 tool_call = tool_call ,
20042030 tool_name = tool_name ,
20052031 call_id = call_id ,
2032+ tool_origin = (
2033+ get_function_tool_origin (resolved_tool )
2034+ if isinstance (resolved_tool , FunctionTool )
2035+ else None
2036+ ),
20062037 )
20072038 return None
20082039
2009- tool = tool_map .get (approval_key ) if approval_key is not None else None
2010- if tool is None and tool_namespace is None :
2011- tool = tool_map .get (tool_name )
2040+ tool = resolved_tool
20122041 if tool is None :
20132042 _append_error (
20142043 message = f"Tool '{ display_tool_name } ' not found." ,
0 commit comments