mirror of
https://github.com/JohnRiceML/clawport-ui.git
synced 2026-08-14 00:47:50 +00:00
When OpenClaw CLI prints debug log lines (e.g. "[plugins] [debug] ...") to stdout before JSON, extractJson() failed because bracketed log lines look like JSON array starts. Now tries each candidate position until one actually parses. Also fixes the onboarding wizard to show a specific "CLI log output" error instead of the misleading "Could not reach gateway" message when /api/crons fails due to JSON parsing. Bumps to v0.8.6. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
/**
|
|
* Extract a JSON value from CLI output that may contain non-JSON preamble.
|
|
*
|
|
* Some OpenClaw versions print validation warnings or debug log lines
|
|
* (e.g. "[plugins] [debug] ...") to stdout before the JSON payload.
|
|
* This function finds the actual JSON structure by trying each `[` or `{`
|
|
* position until one parses successfully.
|
|
*/
|
|
export function extractJson(raw: string): unknown {
|
|
// Fast path: raw is already valid JSON
|
|
const trimmed = raw.trim()
|
|
if (trimmed.startsWith('[') || trimmed.startsWith('{')) {
|
|
try {
|
|
return JSON.parse(trimmed)
|
|
} catch {
|
|
// May start with [ but be a log line like "[plugins] ..." -- fall through
|
|
}
|
|
}
|
|
|
|
// Try each potential JSON start position
|
|
let pos = 0
|
|
while (pos < raw.length) {
|
|
const arrStart = raw.indexOf('[', pos)
|
|
const objStart = raw.indexOf('{', pos)
|
|
const candidates = [arrStart, objStart].filter(i => i >= 0)
|
|
if (candidates.length === 0) break
|
|
|
|
const start = Math.min(...candidates)
|
|
try {
|
|
return JSON.parse(raw.slice(start))
|
|
} catch {
|
|
// This wasn't the real JSON start -- advance past it
|
|
pos = start + 1
|
|
}
|
|
}
|
|
|
|
throw new SyntaxError('No JSON found in CLI output')
|
|
}
|