@hatchet-dev/typescript-sdk 1.3.2 → 1.4.0-alpha.2
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/clients/dispatcher/heartbeat/heartbeat-worker.js +3 -2
- package/clients/hatchet-client/hatchet-client.d.ts +3 -5
- package/clients/hatchet-client/hatchet-client.js +11 -72
- package/clients/worker/worker.d.ts +6 -9
- package/clients/worker/worker.js +2 -164
- package/examples/fanout-worker.js +1 -2
- package/examples/manual-trigger.js +1 -1
- package/package.json +3 -2
- package/step.d.ts +13 -11
- package/step.js +44 -40
- package/util/grpc-helpers.d.ts +10 -0
- package/util/grpc-helpers.js +79 -0
- package/v1/client/admin.d.ts +64 -0
- package/v1/client/admin.js +155 -0
- package/v1/client/client.d.ts +14 -9
- package/v1/client/client.interface.d.ts +4 -2
- package/v1/client/client.js +28 -16
- package/v1/client/worker/context.d.ts +246 -0
- package/v1/client/worker/context.js +512 -0
- package/v1/client/worker/worker-internal.d.ts +62 -0
- package/v1/client/worker/worker-internal.js +703 -0
- package/v1/client/{worker.d.ts → worker/worker.d.ts} +15 -15
- package/v1/client/{worker.js → worker/worker.js} +14 -11
- package/v1/declaration.d.ts +3 -3
- package/v1/declaration.js +21 -14
- package/v1/examples/cancellations/run.js +4 -4
- package/v1/examples/cancellations/workflow.js +2 -2
- package/v1/examples/high-memory/child-worker.js +29 -0
- package/v1/examples/high-memory/parent-worker.d.ts +1 -0
- package/v1/examples/high-memory/parent-worker.js +29 -0
- package/v1/examples/high-memory/run.d.ts +1 -0
- package/v1/examples/high-memory/run.js +27 -0
- package/v1/examples/high-memory/workflow-with-child.d.ts +12 -0
- package/v1/examples/high-memory/workflow-with-child.js +48 -0
- package/v1/examples/with_timeouts/workflow.js +4 -4
- package/v1/index.d.ts +1 -1
- package/v1/index.js +1 -1
- package/v1/task.d.ts +2 -1
- package/version.d.ts +1 -1
- package/version.js +1 -1
- package/examples/api.js +0 -61
- /package/{examples/api.d.ts → v1/examples/high-memory/child-worker.d.ts} +0 -0
package/v1/client/client.js
CHANGED
|
@@ -22,12 +22,13 @@ const config_loader_1 = require("../../util/config-loader");
|
|
|
22
22
|
const hatchet_logger_1 = require("../../clients/hatchet-client/hatchet-logger");
|
|
23
23
|
const zod_1 = require("zod");
|
|
24
24
|
const declaration_1 = require("../declaration");
|
|
25
|
-
const worker_1 = require("./worker");
|
|
25
|
+
const worker_1 = require("./worker/worker");
|
|
26
26
|
const metrics_1 = require("./features/metrics");
|
|
27
27
|
const workers_1 = require("./features/workers");
|
|
28
28
|
const workflows_1 = require("./features/workflows");
|
|
29
29
|
const runs_1 = require("./features/runs");
|
|
30
30
|
const features_1 = require("./features");
|
|
31
|
+
const admin_1 = require("./admin");
|
|
31
32
|
/**
|
|
32
33
|
* HatchetV1 implements the main client interface for interacting with the Hatchet workflow engine.
|
|
33
34
|
* It provides methods for creating and executing workflows, as well as managing workers.
|
|
@@ -60,14 +61,16 @@ class HatchetClient {
|
|
|
60
61
|
logConstructor = hatchet_logger_1.DEFAULT_LOGGER;
|
|
61
62
|
}
|
|
62
63
|
const clientConfig = Object.assign(Object.assign({}, valid), { logger: logConstructor });
|
|
64
|
+
// FIXME: Remove this once we have a proper namespace validation
|
|
63
65
|
if (clientConfig.namespace) {
|
|
64
66
|
clientConfig.namespace = clientConfig.namespace.endsWith('_')
|
|
65
67
|
? clientConfig.namespace.slice(0, -1)
|
|
66
68
|
: clientConfig.namespace;
|
|
67
69
|
}
|
|
70
|
+
this._config = clientConfig;
|
|
68
71
|
this.tenantId = clientConfig.tenant_id;
|
|
69
72
|
this._api = (0, rest_1.default)(clientConfig.api_url, clientConfig.token, axiosConfig);
|
|
70
|
-
this._v0 = new hatchet_client_1.
|
|
73
|
+
this._v0 = new hatchet_client_1.LegacyHatchetClient(clientConfig, options, axiosConfig, this.runs);
|
|
71
74
|
}
|
|
72
75
|
catch (e) {
|
|
73
76
|
if (e instanceof zod_1.z.ZodError) {
|
|
@@ -86,6 +89,9 @@ class HatchetClient {
|
|
|
86
89
|
static init(config, options, axiosConfig) {
|
|
87
90
|
return new HatchetClient(config, options, axiosConfig);
|
|
88
91
|
}
|
|
92
|
+
get config() {
|
|
93
|
+
return this._config;
|
|
94
|
+
}
|
|
89
95
|
/**
|
|
90
96
|
* Creates a new workflow definition.
|
|
91
97
|
* @template I - The input type for the workflow
|
|
@@ -119,17 +125,19 @@ class HatchetClient {
|
|
|
119
125
|
* @returns A WorkflowRunRef containing the run ID and methods to interact with the run
|
|
120
126
|
*/
|
|
121
127
|
runNoWait(workflow, input, options) {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
129
|
+
let name;
|
|
130
|
+
if (typeof workflow === 'string') {
|
|
131
|
+
name = workflow;
|
|
132
|
+
}
|
|
133
|
+
else if ('id' in workflow) {
|
|
134
|
+
name = workflow.id;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
throw new Error('unable to identify workflow');
|
|
138
|
+
}
|
|
139
|
+
return this.admin.runWorkflow(name, input, options);
|
|
140
|
+
});
|
|
133
141
|
}
|
|
134
142
|
/**
|
|
135
143
|
* @alias run
|
|
@@ -157,7 +165,7 @@ class HatchetClient {
|
|
|
157
165
|
*/
|
|
158
166
|
run(workflow_1, input_1) {
|
|
159
167
|
return __awaiter(this, arguments, void 0, function* (workflow, input, options = {}) {
|
|
160
|
-
const run = this.runNoWait(workflow, input, options);
|
|
168
|
+
const run = yield this.runNoWait(workflow, input, options);
|
|
161
169
|
return run.output;
|
|
162
170
|
});
|
|
163
171
|
}
|
|
@@ -272,10 +280,14 @@ class HatchetClient {
|
|
|
272
280
|
return this._api;
|
|
273
281
|
}
|
|
274
282
|
/**
|
|
275
|
-
*
|
|
283
|
+
* Get the admin client for creating and managing workflows
|
|
284
|
+
* @returns A admin client instance
|
|
276
285
|
*/
|
|
277
286
|
get admin() {
|
|
278
|
-
|
|
287
|
+
if (!this._admin) {
|
|
288
|
+
this._admin = new admin_1.AdminClient(this._v0.config, this.api, this.runs);
|
|
289
|
+
}
|
|
290
|
+
return this._admin;
|
|
279
291
|
}
|
|
280
292
|
/**
|
|
281
293
|
* Creates a new worker instance for processing workflow tasks.
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import { Priority, RunOpts, TaskWorkflowDeclaration, BaseWorkflowDeclaration as WorkflowV1 } from '../../declaration';
|
|
2
|
+
import { JsonObject } from '@bufbuild/protobuf';
|
|
3
|
+
import { Action } from '../../../clients/dispatcher/action-listener';
|
|
4
|
+
import { Logger, LogLevel } from '../../../util/logger';
|
|
5
|
+
import WorkflowRunRef from '../../../util/workflow-run-ref';
|
|
6
|
+
import { Conditions } from '../../conditions';
|
|
7
|
+
import { CreateWorkflowTaskOpts } from '../../task';
|
|
8
|
+
import { OutputType } from '../../types';
|
|
9
|
+
import { Workflow } from '../../../workflow';
|
|
10
|
+
import { HatchetClient } from '../..';
|
|
11
|
+
import { ContextWorker, NextStep } from '../../../step';
|
|
12
|
+
import { V1Worker } from './worker-internal';
|
|
13
|
+
import { Duration } from '../duration';
|
|
14
|
+
type TriggerData = Record<string, Record<string, any>>;
|
|
15
|
+
type ChildRunOpts = RunOpts & {
|
|
16
|
+
key?: string;
|
|
17
|
+
sticky?: boolean;
|
|
18
|
+
};
|
|
19
|
+
interface ContextData<T, K> {
|
|
20
|
+
input: T;
|
|
21
|
+
triggers: TriggerData;
|
|
22
|
+
parents: Record<string, any>;
|
|
23
|
+
triggered_by: string;
|
|
24
|
+
user_data: K;
|
|
25
|
+
step_run_errors: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
export declare class Context<T, K = {}> {
|
|
28
|
+
data: ContextData<T, K>;
|
|
29
|
+
input: T;
|
|
30
|
+
controller: AbortController;
|
|
31
|
+
action: Action;
|
|
32
|
+
v1: HatchetClient;
|
|
33
|
+
worker: ContextWorker;
|
|
34
|
+
overridesData: Record<string, any>;
|
|
35
|
+
logger: Logger;
|
|
36
|
+
spawnIndex: number;
|
|
37
|
+
constructor(action: Action, v1: HatchetClient, worker: V1Worker);
|
|
38
|
+
get abortController(): AbortController;
|
|
39
|
+
get cancelled(): boolean;
|
|
40
|
+
/**
|
|
41
|
+
* Retrieves the output of a parent task.
|
|
42
|
+
* @param parentTask - The a CreateTaskOpts or string of the parent task name.
|
|
43
|
+
* @returns The output of the specified parent task.
|
|
44
|
+
* @throws An error if the task output is not found.
|
|
45
|
+
*/
|
|
46
|
+
parentOutput<L extends OutputType>(parentTask: CreateWorkflowTaskOpts<any, L> | string): Promise<L>;
|
|
47
|
+
/**
|
|
48
|
+
* Returns errors from any task runs in the workflow.
|
|
49
|
+
* @returns A record mapping task names to error messages.
|
|
50
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
51
|
+
* @deprecated use ctx.errors() instead
|
|
52
|
+
*/
|
|
53
|
+
stepRunErrors(): Record<string, string>;
|
|
54
|
+
/**
|
|
55
|
+
* Returns errors from any task runs in the workflow.
|
|
56
|
+
* @returns A record mapping task names to error messages.
|
|
57
|
+
* @throws A warning if no errors are found (this method should be used in on-failure tasks).
|
|
58
|
+
*/
|
|
59
|
+
errors(): Record<string, string>;
|
|
60
|
+
/**
|
|
61
|
+
* Gets the dag conditional triggers for the current workflow run.
|
|
62
|
+
* @returns The triggers for the current workflow.
|
|
63
|
+
*/
|
|
64
|
+
triggers(): TriggerData;
|
|
65
|
+
/**
|
|
66
|
+
* Determines if the workflow was triggered by an event.
|
|
67
|
+
* @returns True if the workflow was triggered by an event, otherwise false.
|
|
68
|
+
*/
|
|
69
|
+
triggeredByEvent(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Gets the name of the current workflow.
|
|
72
|
+
* @returns The name of the workflow.
|
|
73
|
+
*/
|
|
74
|
+
workflowName(): string;
|
|
75
|
+
/**
|
|
76
|
+
* Gets the user data associated with the workflow.
|
|
77
|
+
* @returns The user data.
|
|
78
|
+
*/
|
|
79
|
+
userData(): K;
|
|
80
|
+
/**
|
|
81
|
+
* Gets the name of the current running task.
|
|
82
|
+
* @returns The name of the task.
|
|
83
|
+
*/
|
|
84
|
+
taskName(): string;
|
|
85
|
+
/**
|
|
86
|
+
* Gets the ID of the current workflow run.
|
|
87
|
+
* @returns The workflow run ID.
|
|
88
|
+
*/
|
|
89
|
+
workflowRunId(): string;
|
|
90
|
+
/**
|
|
91
|
+
* Gets the ID of the current task run.
|
|
92
|
+
* @returns The task run ID.
|
|
93
|
+
*/
|
|
94
|
+
taskRunId(): string;
|
|
95
|
+
/**
|
|
96
|
+
* Gets the number of times the current task has been retried.
|
|
97
|
+
* @returns The retry count.
|
|
98
|
+
*/
|
|
99
|
+
retryCount(): number;
|
|
100
|
+
/**
|
|
101
|
+
* Logs a message from the current task.
|
|
102
|
+
* @param message - The message to log.
|
|
103
|
+
* @param level - The log level (optional).
|
|
104
|
+
*/
|
|
105
|
+
log(message: string, level?: LogLevel): void;
|
|
106
|
+
/**
|
|
107
|
+
* Refreshes the timeout for the current task.
|
|
108
|
+
* @param incrementBy - The interval by which to increment the timeout.
|
|
109
|
+
* The interval should be specified in the format of '10s' for 10 seconds, '1m' for 1 minute, or '1d' for 1 day.
|
|
110
|
+
*/
|
|
111
|
+
refreshTimeout(incrementBy: Duration): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Releases a worker slot for a task run such that the worker can pick up another task.
|
|
114
|
+
* Note: this is an advanced feature that may lead to unexpected behavior if used incorrectly.
|
|
115
|
+
* @returns A promise that resolves when the slot has been released.
|
|
116
|
+
*/
|
|
117
|
+
releaseSlot(): Promise<void>;
|
|
118
|
+
/**
|
|
119
|
+
* Streams data from the current task run.
|
|
120
|
+
* @param data - The data to stream (string or binary).
|
|
121
|
+
* @returns A promise that resolves when the data has been streamed.
|
|
122
|
+
*/
|
|
123
|
+
putStream(data: string | Uint8Array): Promise<void>;
|
|
124
|
+
private spawnOptions;
|
|
125
|
+
private spawn;
|
|
126
|
+
private spawnBulk;
|
|
127
|
+
/**
|
|
128
|
+
* Runs multiple children workflows in parallel without waiting for their results.
|
|
129
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
130
|
+
* @returns A list of workflow run references to the enqueued runs.
|
|
131
|
+
*/
|
|
132
|
+
bulkRunNoWaitChildren<Q extends JsonObject = any, P extends JsonObject = any>(children: Array<{
|
|
133
|
+
workflow: string | Workflow | WorkflowV1<Q, P>;
|
|
134
|
+
input: Q;
|
|
135
|
+
options?: ChildRunOpts;
|
|
136
|
+
}>): Promise<WorkflowRunRef<P>[]>;
|
|
137
|
+
/**
|
|
138
|
+
* Runs multiple children workflows in parallel and waits for all results.
|
|
139
|
+
* @param children - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
140
|
+
* @returns A list of results from the children workflows.
|
|
141
|
+
*/
|
|
142
|
+
bulkRunChildren<Q extends JsonObject = any, P extends JsonObject = any>(children: Array<{
|
|
143
|
+
workflow: string | Workflow | WorkflowV1<Q, P>;
|
|
144
|
+
input: Q;
|
|
145
|
+
options?: ChildRunOpts;
|
|
146
|
+
}>): Promise<P[]>;
|
|
147
|
+
/**
|
|
148
|
+
* Runs a new workflow and waits for its result.
|
|
149
|
+
*
|
|
150
|
+
* @param workflow - The workflow to run (name, Workflow instance, or WorkflowV1 instance).
|
|
151
|
+
* @param input - The input data for the workflow.
|
|
152
|
+
* @param options - An options object containing key, sticky, priority, and additionalMetadata.
|
|
153
|
+
* @returns The result of the workflow.
|
|
154
|
+
*/
|
|
155
|
+
runChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: ChildRunOpts): Promise<P>;
|
|
156
|
+
/**
|
|
157
|
+
* Enqueues a new workflow without waiting for its result.
|
|
158
|
+
*
|
|
159
|
+
* @param workflow - The workflow to enqueue (name, Workflow instance, or WorkflowV1 instance).
|
|
160
|
+
* @param input - The input data for the workflow.
|
|
161
|
+
* @param options - An options object containing key, sticky, priority, and additionalMetadata.
|
|
162
|
+
* @returns A reference to the spawned workflow run.
|
|
163
|
+
*/
|
|
164
|
+
runNoWaitChild<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P>, input: Q, options?: ChildRunOpts): Promise<WorkflowRunRef<P>>;
|
|
165
|
+
/**
|
|
166
|
+
* Retrieves additional metadata associated with the current workflow run.
|
|
167
|
+
* @returns A record of metadata key-value pairs.
|
|
168
|
+
*/
|
|
169
|
+
additionalMetadata(): Record<string, string>;
|
|
170
|
+
/**
|
|
171
|
+
* Gets the index of this workflow if it was spawned as part of a bulk operation.
|
|
172
|
+
* @returns The child index number, or undefined if not set.
|
|
173
|
+
*/
|
|
174
|
+
childIndex(): number | undefined;
|
|
175
|
+
/**
|
|
176
|
+
* Gets the key associated with this workflow if it was spawned as a child workflow.
|
|
177
|
+
* @returns The child key, or undefined if not set.
|
|
178
|
+
*/
|
|
179
|
+
childKey(): string | undefined;
|
|
180
|
+
/**
|
|
181
|
+
* Gets the ID of the parent workflow run if this workflow was spawned as a child.
|
|
182
|
+
* @returns The parent workflow run ID, or undefined if not a child workflow.
|
|
183
|
+
*/
|
|
184
|
+
parentWorkflowRunId(): string | undefined;
|
|
185
|
+
priority(): Priority | undefined;
|
|
186
|
+
/**
|
|
187
|
+
* Get the output of a task.
|
|
188
|
+
* @param task - The name of the task to get the output for.
|
|
189
|
+
* @returns The output of the task.
|
|
190
|
+
* @throws An error if the task output is not found.
|
|
191
|
+
* @deprecated use ctx.parentOutput instead
|
|
192
|
+
*/
|
|
193
|
+
stepOutput<L = NextStep>(step: string): L;
|
|
194
|
+
/**
|
|
195
|
+
* Gets the input data for the current workflow.
|
|
196
|
+
* @returns The input data for the workflow.
|
|
197
|
+
* @deprecated use task input parameter instead
|
|
198
|
+
*/
|
|
199
|
+
workflowInput(): T;
|
|
200
|
+
/**
|
|
201
|
+
* Gets the name of the current task.
|
|
202
|
+
* @returns The name of the task.
|
|
203
|
+
* @deprecated use ctx.taskName instead
|
|
204
|
+
*/
|
|
205
|
+
stepName(): string;
|
|
206
|
+
/**
|
|
207
|
+
* Spawns multiple workflows.
|
|
208
|
+
*
|
|
209
|
+
* @param workflows - An array of objects containing the workflow name, input data, and options for each workflow.
|
|
210
|
+
* @returns A list of references to the spawned workflow runs.
|
|
211
|
+
* @deprecated Use bulkRunNoWaitChildren or bulkRunChildren instead.
|
|
212
|
+
*/
|
|
213
|
+
spawnWorkflows<Q extends JsonObject = any, P extends JsonObject = any>(workflows: Array<{
|
|
214
|
+
workflow: string | Workflow | WorkflowV1<Q, P>;
|
|
215
|
+
input: Q;
|
|
216
|
+
options?: ChildRunOpts;
|
|
217
|
+
}>): Promise<WorkflowRunRef<P>[]>;
|
|
218
|
+
/**
|
|
219
|
+
* Spawns a new workflow.
|
|
220
|
+
*
|
|
221
|
+
* @param workflow - The workflow to spawn (name, Workflow instance, or WorkflowV1 instance).
|
|
222
|
+
* @param input - The input data for the workflow.
|
|
223
|
+
* @param options - Additional options for spawning the workflow.
|
|
224
|
+
* @returns A reference to the spawned workflow run.
|
|
225
|
+
* @deprecated Use runChild or runNoWaitChild instead.
|
|
226
|
+
*/
|
|
227
|
+
spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: ChildRunOpts): Promise<WorkflowRunRef<P>>;
|
|
228
|
+
}
|
|
229
|
+
export declare class DurableContext<T, K = {}> extends Context<T, K> {
|
|
230
|
+
waitKey: number;
|
|
231
|
+
/**
|
|
232
|
+
* Pauses execution for the specified duration.
|
|
233
|
+
* Duration is "global" meaning it will wait in real time regardless of transient failures like worker restarts.
|
|
234
|
+
* @param duration - The duration to sleep for.
|
|
235
|
+
* @returns A promise that resolves when the sleep duration has elapsed.
|
|
236
|
+
*/
|
|
237
|
+
sleepFor(duration: Duration, readableDataKey?: string): Promise<Record<string, any>>;
|
|
238
|
+
/**
|
|
239
|
+
* Pauses execution until the specified conditions are met.
|
|
240
|
+
* Conditions are "global" meaning they will wait in real time regardless of transient failures like worker restarts.
|
|
241
|
+
* @param conditions - The conditions to wait for.
|
|
242
|
+
* @returns A promise that resolves with the event that satisfied the conditions.
|
|
243
|
+
*/
|
|
244
|
+
waitFor(conditions: Conditions | Conditions[]): Promise<Record<string, any>>;
|
|
245
|
+
}
|
|
246
|
+
export {};
|