@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 1,609 lines 58.9 KB
Raw
1 """Test tool execution with toolkit versions and argument serialization."""
2
3 import json
4 import typing as t
5 from unittest.mock import Mock, patch
6
7 import httpx
8 import pytest
9 from composio_client.types.tool_router.session_execute_response import (
10 SessionExecuteResponse,
11 )
12 from composio_client import ProjectAuth, omit
13 from pydantic import BaseModel, Field, RootModel
14
15 from composio.client import HttpClient
16 from composio.client.types import Tool, tool_list_response
17 from composio.core.models.base import allow_tracking
18 from composio.core.models.tools import Tools, _needs_serialization, _serialize_arguments
19 from composio.core.provider._openai import OpenAIProvider
20 from composio.exceptions import ToolVersionRequiredError
21 from tests.conftest import mock_http_client
22
23
24 @pytest.fixture(autouse=True)
25 def disable_telemetry():
26 """Disable telemetry for all tests to prevent thread issues."""
27 token = allow_tracking.set(False)
28 yield
29 allow_tracking.reset(token)
30
31
32 class TestToolExecution:
33 """Test cases for tool execution with toolkit versions."""
34
35 def create_mock_tool(self, slug: str, toolkit_slug: str) -> Tool:
36 """Create a mock tool for testing."""
37 return Tool(
38 name=f"Test {slug}",
39 slug=slug,
40 description="Test tool",
41 input_parameters={},
42 output_parameters={},
43 available_versions=["v1.0.0"],
44 version="v1.0.0",
45 scopes=[],
46 toolkit=tool_list_response.ItemToolkit(
47 name=toolkit_slug.title(), slug=toolkit_slug, logo=""
48 ),
49 deprecated=tool_list_response.ItemDeprecated(
50 available_versions=["v1.0.0"],
51 displayName=f"Test {slug}",
52 version="v1.0.0",
53 toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
54 is_deprecated=False,
55 ),
56 is_deprecated=False,
57 no_auth=False,
58 tags=[],
59 )
60
61 def test_get_raw_tool_router_meta_tools_fetches_all_pages(self):
62 """Test that ToolRouter session tools are fetched across all pages."""
63 mock_client = mock_http_client()
64 mock_provider = Mock()
65 tools = Tools(client=mock_client, provider=mock_provider)
66
67 first_tool = self.create_mock_tool("FIRST_TOOL", "github")
68 second_tool = self.create_mock_tool("SECOND_TOOL", "slack")
69 first_response = Mock(items=[first_tool], next_cursor="next_page")
70 second_response = Mock(items=[second_tool], next_cursor=None)
71 mock_client.tool_router.session.tools.side_effect = [
72 first_response,
73 second_response,
74 ]
75
76 result = tools.get_raw_tool_router_meta_tools("session_123")
77
78 assert [tool.slug for tool in result] == ["FIRST_TOOL", "SECOND_TOOL"]
79 mock_client.tool_router.session.tools.assert_any_call(
80 session_id="session_123",
81 cursor=omit,
82 limit=500,
83 )
84 mock_client.tool_router.session.tools.assert_any_call(
85 session_id="session_123",
86 limit=500,
87 cursor="next_page",
88 )
89
90 def test_tool_router_modifiers_handle_tool_without_toolkit(self):
91 """Tool Router modifier fallbacks match TypeScript in each phase."""
92 from composio.core.models._modifiers import (
93 after_execute,
94 before_execute,
95 schema_modifier,
96 )
97
98 mock_client = mock_http_client()
99 tools = Tools(client=mock_client, provider=Mock())
100 raw_tool = self.create_mock_tool("CUSTOM_TOOL", "custom").model_dump()
101 raw_tool.pop("toolkit")
102 mock_client.tool_router.session.tools.return_value = Mock(
103 items=[raw_tool], next_cursor=None
104 )
105 seen_toolkits = []
106
107 @schema_modifier
108 def observe_toolkit(tool, toolkit, schema):
109 seen_toolkits.append(("schema", toolkit))
110 return schema
111
112 @before_execute
113 def observe_before_execute(tool, toolkit, params):
114 seen_toolkits.append(("before_execute", toolkit))
115 return params
116
117 @after_execute
118 def observe_after_execute(tool, toolkit, response):
119 seen_toolkits.append(("after_execute", toolkit))
120 return response
121
122 result = tools.get_raw_tool_router_meta_tools(
123 "session_123", modifiers=[observe_toolkit]
124 )
125 mock_client.tool_router.session.execute.return_value = (
126 SessionExecuteResponse.model_validate(
127 {
128 "data": {},
129 "error": None,
130 "result_type": "completed",
131 "log_id": "log_1",
132 }
133 )
134 )
135 execute = tools._wrap_execute_tool_for_tool_router(
136 "session_123", modifiers=[observe_before_execute, observe_after_execute]
137 )
138 execute("CUSTOM_TOOL", {})
139
140 assert [tool.slug for tool in result] == ["CUSTOM_TOOL"]
141 assert seen_toolkits == [
142 ("schema", "unknown"),
143 ("before_execute", "composio"),
144 ("after_execute", "composio"),
145 ]
146
147 @pytest.mark.parametrize(
148 ("value", "expected"),
149 [
150 ("", {"metadata": {"source": "user"}}),
151 (
152 None,
153 {"attachment": None, "metadata": {"source": "user"}},
154 ),
155 ],
156 )
157 def test_tool_router_drops_only_empty_file_strings_without_mutating_arguments(
158 self, value, expected
159 ):
160 """Session execution matches direct execution for disabled auto-upload."""
161 mock_client = mock_http_client()
162 tools = Tools(client=mock_client, provider=Mock())
163 tool = self.create_mock_tool("GMAIL_CREATE_DRAFT", "gmail")
164 tool.input_parameters = {
165 "type": "object",
166 "properties": {
167 "attachment": {
168 "anyOf": [
169 {"type": "object", "file_uploadable": True},
170 {"type": "null"},
171 ]
172 },
173 "metadata": {
174 "type": "object",
175 "properties": {"source": {"type": "string"}},
176 },
177 },
178 "required": ["attachment"],
179 }
180 tools._tool_schemas[tool.slug] = tool
181 mock_client.tool_router.session.execute.return_value = (
182 SessionExecuteResponse.model_validate(
183 {
184 "data": {},
185 "error": None,
186 "result_type": "completed",
187 "log_id": "log_1",
188 }
189 )
190 )
191 execute = tools._wrap_execute_tool_for_tool_router("session_123")
192 arguments = {
193 "attachment": value,
194 "metadata": {"source": "user"},
195 }
196
197 execute(tool.slug, arguments)
198
199 sent = mock_client.tool_router.session.execute.call_args.kwargs["arguments"]
200 assert sent == expected
201 assert arguments == {
202 "attachment": value,
203 "metadata": {"source": "user"},
204 }
205 assert sent["metadata"] is not arguments["metadata"]
206
207 def test_execute_without_toolkit_runs_modifiers_and_fetches_once(self):
208 """Toolkit-less tools execute with the same fallback used by TypeScript."""
209 from composio.core.models._modifiers import after_execute, before_execute
210
211 mock_client = mock_http_client()
212 tools = Tools(
213 client=mock_client,
214 provider=Mock(),
215 toolkit_versions={"unknown": "20250101_00"},
216 )
217 raw_tool = self.create_mock_tool("CUSTOM_TOOL", "custom").model_dump()
218 raw_tool.pop("toolkit")
219 mock_client.tools.retrieve.return_value = raw_tool
220
221 execute_response = Mock()
222 execute_response.model_dump.return_value = {
223 "data": {"value": "original"},
224 "error": None,
225 "successful": True,
226 }
227 mock_client.tools.execute.return_value = execute_response
228 seen_toolkits = []
229
230 @before_execute
231 def modify_request(tool, toolkit, params):
232 seen_toolkits.append(toolkit)
233 params["arguments"]["value"] = "modified"
234 return params
235
236 @after_execute
237 def modify_response(tool, toolkit, response):
238 seen_toolkits.append(toolkit)
239 response["data"]["modified"] = True
240 return response
241
242 result = tools.execute(
243 slug="CUSTOM_TOOL",
244 arguments={"value": "original"},
245 modifiers=[modify_request, modify_response],
246 )
247
248 assert result["data"] == {"value": "original", "modified": True}
249 assert seen_toolkits == ["unknown", "unknown"]
250 mock_client.tools.retrieve.assert_called_once()
251 mock_client.tools.execute.assert_called_once_with(
252 tool_slug="CUSTOM_TOOL",
253 arguments={"value": "modified"},
254 connected_account_id=omit,
255 custom_auth_params=omit,
256 custom_connection_data=omit,
257 user_id=omit,
258 text=omit,
259 version="20250101_00",
260 )
261
262 def test_provider_execution_uses_raw_metadata_after_schema_modifier(self):
263 """Schema presentation changes must not alter execution versioning."""
264 from composio.core.models._modifiers import schema_modifier
265
266 mock_client = mock_http_client()
267 mock_provider = Mock()
268 tools = Tools(
269 client=mock_client,
270 provider=mock_provider,
271 toolkit_versions={
272 "github": "20250101_00",
273 "renamed": "20250102_00",
274 },
275 )
276 raw_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github").model_dump()
277 mock_client.tools.list.return_value = Mock(items=[raw_tool])
278 execute_response = Mock()
279 execute_response.model_dump.return_value = {
280 "data": {"result": "success"},
281 "error": None,
282 "successful": True,
283 }
284 mock_client.tools.execute.return_value = execute_response
285 wrapped = {}
286
287 def capture_tools(tools, execute_tool):
288 wrapped["tools"] = tools
289 wrapped["execute_tool"] = execute_tool
290 return tools
291
292 mock_provider.wrap_tools.side_effect = capture_tools
293
294 @schema_modifier
295 def rename_toolkit(tool, toolkit, schema):
296 schema.toolkit = tool_list_response.ItemToolkit(
297 name="Renamed", slug="renamed", logo=""
298 )
299 return schema
300
301 tools.get(
302 user_id="user_123",
303 tools=["GITHUB_GET_REPOS"],
304 modifiers=[rename_toolkit],
305 )
306 wrapped["execute_tool"]("GITHUB_GET_REPOS", {})
307
308 assert wrapped["tools"][0].toolkit.slug == "renamed"
309 mock_client.tools.retrieve.assert_not_called()
310 assert mock_client.tools.execute.call_args.kwargs["version"] == "20250101_00"
311
312 def test_tool_execution_uses_toolkit_version(self):
313 """Test that tool execution resolves toolkit version correctly."""
314 # Mock client and provider
315 mock_client = mock_http_client()
316 mock_provider = Mock()
317 mock_provider.name = "test_provider"
318
319 # Create toolkit versions configuration
320 toolkit_versions = {"github": "20251201_01", "slack": "20251201_02"}
321
322 # Create Tools instance
323 tools = Tools(
324 client=mock_client,
325 provider=mock_provider,
326 toolkit_versions=toolkit_versions,
327 )
328
329 # Create a mock GitHub tool
330 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
331
332 # Mock the get_raw_composio_tool_by_slug method
333 with patch.object(
334 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
335 ):
336 # Mock the client's execute method
337 mock_execute_response = Mock()
338 mock_execute_response.model_dump.return_value = {
339 "data": {"result": "success"},
340 "error": None,
341 "successful": True,
342 }
343 mock_client.tools.execute.return_value = mock_execute_response
344
345 # Execute the tool
346 tools._execute_tool(
347 slug="GITHUB_GET_REPOS",
348 arguments={"owner": "test", "repo": "test"},
349 tool=github_tool,
350 )
351
352 # Verify that the client was called with the resolved version
353 mock_client.tools.execute.assert_called_once()
354 call_args = mock_client.tools.execute.call_args
355
356 # Should have called with version="20251201_01" (resolved from github toolkit)
357 assert call_args.kwargs["version"] == "20251201_01"
358
359 def test_tool_execution_with_explicit_version(self):
360 """Test that explicit version overrides toolkit version."""
361 # Mock client and provider
362 mock_client = mock_http_client()
363 mock_provider = Mock()
364 mock_provider.name = "test_provider"
365
366 # Create toolkit versions configuration
367 toolkit_versions = {
368 "github": "20251201_01",
369 }
370
371 # Create Tools instance
372 tools = Tools(
373 client=mock_client,
374 provider=mock_provider,
375 toolkit_versions=toolkit_versions,
376 )
377
378 # Create a mock GitHub tool
379 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
380
381 # Mock the get_raw_composio_tool_by_slug method
382 with patch.object(
383 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
384 ):
385 # Mock the client's execute method
386 mock_execute_response = Mock()
387 mock_execute_response.model_dump.return_value = {
388 "data": {"result": "success"},
389 "error": None,
390 "successful": True,
391 }
392 mock_client.tools.execute.return_value = mock_execute_response
393
394 # Execute the tool with explicit version
395 tools._execute_tool(
396 slug="GITHUB_GET_REPOS",
397 arguments={"owner": "test", "repo": "test"},
398 tool=github_tool,
399 version="20251201_03", # Explicit version should take precedence
400 )
401
402 # Verify that the client was called with the explicit version
403 mock_client.tools.execute.assert_called_once()
404 call_args = mock_client.tools.execute.call_args
405
406 # Should have called with explicit version, not resolved from toolkit
407 assert call_args.kwargs["version"] == "20251201_03"
408
409 def test_tool_execution_unknown_toolkit_fallback(self):
410 """Test that unknown toolkit falls back to 'latest'."""
411 # Mock client and provider
412 mock_client = mock_http_client()
413 mock_provider = Mock()
414 mock_provider.name = "test_provider"
415
416 # Create toolkit versions configuration that doesn't include the tool's toolkit
417 toolkit_versions = {
418 "slack": "20251201_02" # No github version specified
419 }
420
421 # Create Tools instance
422 tools = Tools(
423 client=mock_client,
424 provider=mock_provider,
425 toolkit_versions=toolkit_versions,
426 )
427
428 # Create a mock GitHub tool (not in toolkit_versions)
429 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
430
431 # Mock the get_raw_composio_tool_by_slug method
432 with patch.object(
433 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
434 ):
435 # Mock the client's execute method
436 mock_execute_response = Mock()
437 mock_execute_response.model_dump.return_value = {
438 "data": {"result": "success"},
439 "error": None,
440 "successful": True,
441 }
442 mock_client.tools.execute.return_value = mock_execute_response
443
444 # Execute the tool with dangerously_skip_version_check since version will be 'latest'
445 tools._execute_tool(
446 slug="GITHUB_GET_REPOS",
447 arguments={"owner": "test", "repo": "test"},
448 tool=github_tool,
449 dangerously_skip_version_check=True,
450 )
451
452 # Verify that the client was called with "latest" (fallback)
453 mock_client.tools.execute.assert_called_once()
454 call_args = mock_client.tools.execute.call_args
455
456 # Should have called with "latest" since github is not in toolkit_versions
457 assert call_args.kwargs["version"] == "latest"
458
459 def test_tool_execution_with_global_version_string(self):
460 """Test that global version string is used for all toolkits."""
461 # Mock client and provider
462 mock_client = mock_http_client()
463 mock_provider = Mock()
464 mock_provider.name = "test_provider"
465
466 # Create global toolkit version (string)
467 toolkit_versions = "20251201_03"
468
469 # Create Tools instance
470 tools = Tools(
471 client=mock_client,
472 provider=mock_provider,
473 toolkit_versions=toolkit_versions,
474 )
475
476 # Create a mock GitHub tool
477 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
478
479 # Mock the get_raw_composio_tool_by_slug method
480 with patch.object(
481 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
482 ):
483 # Mock the client's execute method
484 mock_execute_response = Mock()
485 mock_execute_response.model_dump.return_value = {
486 "data": {"result": "success"},
487 "error": None,
488 "successful": True,
489 }
490 mock_client.tools.execute.return_value = mock_execute_response
491
492 # Execute the tool
493 tools._execute_tool(
494 slug="GITHUB_GET_REPOS",
495 arguments={"owner": "test", "repo": "test"},
496 tool=github_tool,
497 )
498
499 # Verify that the client was called with the global version
500 mock_client.tools.execute.assert_called_once()
501 call_args = mock_client.tools.execute.call_args
502
503 # Should have called with the global version string
504 assert call_args.kwargs["version"] == "20251201_03"
505
506 def test_tool_execution_matches_typescript_behavior(self):
507 """Test that Python execution matches TypeScript behavior exactly."""
508 # This test verifies the same logic as TypeScript:
509 # version: body.version ?? getToolkitVersion(tool.toolkit?.slug ?? 'unknown', this.toolkitVersions)
510
511 # Mock client and provider
512 mock_client = mock_http_client()
513 mock_provider = Mock()
514 mock_provider.name = "test_provider"
515
516 # Create toolkit versions like TypeScript would have
517 toolkit_versions = {
518 "github": "20251201_01",
519 "slack": "latest",
520 "notion": "20251201_05",
521 }
522
523 # Create Tools instance
524 tools = Tools(
525 client=mock_client,
526 provider=mock_provider,
527 toolkit_versions=toolkit_versions,
528 )
529
530 # Test cases matching TypeScript behavior
531 test_cases = [
532 ("GITHUB_GET_REPOS", "github", "20251201_01", False),
533 (
534 "SLACK_SEND_MESSAGE",
535 "slack",
536 "latest",
537 True,
538 ), # Need skip flag for latest
539 ("NOTION_CREATE_PAGE", "notion", "20251201_05", False),
540 (
541 "CUSTOM_TOOL",
542 "unknown_toolkit",
543 "latest",
544 True,
545 ), # Unknown toolkit fallback to latest
546 ]
547
548 for tool_slug, toolkit_slug, expected_version, needs_skip in test_cases:
549 # Create mock tool
550 mock_tool = self.create_mock_tool(tool_slug, toolkit_slug)
551
552 # Mock the get_raw_composio_tool_by_slug method
553 with patch.object(
554 tools, "get_raw_composio_tool_by_slug", return_value=mock_tool
555 ):
556 # Mock the client's execute method
557 mock_execute_response = Mock()
558 mock_execute_response.model_dump.return_value = {
559 "data": {"result": "success"},
560 "error": None,
561 "successful": True,
562 }
563 mock_client.tools.execute.return_value = mock_execute_response
564
565 # Execute the tool
566 if needs_skip:
567 tools._execute_tool(
568 slug=tool_slug,
569 arguments={"test": "data"},
570 tool=mock_tool,
571 dangerously_skip_version_check=True,
572 )
573 else:
574 tools._execute_tool(
575 slug=tool_slug,
576 arguments={"test": "data"},
577 tool=mock_tool,
578 )
579
580 # Verify the version matches expected
581 call_args = mock_client.tools.execute.call_args
582 assert call_args.kwargs["version"] == expected_version, (
583 f"Tool {tool_slug} with toolkit {toolkit_slug} should use version {expected_version}"
584 )
585
586 # Reset mock for next iteration
587 mock_client.tools.execute.reset_mock()
588
589 def test_execute_raises_error_when_version_is_latest_without_skip_flag(self):
590 """Test that execute raises ToolVersionRequiredError when version is 'latest' without skip flag."""
591 # Mock client and provider
592 mock_client = mock_http_client()
593 mock_provider = Mock()
594 mock_provider.name = "test_provider"
595
596 # Create Tools instance without toolkit versions (defaults to latest)
597 tools = Tools(
598 client=mock_client,
599 provider=mock_provider,
600 )
601
602 # Create a mock GitHub tool with proper input_parameters for file helper
603 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
604 github_tool.input_parameters = {"type": "object", "properties": {}}
605
606 # Mock the retrieve method
607 mock_client.tools.retrieve.return_value = github_tool
608
609 # Execute should raise ToolVersionRequiredError since version will be 'latest'
610 with pytest.raises(ToolVersionRequiredError) as exc_info:
611 tools.execute(
612 slug="GITHUB_GET_REPOS",
613 arguments={"owner": "test", "repo": "test"},
614 )
615
616 assert "Toolkit version not specified" in str(exc_info.value)
617
618 def test_execute_allows_latest_with_dangerously_skip_version_check(self):
619 """Test that execute allows 'latest' version when dangerously_skip_version_check is True."""
620 # Mock client and provider
621 mock_client = mock_http_client()
622 mock_provider = Mock()
623 mock_provider.name = "test_provider"
624
625 # Create Tools instance without toolkit versions (defaults to latest)
626 tools = Tools(
627 client=mock_client,
628 provider=mock_provider,
629 )
630
631 # Create a mock GitHub tool
632 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
633
634 # Mock the get_raw_composio_tool_by_slug and retrieve methods
635 with patch.object(
636 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
637 ):
638 mock_client.tools.retrieve.return_value = github_tool
639
640 # Mock the client's execute method
641 mock_execute_response = Mock()
642 mock_execute_response.model_dump.return_value = {
643 "data": {"result": "success"},
644 "error": None,
645 "successful": True,
646 }
647 mock_client.tools.execute.return_value = mock_execute_response
648
649 # Execute should succeed with dangerously_skip_version_check=True
650 result = tools.execute(
651 slug="GITHUB_GET_REPOS",
652 arguments={"owner": "test", "repo": "test"},
653 dangerously_skip_version_check=True,
654 )
655
656 # Verify execution succeeded
657 assert result["successful"] is True
658 assert result["data"] == {"result": "success"}
659
660 # Verify the client was called with version="latest"
661 mock_client.tools.execute.assert_called_once()
662 call_args = mock_client.tools.execute.call_args
663 assert call_args.kwargs["version"] == "latest"
664
665 def test_execute_with_specific_version_no_error(self):
666 """Test that execute works with specific version without raising error."""
667 # Mock client and provider
668 mock_client = mock_http_client()
669 mock_provider = Mock()
670 mock_provider.name = "test_provider"
671
672 # Create Tools instance without toolkit versions
673 tools = Tools(
674 client=mock_client,
675 provider=mock_provider,
676 )
677
678 # Create a mock GitHub tool
679 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
680
681 # Mock the get_raw_composio_tool_by_slug and retrieve methods
682 with patch.object(
683 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
684 ):
685 mock_client.tools.retrieve.return_value = github_tool
686
687 # Mock the client's execute method
688 mock_execute_response = Mock()
689 mock_execute_response.model_dump.return_value = {
690 "data": {"result": "success"},
691 "error": None,
692 "successful": True,
693 }
694 mock_client.tools.execute.return_value = mock_execute_response
695
696 # Execute with specific version should succeed
697 result = tools.execute(
698 slug="GITHUB_GET_REPOS",
699 arguments={"owner": "test", "repo": "test"},
700 version="20251201_01",
701 )
702
703 # Verify execution succeeded
704 assert result["successful"] is True
705
706 # Verify the client was called with the specific version
707 mock_client.tools.execute.assert_called_once()
708 call_args = mock_client.tools.execute.call_args
709 assert call_args.kwargs["version"] == "20251201_01"
710
711 def test_execute_uses_instance_toolkit_versions(self):
712 """Test that execute method uses instance-level toolkit versions."""
713 # Mock client and provider
714 mock_client = mock_http_client()
715 mock_provider = Mock()
716 mock_provider.name = "test_provider"
717
718 # Create toolkit versions configuration
719 toolkit_versions = {"github": "20251201_01", "slack": "20251201_02"}
720
721 # Create Tools instance with toolkit versions
722 tools = Tools(
723 client=mock_client,
724 provider=mock_provider,
725 toolkit_versions=toolkit_versions,
726 )
727
728 # Create a mock GitHub tool
729 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
730
731 # Mock the get_raw_composio_tool_by_slug and retrieve methods
732 with patch.object(
733 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
734 ):
735 mock_client.tools.retrieve.return_value = github_tool
736
737 # Mock the client's execute method
738 mock_execute_response = Mock()
739 mock_execute_response.model_dump.return_value = {
740 "data": {"result": "success"},
741 "error": None,
742 "successful": True,
743 }
744 mock_client.tools.execute.return_value = mock_execute_response
745
746 # Execute should use the configured toolkit version
747 result = tools.execute(
748 slug="GITHUB_GET_REPOS",
749 arguments={"owner": "test", "repo": "test"},
750 )
751
752 # Verify execution succeeded
753 assert result["successful"] is True
754
755 # Verify the client was called with the configured version
756 mock_client.tools.execute.assert_called_once()
757 call_args = mock_client.tools.execute.call_args
758 assert call_args.kwargs["version"] == "20251201_01"
759
760 def test_execute_version_parameter_overrides_toolkit_versions(self):
761 """Test that explicit version parameter overrides instance toolkit versions."""
762 # Mock client and provider
763 mock_client = mock_http_client()
764 mock_provider = Mock()
765 mock_provider.name = "test_provider"
766
767 # Create toolkit versions configuration
768 toolkit_versions = {"github": "20251201_01"}
769
770 # Create Tools instance with toolkit versions
771 tools = Tools(
772 client=mock_client,
773 provider=mock_provider,
774 toolkit_versions=toolkit_versions,
775 )
776
777 # Create a mock GitHub tool
778 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
779
780 # Mock the get_raw_composio_tool_by_slug and retrieve methods
781 with patch.object(
782 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
783 ):
784 mock_client.tools.retrieve.return_value = github_tool
785
786 # Mock the client's execute method
787 mock_execute_response = Mock()
788 mock_execute_response.model_dump.return_value = {
789 "data": {"result": "success"},
790 "error": None,
791 "successful": True,
792 }
793 mock_client.tools.execute.return_value = mock_execute_response
794
795 # Execute with explicit version should override instance version
796 result = tools.execute(
797 slug="GITHUB_GET_REPOS",
798 arguments={"owner": "test", "repo": "test"},
799 version="20251201_03", # Explicit version
800 )
801
802 # Verify execution succeeded
803 assert result["successful"] is True
804
805 # Verify the client was called with the explicit version, not instance version
806 mock_client.tools.execute.assert_called_once()
807 call_args = mock_client.tools.execute.call_args
808 assert call_args.kwargs["version"] == "20251201_03"
809
810 def test_execute_with_connected_account_id(self):
811 """Test that execute passes connected_account_id correctly."""
812 # Mock client and provider
813 mock_client = mock_http_client()
814 mock_provider = Mock()
815 mock_provider.name = "test_provider"
816
817 # Create Tools instance with toolkit versions
818 tools = Tools(
819 client=mock_client,
820 provider=mock_provider,
821 toolkit_versions={"github": "20251201_01"},
822 )
823
824 # Create a mock GitHub tool
825 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
826
827 # Mock the get_raw_composio_tool_by_slug and retrieve methods
828 with patch.object(
829 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
830 ):
831 mock_client.tools.retrieve.return_value = github_tool
832
833 # Mock the client's execute method
834 mock_execute_response = Mock()
835 mock_execute_response.model_dump.return_value = {
836 "data": {"result": "success"},
837 "error": None,
838 "successful": True,
839 }
840 mock_client.tools.execute.return_value = mock_execute_response
841
842 # Execute with connected_account_id
843 result = tools.execute(
844 slug="GITHUB_GET_REPOS",
845 arguments={"owner": "test", "repo": "test"},
846 connected_account_id="test-account-123",
847 )
848
849 # Verify execution succeeded
850 assert result["successful"] is True
851
852 # Verify the client was called with the connected_account_id
853 mock_client.tools.execute.assert_called_once()
854 call_args = mock_client.tools.execute.call_args
855 assert call_args.kwargs["connected_account_id"] == "test-account-123"
856
857 def test_execute_with_custom_auth_params(self):
858 """Test that execute passes custom_auth_params correctly."""
859 # Mock client and provider
860 mock_client = mock_http_client()
861 mock_provider = Mock()
862 mock_provider.name = "test_provider"
863
864 # Create Tools instance with toolkit versions
865 tools = Tools(
866 client=mock_client,
867 provider=mock_provider,
868 toolkit_versions={"github": "20251201_01"},
869 )
870
871 # Create a mock GitHub tool
872 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
873
874 # Mock the get_raw_composio_tool_by_slug and retrieve methods
875 with patch.object(
876 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
877 ):
878 mock_client.tools.retrieve.return_value = github_tool
879
880 # Mock the client's execute method
881 mock_execute_response = Mock()
882 mock_execute_response.model_dump.return_value = {
883 "data": {"result": "success"},
884 "error": None,
885 "successful": True,
886 }
887 mock_client.tools.execute.return_value = mock_execute_response
888
889 # Execute with custom_auth_params
890 custom_auth = {"api_key": "test-key"}
891 result = tools.execute(
892 slug="GITHUB_GET_REPOS",
893 arguments={"owner": "test", "repo": "test"},
894 custom_auth_params=custom_auth,
895 )
896
897 # Verify execution succeeded
898 assert result["successful"] is True
899
900 # Verify the client was called with the custom_auth_params
901 mock_client.tools.execute.assert_called_once()
902 call_args = mock_client.tools.execute.call_args
903 assert call_args.kwargs["custom_auth_params"] == custom_auth
904
905 def test_execute_with_user_id_and_text(self):
906 """Test that execute passes user_id and text parameters correctly."""
907 # Mock client and provider
908 mock_client = mock_http_client()
909 mock_provider = Mock()
910 mock_provider.name = "test_provider"
911
912 # Create Tools instance with toolkit versions
913 tools = Tools(
914 client=mock_client,
915 provider=mock_provider,
916 toolkit_versions={"github": "20251201_01"},
917 )
918
919 # Create a mock GitHub tool
920 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
921
922 # Mock the get_raw_composio_tool_by_slug and retrieve methods
923 with patch.object(
924 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
925 ):
926 mock_client.tools.retrieve.return_value = github_tool
927
928 # Mock the client's execute method
929 mock_execute_response = Mock()
930 mock_execute_response.model_dump.return_value = {
931 "data": {"result": "success"},
932 "error": None,
933 "successful": True,
934 }
935 mock_client.tools.execute.return_value = mock_execute_response
936
937 # Execute with user_id and text
938 result = tools.execute(
939 slug="GITHUB_GET_REPOS",
940 arguments={"owner": "test", "repo": "test"},
941 user_id="user-123",
942 text="Additional context",
943 )
944
945 # Verify execution succeeded
946 assert result["successful"] is True
947
948 # Verify the client was called with user_id and text
949 mock_client.tools.execute.assert_called_once()
950 call_args = mock_client.tools.execute.call_args
951 assert call_args.kwargs["user_id"] == "user-123"
952 assert call_args.kwargs["text"] == "Additional context"
953
954 def test_execute_with_modifiers_before_execute(self):
955 """Test that execute applies before_execute modifiers correctly."""
956 from composio.core.models._modifiers import before_execute
957
958 # Mock client and provider
959 mock_client = mock_http_client()
960 mock_provider = Mock()
961 mock_provider.name = "test_provider"
962
963 # Create Tools instance with toolkit versions
964 tools = Tools(
965 client=mock_client,
966 provider=mock_provider,
967 toolkit_versions={"github": "20251201_01"},
968 )
969
970 # Create a mock GitHub tool
971 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
972
973 # Mock the get_raw_composio_tool_by_slug and retrieve methods
974 with patch.object(
975 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
976 ):
977 mock_client.tools.retrieve.return_value = github_tool
978
979 # Mock the client's execute method
980 mock_execute_response = Mock()
981 mock_execute_response.model_dump.return_value = {
982 "data": {"result": "success"},
983 "error": None,
984 "successful": True,
985 }
986 mock_client.tools.execute.return_value = mock_execute_response
987
988 # Create a before_execute modifier that changes the arguments
989 def modify_arguments(tool, toolkit, params):
990 params["arguments"]["owner"] = "modified-owner"
991 return params
992
993 modifier = before_execute(modify_arguments)
994
995 # Execute with modifier
996 result = tools.execute(
997 slug="GITHUB_GET_REPOS",
998 arguments={"owner": "test", "repo": "test"},
999 modifiers=[modifier],
1000 )
1001
1002 # Verify execution succeeded
1003 assert result["successful"] is True
1004
1005 # Verify the client was called with modified arguments
1006 mock_client.tools.execute.assert_called_once()
1007 call_args = mock_client.tools.execute.call_args
1008 assert call_args.kwargs["arguments"]["owner"] == "modified-owner"
1009
1010 def test_execute_with_modifiers_after_execute(self):
1011 """Test that execute applies after_execute modifiers correctly."""
1012 from composio.core.models._modifiers import after_execute
1013
1014 # Mock client and provider
1015 mock_client = mock_http_client()
1016 mock_provider = Mock()
1017 mock_provider.name = "test_provider"
1018
1019 # Create Tools instance with toolkit versions
1020 tools = Tools(
1021 client=mock_client,
1022 provider=mock_provider,
1023 toolkit_versions={"github": "20251201_01"},
1024 )
1025
1026 # Create a mock GitHub tool
1027 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
1028
1029 # Mock the get_raw_composio_tool_by_slug and retrieve methods
1030 with patch.object(
1031 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
1032 ):
1033 mock_client.tools.retrieve.return_value = github_tool
1034
1035 # Mock the client's execute method
1036 mock_execute_response = Mock()
1037 mock_execute_response.model_dump.return_value = {
1038 "data": {"result": "success"},
1039 "error": None,
1040 "successful": True,
1041 }
1042 mock_client.tools.execute.return_value = mock_execute_response
1043
1044 # Create an after_execute modifier that modifies the response
1045 def modify_response(tool, toolkit, response):
1046 response["data"]["modified"] = True
1047 return response
1048
1049 modifier = after_execute(modify_response)
1050
1051 # Execute with modifier
1052 result = tools.execute(
1053 slug="GITHUB_GET_REPOS",
1054 arguments={"owner": "test", "repo": "test"},
1055 modifiers=[modifier],
1056 )
1057
1058 # Verify execution succeeded and response was modified
1059 assert result["successful"] is True
1060 assert result["data"]["modified"] is True
1061
1062 def test_merge_before_file_upload_scopes_by_tool(self):
1063 from composio.core.models._modifiers import (
1064 before_file_upload,
1065 merge_before_file_upload,
1066 )
1067
1068 scoped = before_file_upload(tools=["OTHER_TOOL"])(lambda p, t, k: p + "_X")
1069 fn = merge_before_file_upload([scoped], tool="MY", toolkit="gh")
1070 assert fn is not None
1071 assert (
1072 fn({"path": "/a", "source": "path", "tool": "MY", "toolkit": "gh"}) == "/a"
1073 )
1074
1075 all_tools = before_file_upload(lambda p, t, k: p + "_Y")
1076 fn2 = merge_before_file_upload([all_tools], tool="MY", toolkit="gh")
1077 assert fn2 is not None
1078 assert (
1079 fn2({"path": "/a", "source": "path", "tool": "MY", "toolkit": "gh"})
1080 == "/a_Y"
1081 )
1082
1083 def test_merge_before_file_upload_chains_modifiers(self):
1084 from composio.core.models._modifiers import (
1085 before_file_upload,
1086 merge_before_file_upload,
1087 )
1088
1089 m1 = before_file_upload(lambda p, t, k: f"{p}|1")
1090 m2 = before_file_upload(lambda p, t, k: f"{p}|2")
1091 fn = merge_before_file_upload([m1, m2], tool="T", toolkit="k")
1092 assert fn is not None
1093 assert (
1094 fn({"path": "p", "source": "path", "tool": "T", "toolkit": "k"}) == "p|1|2"
1095 )
1096
1097 def test_before_file_upload_context_form_receives_source(self):
1098 """New-form hooks (single ``context`` arg) see ``source`` in the context."""
1099 from composio.core.models._modifiers import (
1100 before_file_upload,
1101 merge_before_file_upload,
1102 )
1103
1104 seen: t.Dict[str, t.Any] = {}
1105
1106 def hook(ctx):
1107 seen.update(ctx)
1108 return ctx["path"] + "!"
1109
1110 m = before_file_upload(hook)
1111 fn = merge_before_file_upload([m], tool="T", toolkit="k")
1112 assert fn is not None
1113
1114 out = fn({"path": "/x", "source": "path", "tool": "T", "toolkit": "k"})
1115 assert out == "/x!"
1116 assert seen == {"path": "/x", "source": "path", "tool": "T", "toolkit": "k"}
1117
1118 seen.clear()
1119 out = fn(
1120 {
1121 "path": "https://example.com/a.pdf",
1122 "source": "url",
1123 "tool": "T",
1124 "toolkit": "k",
1125 }
1126 )
1127 assert out == "https://example.com/a.pdf!"
1128 assert seen["source"] == "url"
1129
1130 def test_before_file_upload_legacy_3arg_form_still_works(self):
1131 """Hooks declared as ``(path, tool, toolkit)`` keep working unchanged."""
1132 from composio.core.models._modifiers import (
1133 before_file_upload,
1134 merge_before_file_upload,
1135 )
1136
1137 def legacy(path, tool, toolkit):
1138 return f"{path}@{tool}/{toolkit}"
1139
1140 m = before_file_upload(legacy)
1141 fn = merge_before_file_upload([m], tool="T", toolkit="k")
1142 assert fn is not None
1143 # Driver always passes context form; adapter unwraps to positional args.
1144 assert (
1145 fn({"path": "/x", "source": "path", "tool": "T", "toolkit": "k"})
1146 == "/x@T/k"
1147 )
1148
1149 def test_before_file_upload_chain_preserves_source(self):
1150 """``source`` stays stable as the ``path`` is rewritten down the chain."""
1151 from composio.core.models._modifiers import (
1152 before_file_upload,
1153 merge_before_file_upload,
1154 )
1155
1156 seen_sources: t.List[str] = []
1157
1158 def h1(ctx):
1159 seen_sources.append(ctx["source"])
1160 return ctx["path"] + "|1"
1161
1162 def h2(ctx):
1163 seen_sources.append(ctx["source"])
1164 return ctx["path"] + "|2"
1165
1166 fn = merge_before_file_upload(
1167 [before_file_upload(h1), before_file_upload(h2)], tool="T", toolkit="k"
1168 )
1169 assert fn is not None
1170 out = fn({"path": "p", "source": "url", "tool": "T", "toolkit": "k"})
1171 assert out == "p|1|2"
1172 assert seen_sources == ["url", "url"]
1173
1174 def test_before_file_upload_abort_propagates_as_false(self):
1175 from composio.core.models._modifiers import (
1176 before_file_upload,
1177 merge_before_file_upload,
1178 )
1179
1180 def abort(ctx):
1181 return False
1182
1183 fn = merge_before_file_upload(
1184 [before_file_upload(abort)], tool="T", toolkit="k"
1185 )
1186 assert fn is not None
1187 assert (
1188 fn({"path": "/x", "source": "path", "tool": "T", "toolkit": "k"}) is False
1189 )
1190
1191 def test_execute_includes_before_file_upload_modifier_in_compose(
1192 self,
1193 ):
1194 """``before_file_upload`` in ``modifiers`` is composed into substitute_file_uploads."""
1195 from composio.core.models._modifiers import before_file_upload
1196
1197 mock_client = mock_http_client()
1198 mock_provider = Mock()
1199 mock_provider.name = "test_provider"
1200
1201 tools = Tools(
1202 client=mock_client,
1203 provider=mock_provider,
1204 toolkit_versions={"github": "20251201_01"},
1205 dangerously_allow_auto_upload_download_files=True,
1206 )
1207
1208 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
1209
1210 with patch.object(
1211 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
1212 ):
1213 mock_client.tools.retrieve.return_value = github_tool
1214
1215 mock_execute_response = Mock()
1216 mock_execute_response.model_dump.return_value = {
1217 "data": {"result": "success"},
1218 "error": None,
1219 "successful": True,
1220 }
1221 mock_client.tools.execute.return_value = mock_execute_response
1222
1223 cap: t.Dict = {}
1224
1225 def capture_sub(**kwargs):
1226 cap["bfu"] = kwargs.get("before_file_upload")
1227 return kwargs["request"]
1228
1229 with patch.object(
1230 tools._file_helper, "substitute_file_uploads", side_effect=capture_sub
1231 ):
1232 mod = before_file_upload(lambda p, t, k: f"{p}>")
1233 tools.execute(
1234 slug="GITHUB_GET_REPOS",
1235 arguments={"owner": "a", "repo": "b"},
1236 modifiers=[mod],
1237 )
1238
1239 bfu = cap.get("bfu")
1240 assert bfu is not None
1241 assert (
1242 bfu(
1243 {
1244 "path": "/p",
1245 "source": "path",
1246 "tool": "GITHUB_GET_REPOS",
1247 "toolkit": "github",
1248 }
1249 )
1250 == "/p>"
1251 )
1252
1253 def test_execute_with_environment_variable_toolkit_version(self):
1254 """Test that execute uses environment variable for toolkit version."""
1255 import os
1256
1257 # Mock client and provider
1258 mock_client = mock_http_client()
1259 mock_provider = Mock()
1260 mock_provider.name = "test_provider"
1261
1262 # Set environment variable for github toolkit version
1263 os.environ["COMPOSIO_TOOLKIT_VERSION_GITHUB"] = "20251201_08"
1264
1265 try:
1266 # Create Tools instance with explicit toolkit versions that match the env var
1267 # This simulates what happens when get_toolkit_version reads the env var
1268 tools = Tools(
1269 client=mock_client,
1270 provider=mock_provider,
1271 toolkit_versions={"github": "20251201_08"}, # This matches the env var
1272 )
1273
1274 # Create a mock GitHub tool with proper structure
1275 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
1276 github_tool.input_parameters = {"type": "object", "properties": {}}
1277
1278 # Mock the retrieve method
1279 mock_client.tools.retrieve.return_value = github_tool
1280
1281 # Mock the client's execute method
1282 mock_execute_response = Mock()
1283 mock_execute_response.model_dump.return_value = {
1284 "data": {"result": "success"},
1285 "error": None,
1286 "successful": True,
1287 }
1288 mock_client.tools.execute.return_value = mock_execute_response
1289
1290 # Execute should use the environment variable version
1291 result = tools.execute(
1292 slug="GITHUB_GET_REPOS",
1293 arguments={"owner": "test", "repo": "test"},
1294 )
1295
1296 # Verify execution succeeded
1297 assert result["successful"] is True
1298
1299 # Verify the client was called with the env variable version
1300 mock_client.tools.execute.assert_called_once()
1301 call_args = mock_client.tools.execute.call_args
1302 assert call_args.kwargs["version"] == "20251201_08"
1303 finally:
1304 # Clean up environment variable
1305 if "COMPOSIO_TOOLKIT_VERSION_GITHUB" in os.environ:
1306 del os.environ["COMPOSIO_TOOLKIT_VERSION_GITHUB"]
1307
1308 def test_execute_raises_error_with_latest_via_env_variable(self):
1309 """Test that execute raises error when env variable sets version to 'latest'."""
1310 import os
1311
1312 # Mock client and provider
1313 mock_client = mock_http_client()
1314 mock_provider = Mock()
1315 mock_provider.name = "test_provider"
1316
1317 # Set environment variable for github toolkit version to 'latest'
1318 os.environ["COMPOSIO_TOOLKIT_VERSION_GITHUB"] = "latest"
1319
1320 try:
1321 # Create Tools instance without explicit toolkit versions
1322 tools = Tools(
1323 client=mock_client,
1324 provider=mock_provider,
1325 )
1326
1327 # Create a mock GitHub tool with proper structure
1328 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
1329 github_tool.input_parameters = {"type": "object", "properties": {}}
1330
1331 # Mock the retrieve method
1332 mock_client.tools.retrieve.return_value = github_tool
1333
1334 # Execute should raise ToolVersionRequiredError since version is 'latest'
1335 with pytest.raises(ToolVersionRequiredError):
1336 tools.execute(
1337 slug="GITHUB_GET_REPOS",
1338 arguments={"owner": "test", "repo": "test"},
1339 )
1340 finally:
1341 # Clean up environment variable
1342 if "COMPOSIO_TOOLKIT_VERSION_GITHUB" in os.environ:
1343 del os.environ["COMPOSIO_TOOLKIT_VERSION_GITHUB"]
1344
1345 def test_execute_with_custom_connection_data(self):
1346 """Test that execute passes custom_connection_data correctly."""
1347 # Mock client and provider
1348 mock_client = mock_http_client()
1349 mock_provider = Mock()
1350 mock_provider.name = "test_provider"
1351
1352 # Create Tools instance with toolkit versions
1353 tools = Tools(
1354 client=mock_client,
1355 provider=mock_provider,
1356 toolkit_versions={"github": "20251201_01"},
1357 )
1358
1359 # Create a mock GitHub tool
1360 github_tool = self.create_mock_tool("GITHUB_GET_REPOS", "github")
1361
1362 # Mock the get_raw_composio_tool_by_slug and retrieve methods
1363 with patch.object(
1364 tools, "get_raw_composio_tool_by_slug", return_value=github_tool
1365 ):
1366 mock_client.tools.retrieve.return_value = github_tool
1367
1368 # Mock the client's execute method
1369 mock_execute_response = Mock()
1370 mock_execute_response.model_dump.return_value = {
1371 "data": {"result": "success"},
1372 "error": None,
1373 "successful": True,
1374 }
1375 mock_client.tools.execute.return_value = mock_execute_response
1376
1377 # Execute with custom_connection_data
1378 custom_connection = {"token": "custom-token"}
1379 result = tools.execute(
1380 slug="GITHUB_GET_REPOS",
1381 arguments={"owner": "test", "repo": "test"},
1382 custom_connection_data=custom_connection,
1383 )
1384
1385 # Verify execution succeeded
1386 assert result["successful"] is True
1387
1388 # Verify the client was called with custom_connection_data
1389 mock_client.tools.execute.assert_called_once()
1390 call_args = mock_client.tools.execute.call_args
1391 assert call_args.kwargs["custom_connection_data"] == custom_connection
1392
1393
1394 class TestSerializeArguments:
1395 """Test _serialize_arguments and _needs_serialization helpers."""
1396
1397 def test_plain_dict_returns_same_object(self):
1398 """When no Pydantic models are present, return the original dict (no copy)."""
1399 args = {"page_id": "abc-123", "title": "Hello"}
1400 result = _serialize_arguments(args)
1401 assert result is args
1402
1403 def test_needs_serialization_false_for_primitives(self):
1404 assert not _needs_serialization({"s": "hello", "i": 42, "b": True})
1405
1406 def test_needs_serialization_true_for_basemodel(self):
1407 class M(BaseModel):
1408 x: int
1409
1410 assert _needs_serialization({"m": M(x=1)})
1411
1412 def test_basemodel_serialized(self):
1413 class Block(BaseModel):
1414 type: str
1415 content: str
1416
1417 result = _serialize_arguments(
1418 {"block": Block(type="paragraph", content="Hello")}
1419 )
1420 assert result == {"block": {"type": "paragraph", "content": "Hello"}}
1421
1422 def test_rootmodel_list_serialized(self):
1423 class Block(BaseModel):
1424 type: str
1425
1426 class BlockList(RootModel[list[Block]]):
1427 pass
1428
1429 blocks = BlockList([Block(type="paragraph"), Block(type="heading_1")])
1430 result = _serialize_arguments({"children": blocks})
1431 assert result == {"children": [{"type": "paragraph"}, {"type": "heading_1"}]}
1432
1433 def test_list_of_models_serialized(self):
1434 class Block(BaseModel):
1435 type: str
1436
1437 result = _serialize_arguments(
1438 {"children": [Block(type="paragraph"), Block(type="heading_1")]}
1439 )
1440 assert result == {"children": [{"type": "paragraph"}, {"type": "heading_1"}]}
1441
1442 def test_nested_dict_with_models(self):
1443 class Inner(BaseModel):
1444 value: int
1445
1446 result = _serialize_arguments({"outer": {"inner": Inner(value=42)}})
1447 assert result == {"outer": {"inner": {"value": 42}}}
1448
1449 @pytest.mark.parametrize("shape", ["direct", "list", "dict", "root", "tuple"])
1450 @pytest.mark.parametrize("serialization_alias", [None, "label"])
1451 def test_execute_preserves_model_aliases_on_the_wire(
1452 self, shape, serialization_alias
1453 ):
1454 """Model arguments must use the same names as their JSON schema."""
1455
1456 class Block(BaseModel):
1457 display_name: str = Field(
1458 alias="displayName", serialization_alias=serialization_alias
1459 )
1460
1461 block = Block(displayName="paragraph")
1462 expected = {serialization_alias or "displayName": "paragraph"}
1463 schema = Block.model_json_schema(mode="serialization")
1464 if shape in ("list", "tuple", "root"):
1465 value = [block]
1466 expected = [expected]
1467 schema = {"type": "array", "items": schema}
1468 if shape == "tuple":
1469 value = tuple(value)
1470 elif shape == "root":
1471 value = RootModel[list[Block]](value)
1472 elif shape == "dict":
1473 value = {"child": block}
1474 expected = {"child": expected}
1475 schema = {"type": "object", "properties": {"child": schema}}
1476 else:
1477 value = block
1478
1479 tool = TestToolExecution().create_mock_tool("TEST_TOOL", "test")
1480 tool.input_parameters = {
1481 "type": "object",
1482 "properties": {"payload": schema},
1483 }
1484 bodies = []
1485
1486 def respond(request):
1487 if request.method == "GET":
1488 return httpx.Response(200, json=tool.model_dump(mode="json"))
1489 bodies.append(json.loads(request.content))
1490 return httpx.Response(
1491 200, json={"data": {}, "error": None, "successful": True}
1492 )
1493
1494 with httpx.Client(transport=httpx.MockTransport(respond)) as transport:
1495 client = HttpClient(
1496 provider="openai",
1497 auth=ProjectAuth("test-key"),
1498 base_url="https://composio.invalid",
1499 http_client=transport,
1500 )
1501 tools = Tools(client=client, provider=OpenAIProvider())
1502 result = tools.execute(
1503 "TEST_TOOL",
1504 arguments={"payload": value},
1505 version="v1.0.0",
1506 )
1507
1508 assert result["successful"] is True
1509 assert len(bodies) == 1
1510 assert bodies[0]["arguments"] == {"payload": expected}
1511
1512 def test_model_aliases_preserve_defaults_and_nulls(self):
1513 class Child(BaseModel):
1514 display_name: str = Field(alias="displayName")
1515 optional_value: str | None = Field(default=None, alias="optionalValue")
1516 default_value: int = Field(default=42, alias="defaultValue")
1517
1518 class Parent(BaseModel):
1519 child_model: Child = Field(alias="childModel")
1520
1521 child = Child(displayName="paragraph")
1522 parent = Parent(childModel=child)
1523
1524 assert _serialize_arguments({"payload": parent}) == {
1525 "payload": {
1526 "childModel": {
1527 "displayName": "paragraph",
1528 "optionalValue": None,
1529 "defaultValue": 42,
1530 }
1531 }
1532 }
1533 assert parent.child_model is child
1534 assert child.model_fields_set == {"display_name"}
1535
1536 def test_execute_tool_serializes_pydantic_arguments(self):
1537 """Regression test for PLEN-1514: Pydantic models in arguments must be
1538 serialized to plain dicts before being sent to the API."""
1539 mock_client = mock_http_client()
1540 mock_provider = Mock()
1541 mock_provider.name = "test_provider"
1542
1543 tools = Tools(
1544 client=mock_client,
1545 provider=mock_provider,
1546 toolkit_versions={"notion": "latest"},
1547 )
1548
1549 notion_tool = Tool(
1550 name="Test NOTION_REPLACE_PAGE_CONTENT",
1551 slug="NOTION_REPLACE_PAGE_CONTENT",
1552 description="Test tool",
1553 input_parameters={},
1554 output_parameters={},
1555 available_versions=["v1.0.0"],
1556 version="v1.0.0",
1557 scopes=[],
1558 toolkit=tool_list_response.ItemToolkit(
1559 name="Notion", slug="notion", logo=""
1560 ),
1561 deprecated=tool_list_response.ItemDeprecated(
1562 available_versions=["v1.0.0"],
1563 displayName="Test NOTION_REPLACE_PAGE_CONTENT",
1564 version="v1.0.0",
1565 toolkit=tool_list_response.ItemDeprecatedToolkit(logo=""),
1566 is_deprecated=False,
1567 ),
1568 is_deprecated=False,
1569 no_auth=False,
1570 tags=[],
1571 )
1572
1573 class Block(BaseModel):
1574 type: str
1575 content: str
1576
1577 with patch.object(
1578 tools, "get_raw_composio_tool_by_slug", return_value=notion_tool
1579 ):
1580 mock_response = Mock()
1581 mock_response.model_dump.return_value = {
1582 "data": {"result": "success"},
1583 "error": None,
1584 "successful": True,
1585 }
1586 mock_client.tools.execute.return_value = mock_response
1587
1588 tools._execute_tool(
1589 slug="NOTION_REPLACE_PAGE_CONTENT",
1590 arguments={
1591 "page_id": "abc-123",
1592 "children": [
1593 Block(type="paragraph", content="Hello from composio"),
1594 ],
1595 },
1596 tool=notion_tool,
1597 dangerously_skip_version_check=True,
1598 )
1599
1600 call_args = mock_client.tools.execute.call_args
1601 sent_arguments = call_args.kwargs["arguments"]
1602 assert sent_arguments == {
1603 "page_id": "abc-123",
1604 "children": [
1605 {"type": "paragraph", "content": "Hello from composio"},
1606 ],
1607 }
1608 for child in sent_arguments["children"]:
1609 assert not isinstance(child, BaseModel)