Spaces:
Running
Running
File size: 14,219 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 | """The Chat endpoint uses each upstream through the real HTTP boundary."""
import json
from typing import Any
import httpx
import httpx2
import pytest
from fastapi.testclient import TestClient
from free_claude_code.config.settings import Settings
from tests.api.support import create_test_app, provider_manager_for_app
from tests.providers.test_custom_provider import completion, definition, provider
@pytest.mark.parametrize(
"wire", ["openai_chat", "openai_responses", "anthropic_messages"]
)
@pytest.mark.parametrize("streaming", [False, True])
def test_chat_http_dispatch_preserves_controls_credentials_and_public_model(
wire, streaming
):
sent = []
def reply(request):
sent.append((json.loads(request.content), dict(request.headers)))
module = httpx if wire == "anthropic_messages" else httpx2
return module.Response(
200, text=completion(wire), headers={"content-type": "text/event-stream"}
)
configured = definition(api_format=wire, api_keys=["upstream-key"])
instance = provider(configured, openai_handler=reply, messages_handler=reply)
app = create_test_app(
Settings(
MODEL=f"{configured.provider_id}/backend",
custom_providers=(configured,),
PROXY_AUTH_ENABLED=True,
proxy_auth_token="ingress-key",
),
providers={configured.provider_id: instance},
)
payload = {
"model": "public",
"messages": [{"role": "user", "content": "hello"}],
"temperature": 0,
"max_completion_tokens": 37,
"stream": streaming,
}
with TestClient(app) as client:
response = client.post(
"/v1/chat/completions",
json=payload,
headers={"authorization": "Bearer ingress-key"},
)
assert response.status_code == 200, response.text
assert response.headers["x-request-id"]
if streaming:
assert response.text.endswith("data: [DONE]\n\n")
values = [
json.loads(line[6:])
for line in response.text.splitlines()
if line.startswith("data: ") and line != "data: [DONE]"
]
assert (
"".join(
choice.get("delta", {}).get("content") or ""
for value in values
for choice in value.get("choices", [])
)
== "Hello"
)
assert all(value["model"] == "public" for value in values)
else:
assert response.json()["model"] == "public"
assert response.json()["choices"][0]["message"]["content"] == "Hello"
assert provider_manager_for_app(app)._current.active_leases == 0
assert len(sent) == 1
body, headers = sent[0]
assert body["model"] == "backend"
assert body["temperature"] == 0
limit = {
"openai_chat": "max_completion_tokens",
"openai_responses": "max_output_tokens",
"anthropic_messages": "max_tokens",
}[wire]
assert body[limit] == 37
assert "ingress-key" not in json.dumps(headers)
assert headers[
"x-api-key" if wire == "anthropic_messages" else "authorization"
].endswith("upstream-key")
@pytest.mark.parametrize("wire", ["openai_chat", "openai_responses"])
@pytest.mark.parametrize(
"strict", [{}, {"strict": None}, {"strict": False}, {"strict": True}]
)
def test_chat_function_strictness_survives_upstream_translation(wire, strict):
sent = []
def reply(request):
sent.append(json.loads(request.content))
return httpx2.Response(
200, text=completion(wire), headers={"content-type": "text/event-stream"}
)
configured = definition(api_format=wire)
instance = provider(configured, openai_handler=reply)
app = create_test_app(
Settings(
MODEL=f"{configured.provider_id}/backend",
custom_providers=(configured,),
),
providers={configured.provider_id: instance},
)
function = {
"name": "weather",
"parameters": {
"type": "object",
"properties": {"city": {"type": "string"}},
"required": ["city"] if strict.get("strict") else [],
"additionalProperties": False,
},
**strict,
}
with TestClient(app) as client:
response = client.post(
"/v1/chat/completions",
json={
"model": "public",
"messages": [{"role": "user", "content": "weather"}],
"tools": [{"type": "function", "function": function}],
},
)
assert response.status_code == 200, response.text
assert len(sent) == 1
if wire == "openai_chat":
assert sent[0]["tools"] == [{"type": "function", "function": function}]
else:
assert sent[0]["tools"] == [
{"type": "function", **function, "strict": strict.get("strict") is True}
]
def test_chat_validation_and_auth_errors_use_openai_shape():
app = create_test_app(
Settings(PROXY_AUTH_ENABLED=True, proxy_auth_token="ingress-key")
)
with TestClient(app) as client:
unauthenticated = client.post(
"/v1/chat/completions",
json={"model": "x", "messages": [{"role": "user", "content": "hi"}]},
)
assert unauthenticated.status_code == 401
assert "error" in unauthenticated.json()
invalid = client.post(
"/v1/chat/completions",
json={"model": "x", "messages": []},
headers={"authorization": "Bearer ingress-key"},
)
assert invalid.status_code == 400
assert invalid.json()["error"]["type"] == "invalid_request_error"
@pytest.mark.parametrize("streaming", [False, True])
@pytest.mark.parametrize("with_tool", [False, True])
def test_native_chat_null_tool_delta_preserves_text_and_later_calls(
streaming, with_tool
):
from tests.api.test_four_protocol_matrix import matrix_app, response_value
call = {
"index": 0,
"id": "call_read",
"type": "function",
"function": {"name": "read", "arguments": "{}"},
}
deltas = [
{"role": "assistant", "content": "hello", "tool_calls": None},
*([{"tool_calls": [{**call, "function": None}]}] if with_tool else []),
{"tool_calls": [call]} if with_tool else {"tool_calls": None},
]
def reply(_):
events = [
{
"id": "chatcmpl-a",
"object": "chat.completion.chunk",
"created": 1,
"model": "backend",
"choices": [
{
"index": 0,
"delta": delta,
"finish_reason": ("tool_calls" if with_tool else "stop")
if index == len(deltas) - 1
else None,
}
],
}
for index, delta in enumerate(deltas)
]
return httpx2.Response(
200,
headers={"content-type": "text/event-stream"},
text="".join(f"data: {json.dumps(event)}\n\n" for event in events),
)
with TestClient(matrix_app("chat", reply)) as client:
response = client.post(
"/v1/chat/completions",
json={
"model": "public",
"messages": [{"role": "user", "content": "hello"}],
"stream": streaming,
},
)
value = response_value(response, "chat", streaming)
message = value["choices"][0]["message"]
assert message["content"] == "hello"
assert message["tool_calls"] == (
[{key: item for key, item in call.items() if key != "index"}]
if with_tool
else None
)
if streaming:
first = json.loads(response.text.splitlines()[0][6:])
assert first["choices"][0]["delta"]["tool_calls"] is None
@pytest.mark.parametrize(
"changes",
[
{"n": 0},
{"n": "two"},
{"max_completion_tokens": "many"},
{"stream_options": ["usage"]},
{"tools": [1]},
{"tools": [{"type": "function", "function": {}}]},
{"tool_choice": "bogus"},
{"messages": [{"role": "user", "content": [{"type": "image_url"}]}]},
{
"messages": [
{
"role": "assistant",
"tool_calls": [
{"type": "function", "id": "x", "function": {"arguments": "{}"}}
],
}
]
},
{"messages": [{"role": "assistant", "reasoning_details": ["bad"]}]},
],
)
def test_chat_malformed_fields_fail_before_provider_execution(changes):
app = create_test_app(Settings())
with TestClient(app) as client:
response = client.post(
"/v1/chat/completions",
json={
"model": "public",
"messages": [{"role": "user", "content": "hi"}],
**changes,
},
)
assert response.status_code == 400, response.text
assert response.json()["error"]["type"] == "invalid_request_error"
@pytest.mark.parametrize("streaming", [False, True])
@pytest.mark.parametrize(
("field", "updates", "expected"),
[
(
"function_call",
[
{"name": None, "arguments": None},
{"name": "read", "arguments": "{"},
{"name": None, "arguments": "}"},
],
{"name": "read", "arguments": "{}"},
),
(
"audio",
[
{"id": "audio_a", "data": None, "transcript": None},
{"data": "YQ==", "transcript": "hello"},
{"id": None, "data": None, "transcript": None, "expires_at": 123},
],
{"id": "audio_a", "data": "YQ==", "transcript": "hello", "expires_at": 123},
),
(
"reasoning_details",
[
[{"type": "reasoning.text", "text": "one"}],
[{"type": "reasoning.text", "text": " two"}],
],
[{"type": "reasoning.text", "text": "one two"}],
),
("citations", [["source-a"], ["source-b"]], ["source-a", "source-b"]),
],
)
def test_native_chat_nullable_fields_accumulate_later_values(
streaming, field, updates, expected
):
from tests.api.test_four_protocol_matrix import matrix_app, response_value
def reply(_):
deltas = [
{"role": "assistant", field: None},
*[{field: value} for value in updates],
{field: None, "content": "done"},
]
events = [
{
"id": "chatcmpl-a",
"object": "chat.completion.chunk",
"created": 1,
"model": "backend",
"choices": [
{
"index": 0,
"delta": delta,
"finish_reason": "stop" if index == len(deltas) - 1 else None,
}
],
}
for index, delta in enumerate(deltas)
]
return httpx2.Response(
200,
headers={"content-type": "text/event-stream"},
text="".join(f"data: {json.dumps(event)}\n\n" for event in events),
)
with TestClient(matrix_app("chat", reply)) as client:
response = client.post(
"/v1/chat/completions",
json={
"model": "public",
"messages": [{"role": "user", "content": "hello"}],
"stream": streaming,
},
)
value = response_value(response, "chat", streaming)
assert value["choices"][0]["message"][field] == expected
if streaming:
first = json.loads(response.text.splitlines()[0][6:])
assert first["choices"][0]["delta"][field] is None
@pytest.mark.parametrize("streaming", [False, True])
def test_native_custom_tool_fragments_survive_public_chat_response(streaming):
from tests.api.test_four_protocol_matrix import matrix_app, response_value
chunks = [
{
"index": 0,
"id": "call_custom",
"type": "custom",
"custom": {"name": "code_exec", "input": "print("},
},
{"index": 0, "custom": {"input": "1)"}},
]
def reply(_):
events: list[dict[str, Any]] = [
{
"id": "chatcmpl-a",
"object": "chat.completion.chunk",
"created": 1,
"model": "backend",
"choices": [
{
"index": 0,
"delta": {"tool_calls": [chunk]},
"finish_reason": None,
}
],
}
for chunk in chunks
]
events.append(
{
"id": "chatcmpl-a",
"object": "chat.completion.chunk",
"created": 1,
"model": "backend",
"choices": [{"index": 0, "delta": {}, "finish_reason": "tool_calls"}],
}
)
return httpx2.Response(
200,
headers={"content-type": "text/event-stream"},
text="".join(f"data: {json.dumps(event)}\n\n" for event in events),
)
with TestClient(matrix_app("chat", reply)) as client:
response = client.post(
"/v1/chat/completions",
json={
"model": "public",
"messages": [{"role": "user", "content": "run"}],
"stream": streaming,
"tools": [{"type": "custom", "custom": {"name": "code_exec"}}],
},
)
value = response_value(response, "chat", streaming)
assert value["choices"][0]["message"]["tool_calls"] == [
{
"id": "call_custom",
"type": "custom",
"custom": {"name": "code_exec", "input": "print(1)"},
}
]
|