mirror of
https://github.com/RooVetGit/Roo-Code.git
synced 2026-09-05 08:10:14 +00:00
fix: enhance suggestion parsing logic in askFollowupQuestionTool for improved compatibility
This commit is contained in:
parent
75945f845d
commit
bb28208f02
1 changed files with 55 additions and 42 deletions
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue