fix(decision): handle fullwidth JSON characters from AI responses

## Problem
AI occasionally outputs fullwidth (全角) characters in JSON responses,
causing JSON parsing failures even though content is logically correct.

Example error:
  JSON format validation failed: actual [ {"symbol": "BTCU...
  (displays as fullwidth: [{"symbol":"BTCU...)

## Root Cause
Claude API can output fullwidth characters when:
- Processing Chinese context
- Mixing languages in responses
- Input method artifacts

## Current Protection
Already handles curly quotes but not brackets/punctuation

## Solution
Extend fixMissingQuotes() to handle fullwidth JSON syntax:
- Brackets: [] -> []
- Braces: {} -> {}
- Colon: : -> :
- Comma: , -> ,

## Impact
- Prevents intermittent JSON parsing failures
- No performance impact (simple string replace)
- Does not affect correctly formatted responses

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
ZhouYongyou
2025-11-04 22:29:27 +08:00
parent 590bd5eddc
commit 1ee791e2b5
2 changed files with 11 additions and 1 deletions

View File

@@ -534,12 +534,22 @@ func validateJSONFormat(jsonStr string) error {
return nil
}
// fixMissingQuotes 替换中文引号为英文引号(避免输入法自动转换
// fixMissingQuotes 替换中文引号和全角符号为英文半角符号避免AI输出或输入法自动转换导致JSON解析失败
func fixMissingQuotes(jsonStr string) string {
// 替换中文引号
jsonStr = strings.ReplaceAll(jsonStr, "\u201c", "\"") // "
jsonStr = strings.ReplaceAll(jsonStr, "\u201d", "\"") // "
jsonStr = strings.ReplaceAll(jsonStr, "\u2018", "'") // '
jsonStr = strings.ReplaceAll(jsonStr, "\u2019", "'") // '
// ⚠️ 替换全角括号、冒号、逗号防止AI输出全角JSON字符
jsonStr = strings.ReplaceAll(jsonStr, "", "[") // 全角左方括号 U+FF3B
jsonStr = strings.ReplaceAll(jsonStr, "", "]") // 全角右方括号 U+FF3D
jsonStr = strings.ReplaceAll(jsonStr, "", "{") // 全角左花括号 U+FF5B
jsonStr = strings.ReplaceAll(jsonStr, "", "}") // 全角右花括号 U+FF5D
jsonStr = strings.ReplaceAll(jsonStr, "", ":") // 全角冒号 U+FF1A
jsonStr = strings.ReplaceAll(jsonStr, "", ",") // 全角逗号 U+FF0C
return jsonStr
}

0
nofx.db Normal file
View File