diff --git a/src/claude/claude-kiro.js b/src/claude/claude-kiro.js index 585a1b5..719fa5c 100644 --- a/src/claude/claude-kiro.js +++ b/src/claude/claude-kiro.js @@ -669,14 +669,31 @@ async initializeAuth(forceRefresh = false) { let fullContent = ''; const toolCalls = []; let currentToolCallDict = null; + // console.log(`rawStr=${rawStr}`); - const eventBlockRegex = /event({.*?(?=event{|$))/gs; + // 改进的 SSE 事件解析:匹配 :message-typeevent 后面的 JSON 数据 + // 使用更精确的正则来匹配 SSE 格式的事件 + const sseEventRegex = /:message-typeevent(\{[^]*?(?=:event-type|$))/g; + const legacyEventRegex = /event(\{.*?(?=event\{|$))/gs; + + // 首先尝试使用 SSE 格式解析 + let matches = [...rawStr.matchAll(sseEventRegex)]; + + // 如果 SSE 格式没有匹配到,回退到旧的格式 + if (matches.length === 0) { + matches = [...rawStr.matchAll(legacyEventRegex)]; + } - for (const match of rawStr.matchAll(eventBlockRegex)) { + for (const match of matches) { const potentialJsonBlock = match[1]; + if (!potentialJsonBlock || potentialJsonBlock.trim().length === 0) { + continue; + } + + // 尝试找到完整的 JSON 对象 let searchPos = 0; while ((searchPos = potentialJsonBlock.indexOf('}', searchPos + 1)) !== -1) { - const jsonCandidate = potentialJsonBlock.substring(0, searchPos + 1); + const jsonCandidate = potentialJsonBlock.substring(0, searchPos + 1).trim(); try { const eventData = JSON.parse(jsonCandidate); @@ -700,22 +717,30 @@ async initializeAuth(forceRefresh = false) { const args = JSON.parse(currentToolCallDict.function.arguments); currentToolCallDict.function.arguments = JSON.stringify(args); } catch (e) { - console.warn(`Tool call arguments not valid JSON: ${currentToolCallDict.function.arguments}`); + console.warn(`[Kiro] Tool call arguments not valid JSON: ${currentToolCallDict.function.arguments}`); } toolCalls.push(currentToolCallDict); currentToolCallDict = null; } } else if (!eventData.followupPrompt && eventData.content) { - const decodedContent = eventData.content.replace(/\\n/g, '\n'); + // 处理内容,移除转义字符 + let decodedContent = eventData.content; + // 处理常见的转义序列 + decodedContent = decodedContent.replace(/(? { + this._flushPendingSaves(); + }, this.saveDebounceTime); + } + + /** + * 优化1: 批量保存所有待保存的 providerType + * @private + */ + async _flushPendingSaves() { + const typesToSave = Array.from(this.pendingSaves); + this.pendingSaves.clear(); + this.saveTimer = null; + + for (const providerType of typesToSave) { + await this._saveProviderPoolsToJson(providerType); + } + } + /** * Saves the current provider pools configuration to the JSON file. * @private