@hatchet-dev/typescript-sdk 1.20.1 → 1.21.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.20.1",
3
+ "version": "1.21.0",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -65,6 +65,10 @@ export type RunOpts = {
65
65
  comparator?: WorkerLabelComparator;
66
66
  }>;
67
67
  };
68
+ export type RunManyOpt<I extends InputType = UnknownInputType> = {
69
+ input: I;
70
+ opts?: RunOpts;
71
+ };
68
72
  /**
69
73
  * Helper type to safely extract output types from task results
70
74
  * @hidden
@@ -263,6 +267,13 @@ export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputT
263
267
  */
264
268
  runNoWait(input: I, options?: RunOpts, _standaloneTaskName?: string): Promise<WorkflowRunRef<O>>;
265
269
  runNoWait(input: I[], options?: RunOpts, _standaloneTaskName?: string): Promise<WorkflowRunRef<O>[]>;
270
+ /**
271
+ * Synchronously trigger multiple workflow runs without waiting for completion.
272
+ * @param runs A list of run requests containing input and per-run options.
273
+ * @returns WorkflowRunRef values in the same order as input runs.
274
+ * @throws Error if the workflow is not bound to a Hatchet client.
275
+ */
276
+ runManyNoWait(runs: RunManyOpt<I>[], _standaloneTaskName?: string): Promise<WorkflowRunRef<O>[]>;
266
277
  /**
267
278
  * @alias run
268
279
  * Triggers a workflow run and waits for the result.
@@ -283,6 +294,12 @@ export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputT
283
294
  */
284
295
  run(input: I, options?: RunOpts, _standaloneTaskName?: string): Promise<O>;
285
296
  run(input: I[], options?: RunOpts, _standaloneTaskName?: string): Promise<O[]>;
297
+ /**
298
+ * Executes many workflow runs and waits for all results.
299
+ * @param runs A list of run requests containing input and per-run options.
300
+ * @returns Results in the same order as input runs.
301
+ */
302
+ runMany(runs: RunManyOpt<I>[], _standaloneTaskName?: string): Promise<O[]>;
286
303
  /**
287
304
  * Schedules a workflow to run at a specific date and time in the future.
288
305
  * @param enqueueAt The date when the workflow should be triggered.
@@ -376,6 +393,7 @@ export declare class BaseWorkflowDeclaration<I extends InputType = UnknownInputT
376
393
  * - Scheduled execution with `schedule()`
377
394
  * - Cron-based recurring execution with `cron()`
378
395
  * - Bulk execution by passing an array input to `run()` and `runNoWait()`
396
+ * - Per-run bulk execution options with `runMany()` and `runManyNoWait()`
379
397
  *
380
398
  * Tasks within workflows can be defined with `workflow.task()` or
381
399
  * `workflow.durableTask()` and arranged into complex dependency patterns.
@@ -484,6 +502,18 @@ export declare class TaskWorkflowDeclaration<I extends InputType = UnknownInputT
484
502
  */
485
503
  runNoWait(input: I & GlobalInput, options?: RunOpts): Promise<WorkflowRunRef<O & Resolved<GlobalOutput, MiddlewareAfter>>>;
486
504
  runNoWait(input: (I & GlobalInput)[], options?: RunOpts): Promise<WorkflowRunRef<O & Resolved<GlobalOutput, MiddlewareAfter>>[]>;
505
+ /**
506
+ * Triggers many task runs and waits for all results.
507
+ * @param runs - The list of inputs and optional per-run options.
508
+ * @returns A promise that resolves with ordered task outputs.
509
+ */
510
+ runMany(runs: RunManyOpt<I & GlobalInput>[]): Promise<(O & Resolved<GlobalOutput, MiddlewareAfter>)[]>;
511
+ /**
512
+ * Triggers many task runs without waiting for completion.
513
+ * @param runs - The list of inputs and optional per-run options.
514
+ * @returns WorkflowRunRef values for each scheduled run.
515
+ */
516
+ runManyNoWait(runs: RunManyOpt<I & GlobalInput>[]): Promise<WorkflowRunRef<O & Resolved<GlobalOutput, MiddlewareAfter>>[]>;
487
517
  /**
488
518
  * Schedules the task to run at a specific date and time.
489
519
  * @param enqueueAt - The date when the task should be triggered.
package/v1/declaration.js CHANGED
@@ -15,6 +15,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
15
15
  step((generator = generator.apply(thisArg, _arguments || [])).next());
16
16
  });
17
17
  };
18
+ var __rest = (this && this.__rest) || function (s, e) {
19
+ var t = {};
20
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
21
+ t[p] = s[p];
22
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
23
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
24
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
25
+ t[p[i]] = s[p[i]];
26
+ }
27
+ return t;
28
+ };
18
29
  Object.defineProperty(exports, "__esModule", { value: true });
19
30
  exports.TaskWorkflowDeclaration = exports.WorkflowDeclaration = exports.BaseWorkflowDeclaration = exports.StickyStrategy = exports.Priority = void 0;
20
31
  exports.CreateTaskWorkflow = CreateTaskWorkflow;
@@ -117,6 +128,59 @@ class BaseWorkflowDeclaration {
117
128
  return res;
118
129
  });
119
130
  }
131
+ /**
132
+ * Synchronously trigger multiple workflow runs without waiting for completion.
133
+ * @param runs A list of run requests containing input and per-run options.
134
+ * @returns WorkflowRunRef values in the same order as input runs.
135
+ * @throws Error if the workflow is not bound to a Hatchet client.
136
+ */
137
+ runManyNoWait(runs, _standaloneTaskName) {
138
+ return __awaiter(this, void 0, void 0, function* () {
139
+ if (!this.client) {
140
+ throw UNBOUND_ERR;
141
+ }
142
+ const parentRunContext = parent_run_context_vars_1.parentRunContextManager.getContext();
143
+ const hasChildKeyOrSticky = runs.some((run) => { var _a, _b; return ((_a = run.opts) === null || _a === void 0 ? void 0 : _a.childKey) || ((_b = run.opts) === null || _b === void 0 ? void 0 : _b.sticky); });
144
+ if (!parentRunContext && hasChildKeyOrSticky) {
145
+ this.client.admin.logger.warn('ignoring childKey or sticky because run is not being spawned from a parent task');
146
+ }
147
+ const inheritedSignal = parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.signal;
148
+ (0, abort_error_1.throwIfAborted)(inheritedSignal, {
149
+ isTrigger: true,
150
+ context: (parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentTaskRunExternalId)
151
+ ? `task run ${parentRunContext.parentTaskRunExternalId}`
152
+ : undefined,
153
+ warn: (message) => this.client.admin.logger.warn(message),
154
+ });
155
+ parent_run_context_vars_1.parentRunContextManager.incrementChildIndex(runs.length);
156
+ const baseOpts = {
157
+ parentId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentId,
158
+ parentTaskRunExternalId: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.parentTaskRunExternalId,
159
+ childIndex: parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.childIndex,
160
+ };
161
+ let resp = [];
162
+ for (let i = 0; i < runs.length; i += 500) {
163
+ const batch = runs.slice(i, i + 500);
164
+ const batchResp = yield this.client.admin.runWorkflows(batch.map((run, batchIndex) => {
165
+ var _a, _b, _c;
166
+ const _d = (_a = run.opts) !== null && _a !== void 0 ? _a : {}, { sticky } = _d, restOpts = __rest(_d, ["sticky"]);
167
+ return {
168
+ workflowName: this.definition.name,
169
+ input: run.input,
170
+ options: Object.assign(Object.assign(Object.assign({}, baseOpts), restOpts), { childIndex: ((_b = baseOpts.childIndex) !== null && _b !== void 0 ? _b : 0) + i + batchIndex, desiredWorkerId: sticky ? parentRunContext === null || parentRunContext === void 0 ? void 0 : parentRunContext.desiredWorkerId : undefined, childKey: (_c = run.opts) === null || _c === void 0 ? void 0 : _c.childKey }),
171
+ };
172
+ }));
173
+ resp = resp.concat(batchResp);
174
+ }
175
+ resp.forEach((ref) => {
176
+ if (_standaloneTaskName) {
177
+ ref._standaloneTaskName = _standaloneTaskName;
178
+ }
179
+ ref.defaultSignal = inheritedSignal;
180
+ });
181
+ return resp;
182
+ });
183
+ }
120
184
  runAndWait(input, options, _standaloneTaskName) {
121
185
  return __awaiter(this, void 0, void 0, function* () {
122
186
  if (!this.client) {
@@ -162,6 +226,43 @@ class BaseWorkflowDeclaration {
162
226
  return res.result();
163
227
  });
164
228
  }
229
+ /**
230
+ * Executes many workflow runs and waits for all results.
231
+ * @param runs A list of run requests containing input and per-run options.
232
+ * @returns Results in the same order as input runs.
233
+ */
234
+ runMany(runs, _standaloneTaskName) {
235
+ return __awaiter(this, void 0, void 0, function* () {
236
+ var _a;
237
+ if (!this.client) {
238
+ throw UNBOUND_ERR;
239
+ }
240
+ const durableCtx = (_a = parent_run_context_vars_1.parentRunContextManager.getContext()) === null || _a === void 0 ? void 0 : _a.durableContext;
241
+ if (durableCtx) {
242
+ return durableCtx.spawnChildren(runs.map((run) => ({
243
+ workflow: this,
244
+ input: run.input,
245
+ options: run.opts,
246
+ })));
247
+ }
248
+ const refs = yield this.runManyNoWait(runs, _standaloneTaskName);
249
+ const returnExceptions = runs.some((run) => { var _a; return (_a = run.opts) === null || _a === void 0 ? void 0 : _a.returnExceptions; });
250
+ if (returnExceptions) {
251
+ const settled = yield Promise.allSettled(refs.map((ref) => ref.result()));
252
+ return settled.map((s) => {
253
+ if (s.status === 'fulfilled') {
254
+ return s.value;
255
+ }
256
+ const { reason } = s;
257
+ if (reason instanceof Error) {
258
+ return reason;
259
+ }
260
+ return new Error(Array.isArray(reason) ? reason.join('; ') : String(reason));
261
+ });
262
+ }
263
+ return Promise.all(refs.map((ref) => ref.result()));
264
+ });
265
+ }
165
266
  /**
166
267
  * Schedules a workflow to run at a specific date and time in the future.
167
268
  * @param enqueueAt The date when the workflow should be triggered.
@@ -344,6 +445,7 @@ exports.BaseWorkflowDeclaration = BaseWorkflowDeclaration;
344
445
  * - Scheduled execution with `schedule()`
345
446
  * - Cron-based recurring execution with `cron()`
346
447
  * - Bulk execution by passing an array input to `run()` and `runNoWait()`
448
+ * - Per-run bulk execution options with `runMany()` and `runManyNoWait()`
347
449
  *
348
450
  * Tasks within workflows can be defined with `workflow.task()` or
349
451
  * `workflow.durableTask()` and arranged into complex dependency patterns.
@@ -492,6 +594,32 @@ class TaskWorkflowDeclaration extends BaseWorkflowDeclaration {
492
594
  : _super.runNoWait.call(this, input, options, this._standalone_task_name);
493
595
  });
494
596
  }
597
+ /**
598
+ * Triggers many task runs and waits for all results.
599
+ * @param runs - The list of inputs and optional per-run options.
600
+ * @returns A promise that resolves with ordered task outputs.
601
+ */
602
+ runMany(runs) {
603
+ const _super = Object.create(null, {
604
+ runMany: { get: () => super.runMany }
605
+ });
606
+ return __awaiter(this, void 0, void 0, function* () {
607
+ return _super.runMany.call(this, runs, this._standalone_task_name);
608
+ });
609
+ }
610
+ /**
611
+ * Triggers many task runs without waiting for completion.
612
+ * @param runs - The list of inputs and optional per-run options.
613
+ * @returns WorkflowRunRef values for each scheduled run.
614
+ */
615
+ runManyNoWait(runs) {
616
+ const _super = Object.create(null, {
617
+ runManyNoWait: { get: () => super.runManyNoWait }
618
+ });
619
+ return __awaiter(this, void 0, void 0, function* () {
620
+ return _super.runManyNoWait.call(this, runs, this._standalone_task_name);
621
+ });
622
+ }
495
623
  /**
496
624
  * Schedules the task to run at a specific date and time.
497
625
  * @param enqueueAt - The date when the task should be triggered.
@@ -26,6 +26,25 @@ function main() {
26
26
  console.log(res[0].TransformedMessage);
27
27
  console.log(res[1].TransformedMessage);
28
28
  // !!
29
+ // > Bulk Run a Task with runMany
30
+ const runManyRes = yield workflow_1.simple.runMany([
31
+ {
32
+ input: {
33
+ Message: 'HeLlO WoRlD',
34
+ },
35
+ },
36
+ {
37
+ input: {
38
+ Message: 'Hello MoOn',
39
+ },
40
+ opts: {
41
+ priority: 3,
42
+ },
43
+ },
44
+ ]);
45
+ console.log(runManyRes[0].TransformedMessage);
46
+ console.log(runManyRes[1].TransformedMessage);
47
+ // !!
29
48
  // > Bulk Run Tasks from within a Task
30
49
  const parent = hatchet_client_1.hatchet.task({
31
50
  name: 'simple',
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.20.1";
1
+ export declare const HATCHET_VERSION = "1.21.0";
package/version.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HATCHET_VERSION = void 0;
4
- exports.HATCHET_VERSION = '1.20.1';
4
+ exports.HATCHET_VERSION = '1.21.0';