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
| 1 | """Tests for provider-facing tool schema aliases.""" |
| 2 | |
| 3 | import asyncio |
| 4 | import copy |
| 5 | import importlib.util |
| 6 | import inspect |
| 7 | import re |
| 8 | import sys |
| 9 | import types |
| 10 | from pathlib import Path |
| 11 | from types import SimpleNamespace |
| 12 | from unittest.mock import Mock |
| 13 | |
| 14 | import pytest |
| 15 | |
| 16 | from composio.exceptions import InvalidSchemaError |
| 17 | from composio.utils.shared import ( |
| 18 | alias_tool_input_schema, |
| 19 | json_schema_to_model, |
| 20 | substitute_reserved_python_keywords, |
| 21 | ) |
| 22 | |
| 23 | PYTHON_ROOT = Path(__file__).resolve().parents[1] |
| 24 | ANTHROPIC_PROPERTY_RE = re.compile(r"^[a-zA-Z0-9_.-]{1,64}$") |
| 25 | |
| 26 | |
| 27 | def _load_module(monkeypatch, module_name: str, path: Path): |
| 28 | spec = importlib.util.spec_from_file_location(module_name, path) |
| 29 | assert spec is not None |
| 30 | assert spec.loader is not None |
| 31 | module = importlib.util.module_from_spec(spec) |
| 32 | monkeypatch.setitem(sys.modules, module_name, module) |
| 33 | spec.loader.exec_module(module) |
| 34 | return module |
| 35 | |
| 36 | |
| 37 | def test_alias_tool_input_schema_restores_nested_aliases_without_mutating_schema(): |
| 38 | schema = { |
| 39 | "type": "object", |
| 40 | "properties": { |
| 41 | "from": {"type": "string"}, |
| 42 | "payload": { |
| 43 | "type": "object", |
| 44 | "properties": {"class": {"type": "string"}}, |
| 45 | "required": ["class"], |
| 46 | }, |
| 47 | "items": { |
| 48 | "type": "array", |
| 49 | "items": { |
| 50 | "type": "object", |
| 51 | "properties": {"for": {"type": "string"}}, |
| 52 | "required": ["for"], |
| 53 | }, |
| 54 | }, |
| 55 | }, |
| 56 | "required": ["from", "payload"], |
| 57 | } |
| 58 | original = copy.deepcopy(schema) |
| 59 | |
| 60 | aliases = alias_tool_input_schema(schema) |
| 61 | |
| 62 | assert schema == original |
| 63 | assert list(aliases.schema["properties"]) == ["from_rs", "payload", "items"] |
| 64 | assert aliases.schema["required"] == ["from_rs", "payload"] |
| 65 | assert aliases.schema["properties"]["payload"]["required"] == ["class_rs"] |
| 66 | assert aliases.schema["properties"]["items"]["items"]["required"] == ["for_rs"] |
| 67 | |
| 68 | arguments = { |
| 69 | "from_rs": "sender@example.com", |
| 70 | "payload": {"class_rs": "primary"}, |
| 71 | "items": [{"for_rs": "recipient@example.com"}], |
| 72 | } |
| 73 | assert aliases.restore_arguments(arguments) == { |
| 74 | "from": "sender@example.com", |
| 75 | "payload": {"class": "primary"}, |
| 76 | "items": [{"for": "recipient@example.com"}], |
| 77 | } |
| 78 | |
| 79 | |
| 80 | def test_alias_tool_input_schema_rejects_duplicate_aliases(): |
| 81 | schema = { |
| 82 | "type": "object", |
| 83 | "properties": { |
| 84 | "from": {"type": "string"}, |
| 85 | "from_rs": {"type": "string"}, |
| 86 | }, |
| 87 | } |
| 88 | |
| 89 | with pytest.raises(InvalidSchemaError, match="duplicate Python parameter alias"): |
| 90 | alias_tool_input_schema(schema) |
| 91 | |
| 92 | |
| 93 | def test_legacy_keyword_helpers_use_tool_schema_aliases(): |
| 94 | long_name = "x" * 80 |
| 95 | schema = { |
| 96 | "title": "ODataParams", |
| 97 | "type": "object", |
| 98 | "properties": { |
| 99 | "$top": {"type": "integer"}, |
| 100 | "@microsoft.graph.conflictBehavior": {"type": "string"}, |
| 101 | long_name: {"type": "string"}, |
| 102 | }, |
| 103 | "required": ["$top", long_name], |
| 104 | } |
| 105 | |
| 106 | aliased_schema, aliases = substitute_reserved_python_keywords(schema) |
| 107 | |
| 108 | aliased_names = list(aliased_schema["properties"]) |
| 109 | assert aliased_names[0] == "param_top" |
| 110 | assert aliased_names[1] == "param_microsoft_graph_conflictBehavior" |
| 111 | assert len(aliased_names[2]) == 64 |
| 112 | assert all(ANTHROPIC_PROPERTY_RE.fullmatch(name) for name in aliased_names) |
| 113 | assert aliased_schema["required"] == ["param_top", aliased_names[2]] |
| 114 | assert aliases["param_top"] == "$top" |
| 115 | assert aliases["param_microsoft_graph_conflictBehavior"] == ( |
| 116 | "@microsoft.graph.conflictBehavior" |
| 117 | ) |
| 118 | assert aliases[aliased_names[2]] == long_name |
| 119 | model = json_schema_to_model(aliased_schema) |
| 120 | assert "param_top" in model.model_fields |
| 121 | |
| 122 | |
| 123 | def test_alias_tool_input_schema_dereferences_refs_before_aliasing(): |
| 124 | schema = { |
| 125 | "$ref": "#/$defs/SearchParams", |
| 126 | "$defs": { |
| 127 | "SearchParams": { |
| 128 | "type": "object", |
| 129 | "properties": { |
| 130 | "filter": {"$ref": "#/$defs/ODataFilter"}, |
| 131 | }, |
| 132 | "required": ["filter"], |
| 133 | }, |
| 134 | "ODataFilter": { |
| 135 | "type": "object", |
| 136 | "properties": {"$top": {"type": "integer"}}, |
| 137 | "required": ["$top"], |
| 138 | }, |
| 139 | }, |
| 140 | } |
| 141 | |
| 142 | aliases = alias_tool_input_schema(schema) |
| 143 | |
| 144 | assert "$defs" not in aliases.schema |
| 145 | assert aliases.schema["required"] == ["filter"] |
| 146 | filter_schema = aliases.schema["properties"]["filter"] |
| 147 | assert list(filter_schema["properties"]) == ["param_top"] |
| 148 | assert filter_schema["required"] == ["param_top"] |
| 149 | assert aliases.restore_arguments({"filter": {"param_top": 10}}) == { |
| 150 | "filter": {"$top": 10} |
| 151 | } |
| 152 | |
| 153 | |
| 154 | def test_gemini_manual_response_restores_provider_visible_aliases(monkeypatch): |
| 155 | google_module = types.ModuleType("google") |
| 156 | genai_module = types.ModuleType("google.genai") |
| 157 | genai_types_module = types.ModuleType("google.genai.types") |
| 158 | |
| 159 | class FunctionResponse: |
| 160 | def __init__(self, name, response): |
| 161 | self.name = name |
| 162 | self.response = response |
| 163 | |
| 164 | class Part: |
| 165 | def __init__(self, function_response): |
| 166 | self.function_response = function_response |
| 167 | |
| 168 | genai_types_module.FunctionResponse = FunctionResponse |
| 169 | genai_types_module.Part = Part |
| 170 | genai_module.types = genai_types_module |
| 171 | google_module.genai = genai_module |
| 172 | monkeypatch.setitem(sys.modules, "google", google_module) |
| 173 | monkeypatch.setitem(sys.modules, "google.genai", genai_module) |
| 174 | monkeypatch.setitem(sys.modules, "google.genai.types", genai_types_module) |
| 175 | |
| 176 | provider_module = _load_module( |
| 177 | monkeypatch, |
| 178 | "test_composio_gemini_provider", |
| 179 | PYTHON_ROOT / "providers/gemini/composio_gemini/provider.py", |
| 180 | ) |
| 181 | provider = provider_module.GeminiProvider() |
| 182 | execute_tool = Mock(return_value={"successful": True, "data": {"ok": True}}) |
| 183 | tool = SimpleNamespace( |
| 184 | slug="TOOL_WITH_RESERVED", |
| 185 | description="Tool with reserved parameters", |
| 186 | input_parameters={ |
| 187 | "type": "object", |
| 188 | "properties": {"for": {"type": "string"}}, |
| 189 | "required": ["for"], |
| 190 | }, |
| 191 | ) |
| 192 | provider.wrap_tools([tool], execute_tool) |
| 193 | |
| 194 | response = SimpleNamespace( |
| 195 | candidates=[ |
| 196 | SimpleNamespace( |
| 197 | content=SimpleNamespace( |
| 198 | parts=[ |
| 199 | SimpleNamespace( |
| 200 | function_call=SimpleNamespace( |
| 201 | name="TOOL_WITH_RESERVED", |
| 202 | args={"for_rs": "recipient@example.com"}, |
| 203 | ) |
| 204 | ) |
| 205 | ] |
| 206 | ) |
| 207 | ) |
| 208 | ] |
| 209 | ) |
| 210 | |
| 211 | function_responses, executed = provider.handle_response(response) |
| 212 | |
| 213 | assert executed is True |
| 214 | assert function_responses[0].function_response.name == "TOOL_WITH_RESERVED" |
| 215 | execute_tool.assert_called_once_with( |
| 216 | slug="TOOL_WITH_RESERVED", arguments={"for": "recipient@example.com"} |
| 217 | ) |
| 218 | |
| 219 | |
| 220 | def test_google_adk_wrap_tool_aliases_signature_and_restores_arguments(monkeypatch): |
| 221 | google_module = types.ModuleType("google") |
| 222 | adk_module = types.ModuleType("google.adk") |
| 223 | tools_module = types.ModuleType("google.adk.tools") |
| 224 | |
| 225 | class FunctionTool: |
| 226 | def __init__(self, func): |
| 227 | self.func = func |
| 228 | |
| 229 | tools_module.FunctionTool = FunctionTool |
| 230 | adk_module.tools = tools_module |
| 231 | google_module.adk = adk_module |
| 232 | monkeypatch.setitem(sys.modules, "google", google_module) |
| 233 | monkeypatch.setitem(sys.modules, "google.adk", adk_module) |
| 234 | monkeypatch.setitem(sys.modules, "google.adk.tools", tools_module) |
| 235 | |
| 236 | provider_module = _load_module( |
| 237 | monkeypatch, |
| 238 | "test_composio_google_adk_provider", |
| 239 | PYTHON_ROOT / "providers/google_adk/composio_google_adk/provider.py", |
| 240 | ) |
| 241 | provider = provider_module.GoogleAdkProvider() |
| 242 | execute_tool = Mock(return_value={"successful": True, "data": {"ok": True}}) |
| 243 | tool = SimpleNamespace( |
| 244 | slug="TOOL_WITH_RESERVED", |
| 245 | description="Tool with reserved parameters", |
| 246 | input_parameters={ |
| 247 | "type": "object", |
| 248 | "properties": { |
| 249 | "from": {"type": "string", "description": "Sender"}, |
| 250 | "limit": {"type": "integer", "description": "Limit"}, |
| 251 | }, |
| 252 | "required": ["from"], |
| 253 | }, |
| 254 | ) |
| 255 | |
| 256 | wrapped = provider.wrap_tool(tool, execute_tool) |
| 257 | |
| 258 | assert list(inspect.signature(wrapped.func).parameters) == ["from_rs", "limit"] |
| 259 | assert "from_rs: Sender" in (wrapped.func.__doc__ or "") |
| 260 | wrapped.func(from_rs="sender@example.com", limit=10) |
| 261 | execute_tool.assert_called_once_with( |
| 262 | slug="TOOL_WITH_RESERVED", |
| 263 | arguments={"from": "sender@example.com", "limit": 10}, |
| 264 | ) |
| 265 | |
| 266 | |
| 267 | def test_anthropic_wrap_tool_aliases_schema_and_restores_arguments(monkeypatch): |
| 268 | anthropic_module = types.ModuleType("anthropic") |
| 269 | types_module = types.ModuleType("anthropic.types") |
| 270 | beta_module = types.ModuleType("anthropic.types.beta") |
| 271 | beta_tool_use_module = types.ModuleType("anthropic.types.beta.beta_tool_use_block") |
| 272 | cache_control_module = types.ModuleType( |
| 273 | "anthropic.types.cache_control_ephemeral_param" |
| 274 | ) |
| 275 | message_module = types.ModuleType("anthropic.types.message") |
| 276 | tool_param_module = types.ModuleType("anthropic.types.tool_param") |
| 277 | tool_use_module = types.ModuleType("anthropic.types.tool_use_block") |
| 278 | |
| 279 | class BetaToolUseBlock: |
| 280 | pass |
| 281 | |
| 282 | class Message: |
| 283 | def __init__(self, **kwargs): |
| 284 | self.__dict__.update(kwargs) |
| 285 | |
| 286 | class ToolUseBlock: |
| 287 | pass |
| 288 | |
| 289 | beta_tool_use_module.BetaToolUseBlock = BetaToolUseBlock |
| 290 | cache_control_module.CacheControlEphemeralParam = dict |
| 291 | message_module.Message = Message |
| 292 | tool_param_module.ToolParam = dict |
| 293 | tool_use_module.ToolUseBlock = ToolUseBlock |
| 294 | monkeypatch.setitem(sys.modules, "anthropic", anthropic_module) |
| 295 | monkeypatch.setitem(sys.modules, "anthropic.types", types_module) |
| 296 | monkeypatch.setitem(sys.modules, "anthropic.types.beta", beta_module) |
| 297 | monkeypatch.setitem( |
| 298 | sys.modules, |
| 299 | "anthropic.types.beta.beta_tool_use_block", |
| 300 | beta_tool_use_module, |
| 301 | ) |
| 302 | monkeypatch.setitem( |
| 303 | sys.modules, |
| 304 | "anthropic.types.cache_control_ephemeral_param", |
| 305 | cache_control_module, |
| 306 | ) |
| 307 | monkeypatch.setitem(sys.modules, "anthropic.types.message", message_module) |
| 308 | monkeypatch.setitem(sys.modules, "anthropic.types.tool_param", tool_param_module) |
| 309 | monkeypatch.setitem(sys.modules, "anthropic.types.tool_use_block", tool_use_module) |
| 310 | |
| 311 | provider_module = _load_module( |
| 312 | monkeypatch, |
| 313 | "test_composio_anthropic_provider", |
| 314 | PYTHON_ROOT / "providers/anthropic/composio_anthropic/provider.py", |
| 315 | ) |
| 316 | provider = provider_module.AnthropicProvider() |
| 317 | provider.execute_tool = Mock(return_value={"successful": True}) |
| 318 | long_name = "x" * 80 |
| 319 | tool = SimpleNamespace( |
| 320 | slug="TOOL_WITH_ODATA", |
| 321 | description="Tool with OData parameters", |
| 322 | input_parameters={ |
| 323 | "type": "object", |
| 324 | "properties": { |
| 325 | "$top": {"type": "integer"}, |
| 326 | "@microsoft.graph.conflictBehavior": {"type": "string"}, |
| 327 | long_name: {"type": "string"}, |
| 328 | }, |
| 329 | "required": ["$top", long_name], |
| 330 | }, |
| 331 | ) |
| 332 | |
| 333 | wrapped = provider.wrap_tool(tool) |
| 334 | |
| 335 | aliased_names = list(wrapped["input_schema"]["properties"]) |
| 336 | assert aliased_names[0] == "param_top" |
| 337 | assert aliased_names[1] == "param_microsoft_graph_conflictBehavior" |
| 338 | assert len(aliased_names[2]) == 64 |
| 339 | assert all(ANTHROPIC_PROPERTY_RE.fullmatch(name) for name in aliased_names) |
| 340 | |
| 341 | tool_call = SimpleNamespace( |
| 342 | id="use-odata", |
| 343 | name="TOOL_WITH_ODATA", |
| 344 | input={ |
| 345 | "param_top": 10, |
| 346 | "param_microsoft_graph_conflictBehavior": "rename", |
| 347 | aliased_names[2]: "value", |
| 348 | }, |
| 349 | ) |
| 350 | provider.execute_tool_call(user_id="user", tool_call=tool_call) |
| 351 | |
| 352 | provider.execute_tool.assert_called_once_with( |
| 353 | slug="TOOL_WITH_ODATA", |
| 354 | arguments={ |
| 355 | "$top": 10, |
| 356 | "@microsoft.graph.conflictBehavior": "rename", |
| 357 | long_name: "value", |
| 358 | }, |
| 359 | modifiers=None, |
| 360 | user_id="user", |
| 361 | ) |
| 362 | |
| 363 | session = Mock() |
| 364 | session.execute.return_value = SimpleNamespace( |
| 365 | data={"ok": True}, error=None, log_id="log-session", result_type="completed" |
| 366 | ) |
| 367 | provider.execute_tool.reset_mock() |
| 368 | result = provider.execute_tool_call(session=session, tool_call=tool_call) |
| 369 | |
| 370 | session.execute.assert_called_once_with( |
| 371 | tool_slug="TOOL_WITH_ODATA", |
| 372 | arguments={ |
| 373 | "$top": 10, |
| 374 | "@microsoft.graph.conflictBehavior": "rename", |
| 375 | long_name: "value", |
| 376 | }, |
| 377 | ) |
| 378 | provider.execute_tool.assert_not_called() |
| 379 | assert result == { |
| 380 | "data": {"ok": True}, |
| 381 | "error": None, |
| 382 | "successful": True, |
| 383 | "tool_use_id": "use-odata", |
| 384 | "log_id": "log-session", |
| 385 | "result_type": "completed", |
| 386 | } |
| 387 | |
| 388 | |
| 389 | def test_claude_agent_sdk_wrap_tool_aliases_schema_and_restores_arguments(monkeypatch): |
| 390 | claude_agent_sdk_module = types.ModuleType("claude_agent_sdk") |
| 391 | |
| 392 | def sdk_tool(name, description, input_schema): |
| 393 | def decorator(fn): |
| 394 | fn._tool_name = name |
| 395 | fn._tool_description = description |
| 396 | fn._input_schema = input_schema |
| 397 | return fn |
| 398 | |
| 399 | return decorator |
| 400 | |
| 401 | claude_agent_sdk_module.McpSdkServerConfig = dict |
| 402 | claude_agent_sdk_module.SdkMcpTool = object |
| 403 | claude_agent_sdk_module.create_sdk_mcp_server = Mock() |
| 404 | claude_agent_sdk_module.tool = sdk_tool |
| 405 | monkeypatch.setitem(sys.modules, "claude_agent_sdk", claude_agent_sdk_module) |
| 406 | |
| 407 | provider_module = _load_module( |
| 408 | monkeypatch, |
| 409 | "test_composio_claude_agent_sdk_provider", |
| 410 | PYTHON_ROOT |
| 411 | / "providers/claude_agent_sdk/composio_claude_agent_sdk/provider.py", |
| 412 | ) |
| 413 | provider = provider_module.ClaudeAgentSDKProvider() |
| 414 | execute_tool = Mock(return_value={"successful": True}) |
| 415 | tool = SimpleNamespace( |
| 416 | slug="TOOL_WITH_ODATA", |
| 417 | description="Tool with OData parameters", |
| 418 | input_parameters={ |
| 419 | "type": "object", |
| 420 | "properties": {"$top": {"type": "integer"}}, |
| 421 | "required": ["$top"], |
| 422 | }, |
| 423 | ) |
| 424 | |
| 425 | wrapped = provider.wrap_tool(tool, execute_tool) |
| 426 | |
| 427 | assert list(wrapped._input_schema["properties"]) == ["param_top"] |
| 428 | assert wrapped._input_schema["required"] == ["param_top"] |
| 429 | result = asyncio.run(wrapped({"param_top": 5})) |
| 430 | |
| 431 | assert result["content"][0]["type"] == "text" |
| 432 | execute_tool.assert_called_once_with("TOOL_WITH_ODATA", {"$top": 5}) |