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
+6 -17
View File
@@ -20,7 +20,6 @@ from domain.models import (
StreamEvent,
)
from domain.ports import ChatStreamer, MessageRepository, SessionRepository
from infrastructure.llm.api_style import resolve_use_responses_api
class ChatService:
@@ -70,7 +69,6 @@ class ChatService:
provider_id=session.llm_provider_id,
model=session.llm_model,
)
# 回落默认或消歧后写回会话;选型变化则清链
changed = False
if session.llm_model != llm.model:
session.llm_model = llm.model
@@ -82,10 +80,9 @@ class ChatService:
session.llm_response_chain = None
await self._session_repo.update(session)
use_responses = resolve_use_responses_api(llm.api_style, llm.base_url)
chain_id = _usable_chain_id(session.llm_response_chain, llm, use_responses)
# 链是否可用由 streamer 按 store 能力决定;此处只做选型匹配
chain_id = _usable_chain_id(session.llm_response_chain, llm)
if session.llm_response_chain and not chain_id:
# 链与当前选型不匹配,落库清空避免脏数据
session.llm_response_chain = None
await self._session_repo.update(session)
@@ -157,7 +154,6 @@ class ChatService:
parts: list[str] = []
new_response_id: str | None = None
used_chain = bool(chain_id)
async for event in self._streamer.stream_reply(
llm=llm,
history=history,
@@ -187,26 +183,19 @@ class ChatService:
)
)
session.last_message_at = datetime.utcnow()
if use_responses and new_response_id:
if new_response_id:
session.llm_response_chain = LlmResponseChain(
provider_id=llm.provider_id,
model=llm.model,
previous_response_id=new_response_id,
)
elif used_chain and not new_response_id:
# 续链失败已 rebuild 但仍无 id:清掉以免下次再踩坑
session.llm_response_chain = None
elif not use_responses:
else:
session.llm_response_chain = None
await self._session_repo.update(session)
def _usable_chain_id(
chain: LlmResponseChain | None,
llm,
use_responses: bool,
) -> str | None:
if not use_responses or chain is None:
def _usable_chain_id(chain: LlmResponseChain | None, llm) -> str | None:
if chain is None:
return None
rid = (chain.previous_response_id or "").strip()
if not rid:
+4 -3
View File
@@ -12,9 +12,10 @@ _BASE_SYSTEM = (
"需要经营数据时优先调用可用工具。"
"工具可多轮检索,但对用户只报结论;默认不汇报检索过程、未命中关键词、搜了几轮或工具名。"
"不要自我纠错或评论上一轮话术(如「不该露编号」)。"
"你具备跨会话的用户级长期记忆,但内容不会自动注入对话。"
"需要称呼、偏好或用户事实时:先 memory_search,必要时再 memory_load;"
"禁止编造未载入的记忆,也不要声称没有长期记忆能力。"
"【本会话上下文】请求里已包含本会话近期的用户与助手原文,你可以直接看见并引用;"
"禁止声称「只收到当前一句」或「前面几轮不会带过来」——那是错误描述。"
"【长期记忆】跨会话的用户级记忆不会自动注入;需要称呼、偏好或用户事实时:"
"先 memory_search,必要时再 memory_load;禁止编造未载入的记忆,也不要声称没有长期记忆能力。"
"介绍记忆:自然语言短列表即可;禁止输出 ref、id、字段名、更新时间或表格。"
"问「有哪些记忆」:有则直接列举命中摘要,无则一句「暂时没有相关记忆」;"
"不必强调「就这一条」,未问勿主动建议再记店铺/商品/口径。"