Files
Void-Homelab/lib/ai/agent/tools/propose_change.js
root 1b8dc91800 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>
2026-06-01 22:21:15 +10:00

51 lines
2.1 KiB
JavaScript

import { canAct } from '../../../auth/capability.js';
import * as pendingChanges from '../../../db/repos/pending_changes.js';
const ENTITY_TYPES = ['task', 'page', 'project', 'ref', 'resource', 'source_doc'];
const ACTIONS = ['create', 'update', 'delete'];
export const proposeChangeTool = {
name: 'propose_change',
description: 'Propose a change to the Void. This NEVER applies directly — it creates a draft the owner must approve. Use for creating/updating/deleting tasks, pages, projects, refs, resources.',
input_schema: {
type: 'object',
properties: {
entity_type: { type: 'string', enum: ENTITY_TYPES },
action: { type: 'string', enum: ACTIONS },
entity_id: { type: 'string', description: 'uuid; required for update/delete' },
payload: { type: 'object', description: 'fields for the change' },
reason: { type: 'string', description: 'one-line rationale shown to the owner' }
},
required: ['entity_type', 'action', 'payload']
},
async handler({ entity_type, action, entity_id, payload, reason }, ctx) {
const tier = canAct(ctx.agent, action, entity_type);
if (tier === 'deny') {
return { error: `not permitted to ${action} ${entity_type}` };
}
// Stamp the current Space onto newly-created space-scoped entities — the
// model doesn't know the Space uuid, and these tables require space_id NOT NULL.
const SPACE_SCOPED = ['task', 'page', 'project', 'resource'];
const finalPayload = { ...(payload ?? {}) };
if (action === 'create' && ctx.space_id && SPACE_SCOPED.includes(entity_type)
&& finalPayload.space_id == null) {
finalPayload.space_id = ctx.space_id;
}
// v1: drafting always routes through approval, even for allow-tier agents.
const change = await pendingChanges.create({
agent_id: ctx.agent.id,
entity_type,
entity_id: entity_id ?? null,
action,
payload: finalPayload,
reason: reason ?? null
});
return {
pending_change_id: change.id,
applied: false,
summary: `${action} ${entity_type}${payload?.title ? ` "${payload.title}"` : ''}`
};
}
};