CapabilityPage.tsx
10.2 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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";
type Tab = "endpoint" | "builtin";
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 [tab, setTab] = useState<Tab>("endpoint");
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">
<Segmented value={tab} onChange={(t) => { setTab(t); setSel(""); }} options={[
{ key: "endpoint", label: "外部端点" }, { key: "builtin", label: "系统函数与效果(参考)" }]} />
{tab === "builtin" ? <Builtins cat={cat} /> : (
<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="还没有外部端点" desc="动作要通知企业微信、调用 ERP 之类的外部系统时,在这里登记地址与凭据。" />}
{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 Builtins({ cat }: { cat: { functions: FunctionBrief[]; tools: ToolBrief[] } }) {
const [open, setOpen] = useState("");
const [args, setArgs] = useState("{}");
const [out, setOut] = useState<any>(null);
return (
<div className="grid gap-4 xl:grid-cols-2">
<Card>
<CardHeader title={<span className="flex items-center gap-1.5"><Wrench className="h-4 w-4" />函数(只读)</span>}
desc="AI、SOP、技能都能直接调用。没有副作用。" />
<div className="divide-y divide-line">
{cat.functions.map((f) => (
<div key={f.name} className="px-4 py-2.5">
<button className="w-full text-left" onClick={() => { setOpen(open === f.name ? "" : f.name); setOut(null); setArgs("{}"); }}>
<p className="text-body font-medium">{f.title} <code className="ml-1 text-caption text-muted">{f.name}</code></p>
<p className="text-label text-muted">{f.description}</p>
</button>
{open === f.name && (
<div className="mt-2 space-y-2">
<p className="text-caption text-muted">参数:{Object.entries(f.input_schema.properties || {}).map(([k, v]: any) =>
`${k}${(f.input_schema.required || []).includes(k) ? "*" : ""}(${v.description || v.type})`).join(";")}</p>
<textarea className="input min-h-[60px] font-mono text-label" value={args} onChange={(e) => setArgs(e.target.value)} />
<button className="btn btn-sm" onClick={async () => {
try { setOut(await onto.callFunction(f.name, JSON.parse(args || "{}"))); } catch (e) { setOut({ error: errText(e) }); }
}}><Play className="h-3.5 w-3.5" />试调用</button>
{out && <pre className="max-h-64 overflow-auto rounded bg-ink/5 p-2 text-caption">{JSON.stringify(out, null, 2)}</pre>}
</div>
)}
</div>
))}
</div>
</Card>
<Card className="self-start">
<CardHeader title={<span className="flex items-center gap-1.5"><Zap className="h-4 w-4" />工具(有副作用)</span>}
desc="只能被经过人工审批的动作调用,AI 与 SOP 不能直接用。" />
<div className="divide-y divide-line">
{cat.tools.map((t) => (
<div key={t.name} className="px-4 py-2.5">
<p className="flex items-center gap-2 text-body font-medium">{t.title} <code className="text-caption text-muted">{t.name}</code>
<Badge tone={RISK_TONE[t.risk_level]}>{t.risk_level}</Badge>
{t.reversible && <Badge>可撤销</Badge>}{t.external && <Badge tone="bg-bad/10 text-bad">外部</Badge>}</p>
<p className="text-label text-muted">{t.description}</p>
</div>
))}
</div>
</Card>
</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="加密保存、永不回显、AI 看不到。留空表示不修改">
<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>
);
}