feat: IM式跨端实时同步——message_persisted 广播 + 前端按id去重追加
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

- history.Store 新增 AppendFull(返回含 id/createdAt 的完整消息),Append 签名不变
- session_orchestrator 加可空 OnPersisted;persistUserMessages/persistAssistantReply 落库后回调广播
- server.go newOrchestrator 接 OnPersisted → hub.broadcast message_persisted(thinking 剥除)
- 前端 ws.ts 加 message_persisted case(仅 activeSessionId 匹配才处理);appendOrUpdateMessage 升级为 id 精确upsert+内容/时间归一
- 实测:双 WS,A stream 发消息,B 免刷新收到 user+assistant 各1条(count=2 PASS)
- 红线: 身份仍靠 actor 元数据,不靠提示词;用户/助手消息、本端/异端统一路径
This commit is contained in:
Aska committed 2026-09-07 04:16:29 +08:00
1 parent bcc53d68f3
commit 1bf60c1ae4
9 files changed
+260 -38

No files matched your search

+34 -1
View File
@@ -1,7 +1,7 @@
import { useAppStore } from "../store/useAppStore";
import { useModeStore } from "../store/useModeStore";
import { getAuthToken } from "./api";
import { MODES, type AgentInfo, type AwakenState, type FlowGraph, type Mode, type ToolUsed, type WsUpEvent } from "./types";
import { MODES, type AgentInfo, type AwakenState, type ChatMessage, type FlowGraph, type Mode, type ToolUsed, type WsUpEvent } from "./types";
import { now, uid } from "./util";
/**
@@ -156,6 +156,28 @@ function parseGraph(payload: unknown): FlowGraph | null {
return { id: typeof r.id === "string" ? r.id : "graph", nodes, edges };
}
/** Build a ChatMessage from a `message_persisted` payload (design/16 §3.3/§4.2):
* the backend broadcast a fully-persisted message carrying the backend-generated
* id. mode is validated against the known modes so a bad value never reaches the
* UI; createdAt falls back to now when the wire omits it. */
function parsePersistedMessage(v: unknown): ChatMessage | null {
const r = toRecord(v);
if (!r) return null;
const id = typeof r.id === "string" && r.id ? r.id : "";
const role = r.role;
if (!id || (role !== "user" && role !== "assistant" && role !== "system")) return null;
return {
id,
sessionId: typeof r.sessionId === "string" && r.sessionId ? r.sessionId : undefined,
role,
content: typeof r.content === "string" ? r.content : "",
mode: typeof r.mode === "string" && MODES.includes(r.mode as Mode) ? (r.mode as Mode) : undefined,
mood: typeof r.mood === "string" ? r.mood : undefined,
image: typeof r.image === "string" ? r.image : undefined,
createdAt: typeof r.createdAt === "number" ? r.createdAt : now(),
};
}
function dispatch(ev: { type: string; data: unknown }): void {
const s = useAppStore.getState();
const r = toRecord(ev.data);
@@ -191,6 +213,17 @@ function dispatch(ev: { type: string; data: unknown }): void {
// do NOT end the stream here (stream_end owns that).
break;
}
case "message_persisted": {
// design/16 §4.2 — a user or assistant message was persisted and broadcast
// with its backend id. Only surface messages for the active session; the
// store dedups by id and adopts the local optimistic bubble (uid → backend
// id), so both the originating and remote browsers converge on one message.
const msg = parsePersistedMessage(r?.message);
if (msg && msg.sessionId === s.activeSessionId) {
s.appendOrUpdateMessage(msg);
}
break;
}
case "thinking.start": {
pendingThinking = "";
break;