fix: 探测 Responses/store 能力并在无状态时用 Completions 带全量历史

This commit is contained in:
2026-09-25 00:23:28 +08:00
parent d20c6a1302
commit 71eca63461
14 changed files with 584 additions and 193 deletions
+55 -9
View File
@@ -1,17 +1,63 @@
"""api_style 解析单测。"""
"""协议探测规划与错误分类。"""
from infrastructure.llm.api_style import normalize_api_style, resolve_use_responses_api
from domain.models import LlmProtocolCapability
from infrastructure.llm.api_style import (
ProtocolAttempt,
classify_protocol_error,
normalize_api_style,
plan_protocol_attempts,
)
def test_normalize_api_style():
assert normalize_api_style(None) == "auto"
assert normalize_api_style("Responses") == "responses"
assert normalize_api_style("weird") == "auto"
def test_resolve_use_responses_api():
assert resolve_use_responses_api("responses", "https://api.deepseek.com/v1") is True
assert resolve_use_responses_api("completions", "https://api.openai.com/v1") is False
assert resolve_use_responses_api("auto", "https://api.openai.com/v1") is True
assert resolve_use_responses_api("auto", "https://api.deepseek.com/v1") is False
assert resolve_use_responses_api("auto", "https://xxx.openai.azure.com/") is False
def test_plan_unknown_auto_tries_store_full_history_then_fallbacks():
attempts = plan_protocol_attempts("auto", None, has_chain_id=True)
assert attempts[0] == ProtocolAttempt(True, True, False)
assert ProtocolAttempt(True, False, False) in attempts
assert ProtocolAttempt(False, False, False) in attempts
assert not any(a.use_chain for a in attempts)
def test_plan_store_true_allows_chain():
cap = LlmProtocolCapability(responses=True, store=True)
attempts = plan_protocol_attempts("auto", cap, has_chain_id=True)
assert attempts[0] == ProtocolAttempt(True, True, True)
assert ProtocolAttempt(True, True, False) in attempts
def test_plan_store_false_uses_completions():
"""DeepSeek 等:Responses 无状态时用 Completions 带 Mongo 历史。"""
cap = LlmProtocolCapability(responses=True, store=False)
attempts = plan_protocol_attempts("auto", cap, has_chain_id=True)
assert attempts == [ProtocolAttempt(False, False, False)]
def test_plan_store_false_forced_responses_still_completions():
cap = LlmProtocolCapability(responses=True, store=False)
attempts = plan_protocol_attempts("responses", cap, has_chain_id=False)
assert attempts == [ProtocolAttempt(False, False, False)]
def test_plan_completions_only():
assert plan_protocol_attempts("completions", None, has_chain_id=True) == [
ProtocolAttempt(False, False, False)
]
def test_plan_responses_known_unsupported_falls_to_completions():
cap = LlmProtocolCapability(responses=False, store=False)
attempts = plan_protocol_attempts("auto", cap, has_chain_id=False)
assert attempts == [ProtocolAttempt(False, False, False)]
def test_classify_protocol_error():
assert classify_protocol_error(Exception("Invalid previous_response_id")) == "chain"
assert (
classify_protocol_error(Exception("Unsupported parameter: store")) == "store"
)
assert classify_protocol_error(Exception("404 /v1/responses not found")) == "responses"
assert classify_protocol_error(Exception("rate limit")) == "other"
+5 -25
View File
@@ -1,8 +1,7 @@
"""Responses 续链匹配与失效判定。"""
"""会话链选型匹配。"""
from domain.models import LlmResponseChain, LlmRuntimeConfig
from application.chat_service import _usable_chain_id
from infrastructure.llm.factory import _is_chain_invalid_error
def test_usable_chain_id_matches():
@@ -11,12 +10,12 @@ def test_usable_chain_id_matches():
api_key="k",
model="gpt-4o-mini",
provider_id=3,
api_style="responses",
api_style="auto",
)
chain = LlmResponseChain(
provider_id=3, model="gpt-4o-mini", previous_response_id="resp_abc"
)
assert _usable_chain_id(chain, llm, True) == "resp_abc"
assert _usable_chain_id(chain, llm) == "resp_abc"
def test_usable_chain_id_clears_on_model_switch():
@@ -25,28 +24,9 @@ def test_usable_chain_id_clears_on_model_switch():
api_key="k",
model="gpt-4o",
provider_id=3,
api_style="responses",
api_style="auto",
)
chain = LlmResponseChain(
provider_id=3, model="gpt-4o-mini", previous_response_id="resp_abc"
)
assert _usable_chain_id(chain, llm, True) is None
def test_usable_chain_id_requires_responses():
llm = LlmRuntimeConfig(
base_url="https://api.deepseek.com/v1",
api_key="k",
model="deepseek-chat",
provider_id=1,
api_style="completions",
)
chain = LlmResponseChain(
provider_id=1, model="deepseek-chat", previous_response_id="resp_x"
)
assert _usable_chain_id(chain, llm, False) is None
def test_is_chain_invalid_error():
assert _is_chain_invalid_error(Exception("Invalid previous_response_id: resp_x"))
assert not _is_chain_invalid_error(Exception("rate limit exceeded"))
assert _usable_chain_id(chain, llm) is None