yaml-flow 2.8.0 → 3.1.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 +189 -3
- package/dist/{constants-BEbO2_OK.d.ts → constants-B2zqu10b.d.ts} +32 -52
- package/dist/{constants-BNjeIlZ8.d.cts → constants-DJZU1pwJ.d.cts} +32 -52
- package/dist/continuous-event-graph/index.cjs +1388 -52
- package/dist/continuous-event-graph/index.cjs.map +1 -1
- package/dist/continuous-event-graph/index.d.cts +643 -5
- package/dist/continuous-event-graph/index.d.ts +643 -5
- package/dist/continuous-event-graph/index.js +1374 -53
- package/dist/continuous-event-graph/index.js.map +1 -1
- package/dist/event-graph/index.cjs +6817 -53
- package/dist/event-graph/index.cjs.map +1 -1
- package/dist/event-graph/index.d.cts +15 -6
- package/dist/event-graph/index.d.ts +15 -6
- package/dist/event-graph/index.js +6808 -51
- package/dist/event-graph/index.js.map +1 -1
- package/dist/index.cjs +1827 -441
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +6 -5
- package/dist/index.d.ts +6 -5
- package/dist/index.js +1808 -440
- package/dist/index.js.map +1 -1
- package/dist/inference/index.cjs +46 -13
- package/dist/inference/index.cjs.map +1 -1
- package/dist/inference/index.d.cts +2 -2
- package/dist/inference/index.d.ts +2 -2
- package/dist/inference/index.js +46 -13
- package/dist/inference/index.js.map +1 -1
- package/dist/step-machine/index.cjs +6600 -0
- package/dist/step-machine/index.cjs.map +1 -1
- package/dist/step-machine/index.d.cts +26 -1
- package/dist/step-machine/index.d.ts +26 -1
- package/dist/step-machine/index.js +6596 -1
- package/dist/step-machine/index.js.map +1 -1
- package/dist/{types-C2lOwquM.d.cts → types-BwvgvlOO.d.cts} +2 -2
- package/dist/{types-mS_pPftm.d.ts → types-ClRA8hzC.d.ts} +2 -2
- package/dist/{types-DAI_a2as.d.ts → types-DEj7OakX.d.cts} +29 -10
- package/dist/{types-DAI_a2as.d.cts → types-DEj7OakX.d.ts} +29 -10
- package/dist/validate-DEZ2Ymdb.d.ts +53 -0
- package/dist/validate-DqKTZg_o.d.cts +53 -0
- package/package.json +1 -1
- package/schema/event-graph.schema.json +254 -0
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { L as LiveGraph, N as NodeInfo, b as LiveGraphSnapshot, S as ScheduleResult, D as DownstreamResult, U as UnreachableNodesResult, c as UnreachableTokensResult, e as UpstreamResult, a as LiveGraphHealth } from '../types-
|
|
2
|
-
export { B as BlockedTask, P as PendingTask, d as UnresolvedDependency } from '../types-
|
|
3
|
-
import { T as TaskConfig,
|
|
4
|
-
export { c as ExecutionState,
|
|
1
|
+
import { L as LiveGraph, N as NodeInfo, b as LiveGraphSnapshot, S as ScheduleResult, D as DownstreamResult, U as UnreachableNodesResult, c as UnreachableTokensResult, e as UpstreamResult, a as LiveGraphHealth } from '../types-BwvgvlOO.cjs';
|
|
2
|
+
export { B as BlockedTask, P as PendingTask, d as UnresolvedDependency } from '../types-BwvgvlOO.cjs';
|
|
3
|
+
import { T as TaskConfig, f as GraphEvent, G as GraphConfig, e as GraphEngineStore } from '../types-DEj7OakX.cjs';
|
|
4
|
+
export { c as ExecutionState, g as GraphSettings } from '../types-DEj7OakX.cjs';
|
|
5
|
+
import { a as GraphValidationResult } from '../validate-DqKTZg_o.cjs';
|
|
5
6
|
|
|
6
7
|
/**
|
|
7
8
|
* Continuous Event Graph — Core
|
|
@@ -27,6 +28,12 @@ declare function createLiveGraph(config: GraphConfig, executionId?: string): Liv
|
|
|
27
28
|
* Config is NOT mutated by events — only state changes.
|
|
28
29
|
*/
|
|
29
30
|
declare function applyEvent(live: LiveGraph, event: GraphEvent): LiveGraph;
|
|
31
|
+
/**
|
|
32
|
+
* Apply multiple events atomically to a LiveGraph.
|
|
33
|
+
* Events are reduced sequentially, but the caller only sees the final state.
|
|
34
|
+
* Use this for batch processing (e.g. draining a journal of pending events).
|
|
35
|
+
*/
|
|
36
|
+
declare function applyEvents(live: LiveGraph, events: GraphEvent[]): LiveGraph;
|
|
30
37
|
/**
|
|
31
38
|
* Add a node (task) to the live graph. Updates both config and state atomically.
|
|
32
39
|
* If the node already exists, returns the graph unchanged.
|
|
@@ -176,4 +183,635 @@ declare function getUpstream(live: LiveGraph, nodeName: string): UpstreamResult;
|
|
|
176
183
|
*/
|
|
177
184
|
declare function getDownstream(live: LiveGraph, nodeName: string): DownstreamResult;
|
|
178
185
|
|
|
179
|
-
|
|
186
|
+
/**
|
|
187
|
+
* Continuous Event Graph — Journal
|
|
188
|
+
*
|
|
189
|
+
* Append-only event log for the reactive layer.
|
|
190
|
+
* Handlers append events here; drain() reads and clears atomically.
|
|
191
|
+
*
|
|
192
|
+
* Two adapters:
|
|
193
|
+
* - MemoryJournal: in-process array (default)
|
|
194
|
+
* - FileJournal: append to a JSONL file, truncate on drain
|
|
195
|
+
*/
|
|
196
|
+
|
|
197
|
+
interface Journal {
|
|
198
|
+
/** Append an event to the journal. Safe to call from concurrent callbacks. */
|
|
199
|
+
append(event: GraphEvent): void;
|
|
200
|
+
/** Read all pending events and clear the journal atomically. */
|
|
201
|
+
drain(): GraphEvent[];
|
|
202
|
+
/** Number of pending events (for observability). */
|
|
203
|
+
readonly size: number;
|
|
204
|
+
}
|
|
205
|
+
declare class MemoryJournal implements Journal {
|
|
206
|
+
private buffer;
|
|
207
|
+
append(event: GraphEvent): void;
|
|
208
|
+
drain(): GraphEvent[];
|
|
209
|
+
get size(): number;
|
|
210
|
+
}
|
|
211
|
+
declare class FileJournal implements Journal {
|
|
212
|
+
private readonly path;
|
|
213
|
+
private pending;
|
|
214
|
+
constructor(path: string);
|
|
215
|
+
append(event: GraphEvent): void;
|
|
216
|
+
drain(): GraphEvent[];
|
|
217
|
+
get size(): number;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Continuous Event Graph — Reactive Layer
|
|
222
|
+
*
|
|
223
|
+
* Push-based, self-sustaining execution wrapper.
|
|
224
|
+
*
|
|
225
|
+
* Pattern:
|
|
226
|
+
* 1. Register handlers for tasks
|
|
227
|
+
* 2. Push an event (or inject tokens)
|
|
228
|
+
* 3. The graph drives itself: drain journal → applyEvents → schedule → dispatch → repeat
|
|
229
|
+
*
|
|
230
|
+
* No daemon, no polling. Each handler callback appends to the journal,
|
|
231
|
+
* which triggers a drain cycle that may dispatch the next wave.
|
|
232
|
+
*
|
|
233
|
+
* Dispatch failures, retries, and timeouts are managed internally
|
|
234
|
+
* without touching the core engine types.
|
|
235
|
+
*/
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Deterministic hash of a data payload.
|
|
239
|
+
* Recursively-sorted JSON → SHA-256 hex (first 16 chars for compactness).
|
|
240
|
+
* Used to auto-compute dataHash when the handler doesn't provide one.
|
|
241
|
+
* Exported so handler authors can pre-compute or test hashes.
|
|
242
|
+
*/
|
|
243
|
+
declare function computeDataHash(data: Record<string, unknown>): string;
|
|
244
|
+
/**
|
|
245
|
+
* Input passed to a task handler function.
|
|
246
|
+
*
|
|
247
|
+
* The reactive layer resolves upstream data from `requires` into `state`,
|
|
248
|
+
* and provides this task's own engine store as `taskState`.
|
|
249
|
+
* Handlers push output data back via `graph.resolveCallback(callbackToken, data)`.
|
|
250
|
+
*/
|
|
251
|
+
interface TaskHandlerInput {
|
|
252
|
+
/** This task's node ID (task name) */
|
|
253
|
+
nodeId: string;
|
|
254
|
+
/**
|
|
255
|
+
* Upstream dependency data, keyed by require token name.
|
|
256
|
+
* Only tokens from this task's `requires` are present.
|
|
257
|
+
* Value is the producing task's `data` field (or undefined if not yet available).
|
|
258
|
+
*/
|
|
259
|
+
state: Readonly<Record<string, Record<string, unknown> | undefined>>;
|
|
260
|
+
/**
|
|
261
|
+
* This task's own GraphEngineStore — includes status, data, executionCount, etc.
|
|
262
|
+
*/
|
|
263
|
+
taskState: Readonly<GraphEngineStore>;
|
|
264
|
+
/** This task's config */
|
|
265
|
+
config: Readonly<TaskConfig>;
|
|
266
|
+
/**
|
|
267
|
+
* Opaque callback token encoding this task's identity.
|
|
268
|
+
* Pass this to `graph.resolveCallback(callbackToken, data)` to complete the task.
|
|
269
|
+
* Can be serialized and sent to external systems (webhooks, other scripts,
|
|
270
|
+
* message queues) — any process with this token can push data back.
|
|
271
|
+
*/
|
|
272
|
+
callbackToken: string;
|
|
273
|
+
}
|
|
274
|
+
/**
|
|
275
|
+
* Handler return value — initiation status only.
|
|
276
|
+
* - `'task-initiated'` — async work started successfully; data will arrive via resolveCallback
|
|
277
|
+
* - `'task-initiate-failure'` — failed to start (bad config, connection refused, etc.)
|
|
278
|
+
*/
|
|
279
|
+
type TaskHandlerReturn = 'task-initiated' | 'task-initiate-failure';
|
|
280
|
+
/**
|
|
281
|
+
* A named task handler function.
|
|
282
|
+
* Registered in the handler registry, referenced by name in `taskConfig.taskHandlers`.
|
|
283
|
+
*
|
|
284
|
+
* The handler's job is to **initiate** async work, not await it.
|
|
285
|
+
*
|
|
286
|
+
* Flow:
|
|
287
|
+
* 1. Handler receives `callbackToken` + upstream `state`
|
|
288
|
+
* 2. Handler kicks off background work (internal, external script, webhook, etc.)
|
|
289
|
+
* — passes `callbackToken` to the background work
|
|
290
|
+
* 3. Handler returns `'task-initiated'` immediately
|
|
291
|
+
* 4. Background work runs independently — when done, it calls
|
|
292
|
+
* `graph.resolveCallback(callbackToken, data)` for success, or
|
|
293
|
+
* `graph.resolveCallback(callbackToken, {}, ['error msg'])` for failure
|
|
294
|
+
* 5. resolveCallback completes the task → data-changed cascade fires
|
|
295
|
+
*
|
|
296
|
+
* The callbackToken is opaque — pass it to the background work so it can
|
|
297
|
+
* call back. Works across processes, scripts, webhooks, message queues.
|
|
298
|
+
*
|
|
299
|
+
* @example
|
|
300
|
+
* ```ts
|
|
301
|
+
* const fetchYahoo: TaskHandlerFn = async ({ state, callbackToken }) => {
|
|
302
|
+
* const symbols = state['portfolio-form']?.holdings?.map(h => h.symbol) ?? [];
|
|
303
|
+
* // Kick off background work — do NOT await
|
|
304
|
+
* fetch(`https://api.yahoo.com/prices?s=${symbols.join(',')}`)
|
|
305
|
+
* .then(res => res.json())
|
|
306
|
+
* .then(prices => graph.resolveCallback(callbackToken, { prices }))
|
|
307
|
+
* .catch(err => graph.resolveCallback(callbackToken, {}, [err.message]));
|
|
308
|
+
* // Return immediately — background work will resolveCallback when done
|
|
309
|
+
* return 'task-initiated';
|
|
310
|
+
* };
|
|
311
|
+
* ```
|
|
312
|
+
*/
|
|
313
|
+
type TaskHandlerFn = (input: TaskHandlerInput) => Promise<TaskHandlerReturn>;
|
|
314
|
+
interface ReactiveGraphOptions {
|
|
315
|
+
/** Named handler registry — handler name → handler function */
|
|
316
|
+
handlers: Record<string, TaskHandlerFn>;
|
|
317
|
+
/** Journal adapter (default: MemoryJournal) */
|
|
318
|
+
journal?: Journal;
|
|
319
|
+
/** Called after each drain cycle — for observability */
|
|
320
|
+
onDrain?: (events: GraphEvent[], live: LiveGraph, scheduleResult: ScheduleResult) => void;
|
|
321
|
+
}
|
|
322
|
+
interface ReactiveGraph {
|
|
323
|
+
/** Push an event into the graph via journal. Triggers drain → schedule → dispatch. */
|
|
324
|
+
push(event: GraphEvent): void;
|
|
325
|
+
/** Push multiple events via journal. Single drain cycle after all are journaled. */
|
|
326
|
+
pushAll(events: GraphEvent[]): void;
|
|
327
|
+
/**
|
|
328
|
+
* Resolve a callback token — complete (or fail) a task after initiation.
|
|
329
|
+
* Journals task-completed or task-failed, then drains.
|
|
330
|
+
* Gracefully ignores invalid tokens or tokens for tasks no longer in the graph.
|
|
331
|
+
*/
|
|
332
|
+
resolveCallback(callbackToken: string, data: Record<string, unknown>, errors?: string[]): void;
|
|
333
|
+
/** Add a node to the graph config. Journals nothing — structural mutation. */
|
|
334
|
+
addNode(name: string, taskConfig: TaskConfig): void;
|
|
335
|
+
/** Remove a node from the graph config. Structural mutation. */
|
|
336
|
+
removeNode(name: string): void;
|
|
337
|
+
/** Add required tokens to an existing node. Structural mutation + drain. */
|
|
338
|
+
addRequires(nodeName: string, tokens: string[]): void;
|
|
339
|
+
/** Remove required tokens from an existing node. Structural mutation + drain. */
|
|
340
|
+
removeRequires(nodeName: string, tokens: string[]): void;
|
|
341
|
+
/** Add provided tokens to an existing node. Structural mutation + drain. */
|
|
342
|
+
addProvides(nodeName: string, tokens: string[]): void;
|
|
343
|
+
/** Remove provided tokens from an existing node. Structural mutation. */
|
|
344
|
+
removeProvides(nodeName: string, tokens: string[]): void;
|
|
345
|
+
/** Register a named handler in the registry. */
|
|
346
|
+
registerHandler(name: string, fn: TaskHandlerFn): void;
|
|
347
|
+
/** Unregister a named handler from the registry. */
|
|
348
|
+
unregisterHandler(name: string): void;
|
|
349
|
+
/**
|
|
350
|
+
* Re-trigger a task: journals a task-restart event, then drains.
|
|
351
|
+
* data-changed cascade handles downstream automatically.
|
|
352
|
+
*/
|
|
353
|
+
retrigger(taskName: string): void;
|
|
354
|
+
/** Re-trigger multiple tasks via journal. */
|
|
355
|
+
retriggerAll(taskNames: string[]): void;
|
|
356
|
+
/** Read-only snapshot of current LiveGraph state. */
|
|
357
|
+
getState(): LiveGraph;
|
|
358
|
+
/** Current schedule projection. */
|
|
359
|
+
getSchedule(): ScheduleResult;
|
|
360
|
+
/** Stop accepting events. */
|
|
361
|
+
dispose(): void;
|
|
362
|
+
}
|
|
363
|
+
declare function createReactiveGraph(config: GraphConfig, options: ReactiveGraphOptions, executionId?: string): ReactiveGraph;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Continuous Event Graph — Validation Utilities
|
|
367
|
+
*
|
|
368
|
+
* Runtime state-consistency checks for LiveGraph and ReactiveGraph.
|
|
369
|
+
* Unlike event-graph/validate.ts which validates static GraphConfig structure,
|
|
370
|
+
* these validate the *live* runtime state against its config.
|
|
371
|
+
*
|
|
372
|
+
* Pure functions — config+state in, diagnostics out.
|
|
373
|
+
*/
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Validate that a LiveGraph's runtime state is consistent with its config.
|
|
377
|
+
*
|
|
378
|
+
* Checks:
|
|
379
|
+
* - Every config task has a corresponding state entry (MISSING_STATE)
|
|
380
|
+
* - No orphan state entries exist for tasks not in config (ORPHAN_STATE)
|
|
381
|
+
* - Running tasks have a startedAt timestamp (RUNNING_WITHOUT_START)
|
|
382
|
+
* - Completed tasks have a completedAt timestamp (COMPLETED_WITHOUT_TIMESTAMP)
|
|
383
|
+
* - Failed tasks have a failedAt timestamp and error (FAILED_WITHOUT_INFO)
|
|
384
|
+
* - Available outputs match what completed tasks should have produced (PHANTOM_OUTPUT / MISSING_OUTPUT)
|
|
385
|
+
* - Execution counts are non-negative (INVALID_EXECUTION_COUNT)
|
|
386
|
+
* - No task has executionCount > maxExecutions when capped (EXCEEDED_MAX_EXECUTIONS)
|
|
387
|
+
*/
|
|
388
|
+
declare function validateLiveGraph(live: LiveGraph): GraphValidationResult;
|
|
389
|
+
/**
|
|
390
|
+
* Input for reactive graph validation.
|
|
391
|
+
* Accepts the reactive graph instance plus the original options (for handler list reference).
|
|
392
|
+
*/
|
|
393
|
+
interface ReactiveGraphValidationInput {
|
|
394
|
+
/** The reactive graph instance to validate */
|
|
395
|
+
graph: ReactiveGraph;
|
|
396
|
+
/** The handler registry (handler name → handler function) */
|
|
397
|
+
handlers: Record<string, unknown>;
|
|
398
|
+
}
|
|
399
|
+
/**
|
|
400
|
+
* Validate reactive-graph-specific consistency.
|
|
401
|
+
*
|
|
402
|
+
* Checks:
|
|
403
|
+
* - Every handler name referenced in taskConfig.taskHandlers exists in the registry (MISSING_HANDLER)
|
|
404
|
+
* - No handlers registered that are not referenced by any task's taskHandlers (ORPHAN_HANDLER)
|
|
405
|
+
* - Plus all validateLiveGraph checks on the underlying state
|
|
406
|
+
*/
|
|
407
|
+
declare function validateReactiveGraph(input: ReactiveGraphValidationInput): GraphValidationResult;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Continuous Event Graph — mutateGraph
|
|
411
|
+
*
|
|
412
|
+
* A higher-level batch mutation API.
|
|
413
|
+
*
|
|
414
|
+
* Unlike calling addNode/removeNode/injectTokens individually, mutateGraph
|
|
415
|
+
* accepts a declarative array of mutations and applies them atomically.
|
|
416
|
+
* This is useful for:
|
|
417
|
+
* - Applying a set of structural changes + events in a single call
|
|
418
|
+
* - Building mutation pipelines from external configs
|
|
419
|
+
* - Reducing boilerplate when scripting graph changes
|
|
420
|
+
*
|
|
421
|
+
* Pattern: mutateGraph(live, mutations[]) → LiveGraph
|
|
422
|
+
* Pure function — no side effects.
|
|
423
|
+
*/
|
|
424
|
+
|
|
425
|
+
type GraphMutation = AddNodeMutation | RemoveNodeMutation | AddRequiresMutation | RemoveRequiresMutation | AddProvidesMutation | RemoveProvidesMutation | InjectTokensMutation | DrainTokensMutation | ResetNodeMutation | DisableNodeMutation | EnableNodeMutation | ApplyEventsMutation;
|
|
426
|
+
interface AddNodeMutation {
|
|
427
|
+
type: 'add-node';
|
|
428
|
+
name: string;
|
|
429
|
+
config: TaskConfig;
|
|
430
|
+
}
|
|
431
|
+
interface RemoveNodeMutation {
|
|
432
|
+
type: 'remove-node';
|
|
433
|
+
name: string;
|
|
434
|
+
}
|
|
435
|
+
interface AddRequiresMutation {
|
|
436
|
+
type: 'add-requires';
|
|
437
|
+
taskName: string;
|
|
438
|
+
tokens: string[];
|
|
439
|
+
}
|
|
440
|
+
interface RemoveRequiresMutation {
|
|
441
|
+
type: 'remove-requires';
|
|
442
|
+
taskName: string;
|
|
443
|
+
tokens: string[];
|
|
444
|
+
}
|
|
445
|
+
interface AddProvidesMutation {
|
|
446
|
+
type: 'add-provides';
|
|
447
|
+
taskName: string;
|
|
448
|
+
tokens: string[];
|
|
449
|
+
}
|
|
450
|
+
interface RemoveProvidesMutation {
|
|
451
|
+
type: 'remove-provides';
|
|
452
|
+
taskName: string;
|
|
453
|
+
tokens: string[];
|
|
454
|
+
}
|
|
455
|
+
interface InjectTokensMutation {
|
|
456
|
+
type: 'inject-tokens';
|
|
457
|
+
tokens: string[];
|
|
458
|
+
}
|
|
459
|
+
interface DrainTokensMutation {
|
|
460
|
+
type: 'drain-tokens';
|
|
461
|
+
tokens: string[];
|
|
462
|
+
}
|
|
463
|
+
interface ResetNodeMutation {
|
|
464
|
+
type: 'reset-node';
|
|
465
|
+
name: string;
|
|
466
|
+
}
|
|
467
|
+
interface DisableNodeMutation {
|
|
468
|
+
type: 'disable-node';
|
|
469
|
+
name: string;
|
|
470
|
+
}
|
|
471
|
+
interface EnableNodeMutation {
|
|
472
|
+
type: 'enable-node';
|
|
473
|
+
name: string;
|
|
474
|
+
}
|
|
475
|
+
interface ApplyEventsMutation {
|
|
476
|
+
type: 'apply-events';
|
|
477
|
+
events: GraphEvent[];
|
|
478
|
+
}
|
|
479
|
+
/**
|
|
480
|
+
* Apply an ordered array of mutations to a LiveGraph, returning the new state.
|
|
481
|
+
*
|
|
482
|
+
* Mutations are applied in order. Each mutation can depend on the result of
|
|
483
|
+
* the previous one (e.g., add a node, then inject tokens it requires).
|
|
484
|
+
*
|
|
485
|
+
* Pure function — does not modify the input.
|
|
486
|
+
*
|
|
487
|
+
* @param live - The current LiveGraph
|
|
488
|
+
* @param mutations - Ordered array of mutations to apply
|
|
489
|
+
* @returns The new LiveGraph after all mutations
|
|
490
|
+
* @throws Error if a mutation references a non-existent task (for safety)
|
|
491
|
+
*/
|
|
492
|
+
declare function mutateGraph(live: LiveGraph, mutations: GraphMutation[]): LiveGraph;
|
|
493
|
+
|
|
494
|
+
/**
|
|
495
|
+
* Continuous Event Graph — Handler Factories
|
|
496
|
+
*
|
|
497
|
+
* Ready-made TaskHandlerFn factories for common integration patterns.
|
|
498
|
+
* Each factory returns a TaskHandlerFn compatible with createReactiveGraph.
|
|
499
|
+
*
|
|
500
|
+
* In the callbackToken model, handlers are **initiators** — they kick off
|
|
501
|
+
* background work and return 'task-initiated'. The background work calls
|
|
502
|
+
* graph.resolveCallback(callbackToken, data) when done.
|
|
503
|
+
*
|
|
504
|
+
* Factories that wrap synchronous/async compute accept a `getGraph` getter
|
|
505
|
+
* to obtain the resolveCallback reference (lazy-bound because the graph
|
|
506
|
+
* doesn't exist yet at handler-creation time).
|
|
507
|
+
*
|
|
508
|
+
* Patterns:
|
|
509
|
+
* createCallbackHandler — wrap an async function that computes data
|
|
510
|
+
* createFireAndForgetHandler — side-effect-only (always resolves empty data)
|
|
511
|
+
* createShellHandler — run a shell command, resolve with stdout
|
|
512
|
+
* createScriptHandler — spawn a Node.js/Python script
|
|
513
|
+
* createWebhookHandler — POST to a URL, resolve with response
|
|
514
|
+
* createNoopHandler — always resolves immediately (testing/placeholders)
|
|
515
|
+
*/
|
|
516
|
+
|
|
517
|
+
/** Minimal resolveCallback interface — matches ReactiveGraph.resolveCallback */
|
|
518
|
+
interface ResolveCallbackFn {
|
|
519
|
+
(callbackToken: string, data: Record<string, unknown>, errors?: string[]): void;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Wrap a plain async function as a TaskHandlerFn.
|
|
523
|
+
*
|
|
524
|
+
* The function receives TaskHandlerInput and returns data.
|
|
525
|
+
* The factory handles the callbackToken plumbing — it fires
|
|
526
|
+
* the function in the background and calls resolveCallback.
|
|
527
|
+
*
|
|
528
|
+
* @param fn - Async function that computes and returns data
|
|
529
|
+
* @param getResolve - Lazy getter for the resolveCallback function
|
|
530
|
+
*
|
|
531
|
+
* @example
|
|
532
|
+
* ```ts
|
|
533
|
+
* let graph: ReactiveGraph;
|
|
534
|
+
* const handler = createCallbackHandler(
|
|
535
|
+
* async ({ state }) => {
|
|
536
|
+
* const prices = await fetchPrices(state['portfolio-form']?.symbols);
|
|
537
|
+
* return { prices };
|
|
538
|
+
* },
|
|
539
|
+
* () => graph.resolveCallback.bind(graph),
|
|
540
|
+
* );
|
|
541
|
+
* graph = createReactiveGraph(config, { handlers: { fetchPrices: handler } });
|
|
542
|
+
* ```
|
|
543
|
+
*/
|
|
544
|
+
declare function createCallbackHandler(fn: (input: TaskHandlerInput) => Promise<Record<string, unknown>>, getResolve: () => ResolveCallbackFn): TaskHandlerFn;
|
|
545
|
+
/**
|
|
546
|
+
* Fire-and-forget variant — the async function is invoked but
|
|
547
|
+
* the handler always resolves the task with empty data.
|
|
548
|
+
* Useful for side-effect-only tasks (logging, notifications).
|
|
549
|
+
*
|
|
550
|
+
* @param fn - Side-effect function (logging, alerting, etc.)
|
|
551
|
+
* @param getResolve - Lazy getter for the resolveCallback function
|
|
552
|
+
*
|
|
553
|
+
* @example
|
|
554
|
+
* ```ts
|
|
555
|
+
* const handler = createFireAndForgetHandler(
|
|
556
|
+
* async ({ nodeId }) => { await sendSlack(`${nodeId} started`); },
|
|
557
|
+
* () => graph.resolveCallback.bind(graph),
|
|
558
|
+
* );
|
|
559
|
+
* ```
|
|
560
|
+
*/
|
|
561
|
+
declare function createFireAndForgetHandler(fn: (input: TaskHandlerInput) => Promise<void> | void, getResolve: () => ResolveCallbackFn): TaskHandlerFn;
|
|
562
|
+
interface ShellHandlerOptions {
|
|
563
|
+
/** Shell command to run. Supports ${taskName} placeholder. */
|
|
564
|
+
command: string;
|
|
565
|
+
/** Working directory (default: process.cwd()) */
|
|
566
|
+
cwd?: string;
|
|
567
|
+
/** Additional environment variables */
|
|
568
|
+
env?: Record<string, string>;
|
|
569
|
+
/** Timeout in ms (default: 30000) */
|
|
570
|
+
timeoutMs?: number;
|
|
571
|
+
/** Map exit codes to result keys (default: 0 → 'success', non-zero → 'failure') */
|
|
572
|
+
exitCodeMap?: Record<number, string>;
|
|
573
|
+
/** If true, include stdout/stderr in data payload */
|
|
574
|
+
captureOutput?: boolean;
|
|
575
|
+
/** Lazy getter for the resolveCallback function */
|
|
576
|
+
getResolve: () => ResolveCallbackFn;
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Create a TaskHandlerFn that runs a shell command.
|
|
580
|
+
*
|
|
581
|
+
* By default, exit code 0 = resolves with stdout data, non-zero = resolves with error.
|
|
582
|
+
* Use exitCodeMap to map specific codes to result keys for conditional routing.
|
|
583
|
+
*
|
|
584
|
+
* @example
|
|
585
|
+
* ```ts
|
|
586
|
+
* const handler = createShellHandler({
|
|
587
|
+
* command: 'python scripts/process.py --task ${taskName}',
|
|
588
|
+
* cwd: '/app',
|
|
589
|
+
* captureOutput: true,
|
|
590
|
+
* getResolve: () => graph.resolveCallback.bind(graph),
|
|
591
|
+
* });
|
|
592
|
+
* ```
|
|
593
|
+
*/
|
|
594
|
+
declare function createShellHandler(options: ShellHandlerOptions): TaskHandlerFn;
|
|
595
|
+
interface ScriptHandlerOptions {
|
|
596
|
+
/** Path to the script file */
|
|
597
|
+
scriptPath: string;
|
|
598
|
+
/** Runtime to use (default: auto-detected from extension) */
|
|
599
|
+
runtime?: 'node' | 'python' | 'python3' | 'bash' | 'sh';
|
|
600
|
+
/** Additional CLI arguments */
|
|
601
|
+
args?: string[];
|
|
602
|
+
/** Working directory */
|
|
603
|
+
cwd?: string;
|
|
604
|
+
/** Timeout in ms (default: 60000) */
|
|
605
|
+
timeoutMs?: number;
|
|
606
|
+
/** If true, include stdout/stderr in data payload */
|
|
607
|
+
captureOutput?: boolean;
|
|
608
|
+
/** Lazy getter for the resolveCallback function */
|
|
609
|
+
getResolve: () => ResolveCallbackFn;
|
|
610
|
+
}
|
|
611
|
+
/**
|
|
612
|
+
* Create a TaskHandlerFn that spawns a script file.
|
|
613
|
+
*
|
|
614
|
+
* Auto-detects the runtime from the file extension unless overridden.
|
|
615
|
+
* The task name is passed as the first argument to the script,
|
|
616
|
+
* followed by any additional args.
|
|
617
|
+
*
|
|
618
|
+
* @example
|
|
619
|
+
* ```ts
|
|
620
|
+
* const handler = createScriptHandler({
|
|
621
|
+
* scriptPath: './scripts/etl.py',
|
|
622
|
+
* args: ['--verbose'],
|
|
623
|
+
* captureOutput: true,
|
|
624
|
+
* getResolve: () => graph.resolveCallback.bind(graph),
|
|
625
|
+
* });
|
|
626
|
+
* ```
|
|
627
|
+
*/
|
|
628
|
+
declare function createScriptHandler(options: ScriptHandlerOptions): TaskHandlerFn;
|
|
629
|
+
interface WebhookHandlerOptions {
|
|
630
|
+
/** URL to POST to. Supports ${taskName} placeholder. */
|
|
631
|
+
url: string;
|
|
632
|
+
/** HTTP method (default: POST) */
|
|
633
|
+
method?: 'POST' | 'PUT' | 'PATCH';
|
|
634
|
+
/** Additional headers */
|
|
635
|
+
headers?: Record<string, string>;
|
|
636
|
+
/** Timeout in ms (default: 30000) */
|
|
637
|
+
timeoutMs?: number;
|
|
638
|
+
/** If true, treat non-2xx status as failure */
|
|
639
|
+
failOnNon2xx?: boolean;
|
|
640
|
+
/** Lazy getter for the resolveCallback function */
|
|
641
|
+
getResolve: () => ResolveCallbackFn;
|
|
642
|
+
}
|
|
643
|
+
/**
|
|
644
|
+
* Create a TaskHandlerFn that sends an HTTP request.
|
|
645
|
+
*
|
|
646
|
+
* Uses native fetch (Node 18+). The task context (nodeId, config)
|
|
647
|
+
* is sent as the JSON body along with the callbackToken.
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```ts
|
|
651
|
+
* const handler = createWebhookHandler({
|
|
652
|
+
* url: 'https://api.example.com/tasks/${taskName}/trigger',
|
|
653
|
+
* headers: { 'Authorization': 'Bearer ...' },
|
|
654
|
+
* getResolve: () => graph.resolveCallback.bind(graph),
|
|
655
|
+
* });
|
|
656
|
+
* ```
|
|
657
|
+
*/
|
|
658
|
+
declare function createWebhookHandler(options: WebhookHandlerOptions): TaskHandlerFn;
|
|
659
|
+
/**
|
|
660
|
+
* Create a handler that always resolves immediately with static data.
|
|
661
|
+
* Useful for testing, placeholders, or passthrough tasks.
|
|
662
|
+
*
|
|
663
|
+
* @param getResolve - Lazy getter for the resolveCallback function
|
|
664
|
+
* @param staticData - Optional static data to resolve with
|
|
665
|
+
*/
|
|
666
|
+
declare function createNoopHandler(getResolve: () => ResolveCallbackFn, staticData?: Record<string, unknown>): TaskHandlerFn;
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Live Cards → Reactive Graph
|
|
670
|
+
*
|
|
671
|
+
* Takes an array of live card JSONs (card / source nodes) and produces
|
|
672
|
+
* a fully wired ReactiveGraph where:
|
|
673
|
+
*
|
|
674
|
+
* - Each card becomes a task in the graph
|
|
675
|
+
* - card.data.requires → task.requires (upstream card IDs as tokens)
|
|
676
|
+
* - Each card produces a token equal to its own ID
|
|
677
|
+
* - Card-type nodes: handler runs CardCompute.run() on a clone of the card,
|
|
678
|
+
* returns the computed state as data (auto-hashed by the reactive layer)
|
|
679
|
+
* - Source-type nodes: handler uses the source definition to fetch data,
|
|
680
|
+
* or falls back to a user-provided handler / noop
|
|
681
|
+
*
|
|
682
|
+
* The reactive graph auto-computes dataHash on every handler result,
|
|
683
|
+
* so `data-changed` refresh strategy works out of the box.
|
|
684
|
+
*
|
|
685
|
+
* @example
|
|
686
|
+
* ```ts
|
|
687
|
+
* import { liveCardsToReactiveGraph } from 'yaml-flow/continuous-event-graph';
|
|
688
|
+
*
|
|
689
|
+
* const cards = [
|
|
690
|
+
* { id: 'prices', type: 'source', source: { kind: 'api', bindTo: 'state.raw', url_template: '...' }, state: {} },
|
|
691
|
+
* { id: 'dashboard', type: 'card', data: { requires: ['prices'] }, state: {}, compute: { total: { fn: 'sum', ... } }, view: { ... } },
|
|
692
|
+
* ];
|
|
693
|
+
*
|
|
694
|
+
* const rg = liveCardsToReactiveGraph(cards, {
|
|
695
|
+
* sourceHandlers: {
|
|
696
|
+
* prices: async () => ({ data: { raw: await fetchPrices() } }),
|
|
697
|
+
* },
|
|
698
|
+
* });
|
|
699
|
+
*
|
|
700
|
+
* // One push → the whole board computes itself
|
|
701
|
+
* rg.push({ type: 'inject-tokens', tokens: [], timestamp: new Date().toISOString() });
|
|
702
|
+
* ```
|
|
703
|
+
*/
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* Minimal live card shape accepted by this utility.
|
|
707
|
+
* Matches the live-cards.schema.json structure.
|
|
708
|
+
*/
|
|
709
|
+
interface LiveCard {
|
|
710
|
+
id: string;
|
|
711
|
+
type: 'card' | 'source';
|
|
712
|
+
meta?: {
|
|
713
|
+
title?: string;
|
|
714
|
+
tags?: string[];
|
|
715
|
+
};
|
|
716
|
+
data?: {
|
|
717
|
+
requires?: string[];
|
|
718
|
+
provides?: Record<string, unknown>;
|
|
719
|
+
};
|
|
720
|
+
state?: Record<string, unknown>;
|
|
721
|
+
compute?: Record<string, unknown>;
|
|
722
|
+
source?: {
|
|
723
|
+
kind: 'api' | 'websocket' | 'static' | 'llm';
|
|
724
|
+
bindTo: string;
|
|
725
|
+
url_template?: string;
|
|
726
|
+
method?: string;
|
|
727
|
+
headers?: Record<string, unknown>;
|
|
728
|
+
body_template?: Record<string, unknown>;
|
|
729
|
+
poll_interval?: number;
|
|
730
|
+
transform?: string;
|
|
731
|
+
[key: string]: unknown;
|
|
732
|
+
};
|
|
733
|
+
view?: Record<string, unknown>;
|
|
734
|
+
}
|
|
735
|
+
/**
|
|
736
|
+
* A Board is a named container of live card nodes.
|
|
737
|
+
* Matches the shape used by LiveCard.Board() in browser/live-cards.js:
|
|
738
|
+
* LiveCard.Board(engine, el, { nodes, positions?, mode, canvas, ... })
|
|
739
|
+
*
|
|
740
|
+
* The `nodes` array contains the card/source JSON objects.
|
|
741
|
+
* Board-level metadata (id, title, settings) is carried through to the
|
|
742
|
+
* generated GraphConfig.
|
|
743
|
+
*/
|
|
744
|
+
interface LiveBoard {
|
|
745
|
+
/** Board identifier */
|
|
746
|
+
id?: string;
|
|
747
|
+
/** Human-readable title */
|
|
748
|
+
title?: string;
|
|
749
|
+
/** The card/source nodes on this board */
|
|
750
|
+
nodes: LiveCard[];
|
|
751
|
+
/** Board display mode (informational — not used by the reactive graph) */
|
|
752
|
+
mode?: 'board' | 'canvas';
|
|
753
|
+
/** Canvas positions keyed by node ID (informational — not used) */
|
|
754
|
+
positions?: Record<string, {
|
|
755
|
+
x?: number;
|
|
756
|
+
y?: number;
|
|
757
|
+
w?: number;
|
|
758
|
+
h?: number;
|
|
759
|
+
}>;
|
|
760
|
+
/** Board-level settings forwarded to GraphConfig.settings */
|
|
761
|
+
settings?: Partial<GraphConfig['settings']>;
|
|
762
|
+
}
|
|
763
|
+
interface LiveCardsToReactiveOptions {
|
|
764
|
+
/** Custom handlers for source nodes (keyed by card ID). */
|
|
765
|
+
sourceHandlers?: Record<string, TaskHandlerFn>;
|
|
766
|
+
/**
|
|
767
|
+
* Default handler factory for source nodes without an explicit handler.
|
|
768
|
+
* Called once per source card during graph construction.
|
|
769
|
+
* If not provided, source nodes without explicit handlers get a noop handler
|
|
770
|
+
* that returns the card's current state.
|
|
771
|
+
*/
|
|
772
|
+
defaultSourceHandler?: (card: LiveCard) => TaskHandlerFn;
|
|
773
|
+
/**
|
|
774
|
+
* Custom handlers for card nodes (keyed by card ID).
|
|
775
|
+
* Overrides the default CardCompute.run() behavior.
|
|
776
|
+
*/
|
|
777
|
+
cardHandlers?: Record<string, TaskHandlerFn>;
|
|
778
|
+
/**
|
|
779
|
+
* If provided, upstream card state is injected into downstream cards
|
|
780
|
+
* before running compute. The key is the upstream card ID and the value
|
|
781
|
+
* is the upstream card's latest state.
|
|
782
|
+
*/
|
|
783
|
+
sharedState?: Map<string, Record<string, unknown>>;
|
|
784
|
+
/** Override reactive graph options (journal, callbacks, etc.) */
|
|
785
|
+
reactiveOptions?: Partial<Omit<ReactiveGraphOptions, 'handlers'>>;
|
|
786
|
+
/** Graph-level settings overrides */
|
|
787
|
+
graphSettings?: Partial<GraphConfig['settings']>;
|
|
788
|
+
/** Execution ID for the reactive graph */
|
|
789
|
+
executionId?: string;
|
|
790
|
+
}
|
|
791
|
+
interface LiveCardsToReactiveResult {
|
|
792
|
+
/** The fully wired reactive graph — ready to push events into. */
|
|
793
|
+
graph: ReactiveGraph;
|
|
794
|
+
/** The generated GraphConfig (for inspection/serialization). */
|
|
795
|
+
config: GraphConfig;
|
|
796
|
+
/** The handler map (for use with validateReactiveGraph). */
|
|
797
|
+
handlers: Record<string, TaskHandlerFn>;
|
|
798
|
+
/** Card lookup by ID (original references). */
|
|
799
|
+
cards: Map<string, LiveCard>;
|
|
800
|
+
/**
|
|
801
|
+
* Shared state map: cardId → latest computed state.
|
|
802
|
+
* Updated automatically by built-in handlers after each task completes.
|
|
803
|
+
* Custom cardHandlers/sourceHandlers can also read upstream data directly
|
|
804
|
+
* from the engine: graph.getState().state.tasks[cardId].data
|
|
805
|
+
*/
|
|
806
|
+
sharedState: Map<string, Record<string, unknown>>;
|
|
807
|
+
}
|
|
808
|
+
/**
|
|
809
|
+
* Convert live card JSONs or a Board into a fully wired ReactiveGraph.
|
|
810
|
+
*
|
|
811
|
+
* Overloads:
|
|
812
|
+
* liveCardsToReactiveGraph(cards[], options?) — from a flat array of cards
|
|
813
|
+
* liveCardsToReactiveGraph(board, options?) — from a LiveBoard object
|
|
814
|
+
*/
|
|
815
|
+
declare function liveCardsToReactiveGraph(input: LiveCard[] | LiveBoard, options?: LiveCardsToReactiveOptions): LiveCardsToReactiveResult;
|
|
816
|
+
|
|
817
|
+
export { DownstreamResult, FileJournal, GraphConfig, GraphEngineStore, GraphEvent, type GraphMutation, type Journal, type LiveBoard, type LiveCard, type LiveCardsToReactiveOptions, type LiveCardsToReactiveResult, LiveGraph, LiveGraphHealth, LiveGraphSnapshot, MemoryJournal, NodeInfo, type ReactiveGraph, type ReactiveGraphOptions, type ReactiveGraphValidationInput, type ResolveCallbackFn, ScheduleResult, type ScriptHandlerOptions, type ShellHandlerOptions, TaskConfig, type TaskHandlerFn, type TaskHandlerInput, type TaskHandlerReturn, UnreachableNodesResult, UnreachableTokensResult, UpstreamResult, type WebhookHandlerOptions, addNode, addProvides, addRequires, applyEvent, applyEvents, computeDataHash, createCallbackHandler, createFireAndForgetHandler, createLiveGraph, createNoopHandler, createReactiveGraph, createScriptHandler, createShellHandler, createWebhookHandler, disableNode, drainTokens, enableNode, getDownstream, getNode, getUnreachableNodes, getUnreachableTokens, getUpstream, injectTokens, inspect, liveCardsToReactiveGraph, mutateGraph, removeNode, removeProvides, removeRequires, resetNode, restore, schedule, snapshot, validateLiveGraph, validateReactiveGraph };
|