feat: init commint

This commit is contained in:
2026-09-18 15:26:19 +08:00
parent 3b61a767a0
commit 9f47dbb5cc
95 changed files with 7712 additions and 1137 deletions

View File

@@ -1,6 +1,12 @@
import httpx
import pytest
from openai import APITimeoutError, AuthenticationError, InternalServerError, RateLimitError
from openai import (
APITimeoutError,
AuthenticationError,
BadRequestError,
InternalServerError,
RateLimitError,
)
from apps.modelhub.crypto import encrypt_secret
from apps.modelhub.models import ModelDeployment, ModelInvocation, ModelProvider
@@ -67,6 +73,60 @@ def test_non_transient_error_fails_without_exposing_raw_error(db, monkeypatch):
assert "secret provider detail" not in invocation.error_message
@pytest.mark.parametrize(
("mode", "expected_response_type", "schema_in_system_message"),
[
("native_json_schema", "json_schema", False),
("json_object", "json_object", True),
("prompt_schema", None, True),
],
)
def test_structured_invocation_records_request_and_audit_snapshot(
db,
monkeypatch,
mode,
expected_response_type,
schema_in_system_message,
):
invocation = create_invocation()
invocation.deployment.structured_output_mode = mode
invocation.deployment.save(update_fields=["structured_output_mode"])
captured = {}
output_schema = {
"type": "object",
"properties": {"answer": {"type": "string"}},
"required": ["answer"],
"additionalProperties": False,
}
def request(*args, **kwargs):
captured.update(kwargs)
captured["messages"] = args[1]
return CompletionResult('{"answer":"ok"}', 10, 4)
monkeypatch.setattr("apps.modelhub.services._request_completion", request)
execute_invocation(
invocation,
invocation.input_snapshot["messages"],
output_schema=output_schema,
schema_name="answer-result",
)
invocation.refresh_from_db()
structured = invocation.model_snapshot["structured_output"]
assert structured["mode"] == mode
assert structured["schema_hash"]
assert structured["schema_name"] == "answer-result"
assert structured["schema_in_system_message"] is schema_in_system_message
assert invocation.input_snapshot["output_schema"] == output_schema
assert invocation.input_snapshot["structured_output"] == structured
if expected_response_type is None:
assert "response_format" not in captured or captured["response_format"] is None
else:
assert captured["response_format"]["type"] == expected_response_type
def test_agent_time_budget_does_not_repeat_a_full_timeout(db, monkeypatch):
invocation = create_invocation()
calls = []
@@ -98,6 +158,47 @@ def provider_error(error_type, status_code):
return error_type("provider raw error", response=response, body={})
@pytest.mark.parametrize(
("mode", "expected_code"),
[
("native_json_schema", "structured_output_unsupported"),
("json_object", "structured_output_unsupported"),
("prompt_schema", "bad_request"),
],
)
def test_response_format_rejection_does_not_downgrade_mode(
db,
monkeypatch,
mode,
expected_code,
):
invocation = create_invocation()
invocation.deployment.structured_output_mode = mode
invocation.deployment.save(update_fields=["structured_output_mode"])
monkeypatch.setattr(
"apps.modelhub.services._request_completion",
lambda *args, **kwargs: (_ for _ in ()).throw(
provider_error(BadRequestError, 400)
),
)
execute_invocation(
invocation,
invocation.input_snapshot["messages"],
output_schema={
"type": "object",
"properties": {"answer": {"type": "string"}},
"required": ["answer"],
"additionalProperties": False,
},
)
invocation.refresh_from_db()
assert invocation.status == "failed"
assert invocation.error_code == expected_code
assert invocation.retry_count == 0
@pytest.mark.parametrize(
("error", "expected_code", "expected_calls"),
[