@synergenius/flow-weaver 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/annotation-generator.js +36 -0
- package/dist/api/generate-in-place.js +39 -0
- package/dist/api/generate.js +11 -1
- package/dist/api/manipulation/nodes.js +22 -0
- package/dist/ast/types.d.ts +27 -1
- package/dist/built-in-nodes/index.d.ts +1 -0
- package/dist/built-in-nodes/index.js +1 -0
- package/dist/built-in-nodes/invoke-workflow.js +12 -1
- package/dist/built-in-nodes/mock-types.d.ts +2 -0
- package/dist/built-in-nodes/wait-for-agent.d.ts +13 -0
- package/dist/built-in-nodes/wait-for-agent.js +26 -0
- package/dist/chevrotain-parser/fan-parser.d.ts +38 -0
- package/dist/chevrotain-parser/fan-parser.js +149 -0
- package/dist/chevrotain-parser/grammar-diagrams.d.ts +1 -0
- package/dist/chevrotain-parser/grammar-diagrams.js +3 -0
- package/dist/chevrotain-parser/index.d.ts +3 -1
- package/dist/chevrotain-parser/index.js +3 -1
- package/dist/chevrotain-parser/tokens.d.ts +2 -0
- package/dist/chevrotain-parser/tokens.js +10 -0
- package/dist/cli/commands/diagram.d.ts +2 -1
- package/dist/cli/commands/diagram.js +9 -6
- package/dist/cli/commands/run.js +59 -1
- package/dist/cli/flow-weaver.mjs +1396 -77
- package/dist/cli/index.js +23 -36
- package/dist/diagram/geometry.js +47 -5
- package/dist/diagram/html-viewer.d.ts +12 -0
- package/dist/diagram/html-viewer.js +399 -0
- package/dist/diagram/index.d.ts +12 -0
- package/dist/diagram/index.js +22 -0
- package/dist/diagram/types.d.ts +1 -0
- package/dist/doc-metadata/extractors/annotations.js +282 -1
- package/dist/doc-metadata/types.d.ts +6 -0
- package/dist/generator/control-flow.d.ts +13 -0
- package/dist/generator/control-flow.js +74 -0
- package/dist/generator/inngest.js +23 -0
- package/dist/generator/unified.js +122 -2
- package/dist/jsdoc-parser.d.ts +24 -0
- package/dist/jsdoc-parser.js +41 -1
- package/dist/mcp/agent-channel.d.ts +35 -0
- package/dist/mcp/agent-channel.js +61 -0
- package/dist/mcp/run-registry.d.ts +29 -0
- package/dist/mcp/run-registry.js +24 -0
- package/dist/mcp/tools-diagram.d.ts +1 -1
- package/dist/mcp/tools-diagram.js +15 -7
- package/dist/mcp/tools-editor.js +75 -3
- package/dist/mcp/workflow-executor.d.ts +28 -0
- package/dist/mcp/workflow-executor.js +62 -1
- package/dist/parser.d.ts +8 -0
- package/dist/parser.js +100 -0
- package/dist/runtime/ExecutionContext.d.ts +2 -0
- package/dist/runtime/ExecutionContext.js +2 -0
- package/dist/runtime/events.d.ts +1 -1
- package/dist/sugar-optimizer.js +28 -3
- package/dist/validator.d.ts +8 -0
- package/dist/validator.js +92 -0
- package/docs/reference/advanced-annotations.md +431 -0
- package/docs/reference/built-in-nodes.md +225 -0
- package/docs/reference/cli-reference.md +882 -0
- package/docs/reference/compilation.md +351 -0
- package/docs/reference/concepts.md +400 -0
- package/docs/reference/debugging.md +255 -0
- package/docs/reference/deployment.md +207 -0
- package/docs/reference/error-codes.md +686 -0
- package/docs/reference/export-interface.md +229 -0
- package/docs/reference/iterative-development.md +186 -0
- package/docs/reference/jsdoc-grammar.md +471 -0
- package/docs/reference/marketplace.md +205 -0
- package/docs/reference/node-conversion.md +308 -0
- package/docs/reference/patterns.md +161 -0
- package/docs/reference/scaffold.md +160 -0
- package/docs/reference/tutorial.md +519 -0
- package/package.json +1 -1
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: Compilation
|
|
3
|
+
description: How compilation works, TypeScript and Inngest targets, compile options, and serve handler generation
|
|
4
|
+
keywords: [compile, compilation, target, typescript, inngest, production, source-map, format, strict, inline-runtime, clean, serve, framework, step.run, durable, trigger, cancelOn, retries, timeout, throttle, cron, markers]
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
# Compilation
|
|
8
|
+
|
|
9
|
+
Flow Weaver compiles annotated TypeScript into executable workflow code. The compiler parses JSDoc annotations, validates the workflow graph, and generates runtime code — all while preserving your existing code.
|
|
10
|
+
|
|
11
|
+
## How Compilation Works
|
|
12
|
+
|
|
13
|
+
Compilation is **in-place** by default. The compiler inserts generated code into marker sections within your source file:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
// Your node types and annotations above — untouched
|
|
17
|
+
|
|
18
|
+
// @flow-weaver-runtime — start
|
|
19
|
+
// Generated imports and runtime setup
|
|
20
|
+
// @flow-weaver-runtime — end
|
|
21
|
+
|
|
22
|
+
export function myWorkflow(params: { data: string }) {
|
|
23
|
+
// @flow-weaver-body — start
|
|
24
|
+
// Generated execution logic
|
|
25
|
+
// @flow-weaver-body — end
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Key guarantee:** Code outside the marker sections is never modified. Your node type functions, imports, and other code are preserved exactly as written.
|
|
30
|
+
|
|
31
|
+
### Compilation Steps
|
|
32
|
+
|
|
33
|
+
1. **Parse** — Extract annotations using Chevrotain grammar parser
|
|
34
|
+
2. **Validate** — Check graph structure, types, connections, and constraints
|
|
35
|
+
3. **Generate** — Produce executable code based on the target
|
|
36
|
+
4. **Write** — Insert generated code into marker sections (or write to output file)
|
|
37
|
+
|
|
38
|
+
---
|
|
39
|
+
|
|
40
|
+
## TypeScript Target (Default)
|
|
41
|
+
|
|
42
|
+
The default `typescript` target generates code that runs directly in Node.js or any JavaScript runtime.
|
|
43
|
+
|
|
44
|
+
### Generated Code Structure
|
|
45
|
+
|
|
46
|
+
- **ExecutionContext** — Runtime context for variable storage, abort signals, and debug events
|
|
47
|
+
- **FunctionRegistry** — Registry of 25+ built-in functions (branching, iteration, error handling)
|
|
48
|
+
- **Debug instrumentation** — `STATUS_CHANGED`, `VARIABLE_SET` events via WebSocket (omitted in production mode)
|
|
49
|
+
- **Scope functions** — Async functions for forEach/iteration patterns
|
|
50
|
+
- **Abort signal support** — Cancellation propagation through the execution graph
|
|
51
|
+
- **Recursion depth protection** — Prevents infinite loops in cyclic workflows
|
|
52
|
+
|
|
53
|
+
### Example
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
flow-weaver compile workflow.ts
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Generates code like:
|
|
60
|
+
```typescript
|
|
61
|
+
// @flow-weaver-runtime — start
|
|
62
|
+
import { ExecutionContext } from '@synergenius/flow-weaver/runtime';
|
|
63
|
+
// @flow-weaver-runtime — end
|
|
64
|
+
|
|
65
|
+
export function myWorkflow(params: { data: string }) {
|
|
66
|
+
// @flow-weaver-body — start
|
|
67
|
+
const ctx = new ExecutionContext();
|
|
68
|
+
const validate_result = validateRecord(true, params.data);
|
|
69
|
+
// ... execution chain
|
|
70
|
+
return { onSuccess: true, onFailure: false, result: score_result.score };
|
|
71
|
+
// @flow-weaver-body — end
|
|
72
|
+
}
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Inngest Target
|
|
78
|
+
|
|
79
|
+
The `inngest` target generates **durable functions** using [Inngest](https://www.inngest.com/). Each workflow node becomes a `step.run()` call, providing automatic retries, event-driven triggers, and crash recovery.
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
flow-weaver compile workflow.ts --target inngest
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
### Per-Node Durability
|
|
86
|
+
|
|
87
|
+
Each node in the workflow becomes an individually durable step:
|
|
88
|
+
|
|
89
|
+
```typescript
|
|
90
|
+
const validate_result = await step.run('validate', async () => {
|
|
91
|
+
return validateRecord(true, params.data);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
const enrich_result = await step.run('enrich', async () => {
|
|
95
|
+
return enrichRecord(true, validate_result.data);
|
|
96
|
+
});
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
If the process crashes after `validate` completes, Inngest replays the function and skips already-completed steps.
|
|
100
|
+
|
|
101
|
+
### Parallel Execution
|
|
102
|
+
|
|
103
|
+
Independent nodes execute in parallel using `Promise.all`:
|
|
104
|
+
|
|
105
|
+
```typescript
|
|
106
|
+
const [branch_a, branch_b] = await Promise.all([
|
|
107
|
+
step.run('branchA', async () => processA(true, data)),
|
|
108
|
+
step.run('branchB', async () => processB(true, data)),
|
|
109
|
+
]);
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Iteration
|
|
113
|
+
|
|
114
|
+
ForEach patterns use indexed `step.run()` for per-element durability:
|
|
115
|
+
|
|
116
|
+
```typescript
|
|
117
|
+
const results = [];
|
|
118
|
+
for (let i = 0; i < items.length; i++) {
|
|
119
|
+
results.push(await step.run(`process-${i}`, async () => processItem(true, items[i])));
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
---
|
|
124
|
+
|
|
125
|
+
## Inngest Annotations
|
|
126
|
+
|
|
127
|
+
These annotations configure Inngest-specific behavior. They go inside `@flowWeaver workflow` blocks.
|
|
128
|
+
|
|
129
|
+
### `@trigger`
|
|
130
|
+
|
|
131
|
+
Define what triggers the workflow:
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
/**
|
|
135
|
+
* @flowWeaver workflow
|
|
136
|
+
* @trigger event="app/expense.submitted"
|
|
137
|
+
*/
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
**Event trigger:**
|
|
141
|
+
```
|
|
142
|
+
@trigger event="app/expense.submitted"
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
**Cron trigger:**
|
|
146
|
+
```
|
|
147
|
+
@trigger cron="0 9 * * *"
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
**Both (event + cron):**
|
|
151
|
+
```
|
|
152
|
+
@trigger event="app/expense.submitted" cron="0 9 * * *"
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Cron expressions use standard 5-field format and are validated at parse time.
|
|
156
|
+
|
|
157
|
+
### `@cancelOn`
|
|
158
|
+
|
|
159
|
+
Cancel a running function when an event is received:
|
|
160
|
+
|
|
161
|
+
```
|
|
162
|
+
@cancelOn event="app/expense.withdrawn"
|
|
163
|
+
@cancelOn event="app/expense.withdrawn" match="data.expenseId"
|
|
164
|
+
@cancelOn event="app/expense.withdrawn" match="data.expenseId" timeout="1h"
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
| Field | Required | Description |
|
|
168
|
+
|-------|----------|-------------|
|
|
169
|
+
| `event=` | Yes | Event name that triggers cancellation |
|
|
170
|
+
| `match=` | No | Field to match between trigger and cancel events |
|
|
171
|
+
| `timeout=` | No | Maximum wait time for the cancel event |
|
|
172
|
+
|
|
173
|
+
### `@retries`
|
|
174
|
+
|
|
175
|
+
Number of retries per function:
|
|
176
|
+
|
|
177
|
+
```
|
|
178
|
+
@retries 5
|
|
179
|
+
@retries 0
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
Must be a non-negative integer.
|
|
183
|
+
|
|
184
|
+
### `@timeout`
|
|
185
|
+
|
|
186
|
+
Function-level timeout:
|
|
187
|
+
|
|
188
|
+
```
|
|
189
|
+
@timeout "30m"
|
|
190
|
+
@timeout "7d"
|
|
191
|
+
@timeout "1h"
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### `@throttle`
|
|
195
|
+
|
|
196
|
+
Rate limiting:
|
|
197
|
+
|
|
198
|
+
```
|
|
199
|
+
@throttle limit=20
|
|
200
|
+
@throttle limit=3 period="1h"
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
| Field | Required | Description |
|
|
204
|
+
|-------|----------|-------------|
|
|
205
|
+
| `limit=` | Yes | Maximum concurrent executions (integer) |
|
|
206
|
+
| `period=` | No | Time period for the limit |
|
|
207
|
+
|
|
208
|
+
### Complete Example
|
|
209
|
+
|
|
210
|
+
```typescript
|
|
211
|
+
/**
|
|
212
|
+
* @flowWeaver workflow
|
|
213
|
+
* @trigger event="app/expense.submitted"
|
|
214
|
+
* @cancelOn event="app/expense.withdrawn" match="data.expenseId"
|
|
215
|
+
* @retries 3
|
|
216
|
+
* @timeout "7d"
|
|
217
|
+
* @throttle limit=10 period="1m"
|
|
218
|
+
*
|
|
219
|
+
* @node v validateExpense
|
|
220
|
+
* @node pay processPayment
|
|
221
|
+
* @node wait waitForEvent [expr: eventName="'app/expense.approved'", match="'data.expenseId'", timeout="'48h'"]
|
|
222
|
+
* @path Start -> v -> wait -> pay -> Exit
|
|
223
|
+
*/
|
|
224
|
+
export async function expenseWorkflow(
|
|
225
|
+
params: { expenseId: string; amount: number }
|
|
226
|
+
): Promise<{ result: object }> {
|
|
227
|
+
throw new Error('Not compiled');
|
|
228
|
+
}
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
---
|
|
232
|
+
|
|
233
|
+
## Compile Options
|
|
234
|
+
|
|
235
|
+
### Production Mode (`--production`)
|
|
236
|
+
|
|
237
|
+
Strips all debug instrumentation from generated code. No `STATUS_CHANGED`, `VARIABLE_SET`, or `LOG_ERROR` events are emitted. Use this for deployed workflows.
|
|
238
|
+
|
|
239
|
+
```bash
|
|
240
|
+
flow-weaver compile workflow.ts --production
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### Source Maps (`--source-map`)
|
|
244
|
+
|
|
245
|
+
Generate source maps alongside compiled output:
|
|
246
|
+
|
|
247
|
+
```bash
|
|
248
|
+
flow-weaver compile workflow.ts --source-map
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
### Module Format (`--format`)
|
|
252
|
+
|
|
253
|
+
Control the output module format:
|
|
254
|
+
|
|
255
|
+
| Value | Description |
|
|
256
|
+
|-------|-------------|
|
|
257
|
+
| `auto` | Auto-detect from `package.json` type field (default) |
|
|
258
|
+
| `esm` | ES modules (`import`/`export`) |
|
|
259
|
+
| `cjs` | CommonJS (`require`/`module.exports`) |
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
flow-weaver compile workflow.ts --format cjs
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
### Strict Mode (`--strict`)
|
|
266
|
+
|
|
267
|
+
Promote type coercion warnings to errors:
|
|
268
|
+
|
|
269
|
+
```bash
|
|
270
|
+
flow-weaver compile workflow.ts --strict
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
Equivalent to adding `@strictTypes` to the workflow annotation.
|
|
274
|
+
|
|
275
|
+
### Inline Runtime (`--inline-runtime`)
|
|
276
|
+
|
|
277
|
+
Force inline runtime code even when `@synergenius/flow-weaver` is installed as a dependency. Normally the compiler generates an import; this flag embeds the runtime directly.
|
|
278
|
+
|
|
279
|
+
```bash
|
|
280
|
+
flow-weaver compile workflow.ts --inline-runtime
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
### Clean Output (`--clean`)
|
|
284
|
+
|
|
285
|
+
Omit redundant `@param`/`@returns` annotations from the compiled output. Produces cleaner generated code.
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
flow-weaver compile workflow.ts --clean
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Dry Run (`--dry-run`)
|
|
292
|
+
|
|
293
|
+
Preview compilation output without writing any files:
|
|
294
|
+
|
|
295
|
+
```bash
|
|
296
|
+
flow-weaver compile workflow.ts --dry-run
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
---
|
|
300
|
+
|
|
301
|
+
## Serve Handler Generation
|
|
302
|
+
|
|
303
|
+
Generate a complete HTTP handler for receiving Inngest events:
|
|
304
|
+
|
|
305
|
+
```bash
|
|
306
|
+
flow-weaver compile workflow.ts --target inngest --serve --framework next
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
### Supported Frameworks
|
|
310
|
+
|
|
311
|
+
| Framework | Handler |
|
|
312
|
+
|-----------|---------|
|
|
313
|
+
| `next` | Next.js App Router route handler |
|
|
314
|
+
| `express` | Express middleware |
|
|
315
|
+
| `hono` | Hono route handler |
|
|
316
|
+
| `fastify` | Fastify plugin |
|
|
317
|
+
| `remix` | Remix action |
|
|
318
|
+
|
|
319
|
+
### Typed Events (`--typed-events`)
|
|
320
|
+
|
|
321
|
+
Generate Zod schemas for event validation from `@param` annotations:
|
|
322
|
+
|
|
323
|
+
```bash
|
|
324
|
+
flow-weaver compile workflow.ts --target inngest --typed-events
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
## CLI Overrides
|
|
330
|
+
|
|
331
|
+
Several Inngest annotations can be overridden from the CLI without modifying the source file:
|
|
332
|
+
|
|
333
|
+
| CLI Flag | Overrides |
|
|
334
|
+
|----------|-----------|
|
|
335
|
+
| `--cron <schedule>` | `@trigger cron=` |
|
|
336
|
+
| `--retries <n>` | `@retries` |
|
|
337
|
+
| `--timeout <duration>` | `@timeout` |
|
|
338
|
+
|
|
339
|
+
```bash
|
|
340
|
+
flow-weaver compile workflow.ts --target inngest --retries 5 --timeout "1h"
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
---
|
|
344
|
+
|
|
345
|
+
## Related Topics
|
|
346
|
+
|
|
347
|
+
- [CLI Reference](cli-reference) — Full compile command flags
|
|
348
|
+
- [Deployment](deployment) — Export to serverless platforms
|
|
349
|
+
- [Advanced Annotations](advanced-annotations) — Annotations that affect compilation
|
|
350
|
+
- [Debugging](debugging) — Debug instrumentation and WebSocket events
|
|
351
|
+
- [Built-in Nodes](built-in-nodes) — delay, waitForEvent, invokeWorkflow
|