@sonamu-kit/tasks 0.2.0 → 0.3.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/.oxlintrc.json +3 -0
- package/AGENTS.md +21 -0
- package/dist/backend.d.ts +126 -107
- package/dist/backend.d.ts.map +1 -1
- package/dist/backend.js +4 -1
- package/dist/backend.js.map +1 -1
- package/dist/client.d.ts +145 -132
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +219 -213
- package/dist/client.js.map +1 -1
- package/dist/config.d.ts +15 -8
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +22 -17
- package/dist/config.js.map +1 -1
- package/dist/core/duration.d.ts +5 -4
- package/dist/core/duration.d.ts.map +1 -1
- package/dist/core/duration.js +54 -59
- package/dist/core/duration.js.map +1 -1
- package/dist/core/error.d.ts +10 -7
- package/dist/core/error.d.ts.map +1 -1
- package/dist/core/error.js +21 -21
- package/dist/core/error.js.map +1 -1
- package/dist/core/json.d.ts +8 -3
- package/dist/core/json.d.ts.map +1 -1
- package/dist/core/result.d.ts +10 -14
- package/dist/core/result.d.ts.map +1 -1
- package/dist/core/result.js +21 -16
- package/dist/core/result.js.map +1 -1
- package/dist/core/retry.d.ts +37 -31
- package/dist/core/retry.d.ts.map +1 -1
- package/dist/core/retry.js +44 -51
- package/dist/core/retry.js.map +1 -1
- package/dist/core/schema.d.ts +57 -53
- package/dist/core/schema.d.ts.map +1 -1
- package/dist/core/step.d.ts +28 -78
- package/dist/core/step.d.ts.map +1 -1
- package/dist/core/step.js +53 -63
- package/dist/core/step.js.map +1 -1
- package/dist/core/workflow.d.ts +33 -61
- package/dist/core/workflow.d.ts.map +1 -1
- package/dist/core/workflow.js +31 -41
- package/dist/core/workflow.js.map +1 -1
- package/dist/database/backend.d.ts +53 -46
- package/dist/database/backend.d.ts.map +1 -1
- package/dist/database/backend.js +544 -577
- package/dist/database/backend.js.map +1 -1
- package/dist/database/base.js +48 -25
- package/dist/database/base.js.map +1 -1
- package/dist/database/migrations/20251212000000_0_init.d.ts +10 -0
- package/dist/database/migrations/20251212000000_0_init.d.ts.map +1 -0
- package/dist/database/migrations/20251212000000_0_init.js +8 -4
- package/dist/database/migrations/20251212000000_0_init.js.map +1 -1
- package/dist/database/migrations/20251212000000_1_tables.d.ts +10 -0
- package/dist/database/migrations/20251212000000_1_tables.d.ts.map +1 -0
- package/dist/database/migrations/20251212000000_1_tables.js +81 -83
- package/dist/database/migrations/20251212000000_1_tables.js.map +1 -1
- package/dist/database/migrations/20251212000000_2_fk.d.ts +10 -0
- package/dist/database/migrations/20251212000000_2_fk.d.ts.map +1 -0
- package/dist/database/migrations/20251212000000_2_fk.js +20 -43
- package/dist/database/migrations/20251212000000_2_fk.js.map +1 -1
- package/dist/database/migrations/20251212000000_3_indexes.d.ts +10 -0
- package/dist/database/migrations/20251212000000_3_indexes.d.ts.map +1 -0
- package/dist/database/migrations/20251212000000_3_indexes.js +88 -102
- package/dist/database/migrations/20251212000000_3_indexes.js.map +1 -1
- package/dist/database/pubsub.d.ts +7 -16
- package/dist/database/pubsub.d.ts.map +1 -1
- package/dist/database/pubsub.js +75 -73
- package/dist/database/pubsub.js.map +1 -1
- package/dist/execution.d.ts +20 -59
- package/dist/execution.d.ts.map +1 -1
- package/dist/execution.js +175 -188
- package/dist/execution.js.map +1 -1
- package/dist/index.d.ts +5 -8
- package/dist/index.js +5 -5
- package/dist/internal.d.ts +12 -13
- package/dist/internal.js +4 -4
- package/dist/registry.d.ts +33 -27
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +58 -49
- package/dist/registry.js.map +1 -1
- package/dist/worker.d.ts +57 -50
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +194 -199
- package/dist/worker.js.map +1 -1
- package/dist/workflow.d.ts +26 -30
- package/dist/workflow.d.ts.map +1 -1
- package/dist/workflow.js +20 -15
- package/dist/workflow.js.map +1 -1
- package/nodemon.json +1 -1
- package/package.json +17 -19
- package/src/backend.ts +25 -9
- package/src/chaos.test.ts +3 -1
- package/src/client.test.ts +2 -0
- package/src/client.ts +30 -8
- package/src/config.test.ts +1 -0
- package/src/config.ts +3 -2
- package/src/core/duration.test.ts +2 -1
- package/src/core/duration.ts +1 -1
- package/src/core/error.test.ts +1 -0
- package/src/core/error.ts +1 -1
- package/src/core/result.test.ts +1 -0
- package/src/core/retry.test.ts +3 -2
- package/src/core/retry.ts +1 -1
- package/src/core/schema.ts +2 -2
- package/src/core/step.test.ts +2 -1
- package/src/core/step.ts +4 -3
- package/src/core/workflow.test.ts +2 -1
- package/src/core/workflow.ts +4 -3
- package/src/database/backend.test.ts +1 -0
- package/src/database/backend.testsuite.ts +44 -40
- package/src/database/backend.ts +207 -25
- package/src/database/base.test.ts +41 -0
- package/src/database/base.ts +51 -2
- package/src/database/migrations/20251212000000_0_init.ts +2 -1
- package/src/database/migrations/20251212000000_1_tables.ts +2 -1
- package/src/database/migrations/20251212000000_2_fk.ts +2 -1
- package/src/database/migrations/20251212000000_3_indexes.ts +2 -1
- package/src/database/pubsub.test.ts +6 -3
- package/src/database/pubsub.ts +55 -33
- package/src/execution.test.ts +2 -0
- package/src/execution.ts +49 -10
- package/src/internal.ts +15 -15
- package/src/practices/01-remote-workflow.ts +1 -0
- package/src/registry.test.ts +1 -0
- package/src/registry.ts +1 -1
- package/src/testing/connection.ts +3 -1
- package/src/worker.test.ts +2 -0
- package/src/worker.ts +30 -9
- package/src/workflow.test.ts +1 -0
- package/src/workflow.ts +3 -3
- package/templates/openworkflow.config.ts +2 -1
- package/tsdown.config.ts +31 -0
- package/.swcrc +0 -17
- package/dist/chaos.test.d.ts +0 -2
- package/dist/chaos.test.d.ts.map +0 -1
- package/dist/chaos.test.js +0 -92
- package/dist/chaos.test.js.map +0 -1
- package/dist/client.test.d.ts +0 -2
- package/dist/client.test.d.ts.map +0 -1
- package/dist/client.test.js +0 -340
- package/dist/client.test.js.map +0 -1
- package/dist/config.test.d.ts +0 -2
- package/dist/config.test.d.ts.map +0 -1
- package/dist/config.test.js +0 -24
- package/dist/config.test.js.map +0 -1
- package/dist/core/duration.test.d.ts +0 -2
- package/dist/core/duration.test.d.ts.map +0 -1
- package/dist/core/duration.test.js +0 -265
- package/dist/core/duration.test.js.map +0 -1
- package/dist/core/error.test.d.ts +0 -2
- package/dist/core/error.test.d.ts.map +0 -1
- package/dist/core/error.test.js +0 -63
- package/dist/core/error.test.js.map +0 -1
- package/dist/core/json.js +0 -3
- package/dist/core/json.js.map +0 -1
- package/dist/core/result.test.d.ts +0 -2
- package/dist/core/result.test.d.ts.map +0 -1
- package/dist/core/result.test.js +0 -19
- package/dist/core/result.test.js.map +0 -1
- package/dist/core/retry.test.d.ts +0 -2
- package/dist/core/retry.test.d.ts.map +0 -1
- package/dist/core/retry.test.js +0 -198
- package/dist/core/retry.test.js.map +0 -1
- package/dist/core/schema.js +0 -4
- package/dist/core/schema.js.map +0 -1
- package/dist/core/step.test.d.ts +0 -2
- package/dist/core/step.test.d.ts.map +0 -1
- package/dist/core/step.test.js +0 -356
- package/dist/core/step.test.js.map +0 -1
- package/dist/core/workflow.test.d.ts +0 -2
- package/dist/core/workflow.test.d.ts.map +0 -1
- package/dist/core/workflow.test.js +0 -172
- package/dist/core/workflow.test.js.map +0 -1
- package/dist/database/backend.test.d.ts +0 -2
- package/dist/database/backend.test.d.ts.map +0 -1
- package/dist/database/backend.test.js +0 -19
- package/dist/database/backend.test.js.map +0 -1
- package/dist/database/backend.testsuite.d.ts +0 -20
- package/dist/database/backend.testsuite.d.ts.map +0 -1
- package/dist/database/backend.testsuite.js +0 -1280
- package/dist/database/backend.testsuite.js.map +0 -1
- package/dist/database/base.d.ts +0 -12
- package/dist/database/base.d.ts.map +0 -1
- package/dist/database/pubsub.test.d.ts +0 -2
- package/dist/database/pubsub.test.d.ts.map +0 -1
- package/dist/database/pubsub.test.js +0 -86
- package/dist/database/pubsub.test.js.map +0 -1
- package/dist/execution.test.d.ts +0 -2
- package/dist/execution.test.d.ts.map +0 -1
- package/dist/execution.test.js +0 -662
- package/dist/execution.test.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/internal.d.ts.map +0 -1
- package/dist/internal.js.map +0 -1
- package/dist/practices/01-remote-workflow.d.ts +0 -2
- package/dist/practices/01-remote-workflow.d.ts.map +0 -1
- package/dist/practices/01-remote-workflow.js +0 -70
- package/dist/practices/01-remote-workflow.js.map +0 -1
- package/dist/registry.test.d.ts +0 -2
- package/dist/registry.test.d.ts.map +0 -1
- package/dist/registry.test.js +0 -95
- package/dist/registry.test.js.map +0 -1
- package/dist/testing/connection.d.ts +0 -7
- package/dist/testing/connection.d.ts.map +0 -1
- package/dist/testing/connection.js +0 -39
- package/dist/testing/connection.js.map +0 -1
- package/dist/worker.test.d.ts +0 -2
- package/dist/worker.test.d.ts.map +0 -1
- package/dist/worker.test.js +0 -1164
- package/dist/worker.test.js.map +0 -1
- package/dist/workflow.test.d.ts +0 -2
- package/dist/workflow.test.d.ts.map +0 -1
- package/dist/workflow.test.js +0 -73
- package/dist/workflow.test.js.map +0 -1
package/dist/client.js
CHANGED
|
@@ -3,222 +3,228 @@ import { validateInput } from "./core/workflow.js";
|
|
|
3
3
|
import { WorkflowRegistry } from "./registry.js";
|
|
4
4
|
import { Worker } from "./worker.js";
|
|
5
5
|
import { defineWorkflow, defineWorkflowSpec } from "./workflow.js";
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
|
|
7
|
+
//#region src/client.ts
|
|
8
|
+
const DEFAULT_RESULT_POLL_INTERVAL_MS = 1e3;
|
|
9
|
+
const DEFAULT_RESULT_TIMEOUT_MS = 300 * 1e3;
|
|
8
10
|
/**
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
11
|
+
* Client used to register workflows and start runs.
|
|
12
|
+
*/
|
|
13
|
+
var OpenWorkflow = class {
|
|
14
|
+
backend;
|
|
15
|
+
registry = new WorkflowRegistry();
|
|
16
|
+
constructor(options) {
|
|
17
|
+
this.backend = options.backend;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Create a new Worker with this client's backend and workflows.
|
|
21
|
+
* @param options - Worker options
|
|
22
|
+
* @param options.concurrency - Max concurrent workflow runs
|
|
23
|
+
* @returns Worker instance
|
|
24
|
+
*/
|
|
25
|
+
newWorker(options) {
|
|
26
|
+
return new Worker({
|
|
27
|
+
backend: this.backend,
|
|
28
|
+
registry: this.registry,
|
|
29
|
+
concurrency: options?.concurrency,
|
|
30
|
+
usePubSub: options?.usePubSub,
|
|
31
|
+
listenDelay: options?.listenDelay
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Provide the implementation for a declared workflow. This links the workflow
|
|
36
|
+
* specification to its execution logic and registers it with this
|
|
37
|
+
* OpenWorkflow instance for worker execution.
|
|
38
|
+
* @param spec - Workflow spec
|
|
39
|
+
* @param fn - Workflow implementation
|
|
40
|
+
*/
|
|
41
|
+
implementWorkflow(spec, fn) {
|
|
42
|
+
const workflow = {
|
|
43
|
+
spec,
|
|
44
|
+
fn
|
|
45
|
+
};
|
|
46
|
+
this.registry.register(workflow);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Run a workflow from its specification. This is the primary way to schedule
|
|
50
|
+
* a workflow using only its WorkflowSpec.
|
|
51
|
+
* @param spec - Workflow spec
|
|
52
|
+
* @param input - Workflow input
|
|
53
|
+
* @param options - Run options
|
|
54
|
+
* @returns Handle for awaiting the result
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* const handle = await ow.runWorkflow(emailWorkflow, { to: 'user@example.com' });
|
|
58
|
+
* const result = await handle.result();
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
async runWorkflow(spec, input, options) {
|
|
62
|
+
const validationResult = await validateInput(spec.schema, input);
|
|
63
|
+
if (!validationResult.success) throw new Error(validationResult.error);
|
|
64
|
+
const parsedInput = validationResult.value;
|
|
65
|
+
const workflowRun = await this.backend.createWorkflowRun({
|
|
66
|
+
workflowName: spec.name,
|
|
67
|
+
version: spec.version ?? null,
|
|
68
|
+
idempotencyKey: null,
|
|
69
|
+
config: {},
|
|
70
|
+
context: null,
|
|
71
|
+
input: parsedInput ?? null,
|
|
72
|
+
availableAt: null,
|
|
73
|
+
deadlineAt: options?.deadlineAt ?? null,
|
|
74
|
+
retryPolicy: spec.retryPolicy ? serializeRetryPolicy(spec.retryPolicy) : void 0
|
|
75
|
+
});
|
|
76
|
+
if (options?.publishToChannel) await this.backend.publish(workflowRun.id);
|
|
77
|
+
return new WorkflowRunHandle({
|
|
78
|
+
backend: this.backend,
|
|
79
|
+
workflowRun,
|
|
80
|
+
resultPollIntervalMs: DEFAULT_RESULT_POLL_INTERVAL_MS,
|
|
81
|
+
resultTimeoutMs: DEFAULT_RESULT_TIMEOUT_MS
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Define and register a new workflow.
|
|
86
|
+
*
|
|
87
|
+
* This is a convenience method that combines `declareWorkflow` and
|
|
88
|
+
* `implementWorkflow` into a single call. For better code splitting and to
|
|
89
|
+
* separate declaration from implementation, consider using those methods
|
|
90
|
+
* separately.
|
|
91
|
+
* @param config - Workflow config
|
|
92
|
+
* @param fn - Workflow implementation
|
|
93
|
+
* @returns Runnable workflow
|
|
94
|
+
* @example
|
|
95
|
+
* ```ts
|
|
96
|
+
* const workflow = ow.defineWorkflow(
|
|
97
|
+
* { name: 'my-workflow' },
|
|
98
|
+
* async ({ input, step }) => {
|
|
99
|
+
* // workflow implementation
|
|
100
|
+
* },
|
|
101
|
+
* );
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
104
|
+
defineWorkflow(spec, fn) {
|
|
105
|
+
const workflow = defineWorkflow(spec, fn);
|
|
106
|
+
this.registry.register(workflow);
|
|
107
|
+
return new RunnableWorkflow(this, workflow);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Unregister a workflow from the registry.
|
|
111
|
+
* @param name - The workflow name
|
|
112
|
+
* @param version - The workflow version (null for unversioned)
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* ow.unregisterWorkflow("my-workflow", "v1");
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
unregisterWorkflow(name, version) {
|
|
119
|
+
this.registry.remove(name, version);
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Check if a workflow is registered in the registry.
|
|
123
|
+
* @param name - The workflow name
|
|
124
|
+
* @param version - The workflow version (null for unversioned)
|
|
125
|
+
* @returns True if the workflow is registered, false otherwise
|
|
126
|
+
* @example
|
|
127
|
+
* ```ts
|
|
128
|
+
* ow.isWorkflowRegistered("my-workflow", "v1");
|
|
129
|
+
* ```
|
|
130
|
+
*/
|
|
131
|
+
isWorkflowRegistered(name, version) {
|
|
132
|
+
return this.registry.has(name, version);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
130
135
|
/**
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
export const declareWorkflow = defineWorkflowSpec;
|
|
146
|
-
//
|
|
147
|
-
// --- Workflow Definition
|
|
148
|
-
//
|
|
136
|
+
* Declare a workflow without providing its implementation (which is provided
|
|
137
|
+
* separately via `implementWorkflow`). Returns a lightweight WorkflowSpec
|
|
138
|
+
* that can be used to schedule workflow runs.
|
|
139
|
+
* @param spec - Workflow spec
|
|
140
|
+
* @returns Workflow spec
|
|
141
|
+
* @example
|
|
142
|
+
* ```ts
|
|
143
|
+
* export const emailWorkflow = declareWorkflow({
|
|
144
|
+
* name: 'send-email',
|
|
145
|
+
* schema: z.object({ to: z.string().email() }),
|
|
146
|
+
* });
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
const declareWorkflow = defineWorkflowSpec;
|
|
149
150
|
/**
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
151
|
+
* A fully defined workflow with its implementation. This class is returned by
|
|
152
|
+
* `defineWorkflow` and provides the `.run()` method for scheduling workflow
|
|
153
|
+
* runs.
|
|
154
|
+
*/
|
|
155
|
+
var RunnableWorkflow = class {
|
|
156
|
+
ow;
|
|
157
|
+
workflow;
|
|
158
|
+
constructor(ow, workflow) {
|
|
159
|
+
this.ow = ow;
|
|
160
|
+
this.workflow = workflow;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Starts a new workflow run.
|
|
164
|
+
* @param input - Workflow input
|
|
165
|
+
* @param options - Run options
|
|
166
|
+
* @returns Workflow run handle
|
|
167
|
+
*/
|
|
168
|
+
async run(input, options) {
|
|
169
|
+
return this.ow.runWorkflow(this.workflow.spec, input, options);
|
|
170
|
+
}
|
|
171
|
+
};
|
|
169
172
|
/**
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
}
|
|
173
|
+
* Represents a started workflow run and provides methods to await its result.
|
|
174
|
+
* Returned from `workflowDef.run()`.
|
|
175
|
+
*/
|
|
176
|
+
var WorkflowRunHandle = class {
|
|
177
|
+
backend;
|
|
178
|
+
workflowRun;
|
|
179
|
+
resultPollIntervalMs;
|
|
180
|
+
resultTimeoutMs;
|
|
181
|
+
constructor(options) {
|
|
182
|
+
this.backend = options.backend;
|
|
183
|
+
this.workflowRun = options.workflowRun;
|
|
184
|
+
this.resultPollIntervalMs = options.resultPollIntervalMs;
|
|
185
|
+
this.resultTimeoutMs = options.resultTimeoutMs;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Waits for the workflow run to complete and returns the result.
|
|
189
|
+
* @returns Workflow output
|
|
190
|
+
*/
|
|
191
|
+
async result() {
|
|
192
|
+
const start = Date.now();
|
|
193
|
+
while (true) {
|
|
194
|
+
const latest = await this.backend.getWorkflowRun({ workflowRunId: this.workflowRun.id });
|
|
195
|
+
if (!latest) throw new Error(`Workflow run ${this.workflowRun.id} no longer exists`);
|
|
196
|
+
if (latest.status === "succeeded" || latest.status === "completed") return latest.output;
|
|
197
|
+
if (latest.status === "failed") throw new Error(`Workflow ${this.workflowRun.workflowName} failed: ${JSON.stringify(latest.error)}`);
|
|
198
|
+
if (latest.status === "canceled") throw new Error(`Workflow ${this.workflowRun.workflowName} was canceled`);
|
|
199
|
+
if (Date.now() - start > this.resultTimeoutMs) throw new Error(`Timed out waiting for workflow run ${this.workflowRun.id} to finish`);
|
|
200
|
+
await new Promise((resolve) => {
|
|
201
|
+
setTimeout(resolve, this.resultPollIntervalMs);
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Cancels the workflow run. Only workflows in pending, running, sleeping,
|
|
207
|
+
* or paused status can be canceled.
|
|
208
|
+
*/
|
|
209
|
+
async cancel() {
|
|
210
|
+
await this.backend.cancelWorkflowRun({ workflowRunId: this.workflowRun.id });
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Pauses the workflow run. Only workflows in pending, running, or sleeping
|
|
214
|
+
* status can be paused.
|
|
215
|
+
*/
|
|
216
|
+
async pause() {
|
|
217
|
+
await this.backend.pauseWorkflowRun({ workflowRunId: this.workflowRun.id });
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Resumes a paused workflow run. Sets the status back to pending so that
|
|
221
|
+
* a worker can reclaim it.
|
|
222
|
+
*/
|
|
223
|
+
async resume() {
|
|
224
|
+
await this.backend.resumeWorkflowRun({ workflowRunId: this.workflowRun.id });
|
|
225
|
+
}
|
|
226
|
+
};
|
|
223
227
|
|
|
228
|
+
//#endregion
|
|
229
|
+
export { OpenWorkflow, declareWorkflow };
|
|
224
230
|
//# sourceMappingURL=client.js.map
|
package/dist/client.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/client.ts"],"sourcesContent":["import type { Backend } from \"./backend\";\nimport { serializeRetryPolicy } from \"./core/retry\";\nimport type { StandardSchemaV1 } from \"./core/schema\";\nimport type { SchemaInput, SchemaOutput, WorkflowRun } from \"./core/workflow\";\nimport { validateInput } from \"./core/workflow\";\nimport type { WorkflowFunction } from \"./execution\";\nimport { WorkflowRegistry } from \"./registry\";\nimport { Worker, type WorkerOptions } from \"./worker\";\nimport { defineWorkflow, defineWorkflowSpec, type Workflow, type WorkflowSpec } from \"./workflow\";\n\nconst DEFAULT_RESULT_POLL_INTERVAL_MS = 1000; // 1s\nconst DEFAULT_RESULT_TIMEOUT_MS = 5 * 60 * 1000; // 5m\n\n/* The data the worker function receives (after transformation). */\ntype WorkflowHandlerInput<TSchema, Input> = SchemaOutput<TSchema, Input>;\n\n/* The data the client sends (before transformation) */\ntype WorkflowRunInput<TSchema, Input> = SchemaInput<TSchema, Input>;\n\n/**\n * Options for the OpenWorkflow client.\n */\nexport interface OpenWorkflowOptions {\n backend: Backend;\n}\n\n/**\n * Client used to register workflows and start runs.\n */\nexport class OpenWorkflow {\n private backend: Backend;\n private registry = new WorkflowRegistry();\n\n constructor(options: OpenWorkflowOptions) {\n this.backend = options.backend;\n }\n\n /**\n * Create a new Worker with this client's backend and workflows.\n * @param options - Worker options\n * @param options.concurrency - Max concurrent workflow runs\n * @returns Worker instance\n */\n newWorker(options?: {\n concurrency?: number | undefined;\n usePubSub?: boolean;\n listenDelay?: number;\n }): Worker {\n return new Worker({\n backend: this.backend,\n registry: this.registry,\n concurrency: options?.concurrency,\n usePubSub: options?.usePubSub,\n listenDelay: options?.listenDelay,\n } satisfies WorkerOptions);\n }\n\n /**\n * Provide the implementation for a declared workflow. This links the workflow\n * specification to its execution logic and registers it with this\n * OpenWorkflow instance for worker execution.\n * @param spec - Workflow spec\n * @param fn - Workflow implementation\n */\n implementWorkflow<Input, Output, RunInput = Input>(\n spec: WorkflowSpec<Input, Output, RunInput>,\n fn: WorkflowFunction<Input, Output>,\n ): void {\n const workflow: Workflow<Input, Output, RunInput> = { spec, fn };\n this.registry.register(workflow as Workflow<unknown, unknown, unknown>);\n }\n\n /**\n * Run a workflow from its specification. This is the primary way to schedule\n * a workflow using only its WorkflowSpec.\n * @param spec - Workflow spec\n * @param input - Workflow input\n * @param options - Run options\n * @returns Handle for awaiting the result\n * @example\n * ```ts\n * const handle = await ow.runWorkflow(emailWorkflow, { to: 'user@example.com' });\n * const result = await handle.result();\n * ```\n */\n async runWorkflow<Input, Output, RunInput = Input>(\n spec: WorkflowSpec<Input, Output, RunInput>,\n input?: RunInput,\n options?: WorkflowRunOptions,\n ): Promise<WorkflowRunHandle<Output>> {\n const validationResult = await validateInput(spec.schema, input);\n if (!validationResult.success) {\n throw new Error(validationResult.error);\n }\n const parsedInput = validationResult.value;\n const workflowRun = await this.backend.createWorkflowRun({\n workflowName: spec.name,\n version: spec.version ?? null,\n idempotencyKey: null,\n config: {},\n context: null,\n input: parsedInput ?? null,\n availableAt: null,\n deadlineAt: options?.deadlineAt ?? null,\n retryPolicy: spec.retryPolicy ? serializeRetryPolicy(spec.retryPolicy) : undefined,\n });\n\n if (options?.publishToChannel) {\n await this.backend.publish(workflowRun.id);\n }\n\n return new WorkflowRunHandle<Output>({\n backend: this.backend,\n workflowRun: workflowRun,\n resultPollIntervalMs: DEFAULT_RESULT_POLL_INTERVAL_MS,\n resultTimeoutMs: DEFAULT_RESULT_TIMEOUT_MS,\n });\n }\n\n /**\n * Define and register a new workflow.\n *\n * This is a convenience method that combines `declareWorkflow` and\n * `implementWorkflow` into a single call. For better code splitting and to\n * separate declaration from implementation, consider using those methods\n * separately.\n * @param config - Workflow config\n * @param fn - Workflow implementation\n * @returns Runnable workflow\n * @example\n * ```ts\n * const workflow = ow.defineWorkflow(\n * { name: 'my-workflow' },\n * async ({ input, step }) => {\n * // workflow implementation\n * },\n * );\n * ```\n */\n defineWorkflow<Input, Output, TSchema extends StandardSchemaV1 | undefined = undefined>(\n spec: WorkflowSpec<\n WorkflowHandlerInput<TSchema, Input>,\n Output,\n WorkflowRunInput<TSchema, Input>\n >,\n fn: WorkflowFunction<WorkflowHandlerInput<TSchema, Input>, Output>,\n ): RunnableWorkflow<\n WorkflowHandlerInput<TSchema, Input>,\n Output,\n WorkflowRunInput<TSchema, Input>\n > {\n const workflow = defineWorkflow(spec, fn);\n this.registry.register(workflow as Workflow<unknown, unknown, unknown>);\n return new RunnableWorkflow(this, workflow);\n }\n\n /**\n * Unregister a workflow from the registry.\n * @param name - The workflow name\n * @param version - The workflow version (null for unversioned)\n * @example\n * ```ts\n * ow.unregisterWorkflow(\"my-workflow\", \"v1\");\n * ```\n */\n unregisterWorkflow(name: string, version: string | null): void {\n this.registry.remove(name, version);\n }\n\n /**\n * Check if a workflow is registered in the registry.\n * @param name - The workflow name\n * @param version - The workflow version (null for unversioned)\n * @returns True if the workflow is registered, false otherwise\n * @example\n * ```ts\n * ow.isWorkflowRegistered(\"my-workflow\", \"v1\");\n * ```\n */\n isWorkflowRegistered(name: string, version: string | null): boolean {\n return this.registry.has(name, version);\n }\n}\n\n/**\n * Declare a workflow without providing its implementation (which is provided\n * separately via `implementWorkflow`). Returns a lightweight WorkflowSpec\n * that can be used to schedule workflow runs.\n * @param spec - Workflow spec\n * @returns Workflow spec\n * @example\n * ```ts\n * export const emailWorkflow = declareWorkflow({\n * name: 'send-email',\n * schema: z.object({ to: z.string().email() }),\n * });\n * ```\n */\n// kept for backwards compatibility, to be deprecated\n// eslint-disable-next-line unicorn/prefer-export-from\nexport const declareWorkflow = defineWorkflowSpec;\n\n//\n// --- Workflow Definition\n//\n\n/**\n * A fully defined workflow with its implementation. This class is returned by\n * `defineWorkflow` and provides the `.run()` method for scheduling workflow\n * runs.\n */\nexport class RunnableWorkflow<Input, Output, RunInput = Input> {\n private readonly ow: OpenWorkflow;\n readonly workflow: Workflow<Input, Output, RunInput>;\n\n constructor(ow: OpenWorkflow, workflow: Workflow<Input, Output, RunInput>) {\n this.ow = ow;\n this.workflow = workflow;\n }\n\n /**\n * Starts a new workflow run.\n * @param input - Workflow input\n * @param options - Run options\n * @returns Workflow run handle\n */\n async run(input?: RunInput, options?: WorkflowRunOptions): Promise<WorkflowRunHandle<Output>> {\n return this.ow.runWorkflow(this.workflow.spec, input, options);\n }\n}\n\n//\n// --- Workflow Run\n//\n\n/**\n * Options for creating a new workflow run from a runnable workflow when calling\n * `workflow.run()`.\n */\nexport interface WorkflowRunOptions {\n /**\n * Set a deadline for the workflow run. If the workflow exceeds this deadline,\n * it will be marked as failed.\n */\n deadlineAt?: Date;\n\n /**\n * Publish when the workflow run is created to the channel.\n * Default: true\n */\n publishToChannel?: boolean;\n}\n\n/**\n * Options for WorkflowHandle.\n */\nexport interface WorkflowHandleOptions {\n backend: Backend;\n workflowRun: WorkflowRun;\n resultPollIntervalMs: number;\n resultTimeoutMs: number;\n}\n\n/**\n * Represents a started workflow run and provides methods to await its result.\n * Returned from `workflowDef.run()`.\n */\nexport class WorkflowRunHandle<Output> {\n private backend: Backend;\n readonly workflowRun: WorkflowRun;\n private resultPollIntervalMs: number;\n private resultTimeoutMs: number;\n\n constructor(options: WorkflowHandleOptions) {\n this.backend = options.backend;\n this.workflowRun = options.workflowRun;\n this.resultPollIntervalMs = options.resultPollIntervalMs;\n this.resultTimeoutMs = options.resultTimeoutMs;\n }\n\n /**\n * Waits for the workflow run to complete and returns the result.\n * @returns Workflow output\n */\n async result(): Promise<Output> {\n const start = Date.now();\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n while (true) {\n const latest = await this.backend.getWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n\n if (!latest) {\n throw new Error(`Workflow run ${this.workflowRun.id} no longer exists`);\n }\n\n // 'succeeded' status is deprecated\n if (latest.status === \"succeeded\" || latest.status === \"completed\") {\n return latest.output as Output;\n }\n\n if (latest.status === \"failed\") {\n throw new Error(\n `Workflow ${this.workflowRun.workflowName} failed: ${JSON.stringify(latest.error)}`,\n );\n }\n\n if (latest.status === \"canceled\") {\n throw new Error(`Workflow ${this.workflowRun.workflowName} was canceled`);\n }\n\n if (Date.now() - start > this.resultTimeoutMs) {\n throw new Error(`Timed out waiting for workflow run ${this.workflowRun.id} to finish`);\n }\n\n await new Promise((resolve) => {\n setTimeout(resolve, this.resultPollIntervalMs);\n });\n }\n }\n\n /**\n * Cancels the workflow run. Only workflows in pending, running, or sleeping\n * status can be canceled.\n */\n async cancel(): Promise<void> {\n await this.backend.cancelWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n }\n}\n"],"names":["serializeRetryPolicy","validateInput","WorkflowRegistry","Worker","defineWorkflow","defineWorkflowSpec","DEFAULT_RESULT_POLL_INTERVAL_MS","DEFAULT_RESULT_TIMEOUT_MS","OpenWorkflow","backend","registry","options","newWorker","concurrency","usePubSub","listenDelay","implementWorkflow","spec","fn","workflow","register","runWorkflow","input","validationResult","schema","success","Error","error","parsedInput","value","workflowRun","createWorkflowRun","workflowName","name","version","idempotencyKey","config","context","availableAt","deadlineAt","retryPolicy","undefined","publishToChannel","publish","id","WorkflowRunHandle","resultPollIntervalMs","resultTimeoutMs","RunnableWorkflow","unregisterWorkflow","remove","isWorkflowRegistered","has","declareWorkflow","ow","run","result","start","Date","now","latest","getWorkflowRun","workflowRunId","status","output","JSON","stringify","Promise","resolve","setTimeout","cancel","cancelWorkflowRun"],"mappings":"AACA,SAASA,oBAAoB,QAAQ,kBAAe;AAGpD,SAASC,aAAa,QAAQ,qBAAkB;AAEhD,SAASC,gBAAgB,QAAQ,gBAAa;AAC9C,SAASC,MAAM,QAA4B,cAAW;AACtD,SAASC,cAAc,EAAEC,kBAAkB,QAA0C,gBAAa;AAElG,MAAMC,kCAAkC,MAAM,KAAK;AACnD,MAAMC,4BAA4B,IAAI,KAAK,MAAM,KAAK;AAetD;;CAEC,GACD,OAAO,MAAMC;IACHC,QAAiB;IACjBC,WAAW,IAAIR,mBAAmB;IAE1C,YAAYS,OAA4B,CAAE;QACxC,IAAI,CAACF,OAAO,GAAGE,QAAQF,OAAO;IAChC;IAEA;;;;;GAKC,GACDG,UAAUD,OAIT,EAAU;QACT,OAAO,IAAIR,OAAO;YAChBM,SAAS,IAAI,CAACA,OAAO;YACrBC,UAAU,IAAI,CAACA,QAAQ;YACvBG,aAAaF,SAASE;YACtBC,WAAWH,SAASG;YACpBC,aAAaJ,SAASI;QACxB;IACF;IAEA;;;;;;GAMC,GACDC,kBACEC,IAA2C,EAC3CC,EAAmC,EAC7B;QACN,MA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EAAE,IAAI,CAACI,WAAW,CAACc,EAAE,CAAC,iBAAiB,CAAC;YACxE;YAEA,mCAAmC;YACnC,IAAIgB,OAAOG,MAAM,KAAK,eAAeH,OAAOG,MAAM,KAAK,aAAa;gBAClE,OAAOH,OAAOI,MAAM;YACtB;YAEA,IAAIJ,OAAOG,MAAM,KAAK,UAAU;gBAC9B,MAAM,IAAIrC,MACR,CAAC,SAAS,EAAE,IAAI,CAACI,WAAW,CAACE,YAAY,CAAC,SAAS,EAAEiC,KAAKC,SAAS,CAACN,OAAOjC,KAAK,GAAG;YAEvF;YAEA,IAAIiC,OAAOG,MAAM,KAAK,YAAY;gBAChC,MAAM,IAAIrC,MAAM,CAAC,SAAS,EAAE,IAAI,CAACI,WAAW,CAACE,YAAY,CAAC,aAAa,CAAC;YAC1E;YAEA,IAAI0B,KAAKC,GAAG,KAAKF,QAAQ,IAAI,CAACV,eAAe,EAAE;gBAC7C,MAAM,IAAIrB,MAAM,CAAC,mCAAmC,EAAE,IAAI,CAACI,WAAW,CAACc,EAAE,CAAC,UAAU,CAAC;YACvF;YAEA,MAAM,IAAIuB,QAAQ,CAACC;gBACjBC,WAAWD,SAAS,IAAI,CAACtB,oBAAoB;YAC/C;QACF;IACF;IAEA;;;GAGC,GACD,MAAMwB,SAAwB;QAC5B,MAAM,IAAI,CAAC7D,OAAO,CAAC8D,iBAAiB,CAAC;YACnCT,eAAe,IAAI,CAAChC,WAAW,CAACc,EAAE;QACpC;IACF;AACF"}
|
|
1
|
+
{"version":3,"file":"client.js","names":["workflow: Workflow<Input, Output, RunInput>"],"sources":["../src/client.ts"],"sourcesContent":["import { type Backend } from \"./backend\";\nimport { serializeRetryPolicy } from \"./core/retry\";\nimport { type StandardSchemaV1 } from \"./core/schema\";\nimport { type SchemaInput, type SchemaOutput, type WorkflowRun } from \"./core/workflow\";\nimport { validateInput } from \"./core/workflow\";\nimport { type WorkflowFunction } from \"./execution\";\nimport { WorkflowRegistry } from \"./registry\";\nimport { Worker } from \"./worker\";\nimport { type WorkerOptions } from \"./worker\";\nimport { defineWorkflow, defineWorkflowSpec } from \"./workflow\";\nimport { type Workflow, type WorkflowSpec } from \"./workflow\";\n\nconst DEFAULT_RESULT_POLL_INTERVAL_MS = 1000; // 1s\nconst DEFAULT_RESULT_TIMEOUT_MS = 5 * 60 * 1000; // 5m\n\n/* The data the worker function receives (after transformation). */\ntype WorkflowHandlerInput<TSchema, Input> = SchemaOutput<TSchema, Input>;\n\n/* The data the client sends (before transformation) */\ntype WorkflowRunInput<TSchema, Input> = SchemaInput<TSchema, Input>;\n\n/**\n * Options for the OpenWorkflow client.\n */\nexport interface OpenWorkflowOptions {\n backend: Backend;\n}\n\n/**\n * Client used to register workflows and start runs.\n */\nexport class OpenWorkflow {\n private backend: Backend;\n private registry = new WorkflowRegistry();\n\n constructor(options: OpenWorkflowOptions) {\n this.backend = options.backend;\n }\n\n /**\n * Create a new Worker with this client's backend and workflows.\n * @param options - Worker options\n * @param options.concurrency - Max concurrent workflow runs\n * @returns Worker instance\n */\n newWorker(options?: {\n concurrency?: number | undefined;\n usePubSub?: boolean;\n listenDelay?: number;\n }): Worker {\n return new Worker({\n backend: this.backend,\n registry: this.registry,\n concurrency: options?.concurrency,\n usePubSub: options?.usePubSub,\n listenDelay: options?.listenDelay,\n } satisfies WorkerOptions);\n }\n\n /**\n * Provide the implementation for a declared workflow. This links the workflow\n * specification to its execution logic and registers it with this\n * OpenWorkflow instance for worker execution.\n * @param spec - Workflow spec\n * @param fn - Workflow implementation\n */\n implementWorkflow<Input, Output, RunInput = Input>(\n spec: WorkflowSpec<Input, Output, RunInput>,\n fn: WorkflowFunction<Input, Output>,\n ): void {\n const workflow: Workflow<Input, Output, RunInput> = { spec, fn };\n this.registry.register(workflow as Workflow<unknown, unknown, unknown>);\n }\n\n /**\n * Run a workflow from its specification. This is the primary way to schedule\n * a workflow using only its WorkflowSpec.\n * @param spec - Workflow spec\n * @param input - Workflow input\n * @param options - Run options\n * @returns Handle for awaiting the result\n * @example\n * ```ts\n * const handle = await ow.runWorkflow(emailWorkflow, { to: 'user@example.com' });\n * const result = await handle.result();\n * ```\n */\n async runWorkflow<Input, Output, RunInput = Input>(\n spec: WorkflowSpec<Input, Output, RunInput>,\n input?: RunInput,\n options?: WorkflowRunOptions,\n ): Promise<WorkflowRunHandle<Output>> {\n const validationResult = await validateInput(spec.schema, input);\n if (!validationResult.success) {\n throw new Error(validationResult.error);\n }\n const parsedInput = validationResult.value;\n const workflowRun = await this.backend.createWorkflowRun({\n workflowName: spec.name,\n version: spec.version ?? null,\n idempotencyKey: null,\n config: {},\n context: null,\n input: parsedInput ?? null,\n availableAt: null,\n deadlineAt: options?.deadlineAt ?? null,\n retryPolicy: spec.retryPolicy ? serializeRetryPolicy(spec.retryPolicy) : undefined,\n });\n\n if (options?.publishToChannel) {\n await this.backend.publish(workflowRun.id);\n }\n\n return new WorkflowRunHandle<Output>({\n backend: this.backend,\n workflowRun: workflowRun,\n resultPollIntervalMs: DEFAULT_RESULT_POLL_INTERVAL_MS,\n resultTimeoutMs: DEFAULT_RESULT_TIMEOUT_MS,\n });\n }\n\n /**\n * Define and register a new workflow.\n *\n * This is a convenience method that combines `declareWorkflow` and\n * `implementWorkflow` into a single call. For better code splitting and to\n * separate declaration from implementation, consider using those methods\n * separately.\n * @param config - Workflow config\n * @param fn - Workflow implementation\n * @returns Runnable workflow\n * @example\n * ```ts\n * const workflow = ow.defineWorkflow(\n * { name: 'my-workflow' },\n * async ({ input, step }) => {\n * // workflow implementation\n * },\n * );\n * ```\n */\n defineWorkflow<Input, Output, TSchema extends StandardSchemaV1 | undefined = undefined>(\n spec: WorkflowSpec<\n WorkflowHandlerInput<TSchema, Input>,\n Output,\n WorkflowRunInput<TSchema, Input>\n >,\n fn: WorkflowFunction<WorkflowHandlerInput<TSchema, Input>, Output>,\n ): RunnableWorkflow<\n WorkflowHandlerInput<TSchema, Input>,\n Output,\n WorkflowRunInput<TSchema, Input>\n > {\n const workflow = defineWorkflow(spec, fn);\n this.registry.register(workflow as Workflow<unknown, unknown, unknown>);\n return new RunnableWorkflow(this, workflow);\n }\n\n /**\n * Unregister a workflow from the registry.\n * @param name - The workflow name\n * @param version - The workflow version (null for unversioned)\n * @example\n * ```ts\n * ow.unregisterWorkflow(\"my-workflow\", \"v1\");\n * ```\n */\n unregisterWorkflow(name: string, version: string | null): void {\n this.registry.remove(name, version);\n }\n\n /**\n * Check if a workflow is registered in the registry.\n * @param name - The workflow name\n * @param version - The workflow version (null for unversioned)\n * @returns True if the workflow is registered, false otherwise\n * @example\n * ```ts\n * ow.isWorkflowRegistered(\"my-workflow\", \"v1\");\n * ```\n */\n isWorkflowRegistered(name: string, version: string | null): boolean {\n return this.registry.has(name, version);\n }\n}\n\n/**\n * Declare a workflow without providing its implementation (which is provided\n * separately via `implementWorkflow`). Returns a lightweight WorkflowSpec\n * that can be used to schedule workflow runs.\n * @param spec - Workflow spec\n * @returns Workflow spec\n * @example\n * ```ts\n * export const emailWorkflow = declareWorkflow({\n * name: 'send-email',\n * schema: z.object({ to: z.string().email() }),\n * });\n * ```\n */\n// kept for backwards compatibility, to be deprecated\n// eslint-disable-next-line unicorn/prefer-export-from\nexport const declareWorkflow = defineWorkflowSpec;\n\n//\n// --- Workflow Definition\n//\n\n/**\n * A fully defined workflow with its implementation. This class is returned by\n * `defineWorkflow` and provides the `.run()` method for scheduling workflow\n * runs.\n */\nexport class RunnableWorkflow<Input, Output, RunInput = Input> {\n private readonly ow: OpenWorkflow;\n readonly workflow: Workflow<Input, Output, RunInput>;\n\n constructor(ow: OpenWorkflow, workflow: Workflow<Input, Output, RunInput>) {\n this.ow = ow;\n this.workflow = workflow;\n }\n\n /**\n * Starts a new workflow run.\n * @param input - Workflow input\n * @param options - Run options\n * @returns Workflow run handle\n */\n async run(input?: RunInput, options?: WorkflowRunOptions): Promise<WorkflowRunHandle<Output>> {\n return this.ow.runWorkflow(this.workflow.spec, input, options);\n }\n}\n\n//\n// --- Workflow Run\n//\n\n/**\n * Options for creating a new workflow run from a runnable workflow when calling\n * `workflow.run()`.\n */\nexport interface WorkflowRunOptions {\n /**\n * Set a deadline for the workflow run. If the workflow exceeds this deadline,\n * it will be marked as failed.\n */\n deadlineAt?: Date;\n\n /**\n * Publish when the workflow run is created to the channel.\n * Default: true\n */\n publishToChannel?: boolean;\n}\n\n/**\n * Options for WorkflowHandle.\n */\nexport interface WorkflowHandleOptions {\n backend: Backend;\n workflowRun: WorkflowRun;\n resultPollIntervalMs: number;\n resultTimeoutMs: number;\n}\n\n/**\n * Represents a started workflow run and provides methods to await its result.\n * Returned from `workflowDef.run()`.\n */\nexport class WorkflowRunHandle<Output> {\n private backend: Backend;\n readonly workflowRun: WorkflowRun;\n private resultPollIntervalMs: number;\n private resultTimeoutMs: number;\n\n constructor(options: WorkflowHandleOptions) {\n this.backend = options.backend;\n this.workflowRun = options.workflowRun;\n this.resultPollIntervalMs = options.resultPollIntervalMs;\n this.resultTimeoutMs = options.resultTimeoutMs;\n }\n\n /**\n * Waits for the workflow run to complete and returns the result.\n * @returns Workflow output\n */\n async result(): Promise<Output> {\n const start = Date.now();\n\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n while (true) {\n const latest = await this.backend.getWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n\n if (!latest) {\n throw new Error(`Workflow run ${this.workflowRun.id} no longer exists`);\n }\n\n // 'succeeded' status is deprecated\n if (latest.status === \"succeeded\" || latest.status === \"completed\") {\n return latest.output as Output;\n }\n\n if (latest.status === \"failed\") {\n throw new Error(\n `Workflow ${this.workflowRun.workflowName} failed: ${JSON.stringify(latest.error)}`,\n );\n }\n\n if (latest.status === \"canceled\") {\n throw new Error(`Workflow ${this.workflowRun.workflowName} was canceled`);\n }\n\n if (Date.now() - start > this.resultTimeoutMs) {\n throw new Error(`Timed out waiting for workflow run ${this.workflowRun.id} to finish`);\n }\n\n await new Promise((resolve) => {\n setTimeout(resolve, this.resultPollIntervalMs);\n });\n }\n }\n\n /**\n * Cancels the workflow run. Only workflows in pending, running, sleeping,\n * or paused status can be canceled.\n */\n async cancel(): Promise<void> {\n await this.backend.cancelWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n }\n\n /**\n * Pauses the workflow run. Only workflows in pending, running, or sleeping\n * status can be paused.\n */\n async pause(): Promise<void> {\n await this.backend.pauseWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n }\n\n /**\n * Resumes a paused workflow run. Sets the status back to pending so that\n * a worker can reclaim it.\n */\n async resume(): Promise<void> {\n await this.backend.resumeWorkflowRun({\n workflowRunId: this.workflowRun.id,\n });\n }\n}\n"],"mappings":";;;;;;;AAYA,MAAM,kCAAkC;AACxC,MAAM,4BAA4B,MAAS;;;;AAkB3C,IAAa,eAAb,MAA0B;CACxB,AAAQ;CACR,AAAQ,WAAW,IAAI,kBAAkB;CAEzC,YAAY,SAA8B;AACxC,OAAK,UAAU,QAAQ;;;;;;;;CASzB,UAAU,SAIC;AACT,SAAO,IAAI,OAAO;GAChB,SAAS,KAAK;GACd,UAAU,KAAK;GACf,aAAa,SAAS;GACtB,WAAW,SAAS;GACpB,aAAa,SAAS;GACvB,CAAyB;;;;;;;;;CAU5B,kBACE,MACA,IACM;EACN,MAAMA,WAA8C;GAAE;GAAM;GAAI;AAChE,OAAK,SAAS,SAAS,SAAgD;;;;;;;;;;;;;;;CAgBzE,MAAM,YACJ,MACA,OACA,SACoC;EACpC,MAAM,mBAAmB,MAAM,cAAc,KAAK,QAAQ,MAAM;AAChE,MAAI,CAAC,iBAAiB,QACpB,OAAM,IAAI,MAAM,iBAAiB,MAAM;EAEzC,MAAM,cAAc,iBAAiB;EACrC,MAAM,cAAc,MAAM,KAAK,QAAQ,kBAAkB;GACvD,cAAc,KAAK;GACnB,SAAS,KAAK,WAAW;GACzB,gBAAgB;GAChB,QAAQ,EAAE;GACV,SAAS;GACT,OAAO,eAAe;GACtB,aAAa;GACb,YAAY,SAAS,cAAc;GACnC,aAAa,KAAK,cAAc,qBAAqB,KAA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,eAAe,OAAO,WAAW,YACrD,QAAO,OAAO;AAGhB,OAAI,OAAO,WAAW,SACpB,OAAM,IAAI,MACR,YAAY,KAAK,YAAY,aAAa,WAAW,KAAK,UAAU,OAAO,MAAM,GAClF;AAGH,OAAI,OAAO,WAAW,WACpB,OAAM,IAAI,MAAM,YAAY,KAAK,YAAY,aAAa,eAAe;AAG3E,OAAI,KAAK,KAAK,GAAG,QAAQ,KAAK,gBAC5B,OAAM,IAAI,MAAM,sCAAsC,KAAK,YAAY,GAAG,YAAY;AAGxF,SAAM,IAAI,SAAS,YAAY;AAC7B,eAAW,SAAS,KAAK,qBAAqB;KAC9C;;;;;;;CAQN,MAAM,SAAwB;AAC5B,QAAM,KAAK,QAAQ,kBAAkB,EACnC,eAAe,KAAK,YAAY,IACjC,CAAC;;;;;;CAOJ,MAAM,QAAuB;AAC3B,QAAM,KAAK,QAAQ,iBAAiB,EAClC,eAAe,KAAK,YAAY,IACjC,CAAC;;;;;;CAOJ,MAAM,SAAwB;AAC5B,QAAM,KAAK,QAAQ,kBAAkB,EACnC,eAAe,KAAK,YAAY,IACjC,CAAC"}
|
package/dist/config.d.ts
CHANGED
|
@@ -1,16 +1,19 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { Backend } from "./backend.js";
|
|
2
|
+
import { WorkerOptions } from "./worker.js";
|
|
3
|
+
import * as c120 from "c12";
|
|
4
|
+
|
|
5
|
+
//#region src/config.d.ts
|
|
6
|
+
interface OpenWorkflowConfig {
|
|
7
|
+
backend: Backend;
|
|
8
|
+
worker?: WorkerConfig;
|
|
6
9
|
}
|
|
7
|
-
|
|
10
|
+
type WorkerConfig = Pick<WorkerOptions, "concurrency">;
|
|
8
11
|
/**
|
|
9
12
|
* Create a typed OpenWorkflow configuration.
|
|
10
13
|
* @param config - the config
|
|
11
14
|
* @returns the config
|
|
12
15
|
*/
|
|
13
|
-
|
|
16
|
+
declare function defineConfig(config: OpenWorkflowConfig): OpenWorkflowConfig;
|
|
14
17
|
/**
|
|
15
18
|
* Load the OpenWorkflow config at openworkflow.config.ts (or other extension;
|
|
16
19
|
* see https://github.com/unjs/c12)
|
|
@@ -18,5 +21,9 @@ export declare function defineConfig(config: OpenWorkflowConfig): OpenWorkflowCo
|
|
|
18
21
|
* process.cwd())
|
|
19
22
|
* @returns The loaded configuration and metadata
|
|
20
23
|
*/
|
|
21
|
-
|
|
24
|
+
declare function loadConfig$1(rootDir?: string): Promise<c120.ResolvedConfig<OpenWorkflowConfig, c120.ConfigLayerMeta>>;
|
|
25
|
+
//# sourceMappingURL=config.d.ts.map
|
|
26
|
+
|
|
27
|
+
//#endregion
|
|
28
|
+
export { OpenWorkflowConfig, WorkerConfig, defineConfig, loadConfig$1 as loadConfig };
|
|
22
29
|
//# sourceMappingURL=config.d.ts.map
|
package/dist/config.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"config.d.ts","
|
|
1
|
+
{"version":3,"file":"config.d.ts","names":[],"sources":["../src/config.ts"],"sourcesContent":[],"mappings":";;;;;UAKiB,kBAAA;WACN;WACA;AAFX;AAAmC,KAKvB,YAAA,GAAe,IALQ,CAKH,aALG,EAAA,aAAA,CAAA;;;;AAKnC;;AAAgC,iBAOhB,YAAA,CAPgB,MAAA,EAOK,kBAPL,CAAA,EAO0B,kBAP1B;;;AAOhC;;;;;AAWsB,iBAAA,YAAA,CAAU,OAAA,CAAA,EAAA,MAAA,CAAA,EAAiB,OAAjB,CAAiB,IAAA,CAAA,cAAjB,CAAiB,kBAAjB,EAAiB,IAAA,CAAA,eAAA,CAAjB,CAAA"}
|
package/dist/config.js
CHANGED
|
@@ -1,23 +1,28 @@
|
|
|
1
|
-
import { loadConfig
|
|
1
|
+
import { loadConfig } from "c12";
|
|
2
|
+
|
|
3
|
+
//#region src/config.ts
|
|
2
4
|
/**
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
* Create a typed OpenWorkflow configuration.
|
|
6
|
+
* @param config - the config
|
|
7
|
+
* @returns the config
|
|
8
|
+
*/
|
|
9
|
+
function defineConfig(config) {
|
|
10
|
+
return config;
|
|
8
11
|
}
|
|
9
12
|
/**
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
13
|
+
* Load the OpenWorkflow config at openworkflow.config.ts (or other extension;
|
|
14
|
+
* see https://github.com/unjs/c12)
|
|
15
|
+
* @param rootDir - Optional root directory to search from (defaults to
|
|
16
|
+
* process.cwd())
|
|
17
|
+
* @returns The loaded configuration and metadata
|
|
18
|
+
*/
|
|
19
|
+
async function loadConfig$1(rootDir) {
|
|
20
|
+
return await loadConfig({
|
|
21
|
+
cwd: rootDir ?? process.cwd(),
|
|
22
|
+
name: "openworkflow"
|
|
23
|
+
});
|
|
21
24
|
}
|
|
22
25
|
|
|
26
|
+
//#endregion
|
|
27
|
+
export { defineConfig, loadConfig$1 as loadConfig };
|
|
23
28
|
//# sourceMappingURL=config.js.map
|
package/dist/config.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/config.ts"],"sourcesContent":["import { loadConfig as loadC12Config } from \"c12\";\nimport type
|
|
1
|
+
{"version":3,"file":"config.js","names":["loadConfig","loadC12Config"],"sources":["../src/config.ts"],"sourcesContent":["import { loadConfig as loadC12Config } from \"c12\";\n\nimport { type Backend } from \"./backend\";\nimport { type WorkerOptions } from \"./worker\";\n\nexport interface OpenWorkflowConfig {\n backend: Backend;\n worker?: WorkerConfig;\n}\n\nexport type WorkerConfig = Pick<WorkerOptions, \"concurrency\">;\n\n/**\n * Create a typed OpenWorkflow configuration.\n * @param config - the config\n * @returns the config\n */\nexport function defineConfig(config: OpenWorkflowConfig): OpenWorkflowConfig {\n return config;\n}\n\n/**\n * Load the OpenWorkflow config at openworkflow.config.ts (or other extension;\n * see https://github.com/unjs/c12)\n * @param rootDir - Optional root directory to search from (defaults to\n * process.cwd())\n * @returns The loaded configuration and metadata\n */\nexport async function loadConfig(rootDir?: string) {\n const cwd = rootDir ?? process.cwd();\n\n return await loadC12Config<OpenWorkflowConfig>({\n cwd,\n name: \"openworkflow\",\n });\n}\n"],"mappings":";;;;;;;;AAiBA,SAAgB,aAAa,QAAgD;AAC3E,QAAO;;;;;;;;;AAUT,eAAsBA,aAAW,SAAkB;AAGjD,QAAO,MAAMC,WAAkC;EAC7C,KAHU,WAAW,QAAQ,KAAK;EAIlC,MAAM;EACP,CAAC"}
|