chore: harden claude json parsing and logging

This commit is contained in:
Peter Steinberger
2025-11-25 03:50:52 +01:00
parent 594fb125e6
commit d2abe3c459
2 changed files with 23 additions and 25 deletions

View File

@@ -51,10 +51,18 @@ export type ClaudeJsonParseResult = {
parsed: unknown;
};
export function parseClaudeJson(raw: string): ClaudeJsonParseResult | undefined {
export function parseClaudeJson(
raw: string,
): ClaudeJsonParseResult | undefined {
// Handle a single JSON blob or newline-delimited JSON; return the first parsed payload.
let firstParsed: unknown;
const candidates = [raw, ...raw.split(/\n+/).map((s) => s.trim()).filter(Boolean)];
const candidates = [
raw,
...raw
.split(/\n+/)
.map((s) => s.trim())
.filter(Boolean),
];
for (const candidate of candidates) {
try {
const parsed = JSON.parse(candidate);