From 298d174640c7e644ee50d1d17557acb188ae4d90 Mon Sep 17 00:00:00 2001 From: DrSmoothl <1787882683@qq.com> Date: Wed, 1 Jul 2026 18:58:22 +0800 Subject: [PATCH] refactor(webui): extract openSseChannel for the three EventSource openers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit stateStream / openLogStream / openDebugStream 三个 EventSource opener 近乎逐字重复 (token 守卫 → EventSource → onopen/onerror → onmessage try-parse-catch → close disposer),token/reconnect/畸形帧-跳过 语义各写一遍、易漂。 - 新增私有 openSseChannel(path, onMessage, onStatus?) 收敛骨架;各 opener 塌成 一行调用,consumer 逻辑作为 onMessage 传入(保持「回调在 try 内、抛错当畸形帧 吞掉」语义)。logs 的控制帧过滤(`'type' in parsed`)留在其 onMessage 回调里。 - StreamStatus 提到顶部 import,删掉 openDebugStream 的 inline import。 行为严格不变、对外签名不变:webui typecheck(tsc -b) + vite build 干净;对抗性 Agent 逐字对比 HEAD 确认三 opener 等价、onMessage-在-try-内 语义保留、无漏并入。 --- packages/webui/src/lib/api/client.ts | 76 +++++++++++----------------- 1 file changed, 30 insertions(+), 46 deletions(-) diff --git a/packages/webui/src/lib/api/client.ts b/packages/webui/src/lib/api/client.ts index b334c157..1219a085 100644 --- a/packages/webui/src/lib/api/client.ts +++ b/packages/webui/src/lib/api/client.ts @@ -12,6 +12,7 @@ import { type ProcessActionResult, type StateStreamEvent, type StateStreamOptions, + type StreamStatus, type TokenStore, } from './types'; import { localStorageTokenStore } from './token-store'; @@ -363,49 +364,40 @@ class HttpApiClient implements ApiClient { } stateStream(options: StateStreamOptions): () => void { - if (!this.currentToken) { options.onStatus?.('closed'); return () => {}; } - const url = `/api/state/stream?token=${encodeURIComponent(this.currentToken)}`; - const source = new EventSource(url); - // EventSource auto-reconnects on transport drop: `onerror` fires once - // when the connection is lost and the browser is about to retry, so - // a single 'reconnecting' status is sufficient — no manual reconnect - // timer needed. - source.onopen = () => options.onStatus?.('open'); - source.onerror = () => options.onStatus?.('reconnecting'); - source.onmessage = (event) => { - try { - options.onEvent(JSON.parse(event.data) as StateStreamEvent); - } catch { - /* malformed frame — skip; the next one will arrive normally */ - } - }; - return () => { source.close(); options.onStatus?.('closed'); }; + return this.openSseChannel('/api/state/stream', options.onEvent, options.onStatus); } // ---------- SSE ---------- - private openLogStream(options: LogsStreamOptions): () => void { - if (!this.currentToken) { - options.onStatus?.('closed'); - return () => {}; - } - const url = `/api/logs/stream?token=${encodeURIComponent(this.currentToken)}`; + /** + * Open a token-authed SSE channel to `path`, dispatching each parsed frame to + * `onMessage` and surfacing transport state ('open' / 'reconnecting' / + * 'closed') via `onStatus`. EventSource auto-reconnects on drop — `onerror` + * fires once per loss, so a single 'reconnecting' is enough. A malformed + * frame (or a throw from `onMessage`) is swallowed; the next frame arrives + * normally. The returned disposer closes the source and reports 'closed'. + */ + private openSseChannel( + path: string, + onMessage: (data: T) => void, + onStatus?: (s: StreamStatus) => void, + ): () => void { + if (!this.currentToken) { onStatus?.('closed'); return () => {}; } + const url = `${path}?token=${encodeURIComponent(this.currentToken)}`; const source = new EventSource(url); - source.onopen = () => options.onStatus?.('open'); - source.onerror = () => options.onStatus?.('reconnecting'); + source.onopen = () => onStatus?.('open'); + source.onerror = () => onStatus?.('reconnecting'); source.onmessage = (event) => { - try { - const parsed = JSON.parse(event.data) as LogEntry | { type: string }; - if ('type' in parsed) return; // control frame, not a log line - options.onLine(parsed); - } catch { - /* malformed frame, skip */ - } - }; - return () => { - source.close(); - options.onStatus?.('closed'); + try { onMessage(JSON.parse(event.data) as T); } catch { /* malformed frame — skip */ } }; + return () => { source.close(); onStatus?.('closed'); }; + } + + private openLogStream(options: LogsStreamOptions): () => void { + return this.openSseChannel('/api/logs/stream', (parsed) => { + if ('type' in parsed) return; // control frame, not a log line + options.onLine(parsed); + }, options.onStatus); } // Invoke a (stream) action and relay each `data: \n\n` SSE frame. Uses @@ -492,17 +484,9 @@ class HttpApiClient implements ApiClient { private openDebugStream( onMessage: (m: DebugStreamMessage) => void, - onStatus?: (s: import('./types').StreamStatus) => void, + onStatus?: (s: StreamStatus) => void, ): () => void { - if (!this.currentToken) { onStatus?.('closed'); return () => {}; } - const url = `/api/debug/stream?token=${encodeURIComponent(this.currentToken)}`; - const source = new EventSource(url); - source.onopen = () => onStatus?.('open'); - source.onerror = () => onStatus?.('reconnecting'); - source.onmessage = (event) => { - try { onMessage(JSON.parse(event.data) as DebugStreamMessage); } catch { /* skip malformed */ } - }; - return () => { source.close(); onStatus?.('closed'); }; + return this.openSseChannel('/api/debug/stream', onMessage, onStatus); } }