@sanity/workflow-engine 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks-cjs/schema.cjs +291 -317
- package/dist/_chunks-cjs/schema.cjs.map +1 -1
- package/dist/_chunks-es/schema.js +291 -317
- package/dist/_chunks-es/schema.js.map +1 -1
- package/dist/define.cjs +480 -74
- package/dist/define.cjs.map +1 -1
- package/dist/define.d.cts +7404 -4450
- package/dist/define.d.ts +7404 -4450
- package/dist/define.js +482 -76
- package/dist/define.js.map +1 -1
- package/dist/index.cjs +1650 -1521
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10557 -5214
- package/dist/index.d.ts +10557 -5214
- package/dist/index.js +1651 -1522
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/define.cjs
CHANGED
|
@@ -17,108 +17,513 @@ function _interopNamespaceCompat(e) {
|
|
|
17
17
|
}), n.default = e, Object.freeze(n);
|
|
18
18
|
}
|
|
19
19
|
var v__namespace = /* @__PURE__ */ _interopNamespaceCompat(v);
|
|
20
|
+
function groq(strings, ...values) {
|
|
21
|
+
return strings.reduce((out, part, i) => i === 0 ? part : `${out}${serializeGroqValue(values[i - 1])}${part}`, "");
|
|
22
|
+
}
|
|
23
|
+
function serializeGroqValue(value) {
|
|
24
|
+
const serialized = JSON.stringify(value);
|
|
25
|
+
if (serialized === void 0)
|
|
26
|
+
throw new Error(
|
|
27
|
+
`groq tag cannot serialize ${typeof value} \u2014 interpolate JSON-representable values only`
|
|
28
|
+
);
|
|
29
|
+
return serialized;
|
|
30
|
+
}
|
|
31
|
+
function desugarWorkflow(authoring) {
|
|
32
|
+
const ctx = { issues: [], claimStates: /* @__PURE__ */ new Map(), claimedStates: /* @__PURE__ */ new Set() }, workflowState = desugarStateEntries(authoring.state, ["state"], ctx), workflowLayer = layerOf(workflowState), stages = authoring.stages.map((stage, i) => {
|
|
33
|
+
const path = ["stages", i], stageState = desugarStateEntries(stage.state, [...path, "state"], ctx), stageEnv = {
|
|
34
|
+
layers: [
|
|
35
|
+
{ scope: "stage", entries: layerOf(stageState) },
|
|
36
|
+
{ scope: "workflow", entries: workflowLayer }
|
|
37
|
+
]
|
|
38
|
+
}, tasks = (stage.tasks ?? []).map(
|
|
39
|
+
(task, j) => desugarTask(task, [...path, "tasks", j], stageEnv, ctx)
|
|
40
|
+
), transitions = (stage.transitions ?? []).map(
|
|
41
|
+
(transition, k) => desugarTransition(transition, [...path, "transitions", k], stageEnv, ctx)
|
|
42
|
+
);
|
|
43
|
+
return {
|
|
44
|
+
...stripUndefined({
|
|
45
|
+
name: stage.name,
|
|
46
|
+
title: stage.title,
|
|
47
|
+
description: stage.description,
|
|
48
|
+
guards: stage.guards
|
|
49
|
+
}),
|
|
50
|
+
...stageState ? { state: stageState } : {},
|
|
51
|
+
...tasks.length > 0 ? { tasks } : {},
|
|
52
|
+
...transitions.length > 0 ? { transitions } : {}
|
|
53
|
+
};
|
|
54
|
+
});
|
|
55
|
+
return checkUnclaimedClaimStates(ctx), { definition: {
|
|
56
|
+
...stripUndefined({
|
|
57
|
+
name: authoring.name,
|
|
58
|
+
version: authoring.version,
|
|
59
|
+
title: authoring.title,
|
|
60
|
+
description: authoring.description,
|
|
61
|
+
initialStage: authoring.initialStage,
|
|
62
|
+
predicates: authoring.predicates
|
|
63
|
+
}),
|
|
64
|
+
...workflowState ? { state: workflowState } : {},
|
|
65
|
+
stages
|
|
66
|
+
}, issues: ctx.issues };
|
|
67
|
+
}
|
|
68
|
+
function desugarStateEntries(entries, path, ctx) {
|
|
69
|
+
return !entries || entries.length === 0 ? entries === void 0 ? void 0 : [] : entries.map((entry, i) => {
|
|
70
|
+
if (entry.type !== "claim") return entry;
|
|
71
|
+
const desugared = {
|
|
72
|
+
...stripUndefined({ name: entry.name, title: entry.title, description: entry.description }),
|
|
73
|
+
type: "value.actor",
|
|
74
|
+
source: { type: "write" }
|
|
75
|
+
};
|
|
76
|
+
return ctx.claimStates.set(desugared, { name: entry.name, path: [...path, i] }), desugared;
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
function layerOf(entries) {
|
|
80
|
+
return new Map((entries ?? []).map((entry) => [entry.name, entry]));
|
|
81
|
+
}
|
|
82
|
+
function desugarTask(task, path, stageEnv, ctx) {
|
|
83
|
+
const taskState = desugarStateEntries(task.state, [...path, "state"], ctx), env = {
|
|
84
|
+
layers: [{ scope: "task", entries: layerOf(taskState) }, ...stageEnv.layers]
|
|
85
|
+
}, ops = desugarOps(task.ops, [...path, "ops"], env, task.name, ctx), actions = (task.actions ?? []).map(
|
|
86
|
+
(action, a) => desugarAction(action, [...path, "actions", a], env, task.name, ctx)
|
|
87
|
+
);
|
|
88
|
+
return {
|
|
89
|
+
...stripUndefined({
|
|
90
|
+
name: task.name,
|
|
91
|
+
title: task.title,
|
|
92
|
+
description: task.description,
|
|
93
|
+
filter: task.filter,
|
|
94
|
+
completeWhen: task.completeWhen,
|
|
95
|
+
failWhen: task.failWhen,
|
|
96
|
+
effects: task.effects,
|
|
97
|
+
subworkflows: task.subworkflows
|
|
98
|
+
}),
|
|
99
|
+
activation: task.activation ?? "manual",
|
|
100
|
+
...taskState ? { state: taskState } : {},
|
|
101
|
+
...ops ? { ops } : {},
|
|
102
|
+
...actions.length > 0 ? { actions } : {}
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
function desugarAction(action, path, env, taskName, ctx) {
|
|
106
|
+
if ("type" in action)
|
|
107
|
+
return desugarClaimAction(action, path, env, ctx);
|
|
108
|
+
const filter = composeFilter([rolesCondition(action.roles), action.filter]), ops = desugarOps(action.ops, [...path, "ops"], env, taskName, ctx) ?? [];
|
|
109
|
+
return action.status !== void 0 && ops.push({ type: "status.set", task: taskName, status: action.status }), {
|
|
110
|
+
...stripUndefined({
|
|
111
|
+
name: action.name,
|
|
112
|
+
title: action.title,
|
|
113
|
+
description: action.description,
|
|
114
|
+
params: action.params,
|
|
115
|
+
effects: action.effects
|
|
116
|
+
}),
|
|
117
|
+
...filter ? { filter } : {},
|
|
118
|
+
...ops.length > 0 ? { ops } : {}
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
function desugarClaimAction(action, path, env, ctx) {
|
|
122
|
+
const ref = typeof action.state == "string" ? { state: action.state } : action.state, resolved = resolveRef(ref, env, [...path, "state"], ctx);
|
|
123
|
+
if (resolved) {
|
|
124
|
+
const entry = entryAt(env, resolved);
|
|
125
|
+
entry && entry.type !== "value.actor" && ctx.issues.push({
|
|
126
|
+
path: [...path, "state"],
|
|
127
|
+
message: `claim action "${action.name}" references "${resolved.state}" of kind "${entry.type}" \u2014 a claim pair needs an actor-valued entry (a "claim" or "value.actor" state declaration)`
|
|
128
|
+
}), checkShadowedClaimTarget(action.name, ref.state, resolved, env, [...path, "state"], ctx), entry && ctx.claimedStates.add(entry);
|
|
129
|
+
}
|
|
130
|
+
const noSteal = `!defined($state.${ref.state})`, filter = composeFilter([noSteal, rolesCondition(action.roles), action.filter]), ops = resolved ? [{ type: "state.set", target: resolved, value: { type: "actor" } }] : [];
|
|
131
|
+
return {
|
|
132
|
+
...stripUndefined({
|
|
133
|
+
name: action.name,
|
|
134
|
+
title: action.title,
|
|
135
|
+
description: action.description,
|
|
136
|
+
params: action.params,
|
|
137
|
+
effects: action.effects
|
|
138
|
+
}),
|
|
139
|
+
filter,
|
|
140
|
+
...ops.length > 0 ? { ops } : {}
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
function checkShadowedClaimTarget(actionName, state, resolved, env, path, ctx) {
|
|
144
|
+
const nearest = env.layers.find((l) => l.entries.has(state));
|
|
145
|
+
nearest === void 0 || nearest.scope === resolved.scope || ctx.issues.push({
|
|
146
|
+
path,
|
|
147
|
+
message: `claim action "${actionName}" targets "${state}" at scope "${resolved.scope}", but a nearer ${nearest.scope}-scope entry of the same name shadows it in $state \u2014 the no-steal filter would read the shadowing entry while the claim writes the "${resolved.scope}" one. Rename one of the entries or claim the nearer one`
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
function checkUnclaimedClaimStates(ctx) {
|
|
151
|
+
for (const [entry, { name, path }] of ctx.claimStates)
|
|
152
|
+
ctx.claimedStates.has(entry) || ctx.issues.push({
|
|
153
|
+
path,
|
|
154
|
+
message: `claim state "${name}" is never referenced by a claim action \u2014 you announced the pattern and wrote half of it. Declare a defineAction({ type: "claim", state: "${name}" }) or use a raw value.actor entry`
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
function desugarTransition(transition, path, stageEnv, ctx) {
|
|
158
|
+
const ops = desugarOps(transition.ops, [...path, "ops"], stageEnv, void 0, ctx);
|
|
159
|
+
return {
|
|
160
|
+
...stripUndefined({
|
|
161
|
+
name: transition.name,
|
|
162
|
+
title: transition.title,
|
|
163
|
+
description: transition.description,
|
|
164
|
+
to: transition.to,
|
|
165
|
+
effects: transition.effects
|
|
166
|
+
}),
|
|
167
|
+
filter: transition.filter ?? "$allTasksDone",
|
|
168
|
+
...ops ? { ops } : {}
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
function desugarOps(ops, path, env, firingTask, ctx) {
|
|
172
|
+
if (ops)
|
|
173
|
+
return ops.map((op, i) => desugarOp(op, [...path, i], env, firingTask, ctx));
|
|
174
|
+
}
|
|
175
|
+
function desugarOp(op, path, env, firingTask, ctx) {
|
|
176
|
+
if (op.type === "status.set") {
|
|
177
|
+
const task = op.task ?? firingTask;
|
|
178
|
+
return task === void 0 && ctx.issues.push({
|
|
179
|
+
path: [...path, "task"],
|
|
180
|
+
message: "status.set outside an action must name its target task"
|
|
181
|
+
}), { type: "status.set", task: task ?? "", status: op.status };
|
|
182
|
+
}
|
|
183
|
+
if (op.type === "audit") return desugarAuditOp(op, path, env, ctx);
|
|
184
|
+
const target = resolveRef(op.target, env, [...path, "target"], ctx) ?? fallbackRef(op.target);
|
|
185
|
+
return { ...op, target };
|
|
186
|
+
}
|
|
187
|
+
const AUDIT_STAMPS = { actor: { type: "actor" }, at: { type: "now" } };
|
|
188
|
+
function desugarAuditOp(op, path, env, ctx) {
|
|
189
|
+
const target = resolveRef(op.target, env, [...path, "target"], ctx) ?? fallbackRef(op.target);
|
|
190
|
+
if (op.value.type !== "object")
|
|
191
|
+
return ctx.issues.push({
|
|
192
|
+
path: [...path, "value"],
|
|
193
|
+
message: `audit value must be an object source carrying the domain fields (got "${op.value.type}")`
|
|
194
|
+
}), { type: "state.append", target, value: op.value };
|
|
195
|
+
const actorField = op.stampFields?.actor ?? "actor", atField = op.stampFields?.at ?? "at", fields = { ...op.value.fields };
|
|
196
|
+
for (const [stamp, source] of [
|
|
197
|
+
[actorField, AUDIT_STAMPS.actor],
|
|
198
|
+
[atField, AUDIT_STAMPS.at]
|
|
199
|
+
]) {
|
|
200
|
+
if (stamp in fields) {
|
|
201
|
+
ctx.issues.push({
|
|
202
|
+
path: [...path, "value", "fields", stamp],
|
|
203
|
+
message: `audit stamp field "${stamp}" collides with an authored domain field \u2014 rename yours or remap the stamp via stampFields`
|
|
204
|
+
});
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
fields[stamp] = source;
|
|
208
|
+
}
|
|
209
|
+
return { type: "state.append", target, value: { type: "object", fields } };
|
|
210
|
+
}
|
|
211
|
+
function resolveRef(ref, env, path, ctx) {
|
|
212
|
+
const layers = ref.scope === void 0 ? env.layers : env.layers.filter((l) => l.scope === ref.scope);
|
|
213
|
+
for (const layer of layers)
|
|
214
|
+
if (layer.entries.has(ref.state)) return { scope: layer.scope, state: ref.state };
|
|
215
|
+
const reachable = env.layers.flatMap((l) => [...l.entries.keys()].map((n) => `${l.scope}:${n}`));
|
|
216
|
+
ctx.issues.push({
|
|
217
|
+
path,
|
|
218
|
+
message: `state reference "${ref.state}"${ref.scope ? ` (scope "${ref.scope}")` : ""} does not resolve to a declared entry. Reachable: ${reachable.join(", ") || "(none)"}`
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
function entryAt(env, ref) {
|
|
222
|
+
return env.layers.find((l) => l.scope === ref.scope)?.entries.get(ref.state);
|
|
223
|
+
}
|
|
224
|
+
function fallbackRef(ref) {
|
|
225
|
+
return { scope: ref.scope ?? "workflow", state: ref.state };
|
|
226
|
+
}
|
|
227
|
+
function rolesCondition(roles) {
|
|
228
|
+
if (!(!roles || roles.length === 0))
|
|
229
|
+
return groq`count($actor.roles[@ in ${roles}]) > 0`;
|
|
230
|
+
}
|
|
231
|
+
function composeFilter(parts) {
|
|
232
|
+
const present = parts.filter((p) => p !== void 0);
|
|
233
|
+
if (present.length !== 0)
|
|
234
|
+
return present.length === 1 ? present[0] : present.map((p) => `(${p})`).join(" && ");
|
|
235
|
+
}
|
|
236
|
+
function stripUndefined(obj) {
|
|
237
|
+
const out = {};
|
|
238
|
+
for (const [k, value] of Object.entries(obj))
|
|
239
|
+
value !== void 0 && (out[k] = value);
|
|
240
|
+
return out;
|
|
241
|
+
}
|
|
242
|
+
const RESERVED_CONDITION_VARS = [
|
|
243
|
+
"state",
|
|
244
|
+
"actor",
|
|
245
|
+
"assigned",
|
|
246
|
+
"can",
|
|
247
|
+
"row",
|
|
248
|
+
"now",
|
|
249
|
+
"effects",
|
|
250
|
+
"tasks",
|
|
251
|
+
"subworkflows",
|
|
252
|
+
"self",
|
|
253
|
+
"stage",
|
|
254
|
+
"parent",
|
|
255
|
+
"ancestors",
|
|
256
|
+
"allTasksDone",
|
|
257
|
+
"anyTaskFailed"
|
|
258
|
+
], PREDICATE_CALLER_VARS = ["actor", "assigned", "can"];
|
|
20
259
|
function knownList(ids) {
|
|
21
260
|
return [...ids].map((s) => `"${s}"`).join(", ");
|
|
22
261
|
}
|
|
23
|
-
function
|
|
24
|
-
const
|
|
25
|
-
for (const
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
262
|
+
function checkDuplicates(names, what, issues) {
|
|
263
|
+
const seen = /* @__PURE__ */ new Set();
|
|
264
|
+
for (const { name, path } of names)
|
|
265
|
+
seen.has(name) && issues.push({ path, message: `duplicate ${what} "${name}" (already declared earlier)` }), seen.add(name);
|
|
266
|
+
return seen;
|
|
267
|
+
}
|
|
268
|
+
function checkStages(def, issues) {
|
|
269
|
+
const stageNames = checkDuplicates(
|
|
270
|
+
def.stages.map((stage, i) => ({ name: stage.name, path: ["stages", i, "name"] })),
|
|
271
|
+
"stage name",
|
|
272
|
+
issues
|
|
273
|
+
);
|
|
274
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
275
|
+
const taskNames = checkDuplicates(
|
|
276
|
+
(stage.tasks ?? []).map((task, j) => ({
|
|
277
|
+
name: task.name,
|
|
278
|
+
path: ["stages", i, "tasks", j, "name"]
|
|
279
|
+
})),
|
|
280
|
+
`task name in stage "${stage.name}"`,
|
|
281
|
+
issues
|
|
282
|
+
);
|
|
283
|
+
checkDuplicates(
|
|
284
|
+
(stage.transitions ?? []).map((t, k) => ({
|
|
285
|
+
name: t.name,
|
|
286
|
+
path: ["stages", i, "transitions", k, "name"]
|
|
287
|
+
})),
|
|
288
|
+
`transition name in stage "${stage.name}"`,
|
|
289
|
+
issues
|
|
290
|
+
), checkTasks(def, i, taskNames, issues);
|
|
291
|
+
}
|
|
292
|
+
return stageNames;
|
|
293
|
+
}
|
|
294
|
+
function checkTasks(def, i, taskNames, issues) {
|
|
295
|
+
const stage = def.stages[i];
|
|
296
|
+
for (const [j, task] of (stage.tasks ?? []).entries()) {
|
|
297
|
+
const path = ["stages", i, "tasks", j];
|
|
298
|
+
checkDuplicates(
|
|
299
|
+
(task.actions ?? []).map((action, a) => ({
|
|
300
|
+
name: action.name,
|
|
301
|
+
path: [...path, "actions", a, "name"]
|
|
302
|
+
})),
|
|
303
|
+
`action name in task "${task.name}"`,
|
|
304
|
+
issues
|
|
305
|
+
), checkStatusSetTargets(task, taskNames, path, issues);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
function checkStatusSetTargets(task, taskNames, path, issues) {
|
|
309
|
+
checkStatusSetOps(task.ops, taskNames, [...path, "ops"], issues);
|
|
310
|
+
for (const [a, action] of (task.actions ?? []).entries())
|
|
311
|
+
checkStatusSetOps(action.ops, taskNames, [...path, "actions", a, "ops"], issues);
|
|
312
|
+
}
|
|
313
|
+
function checkStatusSetOps(ops, taskNames, path, issues) {
|
|
314
|
+
for (const [o, op] of (ops ?? []).entries())
|
|
315
|
+
op.type !== "status.set" || taskNames.has(op.task) || issues.push({
|
|
316
|
+
path: [...path, o, "task"],
|
|
317
|
+
message: `status.set targets task "${op.task}" which is not declared in this stage. Known tasks: ${knownList(taskNames)}`
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
function checkInitialStage(def, stageNames, issues) {
|
|
321
|
+
stageNames.has(def.initialStage) || issues.push({
|
|
322
|
+
path: ["initialStage"],
|
|
323
|
+
message: `initialStage "${def.initialStage}" is not a declared stage. Known stages: ${knownList(stageNames)}`
|
|
47
324
|
});
|
|
48
325
|
}
|
|
49
|
-
function checkTransitionTargets(def,
|
|
326
|
+
function checkTransitionTargets(def, stageNames, issues) {
|
|
50
327
|
for (const [i, stage] of def.stages.entries())
|
|
51
328
|
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
52
|
-
|
|
329
|
+
stageNames.has(t.to) || issues.push({
|
|
53
330
|
path: ["stages", i, "transitions", k, "to"],
|
|
54
|
-
message: `transition target "${t.to}" is not a declared stage. Known stages: ${knownList(
|
|
331
|
+
message: `transition target "${t.to}" is not a declared stage. Known stages: ${knownList(stageNames)}`
|
|
55
332
|
});
|
|
56
333
|
}
|
|
57
|
-
function
|
|
58
|
-
const
|
|
59
|
-
for (const [i, p] of (def.predicates ?? []).entries())
|
|
60
|
-
predicateIds.has(p.id) && issues.push({
|
|
61
|
-
path: ["predicates", i, "id"],
|
|
62
|
-
message: `duplicate predicate id "${p.id}"`
|
|
63
|
-
}), predicateIds.add(p.id);
|
|
64
|
-
return predicateIds;
|
|
65
|
-
}
|
|
66
|
-
function checkFilterRefs(def, predicateIds, issues) {
|
|
67
|
-
const visit = (g, path) => {
|
|
68
|
-
g === void 0 || typeof g == "string" || predicateIds.has(g.ref) || issues.push({
|
|
69
|
-
path: [...path, "ref"],
|
|
70
|
-
message: `Filter references predicate "${g.ref}" which is not declared. Known predicates: ${knownList(predicateIds) || "(none)"}`
|
|
71
|
-
});
|
|
72
|
-
};
|
|
334
|
+
function checkEffectNames(def, issues) {
|
|
335
|
+
const sites = [];
|
|
73
336
|
for (const [i, stage] of def.stages.entries()) {
|
|
74
|
-
visit(stage.completion, ["stages", i, "completion"]);
|
|
75
337
|
for (const [j, task] of (stage.tasks ?? []).entries()) {
|
|
76
|
-
|
|
338
|
+
collectEffects(task.effects, ["stages", i, "tasks", j, "effects"], sites);
|
|
77
339
|
for (const [a, action] of (task.actions ?? []).entries())
|
|
78
|
-
|
|
340
|
+
collectEffects(action.effects, ["stages", i, "tasks", j, "actions", a, "effects"], sites);
|
|
79
341
|
}
|
|
80
342
|
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
81
|
-
|
|
343
|
+
collectEffects(t.effects, ["stages", i, "transitions", k, "effects"], sites);
|
|
344
|
+
}
|
|
345
|
+
checkDuplicates(sites, "effect name (registry key \u2014 unique per definition)", issues);
|
|
346
|
+
}
|
|
347
|
+
function collectEffects(effects, path, sites) {
|
|
348
|
+
for (const [e, effect] of (effects ?? []).entries())
|
|
349
|
+
sites.push({ name: effect.name, path: [...path, e, "name"] });
|
|
350
|
+
}
|
|
351
|
+
function checkGuardNames(def, issues) {
|
|
352
|
+
const sites = def.stages.flatMap(
|
|
353
|
+
(stage, i) => (stage.guards ?? []).map((guard, g) => ({
|
|
354
|
+
name: guard.name,
|
|
355
|
+
path: ["stages", i, "guards", g, "name"]
|
|
356
|
+
}))
|
|
357
|
+
);
|
|
358
|
+
checkDuplicates(
|
|
359
|
+
sites,
|
|
360
|
+
"guard name (the guard lake _id derives from it \u2014 unique per definition)",
|
|
361
|
+
issues
|
|
362
|
+
);
|
|
363
|
+
}
|
|
364
|
+
function stateScopes(def) {
|
|
365
|
+
const scopes = [{ entries: def.state, path: ["state"], label: "workflow state" }];
|
|
366
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
367
|
+
scopes.push({
|
|
368
|
+
entries: stage.state,
|
|
369
|
+
path: ["stages", i, "state"],
|
|
370
|
+
label: `stage "${stage.name}" state`
|
|
371
|
+
});
|
|
372
|
+
for (const [j, task] of (stage.tasks ?? []).entries())
|
|
373
|
+
scopes.push({
|
|
374
|
+
entries: task.state,
|
|
375
|
+
path: ["stages", i, "tasks", j, "state"],
|
|
376
|
+
label: `task "${task.name}" state`
|
|
377
|
+
});
|
|
82
378
|
}
|
|
379
|
+
return scopes;
|
|
380
|
+
}
|
|
381
|
+
function checkStateEntryNames(def, issues) {
|
|
382
|
+
for (const { entries, path, label } of stateScopes(def))
|
|
383
|
+
checkDuplicates(
|
|
384
|
+
(entries ?? []).map((entry, n) => ({ name: entry.name, path: [...path, n, "name"] })),
|
|
385
|
+
`state entry name in ${label}`,
|
|
386
|
+
issues
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
function checkPredicates(def, issues) {
|
|
390
|
+
const reserved = new Set(RESERVED_CONDITION_VARS), names = Object.keys(def.predicates ?? {});
|
|
391
|
+
for (const name of names)
|
|
392
|
+
reserved.has(name) && issues.push({
|
|
393
|
+
path: ["predicates", name],
|
|
394
|
+
message: `predicate "${name}" shadows the built-in $${name} \u2014 pick another name`
|
|
395
|
+
});
|
|
396
|
+
for (const [name, groq2] of Object.entries(def.predicates ?? {}))
|
|
397
|
+
checkPredicateBody(name, groq2, names, issues);
|
|
398
|
+
}
|
|
399
|
+
function checkPredicateBody(name, groq2, names, issues) {
|
|
400
|
+
for (const caller of PREDICATE_CALLER_VARS)
|
|
401
|
+
referencesVar(groq2, caller) && issues.push({
|
|
402
|
+
path: ["predicates", name],
|
|
403
|
+
message: `predicate "${name}" reads $${caller} \u2014 predicates are instance-level (pre-evaluated once, without a caller); compose caller gates at the condition site instead`
|
|
404
|
+
});
|
|
405
|
+
for (const other of names)
|
|
406
|
+
other === name || !referencesVar(groq2, other) || issues.push({
|
|
407
|
+
path: ["predicates", name],
|
|
408
|
+
message: `predicate "${name}" references predicate $${other} \u2014 predicates may read built-in vars only (no cross-references; compose at the condition site instead)`
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
function referencesVar(groq2, name) {
|
|
412
|
+
return schema.GROQ_IDENTIFIER.test(name) ? new RegExp(`\\$${name}\\b`).test(groq2) : !1;
|
|
413
|
+
}
|
|
414
|
+
function checkCanOutsideActionFilters(def, issues) {
|
|
415
|
+
for (const site of nonActionFilterConditionSites(def))
|
|
416
|
+
referencesVar(site.groq, "can") && issues.push({
|
|
417
|
+
path: site.path,
|
|
418
|
+
message: `${site.label} reads $can \u2014 $can (the caller's grants) is bound only when an action fires, so it may appear in action filters only`
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
function nonActionFilterConditionSites(def) {
|
|
422
|
+
const sites = [];
|
|
423
|
+
for (const [i, stage] of def.stages.entries()) {
|
|
424
|
+
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
425
|
+
sites.push({
|
|
426
|
+
groq: t.filter,
|
|
427
|
+
path: ["stages", i, "transitions", k, "filter"],
|
|
428
|
+
label: `transition "${t.name}" filter`
|
|
429
|
+
}), collectBindingSites(
|
|
430
|
+
t.effects,
|
|
431
|
+
["stages", i, "transitions", k, "effects"],
|
|
432
|
+
`transition "${t.name}"`,
|
|
433
|
+
sites
|
|
434
|
+
);
|
|
435
|
+
for (const [j, task] of (stage.tasks ?? []).entries())
|
|
436
|
+
collectTaskConditionSites(task, ["stages", i, "tasks", j], sites);
|
|
437
|
+
}
|
|
438
|
+
return sites;
|
|
439
|
+
}
|
|
440
|
+
function collectTaskConditionSites(task, path, sites) {
|
|
441
|
+
for (const field of ["filter", "completeWhen", "failWhen"]) {
|
|
442
|
+
const groq2 = task[field];
|
|
443
|
+
groq2 !== void 0 && sites.push({ groq: groq2, path: [...path, field], label: `task "${task.name}".${field}` });
|
|
444
|
+
}
|
|
445
|
+
collectBindingSites(task.effects, [...path, "effects"], `task "${task.name}"`, sites);
|
|
446
|
+
for (const [a, action] of (task.actions ?? []).entries())
|
|
447
|
+
collectBindingSites(
|
|
448
|
+
action.effects,
|
|
449
|
+
[...path, "actions", a, "effects"],
|
|
450
|
+
`action "${action.name}"`,
|
|
451
|
+
sites
|
|
452
|
+
);
|
|
453
|
+
collectSubworkflowSites(task, path, sites);
|
|
454
|
+
}
|
|
455
|
+
function collectSubworkflowSites(task, path, sites) {
|
|
456
|
+
const sub = task.subworkflows;
|
|
457
|
+
if (sub === void 0) return;
|
|
458
|
+
const subPath = [...path, "subworkflows"];
|
|
459
|
+
sites.push({
|
|
460
|
+
groq: sub.forEach,
|
|
461
|
+
path: [...subPath, "forEach"],
|
|
462
|
+
label: `task "${task.name}".subworkflows.forEach`
|
|
463
|
+
});
|
|
464
|
+
for (const [group, record] of [
|
|
465
|
+
["with", sub.with],
|
|
466
|
+
["context", sub.context]
|
|
467
|
+
])
|
|
468
|
+
for (const [key, groq2] of Object.entries(record ?? {}))
|
|
469
|
+
sites.push({
|
|
470
|
+
groq: groq2,
|
|
471
|
+
path: [...subPath, group, key],
|
|
472
|
+
label: `task "${task.name}".subworkflows.${group} "${key}"`
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
function collectBindingSites(effects, path, label, sites) {
|
|
476
|
+
for (const [e, effect] of (effects ?? []).entries())
|
|
477
|
+
for (const [key, groq2] of Object.entries(effect.bindings ?? {}))
|
|
478
|
+
sites.push({
|
|
479
|
+
groq: groq2,
|
|
480
|
+
path: [...path, e, "bindings", key],
|
|
481
|
+
label: `${label} effect "${effect.name}" binding "${key}"`
|
|
482
|
+
});
|
|
483
|
+
}
|
|
484
|
+
function checkAssigneesEntries(def, issues) {
|
|
485
|
+
for (const { entries, path } of stateScopes(def))
|
|
486
|
+
(entries ?? []).filter((e) => e.type === "assignees").length <= 1 || issues.push({
|
|
487
|
+
path,
|
|
488
|
+
message: "at most one assignees-kind state entry per scope \u2014 the inbox reads it by kind"
|
|
489
|
+
});
|
|
83
490
|
}
|
|
84
491
|
function checkWorkflowInvariants(def) {
|
|
85
|
-
const issues = [],
|
|
86
|
-
checkInitialStage(def,
|
|
87
|
-
const predicateIds = collectPredicateIds(def, issues);
|
|
88
|
-
return checkFilterRefs(def, predicateIds, issues), issues;
|
|
492
|
+
const issues = [], stageNames = checkStages(def, issues);
|
|
493
|
+
return checkInitialStage(def, stageNames, issues), checkTransitionTargets(def, stageNames, issues), checkEffectNames(def, issues), checkGuardNames(def, issues), checkStateEntryNames(def, issues), checkPredicates(def, issues), checkCanOutsideActionFilters(def, issues), checkAssigneesEntries(def, issues), issues;
|
|
89
494
|
}
|
|
90
495
|
function defineWorkflow(definition) {
|
|
91
|
-
const
|
|
496
|
+
const label = labelFor("defineWorkflow", definition), parsed = parseOrThrow(schema.AuthoringWorkflowSchema, definition, label), { definition: stored, issues: desugarIssues } = desugarWorkflow(parsed), issues = [...desugarIssues, ...checkWorkflowInvariants(stored)];
|
|
92
497
|
if (issues.length > 0) throw new Error(schema.formatValidationError(label, issues));
|
|
93
|
-
return
|
|
498
|
+
return stored;
|
|
94
499
|
}
|
|
95
500
|
function defineStage(stage) {
|
|
96
|
-
return parseOrThrow(schema.
|
|
501
|
+
return parseOrThrow(schema.AuthoringStageSchema, stage, labelFor("defineStage", stage));
|
|
97
502
|
}
|
|
98
503
|
function defineTask(task) {
|
|
99
|
-
return parseOrThrow(schema.
|
|
504
|
+
return parseOrThrow(schema.AuthoringTaskSchema, task, labelFor("defineTask", task));
|
|
100
505
|
}
|
|
101
506
|
function defineAction(action) {
|
|
102
|
-
return parseOrThrow(schema.
|
|
507
|
+
return parseOrThrow(schema.AuthoringActionSchema, action, labelFor("defineAction", action));
|
|
103
508
|
}
|
|
104
509
|
function defineTransition(transition) {
|
|
105
|
-
return parseOrThrow(schema.
|
|
510
|
+
return parseOrThrow(schema.AuthoringTransitionSchema, transition, transitionLabel(transition));
|
|
106
511
|
}
|
|
107
|
-
function
|
|
108
|
-
|
|
109
|
-
return typeof id == "string" ? `defineTransition("${id}")` : typeof to == "string" ? `defineTransition(\u2192 "${to}")` : "defineTransition";
|
|
110
|
-
}
|
|
111
|
-
function defineFilter(filter) {
|
|
112
|
-
return parseOrThrow(schema.FilterSchema, filter, "defineFilter");
|
|
512
|
+
function defineState(entry) {
|
|
513
|
+
return parseOrThrow(schema.AuthoringStateEntrySchema, entry, labelFor("defineState", entry));
|
|
113
514
|
}
|
|
114
|
-
function
|
|
115
|
-
return parseOrThrow(schema.
|
|
515
|
+
function defineOp(op) {
|
|
516
|
+
return parseOrThrow(schema.AuthoringOpSchema, op, "defineOp");
|
|
116
517
|
}
|
|
117
|
-
function
|
|
118
|
-
return parseOrThrow(schema.
|
|
518
|
+
function defineGuard(guard) {
|
|
519
|
+
return parseOrThrow(schema.GuardSchema, guard, labelFor("defineGuard", guard));
|
|
119
520
|
}
|
|
120
521
|
function defineEffect(effect) {
|
|
121
|
-
return parseOrThrow(schema.EffectSchema, effect, labelFor("defineEffect", effect
|
|
522
|
+
return parseOrThrow(schema.EffectSchema, effect, labelFor("defineEffect", effect));
|
|
523
|
+
}
|
|
524
|
+
function transitionLabel(transition) {
|
|
525
|
+
const { name, to } = transition;
|
|
526
|
+
return typeof name == "string" ? `defineTransition("${name}")` : typeof to == "string" ? `defineTransition(\u2192 "${to}")` : "defineTransition";
|
|
122
527
|
}
|
|
123
528
|
function defineEffectDescriptor(descriptor) {
|
|
124
529
|
if (!descriptor.name) throw new Error("EffectDescriptor missing name");
|
|
@@ -130,21 +535,22 @@ function parseOrThrow(schema$1, input, label) {
|
|
|
130
535
|
throw new Error(schema.formatValidationError(label, schema.issuesFromValibot(result.issues)));
|
|
131
536
|
return result.output;
|
|
132
537
|
}
|
|
133
|
-
function labelFor(fn, value
|
|
134
|
-
if (value && typeof value == "object" &&
|
|
135
|
-
const raw = value
|
|
538
|
+
function labelFor(fn, value) {
|
|
539
|
+
if (value && typeof value == "object" && "name" in value) {
|
|
540
|
+
const raw = value.name;
|
|
136
541
|
if (typeof raw == "string") return `${fn}("${raw}")`;
|
|
137
542
|
}
|
|
138
543
|
return fn;
|
|
139
544
|
}
|
|
140
545
|
exports.defineAction = defineAction;
|
|
141
|
-
exports.defineAssignee = defineAssignee;
|
|
142
546
|
exports.defineEffect = defineEffect;
|
|
143
547
|
exports.defineEffectDescriptor = defineEffectDescriptor;
|
|
144
|
-
exports.
|
|
145
|
-
exports.
|
|
548
|
+
exports.defineGuard = defineGuard;
|
|
549
|
+
exports.defineOp = defineOp;
|
|
146
550
|
exports.defineStage = defineStage;
|
|
551
|
+
exports.defineState = defineState;
|
|
147
552
|
exports.defineTask = defineTask;
|
|
148
553
|
exports.defineTransition = defineTransition;
|
|
149
554
|
exports.defineWorkflow = defineWorkflow;
|
|
555
|
+
exports.groq = groq;
|
|
150
556
|
//# sourceMappingURL=define.cjs.map
|