CapabilityPage.tsx
6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { Globe, Play, Plus, Save, Trash2, Wrench, Zap } from "lucide-react";
import { useCallback, useEffect, useState } from "react";
import { Badge, Card, CardHeader, Empty, Field, Segmented, Spinner } from "@/components/ui";
import { useHashParams } from "@/lib/nav";
import { errText, onto, STATUS_LABEL, STATUS_TONE, type CapDef, type FunctionBrief, type Me, type ToolBrief } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { confirmDialog } from "@/lib/dialog";
import { toast } from "@/store/useAppStore";
const RISK_TONE: Record<string, string> = { low: "bg-ok/10 text-ok", medium: "bg-warn/10 text-warn", high: "bg-bad/10 text-bad" };
/**
* 外部系统 / API(数据连接页的一个页签):动作通过 webhook.call 回写外部系统、模型推理函数调用推理服务时用的
* 外部端点(地址 + 加密凭据)。
* 动作、AI 分析函数已经是本体元素,在「动作类型」「函数类型」里编辑。
*/
export function CapabilityPage({ me }: { me: Me | null }) {
const params = useHashParams();
const canEdit = !!me?.permissions.includes("build");
const [cat, setCat] = useState<{ functions: FunctionBrief[]; tools: ToolBrief[]; defs: CapDef[] } | null>(null);
const [sel, setSel] = useState<string>(params.get("id") || "");
const load = useCallback(() => { void onto.catalog().then(setCat).catch((e) => toast("error", errText(e))); }, []);
useEffect(load, [load]);
if (!cat) return <Spinner />;
const defs = cat.defs.filter((d) => d.kind === "endpoint");
return (
<div className="space-y-4">
{(
<div className="grid gap-4 lg:grid-cols-[280px_minmax(0,1fr)]">
<Card className="self-start">
<CardHeader title="外部端点" right={canEdit && <button className="btn btn-sm" onClick={() => setSel("new")}><Plus className="h-3.5 w-3.5" />新建</button>} />
<div className="divide-y divide-line">
{!defs.length && <Empty title="还没有外部端点" />}
{defs.map((d) => (
<button key={d.id} onClick={() => setSel(d.id)}
className={cn("block w-full px-3 py-2 text-left hover:bg-ink/[0.03]", sel === d.id && "bg-brand/5")}>
<div className="flex items-center gap-2">
<span className="min-w-0 flex-1 truncate text-body font-medium">{d.display_name || d.name}</span>
<Badge tone={STATUS_TONE[d.status]}>{STATUS_LABEL[d.status]}</Badge>
</div>
<p className="truncate text-caption text-muted">{d.spec.url}</p>
</button>
))}
</div>
</Card>
<div className="min-w-0">
{sel ? <EndpointEditor key={sel} def={sel === "new" ? null : defs.find((d) => d.id === sel) || null}
canEdit={canEdit} onSaved={(id) => { load(); setSel(id); }} onDeleted={() => { load(); setSel(""); }} />
: <Card><Empty icon={<Globe className="h-10 w-10" />} title="选择或新建外部端点" /></Card>}
</div>
</div>
)}
</div>
);
}
function EndpointEditor({ def, canEdit, onSaved, onDeleted }: {
def: CapDef | null; canEdit: boolean; onSaved: (id: string) => void; onDeleted: () => void;
}) {
const [name, setName] = useState(def?.name || "");
const [display, setDisplay] = useState(def?.display_name || "");
const [desc, setDesc] = useState(def?.description || "");
const [spec, setSpec] = useState<any>(structuredClone(def?.spec || { url: "https://", method: "POST", timeout: 15 }));
const [secret, setSecret] = useState("");
const [busy, setBusy] = useState(false);
const set = (k: string, v: any) => setSpec({ ...spec, [k]: v });
const save = async () => {
setBusy(true);
try {
const body: any = { kind: "endpoint", name, display_name: display, description: desc, spec };
if (secret.trim()) body.secret = JSON.parse(secret);
const d = await onto.saveCapDef(body, def?.id);
toast("ok", "已保存"); onSaved(d.id);
} catch (e) { toast("error", "保存失败", errText(e)); } finally { setBusy(false); }
};
const status = async (s: string) => {
try { await onto.setCapStatus(def!.id, s); toast("ok", STATUS_LABEL[s]); onSaved(def!.id); } catch (e) { toast("error", errText(e)); }
};
return (
<Card>
<CardHeader title={def ? (def.display_name || def.name) : "新建外部端点"} desc={def ? `v${def.version} · 风险 ${def.risk_level}` : undefined}
right={canEdit && <>
{def && def.status !== "published" && <button className="btn btn-sm" onClick={() => void status("published")}>启用</button>}
{def && def.status === "published" && <button className="btn btn-sm" onClick={() => void status("disabled")}>停用</button>}
{def && <button className="btn btn-sm" onClick={async () => {
if (!(await confirmDialog("删除这个端点?", { danger: true }))) return;
try { await onto.deleteCapDef(def.id); onDeleted(); } catch (e) { toast("error", errText(e)); }
}}><Trash2 className="h-3.5 w-3.5" /></button>}
<button className="btn btn-primary btn-sm" disabled={busy} onClick={() => void save()}><Save className="h-3.5 w-3.5" />保存</button>
</>} />
<div className="space-y-4 p-4">
<div className="grid gap-3 sm:grid-cols-3">
<Field label="标识名" required><input className="input" value={name} disabled={!canEdit} onChange={(e) => setName(e.target.value)} /></Field>
<Field label="显示名"><input className="input" value={display} disabled={!canEdit} onChange={(e) => setDisplay(e.target.value)} /></Field>
<Field label="说明"><input className="input" value={desc} disabled={!canEdit} onChange={(e) => setDesc(e.target.value)} /></Field>
</div>
<div className="grid gap-3 sm:grid-cols-3">
<Field label="地址" className="sm:col-span-2"><input className="input" value={spec.url} disabled={!canEdit} onChange={(e) => set("url", e.target.value)} /></Field>
<Field label="方法"><select className="input" value={spec.method} disabled={!canEdit} onChange={(e) => set("method", e.target.value)}>
{["POST", "PUT", "PATCH"].map((m) => <option key={m}>{m}</option>)}</select></Field>
</div>
<Field label="凭据请求头(JSON)" hint="加密保存;不填就不改">
<textarea className="input min-h-[60px] font-mono text-label" value={secret} disabled={!canEdit} placeholder='{"Authorization": "Bearer ..."}'
onChange={(e) => setSecret(e.target.value)} />
</Field>
<p className="text-label text-muted">外部端点只能被动作的「调用外部接口」效果使用,同样需要审批。</p>
</div>
</Card>
);
}