@@ -34,18 +34,19 @@ export async function askFollowupQuestionTool(
3434 }
3535
3636 if ( follow_up ) {
37- // Define the actual structure returned by the XML parser for the new format
37+ // Define the actual structure returned by the XML parser for both formats
3838 type ParsedSuggestion =
3939 | string // For backward compatibility with old format or when stopNodes is used
40- | { "#text" : string ; "@_mode" ?: string } // For backward compatibility with old format
40+ | { "#text" : string ; "@_mode" ?: string } // For backward compatibility with old attribute format
41+ | { content : string ; mode ?: string } // New nested element format
42+ | { [ key : string ] : any } // Fallback for unexpected structures
4143
4244 let parsedSuggest : {
4345 suggest : ParsedSuggestion [ ] | ParsedSuggestion
4446 }
4547
4648 try {
47- // Don't use stopNodes for suggest elements to allow proper parsing of nested structure
48- parsedSuggest = parseXml ( follow_up ) as {
49+ parsedSuggest = parseXml ( follow_up , [ "suggest" ] ) as {
4950 suggest : ParsedSuggestion [ ] | ParsedSuggestion
5051 }
5152 } catch ( error ) {
@@ -61,12 +62,30 @@ export async function askFollowupQuestionTool(
6162 : [ parsedSuggest ?. suggest ] . filter ( ( sug ) : sug is ParsedSuggestion => sug !== undefined )
6263
6364 // Transform parsed XML to our Suggest format
64- const normalizedSuggest : Suggest [ ] = rawSuggestions . map ( ( sug : any ) => {
65+ const normalizedSuggest : Suggest [ ] = rawSuggestions . map ( ( sug : ParsedSuggestion ) => {
6566 if ( typeof sug === "string" ) {
67+ // Check if it's a string containing nested XML (new format with stopNodes)
68+ if ( sug . includes ( "<content>" ) || sug . includes ( "<mode>" ) ) {
69+ try {
70+ // Parse the nested XML structure
71+ const nestedParsed = parseXml ( `<suggest>${ sug } </suggest>` ) as any
72+ if ( nestedParsed ?. suggest ) {
73+ const nested = nestedParsed . suggest
74+ const result : Suggest = { answer : nested . content || sug }
75+ if ( nested . mode ) {
76+ result . mode = nested . mode
77+ }
78+ return result
79+ }
80+ } catch {
81+ // If parsing fails, treat as simple string
82+ return { answer : sug }
83+ }
84+ }
6685 // Simple string suggestion (backward compatibility)
6786 return { answer : sug }
6887 } else if ( sug && typeof sug === "object" ) {
69- // Check for new nested element format
88+ // Check for new nested element format (when not using stopNodes)
7089 if ( "content" in sug ) {
7190 const result : Suggest = { answer : sug . content }
7291 if ( sug . mode ) {
0 commit comments