@frontera-sdk/workflow 1.53.38 → 1.53.39

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/src/definition.ts DELETED
@@ -1,459 +0,0 @@
1
- /**
2
- * The workflow definition: the authored artifact, as types.
3
- *
4
- * This is the shape a script author writes and pushes. It is types only — the
5
- * platform still validates a pushed definition (shape, then lookups: does this
6
- * `resourceId` exist, is this model enabled, does `$steps.risk` name an earlier
7
- * node), and this file deliberately carries none of that. What it buys is the
8
- * compile check and the autocomplete, so a mistake shows up in the editor
9
- * instead of in a `dryRun` round trip.
10
- *
11
- * Every node is three layers: identity (`id`, `kind`, `resourceId?`, `label?`),
12
- * `params` discriminated by `kind` (and, inside `blueprint`, by `mode`), and
13
- * the shared settings every kind accepts.
14
- *
15
- * The node set is closed. All fifteen kinds are describable here, so a
16
- * definition written for a later phase is refused by the engine as "not yet
17
- * runnable", never as "unknown kind".
18
- *
19
- * These types describe the definition as the engine reads it, after the
20
- * platform has applied documented defaults (`trigger.path`, `respond.status`,
21
- * `stop.status` and friends). A field that has a default is written required
22
- * here; the server fills it if the pushed document omits it.
23
- */
24
-
25
- export const WORKFLOW_API_VERSION = 'workflow/v1'
26
-
27
- export const NODE_KINDS = [
28
- 'blueprint',
29
- 'function',
30
- 'llm',
31
- 'decide',
32
- 'action',
33
- 'agent',
34
- 'plugin',
35
- 'http',
36
- 'transform',
37
- 'branch',
38
- 'forEach',
39
- 'parallel',
40
- 'wait',
41
- 'confirm',
42
- 'stop',
43
- 'respond',
44
- ] as const
45
- export type NodeKind = (typeof NODE_KINDS)[number]
46
-
47
- /** A data input inside `params`: a literal, or a JSONata expression evaluated per run. */
48
- export type DataInput = { type: 'static'; value: unknown } | { type: 'expr'; expr: string }
49
-
50
- // ─── Triggers ────────────────────────────────────────────────────────────────
51
-
52
- export const WEBHOOK_METHODS = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD'] as const
53
- export type WebhookMethod = (typeof WEBHOOK_METHODS)[number]
54
-
55
- /** How the webhook caller is authenticated. An open door is `{ type: 'none' }`, never implied. */
56
- export type WebhookAuth =
57
- | { type: 'none' }
58
- | { type: 'header'; name: string; secret: string }
59
- | { type: 'basic'; username: string; secret: string }
60
- | {
61
- type: 'hmac'
62
- header: string
63
- secret: string
64
- algorithm: 'sha256' | 'sha512' | 'sha1'
65
- encoding: 'hex' | 'base64'
66
- /** Stripped before compare, e.g. `sha256=`. */
67
- prefix?: string
68
- }
69
-
70
- /** When and with what the webhook answers its caller. */
71
- export type WebhookRespond =
72
- | {
73
- mode: 'immediately'
74
- status: number
75
- headers?: Record<string, string>
76
- body: 'runId' | 'none'
77
- }
78
- | {
79
- mode: 'onFinish'
80
- /** Bounded 1s..250s. */
81
- timeout: string
82
- status: number
83
- headers?: Record<string, string>
84
- body: 'output' | 'none'
85
- }
86
- /** A `respond` node answers mid-run. */
87
- | { mode: 'node'; timeout: string }
88
-
89
- /**
90
- * The trigger types are type aliases rather than interfaces on purpose: an
91
- * alias gets an implicit index signature, so a stored `Record<string, unknown>`
92
- * row can be narrowed to a trigger without going through `unknown` first. An
93
- * interface would make that a compile error at every read site.
94
- */
95
- export type WebhookTrigger = {
96
- kind: 'webhook'
97
- description?: string
98
- methods: WebhookMethod[]
99
- /** `shipments/:shipmentId`: literal segments and `:name` captures. `''` is the bare workflow URL. */
100
- path: string
101
- auth: WebhookAuth
102
- respond: WebhookRespond
103
- /** JSONata over `$request`, giving the run input object. Absent: the body's declared keys. */
104
- input?: string
105
- /** JSONata over `$request`, giving the subject pk. Required iff the definition has a subject. */
106
- subject?: string
107
- /** JSONata over `$request`, giving the dedupe key. Absent: no dedupe. */
108
- idempotencyKey?: string
109
- /** The whole non-multipart body becomes this `file` input. */
110
- binaryInput?: string
111
- /** `*`, or origins as absolute URLs. */
112
- allowedOrigins?: string[]
113
- /** IPv4/IPv6 addresses or CIDRs. */
114
- ipAllowlist?: string[]
115
- ignoreBots?: boolean
116
- }
117
-
118
- export type ManualTrigger = {
119
- kind: 'manual'
120
- description?: string
121
- }
122
-
123
- export type CronTrigger = {
124
- kind: 'cron'
125
- description?: string
126
- schedule: string
127
- input?: Record<string, unknown>
128
- /** Run once per object the predicate selects. */
129
- subjects?: { where: string; limit?: number }
130
- }
131
-
132
- /** Started by another workflow. */
133
- export type WorkflowCallTrigger = {
134
- kind: 'workflow'
135
- description?: string
136
- }
137
-
138
- /** Started by an agent. */
139
- export type AgentTrigger = {
140
- kind: 'agent'
141
- description?: string
142
- }
143
-
144
- /** Started by something that happened in a connected Plugin account, such as a new email. */
145
- export type PluginTrigger = {
146
- kind: 'plugin'
147
- description?: string
148
- /** The Plugin install, the same reference a `plugin` node uses. */
149
- resourceId: string
150
- /** Event name the install's Plugin kind offers, e.g. `new_email`. */
151
- event: string
152
- /** Whose connection the trigger reads: the shared workspace account, or one named person's own account. */
153
- account: { kind: 'workspace' } | { kind: 'user'; userId: string }
154
- /** Provider-side selection, validated by the event's own schema. Gmail `new_email`: `{ labelId?: string }`. */
155
- config?: Record<string, unknown>
156
- /** JSONata over `$event`. A run starts only when the result is truthy. Absent: every event. */
157
- filter?: string
158
- /** Skip automatic replies and delivery failures. Default true. */
159
- ignoreAutoReplies?: boolean
160
- /** JSONata over `$event`, giving the run input object. Absent: the keys of `$event.data` that are declared inputs. */
161
- input?: string
162
- /** JSONata over `$event`, giving the subject pk. Required iff the definition has a subject. */
163
- subject?: string
164
- /** The declared list-of-files input that receives the event's attachments. Absent: attachments are not downloaded. */
165
- attachmentsInput?: string
166
- /** Most runs this trigger may start in any 24 hours, 1 to 5000. Events over it wait. Default 200. */
167
- maxRunsPerDay?: number
168
- }
169
-
170
- export type WorkflowTrigger =
171
- | ManualTrigger
172
- | CronTrigger
173
- | WorkflowCallTrigger
174
- | AgentTrigger
175
- | WebhookTrigger
176
- | PluginTrigger
177
-
178
- // ─── Shared node settings ────────────────────────────────────────────────────
179
-
180
- export interface RetryPolicy {
181
- attempts: number
182
- backoff?: 'constant' | 'exponential'
183
- /** JSONata predicate: retry only when it holds. */
184
- retryIf?: string
185
- }
186
-
187
- export interface WorkflowNodeBase {
188
- id: string
189
- kind: NodeKind
190
- label?: string
191
- description?: string
192
- resourceId?: string
193
- params: Record<string, unknown>
194
- retry?: RetryPolicy
195
- /** A duration like `30s`, `3m`, `2h`, `1d`. */
196
- timeout?: string
197
- continueOnError?: boolean
198
- cacheTtl?: number
199
- mock?: unknown
200
- skipIf?: string
201
- /**
202
- * Authoring state, not a run decision: `enabled: false` parks the step in
203
- * the editor. Absent means enabled. A parked step is evaluated by nothing and
204
- * leaves no trace at all, unlike `skipIf`, which is evaluated per run and
205
- * recorded as a `skipped` node.
206
- */
207
- enabled?: boolean
208
- /**
209
- * The step's own predecessors, by node id: it runs once every step it names
210
- * has finished. Absent means "after the previous element of my list"; `[]`
211
- * means "after nothing", so the step starts with the list. An entry names a
212
- * sibling — a step in the same container arm, or the same root list.
213
- */
214
- after?: string[]
215
- }
216
-
217
- // ─── Node params by kind ─────────────────────────────────────────────────────
218
-
219
- export interface BlueprintQueryParams {
220
- mode: 'query'
221
- /** Read from this saved object set rather than every object of the type. */
222
- objectSetId?: string
223
- where?: string
224
- select?: string[]
225
- limit?: number
226
- }
227
- export interface BlueprintAggregateParams {
228
- mode: 'aggregate'
229
- /** Read from this saved object set rather than every object of the type. */
230
- objectSetId?: string
231
- where?: string
232
- groupBy: string[]
233
- metrics: Array<{ as: string; fn: 'count' | 'sum' | 'avg' | 'min' | 'max'; property?: string }>
234
- }
235
- export interface FunctionParams {
236
- version?: number
237
- input: DataInput
238
- }
239
- export interface LlmParams {
240
- prompt: DataInput
241
- system?: DataInput
242
- files?: DataInput
243
- structured?: Record<string, unknown>
244
- temperature?: number
245
- maxTokens?: number
246
- }
247
- /** One option of a choice: its name and what it means. */
248
- export interface DecideChoiceOption {
249
- option: string
250
- meaning: string
251
- }
252
- /**
253
- * One question for the decisions model. `choice` picks one option from
254
- * `criteria`: a list of options, or a map of option name to what it means.
255
- * Either way it is stored as the list, in the order written, which is the
256
- * order the model reads them in. A list keeps option names that are numbers
257
- * in the order written too; a map puts them first, in ascending order, so
258
- * write those as the list. `score` places the
259
- * state on an ordered scale (`criteria` lists the levels, lowest first) and
260
- * `noul` answers yes or no as a probability (`criteria` may say what yes and
261
- * no mean).
262
- */
263
- export type DecideQuestion =
264
- | { type: 'choice'; instructions: string; criteria: DecideChoiceOption[] | Record<string, string> }
265
- | { type: 'score'; instructions: string; criteria: string[] }
266
- | { type: 'noul'; instructions: string; criteria?: { true?: string; false?: string } }
267
- export interface DecideParams {
268
- /** The decisions model. Defaults to `typesafe/jev-1.13`. */
269
- model: string
270
- /** The facts the questions are about. */
271
- state: DataInput
272
- /**
273
- * Images the questions are about, as file references (`$input.photo`), at
274
- * most 8, each 20 MB or less. JPEG, PNG, WebP, GIF or BMP up to 64 megapixels; larger images are
275
- * scaled down to 1568 px a side.
276
- */
277
- files?: DataInput
278
- /** Question id to question; each id is a key of the result's `answers`. */
279
- questions: Record<string, DecideQuestion>
280
- }
281
- export interface ActionSubjectParams {
282
- /** The record's primary key: text or a number. */
283
- primaryKey: DataInput
284
- /** The record version the change was decided on: a whole number, 0 or more. */
285
- expectedVersion?: DataInput
286
- }
287
- export interface ActionParams {
288
- input: DataInput
289
- /**
290
- * Why the change is made, recorded on the request, like
291
- * `{ type: 'expr', expr: '"Triage: " & $steps.classify.text' }`. Required
292
- * when the Action's governance requires a reason: push refuses the step
293
- * without one. An empty reason is not sent.
294
- */
295
- reason?: DataInput
296
- /**
297
- * The record the Action acts on, when the step names its own instead of the
298
- * run's subject: typically a row a `blueprint` step found, inside `forEach`.
299
- *
300
- * ```ts
301
- * subject: {
302
- * primaryKey: { type: 'expr', expr: '$item.id' },
303
- * expectedVersion: { type: 'expr', expr: '$item._meta.recordVersion' },
304
- * }
305
- * ```
306
- *
307
- * The record is of the Action's own subject type. `expectedVersion` is the
308
- * version the change was decided on; absent (or evaluating to nothing), the
309
- * record's version is read right before the submit. When `input` leaves them
310
- * out, the Action's primary-key input and its `expectedVersion` input are
311
- * filled from the subject. A create Action takes no subject.
312
- */
313
- subject?: ActionSubjectParams
314
- /**
315
- * Keep the step open until the request settles (succeeded, rejected,
316
- * expired, cancelled, failed, or closed with an unknown outcome) instead of
317
- * returning as soon as it is submitted. Absent or `false`: the step returns
318
- * `{ requestId, lifecycle }` at submit, as it always has.
319
- */
320
- waitForOutcome?: boolean
321
- /**
322
- * How long a `waitForOutcome` step waits, like `30m` or `4h`. Defaults to
323
- * `1h`, at most `24h`. Reaching it is not a failure: the step returns
324
- * `timedOut: true` with the lifecycle it last read.
325
- */
326
- outcomeTimeout?: string
327
- }
328
- export interface AgentParams {
329
- message: DataInput
330
- files?: DataInput
331
- }
332
- export interface PluginParams {
333
- capability: string
334
- input: DataInput
335
- }
336
- export interface HttpParams {
337
- url: string
338
- method: string
339
- headers?: Record<string, DataInput>
340
- body?: DataInput
341
- auth?: { secret: string; header?: string; prefix?: string }
342
- }
343
- export interface TransformParams {
344
- value: DataInput
345
- }
346
- export interface StopParams {
347
- result?: DataInput
348
- status: 'succeeded' | 'failed'
349
- }
350
- export interface BranchParams {
351
- cases: Array<{ when: string; nodes: WorkflowNode[] }>
352
- else?: WorkflowNode[]
353
- }
354
- export interface ForEachParams {
355
- over: string
356
- parallelism?: number
357
- skipFailure?: boolean
358
- mode?: 'auto' | 'inline' | 'child'
359
- nodes: WorkflowNode[]
360
- }
361
- export interface ParallelParams {
362
- branches: Array<{ id: string; nodes: WorkflowNode[] }>
363
- }
364
- /** Wait for a length of time, or until a moment. Exactly one of `for` or `until`. */
365
- export interface WaitTimeParams {
366
- mode?: 'time'
367
- for?: string
368
- until?: string
369
- }
370
- /**
371
- * Wait until one Blueprint record meets a condition, re-reading it until the
372
- * condition holds or `timeout` passes. Reaching the timeout is not a failure:
373
- * the step returns `{ matched: false, timedOut: true, record }`.
374
- */
375
- export interface WaitRecordParams {
376
- mode: 'record'
377
- /** The object type to read, by id. */
378
- objectTypeId: string
379
- /** The record's primary key. */
380
- pk: DataInput
381
- /** JSONata predicate evaluated with the record as `$`, e.g. `jobState in ['accepted', 'declined']`. */
382
- where: string
383
- /** How long to wait, like `30m` or `4h`. Defaults to `1h`, at most `7d`. */
384
- timeout?: string
385
- /** How often to read the record. Defaults to `30s`; widened when the timeout would need too many reads. */
386
- pollEvery?: string
387
- /**
388
- * Never set on a record wait. Declared so code that reads `params.for` or
389
- * `params.until` off a `WaitParams` keeps compiling; `mode` still tells the
390
- * two apart.
391
- */
392
- for?: never
393
- until?: never
394
- }
395
- export type WaitParams = WaitTimeParams | WaitRecordParams
396
- /**
397
- * Ask the person in the conversation that started the run (through an Agent)
398
- * to confirm before the next steps act. They answer with the conversation's
399
- * own buttons; the run waits for the answer. The step returns
400
- * `{ answer: 'confirmed' | 'cancelled' | 'expired' | 'unavailable' }` and
401
- * never fails on a no: branch on `$steps.<id>.answer`. `unavailable` when no
402
- * Agent started the run from a conversation.
403
- */
404
- export interface ConfirmParams {
405
- /** What will be done, in one sentence the person can say yes to. */
406
- summary: DataInput
407
- /** Anything else they should see first, as plain text. */
408
- details?: DataInput
409
- /** The confirm button's label; "Confirm" by default. */
410
- confirmLabel?: string
411
- /** The cancel button's label; "Cancel" by default. */
412
- cancelLabel?: string
413
- /** How long the question stays open, in minutes (1 to 1440); 60 by default. */
414
- timeoutMinutes?: number
415
- /**
416
- * What happens when nobody can answer (a scheduled, manual or webhook run has no person):
417
- * `fail` fails the step with `CONFIRM_UNAVAILABLE`; `continue` answers `unavailable` for
418
- * the definition to branch on. Unset, it fails unless the definition reads the step's answer.
419
- */
420
- onUnavailable?: 'fail' | 'continue'
421
- }
422
- export interface RespondParams {
423
- status: number
424
- headers?: Record<string, DataInput>
425
- body?: DataInput
426
- }
427
-
428
- type NodeOf<K extends NodeKind, P> = Omit<WorkflowNodeBase, 'kind' | 'params'> & { kind: K; params: P }
429
-
430
- export type WorkflowNode =
431
- | NodeOf<'blueprint', BlueprintQueryParams | BlueprintAggregateParams>
432
- | NodeOf<'function', FunctionParams>
433
- | NodeOf<'llm', LlmParams>
434
- | NodeOf<'decide', DecideParams>
435
- | NodeOf<'action', ActionParams>
436
- | NodeOf<'agent', AgentParams>
437
- | NodeOf<'plugin', PluginParams>
438
- | NodeOf<'http', HttpParams>
439
- | NodeOf<'transform', TransformParams>
440
- | NodeOf<'stop', StopParams>
441
- | NodeOf<'branch', BranchParams>
442
- | NodeOf<'forEach', ForEachParams>
443
- | NodeOf<'parallel', ParallelParams>
444
- | NodeOf<'wait', WaitParams>
445
- | NodeOf<'confirm', ConfirmParams>
446
- | NodeOf<'respond', RespondParams>
447
-
448
- export interface WorkflowDefinition {
449
- apiVersion: typeof WORKFLOW_API_VERSION
450
- /** A slug: lowercase letters, digits, dashes. */
451
- name: string
452
- description?: string
453
- subject?: { objectTypeId: string; label?: string }
454
- /** The same flat field map Functions declare. */
455
- input?: Record<string, Record<string, unknown>>
456
- trigger: WorkflowTrigger[]
457
- nodes: WorkflowNode[]
458
- output?: DataInput
459
- }