fix: enhance suggestion parsing logic in askFollowupQuestionTool for improved compatibility

This commit is contained in:
daniel-lxs 2025-08-18 10:25:08 -05:00 committed by Merge Resolver
parent 75945f845d
commit bb28208f02

View file

@ -61,54 +61,67 @@ export async function askFollowupQuestionTool(
? parsedSuggest.suggest
: [parsedSuggest?.suggest].filter((sug): sug is ParsedSuggestion => sug !== undefined)
// Helper functions for parsing different formats
const parseNestedFormat = (sug: any): Suggest | null => {
// New nested element format
if (sug.content) {
return { answer: sug.content, ...(sug.mode && { mode: sug.mode }) }
}
return null
}
const parseAttributeFormat = (sug: any): Suggest | null => {
// Old attribute format (backward compatibility)
if (sug["#text"]) {
return { answer: sug["#text"], ...(sug["@_mode"] && { mode: sug["@_mode"] }) }
}
return null
}
const parseStringWithNestedXml = (str: string): Suggest | null => {
// Check if string contains nested XML (new format with stopNodes)
if (str.includes("<content>") || str.includes("<mode>")) {
try {
const nestedParsed = parseXml(`<suggest>${str}</suggest>`) as any
if (nestedParsed?.suggest) {
const nested = nestedParsed.suggest
return { answer: nested.content || str, ...(nested.mode && { mode: nested.mode }) }
}
} catch {
// If parsing fails, return null to try other formats
}
}
return null
}
// Transform parsed XML to our Suggest format
const normalizedSuggest: Suggest[] = rawSuggestions.map((sug: ParsedSuggestion) => {
// Handle string suggestions
if (typeof sug === "string") {
// Check if it's a string containing nested XML (new format with stopNodes)
if (sug.includes("<content>") || sug.includes("<mode>")) {
try {
// Parse the nested XML structure
const nestedParsed = parseXml(`<suggest>${sug}</suggest>`) 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 }
}
}
// Try parsing as nested XML first
const nestedResult = parseStringWithNestedXml(sug)
if (nestedResult) return nestedResult
// Simple string suggestion (backward compatibility)
return { answer: sug }
} else if (sug && typeof sug === "object") {
// Check for new nested element format (when not using stopNodes)
if ("content" in sug) {
const result: Suggest = { answer: sug.content }
if (sug.mode) {
result.mode = sug.mode
}
return result
}
// Old attribute format (backward compatibility)
else if ("#text" in sug) {
const result: Suggest = { answer: sug["#text"] }
if (sug["@_mode"]) {
result.mode = sug["@_mode"]
}
return result
}
// Fallback for any other object structure
else {
return { answer: JSON.stringify(sug) }
}
} else {
// Fallback for any unexpected type
return { answer: String(sug) }
}
// Handle object suggestions
if (sug && typeof sug === "object") {
// Try new nested element format first
const nestedResult = parseNestedFormat(sug)
if (nestedResult) return nestedResult
// Try old attribute format
const attributeResult = parseAttributeFormat(sug)
if (attributeResult) return attributeResult
// Fallback for any other object structure
return { answer: JSON.stringify(sug) }
}
// Fallback for any unexpected type
return { answer: String(sug) }
})
follow_up_json.suggest = normalizedSuggest