StepEditor.tsx
10.6 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
import { ArrowDown, ArrowUp, Plus, Trash2 } from "lucide-react";
import { Field } from "@/components/ui";
import type { CapDef, ConceptBrief, FunctionBrief, SopSpec, SopStep } from "@/lib/onto";
import { JsonBox } from "../capability/CapabilityPage";
import { STEP_COLOR, STEP_LABEL } from "./SopFlow";
export interface Catalog { functions: FunctionBrief[]; skills: CapDef[]; actions: CapDef[]; roles: ConceptBrief[]; sops: { id: string; title: string }[] }
/** 单个步骤的结构化表单。下一步只能从已有步骤里选,变量引用写 $变量.字段。 */
export function StepEditor({ step, spec, cat, disabled, onChange, onDelete, onMove }: {
step: SopStep; spec: SopSpec; cat: Catalog; disabled: boolean;
onChange: (s: SopStep) => void; onDelete: () => void; onMove: (d: -1 | 1) => void;
}) {
const set = (k: keyof SopStep, v: any) => onChange({ ...step, [k]: v });
const others = spec.steps.filter((s) => s.id !== step.id);
const nextSelect = (k: "next" | "default" | "on_reject", label: string) => (
<Field label={label}>
<select className="input h-8" value={(step as any)[k] || ""} disabled={disabled} onChange={(e) => set(k, e.target.value)}>
<option value="">(无)</option>
{others.map((s) => <option key={s.id} value={s.id}>{s.title}({s.id})</option>)}
</select>
</Field>
);
const vars = [...spec.params.map((p) => p.name), ...spec.steps.filter((s) => s.output).map((s) => s.output)];
return (
<div className="space-y-3 rounded-lg border-l-4 bg-surface p-3" style={{ borderLeftColor: STEP_COLOR[step.type] }}>
<div className="flex flex-wrap items-end gap-2">
<Field label="类型" className="w-32">
<select className="input h-8" value={step.type} disabled={disabled} onChange={(e) => set("type", e.target.value)}>
{Object.entries(STEP_LABEL).map(([k, v]) => <option key={k} value={k}>{v}</option>)}
</select>
</Field>
<Field label="标题" className="min-w-[160px] flex-1"><input className="input h-8" value={step.title} disabled={disabled} onChange={(e) => set("title", e.target.value)} /></Field>
<Field label="id" className="w-28"><input className="input h-8 font-mono" value={step.id} disabled={disabled}
onChange={(e) => set("id", e.target.value.replace(/[^\w-]/g, ""))} /></Field>
{step.type !== "end" && step.type !== "decision" &&
<Field label="输出变量" className="w-28"><input className="input h-8 font-mono" value={step.output} disabled={disabled} placeholder="如 rate"
onChange={(e) => set("output", e.target.value.replace(/\W/g, ""))} /></Field>}
{!disabled && (
<div className="ml-auto flex gap-1">
<button className="btn btn-ghost btn-sm" onClick={() => onMove(-1)}><ArrowUp className="h-3.5 w-3.5" /></button>
<button className="btn btn-ghost btn-sm" onClick={() => onMove(1)}><ArrowDown className="h-3.5 w-3.5" /></button>
<button className="btn btn-ghost btn-sm text-bad" onClick={onDelete}><Trash2 className="h-3.5 w-3.5" /></button>
</div>
)}
</div>
{step.type === "function" && (
<div className="grid gap-2 sm:grid-cols-[220px_1fr]">
<Field label="函数">
<select className="input h-8" value={step.function || ""} disabled={disabled} onChange={(e) => set("function", e.target.value)}>
<option value="">选择</option>
{cat.functions.map((f) => <option key={f.name} value={f.name}>{f.title}({f.name})</option>)}
</select>
</Field>
<Field label="参数(JSON)" hint={`可引用:${vars.map((v) => `$${v}`).join(" ") || "(暂无变量)"}`}>
<JsonBox key={step.id + step.function} value={step.args} disabled={disabled} onChange={(v) => set("args", v)}
placeholder={JSON.stringify(Object.fromEntries(Object.keys(cat.functions.find((f) => f.name === step.function)?.input_schema.properties || {}).map((k) => [k, ""])))} />
</Field>
</div>
)}
{step.type === "skill" && (
<div className="grid gap-2 sm:grid-cols-[220px_1fr]">
<Field label="技能">
<select className="input h-8" value={step.skill || ""} disabled={disabled} onChange={(e) => set("skill", e.target.value)}>
<option value="">选择</option>
{cat.skills.map((s) => <option key={s.id} value={s.name}>{s.display_name || s.name}{s.status !== "published" ? "(未发布)" : ""}</option>)}
</select>
</Field>
<Field label="输入(JSON)"><JsonBox key={step.id} value={step.inputs} disabled={disabled} onChange={(v) => set("inputs", v)} /></Field>
</div>
)}
{step.type === "decision" && (
<div className="space-y-1.5">
{(step.branches || []).map((b, i) => (
<div key={i} className="flex flex-wrap items-center gap-2">
<span className="text-[12px] text-muted">如果</span>
<input className="input h-8 min-w-[180px] flex-1 font-mono text-[12px]" value={b.when} disabled={disabled} placeholder="rate.value < 0.8"
onChange={(e) => { const bs = [...step.branches!]; bs[i] = { ...b, when: e.target.value }; set("branches", bs); }} />
<input className="input h-8 w-20" value={b.label} disabled={disabled} placeholder="标签"
onChange={(e) => { const bs = [...step.branches!]; bs[i] = { ...b, label: e.target.value }; set("branches", bs); }} />
<span className="text-[12px] text-muted">则去</span>
<select className="input h-8 w-40" value={b.next} disabled={disabled}
onChange={(e) => { const bs = [...step.branches!]; bs[i] = { ...b, next: e.target.value }; set("branches", bs); }}>
<option value="">选择</option>
{others.map((s) => <option key={s.id} value={s.id}>{s.title}</option>)}
</select>
{!disabled && <button className="btn btn-ghost btn-sm" onClick={() => set("branches", step.branches!.filter((_, j) => j !== i))}><Trash2 className="h-3.5 w-3.5" /></button>}
</div>
))}
{!disabled && <button className="btn btn-sm" onClick={() => set("branches", [...(step.branches || []), { when: "", next: "", label: "" }])}><Plus className="h-3.5 w-3.5" />加分支</button>}
<div className="w-60">{nextSelect("default", "都不满足时(默认)")}</div>
</div>
)}
{step.type === "human_task" && (
<div className="space-y-2">
<div className="grid gap-2 sm:grid-cols-3">
<Field label="执行角色">
<input className="input h-8" list="sop-roles" value={step.role || ""} disabled={disabled} onChange={(e) => set("role", e.target.value)} />
<datalist id="sop-roles">{cat.roles.map((r) => <option key={r.id} value={r.name} />)}</datalist>
</Field>
<Field label="时限(小时)"><input className="input h-8" type="number" value={step.sla_hours || 0} disabled={disabled} onChange={(e) => set("sla_hours", Number(e.target.value))} /></Field>
</div>
<Field label="要做什么(可引用 $变量)"><textarea className="input min-h-[48px]" value={step.instructions || ""} disabled={disabled} onChange={(e) => set("instructions", e.target.value)} /></Field>
<div className="grid gap-2 sm:grid-cols-2">
<Field label="表单字段(每行:name|中文名|必填)">
<textarea className="input min-h-[56px] font-mono text-[12px]" disabled={disabled}
value={(step.form || []).map((f) => `${f.name}|${f.label}|${f.required ? "必填" : ""}`).join("\n")}
onChange={(e) => set("form", e.target.value.split("\n").filter((l) => l.trim()).map((l) => {
const [name, label, req] = l.split("|"); return { name: name.trim(), label: (label || name).trim(), required: (req || "").includes("必") };
}))} />
</Field>
<Field label="检查清单(每行一项)">
<textarea className="input min-h-[56px]" disabled={disabled} value={(step.checklist || []).join("\n")}
onChange={(e) => set("checklist", e.target.value.split("\n").filter((l) => l.trim()))} />
</Field>
</div>
</div>
)}
{step.type === "action" && (
<div className="grid gap-2 sm:grid-cols-[220px_1fr]">
<div className="space-y-2">
<Field label="动作">
<select className="input h-8" value={step.action || ""} disabled={disabled} onChange={(e) => set("action", e.target.value)}>
<option value="">选择</option>
{cat.actions.map((a) => <option key={a.id} value={a.name}>{a.display_name || a.name}{a.status !== "published" ? "(未发布)" : ""}</option>)}
</select>
</Field>
<Field label="作用对象"><input className="input h-8 font-mono" value={step.object_key || ""} disabled={disabled} placeholder="$store.key"
onChange={(e) => set("object_key", e.target.value)} /></Field>
</div>
<Field label="参数(JSON)" hint="提交后流程挂起,审批通过走「下一步」,驳回走「驳回后」">
<JsonBox key={step.id + step.action} value={step.params} disabled={disabled} onChange={(v) => set("params", v)}
placeholder={JSON.stringify(Object.fromEntries((cat.actions.find((a) => a.name === step.action)?.spec.params || []).map((p: any) => [p.name, ""])))} />
</Field>
</div>
)}
{step.type === "subprocess" && (
<Field label="子流程" className="w-72">
<select className="input h-8" value={step.sop || ""} disabled={disabled} onChange={(e) => set("sop", e.target.value)}>
<option value="">选择</option>{cat.sops.map((s) => <option key={s.id} value={s.id}>{s.title}</option>)}
</select>
</Field>
)}
{step.type === "end" && (
<Field label="结果说明"><input className="input h-8" value={step.outcome || ""} disabled={disabled} onChange={(e) => set("outcome", e.target.value)} /></Field>
)}
{step.type !== "end" && step.type !== "decision" && (
<div className="flex flex-wrap gap-2">
<div className="w-60">{nextSelect("next", step.type === "action" ? "批准后" : "下一步")}</div>
{step.type === "action" && <div className="w-60">{nextSelect("on_reject", "驳回后")}</div>}
</div>
)}
</div>
);
}