(request: Request)
| 39 | }; |
| 40 | |
| 41 | export async function POST(request: Request) { |
| 42 | let body: ChatPostBody; |
| 43 | try { |
| 44 | body = (await request.json()) as ChatPostBody; |
| 45 | } catch { |
| 46 | return NextResponse.json({ error: "Invalid JSON body" }, { status: 400 }); |
| 47 | } |
| 48 | if (!body?.message?.trim()) { |
| 49 | return NextResponse.json({ error: "message is required" }, { status: 400 }); |
| 50 | } |
| 51 | |
| 52 | const project = body.project ? getProject(body.project) : await getActiveProject(); |
| 53 | if (!project) { |
| 54 | return NextResponse.json( |
| 55 | { error: "No active project. Create one first." }, |
| 56 | { status: 400 }, |
| 57 | ); |
| 58 | } |
| 59 | |
| 60 | const requestedSlug = (body.agent ?? "cmo").trim(); |
| 61 | const resolved = await resolveAgentBySlug(project.slug, requestedSlug); |
| 62 | if (!resolved) { |
| 63 | return NextResponse.json( |
| 64 | { error: `Unknown agent: '${requestedSlug}'` }, |
| 65 | { status: 404 }, |
| 66 | ); |
| 67 | } |
| 68 | |
| 69 | const taskId = body.task_id?.trim(); |
| 70 | if (taskId) { |
| 71 | const existing = getTask(taskId); |
| 72 | if (!existing) { |
| 73 | return NextResponse.json({ error: `Unknown task_id '${taskId}'` }, { status: 404 }); |
| 74 | } |
| 75 | if (existing.agent_id !== resolved.agent_id) { |
| 76 | return NextResponse.json( |
| 77 | { |
| 78 | error: `Task ${existing.display_id} belongs to ${existing.agent_id}, not ${resolved.agent_id}`, |
| 79 | }, |
| 80 | { status: 400 }, |
| 81 | ); |
| 82 | } |
| 83 | const claimed = claimProposedTask(existing.id); |
| 84 | if (!claimed) { |
| 85 | return NextResponse.json( |
| 86 | { error: "task already claimed", status: existing.status, task_id: existing.id }, |
| 87 | { status: 409 }, |
| 88 | ); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | const label = |
| 93 | body.thread?.trim() || body.sessionId?.trim() || "main"; |
| 94 | const session = getOrCreateSession({ |
| 95 | project_slug: project.slug, |
| 96 | agent_id: resolved.agent_id, |
| 97 | label, |
| 98 | harness_adapter: project.harness_adapter, |
nothing calls this directly
no test coverage detected