From d922911b7315e4f7711c213898773760b69bf644 Mon Sep 17 00:00:00 2001 From: hannesrudolph Date: Sat, 9 Aug 2025 13:51:42 -0600 Subject: [PATCH] fix: restore stopNodes usage and improve type safety - Restored stopNodes parameter in parseXml call for consistent XML parsing - Added proper TypeScript types for ParsedSuggestion to replace 'any' type - Enhanced parsing logic to handle nested XML when stopNodes returns raw strings - All tests passing (14/14) --- src/core/tools/askFollowupQuestionTool.ts | 31 ++++++++++++++++++----- 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/src/core/tools/askFollowupQuestionTool.ts b/src/core/tools/askFollowupQuestionTool.ts index 9ab45c9835..b61bf1ec86 100644 --- a/src/core/tools/askFollowupQuestionTool.ts +++ b/src/core/tools/askFollowupQuestionTool.ts @@ -34,18 +34,19 @@ export async function askFollowupQuestionTool( } if (follow_up) { - // Define the actual structure returned by the XML parser for the new format + // Define the actual structure returned by the XML parser for both formats type ParsedSuggestion = | string // For backward compatibility with old format or when stopNodes is used - | { "#text": string; "@_mode"?: string } // For backward compatibility with old format + | { "#text": string; "@_mode"?: string } // For backward compatibility with old attribute format + | { content: string; mode?: string } // New nested element format + | { [key: string]: any } // Fallback for unexpected structures let parsedSuggest: { suggest: ParsedSuggestion[] | ParsedSuggestion } try { - // Don't use stopNodes for suggest elements to allow proper parsing of nested structure - parsedSuggest = parseXml(follow_up) as { + parsedSuggest = parseXml(follow_up, ["suggest"]) as { suggest: ParsedSuggestion[] | ParsedSuggestion } } catch (error) { @@ -61,12 +62,30 @@ export async function askFollowupQuestionTool( : [parsedSuggest?.suggest].filter((sug): sug is ParsedSuggestion => sug !== undefined) // Transform parsed XML to our Suggest format - const normalizedSuggest: Suggest[] = rawSuggestions.map((sug: any) => { + const normalizedSuggest: Suggest[] = rawSuggestions.map((sug: ParsedSuggestion) => { if (typeof sug === "string") { + // Check if it's a string containing nested XML (new format with stopNodes) + if (sug.includes("") || sug.includes("")) { + try { + // Parse the nested XML structure + const nestedParsed = parseXml(`${sug}`) as any + if (nestedParsed?.suggest) { + const nested = nestedParsed.suggest + const result: Suggest = { answer: nested.content || sug } + if (nested.mode) { + result.mode = nested.mode + } + return result + } + } catch { + // If parsing fails, treat as simple string + return { answer: sug } + } + } // Simple string suggestion (backward compatibility) return { answer: sug } } else if (sug && typeof sug === "object") { - // Check for new nested element format + // Check for new nested element format (when not using stopNodes) if ("content" in sug) { const result: Suggest = { answer: sug.content } if (sug.mode) {