12
Part,
13
)
14
15
-from composio.core.provider import NonAgenticProvider
15
+from composio.core.provider import NonAgenticProvider, ToolCallSession
16
from composio.types import Modifiers, Tool, ToolExecutionResponse
17
from composio.utils.json_schema import dereference_json_schema
18
from composio.utils.shared import normalize_tool_arguments
62
def wrap_tools(self, tools: t.Sequence[Tool]) -> list[FunctionDeclaration]:
63
return [self.wrap_tool(tool) for tool in tools]
64
65
+ @t.overload
66
def execute_tool_call(
67
self,
68
user_id: str,
69
function_call: t.Any,
70
modifiers: t.Optional[Modifiers] = None,
71
+ ) -> ToolExecutionResponse: ...
72
+
73
+ @t.overload
74
+ def execute_tool_call(
75
+ self,
76
+ *,
77
+ session: ToolCallSession,
78
+ function_call: t.Any,
79
+ ) -> ToolExecutionResponse: ...
80
+
81
+ def execute_tool_call(
82
+ self,
83
+ user_id: t.Optional[str] = None,
84
+ function_call: t.Any = None,
85
+ modifiers: t.Optional[Modifiers] = None,
86
+ *,
87
+ session: t.Optional[ToolCallSession] = None,
88
) -> ToolExecutionResponse:
89
"""
90
Execute a function call.
91
92
:param function_call: Function call metadata from Gemini model response.
75
- :param user_id: User ID to use for executing the function call.
93
+ :param user_id: User ID for direct tool execution.
94
+ :param session: Tool Router session that produced session tools.
95
+ :param modifiers: Modifiers to use for direct execution.
96
:return: Object containing output data from the function call.
97
"""
98
+ if function_call is None:
99
+ raise TypeError("function_call is required")
100
# Gemini returns args as a MapComposite; normalize after converting to a
101
# plain dict so a stringified payload is handled uniformly too (issue #2406).
80
- return self.execute_tool(
81
- slug=function_call.name,
82
- arguments=normalize_tool_arguments(
83
- _convert_map_composite(function_call.args)
102
+ arguments = normalize_tool_arguments(_convert_map_composite(function_call.args))
103
+ return self.execute_tool_for_target(
104
+ target=self.resolve_tool_call_execution_target(
105
+ user_id=user_id, session=session
106
),
107
+ slug=function_call.name,
108
+ arguments=arguments,
109
modifiers=modifiers,
86
- user_id=user_id,
110
)
111
112
+ @t.overload
113
def handle_response(
114
self,
115
user_id: str,
116
response: GenerationResponse,
117
modifiers: t.Optional[Modifiers] = None,
118
+ ) -> t.List[ToolExecutionResponse]: ...
119
+
120
+ @t.overload
121
+ def handle_response(
122
+ self,
123
+ *,
124
+ session: ToolCallSession,
125
+ response: GenerationResponse,
126
+ ) -> t.List[ToolExecutionResponse]: ...
127
+
128
+ def handle_response(
129
+ self,
130
+ user_id: t.Optional[str] = None,
131
+ response: t.Optional[GenerationResponse] = None,
132
+ modifiers: t.Optional[Modifiers] = None,
133
+ *,
134
+ session: t.Optional[ToolCallSession] = None,
135
) -> t.List[ToolExecutionResponse]:
136
"""
137
Handle response from Google AI Python Gemini model.
138
139
:param response: Generation response from the Gemini model.
99
- :param user_id: User ID to use for executing the function call.
140
+ :param user_id: User ID for direct tool execution.
141
+ :param session: Tool Router session that produced session tools.
142
+ :param modifiers: Modifiers to use for direct execution.
143
:return: A list of output objects from the function calls.
144
"""
145
+ if response is None:
146
+ raise TypeError("response is required")
147
+ self.resolve_tool_call_execution_target(user_id=user_id, session=session)
148
+ if session is not None and modifiers is not None:
149
+ raise ValueError(
150
+ "Direct execution modifiers cannot be used with a Tool Router session"
151
+ )
152
outputs = []
153
for candidate in response.candidates:
154
if isinstance(candidate.content, Content) and candidate.content.parts:
156
if isinstance(part, Part) and part.function_call:
157
outputs.append(
158
self.execute_tool_call(
109
- user_id=user_id,
159
+ session=session,
160
+ function_call=part.function_call,
161
+ )
162
+ if session is not None
163
+ else self.execute_tool_call(
164
+ user_id=t.cast(str, user_id),
165
function_call=part.function_call,
166
modifiers=modifiers,
167
)