100 lines
3.1 KiB
TypeScript
100 lines
3.1 KiB
TypeScript
"use client";
|
||
|
||
// Start a team run: objective + the few playbook knobs the hub actually honours.
|
||
|
||
import { useRouter } from "next/navigation";
|
||
import { useState } from "react";
|
||
|
||
import { ErrorNote, Field, Input, Modal, PixelButton, Select, Textarea } from "./ui";
|
||
import { api } from "@/lib/api";
|
||
import { useAsync } from "@/lib/useAsync";
|
||
|
||
export function RunModal({
|
||
team,
|
||
defaultPlaybook = "",
|
||
onClose,
|
||
}: {
|
||
team: string;
|
||
defaultPlaybook?: string;
|
||
onClose: () => void;
|
||
}) {
|
||
const router = useRouter();
|
||
const playbooks = useAsync(() => api.playbooks(), []);
|
||
const [objective, setObjective] = useState("");
|
||
const [playbook, setPlaybook] = useState("");
|
||
const [rounds, setRounds] = useState("2");
|
||
const [hops, setHops] = useState("6");
|
||
const [busy, setBusy] = useState(false);
|
||
const [error, setError] = useState("");
|
||
|
||
const chosen = playbook || defaultPlaybook;
|
||
|
||
const submit = async () => {
|
||
if (!objective.trim()) return;
|
||
setBusy(true);
|
||
setError("");
|
||
try {
|
||
const spec: Record<string, number | string> = {};
|
||
if (playbook) spec.playbook = playbook;
|
||
if (chosen === "round_robin") spec.rounds = Number(rounds) || 1;
|
||
if (chosen === "handoff") spec.max_hops = Number(hops) || 1;
|
||
const run = await api.runTeam(team, objective.trim(), spec);
|
||
router.push("/runs/" + run.id);
|
||
onClose();
|
||
} catch (exc) {
|
||
setError(exc instanceof Error ? exc.message : String(exc));
|
||
setBusy(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<Modal
|
||
title={"运行团队 " + team}
|
||
onClose={onClose}
|
||
footer={
|
||
<>
|
||
<PixelButton onClick={onClose}>取消</PixelButton>
|
||
<PixelButton variant="primary" disabled={busy || !objective.trim()} onClick={submit}>
|
||
{busy ? "启动中" : "开始 Run"}
|
||
</PixelButton>
|
||
</>
|
||
}
|
||
>
|
||
<div className="grid gap-3">
|
||
<Field label="目标 Objective">
|
||
<Textarea
|
||
autoFocus
|
||
value={objective}
|
||
onChange={(event) => setObjective(event.target.value)}
|
||
/>
|
||
</Field>
|
||
<Field label="Playbook(可覆盖团队默认)">
|
||
<Select value={playbook} onChange={(event) => setPlaybook(event.target.value)}>
|
||
<option value="">
|
||
团队默认({defaultPlaybook || "sequential"})
|
||
</option>
|
||
{(playbooks.data ?? []).map((item) => (
|
||
<option key={item.name} value={item.name}>
|
||
{item.name}
|
||
</option>
|
||
))}
|
||
</Select>
|
||
</Field>
|
||
<div className="flex flex-wrap gap-3">
|
||
{chosen === "round_robin" && (
|
||
<Field label="轮次 rounds" className="w-40">
|
||
<Input value={rounds} onChange={(event) => setRounds(event.target.value)} />
|
||
</Field>
|
||
)}
|
||
{chosen === "handoff" && (
|
||
<Field label="最大移交次数 max_hops" className="w-44">
|
||
<Input value={hops} onChange={(event) => setHops(event.target.value)} />
|
||
</Field>
|
||
)}
|
||
</div>
|
||
<ErrorNote error={error} />
|
||
</div>
|
||
</Modal>
|
||
);
|
||
}
|