@protobase/cli 0.100.23 → 0.100.25

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.
@@ -1,4 +1,4 @@
1
1
  //#region package.json
2
- var version = "0.100.23";
2
+ var version = "0.100.25";
3
3
  //#endregion
4
4
  export { version };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@protobase/cli",
3
- "version": "0.100.23",
3
+ "version": "0.100.25",
4
4
  "description": "The protobase command: scaffold, doctor, dev, build and serve a Protobase project.",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -43,13 +43,13 @@
43
43
  "dependencies": {
44
44
  "@better-auth/core": "^1.7.12",
45
45
  "@hono/node-server": "^2.1.3",
46
- "@protobase/config": "0.100.23",
47
- "@protobase/layout": "0.100.23",
48
- "@protobase/query": "0.100.23",
49
- "@protobase/schema": "0.100.23",
50
- "@protobase/server": "0.100.23",
51
- "@protobase/ui": "0.100.23",
52
- "@protobase/workflows": "0.100.23",
46
+ "@protobase/config": "0.100.25",
47
+ "@protobase/layout": "0.100.25",
48
+ "@protobase/query": "0.100.25",
49
+ "@protobase/schema": "0.100.25",
50
+ "@protobase/server": "0.100.25",
51
+ "@protobase/ui": "0.100.25",
52
+ "@protobase/workflows": "0.100.25",
53
53
  "@react-email/render": "^2.1.0",
54
54
  "@scalar/hono-api-reference": "^0.12.10",
55
55
  "@sentry/vite-plugin": "^11.10.0",
@@ -89,6 +89,7 @@ Reference:
89
89
  - When letting people without an account create records → read [Public forms](references/reference/forms.md)
90
90
  - When running background jobs → read [Jobs](references/reference/jobs.md)
91
91
  - When a value worked out from other records should stay current by itself, recomputed when what it read changes or a time rule flips → read [Derived values](references/reference/derived.md)
92
+ - When writing a workflow for the app, over MCP or the REST API, from its trigger to going live → read [Writing workflows](workflows.md)
92
93
  - When writing or running workflows, their steps and their runs → read [Workflows](references/reference/workflows.md)
93
94
  - When adding trigger or step types to workflows → read [Workflow plugins](references/reference/workflow-plugins.md)
94
95
  - When sending email → read [Email](references/reference/email.md)
@@ -11,7 +11,8 @@ Graphs of steps the server runs along their edges, written in a language a model
11
11
  - [One page per workflow](../reference/workflows.md#the-workflows-page) (beta): A workflow's focus page is its graph, with Add step, Test run, Runs and Publish beside it in a panel, so you build, test and debug without leaving it; in the sidebar, Workflows lists the workflows.
12
12
  - [Pins and wires](../reference/workflows.md#the-workflows-page) (beta): A step's inputs and the values it gives are pins coloured by type; drag an output pin to an input to read that value there, and a wire that does not fit is refused, saying why.
13
13
  - [Break and Make](../reference/workflows.md#the-workflows-page) (beta): Take an object apart into a pin for each field, such as a record's fields, or build one from a pin per field, with no extra step to run.
14
- - [Condition nodes](../reference/workflows.md#the-workflows-page) (beta): In data order, build when a step runs from Equals, Not equals, Greater and Less, And, Or and Not on the graph, or drop a step's status on another step's Only when pin to run it once the first succeeded; any other condition is one Expression node, kept as you wrote it.
14
+ - [Condition nodes](../reference/workflows.md#the-workflows-page) (beta): In data order, build when a step runs from Equals, Not equals, Greater, Less and Matches, All of, Any of and Not on the graph, or drop a step's status on another step's Only when pin to run it once the first succeeded; any other condition is one Expression node, kept as you wrote it.
15
+ - [Select](../reference/workflows.md#the-workflows-page) (beta): Choose an argument by another value on the graph, such as a mail's address by a form's topic, a value per case and one otherwise.
15
16
  - [Move nodes by hand](../reference/workflows.md#the-workflows-page) (beta): Drag a node of the graph where you want it; it stays there, marked, for everyone who edits the workflow, and the rest lays itself out around it until you unpin it or tidy up. Pins and Simple each keep their own places, and moving nodes makes no new version.
16
17
  - [Pins or Simple](../reference/workflows.md#the-workflows-page) (beta): Draw the graph with every pin and wire, or with only the steps and the lines between them, and switch at any time; nothing saved changes.
17
18
  - [Build on the graph](../reference/workflows.md#the-workflows-page) (beta): Click a step's handle to add a step right there, or drag from it to another step to connect them; the wheel and a pinch zoom the graph, and dragging pans it.
@@ -46,7 +47,7 @@ Graphs of steps the server runs along their edges, written in a language a model
46
47
  - [Blocks](../reference/workflows.md#blocks) (beta): build your own step from other steps. A block runs once and is cached by what it reads, so a workflow that reaches it again, in this run or another, reuses its result, such as a file fetched from a link that expires.
47
48
  - [Step cache](../reference/workflow-plugins.md#caching-results) (beta): A plugin's step reuses the result of an identical earlier call from any run or workflow, as its plugin decides, while secrets always come in with their current value; a workflow can force it on, and the graph marks a step forced on against its plugin's advice.
48
49
  - [Guarded requests for plugins](../reference/workflow-plugins.md#sending-requests) (beta): A plugin's requests reach only public addresses, follow no redirect and read at most 10 MiB.
49
- - [Webhook trigger](../reference/workflows.md#appwebhook) (beta): A workflow starts from a signed call to a URL of its own, limited in size and rate.
50
+ - [Webhook trigger](../reference/workflows.md#appwebhook) (beta): A workflow starts from a signed call to a URL of its own, limited in size and rate; one call creates it, publishes it and issues its URL.
50
51
  - [Polling triggers](../reference/workflow-plugins.md#polling-and-webhook) (beta): A workflow starts once for each new item a plugin finds on a schedule, or at each time of a cron with `app:schedule`.
51
52
  - [Incoming Gmail mail](../reference/workflow-plugins.md#incoming-gmail-mail) (beta): A workflow starts once for each new mail on a label of a connected Gmail mailbox that passes optional sender, recipient and subject filters, read with read-only access within about a minute.
52
53
  - [Connected accounts](../reference/workflows.md#connecting-an-account) (beta): People connect their own accounts at other services through OAuth from the account page, see which workflows use each one, and MCP clients list them to bind. A workflow's steps and triggers sign in with one only once its owner turned the workflow on, with tokens encrypted and refreshed. Disconnecting deletes the token and revokes it at a provider that has a revocation endpoint, trying again for a few hours when the provider does not take it, and says which it did.
@@ -110,7 +110,7 @@ In an app with [workflows](workflows.md), the endpoint also offers tools to find
110
110
  | `validate_workflow` | Checks a `state_machine`, with its `trigger` and `execution_order`, or a workflow's `id` with an `edit` of its saved definition, as a save checks it, and saves nothing: `{ valid: true }`, or `{ valid: false, problems }`. A valid one may come with `warnings` in the same shape, which a save goes through with, such as a [Wait after a Block](workflows.md#blocks); `create_workflow` and `update_workflow` return them with what they saved. A record trigger is checked against the resources and fields the person may read, so a resource they cannot read is not a resource of the app. |
111
111
  | `get_workflow_run` | A run's status, input, output, error and cause, with its steps in the order they ran, each with its state, scope, try, status, input, output, assigned variables, logs, error and cause; `state` reads one state's steps, a page of 20 by default. |
112
112
  | `list_connections` | The person's own [connected accounts](workflows.md#connecting-an-account), by each connection the app's plugins declare: its key and label, and each account's `id`, scopes and `needs_sign_in`. A definition's `connections` binds an account by its `id`. It returns no token, and no one else's accounts. |
113
- | `create_workflow` | Creates a workflow from its `name`, `description` and `state_machine`, with its `trigger`, `execution_order` and `layout`, none by default, and returns its `id`, `version` and ETag. A state machine from Step Functions [imports](workflows.md#importing-from-step-functions) its HTTP tasks as `app:http` steps, with `connection_headers` naming each connection's header; `update_workflow` and `validate_workflow` import one the same way. |
113
+ | `create_workflow` | Creates a workflow from its `name`, `description` and `state_machine`, with its `trigger`, `execution_order` and `layout`, none by default, and returns its `id`, `version` and ETag. With `publish: true` it publishes version 1 in the same save and answers `enabled_version`, and for a [webhook trigger](workflows.md#appwebhook) also issues its URL, keeping a `webhook_secret` the sender makes, and answers `webhook: { url, secret }`, shown this once. A state machine from Step Functions [imports](workflows.md#importing-from-step-functions) its HTTP tasks as `app:http` steps, with `connection_headers` naming each connection's header; `update_workflow` and `validate_workflow` import one the same way. |
114
114
  | `update_workflow` | Changes a workflow's definition by an `edit` or whole, as `state_machine`, `trigger` and `execution_order`, and its `name`, `description` or `layout`, written whole, and returns its `id`, new `version` and ETag; a change of the layout alone keeps the version. With the `etag` that `get_workflow` gave, it fails when someone changed the workflow since; an edit without one is saved against the version it was applied to. |
115
115
  | `run_workflow` | Starts a run of a workflow's version saved last, or of the `version` it names, with an `input`, and returns its `run_id` and `status`. With `rerun_of`, a failed run's id, it [re-runs](workflows.md#re-running-a-failed-run) that run from its last checkpoint instead, on its input, and with `rerun_from` any ended run that kept its trace from that state; with `replay: true` too, it [replays](workflows.md#replaying-a-run) any ended run of the workflow on its current version, with that run's input, sending none of its outside writes. |
116
116
  | `enable_workflow` | Publishes the saved `version` it names (`enabled: true`), which puts the workflow's trigger on it, or turns the workflow off, and returns its `enabled_version`. A save never goes live by itself. One that uses a [connected account](workflows.md#connecting-an-account) is turned on only by the account's owner in the admin app: the tool answers `own-sign-in-only` and says so. |
@@ -110,6 +110,7 @@ A workflow's `layout`, a field beside its definition, keeps only where nodes of
110
110
  | `$trigger` | The trigger. |
111
111
  | `Save.record` | In `pins` only: a Break of `record` in what `Save` gives; `Save.#break` one of the whole of it, `$trigger.customer` one of the run's input, and `Save.#status` one of `Save`'s status in data order. |
112
112
  | `Save.#make.data` | In `pins` only: a Make of `Save`'s argument `data`. |
113
+ | `Notify.#select.to` | In `pins` only: a Select of `Notify`'s argument `to`. |
113
114
  | `Tell.#when` | In `pins` only, in data order: the condition node wired to `Tell`'s Only when pin; `Tell.#when.1` the one at its second side, `Tell.#when.1.0` the one at that node's first. |
114
115
 
115
116
  In a state's name or a field, `%`, `.` and a leading `#` or `$` are written `%25`, `%2E`, `%23` and `%24`, so the state `Step.1` is `Step%2E1`.
@@ -186,7 +187,7 @@ A save checks a trigger's input against its type, and refuses a type the app doe
186
187
 
187
188
  ### Turning a trigger on
188
189
 
189
- A trigger starts runs only while its workflow is published: set `enabled_version` to any saved version through the API, in the admin app or with `enable_workflow` over MCP, and clear it to turn the workflow off. The trigger is registered at once on every server process, with no restart, and its status, `active` or `failing` with its last error, is the workflow's `workflow_triggers` record. A save makes a new version and changes nothing that is live, so a change can be run and tested first; publishing it moves `enabled_version`, and publishing an older version goes back to it. A published version whose trigger has the same type and input as the one before keeps the running trigger as it is, and any other change stops it and starts the new one. Runs in progress stay on their version. A trigger's runs run the enabled version and act as the user who saved it, as any run acts as its version's saver.
190
+ A trigger starts runs only while its workflow is published: set `enabled_version` to any saved version through the API, in the admin app or with `enable_workflow` over MCP, and clear it to turn the workflow off. A create may publish too, in the same write: `POST /api/v1/workflows` with `"enabled_version": 1`, `POST /api/workflows` or `create_workflow` with `"publish": true`, which for a [webhook trigger](#appwebhook) also issue its URL. The trigger is registered at once on every server process, with no restart, and its status, `active` or `failing` with its last error, is the workflow's `workflow_triggers` record. A save makes a new version and changes nothing that is live, so a change can be run and tested first; publishing it moves `enabled_version`, and publishing an older version goes back to it. A published version whose trigger has the same type and input as the one before keeps the running trigger as it is, and any other change stops it and starts the new one. Runs in progress stay on their version. A trigger's runs run the enabled version and act as the user who saved it, as any run acts as its version's saver.
190
191
 
191
192
  ### app:record
192
193
 
@@ -215,6 +216,8 @@ The rate limits count per server process, not across the app's processes, and `h
215
216
 
216
217
  Starts one run for each call of the workflow's own URL that proves it comes from the sender. Issue the URL with `POST /api/workflows/<id>/webhook`, or `new_webhook_url` over MCP: it answers `{ url, secret }` once, and the app keeps only a hash of the URL's key, so it cannot show them again. Send `{ "secret": "<secret>" }` to keep a secret the sender makes itself, such as a Standard Webhooks sender's `whsec_` secret. A new URL replaces the old one, which stops at once. Issuing needs the right to change that workflow in your tenant; anyone else gets 404.
217
218
 
219
+ A workflow goes live in one call, created, published and with its URL issued: `POST /api/workflows` takes the fields `POST /api/v1/workflows` takes, with `"publish": true` and an optional `"webhook_secret"`, the sender's own, and answers `201` with the saved workflow, `enabled_version` 1, and `webhook: { url, secret }`, shown this once (`Cache-Control: no-store`). `create_workflow` over MCP does the same with `publish` and `webhook_secret`. Without `publish` it only saves, and refuses `enabled_version` with `publish-required`. A secret for another trigger or one the auth mode cannot check, or a caller who may create workflows but not change them, is refused before anything is saved; a URL the app could not issue once the workflow was saved leaves it published without one, the answer's `webhook_error` saying why, and `POST /api/workflows/<id>/webhook` issues it. A workflow that [signs in through a connected account](#connecting-an-account) is published only by the account's owner, so `publish` refuses it and saves nothing.
220
+
218
221
  The sender POSTs to the URL. `auth.mode` says how a call proves it is theirs:
219
222
 
220
223
  | `auth.mode` | The call carries |
@@ -391,11 +394,12 @@ The bar under the breadcrumb holds what is live, the version shown, *Add step*,
391
394
  A user who may read a workflow but not change it gets the same page, its fields and graph locked, without Save or Publish. On a mobile screen the bar holds what is live, *Runs* and *Test run*, or *Save* with unsaved changes, and the rest behind "..."; a step's form can be changed, and steps are added on a wider screen.
392
395
 
393
396
  - **The graph lays itself out.** The trigger is on the left, each state is a box, a Map is a box around the states it runs for each item, and each edge is drawn from the field that names it: a state's `Next`, a Choice's rules (labelled with their condition) and its `Default` ("Otherwise"), and a Task's or Map's catchers ("On error", dashed). The graph is laid out in layers, from left to right, each time a state, an edge, a pin or a wire is added, removed or renamed; the document keeps no positions, so the graph a person sees is the one a model or the API wrote, and the one they edit is what a model reads back. The canvas zooms with the wheel, a pinch and its buttons, and pans by dragging the empty canvas.
394
- - **Moving nodes by hand.** Dragging a node, a step, the trigger, a Break, a Make or a condition node, puts it where it is let go, as do the arrow keys on the chosen node. It stays there, marked with a pin, and the nodes not moved are laid out around it, also as steps and connections change: one the layout would put under it goes below it, and a Map or Block grows around a node moved inside it. *Pins* and *Simple* each keep their own places, so a node moved in one view stays where the layout puts it in the other. The pin's *Unpin* hands the node back to the layout, and *Tidy up*, above the graph, lays the view shown out again, forgetting every node moved in it and leaving the other view's. Where nodes were moved is the workflow's [layout](#layout), shared with everyone who edits it and saved by *Save*; it makes no new version and needs no publish. An older version opens with the layout it was saved with, which cannot be changed. <gap>A step renamed in the editor cannot keep the place it was moved to because carrying its layout key over to its new name is not implemented.</gap>
397
+ - **Moving nodes by hand.** Dragging a node, a step, the trigger, a Break, a Make or a condition node, puts it where it is let go, as do the arrow keys on the chosen node. Shift-click adds a node to the choice or takes it out, and Shift-drag on the empty canvas chooses every node its box covers; the nodes chosen move together, and Delete removes them all. A step's form opens only on a plain click on it, never while a choice is made with Shift or a box. It stays there, marked with a pin, and the nodes not moved are laid out around it, also as steps and connections change: one the layout would put under it goes below it, and a Map or Block grows around a node moved inside it. *Pins* and *Simple* each keep their own places, so a node moved in one view stays where the layout puts it in the other. The pin's *Unpin* hands the node back to the layout, and *Tidy up*, above the graph, lays the view shown out again, forgetting every node moved in it and leaving the other view's. Where nodes were moved is the workflow's [layout](#layout), shared with everyone who edits it and saved by *Save*; it makes no new version and needs no publish. An older version opens with the layout it was saved with, which cannot be changed. <gap>A step renamed in the editor cannot keep the place it was moved to because carrying its layout key over to its new name is not implemented.</gap>
395
398
  - **Adding a step.** *Add step* opens a searchable picker in the panel: every step type the app can run, its plugins' included, each with what it does, then Choice, For each item, Wait, Set values, Succeed and Fail. Search finds other words too, such as *email* for Send mail. It says where the step lands, and choosing a step on the graph changes that. Clicking or tapping one of a step's handles, which grows into a plus under the pointer, opens the same picker for a step right there: at the bottom handle after the step, at a Choice's *When* as a new rule and at its *Otherwise* as its `Default`, at *On error* on the path of every error, and at the trigger's as the first step. Where the handle led to a step already, the new one goes in between and goes on to that step, such as between the trigger and the first step; a Succeed or Fail there stays unconnected. A new step is named `Step 1`, `Step 2` and so on, and goes after the chosen step, taking over where it went on to, or after the last step; into the Map chosen; or first, with the trigger chosen. After a Choice or a step that ends the run it stays unconnected, and so does a Succeed or Fail that would cut off the steps after it, such as one added between two steps or first in a workflow that has steps.
396
- - **Pins and wires.** Execution runs along the square handles in a box's header, and data along round pins below it, coloured by type: each argument of a Task's step type is a pin on the left, with its type on hover, and each value a step returns, or the trigger gives the run, is a pin on the right, such as an HTTP request's *Status*, *Headers* and *Body* and a record trigger's *Record*. A record, an HTTP body with a `body_schema`, headers and any other object with a title are one pin each. Dragging an output pin to an input pin wires them: the input reads that value by name, the step it comes from gets a variable keeping its result when it has none, and taking the wire away empties the input. A wire is refused over a pin that does not take it, saying why, such as *Whole number does not fit Text*, or that the step does not run before. An input no wire reads shows its value on its row, to type there where it is text, a number, true or false or a choice; an expression shows as ƒ and an object as {…}, both edited in the step's form. Inputs a type does not need and that hold nothing fold away behind *N more inputs*.
397
- - **Break and Make.** Letting go of a wire from an object pin on the empty canvas offers *Break <Type>*, a node with a pin for each field of the object, such as a record's fields; from an object input, *Make <Type>*, which builds it from a pin per field, such as the fields a records step writes. They are the editor's own: they run no step and leave no trace, the inputs that use them read `$fetch.body.ip` and hold `{ "name": "{% $fetch.body.ip %}" }`, and opening a workflow draws a Break for each object a read goes into and a Make for each argument written as an object whose fields read values. One that nothing reads yet goes with a reload.
398
- - **Condition nodes.** In data order, a step's Condition is drawn as the nodes it is made of, wired to its Only when pin: *Equals*, *Not equals*, *Greater* and *Less* compare two values, *And*, *Or* and *Not* join conditions. Each side is wired to a pin or holds a value of its own; one compared with a step's Step status pin is a choice of *Succeeded*, *Failed*, *Timed out*, *Aborted* and *Skipped*, and an And's, an Or's or a Not's is true or false. Dropping a step's Step status pin on another step's Only when pin makes that step run once the first succeeded: `{% $states.context.Steps.Save.Status = 'SUCCEEDED' %}`, an Equals of `Save`'s status and *Succeeded*. Letting go of a wire from an empty Only when pin on the empty canvas offers each node, and *Expression*; from a side of an And, an Or or a Not that holds a value, each node again, there. Any other Condition, such as `{% $count($tickets) > 0 %}`, or one spaced or bracketed otherwise than the nodes write it, is one *Expression* node, its text edited inside it, with a pin for each value it reads. The nodes are the editor's own: they write the Condition as JSONata, and one opened and saved unchanged stays as it was, byte for byte. Removing a node leaves its side what it holds with nothing wired, `''` for a Comparison's, `true` for an And's and `false` for an Or's or a Not's; removing the one at Only when removes the Condition.
399
+ - **Pins and wires.** Execution runs along the square handles in a box's header, and data along round pins below it, coloured by type: each argument of a Task's step type is a pin on the left, with its type on hover, and each value a step returns, or the trigger gives the run, is a pin on the right, such as an HTTP request's *Status*, *Headers* and *Body* and a record trigger's *Record*. A record, an HTTP body with a `body_schema`, headers and any other object with a title are one pin each. Dragging an output pin to an input pin wires them: the input reads that value by name, the step it comes from gets a variable keeping its result when it has none, and taking the wire away empties the input. A wire is refused over a pin that does not take it, saying why, such as *Whole number does not fit Text*, or that the step does not run before. An input no wire reads shows its value on its row, to type there where it is text, a number, true or false or a choice; an expression shows as ƒ and an object as {…}, both edited in the step's form. Inputs a type does not need and that hold nothing fold away behind *N more inputs*. The layout orders the nodes by where their wires meet their pins, so wires cross as little as they can, and in data order puts each node just before what reads it, so a Break, a condition node or a Select stands beside its step.
400
+ - **Break and Make.** Letting go of a wire from an object pin on the empty canvas offers *Break <Type>*, a node with a pin for each field of the object, such as a record's fields; from an object input, *Make <Type>*, which builds it from a pin per field, such as the fields a records step writes. They are the editor's own: they run no step and leave no trace, the inputs that use them read `$fetch.body.ip` and hold `{ "name": "{% $fetch.body.ip %}" }`, and opening a workflow draws a Break for each object a read goes into and a Make for each argument written as an object whose fields read values. One that nothing reads yet goes with a reload. A Break shows only the fields something reads, through a wire or an expression, and *N more fields* opens the rest; one that nothing reads yet shows them all.
401
+ - **Select.** An argument chosen by another value is drawn as a *Select* node between them: its one input is the value it looks at, a row per case gives a value of its own when that value is one of its own, and *Otherwise* gives one for any other, all typed on the node; *Add case* adds one. It is the editor's own: it writes a chain of conditionals, `{% $states.context.Execution.Input.body.topic = 'sales' ? 'sales@example.com' : 'support@example.com' %}`, and one written that way, spaced as the node writes it, opens as the node, as it was saved. Letting go of a wire from an input of a value, empty or holding one of its own, on the empty canvas offers *Select*, whose one case and Otherwise give what the input held. Its position is kept under `Notify.#select.to`. Removing it removes the argument.
402
+ - **Condition nodes.** In data order, a step's Condition is drawn as the nodes it is made of, wired to its Only when pin: *Equals*, *Not equals*, *Greater* and *Less* compare two values, *Matches* tests text against a pattern (`$contains($contact.email, /^[^@\s]+@[^@\s]+$/)`), the pattern written on the node, and *All of*, *Any of* and *Not* join conditions. All of and Any of take any number: each has an empty input after its last, and a wire or a node there adds one more, so `a and b and c` is one All of of three and opens as one; removing one of three or more takes its input away. Each input is wired to a pin or holds a value of its own; one compared with a step's Step status pin is a choice of *Succeeded*, *Failed*, *Timed out*, *Aborted* and *Skipped*, and an All of's, an Any of's or a Not's is true or false. Dropping a step's Step status pin on another step's Only when pin makes that step run once the first succeeded: `{% $states.context.Steps.Save.Status = 'SUCCEEDED' %}`, an Equals of `Save`'s status and *Succeeded*. Letting go of a wire from an empty Only when pin on the empty canvas offers each node, and *Expression*; from an input of an All of, an Any of or a Not that holds a value or none, each node again, there. Any other Condition, such as `{% $count($tickets) > 0 %}`, or one spaced or bracketed otherwise than the nodes write it, is one *Expression* node, its text edited inside it, with a pin for each value it reads. The nodes are the editor's own: they write the Condition as JSONata, and one opened and saved unchanged stays as it was, byte for byte. Removing a node leaves its input what it holds with nothing wired, `''` for a Comparison's or a Matches node's text, `true` for an All of's and `false` for an Any of's or a Not's, or takes the input away from an All of or Any of of three or more; removing the one at Only when removes the Condition.
399
403
  - **Execution order.** "Run the steps from the data" or "in sequence" under "..." switches the draft's order. To data, each `Next` becomes what the state it names runs after, by its data or, where it reads none of it, by a Condition on the earlier state's status, so the states run in the same order; refused with a Choice, an error path to another state or a variable two states assign. To sequence, the states form one chain in an order every dependency keeps; refused with a Condition or a catcher without `Next`. In data order the graph has no execution handles: in Pins the wires order the steps, each step has an Only when pin (its Condition) first and a Step status pin last, and in Simple an arrow joins each pair of steps where one reads the other, dashed where it reads only the other's status; a step with a Condition shows an "only when" badge.
400
404
  - **Pins or Simple.** *Pins* and *Simple* above the graph switch how it is drawn: Simple shows the steps and the lines between them alone, with every input edited in the step's form, where a wire made in Pins shows as the read it wrote. Both draw the same document, so switching changes nothing saved; the browser remembers the choice, Pins by default.
401
405
  - **Connecting steps.** Drag from a step's execution handle to another step; a click or tap without a drag adds a step there instead. The first handle sets `Next`, a Choice's *When* handle adds a rule and its *Otherwise* handle sets `Default`, a Task's or Map's *On error* handle adds a catcher of `States.ALL`, and the trigger's handle sets `StartAt`. A step leads only to steps of its own machine, so an edge into or out of a Map is refused. The chosen step's form sets the same edges with menus: *Goes on to*, each rule's and catcher's *Then goes to*, and *Otherwise goes to*.
@@ -0,0 +1,173 @@
1
+ # Writing workflows
2
+
3
+ How to write a Protobase workflow that saves, runs and goes live, over MCP or the REST API. The full language, every
4
+ state, step type and trigger, is in [the workflows reference](references/reference/workflows.md); MCP's tools are in
5
+ [MCP](references/reference/mcp.md#workflows).
6
+
7
+ ## Before writing
8
+
9
+ 1. Call `list_workflow_types` (REST: `GET /api/workflows/types`). It names every step type and trigger this app has,
10
+ built-in, plugin and custom, with their arguments, results and errors. Use only those; a type it does not list is
11
+ refused on save.
12
+ 2. For a records step, find the resource and its fields first (`list_resources`, `describe_resource`). The save does
13
+ not check a resource's name or its fields: `"resource": "contact_messages"` saves and fails when it runs.
14
+ 3. Records steps act as the user who saved the workflow, with that user's roles now; mail always goes from the app's
15
+ own sender.
16
+
17
+ ## Sequence or data order
18
+
19
+ - `"execution_order": "sequence"` (the default) is the standard States Language: `StartAt`, each state's `Next` or
20
+ `End: true`, a `Choice` to branch.
21
+ - `"execution_order": "data"`: no `StartAt`, no `Next`, no `End`, no `Choice`. A state runs once every state it reads
22
+ from has ended, and states that read nothing of each other run at once. Use it when steps are mostly independent,
23
+ or to branch per step with a `Condition`; the editor draws a Condition as nodes (Equals, Not equals, Greater, Less,
24
+ Matches, All of, Any of, Not), any other expression as one Expression node.
25
+
26
+ Data order's rules, each checked against the save or a run:
27
+
28
+ - A step's result reaches later steps only through a variable it assigns: `"Assign": { "contact": "{% $states.result %}" }`,
29
+ read as `$contact.id`. Reading `$contact` with no state assigning it is refused on save, naming the state and field.
30
+ - `$states.context.Steps.<State>` is that state's `{ Status, Error, Cause }`, `Status` one of `SUCCEEDED`, `FAILED`,
31
+ `TIMED_OUT`, `ABORTED` and `SKIPPED`. It never holds the result: `$states.context.Steps.Save.result` saves, and is
32
+ empty at run time.
33
+ - `Condition` is an expression; when it is false the state is skipped. A state reading one that did not succeed is
34
+ skipped too, unless its Condition reads that state's Status: `{% $states.context.Steps.Save.Status = 'SUCCEEDED' %}`.
35
+ - Each variable is assigned by one state. An uncaught failure fails the run at once.
36
+ - A skipped state is not in the run's trace; read `$states.context.Steps.<State>.Status` or the run's graph instead.
37
+
38
+ ## Where the trigger's input arrives
39
+
40
+ Every state reads the run's input as `$states.context.Execution.Input`:
41
+
42
+ | Trigger | Input |
43
+ | --- | --- |
44
+ | `{ "manual": {} }` | what the start passed, such as `run_workflow`'s `input` |
45
+ | `app:webhook` | `{ method, query, headers, body, received_at }`: a form's fields are `...Input.body.<field>` |
46
+ | `app:record` | `{ resource, key, record }`: `...Input.record.<field>`; the resource must declare events |
47
+ | `app:schedule` | `{ scheduled_at }` |
48
+
49
+ ## From a definition to a live workflow
50
+
51
+ | Step | MCP | REST |
52
+ | --- | --- | --- |
53
+ | Check without saving | `validate_workflow` | (a create that does not fit saves nothing) |
54
+ | Save version 1 | `create_workflow` | `POST /api/v1/workflows` `{ name, description, trigger, execution_order, state_machine }` |
55
+ | Try it | `run_workflow` with an input shaped as the trigger gives it, such as `{ "body": { ... } }` for a webhook | `POST /api/v1/workflow_runs` `{ workflow_id, input }` |
56
+ | Read the run | `get_workflow_run`, again until `succeeded` or `failed` | `GET /api/v1/workflow_runs/<id>`, steps at `GET /api/workflows/runs/<id>/trace` |
57
+ | Fix | `update_workflow` (a JSON Merge Patch in `edit`), then run again | `PATCH /api/v1/workflows/<id>` with `If-Match` |
58
+ | Publish | `enable_workflow` `{ id, enabled: true, version }` | `PATCH /api/v1/workflows/<id>` `{ "enabled_version": 1 }` with `If-Match`, or `"enabled_version": 1` in the create itself |
59
+ | Webhook URL | `new_webhook_url` `{ id, secret? }` | `POST /api/workflows/<id>/webhook` `{ "secret": "..." }` |
60
+ | All at once | `create_workflow` with `"publish": true` (and `webhook_secret`) | `POST /api/workflows` with the create's fields and `"publish": true` (and `webhook_secret`) |
61
+
62
+ Once a definition validates and a test run did what it should, one call makes a workflow live: created, version 1
63
+ published, and for a webhook trigger its URL and secret in the answer, `webhook: { url, secret }`, shown once.
64
+
65
+ A REST `PATCH` needs `If-Match`: the ETag the record was read with, or `*`; without it the answer is 428. A save
66
+ never goes live: a trigger starts runs only of the published version. A refused save answers problems, each
67
+ naming the state, the field and what was expected; fix those and save again. The webhook URL and secret are shown once.
68
+ A test run does what its steps do: it sends the mail and writes the records.
69
+
70
+ ## Examples
71
+
72
+ A website contact form, in data order: the sender becomes a lead contact with the message as a note, sales or support
73
+ is mailed by the topic, and the sender gets an auto-reply when the stored address looks valid. The routing Conditions
74
+ show as Equals and Not equals nodes, the address check as a Matches node.
75
+
76
+ ```json
77
+ {
78
+ "name": "Contact form",
79
+ "trigger": { "app:webhook": { "auth": { "mode": "bearer" }, "per_minute": 30 } },
80
+ "execution_order": "data",
81
+ "state_machine": {
82
+ "QueryLanguage": "JSONata",
83
+ "States": {
84
+ "SaveContact": {
85
+ "Type": "Task",
86
+ "Resource": "app:records.create",
87
+ "Arguments": { "resource": "contacts", "data": { "name": "{% $states.context.Execution.Input.body.name %}", "email": "{% $states.context.Execution.Input.body.email %}", "status": "lead" } },
88
+ "Assign": { "contact": "{% $states.result %}" }
89
+ },
90
+ "SaveMessage": {
91
+ "Type": "Task",
92
+ "Resource": "app:records.create",
93
+ "Arguments": { "resource": "notes", "data": { "contact_id": "{% $contact.id %}", "text": "{% '[' & $states.context.Execution.Input.body.topic & '] ' & $states.context.Execution.Input.body.message %}" } },
94
+ "Assign": { "note": "{% $states.result %}" }
95
+ },
96
+ "NotifySales": {
97
+ "Type": "Task",
98
+ "Resource": "app:mail.send",
99
+ "Condition": "{% $states.context.Execution.Input.body.topic = 'sales' %}",
100
+ "Arguments": { "to": "sales@example.com", "subject": "{% 'New sales enquiry from ' & $contact.name %}", "text": "{% $contact.name & ' <' & $contact.email & '> wrote:\n\n' & $states.context.Execution.Input.body.message & '\n\nNote ' & $note.id %}" }
101
+ },
102
+ "NotifySupport": {
103
+ "Type": "Task",
104
+ "Resource": "app:mail.send",
105
+ "Condition": "{% $states.context.Execution.Input.body.topic != 'sales' %}",
106
+ "Arguments": { "to": "support@example.com", "subject": "{% 'New support request from ' & $contact.name %}", "text": "{% $contact.name & ' <' & $contact.email & '> wrote:\n\n' & $states.context.Execution.Input.body.message & '\n\nNote ' & $note.id %}" }
107
+ },
108
+ "AutoReply": {
109
+ "Type": "Task",
110
+ "Resource": "app:mail.send",
111
+ "Condition": "{% $contains($contact.email, /^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$/) %}",
112
+ "Arguments": { "to": "{% $contact.email %}", "subject": "We received your message", "text": "{% 'Hi ' & $contact.name & ',\n\nThanks for getting in touch; we will answer within one working day.' %}" }
113
+ }
114
+ }
115
+ }
116
+ }
117
+ ```
118
+
119
+ Send it to `POST /api/workflows` with `"publish": true` (or `create_workflow` with `publish: true`) and it is live: the
120
+ answer's `webhook.url` and `webhook.secret` are the form's. The form's server POSTs `{ "name", "email", "topic", "message" }` to it with
121
+ `Authorization: Bearer <secret>` and gets `202 { run_id }`.
122
+
123
+ A welcome mail to each new contact, in sequence, from a record trigger:
124
+
125
+ ```json
126
+ {
127
+ "name": "Welcome new contacts",
128
+ "trigger": { "app:record": { "resource": "contacts" } },
129
+ "execution_order": "sequence",
130
+ "state_machine": {
131
+ "QueryLanguage": "JSONata",
132
+ "StartAt": "HasEmail",
133
+ "States": {
134
+ "HasEmail": { "Type": "Choice", "Choices": [{ "Condition": "{% $type($states.context.Execution.Input.record.email) = 'string' %}", "Next": "Welcome" }], "Default": "NoEmail" },
135
+ "Welcome": {
136
+ "Type": "Task",
137
+ "Resource": "app:mail.send",
138
+ "Arguments": { "to": "{% $states.context.Execution.Input.record.email %}", "subject": "Welcome", "text": "{% 'Hi ' & $states.context.Execution.Input.record.name & ', welcome aboard.' %}" },
139
+ "Retry": [{ "ErrorEquals": ["States.ALL"], "IntervalSeconds": 60, "MaxAttempts": 3 }],
140
+ "End": true
141
+ },
142
+ "NoEmail": { "Type": "Succeed" }
143
+ }
144
+ }
145
+ }
146
+ ```
147
+
148
+ A weekday digest of new leads, in data order, mailed only when there are any:
149
+
150
+ ```json
151
+ {
152
+ "name": "Daily leads",
153
+ "trigger": { "app:schedule": { "cron": "0 8 * * 1-5", "timezone": "Europe/Amsterdam" } },
154
+ "execution_order": "data",
155
+ "state_machine": {
156
+ "QueryLanguage": "JSONata",
157
+ "States": {
158
+ "Leads": {
159
+ "Type": "Task",
160
+ "Resource": "app:records.list",
161
+ "Arguments": { "resource": "contacts", "filter": "status = 'lead'", "order_by": "created_at desc", "limit": 50 },
162
+ "Assign": { "leads": "{% $states.result.records %}" }
163
+ },
164
+ "Digest": {
165
+ "Type": "Task",
166
+ "Resource": "app:mail.send",
167
+ "Condition": "{% $count($leads) > 0 %}",
168
+ "Arguments": { "to": "sales@example.com", "subject": "{% $string($count($leads)) & ' open leads' %}", "text": "{% $join($leads.(name & ' <' & email & '>'), '\n') %}" }
169
+ }
170
+ }
171
+ }
172
+ }
173
+ ```