"use client"; // One dispatch form for every entry point (agents, teams, tasks). import { useRouter } from "next/navigation"; import { useState } from "react"; import { ErrorNote, Field, Input, Modal, PixelButton, Select, Tabs, Textarea } from "./ui"; import { api } from "@/lib/api"; import { useAsync } from "@/lib/useAsync"; export function DispatchModal({ onClose, defaultAgent = "", defaultTeam = "", }: { onClose: () => void; defaultAgent?: string; defaultTeam?: string; }) { const router = useRouter(); const agents = useAsync(() => api.agents(), []); const teams = useAsync(() => api.teams(), []); const [mode, setMode] = useState(defaultTeam ? "team" : "agent"); const [target, setTarget] = useState(defaultAgent || defaultTeam); const [title, setTitle] = useState(""); const [prompt, setPrompt] = useState(""); const [session, setSession] = useState(""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const submit = async () => { if (!prompt.trim() || !target) return; setBusy(true); setError(""); try { const body = { prompt: prompt.trim(), title: title.trim(), session_key: session.trim(), ...(mode === "agent" ? { agent_id: target } : { team_id: target }), }; const task = await api.createTask(body); router.push("/tasks/" + task.id); onClose(); } catch (exc) { setError(exc instanceof Error ? exc.message : String(exc)); setBusy(false); } }; const options = mode === "agent" ? (agents.data ?? []).map((agent) => ({ value: agent.name, label: agent.emoji + " " + agent.name })) : (teams.data ?? []).map((team) => ({ value: team.name, label: team.name })); return ( 取消 {busy ? "派发中" : "派发任务"} } >
{ setMode(value); setTarget(""); }} /> setTitle(event.target.value)} />