Skip to content

Commit 3ac5bec

Browse files
authored
fix: ensure XML parser state matches tool protocol on config update (#9535)
1 parent becdb1a commit 3ac5bec

File tree

1 file changed

+11
-20
lines changed

1 file changed

+11
-20
lines changed

src/core/task/Task.ts

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,38 +1164,29 @@ export class Task extends EventEmitter<TaskEvents> implements TaskLike {
11641164
* @param newApiConfiguration - The new API configuration to use
11651165
*/
11661166
public async updateApiConfiguration(newApiConfiguration: ProviderSettings): Promise<void> {
1167-
// Determine the previous protocol before updating
1168-
const prevModelInfo = this.api.getModel().info
1169-
const previousProtocol = this.apiConfiguration
1170-
? resolveToolProtocol(this.apiConfiguration, prevModelInfo)
1171-
: undefined
1172-
1167+
// Update the configuration and rebuild the API handler
11731168
this.apiConfiguration = newApiConfiguration
11741169
this.api = buildApiHandler(newApiConfiguration)
11751170

1176-
// Determine the new tool protocol
1177-
const newModelInfo = this.api.getModel().info
1178-
const newProtocol = resolveToolProtocol(this.apiConfiguration, newModelInfo)
1179-
const shouldUseXmlParser = newProtocol === "xml"
1171+
// Determine what the tool protocol should be
1172+
const modelInfo = this.api.getModel().info
1173+
const protocol = resolveToolProtocol(this.apiConfiguration, modelInfo)
1174+
const shouldUseXmlParser = protocol === "xml"
1175+
1176+
// Ensure parser state matches protocol requirement
1177+
const parserStateCorrect =
1178+
(shouldUseXmlParser && this.assistantMessageParser) || (!shouldUseXmlParser && !this.assistantMessageParser)
11801179

1181-
// Only make changes if the protocol actually changed
1182-
if (previousProtocol === newProtocol) {
1183-
console.log(
1184-
`[Task#${this.taskId}.${this.instanceId}] Tool protocol unchanged (${newProtocol}), no parser update needed`,
1185-
)
1180+
if (parserStateCorrect) {
11861181
return
11871182
}
11881183

1189-
// Handle protocol transitions
1184+
// Fix parser state
11901185
if (shouldUseXmlParser && !this.assistantMessageParser) {
1191-
// Switching from native → XML: create parser
11921186
this.assistantMessageParser = new AssistantMessageParser()
1193-
console.log(`[Task#${this.taskId}.${this.instanceId}] Switched native → xml: initialized XML parser`)
11941187
} else if (!shouldUseXmlParser && this.assistantMessageParser) {
1195-
// Switching from XML → native: remove parser
11961188
this.assistantMessageParser.reset()
11971189
this.assistantMessageParser = undefined
1198-
console.log(`[Task#${this.taskId}.${this.instanceId}] Switched xml → native: removed XML parser`)
11991190
}
12001191
}
12011192

0 commit comments

Comments
 (0)