feat: IM式跨端实时同步——message_persisted 广播 + 前端按id去重追加
- 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:
1 parent
bcc53d68f3
commit
1bf60c1ae4
9 files changed
+260
-38
No files matched your search
@@ -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;
|
||||
|
||||
@@ -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) =>
|
||||
|
||||
Reference in new issue
Block a user