feat(web,ws): 会话级消息缓存 + 图桶 + 忙态按会话过滤(裁决 16.4.5/16.4.2 + 修 16.4.1 的帧缺口)

- 16.4.5:非激活会话的帧**不再丢弃**,按自己的 sessionId 落进该会话桶(新增 store/sessionCache.ts 纯函数 +
  useAppStore.sessionMessages/flowGraphs);切会话改为「存回/取出」而非清空;流式增量 O(1) 定位桶、
  只延长已存在的助手气泡(不凭一帧 delta 凭空造消息)
- 16.4.2:图按会话入桶(上限 12 个),Backthink 页取 sess-backthink 的图而不再被用户会话图顶替;
  空态区分「实时通道未连接」与「尚未产生链路图」;wire_runtime 的图源白名单注释按裁决口径重写(零行为改动)
- 修 16.4.1 的缺口:handlers/session.ts 补 `session_idle` / `session_interrupted`(此前落到未知帧兜底 ⇒
  sessionBusy 可能卡 true ⇒ 预约永不落地);忙态**置忙与置闲两侧**统一按 frameIsActive 过滤(只过滤置闲
  会让后台会话的忙帧点亮徽标而它自己的 idle 被丢 ⇒ 永久卡忙)
- 清理:删除 lib/streamScope.ts(口径已被 16.4.5 推翻)与只剩测试在用的 messageEventApplies
- 复核:tsc 0 错 + vite build 成功;messageSync 69 / session 19 项逻辑测试;4 条变异反证
This commit is contained in:
AskaEth committed 2026-10-04 13:41:02 +08:00
1 parent f454273c5a
commit b480b04dd0
15 files changed
+733 -300

No files matched your search

+20 -23
View File
@@ -1,12 +1,12 @@
import { messageEventApplies } from "../../../store/useAppStore";
import { MODES, type ChatMessage, type Mode } from "../../types";
import { parseAdminChannelMessage, parsePersistedMessage, toRecord } from "../../wsParse";
import type { FrameEvent, FramePayload, StoreState } from "./shared";
/** The fields of a `message_edited` payload the chat needs: the edited message's
* id and its new content. `sessionId` is carried inside the broadcast DTO (and on
* the frame envelope) so the frame can be filtered; `mode`/`mood` are optional —
* an edit never changes them, but the DTO carries them for completeness. */
* the frame envelope) so the store can confine the patch to that session's bucket;
* `mode`/`mood` are optional — an edit never changes them, but the DTO carries them
* for completeness. */
interface MessageEdited {
id: string;
content: string;
@@ -42,39 +42,36 @@ export function handleMessageEvent(ev: FrameEvent, r: FramePayload, s: StoreStat
switch (ev.type) {
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.
// with its backend id. 裁决 16.4.5: the frame is written into the bucket its
// own `sessionId` names (the store routes it), so a background conversation
// keeps accumulating instead of being dropped. Within that bucket 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);
}
if (msg) s.appendOrUpdateMessage(msg);
return true;
}
case "message_edited": {
// design/16 §4.2 (D3 edit path) — the stored message's content was changed
// and broadcast with the updated DTO. The backend row stays the single source
// of truth; this only re-renders the bubble, and only in the session it
// belongs to (same filter as message_persisted). Patch by id — an id we do not
// hold is another session's (or an already-gone) message, never a new row.
// of truth; this only re-renders the bubble of the session the frame names
// (the store confines the patch to that session's bucket). Patch by id — an
// id we do not hold is another session's (or an already-gone) message, never
// a new row.
const edit = parseMessageEdited(ev.data);
if (!edit) return true;
if (messageEventApplies(edit.sessionId ?? ev.sessionId, s.activeSessionId)) {
const patch: Partial<ChatMessage> = { content: edit.content };
if (edit.mode) patch.mode = edit.mode;
if (edit.mood) patch.mood = edit.mood;
s.updateMessageById(edit.id, patch);
}
const patch: Partial<ChatMessage> = { content: edit.content };
if (edit.mode) patch.mode = edit.mode;
if (edit.mood) patch.mood = edit.mood;
s.updateMessageById(edit.id, patch, edit.sessionId ?? ev.sessionId);
return true;
}
case "message_deleted": {
// design/16 §4.2 (D3 delete path) — the message was removed server-side, so
// drop it by id instead of waiting for a manual re-read. The id is globally
// unique; sessionId is advisory and, when present, must be the active one.
// drop it by id instead of waiting for a manual re-read. The frame's session
// scopes the removal; without one the store searches by id (globally unique).
const id = r && typeof r.id === "string" ? r.id : "";
if (id && messageEventApplies(ev.sessionId, s.activeSessionId)) {
s.removeMessageById(id);
}
if (id) s.removeMessageById(id, ev.sessionId);
return true;
}
case "channel_persisted": {