@sanity/workflow-engine 0.1.0 → 0.3.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 +431 -0
- package/dist/_chunks-cjs/schema.cjs.map +1 -0
- package/dist/_chunks-es/schema.js +416 -0
- package/dist/_chunks-es/schema.js.map +1 -0
- package/dist/define.cjs +57 -435
- package/dist/define.cjs.map +1 -1
- package/dist/define.d.cts +5035 -2647
- package/dist/define.d.ts +5035 -2647
- package/dist/define.js +34 -427
- package/dist/define.js.map +1 -1
- package/dist/index.cjs +1737 -1289
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6141 -3196
- package/dist/index.d.ts +6141 -3196
- package/dist/index.js +1624 -1191
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
package/dist/define.js
CHANGED
|
@@ -1,67 +1,54 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
"create",
|
|
4
|
-
"update",
|
|
5
|
-
"delete",
|
|
6
|
-
"publish",
|
|
7
|
-
"unpublish"
|
|
8
|
-
];
|
|
1
|
+
import * as v from "valibot";
|
|
2
|
+
import { formatValidationError, issuesFromValibot, WorkflowDefinitionSchema, StageSchema, TaskSchema, ActionSchema, TransitionSchema, FilterSchema, AssigneeSchema, PredicateSchema, EffectSchema } from "./_chunks-es/schema.js";
|
|
9
3
|
function knownList(ids) {
|
|
10
4
|
return [...ids].map((s) => `"${s}"`).join(", ");
|
|
11
5
|
}
|
|
12
|
-
function checkStagesAndTasks(def,
|
|
6
|
+
function checkStagesAndTasks(def, issues) {
|
|
13
7
|
const stageIds = /* @__PURE__ */ new Set();
|
|
14
8
|
for (const [i, stage] of def.stages.entries())
|
|
15
|
-
stageIds.has(stage.id) &&
|
|
16
|
-
code: "custom",
|
|
9
|
+
stageIds.has(stage.id) && issues.push({
|
|
17
10
|
path: ["stages", i, "id"],
|
|
18
11
|
message: `duplicate stage id "${stage.id}" (already declared earlier)`
|
|
19
|
-
}), stageIds.add(stage.id), stage.kind === "terminal" && stage.transitions && stage.transitions.length > 0 &&
|
|
20
|
-
code: "custom",
|
|
12
|
+
}), stageIds.add(stage.id), stage.kind === "terminal" && stage.transitions && stage.transitions.length > 0 && issues.push({
|
|
21
13
|
path: ["stages", i, "transitions"],
|
|
22
14
|
message: `terminal stage "${stage.id}" must not declare transitions`
|
|
23
|
-
}), checkDuplicateTaskIds(stage, i,
|
|
15
|
+
}), checkDuplicateTaskIds(stage, i, issues);
|
|
24
16
|
return stageIds;
|
|
25
17
|
}
|
|
26
|
-
function checkDuplicateTaskIds(stage, i,
|
|
18
|
+
function checkDuplicateTaskIds(stage, i, issues) {
|
|
27
19
|
const taskIds = /* @__PURE__ */ new Set();
|
|
28
20
|
for (const [j, task] of (stage.tasks ?? []).entries())
|
|
29
|
-
taskIds.has(task.id) &&
|
|
30
|
-
code: "custom",
|
|
21
|
+
taskIds.has(task.id) && issues.push({
|
|
31
22
|
path: ["stages", i, "tasks", j, "id"],
|
|
32
23
|
message: `duplicate task id "${task.id}" in stage "${stage.id}"`
|
|
33
24
|
}), taskIds.add(task.id);
|
|
34
25
|
}
|
|
35
|
-
function checkInitialStage(def, stageIds,
|
|
36
|
-
stageIds.has(def.initialStageId) ||
|
|
37
|
-
code: "custom",
|
|
26
|
+
function checkInitialStage(def, stageIds, issues) {
|
|
27
|
+
stageIds.has(def.initialStageId) || issues.push({
|
|
38
28
|
path: ["initialStageId"],
|
|
39
29
|
message: `initialStageId "${def.initialStageId}" is not a declared stage. Known stages: ${knownList(stageIds)}`
|
|
40
30
|
});
|
|
41
31
|
}
|
|
42
|
-
function checkTransitionTargets(def, stageIds,
|
|
32
|
+
function checkTransitionTargets(def, stageIds, issues) {
|
|
43
33
|
for (const [i, stage] of def.stages.entries())
|
|
44
34
|
for (const [k, t] of (stage.transitions ?? []).entries())
|
|
45
|
-
stageIds.has(t.to) ||
|
|
46
|
-
code: "custom",
|
|
35
|
+
stageIds.has(t.to) || issues.push({
|
|
47
36
|
path: ["stages", i, "transitions", k, "to"],
|
|
48
37
|
message: `transition target "${t.to}" is not a declared stage. Known stages: ${knownList(stageIds)}`
|
|
49
38
|
});
|
|
50
39
|
}
|
|
51
|
-
function collectPredicateIds(def,
|
|
40
|
+
function collectPredicateIds(def, issues) {
|
|
52
41
|
const predicateIds = /* @__PURE__ */ new Set();
|
|
53
42
|
for (const [i, p] of (def.predicates ?? []).entries())
|
|
54
|
-
predicateIds.has(p.id) &&
|
|
55
|
-
code: "custom",
|
|
43
|
+
predicateIds.has(p.id) && issues.push({
|
|
56
44
|
path: ["predicates", i, "id"],
|
|
57
45
|
message: `duplicate predicate id "${p.id}"`
|
|
58
46
|
}), predicateIds.add(p.id);
|
|
59
47
|
return predicateIds;
|
|
60
48
|
}
|
|
61
|
-
function checkFilterRefs(def, predicateIds,
|
|
49
|
+
function checkFilterRefs(def, predicateIds, issues) {
|
|
62
50
|
const visit = (g, path) => {
|
|
63
|
-
g === void 0 || typeof g == "string" || predicateIds.has(g.ref) ||
|
|
64
|
-
code: "custom",
|
|
51
|
+
g === void 0 || typeof g == "string" || predicateIds.has(g.ref) || issues.push({
|
|
65
52
|
path: [...path, "ref"],
|
|
66
53
|
message: `Filter references predicate "${g.ref}" which is not declared. Known predicates: ${knownList(predicateIds) || "(none)"}`
|
|
67
54
|
});
|
|
@@ -77,396 +64,16 @@ function checkFilterRefs(def, predicateIds, ctx) {
|
|
|
77
64
|
visit(t.filter, ["stages", i, "transitions", k, "filter"]);
|
|
78
65
|
}
|
|
79
66
|
}
|
|
80
|
-
function checkWorkflowInvariants(def
|
|
81
|
-
const stageIds = checkStagesAndTasks(def,
|
|
82
|
-
checkInitialStage(def, stageIds,
|
|
83
|
-
const predicateIds = collectPredicateIds(def,
|
|
84
|
-
checkFilterRefs(def, predicateIds,
|
|
85
|
-
}
|
|
86
|
-
const NonEmpty = z.string().min(1, "must be a non-empty string"), GdrSchema = z.object({
|
|
87
|
-
id: z.string().regex(
|
|
88
|
-
/^(dataset|canvas|media-library|dashboard):/,
|
|
89
|
-
"must be a GDR URI of form '<scheme>:<...id-parts>' where scheme is one of: dataset, canvas, media-library, dashboard"
|
|
90
|
-
),
|
|
91
|
-
type: NonEmpty
|
|
92
|
-
}).strict(), SourceSchema = z.lazy(
|
|
93
|
-
() => z.union([
|
|
94
|
-
z.object({
|
|
95
|
-
source: z.literal("literal"),
|
|
96
|
-
value: z.union([z.string(), z.number(), z.boolean(), z.null()])
|
|
97
|
-
}).strict(),
|
|
98
|
-
z.object({ source: z.literal("param"), paramId: NonEmpty }).strict(),
|
|
99
|
-
z.object({ source: z.literal("actor") }).strict(),
|
|
100
|
-
z.object({ source: z.literal("now") }).strict(),
|
|
101
|
-
z.object({ source: z.literal("self") }).strict(),
|
|
102
|
-
z.object({ source: z.literal("stageId") }).strict(),
|
|
103
|
-
z.object({
|
|
104
|
-
source: z.literal("stateRead"),
|
|
105
|
-
scope: z.enum(STATE_SCOPES),
|
|
106
|
-
slotId: NonEmpty,
|
|
107
|
-
path: z.string().optional()
|
|
108
|
-
}).strict(),
|
|
109
|
-
// effectsContext lookup — reads from `instance.effectsContext[id === ID].value`.
|
|
110
|
-
// Used by chained effects where one handler's output feeds another's binding.
|
|
111
|
-
z.object({
|
|
112
|
-
source: z.literal("effectOutput"),
|
|
113
|
-
contextId: NonEmpty,
|
|
114
|
-
path: z.string().optional()
|
|
115
|
-
}).strict(),
|
|
116
|
-
z.object({ source: z.literal("row"), path: z.string().optional() }).strict(),
|
|
117
|
-
z.object({
|
|
118
|
-
source: z.literal("parentState"),
|
|
119
|
-
slotId: NonEmpty,
|
|
120
|
-
path: z.string().optional()
|
|
121
|
-
}).strict(),
|
|
122
|
-
// Compound object source — resolves each field's Source recursively
|
|
123
|
-
// and packages the result into a JSON object. Lets ops append rich
|
|
124
|
-
// rows to `checklist` / `notes` / `assignees` slots, e.g.
|
|
125
|
-
// `{ source: "object", fields: { signer: { source: "actor" },
|
|
126
|
-
// at: { source: "now" } } }`.
|
|
127
|
-
z.object({
|
|
128
|
-
source: z.literal("object"),
|
|
129
|
-
fields: z.record(NonEmpty, SourceSchema)
|
|
130
|
-
}).strict()
|
|
131
|
-
])
|
|
132
|
-
), ScopeKeySchema = z.object({
|
|
133
|
-
scope: z.enum(STATE_SCOPES),
|
|
134
|
-
slotId: NonEmpty
|
|
135
|
-
}).strict(), OpPredicateSchema = z.lazy(
|
|
136
|
-
() => z.union([
|
|
137
|
-
z.object({
|
|
138
|
-
field: NonEmpty,
|
|
139
|
-
equals: SourceSchema
|
|
140
|
-
}).strict(),
|
|
141
|
-
z.object({ all: z.array(OpPredicateSchema) }).strict(),
|
|
142
|
-
z.object({ any: z.array(OpPredicateSchema) }).strict()
|
|
143
|
-
])
|
|
144
|
-
), OpSchema = z.discriminatedUnion("type", [
|
|
145
|
-
z.object({
|
|
146
|
-
type: z.literal("workflow.op.state.set"),
|
|
147
|
-
target: ScopeKeySchema,
|
|
148
|
-
value: SourceSchema
|
|
149
|
-
}).strict(),
|
|
150
|
-
z.object({
|
|
151
|
-
type: z.literal("workflow.op.state.append"),
|
|
152
|
-
target: ScopeKeySchema,
|
|
153
|
-
item: SourceSchema
|
|
154
|
-
}).strict(),
|
|
155
|
-
z.object({
|
|
156
|
-
type: z.literal("workflow.op.state.updateWhere"),
|
|
157
|
-
target: ScopeKeySchema,
|
|
158
|
-
where: OpPredicateSchema,
|
|
159
|
-
merge: z.record(NonEmpty, SourceSchema)
|
|
160
|
-
}).strict(),
|
|
161
|
-
z.object({
|
|
162
|
-
type: z.literal("workflow.op.state.removeWhere"),
|
|
163
|
-
target: ScopeKeySchema,
|
|
164
|
-
where: OpPredicateSchema
|
|
165
|
-
}).strict(),
|
|
166
|
-
z.object({
|
|
167
|
-
type: z.literal("workflow.op.status.set"),
|
|
168
|
-
taskId: NonEmpty,
|
|
169
|
-
status: z.enum(TASK_STATUSES)
|
|
170
|
-
}).strict()
|
|
171
|
-
]), StateSlotSourceSchema = z.union([
|
|
172
|
-
z.object({ kind: z.literal("init") }).strict(),
|
|
173
|
-
z.object({
|
|
174
|
-
kind: z.literal("query"),
|
|
175
|
-
query: NonEmpty,
|
|
176
|
-
resource: z.union([z.literal("workflow"), z.literal("subject"), GdrSchema]).optional()
|
|
177
|
-
}).strict(),
|
|
178
|
-
z.object({ kind: z.literal("write") }).strict(),
|
|
179
|
-
z.object({ kind: z.literal("computed"), compute: NonEmpty }).strict(),
|
|
180
|
-
/**
|
|
181
|
-
* Pre-populate the slot at resolution time with a constant value.
|
|
182
|
-
* Useful for stage- or task-scope slots that should start non-empty
|
|
183
|
-
* (e.g. a default headline, a starting tally).
|
|
184
|
-
*/
|
|
185
|
-
z.object({ kind: z.literal("literal"), value: z.unknown() }).strict(),
|
|
186
|
-
/**
|
|
187
|
-
* Pre-populate by reading another already-resolved slot. `scope:
|
|
188
|
-
* "workflow"` reads from the workflow-scope `state[]` (resolved at
|
|
189
|
-
* startInstance). `scope: "stage"` reads from an earlier slot in
|
|
190
|
-
* this same stage entry's `state[]` (sequential resolution).
|
|
191
|
-
* Optional `path` selects a nested field (e.g. `path: "id"` on a
|
|
192
|
-
* doc.ref slot).
|
|
193
|
-
*/
|
|
194
|
-
z.object({
|
|
195
|
-
kind: z.literal("stateRead"),
|
|
196
|
-
scope: z.union([z.literal("workflow"), z.literal("stage")]),
|
|
197
|
-
slotId: NonEmpty,
|
|
198
|
-
path: z.string().optional()
|
|
199
|
-
}).strict()
|
|
200
|
-
]), StateSlotBase = {
|
|
201
|
-
id: NonEmpty,
|
|
202
|
-
title: z.string().optional(),
|
|
203
|
-
description: z.string().optional(),
|
|
204
|
-
source: StateSlotSourceSchema
|
|
205
|
-
}, StateSlotKindSchema = z.enum([
|
|
206
|
-
"workflow.state.doc.ref",
|
|
207
|
-
"workflow.state.doc.refs",
|
|
208
|
-
// Release reference. A workflow declares this slot to say "I target a
|
|
209
|
-
// Content Release"; the runtime fills it with a specific release at
|
|
210
|
-
// start time, and the engine auto-derives the instance's read
|
|
211
|
-
// perspective from it.
|
|
212
|
-
"workflow.state.release.ref",
|
|
213
|
-
"workflow.state.query",
|
|
214
|
-
"workflow.state.value.string",
|
|
215
|
-
"workflow.state.value.url",
|
|
216
|
-
"workflow.state.value.number",
|
|
217
|
-
"workflow.state.value.boolean",
|
|
218
|
-
"workflow.state.value.dateTime",
|
|
219
|
-
"workflow.state.value.actor",
|
|
220
|
-
"workflow.state.checklist",
|
|
221
|
-
"workflow.state.notes",
|
|
222
|
-
"workflow.state.assignees"
|
|
223
|
-
]), StateSlotSchema = z.discriminatedUnion(
|
|
224
|
-
"type",
|
|
225
|
-
StateSlotKindSchema.options.map(
|
|
226
|
-
(kind) => z.object({ type: z.literal(kind), ...StateSlotBase }).strict()
|
|
227
|
-
)
|
|
228
|
-
), FilterSchema = z.union([
|
|
229
|
-
// Inline GROQ — only reserved params allowed at runtime.
|
|
230
|
-
NonEmpty,
|
|
231
|
-
// Reference to a named predicate, optionally with named args.
|
|
232
|
-
z.object({
|
|
233
|
-
ref: NonEmpty,
|
|
234
|
-
args: z.record(z.string(), z.unknown()).optional()
|
|
235
|
-
}).strict()
|
|
236
|
-
]), PredicateParamSchema = z.object({
|
|
237
|
-
id: NonEmpty,
|
|
238
|
-
title: z.string().optional(),
|
|
239
|
-
description: z.string().optional(),
|
|
240
|
-
type: z.enum(["string", "number", "boolean", "string[]", "reference"]),
|
|
241
|
-
enum: z.array(z.string()).optional()
|
|
242
|
-
}).strict(), PredicateSchema = z.object({
|
|
243
|
-
type: z.literal("workflow.predicate").optional(),
|
|
244
|
-
id: NonEmpty,
|
|
245
|
-
title: z.string().optional(),
|
|
246
|
-
description: z.string().optional(),
|
|
247
|
-
groq: NonEmpty,
|
|
248
|
-
params: z.array(PredicateParamSchema).optional()
|
|
249
|
-
}).strict(), EffectSchema = z.object({
|
|
250
|
-
type: z.literal("workflow.effect").optional(),
|
|
251
|
-
id: NonEmpty,
|
|
252
|
-
title: z.string().optional(),
|
|
253
|
-
description: z.string().optional(),
|
|
254
|
-
// Bindings resolve typed Source values at commit time, producing
|
|
255
|
-
// concrete JSON in the queued effect's `params`. NO GROQ.
|
|
256
|
-
bindings: z.record(z.string(), SourceSchema).optional(),
|
|
257
|
-
input: z.record(z.string(), z.unknown()).optional()
|
|
258
|
-
}).strict(), TerminalTaskStatus = z.enum(["done", "skipped", "failed"]), PermissionSchema = z.enum(DOCUMENT_VALUE_PERMISSIONS), ActionParamSchema = z.object({
|
|
259
|
-
type: z.literal("workflow.action.param").optional(),
|
|
260
|
-
id: NonEmpty,
|
|
261
|
-
title: z.string().optional(),
|
|
262
|
-
description: z.string().optional(),
|
|
263
|
-
paramType: z.enum([
|
|
264
|
-
"string",
|
|
265
|
-
"number",
|
|
266
|
-
"boolean",
|
|
267
|
-
"url",
|
|
268
|
-
"dateTime",
|
|
269
|
-
"actor",
|
|
270
|
-
"doc.ref",
|
|
271
|
-
"doc.refs",
|
|
272
|
-
"json"
|
|
273
|
-
]),
|
|
274
|
-
required: z.boolean().optional()
|
|
275
|
-
}).strict(), ActionSchema = z.object({
|
|
276
|
-
type: z.literal("workflow.action").optional(),
|
|
277
|
-
id: NonEmpty,
|
|
278
|
-
title: z.string().optional(),
|
|
279
|
-
description: z.string().optional(),
|
|
280
|
-
filter: FilterSchema.optional(),
|
|
281
|
-
/** Caller-supplied params, validated before ops/effects run. */
|
|
282
|
-
params: z.array(ActionParamSchema).optional(),
|
|
283
|
-
/**
|
|
284
|
-
* Status the task flips to when this action fires. Omit to leave
|
|
285
|
-
* the task status unchanged — used by intermediate `claim` /
|
|
286
|
-
* `assign` / partial-update actions where the task should stay
|
|
287
|
-
* active so a later action can fire on it. Declared values must
|
|
288
|
-
* be terminal (`done` / `skipped` / `failed`); the engine never
|
|
289
|
-
* mid-flip back to `active` outside of the `workflow.op.status.set`
|
|
290
|
-
* op explicit path.
|
|
291
|
-
*/
|
|
292
|
-
setStatus: TerminalTaskStatus.optional(),
|
|
293
|
-
/**
|
|
294
|
-
* Inline state-mutation primitives executed during the action commit.
|
|
295
|
-
* Each op runs against the in-memory mutation (deterministic, no
|
|
296
|
-
* external I/O). Resolved Sources turn into concrete JSON before the
|
|
297
|
-
* op applies. Logged on `history[]` as `workflow.history.opApplied`
|
|
298
|
-
* and surfaced on the `ActionResult.ranOps`.
|
|
299
|
-
*/
|
|
300
|
-
ops: z.array(OpSchema).optional(),
|
|
301
|
-
effects: z.array(EffectSchema).optional(),
|
|
302
|
-
/**
|
|
303
|
-
* Role gating — OR-match against `Actor.roles[]`. If omitted, no role
|
|
304
|
-
* gate. `"*"` in Actor.roles always matches (bench / all-access).
|
|
305
|
-
*/
|
|
306
|
-
roles: z.array(NonEmpty).optional(),
|
|
307
|
-
/**
|
|
308
|
-
* When true, the actor must appear in the task's `assignees[]` (as a
|
|
309
|
-
* user-kind assignee matching by id) for the action to be allowed.
|
|
310
|
-
*/
|
|
311
|
-
requireAssignment: z.boolean().optional(),
|
|
312
|
-
/**
|
|
313
|
-
* Which permission on the workflow.instance document the actor must
|
|
314
|
-
* hold (per supplied `grants`). Defaults to "update". Only checked
|
|
315
|
-
* when grants are supplied at evaluate/fireAction time.
|
|
316
|
-
*/
|
|
317
|
-
requiredPermission: PermissionSchema.optional()
|
|
318
|
-
}).strict(), CompletionPolicySchema = z.union([
|
|
319
|
-
z.object({ kind: z.literal("all") }).strict(),
|
|
320
|
-
z.object({ kind: z.literal("any") }).strict(),
|
|
321
|
-
z.object({
|
|
322
|
-
kind: z.literal("count"),
|
|
323
|
-
n: z.number().int().positive()
|
|
324
|
-
}).strict()
|
|
325
|
-
]), LogicalDefinitionRefSchema = z.object({
|
|
326
|
-
workflowId: NonEmpty,
|
|
327
|
-
version: z.union([z.number().int().positive(), z.literal("latest")]).optional()
|
|
328
|
-
}).strict(), SpawnForEachSchema = z.object({
|
|
329
|
-
groq: NonEmpty,
|
|
330
|
-
as: z.object({
|
|
331
|
-
initialState: z.array(
|
|
332
|
-
z.object({
|
|
333
|
-
type: StateSlotKindSchema,
|
|
334
|
-
id: NonEmpty,
|
|
335
|
-
value: SourceSchema
|
|
336
|
-
}).strict()
|
|
337
|
-
).optional(),
|
|
338
|
-
effectsContext: z.record(z.string(), SourceSchema).optional()
|
|
339
|
-
}).strict()
|
|
340
|
-
}).strict(), SpawnSchema = z.object({
|
|
341
|
-
type: z.literal("workflow.spawn").optional(),
|
|
342
|
-
id: z.string().optional(),
|
|
343
|
-
title: z.string().optional(),
|
|
344
|
-
description: z.string().optional(),
|
|
345
|
-
definitionRef: LogicalDefinitionRefSchema,
|
|
346
|
-
forEach: SpawnForEachSchema,
|
|
347
|
-
completionPolicy: CompletionPolicySchema.optional()
|
|
348
|
-
}).strict(), AssigneeSchema = z.discriminatedUnion("kind", [
|
|
349
|
-
z.object({ kind: z.literal("user"), id: NonEmpty }).strict(),
|
|
350
|
-
z.object({ kind: z.literal("role"), role: NonEmpty }).strict()
|
|
351
|
-
]), TaskSchema = z.object({
|
|
352
|
-
type: z.literal("workflow.task").optional(),
|
|
353
|
-
id: NonEmpty,
|
|
354
|
-
title: z.string().optional(),
|
|
355
|
-
description: z.string().optional(),
|
|
356
|
-
assignees: z.array(AssigneeSchema).optional(),
|
|
357
|
-
filter: FilterSchema.optional(),
|
|
358
|
-
invokeOn: z.enum(["stageEnter", "manual"]).optional(),
|
|
359
|
-
/**
|
|
360
|
-
* Auto-completion predicate — same shape as `transition.filter`. When
|
|
361
|
-
* truthy at activation or on any subsequent cascade, the engine
|
|
362
|
-
* flips the task to `done` with a `{ kind: "system", id: "engine.completeWhen" }`
|
|
363
|
-
* actor. A full filter, not just a wall-clock deadline.
|
|
364
|
-
*/
|
|
365
|
-
completeWhen: FilterSchema.optional(),
|
|
366
|
-
/**
|
|
367
|
-
* Auto-failure predicate — symmetric to `completeWhen`. Same shape,
|
|
368
|
-
* same evaluation cadence (activation + every cascade), but flips
|
|
369
|
-
* the task to `failed` (not `done`) with a `{ kind: "system",
|
|
370
|
-
* id: "engine.failWhen" }` actor. When both `completeWhen` and
|
|
371
|
-
* `failWhen` resolve truthy on the same evaluation, `failWhen`
|
|
372
|
-
* wins — failure is the more notable signal and should not be
|
|
373
|
-
* silently swallowed.
|
|
374
|
-
*/
|
|
375
|
-
failWhen: FilterSchema.optional(),
|
|
376
|
-
effects: z.array(EffectSchema).optional(),
|
|
377
|
-
actions: z.array(ActionSchema).optional(),
|
|
378
|
-
spawns: SpawnSchema.optional(),
|
|
379
|
-
contentRefs: z.array(z.unknown()).optional(),
|
|
380
|
-
/** Task-scoped state slots. Resolved at task activation time. */
|
|
381
|
-
state: z.array(StateSlotSchema).optional()
|
|
382
|
-
}).strict(), TransitionSchema = z.object({
|
|
383
|
-
type: z.literal("workflow.transition").optional(),
|
|
384
|
-
id: NonEmpty,
|
|
385
|
-
title: z.string().optional(),
|
|
386
|
-
description: z.string().optional(),
|
|
387
|
-
to: NonEmpty,
|
|
388
|
-
on: z.string().optional(),
|
|
389
|
-
filter: FilterSchema.optional(),
|
|
390
|
-
effects: z.array(EffectSchema).optional()
|
|
391
|
-
}).strict(), GuardActionSchema = z.enum(MUTATION_GUARD_ACTIONS), GuardMatchSchema = z.object({
|
|
392
|
-
/** Subject `_type`(s); empty matches any type. */
|
|
393
|
-
types: z.array(NonEmpty).optional(),
|
|
394
|
-
/** Target docs as `Source`s resolved at deploy to bare ids + the resource. */
|
|
395
|
-
idRefs: z.array(SourceSchema).optional(),
|
|
396
|
-
/** Glob id patterns (bare, resource-local). */
|
|
397
|
-
idPatterns: z.array(NonEmpty).optional(),
|
|
398
|
-
actions: z.array(GuardActionSchema).min(1, "a guard must match at least one action")
|
|
399
|
-
}).strict(), GuardSchema = z.object({
|
|
400
|
-
name: z.string().optional(),
|
|
401
|
-
description: z.string().optional(),
|
|
402
|
-
match: GuardMatchSchema,
|
|
403
|
-
/**
|
|
404
|
-
* Lake GROQ predicate. Reads `document.before`/`document.after`, `mutation`,
|
|
405
|
-
* `guard`, and `identity()`. Bare ids/fields only — no GDRs. Omitted or
|
|
406
|
-
* empty means UNCONDITIONAL DENY.
|
|
407
|
-
*/
|
|
408
|
-
predicate: z.string().optional(),
|
|
409
|
-
/**
|
|
410
|
-
* Projected workflow state the predicate reads as `guard.metadata.*`. Values
|
|
411
|
-
* are `Source`s resolved at deploy and re-synced by the engine. Copied data,
|
|
412
|
-
* never a cross-resource pointer.
|
|
413
|
-
*/
|
|
414
|
-
metadata: z.record(NonEmpty, SourceSchema).optional()
|
|
415
|
-
}).strict(), StageKindSchema = z.enum(["initial", "intermediate", "terminal", "waiting"]), StageSchema = z.object({
|
|
416
|
-
type: z.literal("workflow.stage").optional(),
|
|
417
|
-
id: NonEmpty,
|
|
418
|
-
title: z.string().optional(),
|
|
419
|
-
description: z.string().optional(),
|
|
420
|
-
kind: StageKindSchema.optional(),
|
|
421
|
-
tasks: z.array(TaskSchema).optional(),
|
|
422
|
-
transitions: z.array(TransitionSchema).optional(),
|
|
423
|
-
completion: FilterSchema.optional(),
|
|
424
|
-
onEnter: z.array(EffectSchema).optional(),
|
|
425
|
-
onExit: z.array(EffectSchema).optional(),
|
|
426
|
-
/**
|
|
427
|
-
* Lake mutation guards active while this stage holds. Each compiles to a
|
|
428
|
-
* `temp.system.guard` doc deployed on stage entry and retracted on exit.
|
|
429
|
-
* Plural because one stage's intent may span resources (one guard each).
|
|
430
|
-
*/
|
|
431
|
-
guards: z.array(GuardSchema).optional(),
|
|
432
|
-
contentRefs: z.array(z.unknown()).optional(),
|
|
433
|
-
/** Stage-scoped state slots. Resolved at stage entry and persisted on
|
|
434
|
-
* the StageEntry. */
|
|
435
|
-
state: z.array(StateSlotSchema).optional()
|
|
436
|
-
}).strict(), WorkflowDefinitionShape = z.object({
|
|
437
|
-
workflowId: NonEmpty,
|
|
438
|
-
version: z.number().int().positive(),
|
|
439
|
-
title: NonEmpty,
|
|
440
|
-
description: z.string().optional(),
|
|
441
|
-
initialStageId: NonEmpty,
|
|
442
|
-
/**
|
|
443
|
-
* Workflow-level state slots. Persist for the instance lifetime.
|
|
444
|
-
* Every stage's filters/ops can read them via
|
|
445
|
-
* `*[_id == $self][0].state[key == "<key>"][0].value`. Init-sourced
|
|
446
|
-
* slots are supplied at `startInstance` via `initialState`.
|
|
447
|
-
*/
|
|
448
|
-
state: z.array(StateSlotSchema).optional(),
|
|
449
|
-
stages: z.array(StageSchema).min(1, "must declare at least one stage"),
|
|
450
|
-
predicates: z.array(PredicateSchema).optional()
|
|
451
|
-
}).strict(), WorkflowDefinitionSchema = WorkflowDefinitionShape.superRefine(checkWorkflowInvariants);
|
|
452
|
-
function formatZodError(error, label) {
|
|
453
|
-
const lines = error.issues.map((issue) => ` - ${issue.path.length === 0 ? "(root)" : formatPath(issue.path)}: ${issue.message}`);
|
|
454
|
-
return `${label} failed validation (${error.issues.length} issue${error.issues.length === 1 ? "" : "s"}):
|
|
455
|
-
${lines.join(`
|
|
456
|
-
`)}`;
|
|
457
|
-
}
|
|
458
|
-
function formatPath(path) {
|
|
459
|
-
let out = "";
|
|
460
|
-
for (const seg of path)
|
|
461
|
-
typeof seg == "number" ? out += `[${seg}]` : out += out.length === 0 ? String(seg) : `.${String(seg)}`;
|
|
462
|
-
return out;
|
|
67
|
+
function checkWorkflowInvariants(def) {
|
|
68
|
+
const issues = [], stageIds = checkStagesAndTasks(def, issues);
|
|
69
|
+
checkInitialStage(def, stageIds, issues), checkTransitionTargets(def, stageIds, issues);
|
|
70
|
+
const predicateIds = collectPredicateIds(def, issues);
|
|
71
|
+
return checkFilterRefs(def, predicateIds, issues), issues;
|
|
463
72
|
}
|
|
464
73
|
function defineWorkflow(definition) {
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
typeof definition.workflowId == "string" ? `defineWorkflow("${definition.workflowId}")` : "defineWorkflow"
|
|
469
|
-
);
|
|
74
|
+
const workflowId = definition.workflowId, label = typeof workflowId == "string" ? `defineWorkflow("${workflowId}")` : "defineWorkflow", parsed = parseOrThrow(WorkflowDefinitionSchema, definition, label), issues = checkWorkflowInvariants(parsed);
|
|
75
|
+
if (issues.length > 0) throw new Error(formatValidationError(label, issues));
|
|
76
|
+
return parsed;
|
|
470
77
|
}
|
|
471
78
|
function defineStage(stage) {
|
|
472
79
|
return parseOrThrow(StageSchema, stage, labelFor("defineStage", stage, "id"));
|
|
@@ -478,11 +85,11 @@ function defineAction(action) {
|
|
|
478
85
|
return parseOrThrow(ActionSchema, action, labelFor("defineAction", action, "id"));
|
|
479
86
|
}
|
|
480
87
|
function defineTransition(transition) {
|
|
481
|
-
return parseOrThrow(
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
);
|
|
88
|
+
return parseOrThrow(TransitionSchema, transition, transitionLabel(transition));
|
|
89
|
+
}
|
|
90
|
+
function transitionLabel(transition) {
|
|
91
|
+
const { id, to } = transition;
|
|
92
|
+
return typeof id == "string" ? `defineTransition("${id}")` : typeof to == "string" ? `defineTransition(\u2192 "${to}")` : "defineTransition";
|
|
486
93
|
}
|
|
487
94
|
function defineFilter(filter) {
|
|
488
95
|
return parseOrThrow(FilterSchema, filter, "defineFilter");
|
|
@@ -501,15 +108,15 @@ function defineEffectDescriptor(descriptor) {
|
|
|
501
108
|
return descriptor;
|
|
502
109
|
}
|
|
503
110
|
function parseOrThrow(schema, input, label) {
|
|
504
|
-
const result =
|
|
111
|
+
const result = v.safeParse(schema, input);
|
|
505
112
|
if (!result.success)
|
|
506
|
-
throw new Error(
|
|
507
|
-
return result.
|
|
113
|
+
throw new Error(formatValidationError(label, issuesFromValibot(result.issues)));
|
|
114
|
+
return result.output;
|
|
508
115
|
}
|
|
509
116
|
function labelFor(fn, value, key) {
|
|
510
117
|
if (value && typeof value == "object" && key in value) {
|
|
511
|
-
const
|
|
512
|
-
if (typeof
|
|
118
|
+
const raw = value[key];
|
|
119
|
+
if (typeof raw == "string") return `${fn}("${raw}")`;
|
|
513
120
|
}
|
|
514
121
|
return fn;
|
|
515
122
|
}
|