diff --git a/lib/api/routes/dross.js b/lib/api/routes/dross.js index 23713e3..a06c586 100644 --- a/lib/api/routes/dross.js +++ b/lib/api/routes/dross.js @@ -77,22 +77,24 @@ router.post('/turn', requireOwner, validate({ body: turnSchema }), asyncWrap(asy } else if (e.type === 'tool') { send('tool', { type: 'tool', tool: e.tool, status: e.status }); } else if (e.type === 'tool_result') { - let parsed = null; - const tryParse = (s) => { try { return JSON.parse(s); } catch { return null; } }; - if (typeof e.result === 'string') { - parsed = tryParse(e.result); - } else if (e.result?.structuredContent?.pending_change_id) { - parsed = e.result.structuredContent; - } else if (Array.isArray(e.result)) { - for (const b of e.result) { - const c = b?.type === 'text' && b.text ? tryParse(b.text) : null; - if (c?.pending_change_id) { parsed = c; break; } + try { + let parsed = null; + const tryParse = (s) => { try { return JSON.parse(s); } catch { return null; } }; + if (typeof e.result === 'string') { + parsed = tryParse(e.result); + } else if (e.result?.structuredContent?.pending_change_id) { + parsed = e.result.structuredContent; + } else if (Array.isArray(e.result)) { + for (const b of e.result) { + const c = b?.type === 'text' && b.text ? tryParse(b.text) : null; + if (c?.pending_change_id) { parsed = c; break; } + } } - } - if (parsed?.pending_change_id) { - draftIds.push(parsed.pending_change_id); - send('draft', { type: 'draft', pending_change_id: parsed.pending_change_id, summary: parsed.summary || 'a change' }); - } + if (parsed?.pending_change_id) { + draftIds.push(parsed.pending_change_id); + send('draft', { type: 'draft', pending_change_id: parsed.pending_change_id, summary: parsed.summary || 'a change' }); + } + } catch { /* parsing failed — no draft to surface */ } } else if (e.type === 'error') { send('error', { type: 'error', message: e.message }); }