import { outputConfig } from '../config.js'; // 输出缓冲区(用于聚合输出后定时发送) interface BufferedOutput { key: string; chatId: string; messageId: string | null; thinkingMessageId: string | null; replyMessageId: string | null; sessionId: string; content: string[]; thinking: string[]; // 存储思考片段 tools: Array<{ name: string; status: 'pending' | 'running' | 'completed' | 'failed'; output?: string; }>; finalText: string; finalThinking: string; openCodeMsgId: string; showThinking: boolean; dirty: boolean; lastUpdate: number; timer: NodeJS.Timeout | null; status: 'running' | 'completed' | 'failed' | 'aborted'; } class OutputBuffer { private buffers: Map = new Map(); private updateCallback: ((buffer: BufferedOutput) => Promise) | null = null; // 设置更新回调 setUpdateCallback(callback: (buffer: BufferedOutput) => Promise): void { this.updateCallback = callback; } // 创建或获取缓冲区 getOrCreate(key: string, chatId: string, sessionId: string, replyMessageId: string | null): BufferedOutput { let buffer = this.buffers.get(key); if (!buffer) { buffer = { key, chatId, messageId: null, thinkingMessageId: null, replyMessageId, sessionId, content: [], thinking: [], tools: [], finalText: '', finalThinking: '', openCodeMsgId: '', showThinking: false, dirty: false, lastUpdate: Date.now(), timer: null, status: 'running', }; this.buffers.set(key, buffer); } return buffer; } // 追加内容 append(key: string, text: string): void { const buffer = this.buffers.get(key); if (!buffer) return; buffer.content.push(text); buffer.dirty = true; this.scheduleUpdate(key); } // 追加思考内容 appendThinking(key: string, text: string): void { const buffer = this.buffers.get(key); if (!buffer) return; buffer.thinking.push(text); buffer.dirty = true; this.scheduleUpdate(key); } // 设置正文卡片消息ID setMessageId(key: string, messageId: string): void { const buffer = this.buffers.get(key); if (buffer) { buffer.messageId = messageId; } } // 设置思考卡片消息ID setThinkingMessageId(key: string, messageId: string): void { const buffer = this.buffers.get(key); if (buffer) { buffer.thinkingMessageId = messageId; } } // 设置工具状态快照 setTools( key: string, tools: Array<{ name: string; status: 'pending' | 'running' | 'completed' | 'failed'; output?: string }> ): void { const buffer = this.buffers.get(key); if (buffer) { buffer.tools = [...tools]; buffer.dirty = true; this.scheduleUpdate(key); } } touch(key: string): void { const buffer = this.buffers.get(key); if (!buffer) return; buffer.dirty = true; this.scheduleUpdate(key); } // 设置最终文本和思考快照 setFinalSnapshot(key: string, text: string, thinking: string): void { const buffer = this.buffers.get(key); if (buffer) { buffer.finalText = text; buffer.finalThinking = thinking; } } // 设置 OpenCode 消息ID setOpenCodeMsgId(key: string, openCodeMsgId: string): void { const buffer = this.buffers.get(key); if (buffer) { buffer.openCodeMsgId = openCodeMsgId; } } // 设置思考展开状态 setShowThinking(key: string, showThinking: boolean): void { const buffer = this.buffers.get(key); if (buffer) { buffer.showThinking = showThinking; } } // 设置状态 setStatus(key: string, status: BufferedOutput['status']): void { const buffer = this.buffers.get(key); if (buffer) { buffer.status = status; buffer.dirty = true; // 状态变化时立即触发更新 this.triggerUpdate(key); } } // 调度更新 private scheduleUpdate(key: string): void { const buffer = this.buffers.get(key); if (!buffer || buffer.timer) return; buffer.timer = setTimeout(() => { // 再次检查 buffer 是否仍然存在(可能在定时器触发前被 clearAll 清理) const currentBuffer = this.buffers.get(key); if (!currentBuffer) return; this.triggerUpdate(key); }, outputConfig.updateInterval); } // 触发更新 private async triggerUpdate(key: string): Promise { const buffer = this.buffers.get(key); if (!buffer) return; // 清除定时器 if (buffer.timer) { clearTimeout(buffer.timer); buffer.timer = null; } buffer.lastUpdate = Date.now(); const shouldUpdate = buffer.dirty || buffer.status !== 'running'; // 调用回调 if (this.updateCallback && shouldUpdate) { buffer.dirty = false; try { await this.updateCallback(buffer); } catch (error) { buffer.dirty = true; throw error; } } } // 获取并清空内容 getAndClear(key: string): { text: string; thinking: string } { const buffer = this.buffers.get(key); if (!buffer) return { text: '', thinking: '' }; const text = buffer.content.join(''); buffer.content = []; const thinking = buffer.thinking.join(''); buffer.thinking = []; return { text, thinking }; } // 清理缓冲区 clear(key: string): void { const buffer = this.buffers.get(key); if (buffer) { if (buffer.timer) { clearTimeout(buffer.timer); } this.buffers.delete(key); } } // 获取缓冲区 get(key: string): BufferedOutput | undefined { return this.buffers.get(key); } // 中断输出 abort(key: string): void { const buffer = this.buffers.get(key); if (buffer) { if (buffer.timer) { clearTimeout(buffer.timer); buffer.timer = null; } buffer.status = 'aborted'; // 触发最后一次更新 this.triggerUpdate(key); // 清理缓冲区 this.clear(key); } } // 清理所有缓冲区和定时器 clearAll(): void { for (const buffer of this.buffers.values()) { if (buffer.timer) { clearTimeout(buffer.timer); buffer.timer = null; } } this.buffers.clear(); } } // 单例导出 export const outputBuffer = new OutputBuffer();