4
from unittest.mock import Mock, patch
5
6
import pytest
7
-from pydantic import BaseModel, RootModel
7
from composio_client import omit
8
+from pydantic import BaseModel, RootModel
9
10
from composio.client.types import Tool, tool_list_response
11
from composio.core.models.base import allow_tracking
12
-from composio.core.models.tools import Tools, _serialize_arguments, _needs_serialization
12
+from composio.core.models.tools import Tools, _needs_serialization, _serialize_arguments
13
from composio.exceptions import ToolVersionRequiredError
14
-
14
from tests.conftest import mock_http_client
15
16
80
cursor="next_page",
81
)
82
83
+ def test_tool_router_modifiers_handle_tool_without_toolkit(self):
84
+ """Tool Router modifier fallbacks match TypeScript in each phase."""
85
+ from composio.core.models._modifiers import (
86
+ after_execute,
87
+ before_execute,
88
+ schema_modifier,
89
+ )
90
+
91
+ mock_client = mock_http_client()
92
+ tools = Tools(client=mock_client, provider=Mock())
93
+ raw_tool = self.create_mock_tool("CUSTOM_TOOL", "custom").model_dump()
94
+ raw_tool.pop("toolkit")
95
+ mock_client.tool_router.session.tools.return_value = Mock(
96
+ items=[raw_tool], next_cursor=None
97
+ )
98
+ seen_toolkits = []
99
+
100
+ @schema_modifier
101
+ def observe_toolkit(tool, toolkit, schema):
102
+ seen_toolkits.append(("schema", toolkit))
103
+ return schema
104
+
105
+ @before_execute
106
+ def observe_before_execute(tool, toolkit, params):
107
+ seen_toolkits.append(("before_execute", toolkit))
108
+ return params
109
+
110
+ @after_execute
111
+ def observe_after_execute(tool, toolkit, response):
112
+ seen_toolkits.append(("after_execute", toolkit))
113
+ return response
114
+
115
+ result = tools.get_raw_tool_router_meta_tools(
116
+ "session_123", modifiers=[observe_toolkit]
117
+ )
118
+ mock_client.tool_router.session.execute.return_value = Mock(data={}, error=None)
119
+ execute = tools._wrap_execute_tool_for_tool_router(
120
+ "session_123", modifiers=[observe_before_execute, observe_after_execute]
121
+ )
122
+ execute("CUSTOM_TOOL", {})
123
+
124
+ assert [tool.slug for tool in result] == ["CUSTOM_TOOL"]
125
+ assert seen_toolkits == [
126
+ ("schema", "unknown"),
127
+ ("before_execute", "composio"),
128
+ ("after_execute", "composio"),
129
+ ]
130
+
131
+ def test_execute_without_toolkit_runs_modifiers_and_fetches_once(self):
132
+ """Toolkit-less tools execute with the same fallback used by TypeScript."""
133
+ from composio.core.models._modifiers import after_execute, before_execute
134
+
135
+ mock_client = mock_http_client()
136
+ tools = Tools(
137
+ client=mock_client,
138
+ provider=Mock(),
139
+ toolkit_versions={"unknown": "20250101_00"},
140
+ )
141
+ raw_tool = self.create_mock_tool("CUSTOM_TOOL", "custom").model_dump()
142
+ raw_tool.pop("toolkit")
143
+ mock_client.tools.retrieve.return_value = raw_tool
144
+
145
+ execute_response = Mock()
146
+ execute_response.model_dump.return_value = {
147
+ "data": {"value": "original"},
148
+ "error": None,
149
+ "successful": True,
150
+ }
151
+ mock_client.tools.execute.return_value = execute_response
152
+ seen_toolkits = []
153
+
154
+ @before_execute
155
+ def modify_request(tool, toolkit, params):
156
+ seen_toolkits.append(toolkit)
157
+ params["arguments"]["value"] = "modified"
158
+ return params
159
+
160
+ @after_execute
161
+ def modify_response(tool, toolkit, response):
162
+ seen_toolkits.append(toolkit)
163
+ response["data"]["modified"] = True
164
+ return response
165
+
166
+ result = tools.execute(
167
+ slug="CUSTOM_TOOL",
168
+ arguments={"value": "original"},
169
+ modifiers=[modify_request, modify_response],
170
+ )
171
+
172
+ assert result["data"] == {"value": "original", "modified": True}
173
+ assert seen_toolkits == ["unknown", "unknown"]
174
+ mock_client.tools.retrieve.assert_called_once()
175
+ mock_client.tools.execute.assert_called_once_with(
176
+ tool_slug="CUSTOM_TOOL",
177
+ arguments={"value": "modified"},
178
+ connected_account_id=omit,
179
+ custom_auth_params=omit,
180
+ custom_connection_data=omit,
181
+ user_id=omit,
182
+ text=omit,
183
+ version="20250101_00",
184
+ )
185
+
186
+ def test_provider_execution_uses_raw_metadata_after_schema_modifier(self):
187
+ """Schema presentation changes must not alter execution versioning."""
188
+ from composio.core.models._modifiers import schema_modifier
189
+
190
+ mock_client = mock_http_client()
191
+ mock_provider = Mock()
192
+ tools = Tools(
193
+ client=mock_client,
194
+ provider=mock_provider,
195
+ toolkit_versions={
196
+ "github": "20250101_00",
197
+ "renamed": "20250102_00",
198
+ },
199
+ )
200
+ raw_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github").model_dump()
201
+ mock_client.tools.list.return_value = Mock(items=[raw_tool])
202
+ execute_response = Mock()
203
+ execute_response.model_dump.return_value = {
204
+ "data": {"result": "success"},
205
+ "error": None,
206
+ "successful": True,
207
+ }
208
+ mock_client.tools.execute.return_value = execute_response
209
+ wrapped = {}
210
+
211
+ def capture_tools(tools, execute_tool):
212
+ wrapped["tools"] = tools
213
+ wrapped["execute_tool"] = execute_tool
214
+ return tools
215
+
216
+ mock_provider.wrap_tools.side_effect = capture_tools
217
+
218
+ @schema_modifier
219
+ def rename_toolkit(tool, toolkit, schema):
220
+ schema.toolkit = tool_list_response.ItemToolkit(
221
+ name="Renamed", slug="renamed", logo=""
222
+ )
223
+ return schema
224
+
225
+ tools.get(
226
+ user_id="user_123",
227
+ tools=["GITHUB_GET_REPOS"],
228
+ modifiers=[rename_toolkit],
229
+ )
230
+ wrapped["execute_tool"]("GITHUB_GET_REPOS", {})
231
+
232
+ assert wrapped["tools"][0].toolkit.slug == "renamed"
233
+ mock_client.tools.retrieve.assert_not_called()
234
+ assert mock_client.tools.execute.call_args.kwargs["version"] == "20250101_00"
235
+
236
def test_tool_execution_uses_toolkit_version(self):
237
"""Test that tool execution resolves toolkit version correctly."""
238
# Mock client and provider
270
tools._execute_tool(
271
slug="GITHUB_GET_REPOS",
272
arguments={"owner": "test", "repo": "test"},
273
+ tool=github_tool,
274
)
275
276
# Verify that the client was called with the resolved version
319
tools._execute_tool(
320
slug="GITHUB_GET_REPOS",
321
arguments={"owner": "test", "repo": "test"},
322
+ tool=github_tool,
323
version="20251201_03", # Explicit version should take precedence
324
)
325
369
tools._execute_tool(
370
slug="GITHUB_GET_REPOS",
371
arguments={"owner": "test", "repo": "test"},
372
+ tool=github_tool,
373
dangerously_skip_version_check=True,
374
)
375
417
tools._execute_tool(
418
slug="GITHUB_GET_REPOS",
419
arguments={"owner": "test", "repo": "test"},
420
+ tool=github_tool,
421
)
422
423
# Verify that the client was called with the global version
491
tools._execute_tool(
492
slug=tool_slug,
493
arguments={"test": "data"},
494
+ tool=mock_tool,
495
dangerously_skip_version_check=True,
496
)
497
else:
498
tools._execute_tool(
499
slug=tool_slug,
500
arguments={"test": "data"},
501
+ tool=mock_tool,
502
)
503
504
# Verify the version matches expected
1430
Block(type="paragraph", content="Hello from composio"),
1431
],
1432
},
1433
+ tool=notion_tool,
1434
dangerously_skip_version_check=True,
1435
)
1436