243 lines
7.5 KiB
TypeScript
243 lines
7.5 KiB
TypeScript
"use client";
|
||
|
||
import { Plus, Trash2 } from "lucide-react";
|
||
import Link from "next/link";
|
||
import { useMemo, useState } from "react";
|
||
|
||
import { DispatchModal } from "@/components/DispatchModal";
|
||
import { useLive } from "@/components/LiveProvider";
|
||
import {
|
||
Badge,
|
||
Dot,
|
||
Empty,
|
||
ErrorNote,
|
||
LinkButton,
|
||
Pager,
|
||
Panel,
|
||
PixelButton,
|
||
SearchInput,
|
||
Select,
|
||
Spinner,
|
||
useConfirm,
|
||
usePaged,
|
||
useToast,
|
||
} from "@/components/ui";
|
||
import { api } from "@/lib/api";
|
||
import {
|
||
CONNECTION_MODE_LABEL,
|
||
agentCli,
|
||
agentTypeLabel,
|
||
connectionMode,
|
||
agentHost,
|
||
relTime,
|
||
tone,
|
||
} from "@/lib/format";
|
||
import { useAsync } from "@/lib/useAsync";
|
||
import type { Agent } from "@/lib/types";
|
||
|
||
function Row({
|
||
agent,
|
||
onDispatch,
|
||
onDelete,
|
||
onProbe,
|
||
}: {
|
||
agent: Agent;
|
||
onDispatch: (name: string) => void;
|
||
onDelete: (name: string) => void;
|
||
onProbe: (name: string) => void;
|
||
}) {
|
||
return (
|
||
<tr>
|
||
<td>
|
||
<Link href={"/agents/" + agent.name} className="flex items-center gap-2">
|
||
<span className="text-[15px]">{agent.emoji}</span>
|
||
<span className="min-w-0">
|
||
<span className="pixel-link block truncate">{agent.display_name || agent.name}</span>
|
||
<span className="pixel-muted pixel-mono block truncate text-[12px]">{agent.name}</span>
|
||
</span>
|
||
</Link>
|
||
</td>
|
||
<td>
|
||
<Badge tone={tone(agent.status)}>
|
||
<Dot tone={tone(agent.status)} pulse={agent.live} />
|
||
{agent.status}
|
||
</Badge>
|
||
</td>
|
||
<td>
|
||
<Badge tone={agent.agent_type === "custom" && !agentCli(agent) ? "gray" : "accent"}>
|
||
{agentCli(agent) || agentTypeLabel(agent.agent_type)}
|
||
</Badge>
|
||
</td>
|
||
<td className="pixel-mono">{agent.driver}</td>
|
||
<td>
|
||
<Badge tone={agent.live ? "green" : agent.connection_mode === "direct" ? "gray" : "accent"}>
|
||
{connectionMode(agent.connection_mode)}
|
||
</Badge>
|
||
</td>
|
||
<td className="pixel-muted pixel-mono">
|
||
{agent.deployment_kind}
|
||
{agentHost(agent) ? " · " + agentHost(agent) : ""}
|
||
</td>
|
||
<td className="pixel-muted">{agent.role || "-"}</td>
|
||
<td className="pixel-mono">{agent.active_tasks}</td>
|
||
<td className="pixel-muted pixel-mono text-[12px]">
|
||
{agent.live
|
||
? "socket"
|
||
: agent.connection_mode === "direct"
|
||
? "probe " + relTime(agent.last_probe_at)
|
||
: "heartbeat " + relTime(agent.last_heartbeat_at)}
|
||
<br />
|
||
task {relTime(agent.last_task_at)}
|
||
</td>
|
||
<td className="text-right">
|
||
<div className="flex justify-end gap-1">
|
||
<PixelButton size="sm" onClick={() => onProbe(agent.name)} title="主动探测">
|
||
probe
|
||
</PixelButton>
|
||
<PixelButton size="sm" onClick={() => onDispatch(agent.name)}>
|
||
派活
|
||
</PixelButton>
|
||
<PixelButton
|
||
size="sm"
|
||
icon
|
||
variant="danger"
|
||
title="删除登记"
|
||
onClick={() => onDelete(agent.name)}
|
||
>
|
||
<Trash2 size={12} />
|
||
</PixelButton>
|
||
</div>
|
||
</td>
|
||
</tr>
|
||
);
|
||
}
|
||
|
||
export default function AgentsPage() {
|
||
const confirm = useConfirm();
|
||
const toast = useToast();
|
||
const { revision } = useLive();
|
||
const agents = useAsync(() => api.agents(), [revision]);
|
||
const [text, setText] = useState("");
|
||
const [status, setStatus] = useState("");
|
||
const [mode, setMode] = useState("");
|
||
const [dispatch, setDispatch] = useState("");
|
||
|
||
const rows = useMemo(() => {
|
||
const needle = text.trim().toLowerCase();
|
||
return (agents.data ?? []).filter((agent) => {
|
||
if (status && agent.status !== status) return false;
|
||
if (mode && agent.connection_mode !== mode) return false;
|
||
if (!needle) return true;
|
||
return [agent.name, agent.display_name, agent.driver, agent.role, agent.base_url, agent.deployment_ref]
|
||
.join(" ")
|
||
.toLowerCase()
|
||
.includes(needle);
|
||
});
|
||
}, [agents.data, text, status, mode]);
|
||
|
||
const paged = usePaged(rows, 20);
|
||
|
||
return (
|
||
<div className="grid gap-3">
|
||
<div className="flex flex-wrap items-center gap-2">
|
||
<SearchInput
|
||
className="w-64"
|
||
placeholder="搜索 name / driver / host"
|
||
value={text}
|
||
onChange={(event) => setText(event.target.value)}
|
||
/>
|
||
<Select className="w-auto" value={status} onChange={(event) => setStatus(event.target.value)}>
|
||
<option value="">全部状态</option>
|
||
{["online", "degraded", "offline", "unknown"].map((value) => (
|
||
<option key={value} value={value}>
|
||
{value}
|
||
</option>
|
||
))}
|
||
</Select>
|
||
<Select className="w-auto" value={mode} onChange={(event) => setMode(event.target.value)}>
|
||
<option value="">全部接入方式</option>
|
||
{Object.entries(CONNECTION_MODE_LABEL).map(([value, label]) => (
|
||
<option key={value} value={value}>
|
||
{label}({value})
|
||
</option>
|
||
))}
|
||
</Select>
|
||
<LinkButton href="/connect" className="ml-auto">
|
||
<Plus size={12} /> 接入新 Agent
|
||
</LinkButton>
|
||
</div>
|
||
|
||
<Panel bodyClass="p-0">
|
||
{agents.error && <ErrorNote error={agents.error} />}
|
||
{agents.loading && !agents.data ? (
|
||
<Spinner />
|
||
) : rows.length === 0 ? (
|
||
<Empty title="没有匹配的 Agent" />
|
||
) : (
|
||
<>
|
||
<div className="overflow-x-auto">
|
||
<table className="pixel-table">
|
||
<thead>
|
||
<tr>
|
||
<th>Agent</th>
|
||
<th>状态</th>
|
||
<th>类型</th>
|
||
<th>Driver</th>
|
||
<th>接入</th>
|
||
<th>部署</th>
|
||
<th>Role</th>
|
||
<th>在途</th>
|
||
<th>最近</th>
|
||
<th />
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
{paged.rows.map((agent) => (
|
||
<Row
|
||
key={agent.id}
|
||
agent={agent}
|
||
onDispatch={setDispatch}
|
||
onProbe={async (name) => {
|
||
try {
|
||
await api.probe(name);
|
||
toast("ok", "已向 " + name + " 发出探活请求");
|
||
} catch (exc) {
|
||
toast("error", exc instanceof Error ? exc.message : String(exc));
|
||
}
|
||
}}
|
||
onDelete={async (name) => {
|
||
const ok = await confirm({
|
||
title: "删除 " + name + " 的登记?",
|
||
confirmLabel: "删除",
|
||
danger: true,
|
||
});
|
||
if (!ok) return;
|
||
try {
|
||
await api.deleteAgent(name);
|
||
agents.reload();
|
||
} catch (exc) {
|
||
toast("error", exc instanceof Error ? exc.message : String(exc));
|
||
}
|
||
}}
|
||
/>
|
||
))}
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<Pager
|
||
page={paged.page}
|
||
pageCount={paged.pageCount}
|
||
total={paged.total}
|
||
pageSize={paged.pageSize}
|
||
onPage={paged.setPage}
|
||
onPageSize={paged.setPageSize}
|
||
/>
|
||
</>
|
||
)}
|
||
</Panel>
|
||
|
||
{dispatch && <DispatchModal defaultAgent={dispatch} onClose={() => setDispatch("")} />}
|
||
</div>
|
||
);
|
||
}
|