@gravito/flux 2.0.0 → 3.0.1
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/dist/bun.d.cts +1 -1
- package/dist/bun.d.ts +1 -1
- package/dist/{chunk-LULCFPIK.js → chunk-3JGQYHUN.js} +4 -2
- package/dist/chunk-3JGQYHUN.js.map +1 -0
- package/dist/{chunk-X3NC7HS4.cjs → chunk-5OXXH442.cjs} +10 -8
- package/dist/chunk-5OXXH442.cjs.map +1 -0
- package/dist/index.cjs +7 -7
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +39 -5
- package/dist/index.d.ts +39 -5
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/index.node.cjs +2 -2
- package/dist/index.node.d.cts +59 -66
- package/dist/index.node.d.ts +59 -66
- package/dist/index.node.js +1 -1
- package/dist/types-CZwYGpou.d.cts +353 -0
- package/dist/types-CZwYGpou.d.ts +353 -0
- package/package.json +3 -3
- package/dist/chunk-LULCFPIK.js.map +0 -1
- package/dist/chunk-X3NC7HS4.cjs.map +0 -1
- package/dist/types-cnIU1O3n.d.cts +0 -250
- package/dist/types-cnIU1O3n.d.ts +0 -250
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core type definitions for @gravito/flux
|
|
3
|
+
*
|
|
4
|
+
* Platform-agnostic workflow engine types.
|
|
5
|
+
*
|
|
6
|
+
* @module @gravito/flux
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Workflow execution status
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Workflow execution status lifecycle states.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
type WorkflowStatus = 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'suspended' | 'rolling_back' | 'rolled_back';
|
|
16
|
+
/**
|
|
17
|
+
* Result of the Flux.wait() signal.
|
|
18
|
+
* Used to pause a workflow until an external event occurs.
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
interface FluxWaitResult {
|
|
22
|
+
__kind: 'flux_wait';
|
|
23
|
+
/** The unique signal name to wait for */
|
|
24
|
+
signal: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Detailed result of a single step's execution.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
interface StepResult<T = unknown> {
|
|
31
|
+
/** Whether the step completed successfully */
|
|
32
|
+
success: boolean;
|
|
33
|
+
/** Data returned by the step handler */
|
|
34
|
+
data?: T;
|
|
35
|
+
/** Error encountered during execution */
|
|
36
|
+
error?: Error;
|
|
37
|
+
/** Processing time in milliseconds */
|
|
38
|
+
duration: number;
|
|
39
|
+
/** Whether the step triggered a suspension */
|
|
40
|
+
suspended?: boolean;
|
|
41
|
+
/** If suspended, the signal name it is waiting for */
|
|
42
|
+
waitingFor?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Audit log entry for a step execution within a workflow instance.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
interface StepExecution {
|
|
49
|
+
/** The unique name of the step */
|
|
50
|
+
name: string;
|
|
51
|
+
/** Current state of this specific step execution */
|
|
52
|
+
status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'suspended' | 'compensated' | 'compensating';
|
|
53
|
+
/** Timestamp when the step was first started */
|
|
54
|
+
startedAt?: Date;
|
|
55
|
+
/** Timestamp when the step reached a terminal state */
|
|
56
|
+
completedAt?: Date;
|
|
57
|
+
/** Timestamp when the step was suspended */
|
|
58
|
+
suspendedAt?: Date;
|
|
59
|
+
/** Timestamp when the step was compensated (undone) */
|
|
60
|
+
compensatedAt?: Date;
|
|
61
|
+
/** The signal name if the step is currently suspended */
|
|
62
|
+
waitingFor?: string;
|
|
63
|
+
/** Execution time in milliseconds */
|
|
64
|
+
duration?: number;
|
|
65
|
+
/** Serialized output data from the step */
|
|
66
|
+
output?: any;
|
|
67
|
+
/** Serialized error message if failed */
|
|
68
|
+
error?: string;
|
|
69
|
+
/** Number of times this step has been retried */
|
|
70
|
+
retries: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Definition of a single functional unit (step) in a workflow.
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
interface StepDefinition<TInput = any, TData = any> {
|
|
77
|
+
/** Unique name for the step within the workflow */
|
|
78
|
+
name: string;
|
|
79
|
+
/**
|
|
80
|
+
* The primary logic of the step.
|
|
81
|
+
* Can return a result, void, or a wait signal.
|
|
82
|
+
*/
|
|
83
|
+
handler: (ctx: WorkflowContext<TInput, TData>) => void | Promise<void | undefined | FluxWaitResult> | undefined | FluxWaitResult;
|
|
84
|
+
/**
|
|
85
|
+
* Logic to undo the effects of this step if a later step fails.
|
|
86
|
+
* Used in long-running transactions (Saga pattern).
|
|
87
|
+
*/
|
|
88
|
+
compensate?: (ctx: WorkflowContext<TInput, TData>) => Promise<void> | void;
|
|
89
|
+
/** Maximum number of automatic retries on error (default: 0) */
|
|
90
|
+
retries?: number;
|
|
91
|
+
/** Maximum execution time in milliseconds before timeout error */
|
|
92
|
+
timeout?: number;
|
|
93
|
+
/** Predicate to determine if the step should be executed or skipped */
|
|
94
|
+
when?: (ctx: WorkflowContext<TInput, TData>) => boolean;
|
|
95
|
+
/**
|
|
96
|
+
* If true, this step is treated as a side-effect that has already happened.
|
|
97
|
+
* It will NOT be re-executed during a workflow resumption/replay if it previously succeeded.
|
|
98
|
+
*/
|
|
99
|
+
commit?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The dynamic state and utility container passed to every step handler.
|
|
103
|
+
* Provides access to input data, shared state, and orchestration metadata.
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
interface WorkflowContext<TInput = unknown, TData = Record<string, unknown>> {
|
|
107
|
+
/** Unique instance ID for this specific workflow run */
|
|
108
|
+
readonly id: string;
|
|
109
|
+
/** Name of the workflow definition */
|
|
110
|
+
readonly name: string;
|
|
111
|
+
/** The initial input data passed to the engine */
|
|
112
|
+
readonly input: TInput;
|
|
113
|
+
/**
|
|
114
|
+
* Mutable state shared across all steps.
|
|
115
|
+
* Steps should write their results here to pass data to subsequent steps.
|
|
116
|
+
*/
|
|
117
|
+
data: TData;
|
|
118
|
+
/** Current execution status of the entire workflow */
|
|
119
|
+
readonly status: WorkflowStatus;
|
|
120
|
+
/** The 0-based index of the currently executing step */
|
|
121
|
+
readonly currentStep: number;
|
|
122
|
+
/** History of all steps executed or currently running in this instance */
|
|
123
|
+
readonly history: StepExecution[];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The complete, serializable state of a workflow instance.
|
|
127
|
+
* Suitable for persistence in a database to support long-running processes.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
interface WorkflowState<TInput = any, TData = any> {
|
|
131
|
+
/** Unique instance ID */
|
|
132
|
+
id: string;
|
|
133
|
+
/** Name of the workflow definition */
|
|
134
|
+
name: string;
|
|
135
|
+
/** Current lifecycle status */
|
|
136
|
+
status: WorkflowStatus;
|
|
137
|
+
/** Original input data */
|
|
138
|
+
input: TInput;
|
|
139
|
+
/** Current accumulated shared data */
|
|
140
|
+
data: TData;
|
|
141
|
+
/** Index of the next step to execute */
|
|
142
|
+
currentStep: number;
|
|
143
|
+
/** Full step execution history */
|
|
144
|
+
history: StepExecution[];
|
|
145
|
+
/** Creation timestamp */
|
|
146
|
+
createdAt: Date;
|
|
147
|
+
/** Last update timestamp */
|
|
148
|
+
updatedAt: Date;
|
|
149
|
+
/** Completion timestamp (if finished) */
|
|
150
|
+
completedAt?: Date;
|
|
151
|
+
/** Final error message if failed */
|
|
152
|
+
error?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The static blueprint defining a workflow's structure and behavior.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
interface WorkflowDefinition<TInput = unknown, TData = Record<string, unknown>> {
|
|
159
|
+
/** Human-readable workflow identifier */
|
|
160
|
+
name: string;
|
|
161
|
+
/** Sequential list of steps to execute */
|
|
162
|
+
steps: StepDefinition<TInput, TData>[];
|
|
163
|
+
/** Optional runtime validator for input data */
|
|
164
|
+
validateInput?: (input: unknown) => input is TInput;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Lightweight, serializable overview of a workflow definition.
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
interface WorkflowDescriptor {
|
|
171
|
+
name: string;
|
|
172
|
+
steps: StepDescriptor[];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Lightweight, serializable overview of a step definition.
|
|
176
|
+
* @public
|
|
177
|
+
*/
|
|
178
|
+
interface StepDescriptor {
|
|
179
|
+
name: string;
|
|
180
|
+
commit: boolean;
|
|
181
|
+
retries?: number;
|
|
182
|
+
timeout?: number;
|
|
183
|
+
hasCondition: boolean;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Interface for workflow persistence adapters.
|
|
187
|
+
* Allows Flux to survive server restarts by saving state to a database.
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
190
|
+
interface WorkflowStorage {
|
|
191
|
+
/** Persist a workflow instance state */
|
|
192
|
+
save(state: WorkflowState): Promise<void>;
|
|
193
|
+
/** Retrieve a workflow instance by its unique ID */
|
|
194
|
+
load(id: string): Promise<WorkflowState | null>;
|
|
195
|
+
/** Search/list workflow instances based on criteria */
|
|
196
|
+
list(filter?: WorkflowFilter): Promise<WorkflowState[]>;
|
|
197
|
+
/** Permanently remove a workflow instance record */
|
|
198
|
+
delete(id: string): Promise<void>;
|
|
199
|
+
/** Prepare the storage backend (e.g., migrations) */
|
|
200
|
+
init?(): Promise<void>;
|
|
201
|
+
/** Gracefully shut down storage connections */
|
|
202
|
+
close?(): Promise<void>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Query parameters for listing workflow instances.
|
|
206
|
+
* @public
|
|
207
|
+
*/
|
|
208
|
+
interface WorkflowFilter {
|
|
209
|
+
/** Filter by workflow definition name */
|
|
210
|
+
name?: string;
|
|
211
|
+
/** Filter by one or more statuses */
|
|
212
|
+
status?: WorkflowStatus | WorkflowStatus[];
|
|
213
|
+
/** Maximum number of records to return */
|
|
214
|
+
limit?: number;
|
|
215
|
+
/** Number of records to skip */
|
|
216
|
+
offset?: number;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Pluggable logger interface for the Flux engine.
|
|
220
|
+
* @public
|
|
221
|
+
*/
|
|
222
|
+
interface FluxLogger {
|
|
223
|
+
debug(message: string, ...args: unknown[]): void;
|
|
224
|
+
info(message: string, ...args: unknown[]): void;
|
|
225
|
+
warn(message: string, ...args: unknown[]): void;
|
|
226
|
+
error(message: string, ...args: unknown[]): void;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Low-level event types emitted during workflow execution for observability.
|
|
230
|
+
*
|
|
231
|
+
* These types identify specific lifecycle events for both workflows and
|
|
232
|
+
* individual steps, enabling fine-grained tracing and monitoring.
|
|
233
|
+
*
|
|
234
|
+
* @public
|
|
235
|
+
* @since 3.0.0
|
|
236
|
+
*/
|
|
237
|
+
type FluxTraceEventType = 'workflow:start' | 'workflow:complete' | 'workflow:error' | 'workflow:rollback_start' | 'workflow:rollback_complete' | 'step:start' | 'step:complete' | 'step:error' | 'step:skipped' | 'step:retry' | 'step:suspend' | 'step:compensate' | 'signal:received';
|
|
238
|
+
/**
|
|
239
|
+
* A discrete event representing a change in workflow or step state.
|
|
240
|
+
*
|
|
241
|
+
* Trace events provide a comprehensive audit log of everything that happens
|
|
242
|
+
* during a workflow execution, including performance metrics (duration),
|
|
243
|
+
* error details, and captured data snapshots.
|
|
244
|
+
*
|
|
245
|
+
* @public
|
|
246
|
+
* @since 3.0.0
|
|
247
|
+
*/
|
|
248
|
+
interface FluxTraceEvent {
|
|
249
|
+
/** The type of event */
|
|
250
|
+
type: FluxTraceEventType;
|
|
251
|
+
/** Epoch timestamp in milliseconds */
|
|
252
|
+
timestamp: number;
|
|
253
|
+
/** Unique ID of the affected workflow instance */
|
|
254
|
+
workflowId: string;
|
|
255
|
+
/** Name of the affected workflow definition */
|
|
256
|
+
workflowName: string;
|
|
257
|
+
/** Name of the step (if applicable) */
|
|
258
|
+
stepName?: string;
|
|
259
|
+
/** 0-based index of the step (if applicable) */
|
|
260
|
+
stepIndex?: number;
|
|
261
|
+
/** Whether the step was a commit step */
|
|
262
|
+
commit?: boolean;
|
|
263
|
+
/** Current retry attempt count */
|
|
264
|
+
retries?: number;
|
|
265
|
+
/** Limit of retries configured */
|
|
266
|
+
maxRetries?: number;
|
|
267
|
+
/** Duration of the operation in milliseconds */
|
|
268
|
+
duration?: number;
|
|
269
|
+
/** Error message (if applicable) */
|
|
270
|
+
error?: string;
|
|
271
|
+
/** The new status reached */
|
|
272
|
+
status?: WorkflowStatus | StepExecution['status'];
|
|
273
|
+
/** Captured input (if tracing enabled) */
|
|
274
|
+
input?: unknown;
|
|
275
|
+
/** Captured partial data (if tracing enabled) */
|
|
276
|
+
data?: Record<string, unknown>;
|
|
277
|
+
/** Additional custom metadata */
|
|
278
|
+
meta?: Record<string, unknown>;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Destination for trace events (e.g., Log file, OpenTelemetry, Zipkin).
|
|
282
|
+
*
|
|
283
|
+
* Implement this interface to create custom observability adapters for Flux.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* class MyTraceSink implements FluxTraceSink {
|
|
288
|
+
* emit(event: FluxTraceEvent) {
|
|
289
|
+
* console.log(`[Flux Trace] ${event.type} in ${event.workflowName}`);
|
|
290
|
+
* }
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @public
|
|
295
|
+
* @since 3.0.0
|
|
296
|
+
*/
|
|
297
|
+
interface FluxTraceSink {
|
|
298
|
+
/**
|
|
299
|
+
* Emit a trace event to the sink.
|
|
300
|
+
*
|
|
301
|
+
* @param event - The trace event to process.
|
|
302
|
+
*/
|
|
303
|
+
emit(event: FluxTraceEvent): void | Promise<void>;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Global configuration for a FluxEngine instance.
|
|
307
|
+
* @public
|
|
308
|
+
*/
|
|
309
|
+
interface FluxConfig {
|
|
310
|
+
/** The persistence adapter to use (default: MemoryStorage) */
|
|
311
|
+
storage?: WorkflowStorage;
|
|
312
|
+
/** The logger to use for engine internals */
|
|
313
|
+
logger?: FluxLogger;
|
|
314
|
+
/** The observability sink for trace events */
|
|
315
|
+
trace?: FluxTraceSink;
|
|
316
|
+
/** Global default retry count for all steps (default: 0) */
|
|
317
|
+
defaultRetries?: number;
|
|
318
|
+
/** Global default timeout in ms for all steps (default: no timeout) */
|
|
319
|
+
defaultTimeout?: number;
|
|
320
|
+
/**
|
|
321
|
+
* Experimental: Attempt to execute independent steps in parallel.
|
|
322
|
+
* (Standard workflows execute sequentially).
|
|
323
|
+
*/
|
|
324
|
+
parallel?: boolean;
|
|
325
|
+
/** Functional hooks for reacting to engine events */
|
|
326
|
+
on?: {
|
|
327
|
+
stepStart?: (step: string, ctx: WorkflowContext) => void;
|
|
328
|
+
stepComplete?: (step: string, ctx: WorkflowContext, result: StepResult) => void;
|
|
329
|
+
stepError?: (step: string, ctx: WorkflowContext, error: Error) => void;
|
|
330
|
+
workflowComplete?: (ctx: WorkflowContext) => void;
|
|
331
|
+
workflowError?: (ctx: WorkflowContext, error: Error) => void;
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The result returned after executing or resuming a workflow.
|
|
336
|
+
* @public
|
|
337
|
+
*/
|
|
338
|
+
interface FluxResult<TData = Record<string, unknown>> {
|
|
339
|
+
/** Unique instance ID of the execution */
|
|
340
|
+
id: string;
|
|
341
|
+
/** Terminal status (e.g., 'completed', 'failed', 'suspended') */
|
|
342
|
+
status: WorkflowStatus;
|
|
343
|
+
/** The final state of the shared data map */
|
|
344
|
+
data: TData;
|
|
345
|
+
/** Full execution history including all step outcomes */
|
|
346
|
+
history: StepExecution[];
|
|
347
|
+
/** Wall-clock time of the execution session in milliseconds */
|
|
348
|
+
duration: number;
|
|
349
|
+
/** The final error that halted the workflow (if applicable) */
|
|
350
|
+
error?: Error;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export type { FluxWaitResult as F, StepDefinition as S, WorkflowDefinition as W, FluxConfig as a, FluxLogger as b, FluxResult as c, FluxTraceEvent as d, FluxTraceEventType as e, FluxTraceSink as f, StepDescriptor as g, StepExecution as h, StepResult as i, WorkflowContext as j, WorkflowDescriptor as k, WorkflowFilter as l, WorkflowState as m, WorkflowStatus as n, WorkflowStorage as o };
|
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Core type definitions for @gravito/flux
|
|
3
|
+
*
|
|
4
|
+
* Platform-agnostic workflow engine types.
|
|
5
|
+
*
|
|
6
|
+
* @module @gravito/flux
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Workflow execution status
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* Workflow execution status lifecycle states.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
15
|
+
type WorkflowStatus = 'pending' | 'running' | 'paused' | 'completed' | 'failed' | 'suspended' | 'rolling_back' | 'rolled_back';
|
|
16
|
+
/**
|
|
17
|
+
* Result of the Flux.wait() signal.
|
|
18
|
+
* Used to pause a workflow until an external event occurs.
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
interface FluxWaitResult {
|
|
22
|
+
__kind: 'flux_wait';
|
|
23
|
+
/** The unique signal name to wait for */
|
|
24
|
+
signal: string;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Detailed result of a single step's execution.
|
|
28
|
+
* @public
|
|
29
|
+
*/
|
|
30
|
+
interface StepResult<T = unknown> {
|
|
31
|
+
/** Whether the step completed successfully */
|
|
32
|
+
success: boolean;
|
|
33
|
+
/** Data returned by the step handler */
|
|
34
|
+
data?: T;
|
|
35
|
+
/** Error encountered during execution */
|
|
36
|
+
error?: Error;
|
|
37
|
+
/** Processing time in milliseconds */
|
|
38
|
+
duration: number;
|
|
39
|
+
/** Whether the step triggered a suspension */
|
|
40
|
+
suspended?: boolean;
|
|
41
|
+
/** If suspended, the signal name it is waiting for */
|
|
42
|
+
waitingFor?: string;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Audit log entry for a step execution within a workflow instance.
|
|
46
|
+
* @public
|
|
47
|
+
*/
|
|
48
|
+
interface StepExecution {
|
|
49
|
+
/** The unique name of the step */
|
|
50
|
+
name: string;
|
|
51
|
+
/** Current state of this specific step execution */
|
|
52
|
+
status: 'pending' | 'running' | 'completed' | 'failed' | 'skipped' | 'suspended' | 'compensated' | 'compensating';
|
|
53
|
+
/** Timestamp when the step was first started */
|
|
54
|
+
startedAt?: Date;
|
|
55
|
+
/** Timestamp when the step reached a terminal state */
|
|
56
|
+
completedAt?: Date;
|
|
57
|
+
/** Timestamp when the step was suspended */
|
|
58
|
+
suspendedAt?: Date;
|
|
59
|
+
/** Timestamp when the step was compensated (undone) */
|
|
60
|
+
compensatedAt?: Date;
|
|
61
|
+
/** The signal name if the step is currently suspended */
|
|
62
|
+
waitingFor?: string;
|
|
63
|
+
/** Execution time in milliseconds */
|
|
64
|
+
duration?: number;
|
|
65
|
+
/** Serialized output data from the step */
|
|
66
|
+
output?: any;
|
|
67
|
+
/** Serialized error message if failed */
|
|
68
|
+
error?: string;
|
|
69
|
+
/** Number of times this step has been retried */
|
|
70
|
+
retries: number;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Definition of a single functional unit (step) in a workflow.
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
interface StepDefinition<TInput = any, TData = any> {
|
|
77
|
+
/** Unique name for the step within the workflow */
|
|
78
|
+
name: string;
|
|
79
|
+
/**
|
|
80
|
+
* The primary logic of the step.
|
|
81
|
+
* Can return a result, void, or a wait signal.
|
|
82
|
+
*/
|
|
83
|
+
handler: (ctx: WorkflowContext<TInput, TData>) => void | Promise<void | undefined | FluxWaitResult> | undefined | FluxWaitResult;
|
|
84
|
+
/**
|
|
85
|
+
* Logic to undo the effects of this step if a later step fails.
|
|
86
|
+
* Used in long-running transactions (Saga pattern).
|
|
87
|
+
*/
|
|
88
|
+
compensate?: (ctx: WorkflowContext<TInput, TData>) => Promise<void> | void;
|
|
89
|
+
/** Maximum number of automatic retries on error (default: 0) */
|
|
90
|
+
retries?: number;
|
|
91
|
+
/** Maximum execution time in milliseconds before timeout error */
|
|
92
|
+
timeout?: number;
|
|
93
|
+
/** Predicate to determine if the step should be executed or skipped */
|
|
94
|
+
when?: (ctx: WorkflowContext<TInput, TData>) => boolean;
|
|
95
|
+
/**
|
|
96
|
+
* If true, this step is treated as a side-effect that has already happened.
|
|
97
|
+
* It will NOT be re-executed during a workflow resumption/replay if it previously succeeded.
|
|
98
|
+
*/
|
|
99
|
+
commit?: boolean;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* The dynamic state and utility container passed to every step handler.
|
|
103
|
+
* Provides access to input data, shared state, and orchestration metadata.
|
|
104
|
+
* @public
|
|
105
|
+
*/
|
|
106
|
+
interface WorkflowContext<TInput = unknown, TData = Record<string, unknown>> {
|
|
107
|
+
/** Unique instance ID for this specific workflow run */
|
|
108
|
+
readonly id: string;
|
|
109
|
+
/** Name of the workflow definition */
|
|
110
|
+
readonly name: string;
|
|
111
|
+
/** The initial input data passed to the engine */
|
|
112
|
+
readonly input: TInput;
|
|
113
|
+
/**
|
|
114
|
+
* Mutable state shared across all steps.
|
|
115
|
+
* Steps should write their results here to pass data to subsequent steps.
|
|
116
|
+
*/
|
|
117
|
+
data: TData;
|
|
118
|
+
/** Current execution status of the entire workflow */
|
|
119
|
+
readonly status: WorkflowStatus;
|
|
120
|
+
/** The 0-based index of the currently executing step */
|
|
121
|
+
readonly currentStep: number;
|
|
122
|
+
/** History of all steps executed or currently running in this instance */
|
|
123
|
+
readonly history: StepExecution[];
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* The complete, serializable state of a workflow instance.
|
|
127
|
+
* Suitable for persistence in a database to support long-running processes.
|
|
128
|
+
* @public
|
|
129
|
+
*/
|
|
130
|
+
interface WorkflowState<TInput = any, TData = any> {
|
|
131
|
+
/** Unique instance ID */
|
|
132
|
+
id: string;
|
|
133
|
+
/** Name of the workflow definition */
|
|
134
|
+
name: string;
|
|
135
|
+
/** Current lifecycle status */
|
|
136
|
+
status: WorkflowStatus;
|
|
137
|
+
/** Original input data */
|
|
138
|
+
input: TInput;
|
|
139
|
+
/** Current accumulated shared data */
|
|
140
|
+
data: TData;
|
|
141
|
+
/** Index of the next step to execute */
|
|
142
|
+
currentStep: number;
|
|
143
|
+
/** Full step execution history */
|
|
144
|
+
history: StepExecution[];
|
|
145
|
+
/** Creation timestamp */
|
|
146
|
+
createdAt: Date;
|
|
147
|
+
/** Last update timestamp */
|
|
148
|
+
updatedAt: Date;
|
|
149
|
+
/** Completion timestamp (if finished) */
|
|
150
|
+
completedAt?: Date;
|
|
151
|
+
/** Final error message if failed */
|
|
152
|
+
error?: string;
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The static blueprint defining a workflow's structure and behavior.
|
|
156
|
+
* @public
|
|
157
|
+
*/
|
|
158
|
+
interface WorkflowDefinition<TInput = unknown, TData = Record<string, unknown>> {
|
|
159
|
+
/** Human-readable workflow identifier */
|
|
160
|
+
name: string;
|
|
161
|
+
/** Sequential list of steps to execute */
|
|
162
|
+
steps: StepDefinition<TInput, TData>[];
|
|
163
|
+
/** Optional runtime validator for input data */
|
|
164
|
+
validateInput?: (input: unknown) => input is TInput;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Lightweight, serializable overview of a workflow definition.
|
|
168
|
+
* @public
|
|
169
|
+
*/
|
|
170
|
+
interface WorkflowDescriptor {
|
|
171
|
+
name: string;
|
|
172
|
+
steps: StepDescriptor[];
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Lightweight, serializable overview of a step definition.
|
|
176
|
+
* @public
|
|
177
|
+
*/
|
|
178
|
+
interface StepDescriptor {
|
|
179
|
+
name: string;
|
|
180
|
+
commit: boolean;
|
|
181
|
+
retries?: number;
|
|
182
|
+
timeout?: number;
|
|
183
|
+
hasCondition: boolean;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Interface for workflow persistence adapters.
|
|
187
|
+
* Allows Flux to survive server restarts by saving state to a database.
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
190
|
+
interface WorkflowStorage {
|
|
191
|
+
/** Persist a workflow instance state */
|
|
192
|
+
save(state: WorkflowState): Promise<void>;
|
|
193
|
+
/** Retrieve a workflow instance by its unique ID */
|
|
194
|
+
load(id: string): Promise<WorkflowState | null>;
|
|
195
|
+
/** Search/list workflow instances based on criteria */
|
|
196
|
+
list(filter?: WorkflowFilter): Promise<WorkflowState[]>;
|
|
197
|
+
/** Permanently remove a workflow instance record */
|
|
198
|
+
delete(id: string): Promise<void>;
|
|
199
|
+
/** Prepare the storage backend (e.g., migrations) */
|
|
200
|
+
init?(): Promise<void>;
|
|
201
|
+
/** Gracefully shut down storage connections */
|
|
202
|
+
close?(): Promise<void>;
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* Query parameters for listing workflow instances.
|
|
206
|
+
* @public
|
|
207
|
+
*/
|
|
208
|
+
interface WorkflowFilter {
|
|
209
|
+
/** Filter by workflow definition name */
|
|
210
|
+
name?: string;
|
|
211
|
+
/** Filter by one or more statuses */
|
|
212
|
+
status?: WorkflowStatus | WorkflowStatus[];
|
|
213
|
+
/** Maximum number of records to return */
|
|
214
|
+
limit?: number;
|
|
215
|
+
/** Number of records to skip */
|
|
216
|
+
offset?: number;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Pluggable logger interface for the Flux engine.
|
|
220
|
+
* @public
|
|
221
|
+
*/
|
|
222
|
+
interface FluxLogger {
|
|
223
|
+
debug(message: string, ...args: unknown[]): void;
|
|
224
|
+
info(message: string, ...args: unknown[]): void;
|
|
225
|
+
warn(message: string, ...args: unknown[]): void;
|
|
226
|
+
error(message: string, ...args: unknown[]): void;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Low-level event types emitted during workflow execution for observability.
|
|
230
|
+
*
|
|
231
|
+
* These types identify specific lifecycle events for both workflows and
|
|
232
|
+
* individual steps, enabling fine-grained tracing and monitoring.
|
|
233
|
+
*
|
|
234
|
+
* @public
|
|
235
|
+
* @since 3.0.0
|
|
236
|
+
*/
|
|
237
|
+
type FluxTraceEventType = 'workflow:start' | 'workflow:complete' | 'workflow:error' | 'workflow:rollback_start' | 'workflow:rollback_complete' | 'step:start' | 'step:complete' | 'step:error' | 'step:skipped' | 'step:retry' | 'step:suspend' | 'step:compensate' | 'signal:received';
|
|
238
|
+
/**
|
|
239
|
+
* A discrete event representing a change in workflow or step state.
|
|
240
|
+
*
|
|
241
|
+
* Trace events provide a comprehensive audit log of everything that happens
|
|
242
|
+
* during a workflow execution, including performance metrics (duration),
|
|
243
|
+
* error details, and captured data snapshots.
|
|
244
|
+
*
|
|
245
|
+
* @public
|
|
246
|
+
* @since 3.0.0
|
|
247
|
+
*/
|
|
248
|
+
interface FluxTraceEvent {
|
|
249
|
+
/** The type of event */
|
|
250
|
+
type: FluxTraceEventType;
|
|
251
|
+
/** Epoch timestamp in milliseconds */
|
|
252
|
+
timestamp: number;
|
|
253
|
+
/** Unique ID of the affected workflow instance */
|
|
254
|
+
workflowId: string;
|
|
255
|
+
/** Name of the affected workflow definition */
|
|
256
|
+
workflowName: string;
|
|
257
|
+
/** Name of the step (if applicable) */
|
|
258
|
+
stepName?: string;
|
|
259
|
+
/** 0-based index of the step (if applicable) */
|
|
260
|
+
stepIndex?: number;
|
|
261
|
+
/** Whether the step was a commit step */
|
|
262
|
+
commit?: boolean;
|
|
263
|
+
/** Current retry attempt count */
|
|
264
|
+
retries?: number;
|
|
265
|
+
/** Limit of retries configured */
|
|
266
|
+
maxRetries?: number;
|
|
267
|
+
/** Duration of the operation in milliseconds */
|
|
268
|
+
duration?: number;
|
|
269
|
+
/** Error message (if applicable) */
|
|
270
|
+
error?: string;
|
|
271
|
+
/** The new status reached */
|
|
272
|
+
status?: WorkflowStatus | StepExecution['status'];
|
|
273
|
+
/** Captured input (if tracing enabled) */
|
|
274
|
+
input?: unknown;
|
|
275
|
+
/** Captured partial data (if tracing enabled) */
|
|
276
|
+
data?: Record<string, unknown>;
|
|
277
|
+
/** Additional custom metadata */
|
|
278
|
+
meta?: Record<string, unknown>;
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Destination for trace events (e.g., Log file, OpenTelemetry, Zipkin).
|
|
282
|
+
*
|
|
283
|
+
* Implement this interface to create custom observability adapters for Flux.
|
|
284
|
+
*
|
|
285
|
+
* @example
|
|
286
|
+
* ```typescript
|
|
287
|
+
* class MyTraceSink implements FluxTraceSink {
|
|
288
|
+
* emit(event: FluxTraceEvent) {
|
|
289
|
+
* console.log(`[Flux Trace] ${event.type} in ${event.workflowName}`);
|
|
290
|
+
* }
|
|
291
|
+
* }
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @public
|
|
295
|
+
* @since 3.0.0
|
|
296
|
+
*/
|
|
297
|
+
interface FluxTraceSink {
|
|
298
|
+
/**
|
|
299
|
+
* Emit a trace event to the sink.
|
|
300
|
+
*
|
|
301
|
+
* @param event - The trace event to process.
|
|
302
|
+
*/
|
|
303
|
+
emit(event: FluxTraceEvent): void | Promise<void>;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Global configuration for a FluxEngine instance.
|
|
307
|
+
* @public
|
|
308
|
+
*/
|
|
309
|
+
interface FluxConfig {
|
|
310
|
+
/** The persistence adapter to use (default: MemoryStorage) */
|
|
311
|
+
storage?: WorkflowStorage;
|
|
312
|
+
/** The logger to use for engine internals */
|
|
313
|
+
logger?: FluxLogger;
|
|
314
|
+
/** The observability sink for trace events */
|
|
315
|
+
trace?: FluxTraceSink;
|
|
316
|
+
/** Global default retry count for all steps (default: 0) */
|
|
317
|
+
defaultRetries?: number;
|
|
318
|
+
/** Global default timeout in ms for all steps (default: no timeout) */
|
|
319
|
+
defaultTimeout?: number;
|
|
320
|
+
/**
|
|
321
|
+
* Experimental: Attempt to execute independent steps in parallel.
|
|
322
|
+
* (Standard workflows execute sequentially).
|
|
323
|
+
*/
|
|
324
|
+
parallel?: boolean;
|
|
325
|
+
/** Functional hooks for reacting to engine events */
|
|
326
|
+
on?: {
|
|
327
|
+
stepStart?: (step: string, ctx: WorkflowContext) => void;
|
|
328
|
+
stepComplete?: (step: string, ctx: WorkflowContext, result: StepResult) => void;
|
|
329
|
+
stepError?: (step: string, ctx: WorkflowContext, error: Error) => void;
|
|
330
|
+
workflowComplete?: (ctx: WorkflowContext) => void;
|
|
331
|
+
workflowError?: (ctx: WorkflowContext, error: Error) => void;
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* The result returned after executing or resuming a workflow.
|
|
336
|
+
* @public
|
|
337
|
+
*/
|
|
338
|
+
interface FluxResult<TData = Record<string, unknown>> {
|
|
339
|
+
/** Unique instance ID of the execution */
|
|
340
|
+
id: string;
|
|
341
|
+
/** Terminal status (e.g., 'completed', 'failed', 'suspended') */
|
|
342
|
+
status: WorkflowStatus;
|
|
343
|
+
/** The final state of the shared data map */
|
|
344
|
+
data: TData;
|
|
345
|
+
/** Full execution history including all step outcomes */
|
|
346
|
+
history: StepExecution[];
|
|
347
|
+
/** Wall-clock time of the execution session in milliseconds */
|
|
348
|
+
duration: number;
|
|
349
|
+
/** The final error that halted the workflow (if applicable) */
|
|
350
|
+
error?: Error;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
export type { FluxWaitResult as F, StepDefinition as S, WorkflowDefinition as W, FluxConfig as a, FluxLogger as b, FluxResult as c, FluxTraceEvent as d, FluxTraceEventType as e, FluxTraceSink as f, StepDescriptor as g, StepExecution as h, StepResult as i, WorkflowContext as j, WorkflowDescriptor as k, WorkflowFilter as l, WorkflowState as m, WorkflowStatus as n, WorkflowStorage as o };
|