From 5fdcab444cecddc5c135004263891f27db0688a3 Mon Sep 17 00:00:00 2001 From: thzxx Date: Sat, 13 Jun 2026 09:27:14 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20logStreamProgress=20=E9=98=B2=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E6=9D=A1=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - addLog 支持 customId 参数 - _progressCreated 标记防止 addLog 在 DOM 未渲染前重复调用 - 已渲染后直接更新 DOM 文本 --- src/renderer/services/log-service.ts | 35 +++++++++++++++------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/src/renderer/services/log-service.ts b/src/renderer/services/log-service.ts index 6e32f7a..47b6fb4 100644 --- a/src/renderer/services/log-service.ts +++ b/src/renderer/services/log-service.ts @@ -125,10 +125,10 @@ function flushPending(): void { } /** 核心日志方法 — 永远不阻塞调用方 */ -export function addLog(level: LogLevel, message: string, detail?: string, icon?: string): void { +export function addLog(level: LogLevel, message: string, detail?: string, icon?: string, customId?: string): void { try { const entry: LogEntry = { - id: genId(), + id: customId || genId(), time: Date.now(), level, icon: icon || LEVEL_ICONS[level] || '•', @@ -177,21 +177,24 @@ export function logThink(thinking: string): void { export function logStream(msg: string): void { addLog('stream', msg); } /** 更新流式进度日志(原地更新,不追加新条目) */ +let _progressCreated = false; export function logStreamProgress(msg: string): void { - if (!logBodyEl) { addLog('stream', msg); return; } - const existing = document.getElementById('log-stream-progress'); - if (existing) { - // 更新已有条目 - const timeSpan = existing.querySelector('.log-time'); - if (timeSpan) timeSpan.textContent = formatTime(Date.now()); - const msgSpan = existing.querySelector('.log-msg'); - if (msgSpan) msgSpan.textContent = msg; - } else { - // 创建新条目 — 直接 addLog 然后手动修改 id - addLog('stream', msg); - // 最后一个日志条目就是我们刚加的 - const lastEntry = logBodyEl.querySelector('.log-stream:last-of-type'); - if (lastEntry) lastEntry.id = 'log-stream-progress'; + if (logBodyEl) { + const existing = document.getElementById('log-stream-progress'); + if (existing) { + // 已在 DOM 中 → 原地更新 + const timeSpan = existing.querySelector('.log-time'); + if (timeSpan) timeSpan.textContent = formatTime(Date.now()); + const msgSpan = existing.querySelector('.log-msg'); + if (msgSpan) msgSpan.textContent = msg; + return; + } + } + // DOM 中不存在,且还没创建过 → addLog(customId 确保渲染后 ID 正确) + // _progressCreated 防止 addLog 在未渲染前被重复调用 + if (!_progressCreated) { + _progressCreated = true; + addLog('stream', msg, undefined, undefined, 'log-stream-progress'); } }