| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- import base64
- from agent.llm import openrouter, yescode
- from agent.llm.anthropic_protocol import (
- ANTHROPIC_MODEL_EXACT,
- ANTHROPIC_MODEL_FUZZY,
- build_anthropic_result,
- normalize_tool_call_ids,
- parse_anthropic_response,
- to_anthropic_messages,
- )
- def test_provider_model_tables_share_one_source_of_truth():
- assert yescode.MODEL_EXACT is ANTHROPIC_MODEL_EXACT
- assert yescode.MODEL_FUZZY is ANTHROPIC_MODEL_FUZZY
- assert openrouter._OR_MODEL_EXACT is ANTHROPIC_MODEL_EXACT
- assert openrouter._OR_MODEL_FUZZY is ANTHROPIC_MODEL_FUZZY
- assert yescode._resolve_model("anthropic/claude-sonnet-4.5") == (
- "claude-sonnet-4-5-20250929"
- )
- assert openrouter._resolve_openrouter_model("claude-sonnet-4.5") == (
- "anthropic/claude-sonnet-4-5-20250929"
- )
- def test_unknown_model_fallback_remains_provider_specific():
- assert yescode._resolve_model("vendor/new-model") == "new-model"
- assert openrouter._resolve_openrouter_model("vendor/new-model") == (
- "vendor/new-model"
- )
- def test_tool_call_id_normalization_links_assistant_and_tool_without_mutation():
- messages = [
- {
- "role": "assistant",
- "tool_calls": [{"id": "call_old", "function": {"name": "read"}}],
- },
- {"role": "tool", "tool_call_id": "call_old", "content": "ok"},
- ]
- normalized = normalize_tool_call_ids(messages, "toolu")
- assert normalized[0]["tool_calls"][0]["id"] == "toolu_000000"
- assert normalized[1]["tool_call_id"] == "toolu_000000"
- assert messages[0]["tool_calls"][0]["id"] == "call_old"
- assert normalize_tool_call_ids(normalized, "toolu") is normalized
- def test_yescode_and_openrouter_keep_their_image_nesting_contracts():
- png = b"\x89PNG\r\n\x1a\n" + b"\x00" * 8 + (2).to_bytes(4, "big") + (
- 3
- ).to_bytes(4, "big")
- uri = "data:image/png;base64," + base64.b64encode(png).decode()
- messages = [
- {
- "role": "tool",
- "tool_call_id": "toolu_1",
- "content": [{"type": "image_url", "image_url": {"url": uri}}],
- }
- ]
- _, yescode_messages = yescode._convert_messages_to_anthropic(messages)
- _, shared_messages = to_anthropic_messages(messages)
- nested = yescode_messages[0]["content"][0]["content"][0]
- assert nested["type"] == "image"
- assert "_image_meta" not in nested
- sibling = shared_messages[0]["content"][1]
- assert sibling["type"] == "image"
- assert sibling["_image_meta"] == {"width": 2, "height": 3}
- def test_anthropic_response_and_result_shape_remain_compatible():
- parsed = parse_anthropic_response(
- {
- "content": [
- {"type": "text", "text": "done"},
- {"type": "tool_use", "id": "1", "name": "read", "input": {}},
- ],
- "stop_reason": "tool_use",
- "usage": {"input_tokens": 4, "output_tokens": 2},
- }
- )
- result = build_anthropic_result(parsed, "claude-sonnet-4-6")
- assert result["content"] == "done"
- assert result["tool_calls"][0]["function"]["name"] == "read"
- assert result["finish_reason"] == "tool_calls"
- assert result["prompt_tokens"] == 4
- assert result["completion_tokens"] == 2
- assert "cost" in result
|