SopDraftView.tsx
11.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
/**
* 流程卡草稿就地看:做什么用、谁负责、要填哪些参数、每一步做什么(分支 / 循环缩进显示)。
* 问题挂在出问题的那一步下面,颜色分三档:红 = 必须改才能上线;黄 = 用到这次还没采纳的草案(采纳时会一起带上,不是错);
* 灰 = 提示。顶上一行写清文档里的步骤对上了几步、试运行结果;有错误时可以让 AI 按错误改,也可以一句话让 AI 改。
*/
import { AlertTriangle, Check, ExternalLink, Play, Wand2, X } from "lucide-react";
import { useState } from "react";
import { Badge, Spinner, Term } from "@/components/ui";
import { go, here } from "@/lib/nav";
import { errText, onto, type ModelReq, type ModelSopDraft } from "@/lib/onto";
import { cn } from "@/lib/utils";
import { toast } from "@/store/useAppStore";
import { DraftRefChip } from "./parts";
export function sopState(s: ModelSopDraft): { label: string; variant: "ok" | "warn" | "neutral" } {
if (s.status === "archived") return { label: "不采纳", variant: "neutral" };
if (s.current_version > 0) return { label: "已上线", variant: "ok" };
return { label: "待定", variant: "warn" };
}
export function SopDraftView({ s, runId, reqs, canEdit, busy, onAdopt, onDiscard, onChanged, onJumpName }: {
s: ModelSopDraft; runId: string; reqs: ModelReq[]; canEdit: boolean; busy: boolean;
onAdopt: () => void; onDiscard: () => void; onChanged: () => void; onJumpName: (name: string) => void;
}) {
const [working, setWorking] = useState<"" | "fix" | "trial">("");
const st = sopState(s);
const v = s.validation || { errors: [], warnings: [] };
const issues: any[] = v.issues || [];
const refs: any[] = v.draft_refs || [];
const params = s.params || [];
const pending = st.label === "待定";
// 「引用了还没发布的元素」那条提示和黄色小标说的是一回事,不再重复
const warnings = (v.warnings || []).filter((w) => !w.startsWith("引用了还没发布的元素"));
const cov = s.coverage || {};
const covers = cov.covers || {};
const missing = (cov.missing || []).map((id) => reqs.find((r) => r.id === id)).filter(Boolean) as ModelReq[];
const stepReq: Record<string, string[]> = {};
Object.entries(covers).forEach(([rid, sid]) => { (stepReq[sid] = stepReq[sid] || []).push(rid); });
const general = issues.filter((x) => !x.step_id && x.level === "error");
const fix = async () => {
setWorking("fix");
try {
const r = await onto.aiEditModelingSop(runId, s.id);
toast(r.changed ? (r.validation.errors.length ? "warn" : "ok") : "warn", r.changed ? "AI 改好了" : "没改", r.summary
+ (r.changed && r.validation.errors.length ? `;还剩 ${r.validation.errors.length} 处` : ""));
onChanged();
} catch (e) { toast("error", "AI 没改成", errText(e)); } finally { setWorking(""); }
};
const trial = async () => {
setWorking("trial");
try { await onto.trialModelingSop(runId, s.id); onChanged(); } catch (e) { toast("error", "试运行失败", errText(e)); } finally { setWorking(""); }
};
return (
<div className="space-y-4 p-4">
<div>
<div className="flex flex-wrap items-center gap-2">
<Badge variant="info"><Term>流程卡</Term></Badge>
<h3 className="min-w-0 break-words text-section font-semibold">{s.title}</h3>
<Badge variant={st.variant}>{st.label}</Badge>
</div>
{s.description && <p className="mt-2 break-words text-body">{s.description}</p>}
<p className="mt-1 text-caption text-muted">负责角色:{s.owner_role || "没指定"} · {s.step_count} 步
{s.schedule ? <> · 文档写的执行时间:<b className="text-ink/80">{s.schedule}</b>(已配成定时任务草稿,见「定时与监控」)</> : null}</p>
</div>
{s.issue && <p className="rounded-lg border border-bad/30 bg-bad/5 px-3 py-2 text-label text-bad">没能上线:{s.issue}</p>}
<div className="flex flex-wrap items-center gap-1.5 text-label">
{v.errors.length > 0 ? <Badge variant="danger">{v.errors.length} 处要改</Badge> : <Badge variant="ok">没有错误</Badge>}
{refs.length > 0 && <Badge variant="warn" title="采纳流程卡时会一起采纳">用到 {new Set(refs.map((r) => r.name)).size} 个待采纳的草案</Badge>}
{(cov.total || 0) > 0 && (
<Badge variant={cov.covered === cov.total ? "ok" : "warn"}>文档里的 {cov.total} 步对上了 {cov.covered} 步</Badge>
)}
{s.dry_run && (s.dry_run.summary || s.dry_run.error) && (
<Badge variant={s.dry_run.ok ? "ok" : s.dry_run.skipped ? "neutral" : "danger"} title={s.dry_run.error || ""}>
{s.dry_run.ok ? "试运行通过" : s.dry_run.skipped ? "还没试运行" : "试运行没走通"}</Badge>
)}
</div>
{s.dry_run?.summary && !s.dry_run.skipped && (
<p className={cn("rounded-md px-2.5 py-1.5 text-caption", s.dry_run.ok ? "bg-ok/[0.06]" : "bg-bad/5 text-bad")}>
试运行(不写数据、不发通知、审批视为通过):{s.dry_run.summary}</p>
)}
{missing.length > 0 && (
<div className="rounded-lg border border-warn/40 bg-warn/[0.06] px-3 py-2 text-label">
<p className="font-semibold text-[#b45309]">文档里这几步流程卡还没做:</p>
{missing.map((r) => <p key={r.id} className="text-ink/80">{r.id} {r.text}</p>)}
</div>
)}
{(s.fix_log || []).length > 0 && (
<p className="text-caption text-brand">AI 改过:{s.fix_log!.map((l) => `${l.summary || l.instruction}(错误 ${l.before_errors} → ${l.after_errors})`).join(";")}</p>
)}
{canEdit && (
<div className="flex flex-wrap gap-1.5">
{pending && <>
<button className="btn btn-primary btn-sm" disabled={busy || v.errors.length > 0} onClick={onAdopt}
title={v.errors.length ? "先把红色的错误改掉" : refs.length ? "用到的待采纳草案会一起采纳" : ""}>
{busy ? <Spinner className="h-3.5 w-3.5" /> : <Check className="h-3.5 w-3.5" />}采纳并上线</button>
{(v.errors.length > 0 || missing.length > 0) && (
<button className="btn btn-sm" disabled={!!working} onClick={() => void fix()}>
{working === "fix" ? <Spinner className="h-3.5 w-3.5" /> : <Wand2 className="h-3.5 w-3.5" />}让 AI 按问题改</button>
)}
{v.errors.length === 0 && (
<button className="btn btn-sm" disabled={!!working} onClick={() => void trial()}>
{working === "trial" ? <Spinner className="h-3.5 w-3.5" /> : <Play className="h-3.5 w-3.5" />}试运行</button>
)}
<button className="btn btn-sm" disabled={busy} onClick={onDiscard}><X className="h-3.5 w-3.5" />不要</button>
</>}
<button className="btn btn-sm" onClick={() => go("sop", { id: s.id, back: here() })}><ExternalLink className="h-3.5 w-3.5" />在流程卡编辑器中打开</button>
</div>
)}
<section>
<h4 className="mb-1.5 text-label font-semibold"><Term>参数</Term>(运行时要填,{params.length} 个)</h4>
{params.length ? (
<div className="overflow-x-auto rounded-lg border border-line">
<table className="tbl text-label">
<thead><tr><th>名称</th><th>类型</th><th>必填</th><th>默认</th></tr></thead>
<tbody>{params.map((p) => (
<tr key={p.name}><td className="break-words">{p.label || p.name}{p.description ? <span className="text-muted">:{p.description}</span> : null}</td>
<td className="text-muted">{p.type}{p.object_type ? ` · ${p.object_type}` : ""}</td><td>{p.required ? "是" : ""}</td>
<td className="text-muted">{p.default !== undefined && p.default !== null && p.default !== "" ? String(p.default) : ""}</td></tr>
))}</tbody>
</table>
</div>
) : <p className="text-label text-muted">不需要参数</p>}
</section>
<section>
<h4 className="mb-1.5 text-label font-semibold">步骤</h4>
{general.map((x, i) => <p key={i} className="mb-1 flex gap-1.5 text-label text-bad"><AlertTriangle className="mt-0.5 h-3.5 w-3.5 shrink-0" />{x.message}</p>)}
{s.outline.length ? (
<ol className="space-y-1 text-label">
{s.outline.map((x, i) => {
const errs = issues.filter((y) => y.step_id === x.id && x.id && y.level === "error");
const warns = issues.filter((y) => y.step_id === x.id && x.id && y.level === "warning" && !String(y.message).startsWith("引用了还没发布"));
const drafts = refs.filter((r) => r.step_id === x.id && x.id);
return (
<li key={i} style={{ paddingLeft: x.depth * 18 }}>
<p className={cn(x.kind === "case" ? "text-caption font-medium text-brand" : "", errs.length && "text-bad")}>
{x.title}{x.kind_label && <span className="ml-1.5 rounded bg-ink/[0.05] px-1.5 text-caption text-muted">{x.kind_label}</span>}
{(stepReq[x.id] || []).length > 0 && <span className="ml-1.5 text-caption text-muted">落实 {stepReq[x.id].join("、")}</span>}
{drafts.map((r) => <span key={r.name} className="ml-1.5"><DraftRefChip name={r.name} onClick={() => onJumpName(r.name)} /></span>)}
</p>
{errs.map((y, j) => <p key={`e${j}`} className="ml-3 flex gap-1 text-caption text-bad"><AlertTriangle className="mt-0.5 h-3 w-3 shrink-0" />{y.message}</p>)}
{warns.map((y, j) => <p key={`w${j}`} className="ml-3 text-caption text-muted">提示:{y.message}</p>)}
</li>
);
})}
</ol>
) : <p className="text-label text-muted">没有步骤</p>}
{warnings.filter((w) => !issues.some((y) => y.message === w && y.step_id)).map((w, i) => (
<p key={i} className="mt-1 text-caption text-muted">提示:{w}</p>
))}
</section>
{canEdit && pending && <SopAsk runId={runId} sid={s.id} onChanged={onChanged} />}
</div>
);
}
/** 一句话让 AI 改流程卡:流程的结构改动不好逐字段对比,改好且问题不比原来多就直接保存,再试运行一遍 */
function SopAsk({ runId, sid, onChanged }: { runId: string; sid: string; onChanged: () => void }) {
const [text, setText] = useState("");
const [busy, setBusy] = useState(false);
const [msg, setMsg] = useState("");
const go_ = async () => {
if (!text.trim()) return;
setBusy(true);
try {
const r = await onto.aiEditModelingSop(runId, sid, text.trim());
setMsg((r.changed ? "改好并保存了:" : "没改:") + r.summary
+ (r.changed ? `(现在 ${r.validation.errors.length} 处错误)` : ""));
if (r.changed) { setText(""); onChanged(); }
} catch (e) { toast("error", "AI 没改成", errText(e)); } finally { setBusy(false); }
};
return (
<section className="rounded-lg border border-dashed border-brand/40 p-3">
<h4 className="mb-1.5 flex items-center gap-1.5 text-label font-semibold"><Wand2 className="h-3.5 w-3.5 text-brand" />告诉 AI 怎么改这张流程卡</h4>
<div className="flex gap-1.5">
<input className="input h-8 flex-1" placeholder="比如:查到的结果按从大到小排序;通知里带上汇总数字;超期的逐条派待办" value={text}
disabled={busy} onChange={(e) => setText(e.target.value)} onKeyDown={(e) => { if (e.key === "Enter") void go_(); }} />
<button className="btn btn-sm" disabled={!text.trim() || busy} onClick={() => void go_()}>
{busy && <Spinner className="h-3.5 w-3.5" />}让 AI 改</button>
</div>
<p className="mt-1 text-caption text-muted">AI 改完会重新校验、试运行一遍;问题比原来多就不保存。改得不满意可以在流程卡编辑器里手动调。</p>
{msg && <p className="mt-1 text-label">{msg}</p>}
</section>
);
}