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, 04:45 UTC
| 1 | """Tests for the Anthropic provider.""" |
| 2 | |
| 3 | import json |
| 4 | from unittest.mock import MagicMock |
| 5 | |
| 6 | import httpx |
| 7 | import pytest |
| 8 | from anthropic.types import Message, ToolUseBlock |
| 9 | from composio_anthropic.provider import AnthropicProvider |
| 10 | from composio_client import AuthenticationError, ProjectAuth |
| 11 | |
| 12 | from composio.client import HttpClient |
| 13 | from composio.core.models.tool_router_session import ToolRouterSession |
| 14 | |
| 15 | |
| 16 | def _tool(slug="SEARCH_TOOL", description="Search for information"): |
| 17 | return MagicMock( |
| 18 | slug=slug, |
| 19 | description=description, |
| 20 | input_parameters={ |
| 21 | "type": "object", |
| 22 | "properties": {"query": {"type": "string"}}, |
| 23 | "required": ["query"], |
| 24 | }, |
| 25 | ) |
| 26 | |
| 27 | |
| 28 | def test_wrap_tool_omits_cache_control_by_default(): |
| 29 | """No `cache_tools` option means no cache_control on the wrapped tool.""" |
| 30 | wrapped = AnthropicProvider().wrap_tool(_tool()) |
| 31 | |
| 32 | assert "cache_control" not in wrapped |
| 33 | |
| 34 | |
| 35 | def test_wrap_tool_attaches_ephemeral_cache_control_when_cache_tools_enabled(): |
| 36 | """`cache_tools=True` attaches Anthropic's ephemeral cache breakpoint to |
| 37 | the tool `wrap_tool` returns. |
| 38 | """ |
| 39 | wrapped = AnthropicProvider(cache_tools=True).wrap_tool(_tool()) |
| 40 | |
| 41 | assert wrapped["cache_control"] == {"type": "ephemeral"} |
| 42 | |
| 43 | |
| 44 | def test_wrap_tools_caches_only_the_last_tool(): |
| 45 | """`wrap_tools` places a single breakpoint on the last tool: a breakpoint |
| 46 | caches everything up to and including it, so one is enough to cover the |
| 47 | whole tool list. Anthropic caps requests at 4 breakpoints total (shared |
| 48 | with the system prompt and messages); a breakpoint on every tool would |
| 49 | blow past that limit once a caller passes 5+ tools. |
| 50 | """ |
| 51 | tools = [_tool(slug="TOOL_A"), _tool(slug="TOOL_B"), _tool(slug="TOOL_C")] |
| 52 | |
| 53 | wrapped_tools = AnthropicProvider(cache_tools=True).wrap_tools(tools) |
| 54 | |
| 55 | assert all("cache_control" not in t for t in wrapped_tools[:-1]) |
| 56 | assert wrapped_tools[-1]["cache_control"] == {"type": "ephemeral"} |
| 57 | |
| 58 | |
| 59 | def test_wrap_tools_single_tool_still_gets_cache_control(): |
| 60 | """A batch of exactly one tool is also "the last tool" and should still |
| 61 | be cached. |
| 62 | """ |
| 63 | wrapped_tools = AnthropicProvider(cache_tools=True).wrap_tools([_tool()]) |
| 64 | |
| 65 | assert wrapped_tools[-1]["cache_control"] == {"type": "ephemeral"} |
| 66 | |
| 67 | |
| 68 | def test_wrap_tools_omits_cache_control_by_default(): |
| 69 | """No `cache_tools` option means no cache_control anywhere in the batch, |
| 70 | including the last tool. |
| 71 | """ |
| 72 | tools = [_tool(slug="TOOL_A"), _tool(slug="TOOL_B")] |
| 73 | |
| 74 | wrapped_tools = AnthropicProvider().wrap_tools(tools) |
| 75 | |
| 76 | assert all("cache_control" not in t for t in wrapped_tools) |
| 77 | |
| 78 | |
| 79 | def test_wrap_tool_still_preserves_schema_and_metadata_with_caching_enabled(): |
| 80 | """Enabling caching must not change the wrapped tool's schema/name/description.""" |
| 81 | tool = _tool() |
| 82 | |
| 83 | wrapped = AnthropicProvider(cache_tools=True).wrap_tool(tool) |
| 84 | |
| 85 | assert wrapped["name"] == tool.slug |
| 86 | assert wrapped["description"] == tool.description |
| 87 | assert wrapped["input_schema"] == tool.input_parameters |
| 88 | |
| 89 | |
| 90 | @pytest.mark.parametrize("target", ["session", "direct"]) |
| 91 | def test_tool_results_preserve_call_ids_and_explicit_outcomes(target): |
| 92 | """Repeated tool names must remain correlated, even when failure has no error.""" |
| 93 | provider = AnthropicProvider() |
| 94 | payloads = iter( |
| 95 | [ |
| 96 | { |
| 97 | "data": {"value": "found"}, |
| 98 | "error": None, |
| 99 | "result_type": "completed", |
| 100 | "log_id": "log-first", |
| 101 | "instant_charge": { |
| 102 | "amount": "0.01", |
| 103 | "currency": "USD", |
| 104 | "charged_by": "composio", |
| 105 | }, |
| 106 | }, |
| 107 | { |
| 108 | "data": {}, |
| 109 | "error": "", |
| 110 | "result_type": "failed", |
| 111 | "log_id": "log-second", |
| 112 | }, |
| 113 | ] |
| 114 | ) |
| 115 | |
| 116 | def respond(request): |
| 117 | return httpx.Response(200, json=next(payloads)) |
| 118 | |
| 119 | message = Message( |
| 120 | id="message", |
| 121 | model="test", |
| 122 | role="assistant", |
| 123 | type="message", |
| 124 | stop_reason="tool_use", |
| 125 | usage={"input_tokens": 1, "output_tokens": 1}, |
| 126 | content=[ |
| 127 | {"type": "tool_use", "id": "use-first", "name": "SEARCH", "input": {}}, |
| 128 | {"type": "text", "text": "Checking another result."}, |
| 129 | {"type": "tool_use", "id": "use-second", "name": "SEARCH", "input": {}}, |
| 130 | ], |
| 131 | ) |
| 132 | with HttpClient( |
| 133 | provider="anthropic", |
| 134 | auth=ProjectAuth("test-key"), |
| 135 | base_url="https://backend.invalid", |
| 136 | http_client=httpx.Client(transport=httpx.MockTransport(respond)), |
| 137 | ) as client: |
| 138 | session = ToolRouterSession( |
| 139 | client=client, |
| 140 | provider=provider, |
| 141 | dangerously_allow_auto_upload_download_files=False, |
| 142 | session_id="session", |
| 143 | mcp=None, |
| 144 | experimental={}, |
| 145 | ) |
| 146 | if target == "session": |
| 147 | results = provider.handle_tool_calls(session=session, response=message) |
| 148 | else: |
| 149 | # Keep the real direct-result dictionary to check that adding correlation |
| 150 | # doesn't mutate an executor's result (it may be shared with a modifier). |
| 151 | direct_results = [ |
| 152 | {"data": {"value": "found"}, "error": None, "successful": True}, |
| 153 | {"data": {}, "error": "", "successful": False}, |
| 154 | ] |
| 155 | pending = iter(direct_results) |
| 156 | provider.set_execute_tool_fn(lambda *args, **kwargs: next(pending)) |
| 157 | results = provider.handle_tool_calls(user_id="user", response=message) |
| 158 | assert all("tool_use_id" not in result for result in direct_results) |
| 159 | |
| 160 | assert [result["tool_use_id"] for result in results] == ["use-first", "use-second"] |
| 161 | assert [result["successful"] for result in results] == [True, False] |
| 162 | assert results[1]["error"] == "" |
| 163 | if target == "session": |
| 164 | assert [result["result_type"] for result in results] == ["completed", "failed"] |
| 165 | assert [result["log_id"] for result in results] == ["log-first", "log-second"] |
| 166 | assert json.loads(json.dumps(results[0]))["instant_charge"]["amount"] == "0.01" |
| 167 | assert "instant_charge" not in results[1] |
| 168 | |
| 169 | |
| 170 | def test_tool_call_does_not_turn_auth_failure_into_tool_failure(): |
| 171 | provider = AnthropicProvider() |
| 172 | |
| 173 | def respond(request): |
| 174 | return httpx.Response(401, json={"message": "Invalid test credentials"}) |
| 175 | |
| 176 | with HttpClient( |
| 177 | provider="anthropic", |
| 178 | auth=ProjectAuth("test-key"), |
| 179 | base_url="https://backend.invalid", |
| 180 | http_client=httpx.Client(transport=httpx.MockTransport(respond)), |
| 181 | ) as client: |
| 182 | session = ToolRouterSession( |
| 183 | client=client, |
| 184 | provider=provider, |
| 185 | dangerously_allow_auto_upload_download_files=False, |
| 186 | session_id="session", |
| 187 | mcp=None, |
| 188 | experimental={}, |
| 189 | ) |
| 190 | with pytest.raises(AuthenticationError): |
| 191 | provider.execute_tool_call( |
| 192 | session=session, |
| 193 | tool_call=ToolUseBlock( |
| 194 | type="tool_use", id="use-auth", name="SEARCH", input={} |
| 195 | ), |
| 196 | ) |