Browse Source

fix: agent67 JSON parsing — proper newline handling inside string values

Previous fix (global replace \n→\\n) broke JSON structure.
New approach: character-by-character walk that only escapes newlines
when inside a quoted string value, preserving JSON syntax.

_fix_json_newlines() tracks in_string state with escape handling,
replacing real newlines with \\n only within "..." values.

This fixes WriteFile calls with long multiline Markdown content.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
kenny67nju 7 months ago
parent
commit
645c3c4c7b
1 changed files with 56 additions and 15 deletions
  1. 56 15
      agentexample/agent67/core/assistant.py

+ 56 - 15
agentexample/agent67/core/assistant.py

@@ -81,35 +81,76 @@ def _try_parse_json(json_str: str) -> dict | None:
     except json.JSONDecodeError:
         pass
 
-    # Strategy 2: replace real newlines inside string values with \\n
-    # This handles: {"content": "line1\nline2"} where \n is a real newline
+    # Strategy 2: fix newlines inside JSON string values
+    # LLM outputs real newlines inside "content": "line1\nline2"
+    # We need to escape them but ONLY inside string values
     try:
-        fixed = json_str.replace("\n", "\\n")
-        # But we need to un-escape \\n that were already escaped (\\\\n → \\n)
-        fixed = fixed.replace("\\\\n", "\\n")
+        fixed = _fix_json_newlines(json_str)
         return json.loads(fixed)
-    except json.JSONDecodeError:
+    except (json.JSONDecodeError, Exception):
         pass
 
-    # Strategy 3: extract just the first { ... } block more aggressively
+    # Strategy 3: balanced brace extraction + newline fix
     try:
         depth = 0
         start = json_str.index("{")
+        in_string = False
+        escape = False
         for i in range(start, len(json_str)):
-            if json_str[i] == "{":
-                depth += 1
-            elif json_str[i] == "}":
-                depth -= 1
-                if depth == 0:
-                    candidate = json_str[start:i+1]
-                    fixed = candidate.replace("\n", "\\n").replace("\\\\n", "\\n")
-                    return json.loads(fixed)
+            c = json_str[i]
+            if escape:
+                escape = False
+                continue
+            if c == '\\':
+                escape = True
+                continue
+            if c == '"':
+                in_string = not in_string
+                continue
+            if not in_string:
+                if c == '{':
+                    depth += 1
+                elif c == '}':
+                    depth -= 1
+                    if depth == 0:
+                        candidate = json_str[start:i+1]
+                        fixed = _fix_json_newlines(candidate)
+                        return json.loads(fixed)
     except (json.JSONDecodeError, ValueError):
         pass
 
     return None
 
 
+def _fix_json_newlines(s: str) -> str:
+    """Replace real newlines inside JSON string values with \\n.
+
+    Walks the string character by character, only replacing newlines
+    when inside a quoted string value.
+    """
+    result = []
+    in_string = False
+    escape = False
+    for c in s:
+        if escape:
+            result.append(c)
+            escape = False
+            continue
+        if c == '\\':
+            result.append(c)
+            escape = True
+            continue
+        if c == '"':
+            in_string = not in_string
+            result.append(c)
+            continue
+        if c == '\n' and in_string:
+            result.append('\\n')
+            continue
+        result.append(c)
+    return ''.join(result)
+
+
 def parse_and_execute(llm_output: str, hooks: HookRegistry = None) -> tuple[str, bool]:
     """
     解析 LLM 输出,提取工具调用并执行。