techtools
Головна / proto / flow-old / src / blockHelp.js

blockHelp.js

JavaScript · 257 рядків

1const EXPRESSIONS = `## Expression language
2
3Conditions in CONDITION, EVENT and on connectors, and \`=\` values in ACTION use one language.
4
5- **Scopes:** \`data.*\` (current flow data), \`task.*\` (the routed task; \`task\` alone is the task or \`false\`), \`event.*\` (the event that triggered the pass), \`input.*\` / \`model.*\` (EVENT conditions).
6- **Literals:** \`12\`, \`1.5\`, \`"text"\`, \`'text'\`, \`true\`, \`false\`, \`null\`.
7- **Comparison:** \`==\` / \`!=\` (loose, \`10 == "10"\`), \`===\` / \`!==\` (strict), \`<\`, \`<=\`, \`>\`, \`>=\`.
8- **Logic:** \`&&\`, \`||\`, \`!\`, parentheses.
9- **Arithmetic:** \`+\`, \`-\`, \`*\`, \`/\`, \`%\`, unary \`-\`. \`+\` with a string joins text.
10- **Functions:** \`len(x)\` (array, text or object size), \`coalesce(a, b, …)\` (first non-empty), \`min(…)\`, \`max(…)\`, \`round(x)\`, \`floor(x)\`, \`ceil(x)\`, \`abs(x)\`, \`lower(s)\`, \`upper(s)\`, \`now()\` and \`ts(date)\` (milliseconds).
11
12\`\`\`text
13data.expected - data.accepted > 0
14len(data.lines) == 0 || task == false
15now() - ts(task.createdAt) > 2 * 3600000
16coalesce(data.qty, 1) <= data.left
17\`\`\`
18
19Validation checks every field against the data model: an unknown field or a wrong type (\`"text" * 2\`) is an error. Fields typed \`any\` compare and calculate with anything. \`data.task\` is not the routed task — use \`task\`.
20`
21
22export const BLOCK_HELP = {
23  ACTION: {
24    title: 'Action block',
25    subtitle: 'Write fields into flow data: constants or calculated values.',
26    markdown: `# Action tutorial
27
28ACTION writes fields into the current flow data and passes it on. Configure \`set\` in the block source: every key is a target path, every value is what to write.
29
30\`\`\`json
31{
32  "set": {
33    "status": "checked",
34    "movement.done": true,
35    "left": "=data.expected - data.accepted",
36    "label": "='Pallet ' + upper(data.code)",
37    "late": "=now() - ts(task.createdAt) > 7200000"
38  }
39}
40\`\`\`
41
42- A value that starts with \`=\` is an **expression**; anything else is a constant, exactly as before. \`==text\` stays text.
43- Fields are written top to bottom: a later expression sees the fields written above it.
44- Written fields are known to the following blocks: hover hints, conditions and mappings validate against them.
45
46${EXPRESSIONS}`
47  },
48  DECISION: {
49    title: 'Decision table',
50    subtitle: 'A business rule as a table: first matching row wins.',
51    markdown: `# Decision table tutorial
52
53DECISION keeps a rule that changes often — "where does the item go", "what task does a closed box get" — as a table instead of a chain of CONDITION and ACTION blocks. Engineers change rows, not the graph.
54
55\`\`\`json
56{
57  "resultPath": "dest",
58  "rows": [
59    { "label": "not checked", "when": "data.line.facts.missingData == true", "port": "next", "set": { "destination": "control", "target": "CTRL" } },
60    { "label": "customer reserve", "when": "data.line.facts.reserveOrder != \\"\\"", "port": "next", "set": { "destination": "crossdock", "target": "=data.line.facts.reserveOrder" } },
61    { "label": "otherwise", "when": "", "port": "next", "set": { "destination": "storage", "target": "normal" } }
62  ]
63}
64\`\`\`
65
66- Rows run **top to bottom**; the first true **when** wins. Row order is part of the rule.
67- An empty **when** is "otherwise"; it must be the last row. Without it the block gets a \`none\` output for "no row matched".
68- The winning row writes every column into \`data.<resultPath>\` and its name into \`data.<resultPath>.rule\` — the answer to "why was it decided so".
69- Values are constants or expressions starting with \`=\`, exactly as in ACTION. Every row must give every column the same type.
70- **port** sends rows to different outputs (for example \`putaway\` and \`move\` to create different tasks). Rows with the same port share one output.
71
72${EXPRESSIONS}`
73  },
74  SCREEN: {
75    title: 'TSD screen',
76    subtitle: 'What the scanner shows and what it waits for.',
77    markdown: `# Screen tutorial
78
79SCREEN ends the pass and answers the scanner (TSD) with a screen: text, what to scan or enter next, tone, quantity and buttons. The scanner shows only what the screen says.
80
81\`\`\`json
82{
83  "kind": "put",
84  "text": "{{data.line.name}}: put {{data.pending.qty}} pcs into {{data.box.code}}",
85  "expect": "qty",
86  "tone": "info",
87  "qty": "=data.pending.qty",
88  "buttons": ["Damaged", "Missing"]
89}
90\`\`\`
91
92- **text** is a template with \`{{data.*}}\`, \`{{task.*}}\`, \`{{event.*}}\` fields, or an expression starting with \`=\`. Unknown fields are validation errors.
93- **expect** — what the scanner waits for: \`product\`, \`qty\`, \`container\`, \`pallet\`, \`location\`, \`button\` or \`none\`.
94- **tone** — \`info\`, \`ok\`, \`warn\`, \`error\`.
95- **qty** and **value** (the code expected to be scanned) are optional constants or \`=\` expressions.
96- Response body: \`{ "screen": { "kind", "text", "expect", "tone", "qty"?, "value"?, "buttons"? } }\`.
97
98${EXPRESSIONS}`
99  },
100  CONDITION: {
101    title: 'Condition block',
102    subtitle: 'Route the flow by the first true condition.',
103    markdown: `# Condition tutorial
104
105CONDITION checks its routes **top to bottom** and continues through the port of the first true one.
106
107\`\`\`json
108{
109  "routes": {
110    "done": "data.left == 0",
111    "more": "data.left > 0",
112    "other": "",
113    "Error": "$error"
114  }
115}
116\`\`\`
117
118- An empty condition is the fallback: it runs when no other route is true. Only one is allowed.
119- \`$error\` receives evaluation errors in \`data.error\`; without it an error fails the execution.
120
121## Conditions on connectors
122
123A connector can carry its own condition (select the connector → Condition). From one port the engine follows the first connector whose condition is true, otherwise the connector without a condition. Only one connector per port may have no condition: a second one would never run, so validation reports it.
124
125${EXPRESSIONS}`
126  },
127  MAPPING: {
128    title: 'Mapping block',
129    subtitle: 'Build a new flow-data object from input, task, event, and array items.',
130    markdown: `# Mapping tutorial
131
132The Mapping block transforms the current flow data into a predictable JSON object. It does not call an API and does not wait. Its result becomes the data received by the next block.
133
134## Mental model
135
136Every JSON key is a **target field**. Every value is a **source expression**. String literals are not supported: \`"ready"\` is interpreted as a path named \`ready\`, not as constant text.
137
138## Available scopes
139
140- \`data\` or \`$data\` — the complete current input object.
141- \`data.*\` or \`$data.*\` — a field or subtree from current input.
142- \`task.*\` — the active routed task and its durable workflow context.
143- \`event.*\` — the event that triggered the current execution.
144- \`item.*\` — the current array item; valid only inside \`$map.value\`.
145- \`parent.*\` — in a batch TASK only: the flow data around the array, while \`data.*\` is the current item.
146- \`{ "$value": … }\` — a constant (text, number, boolean, object, array) instead of a path.
147
148\`\`\`json
149{
150  "type": { "$value": "SUPPLY_RECEIVED" },
151  "orderId": "data.orderId",
152  "pending": { "$value": { "sku": "", "qty": 0 } }
153}
154\`\`\`
155
156## Keep the existing input
157
158Use \`$copy\` only at the mapping root. It must equal \`data\`. Other fields overwrite or extend the copied object.
159
160\`\`\`json
161{
162  "$copy": "data",
163  "externalSku": "data.item.sku",
164  "request.quantity": "data.quantity"
165}
166\`\`\`
167
168Without \`$copy\`, the output contains only the explicitly mapped fields.
169
170## Wrap or extract data
171
172\`$data\` means the whole input, while \`$data.item\` copies one subtree.
173
174\`\`\`json
175{
176  "payload": "$data",
177  "item": "$data.item"
178}
179\`\`\`
180
181## Project a list of IDs
182
183\`$map\` must point to an array. Its \`value\` is evaluated once for every element.
184
185\`\`\`json
186{
187  "partIds": {
188    "$map": "data.parts",
189    "value": "item.id"
190  }
191}
192\`\`\`
193
194## Project an array of objects
195
196Inside \`value\`, combine \`item.*\` with root scopes such as \`data.*\`.
197
198\`\`\`json
199{
200  "orderId": "data.orderId",
201  "parts": {
202    "$map": "data.parts",
203    "value": {
204      "id": "item.id",
205      "sku": "item.sku",
206      "shelfId": "item.shelf.id",
207      "orderId": "data.orderId"
208    }
209  }
210}
211\`\`\`
212
213## Use task and event context
214
215\`\`\`json
216{
217  "$copy": "data",
218  "taskId": "task.id",
219  "taskType": "task.type",
220  "scanEvent": "event.event",
221  "entityId": "event.entityId"
222}
223\`\`\`
224
225Task and event fields must exist in that execution. A workflow started without a routed task cannot provide \`task.*\` values.
226
227## Mapping before API Request
228
229Use Mapping when following blocks should receive the transformed structure. API Request uses the same mapping syntax for a POST body, but its request mapping only builds the HTTP payload; the API response is then merged according to API Request behavior.
230
231## Validation and preview
232
233The editor validates JSON, source paths, \`$copy\`, array types, and \`item.*\` paths. **Result structure** previews the derived schema, not live runtime values. Format only reformats valid JSON.
234
235## Common mistakes
236
237- \`"status": "ready"\` tries to read a source path called \`ready\`; constants are not currently supported.
238- \`item.id\` outside \`$map.value\` has no current item.
239- Mapping a non-array with \`$map\` fails validation.
240- Omitting \`$copy\` intentionally drops all unmapped input fields.
241- Values other than \`"data"\` are invalid for \`$copy\`.
242- A source missing from the inherited input model fails validation.
243
244## Quick checklist
245
2461. Decide whether to preserve input with \`"$copy": "data"\`.
2472. Map each output key to an available source path.
2483. Use \`$map\` for arrays and \`item.*\` only inside its value.
2494. Check validation and Result structure before saving the workflow.
250`
251  }
252}
253
254export function blockHelp(type) {
255  return BLOCK_HELP[type] || null
256}
257