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

+44 -5
View File
@@ -61,7 +61,9 @@ export interface AppStore {
setMessages: (list: ChatMessage[]) => void;
/** Prepend an older page (chronological) onto the active session's messages. */
prependMessages: (list: ChatMessage[]) => void;
/** Append a new message, or merge by id if it already exists (WS/dedup). */
/** Append a message, or merge by id if it already exists. On message_persisted
* it also adopts a same-role local optimistic bubble (rewriting its uid to the
* backend id) so a message never renders twice (design/16 §4.3 dedup). */
appendOrUpdateMessage: (msg: ChatMessage) => void;
addUserMessage: (text: string, mode?: Mode, image?: string) => void;
@@ -135,11 +137,48 @@ export const useAppStore = create<AppStore>()(
}),
appendOrUpdateMessage: (msg) =>
set((s) => {
// Exact id match → merge. Keeps local streaming/tools/thinking, takes
// the backend's content/mode/mood as truth.
const idx = s.messages.findIndex((m) => m.id === msg.id);
if (idx === -1) return { messages: [...s.messages, msg] };
const messages = s.messages.slice();
messages[idx] = { ...messages[idx], ...msg };
return { messages };
if (idx !== -1) {
const messages = s.messages.slice();
messages[idx] = { ...messages[idx], ...msg };
return { messages };
}
// No exact id: adopt a local optimistic bubble (no sessionId) of the
// same role, so a message the local browser already rendered
// optimistically is never duplicated when message_persisted arrives
// (design/16 §4.3 dedup by id). Prefer a content match, else the
// nearest by createdAt; scan newest-first.
let best = -1;
let bestDist = Number.MAX_SAFE_INTEGER;
for (let i = s.messages.length - 1; i >= 0; i--) {
const m = s.messages[i];
if (m.sessionId || m.role !== msg.role || m.id === msg.id) continue;
const dist = m.content === msg.content ? 0 : Math.abs(m.createdAt - msg.createdAt);
if (dist < bestDist) {
bestDist = dist;
best = i;
}
}
if (best !== -1) {
const m = s.messages[best];
const messages = s.messages.slice();
messages[best] = {
...m,
id: msg.id,
sessionId: msg.sessionId ?? m.sessionId,
content: msg.content || m.content,
mode: msg.mode ?? m.mode,
mood: msg.mood ?? m.mood,
image: msg.image ?? m.image,
createdAt: msg.createdAt ?? m.createdAt,
};
return { messages };
}
// No local bubble (e.g. a remote browser that never optimistically
// rendered this message): append.
return { messages: [...s.messages, msg] };
}),
addUserMessage: (text, mode, image) =>