面板:两边记忆绝对一致——DeepSeek与WorkBuddy统一用前端完整历史驱动(不再各自session记各自的)

This commit is contained in:
Zhuyuan Operations 2026-08-04 03:02:25 +08:00
parent 84b69c69ef
commit 451a3dca26

View File

@ -2840,17 +2840,14 @@ class Handler(http.server.BaseHTTPRequestHandler):
try: self._chunk_end() try: self._chunk_end()
except Exception: pass except Exception: pass
def _stream_opencode(self, message, model, eed_sid, token): def _stream_opencode(self, message, model, eed_sid, token, history=None):
"""OpenCode 引擎opencode run --agent egg -m <model> [-s oc_sid] --format json -- <msg> """OpenCode 引擎opencode run --agent egg -m <model> --format json -- <msg>
解析 NDJSON 事件流翻译成前端要的 delta/thinking/tool/tool_result/usage/error 解析 NDJSON 事件流翻译成前端要的 delta/thinking/tool/tool_result/usage/error
多轮对话靠 OpenCode 自己的 session后端维护 eed_sid -> oc_sid 映射""" 记忆统一以前端完整历史为准 WorkBuddy 共享同一份记忆 · 两边绝对一致
oc_sid = None 每次调用都用 build_prompt 把完整对话历史拼进消息不依赖 opencode session"""
_oc = OC_SESS.get(eed_sid) if history:
if _oc: message = build_prompt(history, message)
oc_sid = _oc.get("sid")
cmd = [OPENCODE, "run", "--agent", AGENT_NAME, "-m", model, "--format", "json", "--auto", "--thinking"] cmd = [OPENCODE, "run", "--agent", AGENT_NAME, "-m", model, "--format", "json", "--auto", "--thinking"]
if oc_sid:
cmd += ["-s", oc_sid]
cmd += ["--", message] cmd += ["--", message]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL, proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.DEVNULL,
text=True, bufsize=1, start_new_session=True, cwd=EED_CWD) text=True, bufsize=1, start_new_session=True, cwd=EED_CWD)
@ -2919,29 +2916,22 @@ class Handler(http.server.BaseHTTPRequestHandler):
def _stream_workbuddy(self, message, model, eed_sid, token, history=None): def _stream_workbuddy(self, message, model, eed_sid, token, history=None):
"""WorkBuddy 免费积分大脑codebuddy CLI 路径(苍耳 2026-08-04 接入)。 """WorkBuddy 免费积分大脑codebuddy CLI 路径(苍耳 2026-08-04 接入)。
model 形如 wb/<实际模型id>多轮会话靠 codebuddy 自己的会话文件续聊 model 形如 wb/<实际模型id>
本场 sid 已在 codebuddy 开过WB_SESS 有记录 --resume 接续记忆 记忆统一以前端完整历史为准 DeepSeek 共享同一份记忆 · 两边绝对一致
没开过 --session-id 开新场并把前端传来的 history DeepSeek 聊过的内容 每次调用都用 build_prompt 把完整对话历史拼进消息
拼进消息前缀 WorkBuddy 一上来就知道之前聊过什么跨大脑记忆共享 session-id 用固定 sid 便于 codebuddy 保持会话文件但记忆不依赖它"""
2026-08-04 修复之前靠猜 jsonl 路径判断 resume路径不对 每次当新会话 完全失忆"""
# wb/ 前缀剥掉,得到 codebuddy 认识的模型 idhy3 / glm-5.2 / kimi-k3-1 ... # wb/ 前缀剥掉,得到 codebuddy 认识的模型 idhy3 / glm-5.2 / kimi-k3-1 ...
cb_model = model.split("/", 1)[1] if "/" in model else model cb_model = model.split("/", 1)[1] if "/" in model else model
# 会话:面板侧 eed_sid 直接当 codebuddy 的 sid 用(稳定的 eed_ 前缀 = 同一场对话) # 会话:面板侧 eed_sid 直接当 codebuddy 的 sid 用(稳定的 eed_ 前缀 = 同一场对话)
sid = eed_sid if eed_sid and eed_sid.startswith("eed_") else ("eed_" + uuid.uuid4().hex[:12]) sid = eed_sid if eed_sid and eed_sid.startswith("eed_") else ("eed_" + uuid.uuid4().hex[:12])
if history:
message = build_prompt(history, message)
cmd = [CODEBUDDY, "--print", "--model", cb_model, cmd = [CODEBUDDY, "--print", "--model", cb_model,
"--tools", "Read,WebSearch,Bash", "--tools", "Read,WebSearch,Bash",
"--output-format", "stream-json", "--output-format", "stream-json",
"--system-prompt", EED_SYS] "--system-prompt", EED_SYS,
# 每次都用前端最新的完整历史当权威上下文DeepSeek/WB 切换后也不丢新增内容), "--session-id", sid, "-p", message]
# codebuddy 会话文件只用来给模型"参考之前自己说过什么"。 return self._stream_cmd(cmd, token)
if history:
message = build_prompt(history, message)
if sid in WB_SESS:
return self._stream_cmd(cmd + ["--resume", sid, "-p", message], token)
acc = self._stream_cmd(cmd + ["--session-id", sid, "-p", message], token)
WB_SESS[sid] = {"ts": time.time()}
_save_wb_sess()
return acc
def _stream_cmd(self, cmd, token=""): def _stream_cmd(self, cmd, token=""):
"""跑一次 codebuddy 子进程,边解析边把事件流式推给前端,返回累计文本。""" """跑一次 codebuddy 子进程,边解析边把事件流式推给前端,返回累计文本。"""
@ -3142,7 +3132,8 @@ class Handler(http.server.BaseHTTPRequestHandler):
# WorkBuddy 免费积分大脑:走 codebuddy CLI不花 DeepSeek 余额) # WorkBuddy 免费积分大脑:走 codebuddy CLI不花 DeepSeek 余额)
acc = self._stream_workbuddy(message, model, sid, token, history) acc = self._stream_workbuddy(message, model, sid, token, history)
else: else:
acc = self._stream_opencode(message, model, sid, token) # DeepSeek 大脑:同样基于前端完整历史(与 WB 共享同一份记忆)
acc = self._stream_opencode(message, model, sid, token, history)
if not acc: if not acc:
acc = "(蛋蛋没回话,换个说法试试~)" acc = "(蛋蛋没回话,换个说法试试~)"
self._event("done", {"text": acc, "session_id": sid, "compacted": False}) self._event("done", {"text": acc, "session_id": sid, "compacted": False})