From 8d661e18f24515679c263f9ea8ba4466753d8fc5 Mon Sep 17 00:00:00 2001 From: daniel-lxs Date: Mon, 24 Nov 2025 09:34:04 -0500 Subject: [PATCH] fix: ensure XML parser state matches tool protocol on config update Simplified updateApiConfiguration to directly verify and fix parser state instead of relying on protocol change detection. This ensures the parser is always correctly initialized or removed based on the current protocol requirement, preventing inconsistent states when switching between XML and native tool protocols. --- src/core/task/Task.ts | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/src/core/task/Task.ts b/src/core/task/Task.ts index b9a3b5ae7bf..6c17e201210 100644 --- a/src/core/task/Task.ts +++ b/src/core/task/Task.ts @@ -1164,38 +1164,29 @@ export class Task extends EventEmitter implements TaskLike { * @param newApiConfiguration - The new API configuration to use */ public async updateApiConfiguration(newApiConfiguration: ProviderSettings): Promise { - // Determine the previous protocol before updating - const prevModelInfo = this.api.getModel().info - const previousProtocol = this.apiConfiguration - ? resolveToolProtocol(this.apiConfiguration, prevModelInfo) - : undefined - + // Update the configuration and rebuild the API handler this.apiConfiguration = newApiConfiguration this.api = buildApiHandler(newApiConfiguration) - // Determine the new tool protocol - const newModelInfo = this.api.getModel().info - const newProtocol = resolveToolProtocol(this.apiConfiguration, newModelInfo) - const shouldUseXmlParser = newProtocol === "xml" + // Determine what the tool protocol should be + const modelInfo = this.api.getModel().info + const protocol = resolveToolProtocol(this.apiConfiguration, modelInfo) + const shouldUseXmlParser = protocol === "xml" + + // Ensure parser state matches protocol requirement + const parserStateCorrect = + (shouldUseXmlParser && this.assistantMessageParser) || (!shouldUseXmlParser && !this.assistantMessageParser) - // Only make changes if the protocol actually changed - if (previousProtocol === newProtocol) { - console.log( - `[Task#${this.taskId}.${this.instanceId}] Tool protocol unchanged (${newProtocol}), no parser update needed`, - ) + if (parserStateCorrect) { return } - // Handle protocol transitions + // Fix parser state if (shouldUseXmlParser && !this.assistantMessageParser) { - // Switching from native → XML: create parser this.assistantMessageParser = new AssistantMessageParser() - console.log(`[Task#${this.taskId}.${this.instanceId}] Switched native → xml: initialized XML parser`) } else if (!shouldUseXmlParser && this.assistantMessageParser) { - // Switching from XML → native: remove parser this.assistantMessageParser.reset() this.assistantMessageParser = undefined - console.log(`[Task#${this.taskId}.${this.instanceId}] Switched xml → native: removed XML parser`) } }