agenthub/frontend/components/DispatchModal.tsx

114 lines
3.4 KiB
TypeScript

"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 (
<Modal
title="派活 / Dispatch"
onClose={onClose}
footer={
<>
<PixelButton onClick={onClose}>取消</PixelButton>
<PixelButton variant="primary" disabled={busy || !prompt.trim() || !target} onClick={submit}>
{busy ? "派发中" : "派发任务"}
</PixelButton>
</>
}
>
<div className="grid gap-3">
<Tabs
items={[
{ value: "agent", label: "单个 Agent" },
{ value: "team", label: "团队(直接跑首个成员)" },
]}
value={mode}
onChange={(value) => {
setMode(value);
setTarget("");
}}
/>
<Field label={mode === "agent" ? "目标 Agent" : "目标团队"}>
<Select value={target} onChange={(event) => setTarget(event.target.value)}>
<option value="">请选择…</option>
{options.map((option) => (
<option key={option.value} value={option.value}>
{option.label}
</option>
))}
</Select>
</Field>
<Field label="标题">
<Input value={title} onChange={(event) => setTitle(event.target.value)} />
</Field>
<Field label="Prompt">
<Textarea
value={prompt}
autoFocus
onChange={(event) => setPrompt(event.target.value)}
/>
</Field>
<Field label="Session Key">
<Input
value={session}
placeholder="console:demo"
onChange={(event) => setSession(event.target.value)}
/>
</Field>
<ErrorNote error={error} />
</div>
</Modal>
);
}