184 lines
6.1 KiB
TypeScript
184 lines
6.1 KiB
TypeScript
"use client";
|
|
|
|
// 任务卡设计:状态色条 + 标题/prompt 预览 + 归属元信息 + 行内动作。列表一律走翻页。
|
|
|
|
import { ListChecks, Plus, XCircle } from "lucide-react";
|
|
import Link from "next/link";
|
|
import { useState } from "react";
|
|
|
|
import { DispatchModal } from "@/components/DispatchModal";
|
|
import { useLive } from "@/components/LiveProvider";
|
|
import {
|
|
Badge,
|
|
Dot,
|
|
Empty,
|
|
ErrorNote,
|
|
Pager,
|
|
Panel,
|
|
PixelButton,
|
|
Select,
|
|
Spinner,
|
|
useConfirm,
|
|
usePaged,
|
|
useToast,
|
|
} from "@/components/ui";
|
|
import { api } from "@/lib/api";
|
|
import { duration, relTime, shortId, tone } from "@/lib/format";
|
|
import { useAsync } from "@/lib/useAsync";
|
|
import type { Task } from "@/lib/types";
|
|
|
|
const STATUSES = ["", "queued", "running", "completed", "failed", "cancelled"];
|
|
|
|
function TaskCard({ task, onCancel }: { task: Task; onCancel: (task: Task) => void }) {
|
|
const liveTask = task.status === "queued" || task.status === "running";
|
|
return (
|
|
<article className="pixel-card min-w-[300px] flex-1">
|
|
<span className={"pixel-card__rail pixel-card__rail--" + tone(task.status)} />
|
|
<div className="flex items-center gap-2">
|
|
<Link href={"/tasks/" + task.id}>
|
|
<Badge tone={tone(task.status)}>
|
|
<Dot tone={tone(task.status)} pulse={task.status === "running"} />
|
|
{task.status}
|
|
</Badge>
|
|
</Link>
|
|
<span className="pixel-muted pixel-mono text-[12px]">{shortId(task.id)}</span>
|
|
<span className="pixel-muted pixel-mono ml-auto text-[12px]">{relTime(task.created_at)}</span>
|
|
</div>
|
|
|
|
<div className="grid gap-1">
|
|
<Link className="pixel-link line-clamp-1 text-[15px] font-semibold" href={"/tasks/" + task.id}>
|
|
{task.title || "(无标题)"}
|
|
</Link>
|
|
{task.prompt?.trim() && task.prompt.trim() !== task.title?.trim() && (
|
|
<p className="pixel-muted line-clamp-2 text-[13px] leading-snug break-words">{task.prompt}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex flex-wrap items-center gap-1.5 text-[12px]">
|
|
{task.agent_id ? (
|
|
<Link className="pixel-link" href={"/agents/" + task.agent_id}>
|
|
{task.agent_name || shortId(task.agent_id)}
|
|
</Link>
|
|
) : (
|
|
<span className="pixel-muted">未分配</span>
|
|
)}
|
|
<span className="pixel-muted pixel-mono">{task.source || "console"}</span>
|
|
{task.run_id && (
|
|
<Link className="pixel-link pixel-mono" href={"/runs/" + task.run_id}>
|
|
run
|
|
</Link>
|
|
)}
|
|
<span className="pixel-muted pixel-mono ml-auto">
|
|
{task.status === "completed" || task.status === "failed" ? duration(task.duration_s) : ""}
|
|
</span>
|
|
</div>
|
|
|
|
{liveTask && (
|
|
<div className="flex justify-end border-t-2 border-[var(--pixel-border-soft)] pt-2">
|
|
<PixelButton size="sm" variant="danger" onClick={() => onCancel(task)}>
|
|
<XCircle size={12} />
|
|
取消
|
|
</PixelButton>
|
|
</div>
|
|
)}
|
|
</article>
|
|
);
|
|
}
|
|
|
|
export default function TasksPage() {
|
|
const { revision } = useLive();
|
|
const confirm = useConfirm();
|
|
const toast = useToast();
|
|
const [status, setStatus] = useState("");
|
|
const [agent, setAgent] = useState("");
|
|
const [limit, setLimit] = useState("100");
|
|
const [dispatch, setDispatch] = useState(false);
|
|
|
|
const agents = useAsync(() => api.agents(), []);
|
|
const tasks = useAsync(
|
|
() => api.tasks({ status, agent_id: agent, limit: Number(limit) || 100 }),
|
|
[status, agent, limit, revision]
|
|
);
|
|
const paged = usePaged(tasks.data ?? [], 20);
|
|
|
|
const cancel = async (task: Task) => {
|
|
const ok = await confirm({
|
|
title: "取消任务?",
|
|
confirmLabel: "取消任务",
|
|
danger: true,
|
|
});
|
|
if (!ok) return;
|
|
try {
|
|
await api.cancelTask(task.id);
|
|
toast("ok", "已发出取消信号");
|
|
tasks.reload();
|
|
} catch (exc) {
|
|
toast("error", exc instanceof Error ? exc.message : String(exc));
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div className="grid gap-3">
|
|
<div className="flex flex-wrap items-center gap-2">
|
|
<PixelButton variant="primary" onClick={() => setDispatch(true)}>
|
|
<Plus size={14} />
|
|
派活
|
|
</PixelButton>
|
|
<PixelButton variant="ghost" onClick={tasks.reload}>
|
|
重载
|
|
</PixelButton>
|
|
<div className="ml-auto flex flex-wrap items-center gap-2">
|
|
<Select value={agent} onChange={(event) => setAgent(event.target.value)}>
|
|
<option value="">全部 Agent</option>
|
|
{(agents.data ?? []).map((row) => (
|
|
<option key={row.id} value={row.name}>
|
|
{row.emoji} {row.name}
|
|
</option>
|
|
))}
|
|
</Select>
|
|
<Select value={status} onChange={(event) => setStatus(event.target.value)}>
|
|
{STATUSES.map((item) => (
|
|
<option key={item} value={item}>
|
|
{item || "全部状态"}
|
|
</option>
|
|
))}
|
|
</Select>
|
|
<Select value={limit} onChange={(event) => setLimit(event.target.value)}>
|
|
{["50", "100", "300"].map((item) => (
|
|
<option key={item} value={item}>
|
|
最近 {item}
|
|
</option>
|
|
))}
|
|
</Select>
|
|
</div>
|
|
</div>
|
|
|
|
<Panel title="任务" icon={<ListChecks size={14} />} bodyClass="p-0">
|
|
{tasks.error && <ErrorNote error={tasks.error} />}
|
|
{tasks.loading && !tasks.data && <Spinner />}
|
|
{paged.total === 0 && !tasks.loading ? (
|
|
<Empty title="还没有任务" />
|
|
) : (
|
|
<>
|
|
<div className="flex flex-wrap items-stretch gap-3 p-3">
|
|
{paged.rows.map((task) => (
|
|
<TaskCard key={task.id} task={task} onCancel={cancel} />
|
|
))}
|
|
</div>
|
|
<Pager
|
|
page={paged.page}
|
|
pageCount={paged.pageCount}
|
|
total={paged.total}
|
|
pageSize={paged.pageSize}
|
|
onPage={paged.setPage}
|
|
onPageSize={paged.setPageSize}
|
|
/>
|
|
</>
|
|
)}
|
|
</Panel>
|
|
|
|
{dispatch && <DispatchModal onClose={() => setDispatch(false)} />}
|
|
</div>
|
|
);
|
|
}
|