mirror of
https://gitee.com/qqtt-ah/ai-crm.git
synced 2026-11-24 06:29:21 +08:00
129 lines
3.9 KiB
Python
129 lines
3.9 KiB
Python
"""协议偏好与错误分类:先验缓存 + 运行时探测回退(不写死主机)。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass
|
|
|
|
from domain.models import LlmProtocolCapability
|
|
|
|
|
|
def normalize_api_style(raw: str | None) -> str:
|
|
s = (raw or "auto").strip().lower()
|
|
if s in ("auto", "completions", "responses"):
|
|
return s
|
|
return "auto"
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class ProtocolAttempt:
|
|
"""单次调用策略。"""
|
|
|
|
use_responses: bool
|
|
use_store: bool
|
|
use_chain: bool # 是否只发增量 + previous_response_id(须 store 已证实)
|
|
|
|
|
|
def plan_protocol_attempts(
|
|
api_style: str | None,
|
|
cap: LlmProtocolCapability | None,
|
|
*,
|
|
has_chain_id: bool,
|
|
) -> list[ProtocolAttempt]:
|
|
"""
|
|
同轮探测顺序(未产出 token 前可依次尝试):
|
|
|
|
- store 已证实:Responses+store,有链则增量续聊
|
|
- store 未知:Responses+store + **Mongo 全量**,成功后再语义探测
|
|
- store 证伪:先 **无状态 Responses + Mongo 全量**,失败再 **Completions**
|
|
- Responses 不可用:Completions
|
|
"""
|
|
style = normalize_api_style(api_style)
|
|
cap = cap or LlmProtocolCapability()
|
|
|
|
if style == "completions":
|
|
return [ProtocolAttempt(False, False, False)]
|
|
|
|
attempts: list[ProtocolAttempt] = []
|
|
|
|
if cap.store is False:
|
|
# 无真实 store:无状态 Responses(全量历史)→ Completions
|
|
if cap.responses is not False:
|
|
attempts.append(ProtocolAttempt(True, False, False))
|
|
attempts.append(ProtocolAttempt(False, False, False))
|
|
return _dedupe(attempts)
|
|
|
|
if cap.responses is not False:
|
|
if cap.store is True:
|
|
attempts.append(
|
|
ProtocolAttempt(True, True, use_chain=bool(has_chain_id))
|
|
)
|
|
# 有链失败时可降到同协议全量
|
|
attempts.append(ProtocolAttempt(True, True, use_chain=False))
|
|
else:
|
|
# store 未知:Responses+store,必须全量历史,禁止 use_chain
|
|
attempts.append(ProtocolAttempt(True, True, use_chain=False))
|
|
attempts.append(ProtocolAttempt(True, False, False))
|
|
|
|
# Completions 兜底
|
|
if style == "auto" or cap.responses is False:
|
|
attempts.append(ProtocolAttempt(False, False, False))
|
|
elif style == "responses" and cap.responses is False:
|
|
attempts.append(ProtocolAttempt(False, False, False))
|
|
|
|
return _dedupe(attempts) or [ProtocolAttempt(False, False, False)]
|
|
|
|
|
|
def _dedupe(attempts: list[ProtocolAttempt]) -> list[ProtocolAttempt]:
|
|
seen: set[tuple[bool, bool, bool]] = set()
|
|
out: list[ProtocolAttempt] = []
|
|
for a in attempts:
|
|
key = (a.use_responses, a.use_store, a.use_chain)
|
|
if key in seen:
|
|
continue
|
|
seen.add(key)
|
|
out.append(a)
|
|
return out
|
|
|
|
|
|
def classify_protocol_error(exc: BaseException) -> str:
|
|
"""返回: store | responses | chain | other。"""
|
|
text = str(exc).lower()
|
|
status = getattr(exc, "status_code", None) or getattr(
|
|
getattr(exc, "response", None), "status_code", None
|
|
)
|
|
|
|
if "previous_response_id" in text or "previous response" in text:
|
|
return "chain"
|
|
if status == 400 and "response" in text and "not found" in text:
|
|
return "chain"
|
|
|
|
if "store" in text and any(
|
|
n in text
|
|
for n in (
|
|
"unsupported",
|
|
"unknown",
|
|
"unrecognized",
|
|
"invalid",
|
|
"not support",
|
|
"not supported",
|
|
)
|
|
):
|
|
return "store"
|
|
|
|
if status in (404, 405):
|
|
return "responses"
|
|
responses_needles = (
|
|
"/responses",
|
|
"responses api",
|
|
"unknown endpoint",
|
|
"not implemented",
|
|
"invalid pathname",
|
|
"no route",
|
|
)
|
|
if any(n in text for n in responses_needles):
|
|
return "responses"
|
|
if status == 400 and ("responses" in text or "response.create" in text):
|
|
return "responses"
|
|
|
|
return "other"
|