@@ -61,54 +61,67 @@ export async function askFollowupQuestionTool(
6161 ? parsedSuggest . suggest
6262 : [ parsedSuggest ?. suggest ] . filter ( ( sug ) : sug is ParsedSuggestion => sug !== undefined )
6363
64+ // Helper functions for parsing different formats
65+ const parseNestedFormat = ( sug : any ) : Suggest | null => {
66+ // New nested element format
67+ if ( sug . content ) {
68+ return { answer : sug . content , ...( sug . mode && { mode : sug . mode } ) }
69+ }
70+ return null
71+ }
72+
73+ const parseAttributeFormat = ( sug : any ) : Suggest | null => {
74+ // Old attribute format (backward compatibility)
75+ if ( sug [ "#text" ] ) {
76+ return { answer : sug [ "#text" ] , ...( sug [ "@_mode" ] && { mode : sug [ "@_mode" ] } ) }
77+ }
78+ return null
79+ }
80+
81+ const parseStringWithNestedXml = ( str : string ) : Suggest | null => {
82+ // Check if string contains nested XML (new format with stopNodes)
83+ if ( str . includes ( "<content>" ) || str . includes ( "<mode>" ) ) {
84+ try {
85+ const nestedParsed = parseXml ( `<suggest>${ str } </suggest>` ) as any
86+ if ( nestedParsed ?. suggest ) {
87+ const nested = nestedParsed . suggest
88+ return { answer : nested . content || str , ...( nested . mode && { mode : nested . mode } ) }
89+ }
90+ } catch {
91+ // If parsing fails, return null to try other formats
92+ }
93+ }
94+ return null
95+ }
96+
6497 // Transform parsed XML to our Suggest format
6598 const normalizedSuggest : Suggest [ ] = rawSuggestions . map ( ( sug : ParsedSuggestion ) => {
99+ // Handle string suggestions
66100 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- }
101+ // Try parsing as nested XML first
102+ const nestedResult = parseStringWithNestedXml ( sug )
103+ if ( nestedResult ) return nestedResult
104+
85105 // Simple string suggestion (backward compatibility)
86106 return { answer : sug }
87- } else if ( sug && typeof sug === "object" ) {
88- // Check for new nested element format (when not using stopNodes)
89- if ( "content" in sug ) {
90- const result : Suggest = { answer : sug . content }
91- if ( sug . mode ) {
92- result . mode = sug . mode
93- }
94- return result
95- }
96- // Old attribute format (backward compatibility)
97- else if ( "#text" in sug ) {
98- const result : Suggest = { answer : sug [ "#text" ] }
99- if ( sug [ "@_mode" ] ) {
100- result . mode = sug [ "@_mode" ]
101- }
102- return result
103- }
107+ }
108+
109+ // Handle object suggestions
110+ if ( sug && typeof sug === "object" ) {
111+ // Try new nested element format first
112+ const nestedResult = parseNestedFormat ( sug )
113+ if ( nestedResult ) return nestedResult
114+
115+ // Try old attribute format
116+ const attributeResult = parseAttributeFormat ( sug )
117+ if ( attributeResult ) return attributeResult
118+
104119 // Fallback for any other object structure
105- else {
106- return { answer : JSON . stringify ( sug ) }
107- }
108- } else {
109- // Fallback for any unexpected type
110- return { answer : String ( sug ) }
120+ return { answer : JSON . stringify ( sug ) }
111121 }
122+
123+ // Fallback for any unexpected type
124+ return { answer : String ( sug ) }
112125 } )
113126
114127 follow_up_json . suggest = normalizedSuggest
0 commit comments