65 lines
3.0 KiB
TypeScript
65 lines
3.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
|
|
import { findAgentByAssignmentKey } from "@/lib/agents";
|
|
import { createTask, listTasks, validateTaskPayload } from "@/lib/tasks";
|
|
|
|
function extractTagValue(tags: string[], prefix: string) {
|
|
const match = tags.find((tag) => tag.startsWith(prefix));
|
|
return match ? match.slice(prefix.length) : null;
|
|
}
|
|
|
|
export async function GET() {
|
|
return NextResponse.json(await listTasks());
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const payload = (await request.json()) as Record<string, unknown>;
|
|
const errors = validateTaskPayload(payload as never, false);
|
|
if (errors.length > 0) {
|
|
return NextResponse.json({ error: "validation_error", details: errors }, { status: 400 });
|
|
}
|
|
|
|
const assignee = typeof payload.assignee === "string" ? payload.assignee : "";
|
|
const tags = Array.isArray(payload.tags) ? payload.tags.filter((tag) => typeof tag === "string") : [];
|
|
const assigneeAgent = assignee ? await findAgentByAssignmentKey(assignee) : null;
|
|
const derivedRepoSlug = typeof payload.repo_slug === "string" ? payload.repo_slug : extractTagValue(tags, "repo:");
|
|
const derivedPreferredAgent =
|
|
typeof payload.preferred_agent === "string" ? payload.preferred_agent : extractTagValue(tags, "agent:");
|
|
const requestedFamily = payload.family === null ? null : (payload.family as never);
|
|
const requestedDispatchMethod = payload.dispatch_method as never;
|
|
const wantsOpenClawSwarm =
|
|
requestedFamily === "openclaw" ||
|
|
requestedDispatchMethod === "openclaw-swarm" ||
|
|
tags.includes("swarm") ||
|
|
Boolean(derivedRepoSlug) ||
|
|
Boolean(derivedPreferredAgent && ["codex", "opencode", "gemini"].includes(derivedPreferredAgent));
|
|
|
|
const task = await createTask({
|
|
title: String(payload.title),
|
|
description: typeof payload.description === "string" ? payload.description : "",
|
|
assignee,
|
|
family: requestedFamily || assigneeAgent?.family || (wantsOpenClawSwarm ? "openclaw" : null),
|
|
target_host:
|
|
typeof payload.target_host === "string"
|
|
? payload.target_host
|
|
: assigneeAgent?.host || (wantsOpenClawSwarm ? "ubuntu" : ""),
|
|
target_channel:
|
|
typeof payload.target_channel === "string"
|
|
? payload.target_channel
|
|
: assigneeAgent?.channels[0]?.value || (wantsOpenClawSwarm ? "OpenClaw swarm registry" : ""),
|
|
dispatch_method:
|
|
requestedDispatchMethod || assigneeAgent?.defaultDispatchMethod || (wantsOpenClawSwarm ? "openclaw-swarm" : "manual"),
|
|
template_key: typeof payload.template_key === "string" ? payload.template_key : null,
|
|
repo_slug: derivedRepoSlug,
|
|
base_branch: typeof payload.base_branch === "string" ? payload.base_branch : null,
|
|
preferred_agent: derivedPreferredAgent,
|
|
reasoning_effort: typeof payload.reasoning_effort === "string" ? payload.reasoning_effort : null,
|
|
model_hint: typeof payload.model_hint === "string" ? payload.model_hint : null,
|
|
priority: payload.priority as never,
|
|
status: (payload.status as never) || "Backlog",
|
|
tags,
|
|
});
|
|
|
|
return NextResponse.json(task, { status: 201 });
|
|
}
|