Explorar o código

fix: sanitize JSON before parsing in settler-delta-parser

Add sanitizeJSON() to strip control characters (\x00-\x1F\x7F) and
trailing commas from LLM output before passing to JSON.parse. This
prevents the parser from crashing when MiniMax or other providers emit
invalid JSON sequences, allowing state settlement to proceed normally.
OpenClaw Bot hai 7 meses
pai
achega
9b8a12b22b
Modificáronse 1 ficheiros con 7 adicións e 1 borrados
  1. 7 1
      packages/core/src/agents/settler-delta-parser.ts

+ 7 - 1
packages/core/src/agents/settler-delta-parser.ts

@@ -8,6 +8,12 @@ export interface SettlerDeltaOutput {
   readonly runtimeStateDelta: RuntimeStateDelta;
 }
 
+function sanitizeJSON(str: string): string {
+  return str
+    .replace(/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]/g, "")
+    .replace(/,\s*([}\]])/g, "$1");
+}
+
 export function parseSettlerDeltaOutput(content: string): SettlerDeltaOutput {
   const extract = (tag: string): string => {
     const regex = new RegExp(
@@ -25,7 +31,7 @@ export function parseSettlerDeltaOutput(content: string): SettlerDeltaOutput {
   const jsonPayload = stripCodeFence(rawDelta);
   let parsed: unknown;
   try {
-    parsed = JSON.parse(jsonPayload);
+    parsed = JSON.parse(sanitizeJSON(jsonPayload));
   } catch (error) {
     throw new Error(`runtime state delta is not valid JSON: ${String(error)}`);
   }