ActionCard.tsx
8.84 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
164
165
166
167
import { AlertTriangle, ExternalLink, ListChecks, 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.auto && (
<li className={`rounded-md px-2 py-1 text-caption ${preview.auto.eligible ? "bg-ok/10 text-ok" : "bg-ink/[0.03] text-muted"}`}>
{preview.auto.eligible ? "提交后将自动执行:" : "需要人工审批:"}{preview.auto.why}
</li>
)}
{(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-label">
<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 || e.tool}</p>
<p className="mt-0.5 text-caption 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>
);
}
/** 流程卡提案里的步骤概要:审批人要先看清「批了之后会发生什么」。 */
function StepOutline({ steps }: { steps: { id: string; title: string; type_label: string; lane?: string }[] }) {
return (
<ol className="mt-2 space-y-1">
{steps.map((s, i) => (
<li key={s.id} className="flex items-start gap-2 text-caption">
<span className="mt-0.5 flex h-4 w-4 shrink-0 items-center justify-center rounded-full bg-ink/[0.06] text-[10px] text-muted">{i + 1}</span>
<span className="min-w-0 flex-1 truncate">{s.title}</span>
<span className="shrink-0 text-muted">{s.type_label}{s.lane ? ` · ${s.lane}` : ""}</span>
</li>
))}
</ol>
);
}
/** 答案里 AI 建议的动作,或建议发起的流程卡(card.kind === "sop")。
* 两者都只能「提交审批」:动作批准后执行它的效果,流程卡批准后才真正跑起来。 */
export function SuggestedActionCard({ card, turnId, evidence = [] }: {
card: AskCard; turnId: string;
/** 这一轮取到的数与口径:审批人要看到「凭什么建议我批」 */
evidence?: { kind: string; text: string; ref: string }[];
}) {
const [reason, setReason] = useState(card.reason);
const [busy, setBusy] = useState(false);
const [runId, setRunId] = useState<string>("");
// AI 填的参数只是建议:人可以改,改完重新预览再提交
const [params, setParams] = useState<Record<string, any>>(card.params || {});
const [objKey, setObjKey] = useState(card.object_key || "");
const [preview, setPreview] = useState(card.preview);
const [err, setErr] = useState(card.error || "");
const [dirty, setDirty] = useState(false);
const defs = card.param_defs || [];
const isSop = card.kind === "sop";
const repreview = async () => {
setBusy(true);
try {
setPreview(await onto.prepareAction(card.action_id || card.action, params, objKey));
setErr(""); setDirty(false);
} catch (e) { setPreview(undefined); setErr(errText(e)); } finally { setBusy(false); }
};
const submit = async () => {
setBusy(true);
try {
const run = await onto.proposeAction(card.action_id || card.action, {
params, object_key: objKey, reason,
initiator_kind: "ai", source_ref: `turn:${turnId}`,
evidence: [{ kind: "ask", text: `由提问中的 AI 建议:${card.reason || ""}`.slice(0, 900), ref: `turn:${turnId}` }, ...evidence],
});
setRunId(run.id);
toast("ok", "已提交审批", isSop ? "审批人在「审批与待办」里处理,批准后流程才会跑起来"
: "审批人在「审批与待办」里处理,批准后才会执行");
} 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">
{isSop ? <ListChecks className="h-4 w-4 text-brand" /> : <ShieldCheck className="h-4 w-4 text-brand" />}
<span className="text-body font-medium">{isSop ? "建议发起流程" : "建议动作"}:{card.display_name}</span>
{preview && <Badge tone={RISK_TONE[preview.action.risk_level]}>{RISK[preview.action.risk_level]}</Badge>}
{preview?.action.reversible && <Badge tone="bg-ink/5 text-muted"><Undo2 className="h-3 w-3" />可撤销</Badge>}
{isSop && card.sop_id && (
<button className="ml-auto text-caption text-muted hover:text-brand" onClick={() => go("sop", { id: card.sop_id! })}>
查看流程卡 <ExternalLink className="inline h-3 w-3" />
</button>
)}
</div>
{isSop && card.description && <p className="mt-1 text-caption text-muted">{card.description}</p>}
{isSop && card.steps?.length ? <StepOutline steps={card.steps} /> : null}
{(defs.length > 0 || card.target_type) && !runId && (
<div className="mt-2 grid gap-2 sm:grid-cols-2">
{card.target_type && (
<label className="flex items-center gap-2 text-label"><span className="w-20 shrink-0 text-muted">作用对象</span>
<input className="input h-8 flex-1 font-mono text-label" value={objKey} placeholder="对象主键"
onChange={(e) => { setObjKey(e.target.value); setDirty(true); }} /></label>
)}
{defs.map((d) => (
<label key={d.name} className="flex items-center gap-2 text-label">
<span className="w-20 shrink-0 truncate text-muted" title={d.name}>{d.label || d.name}{d.required && <span className="text-bad">*</span>}</span>
{d.enum?.length ? (
<select className="input h-8 flex-1" value={params[d.name] ?? ""} onChange={(e) => { setParams({ ...params, [d.name]: e.target.value }); setDirty(true); }}>
<option value="">请选择</option>{d.enum.map((x) => <option key={x}>{x}</option>)}
</select>
) : (
<input className="input h-8 flex-1 text-label" value={params[d.name] ?? ""} inputMode={d.type === "number" || d.type === "integer" ? "decimal" : undefined}
onChange={(e) => { setParams({ ...params, [d.name]: e.target.value }); setDirty(true); }} />
)}
</label>
))}
{(dirty || err) && <button className="btn btn-sm w-fit" disabled={busy} onClick={() => void repreview()}>{busy && <Spinner className="h-3.5 w-3.5" />}重新预览</button>}
</div>
)}
{err ? (
<p className="mt-2 flex items-center gap-1.5 text-label text-warn">
<AlertTriangle className="h-3.5 w-3.5" /> 暂不能提交:{err}{defs.length > 0 && "(改一下上面的参数再预览)"}
</p>
) : preview && (
<>
<div className="mt-2"><EffectList preview={preview} /></div>
<div className="mt-2 flex flex-wrap items-center gap-2">
<input className="input h-8 min-w-0 flex-1 text-label" 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 || dirty || !reason.trim()} title={dirty ? "参数改过了,先重新预览" : ""} onClick={submit}>
{busy && <Spinner className="h-3.5 w-3.5" />} 提交审批
</button>
)}
</div>
<p className="mt-1.5 text-caption text-muted">
AI 只能建议,不能{isSop ? "发起" : "执行"}。提交后由{preview.action?.approver_roles?.length
? `「${preview.action.approver_roles.join("、")}」` : "有审批权限的人"}审批
{isSop ? ",批准后流程才会开始跑 —— 里面的人工任务会派给对应角色,动作步骤各自再走一次审批"
: ",你本人不能审批自己提交的动作"}。
</p>
</>
)}
</div>
);
}