ActionCard.tsx
12.9 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import { AlertTriangle, ExternalLink, ListChecks, ShieldCheck, Undo2, Zap } from "lucide-react";
import { PreviewView } from "@/features/actions/ActionParts";
import { useEffect, useState } from "react";
import { Badge, Spinner } from "@/components/ui";
import { go } from "@/lib/nav";
import { ParamInput } from "@/features/sop/RunParts";
import { errText, onto, type ActionPreview, type AskCard, type Me, type SopParam } 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 <PreviewView preview={preview} />;
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function LegacyEffectList({ 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>
);
}
/** 当前登录人(各张卡片共用一次请求):没有「运行」权限的人只能看,不能发起。 */
let mePromise: Promise<Me> | null = null;
function useCanRun(): boolean | null {
const [can, setCan] = useState<boolean | null>(null);
useEffect(() => {
mePromise = mePromise || onto.me();
void mePromise.then((m) => setCan(m.permissions.includes("operate"))).catch(() => { mePromise = null; setCan(false); });
}, []);
return can;
}
/** 答案里 AI 建议的动作:人核对参数后提交审批,批准后才执行。提交过的卡片刷新后仍显示「已提交」。 */
export function SuggestedActionCard({ card, turnId, index, evidence = [] }: {
card: AskCard; turnId: string; index: number;
/** 这一轮取到的数与口径:审批人要看到「凭什么建议我批」 */
evidence?: { kind: string; text: string; ref: string }[];
}) {
const [reason, setReason] = useState(card.reason);
const [busy, setBusy] = useState(false);
const [runId, setRunId] = useState<string>(card.submitted?.ref || "");
// 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 canRun = useCanRun();
const defs: { name: string; label: string; type: string; required: boolean; enum: string[] }[] = card.param_defs || [];
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, now_id: preview?.now_id,
initiator_kind: "ai", source_ref: `turn:${turnId}`,
evidence: [{ kind: "ask", text: `由提问中的 AI 建议:${card.reason || ""}`.slice(0, 900), ref: `turn:${turnId}` }, ...evidence],
});
setRunId(run.id);
void onto.markAskCard(turnId, "action", index, run.id).catch(() => undefined);
toast("ok", run.status === "done" ? "已自动执行" : "已提交审批",
run.status === "done" ? "这个动作满足自动执行的条件" : "审批人在「审批与待办」里处理,批准后才会执行");
} 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-body font-medium">建议动作:{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>}
</div>
{(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">
{!runId && <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>
) : canRun === false ? (
<span className="text-label text-muted">你的账号不能发起动作,请有权限的同事来提交。</span>
) : (
<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>
{!runId && (
<p className="mt-1.5 text-caption text-muted">
AI 只能建议,不能执行。AI 建议的动作一律要人审批:提交后由{preview.action?.approver_roles?.length
? `「${preview.action.approver_roles.join("、")}」` : "有审批权限的人"}处理。
</p>
)}
</>
)}
</div>
);
}
/** 答案里 AI 建议发起的流程卡:不建审批单。提问的人核对参数后点「确认并发起」,流程就以他本人的名义开始,
* 流程里的动作再按各自的审批方式走。 */
export function SuggestedSopCard({ card, turnId, index }: { card: AskCard; turnId: string; index: number }) {
const [params, setParams] = useState<Record<string, any>>(card.params || {});
const [busy, setBusy] = useState(false);
const [runId, setRunId] = useState(card.submitted?.ref || "");
const canRun = useCanRun();
const defs: SopParam[] = card.param_defs || [];
const missing = defs.filter((p) => p.required && (p.default ?? "") === "" && (params[p.name] === undefined || params[p.name] === ""));
const confirm = async () => {
if (!card.sop_id) return;
setBusy(true);
try {
const run = await onto.runSop(card.sop_id, params, "ask");
setRunId(run.id);
void onto.markAskCard(turnId, "sop", index, run.id).catch(() => undefined);
toast("ok", `已发起「${card.display_name}」`, "需要人处理的步骤会出现在对应的人的待办里");
} 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">
<ListChecks className="h-4 w-4 text-brand" />
<span className="text-body font-medium">建议发起流程:{card.display_name}</span>
{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>
{card.reason && <p className="mt-1 text-label">{card.reason}</p>}
{card.description && <p className="mt-1 text-caption text-muted">{card.description}</p>}
{card.error ? (
<p className="mt-2 flex items-center gap-1.5 text-label text-warn"><AlertTriangle className="h-3.5 w-3.5" />{card.error}</p>
) : (
<>
{card.steps?.length ? (
<ol className="mt-2 space-y-1">
{card.steps.slice(0, 8).map((s, i) => (
<li key={s.id || i} className="flex items-start gap-2 text-caption" style={{ paddingLeft: `${(s.depth || 0) * 12}px` }}>
<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.kind_label}</span>
</li>
))}
</ol>
) : null}
{defs.length > 0 && !runId && (
<div className="mt-2 grid gap-2 sm:grid-cols-2">
{defs.map((p) => (
<label key={p.name} className="flex items-center gap-2 text-label">
<span className="w-20 shrink-0 truncate text-muted">{p.label || p.name}{p.required && <span className="text-bad">*</span>}</span>
<span className="min-w-0 flex-1"><ParamInput p={p} value={params[p.name]} onChange={(v) => setParams({ ...params, [p.name]: v })} /></span>
</label>
))}
</div>
)}
<div className="mt-2 flex flex-wrap items-center gap-2">
{runId ? (
<button className="btn btn-sm" onClick={() => go("sop", { run: runId })}>已发起,查看运行情况 <ExternalLink className="h-3 w-3" /></button>
) : canRun === false ? (
<span className="text-label text-muted">你的账号不能发起流程,请有权限的同事来确认。</span>
) : (
<button className="btn btn-primary btn-sm" disabled={busy || missing.length > 0}
title={missing.length ? `还没填:${missing.map((p) => p.label).join("、")}` : ""} onClick={confirm}>
{busy && <Spinner className="h-3.5 w-3.5" />} 确认并发起
</button>
)}
{!runId && <span className="text-caption text-muted">确认后以你的名义开始;里面的动作仍按各自的审批方式走。</span>}
</div>
</>
)}
</div>
);
}