import { PLATFORM_SERVICE_NAME } from '../src/config.js'; const API_PORT = parseInt(process.env.CONTROLPLANE_PORT ?? '3100', 10); const API_HOST = process.env.CONTROLPLANE_BIND_HOST ?? '127.0.0.1'; const DEFAULT_ASSIGNED_TO = 'coordinator'; const KNOWN_ROLES = new Set([ 'coordinator', 'sysadmin', 'db-admin', 'git-admin', 'mevy', ]); const arg1 = process.argv[2]; const argRest = process.argv.slice(3).join(' ').trim(); const assignedTo = arg1 && KNOWN_ROLES.has(arg1) ? arg1 : DEFAULT_ASSIGNED_TO; const title = arg1 && KNOWN_ROLES.has(arg1) ? argRest : process.argv.slice(2).join(' ').trim(); if (!title) { console.error( `Usage: agent-task.ts # defaults assigned_to=${DEFAULT_ASSIGNED_TO} agent-task.ts # role in: ${Array.from(KNOWN_ROLES).join(', ')}`, ); process.exit(1); } async function main(): Promise { try { const secret = process.env.CONTROLPLANE_SHARED_SECRET; if (!secret) { console.error('Missing CONTROLPLANE_SHARED_SECRET in environment'); process.exit(1); } const res = await fetch( `http://${API_HOST}:${API_PORT}/api/controlplane/tasks`, { method: 'POST', headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${secret}`, }, body: JSON.stringify({ title, description: title, assigned_to: assignedTo, priority: 'medium', }), }, ); if (!res.ok) { console.error(`API error ${res.status}: ${await res.text()}`); process.exit(1); } const data = (await res.json()) as Record; const taskId = typeof data.task_id === 'string' ? data.task_id : ''; if (!taskId) { console.log('Task created (no task_id returned)'); return; } console.log(`Task created: ${taskId}`); console.log(` Assigned: ${assignedTo}`); console.log(` Title: ${title}`); } catch (err) { const msg = err instanceof Error ? err.message : String(err); if (msg.includes('ECONNREFUSED')) { console.error( `${PLATFORM_SERVICE_NAME} controlplane API not reachable at`, `${API_HOST}:${API_PORT}`, ); } else { console.error('Error:', msg); } process.exit(1); } } main();