|
1 | 1 | import json |
| 2 | +from types import SimpleNamespace |
2 | 3 |
|
3 | 4 | import pytest |
| 5 | +from anthropic import BadRequestError |
4 | 6 | from anthropic import _base_client as anthropic_base_client |
5 | | -from anthropic.types import MessageDeltaUsage, Usage |
| 7 | +from anthropic.types import Message, MessageDeltaUsage, TextBlock, Usage |
6 | 8 |
|
7 | 9 | from astrbot.core.agent.tool import FunctionTool, ToolSet |
8 | 10 | from astrbot.core.provider.entities import TokenUsage |
@@ -87,6 +89,203 @@ def test_anthropic_update_usage_omitted_fields_are_preserved(): |
87 | 89 | assert token_usage.output == 7 |
88 | 90 |
|
89 | 91 |
|
| 92 | +@pytest.mark.asyncio |
| 93 | +async def test_query_merges_custom_server_tools_with_registered_tools(): |
| 94 | + provider = ProviderAnthropic( |
| 95 | + provider_config={ |
| 96 | + "id": "anthropic-test", |
| 97 | + "type": "anthropic_chat_completion", |
| 98 | + "model": "claude-test", |
| 99 | + "key": ["test-key"], |
| 100 | + "custom_extra_body": { |
| 101 | + "tools": [{"type": "web_search_20250305", "name": "web_search"}], |
| 102 | + "custom_flag": True, |
| 103 | + }, |
| 104 | + }, |
| 105 | + provider_settings={}, |
| 106 | + ) |
| 107 | + |
| 108 | + captured: dict[str, object] = {} |
| 109 | + |
| 110 | + class FakeMessages: |
| 111 | + async def create(self, **kwargs): |
| 112 | + captured.update(kwargs) |
| 113 | + return Message( |
| 114 | + id="msg_1", |
| 115 | + content=[TextBlock(text="ok", type="text")], |
| 116 | + model="claude-test", |
| 117 | + role="assistant", |
| 118 | + stop_reason="end_turn", |
| 119 | + stop_sequence=None, |
| 120 | + type="message", |
| 121 | + usage=Usage(input_tokens=1, output_tokens=1), |
| 122 | + ) |
| 123 | + |
| 124 | + provider.client = SimpleNamespace(messages=FakeMessages()) |
| 125 | + |
| 126 | + tool_set = ToolSet( |
| 127 | + [ |
| 128 | + FunctionTool( |
| 129 | + name="get_time", |
| 130 | + description="Get the current time.", |
| 131 | + parameters={"type": "object", "properties": {}}, |
| 132 | + ) |
| 133 | + ] |
| 134 | + ) |
| 135 | + |
| 136 | + await provider._query({"model": "claude-test", "messages": []}, tool_set) |
| 137 | + |
| 138 | + # Server-side tools from the custom body must coexist with the |
| 139 | + # registered function tools instead of replacing them. |
| 140 | + assert [tool["name"] for tool in captured["tools"]] == ["get_time", "web_search"] |
| 141 | + assert "tools" not in captured["extra_body"] |
| 142 | + assert captured["extra_body"]["custom_flag"] is True |
| 143 | + |
| 144 | + |
| 145 | +@pytest.mark.asyncio |
| 146 | +async def test_query_custom_tools_without_registered_tools_pass_through(): |
| 147 | + provider = ProviderAnthropic( |
| 148 | + provider_config={ |
| 149 | + "id": "anthropic-test", |
| 150 | + "type": "anthropic_chat_completion", |
| 151 | + "model": "claude-test", |
| 152 | + "key": ["test-key"], |
| 153 | + "custom_extra_body": { |
| 154 | + "tools": [{"type": "web_search_20250305", "name": "web_search"}], |
| 155 | + }, |
| 156 | + }, |
| 157 | + provider_settings={}, |
| 158 | + ) |
| 159 | + |
| 160 | + captured: dict[str, object] = {} |
| 161 | + |
| 162 | + class FakeMessages: |
| 163 | + async def create(self, **kwargs): |
| 164 | + captured.update(kwargs) |
| 165 | + return Message( |
| 166 | + id="msg_1", |
| 167 | + content=[TextBlock(text="ok", type="text")], |
| 168 | + model="claude-test", |
| 169 | + role="assistant", |
| 170 | + stop_reason="end_turn", |
| 171 | + stop_sequence=None, |
| 172 | + type="message", |
| 173 | + usage=Usage(input_tokens=1, output_tokens=1), |
| 174 | + ) |
| 175 | + |
| 176 | + provider.client = SimpleNamespace(messages=FakeMessages()) |
| 177 | + |
| 178 | + await provider._query({"model": "claude-test", "messages": []}, None) |
| 179 | + |
| 180 | + # No registered tools: the custom body keeps its previous override |
| 181 | + # semantics and goes out via extra_body untouched. |
| 182 | + assert "tools" not in captured |
| 183 | + assert captured["extra_body"]["tools"] == [ |
| 184 | + {"type": "web_search_20250305", "name": "web_search"} |
| 185 | + ] |
| 186 | + |
| 187 | + |
| 188 | +@pytest.mark.asyncio |
| 189 | +async def test_query_custom_tool_overrides_registered_tool_with_same_name(): |
| 190 | + provider = ProviderAnthropic( |
| 191 | + provider_config={ |
| 192 | + "id": "anthropic-test", |
| 193 | + "type": "anthropic_chat_completion", |
| 194 | + "model": "claude-test", |
| 195 | + "key": ["test-key"], |
| 196 | + "custom_extra_body": { |
| 197 | + "tools": [ |
| 198 | + {"type": "web_search_20250305", "name": "web_search", "max_uses": 3} |
| 199 | + ], |
| 200 | + }, |
| 201 | + }, |
| 202 | + provider_settings={}, |
| 203 | + ) |
| 204 | + |
| 205 | + captured: dict[str, object] = {} |
| 206 | + |
| 207 | + class FakeMessages: |
| 208 | + async def create(self, **kwargs): |
| 209 | + captured.update(kwargs) |
| 210 | + return Message( |
| 211 | + id="msg_1", |
| 212 | + content=[TextBlock(text="ok", type="text")], |
| 213 | + model="claude-test", |
| 214 | + role="assistant", |
| 215 | + stop_reason="end_turn", |
| 216 | + stop_sequence=None, |
| 217 | + type="message", |
| 218 | + usage=Usage(input_tokens=1, output_tokens=1), |
| 219 | + ) |
| 220 | + |
| 221 | + provider.client = SimpleNamespace(messages=FakeMessages()) |
| 222 | + |
| 223 | + tool_set = ToolSet( |
| 224 | + [ |
| 225 | + FunctionTool( |
| 226 | + name="web_search", |
| 227 | + description="Client-side web search.", |
| 228 | + parameters={"type": "object", "properties": {}}, |
| 229 | + ) |
| 230 | + ] |
| 231 | + ) |
| 232 | + |
| 233 | + await provider._query({"model": "claude-test", "messages": []}, tool_set) |
| 234 | + |
| 235 | + # A name collision must not duplicate the tool (the API rejects |
| 236 | + # duplicates); the custom body keeps its override authority. |
| 237 | + assert captured["tools"] == [ |
| 238 | + {"type": "web_search_20250305", "name": "web_search", "max_uses": 3} |
| 239 | + ] |
| 240 | + |
| 241 | + |
| 242 | +@pytest.mark.asyncio |
| 243 | +async def test_query_null_custom_extra_body_does_not_break_request(): |
| 244 | + provider = ProviderAnthropic( |
| 245 | + provider_config={ |
| 246 | + "id": "anthropic-test", |
| 247 | + "type": "anthropic_chat_completion", |
| 248 | + "model": "claude-test", |
| 249 | + "key": ["test-key"], |
| 250 | + "custom_extra_body": None, |
| 251 | + }, |
| 252 | + provider_settings={}, |
| 253 | + ) |
| 254 | + |
| 255 | + captured: dict[str, object] = {} |
| 256 | + |
| 257 | + class FakeMessages: |
| 258 | + async def create(self, **kwargs): |
| 259 | + captured.update(kwargs) |
| 260 | + return Message( |
| 261 | + id="msg_1", |
| 262 | + content=[TextBlock(text="ok", type="text")], |
| 263 | + model="claude-test", |
| 264 | + role="assistant", |
| 265 | + stop_reason="end_turn", |
| 266 | + stop_sequence=None, |
| 267 | + type="message", |
| 268 | + usage=Usage(input_tokens=1, output_tokens=1), |
| 269 | + ) |
| 270 | + |
| 271 | + provider.client = SimpleNamespace(messages=FakeMessages()) |
| 272 | + |
| 273 | + tool_set = ToolSet( |
| 274 | + [ |
| 275 | + FunctionTool( |
| 276 | + name="get_time", |
| 277 | + description="Get the current time.", |
| 278 | + parameters={"type": "object", "properties": {}}, |
| 279 | + ) |
| 280 | + ] |
| 281 | + ) |
| 282 | + |
| 283 | + await provider._query({"model": "claude-test", "messages": []}, tool_set) |
| 284 | + |
| 285 | + assert [tool["name"] for tool in captured["tools"]] == ["get_time"] |
| 286 | + assert captured["extra_body"] == {} |
| 287 | + |
| 288 | + |
90 | 289 | def _tool_use_stream( |
91 | 290 | partial_json_chunks: list[str], start_input: dict | None = None |
92 | 291 | ) -> bytes: |
@@ -196,3 +395,74 @@ async def test_anthropic_stream_keeps_tool_call_with_empty_input( |
196 | 395 | assert response.tools_call_name == ["get_time"] |
197 | 396 | assert response.tools_call_args == [expected_args] |
198 | 397 | assert response.tools_call_ids == ["toolu_01"] |
| 398 | + |
| 399 | + |
| 400 | +@pytest.mark.asyncio |
| 401 | +@pytest.mark.parametrize("streaming", [False, True], ids=["standard", "stream"]) |
| 402 | +@pytest.mark.parametrize( |
| 403 | + "custom_tool", |
| 404 | + [{}, {"name": ""}, {"name": None}, None, "invalid", 0, False], |
| 405 | + ids=["missing-name", "empty-name", "null-name", "null", "string", "zero", "false"], |
| 406 | +) |
| 407 | +async def test_malformed_custom_tools_reach_api_validation( |
| 408 | + monkeypatch, streaming, custom_tool |
| 409 | +): |
| 410 | + captured = [] |
| 411 | + |
| 412 | + def handle(request): |
| 413 | + captured.append(json.loads(request.content)) |
| 414 | + return sdk_httpx.Response( |
| 415 | + 400, |
| 416 | + json={ |
| 417 | + "type": "error", |
| 418 | + "error": {"type": "invalid_request_error", "message": "Invalid tool"}, |
| 419 | + }, |
| 420 | + ) |
| 421 | + |
| 422 | + monkeypatch.setattr( |
| 423 | + ProviderAnthropic, |
| 424 | + "_create_http_client", |
| 425 | + lambda self, config: sdk_httpx.AsyncClient( |
| 426 | + transport=sdk_httpx.MockTransport(handle) |
| 427 | + ), |
| 428 | + ) |
| 429 | + custom_tools = [custom_tool, {"type": "unnamed"}] |
| 430 | + provider = ProviderAnthropic( |
| 431 | + { |
| 432 | + "id": "test", |
| 433 | + "type": "anthropic_chat_completion", |
| 434 | + "key": ["test-key"], |
| 435 | + "model": "claude-test", |
| 436 | + "api_base": "https://offline.invalid", |
| 437 | + "custom_extra_body": {"tools": custom_tools}, |
| 438 | + }, |
| 439 | + {}, |
| 440 | + ) |
| 441 | + tools = ToolSet( |
| 442 | + [ |
| 443 | + FunctionTool( |
| 444 | + name="get_time", |
| 445 | + description="Offline tool schema.", |
| 446 | + parameters={"type": "object", "properties": {}}, |
| 447 | + ) |
| 448 | + ] |
| 449 | + ) |
| 450 | + payload = {"model": "claude-test", "messages": [{"role": "user", "content": "hi"}]} |
| 451 | + try: |
| 452 | + for _ in range(2): |
| 453 | + with pytest.raises(BadRequestError): |
| 454 | + if streaming: |
| 455 | + async for _ in provider._query_stream( |
| 456 | + payload, tools, request_max_retries=1 |
| 457 | + ): |
| 458 | + pass |
| 459 | + else: |
| 460 | + await provider._query(payload, tools, request_max_retries=1) |
| 461 | + finally: |
| 462 | + await provider.terminate() |
| 463 | + |
| 464 | + assert len(captured) == 2 |
| 465 | + assert captured[0] == captured[1] |
| 466 | + assert captured[0]["tools"][0]["name"] == "get_time" |
| 467 | + assert captured[0]["tools"][1:] == custom_tools |
| 468 | + assert provider.provider_config["custom_extra_body"]["tools"] == custom_tools |
0 commit comments