feat(channels): 适配器日志接 WS 实时推送(落库即秒推, channel_persisted)
CI / Backend (build / vet / test / cross-compile) (push) Has been cancelled
CI / Frontend (tsc + vite build) (push) Has been cancelled

后端: persistIsolated 由 _ = full(不广播)改为 notifyPersisted(full); BroadcastPersisted 按
sessionId 分流——main/web:<uuid> → message_persisted(Web聊天同步), 隔离 sess-xxx → channel_persisted
(适配器日志专用, 永不写主会话 web, 红线). payload data:{message:historyMessage}(strip thinking).
前端: ws.ts 新增 channel_persisted case→parseAdminChannelMessage→pushChannelEvent; wsParse.ts 纯函数;
useAppStore 新增 channelEvents(最新在前,按id去重,cap200,不持久化); channelLogList 订阅过滤
(单adapter按adapterId/总览取adapterId非空)merge进列表(仅追加、id去重、不整页重载),保留拉历史+10s兜底.
Docs/api-client.md §3.4 补 channel_persisted 契约.
验收: 后端 build/vet/test 全绿+windows/darwin交叉编译绿; 前端 npm run build 99模块绿.
注: 偏离=web:<uuid> 归 message_persisted(保Web副会话同步), 隔离永不带web前缀红线仍守.
This commit is contained in:
Aska committed 2026-09-07 18:51:00 +08:00
1 parent 9e7b4ebaac
commit 3386679d45
10 files changed
+180 -23

No files matched your search

@@ -1,6 +1,7 @@
import { adminChannelMessages, adminChannelLogOverview } from "../../../lib/api";
import type { AdminChannelMessage } from "../../../lib/types";
import { useAsyncData } from "../../../lib/useAsyncData";
import { useAppStore } from "../../../store/useAppStore";
import { Button, Tag } from "../../../components/primitives";
import { Notice } from "../../components";
import { useAutoReload } from "../../hooks";
@@ -48,7 +49,20 @@ export function ChannelLogList({
[adapter, limit],
);
useAutoReload(reload, 10000);
const msgs = data ? [...data].reverse() : data;
// Realtime tail pushed by `channel_persisted` WS frames (design/16 §4.6 §4). The
// store keeps them newest-first, capped at 200 and deduped by id; we filter to the
// surface this list renders (single adapter, or the overview which shows adapter
// messages only), flip to oldest→newest, then append only ids the history page has
// not already covered. This makes a new persisted message appear without a reload;
// the 10s useAutoReload stays as a fallback for events the socket missed.
const channelEvents = useAppStore((s) => s.channelEvents);
const base = data ? [...data].reverse() : [];
const live = channelEvents
.filter((m) => (adapter ? m.adapterId === adapter : m.adapterId))
.reverse();
const seen = new Set(base.map((m) => m.id));
const msgs = [...base, ...live.filter((m) => !seen.has(m.id))];
return (
<div className={className}>
+10
View File
@@ -2,6 +2,7 @@ import { useAppStore } from "../store/useAppStore";
import { useModeStore } from "../store/useModeStore";
import { getAuthToken, isTokenExpired, onAuthExpired, setAuthToken } from "./api";
import {
parseAdminChannelMessage,
parseAgent,
parseAwakenTool,
parseGraph,
@@ -85,6 +86,15 @@ function dispatch(ev: { type: string; data: unknown }): void {
}
break;
}
case "channel_persisted": {
// design/16 §4.6 §4 — an isolated (group/stranger) channel message was
// persisted and broadcast for the IM adapter log page. The web chat has no
// handler for this type, so it never reaches the active conversation; the
// admin adapter log consumes it via `channelEvents` to append in realtime.
const msg = parseAdminChannelMessage(r?.message);
if (msg) s.pushChannelEvent(msg);
break;
}
case "thinking.start": {
pendingThinking = "";
break;
+26 -1
View File
@@ -1,4 +1,4 @@
import { MODES, type AgentInfo, type ChatMessage, type FlowGraph, type Mode, type ToolUsed } from "./types";
import { MODES, type AdminChannelMessage, type AgentInfo, type ChatMessage, type FlowGraph, type Mode, type ToolUsed } from "./types";
import { now, uid } from "./util";
/**
@@ -158,3 +158,28 @@ export function parsePersistedMessage(v: unknown): ChatMessage | null {
createdAt: typeof r.createdAt === "number" ? r.createdAt : now(),
};
}
/** Build an AdminChannelMessage from a `channel_persisted` payload (design/16
* §4.6 §4): the backend broadcast a fully-persisted isolated (group/stranger)
* channel message carrying the backend id + adapter id, for the IM adapter log
* page to append in realtime. Requires id + sessionId; role/content fall back to
* defaults so a malformed frame is dropped rather than rendered. */
export function parseAdminChannelMessage(v: unknown): AdminChannelMessage | null {
const r = toRecord(v);
if (!r) return null;
const id = typeof r.id === "string" && r.id ? r.id : "";
const sessionId = typeof r.sessionId === "string" && r.sessionId ? r.sessionId : "";
if (!id || !sessionId) return null;
return {
id,
sessionId,
role: typeof r.role === "string" ? r.role : "user",
content: typeof r.content === "string" ? r.content : "",
mode: typeof r.mode === "string" ? r.mode : undefined,
platform: typeof r.platform === "string" && r.platform ? r.platform : undefined,
deviceId: typeof r.deviceId === "string" && r.deviceId ? r.deviceId : undefined,
adapterId: typeof r.adapterId === "string" && r.adapterId ? r.adapterId : undefined,
senderName: typeof r.senderName === "string" && r.senderName ? r.senderName : undefined,
createdAt: typeof r.createdAt === "number" ? r.createdAt : now(),
};
}
+15
View File
@@ -1,6 +1,7 @@
import { create } from "zustand";
import { persist } from "zustand/middleware";
import type {
AdminChannelMessage,
AgentInfo,
AwakenState,
BackthinkItem,
@@ -45,6 +46,13 @@ export interface AppStore {
sessionBusy: boolean;
pendingCount: number;
/** 适配器消息日志实时事件(`channel_persisted` WS 帧,design/16 §4.6 §4)——
* 隔离会话(群聊/陌生人)落库后秒推,供 IM 适配器日志页增量插入。最新在前,
* 最多保留最近 ~200 条,按 id 去重。web 聊天无此字段的消费者。 */
channelEvents: AdminChannelMessage[];
/** Push one `channel_persisted` message (newest first, dedup by id, capped ~200). */
pushChannelEvent: (msg: AdminChannelMessage) => void;
/** 当前 persona(web 主会话=默认 persona)。加载前/失败时为 null,头像回退中性默认占位。 */
currentPersona: PersonaRecord | null;
@@ -116,6 +124,7 @@ export const useAppStore = create<AppStore>()(
pendingCount: 0,
currentPersona: null,
taskLogs: {},
channelEvents: [],
setWsStatus: (status) => set({ wsStatus: status }),
setCurrentPersona: (p) => set({ currentPersona: p }),
@@ -316,6 +325,12 @@ export const useAppStore = create<AppStore>()(
return { taskLogs };
}),
pushChannelEvent: (msg) =>
set((s) => {
if (s.channelEvents.some((m) => m.id === msg.id)) return s;
return { channelEvents: [msg, ...s.channelEvents].slice(0, 200) };
}),
resetChat: () =>
set((s) => ({
messages: [],