fix(companion): emit draft from user-turn tool_result + stamp space_id on created entities

- driver: tool_results arrive as type:'user' content blocks (not bare); parse them
- route: tool_result content is a JSON string; parse it for pending_change_id → draft event
- propose_change: inject ctx.space_id into create payloads (model can't know the uuid; tables require it)

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
root
2026-06-01 22:21:15 +10:00
parent 1e8bbca2a5
commit 1b8dc91800
3 changed files with 37 additions and 21 deletions

View File

@@ -137,26 +137,20 @@ spacesScopedRouter.post('/turn',
//
// Defensive parsing: try structuredContent first (future-proof), then
// scan content array text blocks and JSON.parse them.
// The CLI delivers an MCP tool_result `content` as a JSON STRING,
// e.g. '{"pending_change_id":"...","applied":false,"summary":"..."}'.
// Be defensive: also accept a content-block array or a structuredContent object.
let parsed = null;
const tryParse = (s) => { try { return JSON.parse(s); } catch { return null; } };
try {
// Shape A: structuredContent forwarded through (hypothetical future CLI)
if (e.result?.structuredContent?.pending_change_id) {
if (typeof e.result === 'string') {
parsed = tryParse(e.result);
} else if (e.result?.structuredContent?.pending_change_id) {
parsed = e.result.structuredContent;
}
// Shape B: array of content blocks (real current shape from companion-stdio.js)
if (!parsed && Array.isArray(e.result)) {
} else if (Array.isArray(e.result)) {
for (const block of e.result) {
if (block?.type === 'text' && block.text) {
try {
const candidate = JSON.parse(block.text);
if (candidate?.pending_change_id) {
parsed = candidate;
break;
}
} catch {
// not JSON or not a change result — skip
}
}
const candidate = block?.type === 'text' && block.text ? tryParse(block.text) : null;
if (candidate?.pending_change_id) { parsed = candidate; break; }
}
}
} catch {