( input: CreateTaskInput, ctx: HandlerContext, )
| 92 | }; |
| 93 | |
| 94 | export async function handleCreateTask( |
| 95 | input: CreateTaskInput, |
| 96 | ctx: HandlerContext, |
| 97 | ): Promise<HandlerResult<Task>> { |
| 98 | const template = templateForKey(input.assignee); |
| 99 | if (!template) { |
| 100 | return { ok: false, error: `Unknown assignee template '${input.assignee}'` }; |
| 101 | } |
| 102 | if (template.key === "cmo") { |
| 103 | return { ok: false, error: "CMO cannot assign tasks to itself — pick a specialist" }; |
| 104 | } |
| 105 | |
| 106 | // Look up the assignee by template_key — agent_ids encode the |
| 107 | // personal name (e.g. `acme-google-ads-ana`), so we can't synthesize |
| 108 | // one from the role alone. |
| 109 | const assigneeEntry = (await listProjectAgents(ctx.project_slug)).find( |
| 110 | (a) => a.template_key === template.key, |
| 111 | ); |
| 112 | if (!assigneeEntry || !(await agentExists(assigneeEntry.agent_id))) { |
| 113 | return { |
| 114 | ok: false, |
| 115 | error: `Assignee agent for role '${template.key}' is not provisioned for this project`, |
| 116 | }; |
| 117 | } |
| 118 | const assigneeAgentId = assigneeEntry.agent_id; |
| 119 | |
| 120 | const task = createTask({ |
| 121 | project_slug: ctx.project_slug, |
| 122 | agent_id: assigneeAgentId, |
| 123 | title: input.title, |
| 124 | brief: input.brief, |
| 125 | success_criteria: input.success_criteria ?? null, |
| 126 | assigner_agent_id: ctx.agent_id, |
| 127 | status: "proposed", |
| 128 | }); |
| 129 | |
| 130 | logAgentAction({ |
| 131 | project_slug: ctx.project_slug, |
| 132 | agent_id: ctx.agent_id, |
| 133 | action_type: "task_created", |
| 134 | summary: `Created task '${task.title}' for ${template.display_name}.`, |
| 135 | payload: { task_id: task.id, assignee: assigneeAgentId }, |
| 136 | }); |
| 137 | |
| 138 | // Auto-start the delegated task. Lazy import keeps the orchestration → |
| 139 | // run-task → gateway chain out of modules that just want createTask. |
| 140 | const { startTaskIfProposed } = await import("./run-task"); |
| 141 | const started = startTaskIfProposed(task); |
| 142 | return { ok: true, data: started }; |
| 143 | } |
| 144 | |
| 145 | // ── submit_task_status ────────────────────────────────────────────────── |
| 146 |
no test coverage detected