@multiplier-labs/stepflow 0.3.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/.github/workflows/publish.yml +30 -0
- package/README.md +408 -0
- package/dist/chunk-FQCT3I2A.js +1512 -0
- package/dist/chunk-FQCT3I2A.js.map +1 -0
- package/dist/chunk-GXY66V7Q.js +111 -0
- package/dist/chunk-GXY66V7Q.js.map +1 -0
- package/dist/chunk-U5QGN3RT.js +715 -0
- package/dist/chunk-U5QGN3RT.js.map +1 -0
- package/dist/chunk-XCF4PLN7.js +575 -0
- package/dist/chunk-XCF4PLN7.js.map +1 -0
- package/dist/events/index.d.ts +346 -0
- package/dist/events/index.js +11 -0
- package/dist/events/index.js.map +1 -0
- package/dist/index-CPfGsmrp.d.ts +567 -0
- package/dist/index.d.ts +748 -0
- package/dist/index.js +1707 -0
- package/dist/index.js.map +1 -0
- package/dist/scheduler/index.d.ts +6 -0
- package/dist/scheduler/index.js +12 -0
- package/dist/scheduler/index.js.map +1 -0
- package/dist/storage/index.d.ts +402 -0
- package/dist/storage/index.js +13 -0
- package/dist/storage/index.js.map +1 -0
- package/dist/types-BVShq7zT.d.ts +150 -0
- package/dist/types-BlwFEGzj.d.ts +276 -0
- package/dist/types-DzkEqnQJ.d.ts +75 -0
- package/docs/github-packages-setup.md +97 -0
- package/docs/stepflow-api-reference.md +1662 -0
- package/package.json +85 -0
- package/src/core/engine.test.ts +1421 -0
- package/src/core/engine.ts +717 -0
- package/src/core/index.ts +7 -0
- package/src/core/orchestrator.ts +590 -0
- package/src/core/types.ts +242 -0
- package/src/events/index.ts +8 -0
- package/src/events/memory.test.ts +109 -0
- package/src/events/memory.ts +95 -0
- package/src/events/socketio.test.ts +428 -0
- package/src/events/socketio.ts +269 -0
- package/src/events/types.ts +121 -0
- package/src/events/webhook.test.ts +695 -0
- package/src/events/webhook.ts +591 -0
- package/src/index.ts +270 -0
- package/src/planning/index.ts +52 -0
- package/src/planning/planner.ts +601 -0
- package/src/planning/planning.test.ts +790 -0
- package/src/planning/registry.test.ts +445 -0
- package/src/planning/registry.ts +369 -0
- package/src/planning/types.ts +570 -0
- package/src/scheduler/cron.test.ts +480 -0
- package/src/scheduler/cron.ts +449 -0
- package/src/scheduler/index.ts +18 -0
- package/src/scheduler/postgres-persistence.test.ts +906 -0
- package/src/scheduler/postgres-persistence.ts +546 -0
- package/src/scheduler/sqlite-persistence.test.ts +235 -0
- package/src/scheduler/sqlite-persistence.ts +268 -0
- package/src/scheduler/types.ts +78 -0
- package/src/storage/index.ts +27 -0
- package/src/storage/memory.test.ts +399 -0
- package/src/storage/memory.ts +248 -0
- package/src/storage/postgres.ts +1550 -0
- package/src/storage/sqlite.test.ts +593 -0
- package/src/storage/sqlite.ts +662 -0
- package/src/storage/types.ts +368 -0
- package/src/utils/errors.test.ts +152 -0
- package/src/utils/errors.ts +191 -0
- package/src/utils/id.ts +21 -0
- package/src/utils/index.ts +8 -0
- package/src/utils/logger.test.ts +127 -0
- package/src/utils/logger.ts +96 -0
- package/src/utils/postgres-deps.ts +63 -0
- package/src/utils/retry.test.ts +197 -0
- package/src/utils/retry.ts +121 -0
- package/tsconfig.json +21 -0
- package/tsup.config.ts +14 -0
- package/vitest.config.ts +9 -0
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
import { i as StorageAdapter, j as WorkflowRunRecord, d as ListRunsOptions, P as PaginatedResult, k as WorkflowRunStepRecord, W as WorkflowEventRecord, L as ListEventsOptions, C as CreateRunInput, U as UpdateRunInput, e as StepResult } from '../types-BlwFEGzj.js';
|
|
2
|
+
export { E as ExtendedListRunsOptions, a as ExtendedRunStatus, b as ExtendedStepStatus, c as ExtendedWorkflowRunRecord, S as StepRecord, f as StepflowDatabase, g as StepflowRunsTable, h as StepflowStepResultsTable, l as WorkflowStorage } from '../types-BlwFEGzj.js';
|
|
3
|
+
import Database from 'better-sqlite3';
|
|
4
|
+
import { Pool, PoolConfig } from 'pg';
|
|
5
|
+
import '../types-BVShq7zT.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* In-memory storage adapter for development and testing.
|
|
9
|
+
* All data is lost when the process exits.
|
|
10
|
+
*
|
|
11
|
+
* NOTE: This adapter is intended for development and testing only. It stores
|
|
12
|
+
* all state in-process with no persistence or multi-process safety guarantees.
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* In-memory implementation of StorageAdapter.
|
|
17
|
+
* Useful for development, testing, and lightweight deployments.
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* import { WorkflowEngine } from 'stepflow';
|
|
22
|
+
* import { MemoryStorageAdapter } from 'stepflow/storage';
|
|
23
|
+
*
|
|
24
|
+
* const storage = new MemoryStorageAdapter();
|
|
25
|
+
* const engine = new WorkflowEngine({ storage });
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
declare class MemoryStorageAdapter implements StorageAdapter {
|
|
29
|
+
private runs;
|
|
30
|
+
private steps;
|
|
31
|
+
private events;
|
|
32
|
+
/** Create and persist a new workflow run record. */
|
|
33
|
+
createRun(run: Omit<WorkflowRunRecord, "id" | "createdAt">): Promise<WorkflowRunRecord>;
|
|
34
|
+
/** Retrieve a workflow run by ID, or null if not found. */
|
|
35
|
+
getRun(runId: string): Promise<WorkflowRunRecord | null>;
|
|
36
|
+
/** Apply partial updates to an existing workflow run. No-op if the run does not exist. */
|
|
37
|
+
updateRun(runId: string, updates: Partial<WorkflowRunRecord>): Promise<void>;
|
|
38
|
+
/** List workflow runs with optional filtering, sorting, and pagination. */
|
|
39
|
+
listRuns(options?: ListRunsOptions): Promise<PaginatedResult<WorkflowRunRecord>>;
|
|
40
|
+
/** Create and persist a new step execution record. */
|
|
41
|
+
createStep(step: Omit<WorkflowRunStepRecord, "id">): Promise<WorkflowRunStepRecord>;
|
|
42
|
+
/** Retrieve a step record by ID, or null if not found. */
|
|
43
|
+
getStep(stepId: string): Promise<WorkflowRunStepRecord | null>;
|
|
44
|
+
/** Apply partial updates to an existing step record. No-op if the step does not exist. */
|
|
45
|
+
updateStep(stepId: string, updates: Partial<WorkflowRunStepRecord>): Promise<void>;
|
|
46
|
+
/** Retrieve all step records for a workflow run, ordered by start time ascending. */
|
|
47
|
+
getStepsForRun(runId: string): Promise<WorkflowRunStepRecord[]>;
|
|
48
|
+
/** Persist a workflow event record. */
|
|
49
|
+
saveEvent(event: Omit<WorkflowEventRecord, "id">): Promise<void>;
|
|
50
|
+
/** Retrieve events for a workflow run with optional filtering and pagination. */
|
|
51
|
+
getEventsForRun(runId: string, options?: ListEventsOptions): Promise<WorkflowEventRecord[]>;
|
|
52
|
+
/** Delete runs (and their associated steps and events) created before the given date. Returns the number of deleted runs. */
|
|
53
|
+
deleteOldRuns(olderThan: Date): Promise<number>;
|
|
54
|
+
/**
|
|
55
|
+
* Clear all stored data. Useful for testing.
|
|
56
|
+
*/
|
|
57
|
+
clear(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Get counts of stored records. Useful for testing.
|
|
60
|
+
*/
|
|
61
|
+
getStats(): {
|
|
62
|
+
runs: number;
|
|
63
|
+
steps: number;
|
|
64
|
+
events: number;
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* SQLite storage adapter using better-sqlite3.
|
|
70
|
+
*
|
|
71
|
+
* Provides durable persistence for workflow runs, steps, and events.
|
|
72
|
+
*/
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Configuration options for the SQLite storage adapter.
|
|
76
|
+
*/
|
|
77
|
+
interface SQLiteStorageConfig {
|
|
78
|
+
/**
|
|
79
|
+
* The better-sqlite3 database instance.
|
|
80
|
+
* Can be an in-memory database (`:memory:`) or a file path.
|
|
81
|
+
*/
|
|
82
|
+
db: Database.Database;
|
|
83
|
+
/**
|
|
84
|
+
* Whether to automatically create tables on initialization.
|
|
85
|
+
* Default: true
|
|
86
|
+
*/
|
|
87
|
+
autoCreateTables?: boolean;
|
|
88
|
+
/**
|
|
89
|
+
* @deprecated This option is not currently supported and will be ignored.
|
|
90
|
+
* Table names are always prefixed with 'workflow_'.
|
|
91
|
+
*/
|
|
92
|
+
tablePrefix?: string;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* SQLite implementation of StorageAdapter using better-sqlite3.
|
|
96
|
+
*
|
|
97
|
+
* Features:
|
|
98
|
+
* - Synchronous operations (better-sqlite3 is sync by design)
|
|
99
|
+
* - Automatic table creation
|
|
100
|
+
* - Transaction support
|
|
101
|
+
* - Customizable table prefix
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```typescript
|
|
105
|
+
* import Database from 'better-sqlite3';
|
|
106
|
+
* import { SQLiteStorageAdapter } from 'stepflow/storage';
|
|
107
|
+
*
|
|
108
|
+
* const db = new Database('./workflows.db');
|
|
109
|
+
* const storage = new SQLiteStorageAdapter({ db });
|
|
110
|
+
*
|
|
111
|
+
* const engine = new WorkflowEngine({ storage });
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare class SQLiteStorageAdapter implements StorageAdapter {
|
|
115
|
+
private db;
|
|
116
|
+
private prefix;
|
|
117
|
+
private stmts;
|
|
118
|
+
constructor(config: SQLiteStorageConfig);
|
|
119
|
+
/**
|
|
120
|
+
* Create the workflow tables if they don't exist.
|
|
121
|
+
*/
|
|
122
|
+
private createTables;
|
|
123
|
+
/**
|
|
124
|
+
* Prepare all SQL statements for better performance.
|
|
125
|
+
*/
|
|
126
|
+
private prepareStatements;
|
|
127
|
+
createRun(run: Omit<WorkflowRunRecord, "id" | "createdAt">): Promise<WorkflowRunRecord>;
|
|
128
|
+
getRun(runId: string): Promise<WorkflowRunRecord | null>;
|
|
129
|
+
updateRun(runId: string, updates: Partial<WorkflowRunRecord>): Promise<void>;
|
|
130
|
+
listRuns(options?: ListRunsOptions): Promise<PaginatedResult<WorkflowRunRecord>>;
|
|
131
|
+
createStep(step: Omit<WorkflowRunStepRecord, "id">): Promise<WorkflowRunStepRecord>;
|
|
132
|
+
getStep(stepId: string): Promise<WorkflowRunStepRecord | null>;
|
|
133
|
+
updateStep(stepId: string, updates: Partial<WorkflowRunStepRecord>): Promise<void>;
|
|
134
|
+
getStepsForRun(runId: string): Promise<WorkflowRunStepRecord[]>;
|
|
135
|
+
saveEvent(event: Omit<WorkflowEventRecord, "id">): Promise<void>;
|
|
136
|
+
getEventsForRun(runId: string, options?: ListEventsOptions): Promise<WorkflowEventRecord[]>;
|
|
137
|
+
/**
|
|
138
|
+
* Execute a function within a database transaction (async interface).
|
|
139
|
+
*
|
|
140
|
+
* @deprecated Use `transactionSync()` instead. This method only works when
|
|
141
|
+
* the callback performs purely synchronous operations wrapped in async/await.
|
|
142
|
+
* If the callback awaits real async I/O (network, timers, etc.), it will
|
|
143
|
+
* throw an error. `transactionSync()` makes the synchronous requirement explicit.
|
|
144
|
+
*/
|
|
145
|
+
transaction<T>(fn: (tx: StorageAdapter) => Promise<T>): Promise<T>;
|
|
146
|
+
/**
|
|
147
|
+
* Execute a synchronous transaction (preferred for better-sqlite3).
|
|
148
|
+
* Use this when you need transaction guarantees.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* ```typescript
|
|
152
|
+
* storage.transactionSync(() => {
|
|
153
|
+
* // Multiple operations in a single transaction
|
|
154
|
+
* const run = storage.createRunSync(...);
|
|
155
|
+
* storage.updateRunSync(run.id, ...);
|
|
156
|
+
* });
|
|
157
|
+
* ```
|
|
158
|
+
*/
|
|
159
|
+
transactionSync<T>(fn: () => T): T;
|
|
160
|
+
/**
|
|
161
|
+
* Delete runs older than the specified date.
|
|
162
|
+
* Also deletes associated steps and events (via CASCADE).
|
|
163
|
+
*/
|
|
164
|
+
deleteOldRuns(olderThan: Date): Promise<number>;
|
|
165
|
+
/**
|
|
166
|
+
* Get all runs that were interrupted (status is 'running' or 'queued').
|
|
167
|
+
* These runs can potentially be resumed.
|
|
168
|
+
*/
|
|
169
|
+
getInterruptedRuns(): Promise<WorkflowRunRecord[]>;
|
|
170
|
+
/**
|
|
171
|
+
* Get the last completed step for a run.
|
|
172
|
+
* Useful for resuming from a checkpoint.
|
|
173
|
+
*/
|
|
174
|
+
getLastCompletedStep(runId: string): Promise<WorkflowRunStepRecord | null>;
|
|
175
|
+
private mapRunRow;
|
|
176
|
+
private mapStepRow;
|
|
177
|
+
private mapEventRow;
|
|
178
|
+
/**
|
|
179
|
+
* Close the database connection.
|
|
180
|
+
*/
|
|
181
|
+
close(): void;
|
|
182
|
+
/**
|
|
183
|
+
* Get database statistics.
|
|
184
|
+
*/
|
|
185
|
+
getStats(): {
|
|
186
|
+
runs: number;
|
|
187
|
+
steps: number;
|
|
188
|
+
events: number;
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* PostgreSQL storage adapter using Kysely.
|
|
194
|
+
*
|
|
195
|
+
* Provides durable persistence for workflow runs, steps, and events
|
|
196
|
+
* with support for distributed deployments and connection pooling.
|
|
197
|
+
*/
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Configuration options for the PostgreSQL storage adapter.
|
|
201
|
+
*/
|
|
202
|
+
interface PostgresStorageConfig {
|
|
203
|
+
/**
|
|
204
|
+
* PostgreSQL connection string.
|
|
205
|
+
* Example: "postgresql://user:pass@localhost:5432/dbname"
|
|
206
|
+
*/
|
|
207
|
+
connectionString?: string;
|
|
208
|
+
/**
|
|
209
|
+
* Existing pg.Pool instance for connection sharing with application.
|
|
210
|
+
* If provided, the adapter will not close this pool on close().
|
|
211
|
+
*/
|
|
212
|
+
pool?: Pool;
|
|
213
|
+
/**
|
|
214
|
+
* Pool configuration options (if not providing pool or connectionString).
|
|
215
|
+
*/
|
|
216
|
+
poolConfig?: PoolConfig;
|
|
217
|
+
/**
|
|
218
|
+
* Schema name for Stepflow tables.
|
|
219
|
+
* @default 'public'
|
|
220
|
+
*/
|
|
221
|
+
schema?: string;
|
|
222
|
+
/**
|
|
223
|
+
* Automatically create tables on initialize().
|
|
224
|
+
* @default true
|
|
225
|
+
*/
|
|
226
|
+
autoMigrate?: boolean;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* PostgreSQL implementation of StorageAdapter using Kysely.
|
|
230
|
+
*
|
|
231
|
+
* Features:
|
|
232
|
+
* - Connection pooling (shared or dedicated)
|
|
233
|
+
* - Automatic table creation
|
|
234
|
+
* - Transaction support
|
|
235
|
+
* - Atomic dequeue operations for distributed workers
|
|
236
|
+
* - JSONB storage for flexible data
|
|
237
|
+
*
|
|
238
|
+
* @example
|
|
239
|
+
* ```typescript
|
|
240
|
+
* import { PostgresStorageAdapter } from 'stepflow/storage';
|
|
241
|
+
*
|
|
242
|
+
* const storage = new PostgresStorageAdapter({
|
|
243
|
+
* connectionString: process.env.DATABASE_URL,
|
|
244
|
+
* });
|
|
245
|
+
*
|
|
246
|
+
* await storage.initialize();
|
|
247
|
+
*
|
|
248
|
+
* const engine = new WorkflowEngine({ storage });
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* @example Sharing connection pool
|
|
252
|
+
* ```typescript
|
|
253
|
+
* import pg from 'pg';
|
|
254
|
+
* import { PostgresStorageAdapter } from 'stepflow/storage';
|
|
255
|
+
*
|
|
256
|
+
* // Application's existing pool
|
|
257
|
+
* const pool = new pg.Pool({
|
|
258
|
+
* connectionString: process.env.DATABASE_URL,
|
|
259
|
+
* max: 20,
|
|
260
|
+
* });
|
|
261
|
+
*
|
|
262
|
+
* // Share with Stepflow
|
|
263
|
+
* const storage = new PostgresStorageAdapter({ pool });
|
|
264
|
+
* ```
|
|
265
|
+
*/
|
|
266
|
+
declare class PostgresStorageAdapter implements StorageAdapter {
|
|
267
|
+
private db;
|
|
268
|
+
private pool;
|
|
269
|
+
private ownsPool;
|
|
270
|
+
private schema;
|
|
271
|
+
private autoMigrate;
|
|
272
|
+
private initialized;
|
|
273
|
+
private config;
|
|
274
|
+
constructor(config: PostgresStorageConfig);
|
|
275
|
+
/**
|
|
276
|
+
* Get a schema-scoped query builder.
|
|
277
|
+
* All queries MUST use this instead of this.db directly to respect config.schema.
|
|
278
|
+
*/
|
|
279
|
+
private get qb();
|
|
280
|
+
private ensureInitialized;
|
|
281
|
+
/**
|
|
282
|
+
* Initialize the storage adapter.
|
|
283
|
+
* Creates tables if autoMigrate is enabled.
|
|
284
|
+
*/
|
|
285
|
+
initialize(): Promise<void>;
|
|
286
|
+
/**
|
|
287
|
+
* Close the database connection.
|
|
288
|
+
* Only closes the pool if it was created by this adapter.
|
|
289
|
+
*/
|
|
290
|
+
close(): Promise<void>;
|
|
291
|
+
/**
|
|
292
|
+
* Create the workflow tables if they don't exist.
|
|
293
|
+
*/
|
|
294
|
+
private createTables;
|
|
295
|
+
/**
|
|
296
|
+
* Create a new workflow run.
|
|
297
|
+
* Supports both legacy and new CreateRunInput interfaces.
|
|
298
|
+
*/
|
|
299
|
+
createRun(run: CreateRunInput | Omit<WorkflowRunRecord, "id" | "createdAt">): Promise<WorkflowRunRecord>;
|
|
300
|
+
getRun(runId: string): Promise<WorkflowRunRecord | null>;
|
|
301
|
+
/**
|
|
302
|
+
* Update a workflow run.
|
|
303
|
+
* Supports both legacy Partial<WorkflowRunRecord> and new UpdateRunInput interfaces.
|
|
304
|
+
*/
|
|
305
|
+
updateRun(runId: string, updates: UpdateRunInput | Partial<WorkflowRunRecord>): Promise<void>;
|
|
306
|
+
/**
|
|
307
|
+
* List workflow runs with filtering and pagination.
|
|
308
|
+
*/
|
|
309
|
+
/**
|
|
310
|
+
* Apply common run filters to a Kysely query builder.
|
|
311
|
+
* Used by both the data query and count query in listRuns to avoid duplication.
|
|
312
|
+
*/
|
|
313
|
+
private applyRunsFilters;
|
|
314
|
+
listRuns(options?: ListRunsOptions): Promise<PaginatedResult<WorkflowRunRecord>>;
|
|
315
|
+
createStep(step: Omit<WorkflowRunStepRecord, "id">): Promise<WorkflowRunStepRecord>;
|
|
316
|
+
getStep(stepId: string): Promise<WorkflowRunStepRecord | null>;
|
|
317
|
+
updateStep(stepId: string, updates: Partial<WorkflowRunStepRecord>): Promise<void>;
|
|
318
|
+
getStepsForRun(runId: string): Promise<WorkflowRunStepRecord[]>;
|
|
319
|
+
saveEvent(event: Omit<WorkflowEventRecord, "id">): Promise<void>;
|
|
320
|
+
getEventsForRun(runId: string, options?: ListEventsOptions): Promise<WorkflowEventRecord[]>;
|
|
321
|
+
/**
|
|
322
|
+
* Execute a function within a database transaction.
|
|
323
|
+
*/
|
|
324
|
+
transaction<T>(fn: (tx: StorageAdapter) => Promise<T>): Promise<T>;
|
|
325
|
+
/**
|
|
326
|
+
* Delete runs older than the specified date.
|
|
327
|
+
* Also deletes associated steps and events (via CASCADE).
|
|
328
|
+
*/
|
|
329
|
+
deleteOldRuns(olderThan: Date): Promise<number>;
|
|
330
|
+
/**
|
|
331
|
+
* Get all runs that were interrupted (status is 'running' or 'queued').
|
|
332
|
+
* These runs can potentially be resumed.
|
|
333
|
+
*/
|
|
334
|
+
getInterruptedRuns(): Promise<WorkflowRunRecord[]>;
|
|
335
|
+
/**
|
|
336
|
+
* Get the last completed step for a run.
|
|
337
|
+
* Useful for resuming from a checkpoint.
|
|
338
|
+
*/
|
|
339
|
+
getLastCompletedStep(runId: string): Promise<WorkflowRunStepRecord | null>;
|
|
340
|
+
/**
|
|
341
|
+
* Atomically dequeue a run for processing.
|
|
342
|
+
* Uses FOR UPDATE SKIP LOCKED for safe concurrent access.
|
|
343
|
+
*
|
|
344
|
+
* @param workflowKinds - Optional list of workflow kinds to filter by
|
|
345
|
+
* @returns The dequeued run, or null if no runs are available
|
|
346
|
+
*/
|
|
347
|
+
dequeueRun(workflowKinds?: string[]): Promise<WorkflowRunRecord | null>;
|
|
348
|
+
private mapRunRow;
|
|
349
|
+
/**
|
|
350
|
+
* Map a database row to an extended workflow run record.
|
|
351
|
+
*/
|
|
352
|
+
private mapExtendedRunRow;
|
|
353
|
+
private mapStepResultRow;
|
|
354
|
+
private mapStepRow;
|
|
355
|
+
private mapEventRow;
|
|
356
|
+
/**
|
|
357
|
+
* Delete a workflow run by ID.
|
|
358
|
+
* Also deletes associated steps and events (via CASCADE).
|
|
359
|
+
*/
|
|
360
|
+
deleteRun(id: string): Promise<void>;
|
|
361
|
+
/**
|
|
362
|
+
* Cleanup stale runs that have exceeded their timeout.
|
|
363
|
+
* Marks them as 'timeout' status with an appropriate error.
|
|
364
|
+
*
|
|
365
|
+
* @param defaultTimeoutMs - Default timeout in ms for runs without explicit timeout (default: 600000 = 10 minutes)
|
|
366
|
+
* @returns Number of runs marked as timed out
|
|
367
|
+
*/
|
|
368
|
+
cleanupStaleRuns(defaultTimeoutMs?: number): Promise<number>;
|
|
369
|
+
/**
|
|
370
|
+
* Mark multiple runs as failed with a given reason.
|
|
371
|
+
* Useful for cleanup when a worker shuts down unexpectedly.
|
|
372
|
+
*
|
|
373
|
+
* @param runIds - Array of run IDs to mark as failed
|
|
374
|
+
* @param reason - Reason message for the failure
|
|
375
|
+
*/
|
|
376
|
+
markRunsAsFailed(runIds: string[], reason: string): Promise<void>;
|
|
377
|
+
/**
|
|
378
|
+
* Get a specific step result by run ID and step name.
|
|
379
|
+
*/
|
|
380
|
+
getStepResult(runId: string, stepName: string): Promise<StepResult | undefined>;
|
|
381
|
+
/**
|
|
382
|
+
* Get all step results for a run.
|
|
383
|
+
*/
|
|
384
|
+
getStepResults(runId: string): Promise<StepResult[]>;
|
|
385
|
+
/**
|
|
386
|
+
* Save or update a step result.
|
|
387
|
+
* Uses upsert to handle both new and existing results.
|
|
388
|
+
*/
|
|
389
|
+
saveStepResult(result: Omit<StepResult, "id"> & {
|
|
390
|
+
id?: string;
|
|
391
|
+
}): Promise<void>;
|
|
392
|
+
/**
|
|
393
|
+
* Get database statistics.
|
|
394
|
+
*/
|
|
395
|
+
getStats(): Promise<{
|
|
396
|
+
runs: number;
|
|
397
|
+
steps: number;
|
|
398
|
+
events: number;
|
|
399
|
+
}>;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export { CreateRunInput, ListEventsOptions, ListRunsOptions, MemoryStorageAdapter, PaginatedResult, PostgresStorageAdapter as PostgresStorage, PostgresStorageAdapter, type PostgresStorageConfig, type PostgresStorageConfig as PostgresStorageOptions, SQLiteStorageAdapter, type SQLiteStorageConfig, StepResult, StorageAdapter, UpdateRunInput, WorkflowEventRecord, WorkflowRunRecord, WorkflowRunStepRecord };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import {
|
|
2
|
+
MemoryStorageAdapter,
|
|
3
|
+
PostgresStorageAdapter,
|
|
4
|
+
SQLiteStorageAdapter
|
|
5
|
+
} from "../chunk-FQCT3I2A.js";
|
|
6
|
+
import "../chunk-GXY66V7Q.js";
|
|
7
|
+
export {
|
|
8
|
+
MemoryStorageAdapter,
|
|
9
|
+
PostgresStorageAdapter as PostgresStorage,
|
|
10
|
+
PostgresStorageAdapter,
|
|
11
|
+
SQLiteStorageAdapter
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for the workflow engine.
|
|
3
|
+
* These are the foundational interfaces used throughout the system.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Unique identifier for a workflow type.
|
|
7
|
+
* Used to register and look up workflow definitions.
|
|
8
|
+
*/
|
|
9
|
+
type WorkflowKind = string;
|
|
10
|
+
/**
|
|
11
|
+
* Status of a workflow run.
|
|
12
|
+
*/
|
|
13
|
+
type RunStatus = "queued" | "running" | "succeeded" | "failed" | "canceled";
|
|
14
|
+
/**
|
|
15
|
+
* Status of a workflow step.
|
|
16
|
+
*/
|
|
17
|
+
type StepStatus = "pending" | "running" | "succeeded" | "failed" | "skipped" | "canceled";
|
|
18
|
+
/**
|
|
19
|
+
* Error handling strategy for a step.
|
|
20
|
+
* - 'fail': Stop the workflow immediately
|
|
21
|
+
* - 'retry': Retry the step up to maxRetries times
|
|
22
|
+
* - 'skip': Mark as skipped and continue to next step
|
|
23
|
+
*/
|
|
24
|
+
type StepErrorStrategy = "fail" | "retry" | "skip";
|
|
25
|
+
/**
|
|
26
|
+
* Structured error information stored with runs and steps.
|
|
27
|
+
*/
|
|
28
|
+
interface WorkflowError {
|
|
29
|
+
code: string;
|
|
30
|
+
message: string;
|
|
31
|
+
stack?: string;
|
|
32
|
+
details?: Record<string, unknown>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Result of a completed workflow run.
|
|
36
|
+
*/
|
|
37
|
+
interface RunResult {
|
|
38
|
+
status: "succeeded" | "failed" | "canceled";
|
|
39
|
+
results: Record<string, unknown>;
|
|
40
|
+
error?: WorkflowError;
|
|
41
|
+
duration: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Minimal logger interface that consumers can implement.
|
|
45
|
+
* Compatible with console, pino, winston, etc.
|
|
46
|
+
*/
|
|
47
|
+
interface Logger {
|
|
48
|
+
debug(message: string, ...args: unknown[]): void;
|
|
49
|
+
info(message: string, ...args: unknown[]): void;
|
|
50
|
+
warn(message: string, ...args: unknown[]): void;
|
|
51
|
+
error(message: string, ...args: unknown[]): void;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Options for spawning a child workflow from within a step handler.
|
|
55
|
+
*/
|
|
56
|
+
interface SpawnChildOptions {
|
|
57
|
+
kind: WorkflowKind;
|
|
58
|
+
input?: Record<string, unknown>;
|
|
59
|
+
metadata?: Record<string, unknown>;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Runtime context passed to each step handler.
|
|
63
|
+
* Contains all information a step needs to execute and interact with the engine.
|
|
64
|
+
*/
|
|
65
|
+
interface WorkflowContext<TInput = Record<string, unknown>> {
|
|
66
|
+
/** Unique run identifier */
|
|
67
|
+
runId: string;
|
|
68
|
+
/** Current step identifier (for granular tracking, e.g., token usage) */
|
|
69
|
+
stepId: string;
|
|
70
|
+
/** Workflow type */
|
|
71
|
+
kind: WorkflowKind;
|
|
72
|
+
/** Input parameters passed when starting the run */
|
|
73
|
+
input: TInput;
|
|
74
|
+
/** Accumulated results from previous steps (keyed by step key) */
|
|
75
|
+
results: Record<string, unknown>;
|
|
76
|
+
/** Optional metadata (topicId, userId, etc.) */
|
|
77
|
+
metadata: Record<string, unknown>;
|
|
78
|
+
/** Current step key (set during execution) */
|
|
79
|
+
currentStep?: string;
|
|
80
|
+
/** Logger instance scoped to this run */
|
|
81
|
+
logger: Logger;
|
|
82
|
+
/** Abort signal for cancellation */
|
|
83
|
+
signal: AbortSignal;
|
|
84
|
+
/** Helper to spawn child workflows */
|
|
85
|
+
spawnChild: (options: SpawnChildOptions) => Promise<string>;
|
|
86
|
+
/** Helper to emit custom events */
|
|
87
|
+
emit: (eventType: string, payload?: unknown) => void;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Definition of a single workflow step.
|
|
91
|
+
* Steps are executed sequentially within a workflow.
|
|
92
|
+
*/
|
|
93
|
+
interface WorkflowStep<TInput = Record<string, unknown>> {
|
|
94
|
+
/** Unique step identifier within the workflow */
|
|
95
|
+
key: string;
|
|
96
|
+
/** Human-readable step name */
|
|
97
|
+
name: string;
|
|
98
|
+
/** Async function that performs the step's work */
|
|
99
|
+
handler: (ctx: WorkflowContext<TInput>) => Promise<unknown>;
|
|
100
|
+
/** What to do when the step fails (default: 'fail') */
|
|
101
|
+
onError?: StepErrorStrategy;
|
|
102
|
+
/** Maximum retry attempts when onError is 'retry' @default 3 */
|
|
103
|
+
maxRetries?: number;
|
|
104
|
+
/** Delay between retries in ms @default 1000 */
|
|
105
|
+
retryDelay?: number;
|
|
106
|
+
/** Exponential backoff multiplier @default 2 */
|
|
107
|
+
retryBackoff?: number;
|
|
108
|
+
/** Timeout for this step in ms (optional) */
|
|
109
|
+
timeout?: number;
|
|
110
|
+
/** Condition to skip this step */
|
|
111
|
+
skipIf?: (ctx: WorkflowContext<TInput>) => boolean | Promise<boolean>;
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Lifecycle hooks for workflows.
|
|
115
|
+
* Allow custom logic at key points in the workflow execution.
|
|
116
|
+
*/
|
|
117
|
+
interface WorkflowHooks<TInput = Record<string, unknown>> {
|
|
118
|
+
/** Called before workflow starts */
|
|
119
|
+
beforeRun?: (ctx: WorkflowContext<TInput>) => Promise<void>;
|
|
120
|
+
/** Called after workflow completes (success or failure) */
|
|
121
|
+
afterRun?: (ctx: WorkflowContext<TInput>, result: RunResult) => Promise<void>;
|
|
122
|
+
/** Called before each step */
|
|
123
|
+
beforeStep?: (ctx: WorkflowContext<TInput>, step: WorkflowStep<TInput>) => Promise<void>;
|
|
124
|
+
/** Called after each step (success only) */
|
|
125
|
+
afterStep?: (ctx: WorkflowContext<TInput>, step: WorkflowStep<TInput>, result: unknown) => Promise<void>;
|
|
126
|
+
/** Called when a step fails */
|
|
127
|
+
onStepError?: (ctx: WorkflowContext<TInput>, step: WorkflowStep<TInput>, error: Error) => Promise<void>;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Complete workflow definition.
|
|
131
|
+
* Registered with the engine to enable starting runs of this type.
|
|
132
|
+
*/
|
|
133
|
+
interface WorkflowDefinition<TInput = Record<string, unknown>> {
|
|
134
|
+
/** Unique workflow type identifier */
|
|
135
|
+
kind: WorkflowKind;
|
|
136
|
+
/** Human-readable workflow name */
|
|
137
|
+
name: string;
|
|
138
|
+
/** Optional description */
|
|
139
|
+
description?: string;
|
|
140
|
+
/** Ordered list of steps to execute */
|
|
141
|
+
steps: WorkflowStep<TInput>[];
|
|
142
|
+
/** Default error strategy for all steps (default: 'fail') */
|
|
143
|
+
defaultOnError?: StepErrorStrategy;
|
|
144
|
+
/** Global timeout for the entire workflow in ms */
|
|
145
|
+
timeout?: number;
|
|
146
|
+
/** Hooks for workflow lifecycle */
|
|
147
|
+
hooks?: WorkflowHooks<TInput>;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
export type { Logger as L, RunStatus as R, StepStatus as S, WorkflowKind as W, WorkflowError as a, StepErrorStrategy as b, WorkflowStep as c, RunResult as d, SpawnChildOptions as e, WorkflowContext as f, WorkflowDefinition as g, WorkflowHooks as h };
|