feat: 支持 Completions/Responses 双协议,并在同选型下续链加速

This commit is contained in:
2026-09-24 17:10:34 +08:00
parent 9c2c1ed061
commit d20c6a1302
26 changed files with 533 additions and 43 deletions
@@ -11,6 +11,7 @@ from motor.motor_asyncio import AsyncIOMotorDatabase
from domain.models import (
ChatMessage,
LlmResponseChain,
MemoryKind,
MemoryStatus,
MessageRole,
@@ -36,6 +37,30 @@ def _optional_int(v: Any) -> int | None:
return None
def _chain_to_doc(chain: LlmResponseChain | None) -> dict[str, Any] | None:
if chain is None or not (chain.previous_response_id or "").strip():
return None
return {
"provider_id": chain.provider_id,
"model": chain.model,
"previous_response_id": chain.previous_response_id.strip(),
}
def _chain_from_doc(raw: Any) -> LlmResponseChain | None:
if not isinstance(raw, dict):
return None
rid = str(raw.get("previous_response_id") or "").strip()
model = str(raw.get("model") or "").strip()
if not rid or not model:
return None
return LlmResponseChain(
provider_id=_optional_int(raw.get("provider_id")),
model=model,
previous_response_id=rid,
)
class MongoSessionRepository(SessionRepository):
def __init__(self, db: AsyncIOMotorDatabase) -> None:
self._col = db.agent_sessions
@@ -52,6 +77,7 @@ class MongoSessionRepository(SessionRepository):
"scene_ref": session.scene_ref,
"llm_provider_id": session.llm_provider_id,
"llm_model": session.llm_model,
"llm_response_chain": _chain_to_doc(session.llm_response_chain),
"created_at": session.created_at,
"updated_at": session.updated_at,
"last_message_at": session.last_message_at,
@@ -135,6 +161,7 @@ class MongoSessionRepository(SessionRepository):
"scene_ref": session.scene_ref,
"llm_provider_id": session.llm_provider_id,
"llm_model": session.llm_model,
"llm_response_chain": _chain_to_doc(session.llm_response_chain),
"updated_at": session.updated_at,
"last_message_at": session.last_message_at,
}
@@ -155,6 +182,7 @@ class MongoSessionRepository(SessionRepository):
scene_ref=doc.get("scene_ref"),
llm_provider_id=_optional_int(doc.get("llm_provider_id")),
llm_model=doc.get("llm_model"),
llm_response_chain=_chain_from_doc(doc.get("llm_response_chain")),
created_at=_dt(doc.get("created_at")),
updated_at=_dt(doc.get("updated_at")),
last_message_at=doc.get("last_message_at"),
@@ -9,6 +9,7 @@ import redis.asyncio as redis
from config.settings import Settings
from domain.models import LlmRuntimeConfig
from domain.ports import LlmRuntimeCache
from infrastructure.llm.api_style import normalize_api_style
class RedisLlmRuntimeCache(LlmRuntimeCache):
@@ -43,6 +44,7 @@ class RedisLlmRuntimeCache(LlmRuntimeCache):
provider_id=_optional_int(data.get("provider_id")) or provider_id,
extra_headers=data.get("extra_headers") or {},
context_window=_optional_int(data.get("context_window")),
api_style=normalize_api_style(data.get("api_style")),
)
if model and cfg.model and cfg.model != model:
return None
@@ -63,6 +65,7 @@ class RedisLlmRuntimeCache(LlmRuntimeCache):
"model": config.model,
"extra_headers": config.extra_headers,
"context_window": config.context_window,
"api_style": normalize_api_style(config.api_style),
}
await self._r.set(
self._key(company_id, config.provider_id, config.model),