feat: implement structured response parsing with enhanced error recovery and UTF-8 sanitization

This commit is contained in:
lilong.129
2025-06-18 16:59:35 +08:00
parent 6965cf9fe9
commit 1f3366453e
8 changed files with 1076 additions and 217 deletions
+5 -15
View File
@@ -169,24 +169,14 @@ func validateQueryInput(opts *QueryOptions) error {
// parseQueryResult parses the model response into QueryResult
func parseQueryResult(content string) (*QueryResult, error) {
// Extract JSON content from response
jsonContent := extractJSONFromContent(content)
if jsonContent == "" {
// If no JSON found, treat the entire content as the result
// This handles cases where the model returns plain text instead of JSON
return &QueryResult{
Content: content,
Thought: "Direct response from model",
}, nil
}
// Parse JSON response
var result QueryResult
if err := json.Unmarshal([]byte(jsonContent), &result); err != nil {
// If JSON parsing fails, treat the content as plain text result
// Use the generic structured response parser with enhanced error recovery
if err := parseStructuredResponse(content, &result); err != nil {
// If parseStructuredResponse fails completely, treat content as plain text
return &QueryResult{
Content: content,
Thought: "Failed to parse as JSON, returning raw content",
Thought: "Failed to parse response, returning raw content",
}, nil
}