API: core (@fragiola/flow)
Commands
Every command the model runs, with its payload and result types and how to run it from code or as JSON. Generated from the registry.
The model runs 18 commands: the core's, and those of the features that bring some (each says which). Each one takes a JSON payload, checked against its JSON Schema, and returns a CommandResult: { ok: true, value }, or { ok: false, error }. Run one with model.run(name, payload) (typed by your registry and your features), or give it as untrusted JSON to model.dispatch({ command, payload }), which validates it first. model.get("commands") returns the same list for the commands of the installed features, with the schemas, ready to become tool definitions for an assistant.
Every command can return invalid_payload (the payload does not match its schema, or names a field of a feature that is not installed, with the path of each problem), vetoed (a middleware refused it), queued (a middleware ran it while another command was running) and middleware_error; a command of a feature that is not installed returns not_found, and an unknown name unknown_command. Each command lists the codes its own rules add: not_found (a node or edge it names is missing) and refused (a rule of the flow refuses the change: a permission flag, a duplicate edge, a parent cycle).
Positions are in flow coordinates (x grows to the right, y grows down); with sub-flows, a node's position is relative to its parent. The payload types below use T, your registry of node and edge types.
Nodes
node.add
Adds a node at the end of the drawing order: its type, position and data, optionally its id (generated when missing), size and flags. Refused when the id is taken.
Payload (FlowNodeInput<T>)
| field | type | required | description |
|---|---|---|---|
id | string | no | its id; generated when missing |
type | string | yes | its type, from the app's registry |
position | object | yes | its top-left corner, in flow coordinates (relative to its parent with sub-flows) |
data | object | yes | the app's data, a JSON object typed by the node or edge type |
width | number | no | its width, when the document gives one |
height | number | no | its height, when the document gives one |
initialWidth | number | no | its width until it is measured; width wins |
initialHeight | number | no | its height until it is measured; height wins |
handles | object[] | no | its handles, declared for its edges to be drawn before it is measured |
sourcePosition | "top" | "right" | "bottom" | "left" | no | the side its edges leave by when no handle gives one; default bottom |
targetPosition | "top" | "right" | "bottom" | "left" | no | the side its edges enter by when no handle gives one; default top |
origin | object | no | the point of its box its position stands at, as shares of its size, over the engine's nodeOrigin |
zIndex | integer | no | its place in the stacking order of its layer (nodes among nodes, edges among edges): a greater one is drawn above; default 0 |
draggable | boolean | no | it may be moved (node.move); default true |
selectable | boolean | no | it may be selected (selection.set); default true |
connectable | boolean | no | edges may start or end at it (edge.add, edge.reconnect); default true |
deletable | boolean | no | it may be removed (node.remove, selection.remove); default true |
hidden | boolean | no | it is not shown; default false |
focusable | boolean | no | it takes DOM focus (with the keyboard feature); default true |
selected | boolean | no | it is selected (requires the selection feature) |
parentId | string | no | the node it lives in, its position relative to it (requires the subflows feature) |
extent | "parent" | object | no | "parent": it stays inside its parent's box; a rectangle (x, y, width, height): it stays inside it, in the coordinates of its position (requires the subflows feature) |
expandParent | boolean | no | its parent grows to keep it inside (requires the subflows feature) |
Result ({ nodeId: string })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
Errors: refused, besides those every command can return.
model.run("node.add", {
id: "start",
type: "task",
position: {
x: 0,
y: 0
},
data: {
title: "Start"
}
});{ "command": "node.add", "payload": {"id":"start","type":"task","position":{"x":0,"y":0},"data":{"title":"Start"}} }node.remove
Removes a node and every edge that starts or ends at it, in one step; with sub-flows, its children go too. Refused when the node (or a child) is not deletable.
Payload ({ nodeId: string })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
Result (RemovedResult)
| field | type | required | description |
|---|---|---|---|
removedNodeIds | string[] | yes | the nodes removed, in drawing order |
removedEdgeIds | string[] | yes | the edges removed: those that touched a removed node, and those removed themselves |
Errors: not_found, refused, besides those every command can return.
model.run("node.remove", {
nodeId: "start"
});{ "command": "node.remove", "payload": {"nodeId":"start"} }node.move
Moves a node to a position (relative to its parent with sub-flows), kept inside its parent with extent: "parent" (a rectangle extent is kept by the gestures, not here). Refused when the node is not draggable. A drag runs it as transient steps, then once more for the drop.
It may run as a step of a continuous gesture: model.run(…, { transient: true }).
Payload ({ nodeId: string; position: XY })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
position | object | yes | its new top-left corner, in flow coordinates (relative to its parent with sub-flows) |
Result ({ nodeId: string; position: XY })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
position | object | yes | the position it got, kept inside its parent with extent: "parent" |
Errors: not_found, refused, besides those every command can return.
model.run("node.move", {
nodeId: "start",
position: {
x: 120,
y: 40
}
});{ "command": "node.move", "payload": {"nodeId":"start","position":{"x":120,"y":40}} }node.resize
Sets a node's width and height, and optionally its position (a resize from a top or left edge moves it), kept inside its parent with extent: "parent" (a rectangle extent is kept by the gestures, not here). A resize gesture runs it as transient steps, then once more at the end.
It may run as a step of a continuous gesture: model.run(…, { transient: true }).
Payload ({ nodeId: string; width: number; height: number; position?: XY; })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
width | number | yes | its new width |
height | number | yes | its new height |
position | object | no | its new top-left corner; none: it stays |
Result ({ nodeId: string; width: number; height: number; position: XY })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
width | number | yes | its width |
height | number | yes | its height |
position | object | yes | the position it got, kept inside its parent with extent: "parent" |
Errors: not_found, besides those every command can return.
model.run("node.resize", {
nodeId: "start",
width: 180,
height: 60
});{ "command": "node.resize", "payload": {"nodeId":"start","width":180,"height":60} }node.set-data
Merges a shallow patch into a node's data: each top-level key given replaces the node's own.
Payload ({ nodeId: string; data: NodeDataPatchOf<T> })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
data | object | yes | the keys to set in the node's data |
Result ({ nodeId: string })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
Errors: not_found, besides those every command can return.
model.run("node.set-data", {
nodeId: "start",
data: {
title: "Begin"
}
});{ "command": "node.set-data", "payload": {"nodeId":"start","data":{"title":"Begin"}} }node.set-flags
Sets some of a node's flags (draggable, selectable, connectable, deletable, hidden, focusable); the others stay. A node made unselectable is deselected.
Payload (NodeFlagsPayload)
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
draggable | boolean | no | it may be moved (node.move); default true |
selectable | boolean | no | it may be selected (selection.set); default true |
connectable | boolean | no | edges may start or end at it (edge.add, edge.reconnect); default true |
deletable | boolean | no | it may be removed (node.remove, selection.remove); default true |
hidden | boolean | no | it is not shown; default false |
focusable | boolean | no | it takes DOM focus (with the keyboard feature); default true |
Result ({ nodeId: string })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
Errors: not_found, besides those every command can return.
model.run("node.set-flags", {
nodeId: "start",
draggable: false
});{ "command": "node.set-flags", "payload": {"nodeId":"start","draggable":false} }node.set-z-index
Sets a node's zIndex, its place in the nodes' stacking order (null: the default, 0), in place: its edges and measurements stay.
Payload ({ nodeId: string; zIndex: number \| null })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
zIndex | integer | null | yes | its place in its layer's stacking order (an integer); null: back to the default, 0 |
Result ({ nodeId: string })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
Errors: not_found, besides those every command can return.
model.run("node.set-z-index", {
nodeId: "start",
zIndex: 2
});{ "command": "node.set-z-index", "payload": {"nodeId":"start","zIndex":2} }node.set-parent
Puts a node into a parent node (parentId), or back on the flow (null), keeping its place on screen: its position becomes relative to the new parent. extent and expandParent set how it stays in its parent. Refused when the parent is the node itself or one of its descendants.
It comes with the subflows feature: install subflows() from @fragiola/flow/subflows (without it, the command is not_found).
Payload ({ nodeId: string; parentId: string \| null; extent?: "parent" \| Rect \| null; expandParent?: boolean; })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
parentId | string | null | yes | the new parent's id, or null for none |
extent | "parent" | object | null | no | "parent": it stays inside its parent; a rectangle: it stays inside it (in the coordinates of its position); null: no extent; none: as it was |
expandParent | boolean | no | its parent grows to keep it inside; none: as it was |
Result ({ nodeId: string; position: XY })
| field | type | required | description |
|---|---|---|---|
nodeId | string | yes | the node's id |
position | object | yes | its position, relative to its new parent |
Errors: not_found, refused, besides those every command can return.
model.run("node.set-parent", {
nodeId: "start",
parentId: "group",
extent: "parent"
});{ "command": "node.set-parent", "payload": {"nodeId":"start","parentId":"group","extent":"parent"} }Edges
edge.add
Adds an edge from a source node to a target node, optionally between two of their handles, with its type, data and flags (its id generated when missing). Refused when a node is not connectable, when an edge already joins the same handles, or when the id is taken.
Payload (FlowEdgeInput<T>)
| field | type | required | description |
|---|---|---|---|
id | string | no | its id; generated when missing |
source | string | yes | the id of the node it starts at |
target | string | yes | the id of the node it ends at |
sourceHandle | string | no | the source node's handle it starts at; none: the node's default source handle |
targetHandle | string | no | the target node's handle it ends at; none: the node's default target handle |
type | string | no | how it is drawn: "bezier" (the default), "simplebezier", "smoothstep", "step", "straight" or an app type |
data | object | no | the app's data, a JSON object typed by the node or edge type |
zIndex | integer | no | its place in the stacking order of its layer (nodes among nodes, edges among edges): a greater one is drawn above; default 0 |
pathOptions | object | no | the shape of its path, over the engine's path options: each built-in type reads its own keys (bezier: curvature; smoothstep: borderRadius, offset, stepPosition, centerX, centerY; step: the same but borderRadius) |
selectable | boolean | no | it may be selected (selection.set); default true |
deletable | boolean | no | it may be removed (edge.remove, selection.remove); default true |
hidden | boolean | no | it is not shown; default false |
reconnectable | boolean | "source" | "target" | no | its ends may be moved to other nodes or handles (edge.reconnect): both (true), none (false), or only its "source" or its "target" end; default true |
focusable | boolean | no | it takes DOM focus (with the keyboard feature); default true |
animated | boolean | no | it is drawn animated (the app's CSS animates its data-animated parts); default false |
selected | boolean | no | it is selected (requires the selection feature) |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, refused, besides those every command can return.
model.run("edge.add", {
source: "start",
target: "end",
type: "smoothstep"
});{ "command": "edge.add", "payload": {"source":"start","target":"end","type":"smoothstep"} }edge.remove
Removes an edge. Refused when it is not deletable.
Payload ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, refused, besides those every command can return.
model.run("edge.remove", {
edgeId: "edge-1"
});{ "command": "edge.remove", "payload": {"edgeId":"edge-1"} }edge.set-data
Merges a shallow patch into an edge's data: each top-level key given replaces the edge's own.
Payload ({ edgeId: string; data: EdgeDataPatchOf<T> })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
data | object | yes | the keys to set in the edge's data |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, besides those every command can return.
model.run("edge.set-data", {
edgeId: "edge-1",
data: {
weight: 2
}
});{ "command": "edge.set-data", "payload": {"edgeId":"edge-1","data":{"weight":2}} }edge.set-flags
Sets some of an edge's flags (selectable, deletable, hidden, reconnectable, focusable, animated); the others stay. An edge made unselectable is deselected.
Payload (EdgeFlagsPayload)
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
selectable | boolean | no | it may be selected (selection.set); default true |
deletable | boolean | no | it may be removed (edge.remove, selection.remove); default true |
hidden | boolean | no | it is not shown; default false |
reconnectable | boolean | "source" | "target" | no | its ends may be moved to other nodes or handles (edge.reconnect): both (true), none (false), or only its "source" or its "target" end; default true |
focusable | boolean | no | it takes DOM focus (with the keyboard feature); default true |
animated | boolean | no | it is drawn animated (the app's CSS animates its data-animated parts); default false |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, besides those every command can return.
model.run("edge.set-flags", {
edgeId: "edge-1",
deletable: false
});{ "command": "edge.set-flags", "payload": {"edgeId":"edge-1","deletable":false} }edge.set-z-index
Sets an edge's zIndex, its place in the edges' drawing order (null: the default, 0), in place.
Payload ({ edgeId: string; zIndex: number \| null })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
zIndex | integer | null | yes | its place in its layer's stacking order (an integer); null: back to the default, 0 |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, besides those every command can return.
model.run("edge.set-z-index", {
edgeId: "edge-1",
zIndex: 1
});{ "command": "edge.set-z-index", "payload": {"edgeId":"edge-1","zIndex":1} }edge.reconnect
Moves an edge's ends: its new source and target nodes and, optionally, handles (a handle left out is none). Its id, type and data stay. Refused when the edge is not reconnectable (or an end it moves is not: reconnectable "source" or "target"), a node is not connectable, or another edge already joins the same handles.
It comes with the reconnection feature: install reconnection() from @fragiola/flow/reconnection (without it, the command is not_found).
Payload ({ edgeId: string } & ConnectionPayload)
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
source | string | yes | the id of the node it starts at |
target | string | yes | the id of the node it ends at |
sourceHandle | string | no | the source node's handle it starts at; none: the node's default source handle |
targetHandle | string | no | the target node's handle it ends at; none: the node's default target handle |
Result ({ edgeId: string })
| field | type | required | description |
|---|---|---|---|
edgeId | string | yes | the edge's id |
Errors: not_found, refused, besides those every command can return.
model.run("edge.reconnect", {
edgeId: "edge-1",
source: "start",
target: "review"
});{ "command": "edge.reconnect", "payload": {"edgeId":"edge-1","source":"start","target":"review"} }Selection
selection.set
Selects nodes and edges: "replace" (the default) makes them the whole selection, "add" adds them, "toggle" flips each one. Refused when one is not selectable.
It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).
It may run as a step of a continuous gesture: model.run(…, { transient: true }).
Payload ({ nodeIds?: string[]; edgeIds?: string[]; mode?: SelectionMode; })
| field | type | required | description |
|---|---|---|---|
nodeIds | string[] | no | the nodes; none: no node |
edgeIds | string[] | no | the edges; none: no edge |
mode | "replace" | "add" | "toggle" | no | how they combine with the selection: "replace" (default), "add" or "toggle" |
Result (SelectionResult)
| field | type | required | description |
|---|---|---|---|
nodeIds | string[] | yes | the nodes, in drawing order |
edgeIds | string[] | yes | the edges, in drawing order |
Errors: not_found, refused, besides those every command can return.
model.run("selection.set", {
nodeIds: [
"start",
"end"
],
mode: "add"
});{ "command": "selection.set", "payload": {"nodeIds":["start","end"],"mode":"add"} }selection.clear
Deselects every node and edge.
It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).
Payload (Record<never, never>)
No fields.
Result (SelectionResult)
| field | type | required | description |
|---|---|---|---|
nodeIds | string[] | yes | the nodes, in drawing order |
edgeIds | string[] | yes | the edges, in drawing order |
Errors: only those every command can return.
model.run("selection.clear", {});{ "command": "selection.clear", "payload": {} }selection.remove
Removes the selected nodes and edges that are deletable, in one step: a node takes its edges with it and, with sub-flows, its children (a node holding one that is not deletable stays).
It comes with the selection feature: install selection() from @fragiola/flow/selection (without it, the command is not_found).
Payload (Record<never, never>)
No fields.
Result (RemovedResult)
| field | type | required | description |
|---|---|---|---|
removedNodeIds | string[] | yes | the nodes removed, in drawing order |
removedEdgeIds | string[] | yes | the edges removed: the selected ones and those that touched a removed node |
Errors: only those every command can return.
model.run("selection.remove", {});{ "command": "selection.remove", "payload": {} }Batches
batch
Runs several commands as one atomic step: in order, all or nothing, with one event. The first failure is the batch's result, its path pointing into commands.
It may run as a step of a continuous gesture: model.run(…, { transient: true }).
Payload ({ commands: BatchEntry<T>[] })
| field | type | required | description |
|---|---|---|---|
commands | object[] | yes | the commands, in order: each a { command, payload } |
Result ({ results: unknown[] })
| field | type | required | description |
|---|---|---|---|
results | JSON[] | yes | each command's value, in order |
Errors: those of the commands it runs (the first failure, at its path in commands); nothing applies unless all succeed.
model.run("batch", {
commands: [
{
command: "node.add",
payload: {
id: "a",
type: "task",
position: {
x: 0,
y: 0
},
data: {}
}
},
{
command: "node.add",
payload: {
id: "b",
type: "task",
position: {
x: 200,
y: 0
},
data: {}
}
},
{
command: "edge.add",
payload: {
source: "a",
target: "b"
}
}
]
});{ "command": "batch", "payload": {"commands":[{"command":"node.add","payload":{"id":"a","type":"task","position":{"x":0,"y":0},"data":{}}},{"command":"node.add","payload":{"id":"b","type":"task","position":{"x":200,"y":0},"data":{}}},{"command":"edge.add","payload":{"source":"a","target":"b"}}]} }