ActionCard.tsx
4.17 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
import { AlertTriangle, ExternalLink, ShieldCheck, Undo2, Zap } from "lucide-react";
import { useState } from "react";
import { Badge, Spinner } from "@/components/ui";
import { go } from "@/lib/nav";
import { errText, onto, type ActionPreview, type AskCard } from "@/lib/onto";
import { toast } from "@/store/useAppStore";
const RISK: Record<string, string> = { low: "低风险", medium: "中风险", high: "高风险" };
const RISK_TONE: Record<string, string> = {
low: "bg-ok/10 text-ok", medium: "bg-warn/10 text-warn", high: "bg-bad/10 text-bad",
};
/** 动作预览:将调用哪些工具、改什么。审批人与提议人看到的是同一份预览。 */
export function EffectList({ preview }: { preview: ActionPreview }) {
return (
<ul className="space-y-1.5">
{preview.effects.map((e, i) => (
<li key={i} className="flex items-start gap-2 rounded-md bg-ink/[0.03] px-2 py-1.5 text-[12.5px]">
<Zap className="mt-0.5 h-3.5 w-3.5 shrink-0 text-brand" />
<div className="min-w-0 flex-1">
<p>{e.preview.summary}</p>
<p className="mt-0.5 text-[11px] text-muted">
工具 <code className="font-mono">{e.tool}</code>
{e.external && <span className="ml-1 text-bad">· 调用外部系统</span>}
</p>
</div>
<Badge tone={RISK_TONE[e.risk_level]}>{RISK[e.risk_level]}</Badge>
</li>
))}
</ul>
);
}
/** 答案里 AI 建议的动作。只能「提交审批」,不能直接执行。 */
export function SuggestedActionCard({ card, turnId }: { card: AskCard; turnId: string }) {
const [reason, setReason] = useState(card.reason);
const [busy, setBusy] = useState(false);
const [runId, setRunId] = useState<string>("");
const submit = async () => {
setBusy(true);
try {
const run = await onto.proposeAction(card.action_id || card.action, {
params: card.params, object_key: card.object_key, reason,
initiator_kind: "ai", source_ref: `turn:${turnId}`,
});
setRunId(run.id);
toast("ok", "已提交审批", "审批人在收件箱里处理,批准后才会执行");
} catch (e) {
toast("error", "提交失败", errText(e));
} finally {
setBusy(false);
}
};
return (
<div className="rounded-lg border border-brand/30 bg-brand/[0.03] p-3">
<div className="flex items-center gap-2">
<ShieldCheck className="h-4 w-4 text-brand" />
<span className="text-[13px] font-medium">建议动作:{card.display_name}</span>
{card.preview && <Badge tone={RISK_TONE[card.preview.action.risk_level]}>{RISK[card.preview.action.risk_level]}</Badge>}
{card.preview?.action.reversible && <Badge tone="bg-ink/5 text-muted"><Undo2 className="h-3 w-3" />可撤销</Badge>}
</div>
{card.error ? (
<p className="mt-2 flex items-center gap-1.5 text-[12.5px] text-warn">
<AlertTriangle className="h-3.5 w-3.5" /> 暂不能提交:{card.error}
</p>
) : card.preview && (
<>
<div className="mt-2"><EffectList preview={card.preview} /></div>
<div className="mt-2 flex flex-wrap items-center gap-2">
<input className="input h-8 min-w-0 flex-1 text-[12.5px]" value={reason}
onChange={(e) => setReason(e.target.value)} placeholder="提交理由(审批人会看到)" />
{runId ? (
<button className="btn btn-sm" onClick={() => go("inbox", { tab: "approvals", run: runId })}>
已提交,查看审批单 <ExternalLink className="h-3 w-3" />
</button>
) : (
<button className="btn btn-primary btn-sm" disabled={busy || !reason.trim()} onClick={submit}>
{busy && <Spinner className="h-3.5 w-3.5" />} 提交审批
</button>
)}
</div>
<p className="mt-1.5 text-[11px] text-muted">
AI 只能建议,不能执行。提交后由{card.preview.action.approver_roles.length
? `「${card.preview.action.approver_roles.join("、")}」` : "有审批权限的人"}审批,你本人不能审批自己提交的动作。
</p>
</>
)}
</div>
);
}