@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.
Files changed (76) hide show
  1. package/.github/workflows/publish.yml +30 -0
  2. package/README.md +408 -0
  3. package/dist/chunk-FQCT3I2A.js +1512 -0
  4. package/dist/chunk-FQCT3I2A.js.map +1 -0
  5. package/dist/chunk-GXY66V7Q.js +111 -0
  6. package/dist/chunk-GXY66V7Q.js.map +1 -0
  7. package/dist/chunk-U5QGN3RT.js +715 -0
  8. package/dist/chunk-U5QGN3RT.js.map +1 -0
  9. package/dist/chunk-XCF4PLN7.js +575 -0
  10. package/dist/chunk-XCF4PLN7.js.map +1 -0
  11. package/dist/events/index.d.ts +346 -0
  12. package/dist/events/index.js +11 -0
  13. package/dist/events/index.js.map +1 -0
  14. package/dist/index-CPfGsmrp.d.ts +567 -0
  15. package/dist/index.d.ts +748 -0
  16. package/dist/index.js +1707 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/scheduler/index.d.ts +6 -0
  19. package/dist/scheduler/index.js +12 -0
  20. package/dist/scheduler/index.js.map +1 -0
  21. package/dist/storage/index.d.ts +402 -0
  22. package/dist/storage/index.js +13 -0
  23. package/dist/storage/index.js.map +1 -0
  24. package/dist/types-BVShq7zT.d.ts +150 -0
  25. package/dist/types-BlwFEGzj.d.ts +276 -0
  26. package/dist/types-DzkEqnQJ.d.ts +75 -0
  27. package/docs/github-packages-setup.md +97 -0
  28. package/docs/stepflow-api-reference.md +1662 -0
  29. package/package.json +85 -0
  30. package/src/core/engine.test.ts +1421 -0
  31. package/src/core/engine.ts +717 -0
  32. package/src/core/index.ts +7 -0
  33. package/src/core/orchestrator.ts +590 -0
  34. package/src/core/types.ts +242 -0
  35. package/src/events/index.ts +8 -0
  36. package/src/events/memory.test.ts +109 -0
  37. package/src/events/memory.ts +95 -0
  38. package/src/events/socketio.test.ts +428 -0
  39. package/src/events/socketio.ts +269 -0
  40. package/src/events/types.ts +121 -0
  41. package/src/events/webhook.test.ts +695 -0
  42. package/src/events/webhook.ts +591 -0
  43. package/src/index.ts +270 -0
  44. package/src/planning/index.ts +52 -0
  45. package/src/planning/planner.ts +601 -0
  46. package/src/planning/planning.test.ts +790 -0
  47. package/src/planning/registry.test.ts +445 -0
  48. package/src/planning/registry.ts +369 -0
  49. package/src/planning/types.ts +570 -0
  50. package/src/scheduler/cron.test.ts +480 -0
  51. package/src/scheduler/cron.ts +449 -0
  52. package/src/scheduler/index.ts +18 -0
  53. package/src/scheduler/postgres-persistence.test.ts +906 -0
  54. package/src/scheduler/postgres-persistence.ts +546 -0
  55. package/src/scheduler/sqlite-persistence.test.ts +235 -0
  56. package/src/scheduler/sqlite-persistence.ts +268 -0
  57. package/src/scheduler/types.ts +78 -0
  58. package/src/storage/index.ts +27 -0
  59. package/src/storage/memory.test.ts +399 -0
  60. package/src/storage/memory.ts +248 -0
  61. package/src/storage/postgres.ts +1550 -0
  62. package/src/storage/sqlite.test.ts +593 -0
  63. package/src/storage/sqlite.ts +662 -0
  64. package/src/storage/types.ts +368 -0
  65. package/src/utils/errors.test.ts +152 -0
  66. package/src/utils/errors.ts +191 -0
  67. package/src/utils/id.ts +21 -0
  68. package/src/utils/index.ts +8 -0
  69. package/src/utils/logger.test.ts +127 -0
  70. package/src/utils/logger.ts +96 -0
  71. package/src/utils/postgres-deps.ts +63 -0
  72. package/src/utils/retry.test.ts +197 -0
  73. package/src/utils/retry.ts +121 -0
  74. package/tsconfig.json +21 -0
  75. package/tsup.config.ts +14 -0
  76. package/vitest.config.ts +9 -0
@@ -0,0 +1,276 @@
1
+ import { R as RunStatus, W as WorkflowKind, S as StepStatus, a as WorkflowError } from './types-BVShq7zT.js';
2
+
3
+ /**
4
+ * Storage interface types for the workflow engine.
5
+ * Implement StorageAdapter to use your preferred database.
6
+ */
7
+
8
+ /**
9
+ * Extended status of a workflow run.
10
+ * Adds 'pending' and 'timeout' to the core statuses.
11
+ */
12
+ type ExtendedRunStatus = "pending" | "queued" | "running" | "succeeded" | "failed" | "canceled" | "timeout";
13
+ /**
14
+ * Extended status of a workflow step.
15
+ * Uses 'completed' instead of 'succeeded' for consistency.
16
+ */
17
+ type ExtendedStepStatus = "pending" | "running" | "completed" | "failed" | "skipped" | "canceled";
18
+ /**
19
+ * Stored representation of a workflow run.
20
+ */
21
+ interface WorkflowRunRecord {
22
+ id: string;
23
+ kind: string;
24
+ status: RunStatus;
25
+ parentRunId?: string;
26
+ input: Record<string, unknown>;
27
+ context: Record<string, unknown>;
28
+ output?: Record<string, unknown>;
29
+ error?: {
30
+ code: string;
31
+ message: string;
32
+ };
33
+ metadata?: Record<string, unknown>;
34
+ priority?: number;
35
+ timeoutMs?: number;
36
+ createdAt: Date;
37
+ startedAt?: Date;
38
+ finishedAt?: Date;
39
+ }
40
+ /**
41
+ * Stored representation of a workflow step execution.
42
+ */
43
+ interface WorkflowRunStepRecord {
44
+ id: string;
45
+ runId: string;
46
+ stepKey: string;
47
+ stepName: string;
48
+ status: StepStatus;
49
+ attempt: number;
50
+ result?: unknown;
51
+ error?: WorkflowError;
52
+ startedAt?: Date;
53
+ finishedAt?: Date;
54
+ }
55
+ /**
56
+ * Stored workflow event.
57
+ */
58
+ interface WorkflowEventRecord {
59
+ id: string;
60
+ runId: string;
61
+ stepKey?: string;
62
+ eventType: string;
63
+ level: "info" | "warn" | "error";
64
+ payload?: unknown;
65
+ timestamp: Date;
66
+ }
67
+ /**
68
+ * Extended workflow run record with additional fields.
69
+ * Used by new WorkflowStorage implementations.
70
+ */
71
+ interface ExtendedWorkflowRunRecord {
72
+ id: string;
73
+ kind: string;
74
+ status: ExtendedRunStatus;
75
+ input: Record<string, unknown>;
76
+ context: Record<string, unknown>;
77
+ output?: Record<string, unknown>;
78
+ error?: {
79
+ code: string;
80
+ message: string;
81
+ };
82
+ metadata?: Record<string, unknown>;
83
+ priority: number;
84
+ timeoutMs?: number;
85
+ createdAt: Date;
86
+ startedAt?: Date;
87
+ finishedAt?: Date;
88
+ }
89
+ /**
90
+ * Step result representation.
91
+ */
92
+ interface StepResult {
93
+ id: string;
94
+ runId: string;
95
+ stepName: string;
96
+ status: ExtendedStepStatus;
97
+ output?: Record<string, unknown>;
98
+ error?: Record<string, unknown>;
99
+ attempt: number;
100
+ startedAt?: Date;
101
+ completedAt?: Date;
102
+ }
103
+ /**
104
+ * Step record for workflow steps.
105
+ */
106
+ interface StepRecord {
107
+ stepKey: string;
108
+ stepName: string;
109
+ status: ExtendedStepStatus;
110
+ result?: Record<string, unknown>;
111
+ error?: Record<string, unknown>;
112
+ attempt: number;
113
+ startedAt?: Date;
114
+ finishedAt?: Date;
115
+ }
116
+ /**
117
+ * Input for creating a new workflow run.
118
+ */
119
+ interface CreateRunInput {
120
+ id?: string;
121
+ kind: string;
122
+ status: ExtendedRunStatus;
123
+ parentRunId?: string;
124
+ input: Record<string, unknown>;
125
+ context?: Record<string, unknown>;
126
+ metadata?: Record<string, unknown>;
127
+ priority?: number;
128
+ timeoutMs?: number;
129
+ }
130
+ /**
131
+ * Input for updating a workflow run.
132
+ */
133
+ interface UpdateRunInput {
134
+ status?: ExtendedRunStatus;
135
+ context?: Record<string, unknown>;
136
+ output?: Record<string, unknown>;
137
+ error?: {
138
+ code: string;
139
+ message: string;
140
+ };
141
+ startedAt?: Date;
142
+ finishedAt?: Date;
143
+ }
144
+ /**
145
+ * Options for listing runs.
146
+ */
147
+ interface ListRunsOptions {
148
+ kind?: WorkflowKind;
149
+ status?: RunStatus | RunStatus[];
150
+ parentRunId?: string;
151
+ limit?: number;
152
+ offset?: number;
153
+ orderBy?: "createdAt" | "startedAt" | "finishedAt";
154
+ orderDirection?: "asc" | "desc";
155
+ }
156
+ /**
157
+ * Extended options for listing runs.
158
+ */
159
+ interface ExtendedListRunsOptions {
160
+ kind?: string;
161
+ status?: ExtendedRunStatus | ExtendedRunStatus[];
162
+ limit?: number;
163
+ offset?: number;
164
+ orderBy?: "createdAt" | "startedAt" | "finishedAt";
165
+ orderDir?: "asc" | "desc";
166
+ }
167
+ /**
168
+ * Options for listing events.
169
+ */
170
+ interface ListEventsOptions {
171
+ stepKey?: string;
172
+ level?: "info" | "warn" | "error";
173
+ limit?: number;
174
+ offset?: number;
175
+ }
176
+ /**
177
+ * Paginated result wrapper.
178
+ */
179
+ interface PaginatedResult<T> {
180
+ items: T[];
181
+ total: number;
182
+ limit?: number;
183
+ offset?: number;
184
+ }
185
+ /**
186
+ * New storage interface for workflow persistence.
187
+ * Implement this interface for new implementations.
188
+ */
189
+ interface WorkflowStorage {
190
+ createRun(run: CreateRunInput): Promise<ExtendedWorkflowRunRecord>;
191
+ getRun(id: string): Promise<ExtendedWorkflowRunRecord | null>;
192
+ updateRun(id: string, updates: UpdateRunInput): Promise<void>;
193
+ listRuns(options?: ExtendedListRunsOptions): Promise<PaginatedResult<ExtendedWorkflowRunRecord>>;
194
+ deleteRun(id: string): Promise<void>;
195
+ dequeueRun(workflowKinds: string[]): Promise<ExtendedWorkflowRunRecord | null>;
196
+ cleanupStaleRuns(defaultTimeoutMs?: number): Promise<number>;
197
+ markRunsAsFailed(runIds: string[], reason: string): Promise<void>;
198
+ getStepResult(runId: string, stepName: string): Promise<StepResult | undefined>;
199
+ getStepResults(runId: string): Promise<StepResult[]>;
200
+ getStepsForRun(runId: string): Promise<StepRecord[]>;
201
+ saveStepResult(result: Omit<StepResult, "id"> & {
202
+ id?: string;
203
+ }): Promise<void>;
204
+ initialize(): Promise<void>;
205
+ close(): Promise<void>;
206
+ }
207
+ /**
208
+ * Abstract storage adapter interface.
209
+ * Implement this interface to use your preferred database.
210
+ */
211
+ interface StorageAdapter {
212
+ createRun(run: Omit<WorkflowRunRecord, "id" | "createdAt">): Promise<WorkflowRunRecord>;
213
+ getRun(runId: string): Promise<WorkflowRunRecord | null>;
214
+ updateRun(runId: string, updates: Partial<WorkflowRunRecord>): Promise<void>;
215
+ listRuns(options?: ListRunsOptions): Promise<PaginatedResult<WorkflowRunRecord>>;
216
+ createStep(step: Omit<WorkflowRunStepRecord, "id">): Promise<WorkflowRunStepRecord>;
217
+ getStep(stepId: string): Promise<WorkflowRunStepRecord | null>;
218
+ updateStep(stepId: string, updates: Partial<WorkflowRunStepRecord>): Promise<void>;
219
+ getStepsForRun(runId: string): Promise<WorkflowRunStepRecord[]>;
220
+ saveEvent(event: Omit<WorkflowEventRecord, "id">): Promise<void>;
221
+ getEventsForRun(runId: string, options?: ListEventsOptions): Promise<WorkflowEventRecord[]>;
222
+ /**
223
+ * Optional transaction support.
224
+ * @deprecated Transaction support is deprecated and will be removed in a future release.
225
+ */
226
+ transaction?<T>(fn: (tx: StorageAdapter) => Promise<T>): Promise<T>;
227
+ deleteOldRuns?(olderThan: Date): Promise<number>;
228
+ initialize?(): Promise<void>;
229
+ close?(): void | Promise<void>;
230
+ }
231
+ /**
232
+ * Database table schema for workflow runs.
233
+ * Note: When used with Kysely, wrap auto-generated fields with Generated<T>.
234
+ */
235
+ interface StepflowRunsTable {
236
+ id: string;
237
+ kind: string;
238
+ status: ExtendedRunStatus;
239
+ input: Record<string, unknown>;
240
+ context: Record<string, unknown>;
241
+ output: Record<string, unknown> | null;
242
+ error: {
243
+ code: string;
244
+ message: string;
245
+ } | null;
246
+ metadata: Record<string, unknown> | null;
247
+ priority: number;
248
+ timeout_ms: number | null;
249
+ created_at: Date;
250
+ started_at: Date | null;
251
+ finished_at: Date | null;
252
+ }
253
+ /**
254
+ * Database table schema for step results.
255
+ * Note: When used with Kysely, wrap auto-generated fields with Generated<T>.
256
+ */
257
+ interface StepflowStepResultsTable {
258
+ id: string;
259
+ run_id: string;
260
+ step_name: string;
261
+ status: ExtendedStepStatus;
262
+ output: Record<string, unknown> | null;
263
+ error: Record<string, unknown> | null;
264
+ attempt: number;
265
+ started_at: Date | null;
266
+ completed_at: Date | null;
267
+ }
268
+ /**
269
+ * Combined database schema for Stepflow.
270
+ */
271
+ interface StepflowDatabase {
272
+ "stepflow.runs": StepflowRunsTable;
273
+ "stepflow.step_results": StepflowStepResultsTable;
274
+ }
275
+
276
+ export type { CreateRunInput as C, ExtendedListRunsOptions as E, ListEventsOptions as L, PaginatedResult as P, StepRecord as S, UpdateRunInput as U, WorkflowEventRecord as W, ExtendedRunStatus as a, ExtendedStepStatus as b, ExtendedWorkflowRunRecord as c, ListRunsOptions as d, StepResult as e, StepflowDatabase as f, StepflowRunsTable as g, StepflowStepResultsTable as h, StorageAdapter as i, WorkflowRunRecord as j, WorkflowRunStepRecord as k, WorkflowStorage as l };
@@ -0,0 +1,75 @@
1
+ import { W as WorkflowKind } from './types-BVShq7zT.js';
2
+
3
+ /**
4
+ * Event system types for the workflow engine.
5
+ */
6
+
7
+ /**
8
+ * Built-in event types emitted by the workflow engine.
9
+ */
10
+ type BuiltInEventType = "run.created" | "run.queued" | "run.dequeued" | "run.started" | "run.resumed" | "run.completed" | "run.failed" | "run.canceled" | "run.timeout" | "step.started" | "step.completed" | "step.failed" | "step.skipped" | "step.retry";
11
+ /**
12
+ * Event types emitted by the workflow engine.
13
+ * Includes built-in types and allows custom events via string.
14
+ */
15
+ type WorkflowEventType = BuiltInEventType | (string & {});
16
+ /**
17
+ * Workflow event payload structure.
18
+ */
19
+ interface WorkflowEvent {
20
+ /** Unique run identifier */
21
+ runId: string;
22
+ /** Workflow type */
23
+ kind: WorkflowKind;
24
+ /** Type of event */
25
+ eventType: WorkflowEventType;
26
+ /** Step key (for step-related events) */
27
+ stepKey?: string;
28
+ /** When the event occurred */
29
+ timestamp: Date;
30
+ /** Event-specific data */
31
+ payload?: unknown;
32
+ }
33
+ /**
34
+ * Callback function for handling workflow events.
35
+ */
36
+ type EventCallback = (event: WorkflowEvent) => void;
37
+ /**
38
+ * Unsubscribe function returned when subscribing to events.
39
+ */
40
+ type Unsubscribe = () => void;
41
+ /**
42
+ * Event transport interface.
43
+ * Implement this to customize how events are delivered.
44
+ */
45
+ interface EventTransport {
46
+ /**
47
+ * Emit an event to all subscribers.
48
+ */
49
+ emit(event: WorkflowEvent): void;
50
+ /**
51
+ * Subscribe to events for a specific run.
52
+ * @returns Unsubscribe function
53
+ */
54
+ subscribe(runId: string, callback: EventCallback): Unsubscribe;
55
+ /**
56
+ * Subscribe to all events.
57
+ * @returns Unsubscribe function
58
+ */
59
+ subscribeAll(callback: EventCallback): Unsubscribe;
60
+ /**
61
+ * Optional: Filter events by type.
62
+ * @returns Unsubscribe function
63
+ */
64
+ subscribeToType?(eventType: WorkflowEventType, callback: EventCallback): Unsubscribe;
65
+ /**
66
+ * Optional: Persist event for later retrieval.
67
+ */
68
+ persist?(event: WorkflowEvent): Promise<void>;
69
+ /**
70
+ * Optional: Close/cleanup the transport.
71
+ */
72
+ close?(): void;
73
+ }
74
+
75
+ export type { BuiltInEventType as B, EventCallback as E, Unsubscribe as U, WorkflowEvent as W, EventTransport as a, WorkflowEventType as b };
@@ -0,0 +1,97 @@
1
+ # GitHub Packages Setup for @multiplier-labs/stepflow
2
+
3
+ This document describes how to publish stepflow to GitHub Packages and how to install it in your projects.
4
+
5
+ ## Publishing (Maintainers)
6
+
7
+ ### Automated Publishing via Release
8
+
9
+ 1. Ensure your changes are merged to the main branch
10
+ 2. Go to **Releases** in the GitHub repository
11
+ 3. Click **Draft a new release**
12
+ 4. Create a tag matching the version in `package.json` (e.g., `v0.1.0`)
13
+ 5. Publish the release
14
+
15
+ The GitHub Actions workflow (`.github/workflows/publish.yml`) automatically:
16
+ - Installs dependencies
17
+ - Builds the package
18
+ - Publishes to GitHub Packages
19
+
20
+ ### Manual Publishing (if needed)
21
+
22
+ ```bash
23
+ # Authenticate with GitHub Packages
24
+ npm login --registry=https://npm.pkg.github.com
25
+ # Username: your GitHub username
26
+ # Password: Personal Access Token with write:packages scope
27
+
28
+ # Build and publish
29
+ npm run build
30
+ npm publish
31
+ ```
32
+
33
+ ## Installing in Projects
34
+
35
+ ### Step 1: Configure npm for the @multiplier-labs scope
36
+
37
+ Create or update `.npmrc` in your project root:
38
+
39
+ ```
40
+ @multiplier-labs:registry=https://npm.pkg.github.com
41
+ ```
42
+
43
+ ### Step 2: Authenticate with GitHub Packages
44
+
45
+ Generate a Personal Access Token (PAT):
46
+ 1. Go to GitHub → Settings → Developer settings → Personal access tokens
47
+ 2. Generate a token with `read:packages` scope
48
+ 3. Authenticate npm:
49
+
50
+ ```bash
51
+ npm login --registry=https://npm.pkg.github.com
52
+ # Username: your GitHub username
53
+ # Password: your PAT
54
+ # Email: your email
55
+ ```
56
+
57
+ Alternatively, add to your `~/.npmrc` (for global auth):
58
+
59
+ ```
60
+ //npm.pkg.github.com/:_authToken=YOUR_PAT_HERE
61
+ ```
62
+
63
+ ### Step 3: Install the package
64
+
65
+ ```bash
66
+ npm install @multiplier-labs/stepflow
67
+ ```
68
+
69
+ ## Usage in CI/CD
70
+
71
+ For GitHub Actions, use the built-in `GITHUB_TOKEN`:
72
+
73
+ ```yaml
74
+ - uses: actions/setup-node@v4
75
+ with:
76
+ node-version: '20'
77
+ registry-url: 'https://npm.pkg.github.com'
78
+ scope: '@multiplier-labs'
79
+
80
+ - run: npm ci
81
+ env:
82
+ NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
83
+ ```
84
+
85
+ For other CI systems, set `NODE_AUTH_TOKEN` environment variable to a PAT with `read:packages` scope.
86
+
87
+ ## Versioning
88
+
89
+ Update the version in `package.json` before creating a release:
90
+
91
+ ```bash
92
+ npm version patch # 0.1.0 → 0.1.1
93
+ npm version minor # 0.1.0 → 0.2.0
94
+ npm version major # 0.1.0 → 1.0.0
95
+ ```
96
+
97
+ Then push the tag and create a release on GitHub.