File size: 6,226 Bytes
a9e27c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
"""Native Google requests preserve Parts and do not parse text tags."""

from typing import Any, cast

import pytest

from free_claude_code.core.chat_completions.request import ChatRequest
from free_claude_code.core.gemini.native_request import chat_to_gemini


def test_chat_to_gemini_keeps_thoughts_typed_and_tool_ids():
    request = ChatRequest(
        model="models/model",
        messages=[
            {"role": "user", "content": "hello"},
            {
                "role": "assistant",
                "content": "<thought>literal answer</thought>",
                "reasoning_content": "actual thought",
                "tool_calls": [
                    {
                        "id": "call_1",
                        "type": "function",
                        "function": {"name": "read", "arguments": "{}"},
                    }
                ],
            },
            {"role": "tool", "tool_call_id": "call_1", "content": "file"},
        ],
    )
    body = cast(dict[str, Any], chat_to_gemini(request))
    parts = body["contents"][1]["parts"]
    assert parts[:2] == [
        {"text": "actual thought", "thought": True},
        {"text": "<thought>literal answer</thought>"},
    ]
    assert parts[2]["functionCall"] == {"id": "call_1", "name": "read", "args": {}}
    assert body["contents"][2]["parts"][0]["functionResponse"]["id"] == "call_1"


def test_messages_auto_choice_without_tools_has_no_function_config():
    from free_claude_code.core.anthropic.models import MessagesRequest
    from free_claude_code.core.gemini.native_request import messages_to_gemini

    request = MessagesRequest(
        model="model",
        messages=[{"role": "user", "content": "hello"}],
        tool_choice={"type": "auto"},
    )
    assert "toolConfig" not in messages_to_gemini(request)


def test_messages_object_tool_result_retains_complete_value():
    from free_claude_code.core.anthropic.models import MessagesRequest
    from free_claude_code.core.gemini.native_request import messages_to_gemini

    request = MessagesRequest(
        model="model",
        messages=[
            {
                "role": "assistant",
                "content": [
                    {"type": "tool_use", "id": "call", "name": "read", "input": {}}
                ],
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": "call",
                        "content": {"count": 3, "nested": {"ok": True}},
                    }
                ],
            },
        ],
    )
    result = cast(dict[str, Any], messages_to_gemini(request))
    assert result["contents"][-1]["parts"][0]["functionResponse"]["response"][
        "output"
    ] == {"count": 3, "nested": {"ok": True}}


def test_messages_tool_result_and_followup_remain_distinct_turns():
    from free_claude_code.core.anthropic.models import MessagesRequest
    from free_claude_code.core.gemini.native_request import messages_to_gemini

    request = MessagesRequest(
        model="model",
        messages=[
            {
                "role": "assistant",
                "content": [
                    {
                        "type": "tool_use",
                        "id": "call",
                        "name": "echo",
                        "input": {"value": "FCC_RESUME"},
                    },
                ],
            },
            {
                "role": "user",
                "content": [
                    {
                        "type": "tool_result",
                        "tool_use_id": "call",
                        "content": "FCC_RESUME",
                    },
                ],
            },
            {"role": "user", "content": "Reply exactly OK without calling tools."},
        ],
    )
    contents = cast(dict[str, Any], messages_to_gemini(request))["contents"]
    assert contents[-2:] == [
        {
            "role": "user",
            "parts": [
                {
                    "functionResponse": {
                        "id": "call",
                        "name": "echo",
                        "response": {"output": "FCC_RESUME"},
                    }
                }
            ],
        },
        {
            "role": "user",
            "parts": [{"text": "Reply exactly OK without calling tools."}],
        },
    ]


def test_gemini_25_effort_uses_supported_numeric_budget():
    from free_claude_code.core.gemini.native_request import apply_reasoning
    from free_claude_code.core.reasoning import ReasoningEffort, ReasoningPolicy

    for model in ("gemini-2.5-flash", "models/gemini-2.5-pro", "gemini-2.5-flash-lite"):
        for effort in ReasoningEffort:
            body: dict[str, Any] = {"model": model}
            apply_reasoning(body, ReasoningPolicy.on(effort=effort))
            assert body["generationConfig"]["thinkingConfig"] == {
                "includeThoughts": True,
                "thinkingBudget": effort.budget_tokens,
            }


def test_messages_strict_tool_guarantee_is_rejected_for_gemini():
    from free_claude_code.core.anthropic.models import MessagesRequest
    from free_claude_code.core.gemini.native_request import messages_to_gemini

    request = MessagesRequest(
        model="model",
        messages=[{"role": "user", "content": "hello"}],
        tools=[{"name": "read", "input_schema": {"type": "object"}, "strict": True}],
    )
    with pytest.raises(ValueError, match="strict"):
        messages_to_gemini(request)


def test_messages_noop_context_management_keeps_claude_code_compatible():
    from free_claude_code.core.anthropic.models import MessagesRequest
    from free_claude_code.core.gemini.native_request import messages_to_gemini

    request = MessagesRequest(
        model="model",
        messages=[{"role": "user", "content": "hello"}],
        context_management={
            "edits": [{"type": "clear_thinking_20251015", "keep": "all"}]
        },
    )
    result = messages_to_gemini(request)
    assert result["contents"] == [{"role": "user", "parts": [{"text": "hello"}]}]
    assert "context_management" not in result