fix: logStreamProgress 防重复条目

- addLog 支持 customId 参数
- _progressCreated 标记防止 addLog 在 DOM 未渲染前重复调用
- 已渲染后直接更新 DOM 文本
This commit is contained in:
thzxx
2026-06-13 09:27:14 +08:00
parent ad5b67ea3e
commit 5fdcab444c
+13 -10
View File
@@ -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 { try {
const entry: LogEntry = { const entry: LogEntry = {
id: genId(), id: customId || genId(),
time: Date.now(), time: Date.now(),
level, level,
icon: icon || LEVEL_ICONS[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); } export function logStream(msg: string): void { addLog('stream', msg); }
/** 更新流式进度日志(原地更新,不追加新条目) */ /** 更新流式进度日志(原地更新,不追加新条目) */
let _progressCreated = false;
export function logStreamProgress(msg: string): void { export function logStreamProgress(msg: string): void {
if (!logBodyEl) { addLog('stream', msg); return; } if (logBodyEl) {
const existing = document.getElementById('log-stream-progress'); const existing = document.getElementById('log-stream-progress');
if (existing) { if (existing) {
// 更新已有条目 // 已在 DOM 中 → 原地更新
const timeSpan = existing.querySelector('.log-time'); const timeSpan = existing.querySelector('.log-time');
if (timeSpan) timeSpan.textContent = formatTime(Date.now()); if (timeSpan) timeSpan.textContent = formatTime(Date.now());
const msgSpan = existing.querySelector('.log-msg'); const msgSpan = existing.querySelector('.log-msg');
if (msgSpan) msgSpan.textContent = msg; if (msgSpan) msgSpan.textContent = msg;
} else { return;
// 创建新条目 — 直接 addLog 然后手动修改 id }
addLog('stream', msg); }
// 最后一个日志条目就是我们刚加的 // DOM 中不存在,且还没创建过 → addLog(customId 确保渲染后 ID 正确)
const lastEntry = logBodyEl.querySelector('.log-stream:last-of-type'); // _progressCreated 防止 addLog 在未渲染前被重复调用
if (lastEntry) lastEntry.id = 'log-stream-progress'; if (!_progressCreated) {
_progressCreated = true;
addLog('stream', msg, undefined, undefined, 'log-stream-progress');
} }
} }