@hatchet-dev/typescript-sdk 0.10.0 → 0.12.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.
Files changed (68) hide show
  1. package/clients/admin/admin-client.d.ts +1 -0
  2. package/clients/dispatcher/dispatcher-client.d.ts +3 -0
  3. package/clients/dispatcher/dispatcher-client.js +20 -1
  4. package/clients/hatchet-client/hatchet-client.d.ts +2 -2
  5. package/clients/hatchet-client/hatchet-client.js +11 -4
  6. package/clients/listener/listener-client.d.ts +10 -4
  7. package/clients/listener/listener-client.js +45 -13
  8. package/clients/worker/worker.d.ts +12 -0
  9. package/clients/worker/worker.js +54 -2
  10. package/examples/affinity-workers.d.ts +1 -0
  11. package/examples/affinity-workers.js +93 -0
  12. package/examples/api.d.ts +1 -0
  13. package/examples/api.js +61 -0
  14. package/examples/concurrency/cancel-in-progress/concurrency-event.d.ts +1 -0
  15. package/examples/concurrency/cancel-in-progress/concurrency-event.js +37 -0
  16. package/examples/concurrency/cancel-in-progress/concurrency-worker.d.ts +1 -0
  17. package/examples/concurrency/cancel-in-progress/concurrency-worker.js +66 -0
  18. package/examples/concurrency/group-round-robin/concurrency-event.d.ts +1 -0
  19. package/examples/concurrency/group-round-robin/concurrency-event.js +35 -0
  20. package/examples/concurrency/group-round-robin/concurrency-worker.d.ts +1 -0
  21. package/examples/concurrency/group-round-robin/concurrency-worker.js +61 -0
  22. package/examples/dag-worker.d.ts +1 -0
  23. package/examples/dag-worker.js +76 -0
  24. package/examples/example-event-with-results.d.ts +1 -0
  25. package/examples/example-event-with-results.js +50 -0
  26. package/examples/example-event.d.ts +1 -0
  27. package/examples/example-event.js +10 -0
  28. package/examples/fanout-worker.d.ts +1 -0
  29. package/examples/fanout-worker.js +69 -0
  30. package/examples/logger.d.ts +1 -0
  31. package/examples/logger.js +50 -0
  32. package/examples/manual-trigger.d.ts +1 -0
  33. package/examples/manual-trigger.js +46 -0
  34. package/examples/multi-workflow.d.ts +1 -0
  35. package/examples/multi-workflow.js +55 -0
  36. package/examples/namespaced-worker.d.ts +1 -0
  37. package/examples/namespaced-worker.js +56 -0
  38. package/examples/on-failure.d.ts +1 -0
  39. package/examples/on-failure.js +53 -0
  40. package/examples/playground.d.ts +1 -0
  41. package/examples/playground.js +39 -0
  42. package/examples/rate-limit/events.d.ts +1 -0
  43. package/examples/rate-limit/events.js +16 -0
  44. package/examples/rate-limit/worker.d.ts +1 -0
  45. package/examples/rate-limit/worker.js +43 -0
  46. package/examples/retries-worker.d.ts +1 -0
  47. package/examples/retries-worker.js +60 -0
  48. package/examples/simple-worker.d.ts +1 -0
  49. package/examples/simple-worker.js +55 -0
  50. package/examples/sticky-worker.d.ts +1 -0
  51. package/examples/sticky-worker.js +73 -0
  52. package/examples/stream-by-additional-meta.d.ts +1 -0
  53. package/examples/stream-by-additional-meta.js +55 -0
  54. package/package.json +24 -21
  55. package/protoc/dispatcher/dispatcher.d.ts +109 -2
  56. package/protoc/dispatcher/dispatcher.js +518 -17
  57. package/protoc/events/events.d.ts +1 -1
  58. package/protoc/events/events.js +0 -5
  59. package/protoc/google/protobuf/timestamp.js +0 -8
  60. package/protoc/workflows/workflows.d.ts +82 -1
  61. package/protoc/workflows/workflows.js +404 -9
  62. package/step.d.ts +79 -2
  63. package/step.js +90 -3
  64. package/util/config-loader/config-loader.js +9 -8
  65. package/util/workflow-run-ref.d.ts +3 -0
  66. package/util/workflow-run-ref.js +21 -4
  67. package/workflow.d.ts +116 -1
  68. package/workflow.js +6 -1
package/step.d.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  import * as z from 'zod';
2
+ import { Workflow } from './workflow';
2
3
  import { Action } from './clients/dispatcher/action-listener';
3
4
  import { LogLevel } from './clients/event/event-client';
4
5
  import { Logger } from './util/logger';
5
6
  import { HatchetClient } from './clients/hatchet-client';
6
7
  import WorkflowRunRef from './util/workflow-run-ref';
8
+ import { Worker } from './clients/worker';
9
+ import { WorkerLabels } from './clients/dispatcher/dispatcher-client';
10
+ import { WorkerLabelComparator } from './protoc/workflows';
7
11
  export declare const CreateRateLimitSchema: z.ZodObject<{
8
12
  key: z.ZodString;
9
13
  units: z.ZodNumber;
@@ -14,6 +18,22 @@ export declare const CreateRateLimitSchema: z.ZodObject<{
14
18
  key: string;
15
19
  units: number;
16
20
  }>;
21
+ export declare const DesiredWorkerLabelSchema: z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodObject<{
22
+ value: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
23
+ required: z.ZodOptional<z.ZodBoolean>;
24
+ weight: z.ZodOptional<z.ZodNumber>;
25
+ comparator: z.ZodOptional<z.ZodNativeEnum<typeof WorkerLabelComparator>>;
26
+ }, "strip", z.ZodTypeAny, {
27
+ value: string | number;
28
+ required?: boolean | undefined;
29
+ comparator?: WorkerLabelComparator | undefined;
30
+ weight?: number | undefined;
31
+ }, {
32
+ value: string | number;
33
+ required?: boolean | undefined;
34
+ comparator?: WorkerLabelComparator | undefined;
35
+ weight?: number | undefined;
36
+ }>]>>;
17
37
  export declare const CreateStepSchema: z.ZodObject<{
18
38
  name: z.ZodString;
19
39
  parents: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -29,6 +49,22 @@ export declare const CreateStepSchema: z.ZodObject<{
29
49
  key: string;
30
50
  units: number;
31
51
  }>, "many">>;
52
+ worker_labels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodLazy<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodObject<{
53
+ value: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
54
+ required: z.ZodOptional<z.ZodBoolean>;
55
+ weight: z.ZodOptional<z.ZodNumber>;
56
+ comparator: z.ZodOptional<z.ZodNativeEnum<typeof WorkerLabelComparator>>;
57
+ }, "strip", z.ZodTypeAny, {
58
+ value: string | number;
59
+ required?: boolean | undefined;
60
+ comparator?: WorkerLabelComparator | undefined;
61
+ weight?: number | undefined;
62
+ }, {
63
+ value: string | number;
64
+ required?: boolean | undefined;
65
+ comparator?: WorkerLabelComparator | undefined;
66
+ weight?: number | undefined;
67
+ }>]>>>>>;
32
68
  }, "strip", z.ZodTypeAny, {
33
69
  name: string;
34
70
  timeout?: string | undefined;
@@ -38,6 +74,12 @@ export declare const CreateStepSchema: z.ZodObject<{
38
74
  key: string;
39
75
  units: number;
40
76
  }[] | undefined;
77
+ worker_labels?: Record<string, string | number | {
78
+ value: string | number;
79
+ required?: boolean | undefined;
80
+ comparator?: WorkerLabelComparator | undefined;
81
+ weight?: number | undefined;
82
+ } | undefined> | undefined;
41
83
  }, {
42
84
  name: string;
43
85
  timeout?: string | undefined;
@@ -47,6 +89,12 @@ export declare const CreateStepSchema: z.ZodObject<{
47
89
  key: string;
48
90
  units: number;
49
91
  }[] | undefined;
92
+ worker_labels?: Record<string, string | number | {
93
+ value: string | number;
94
+ required?: boolean | undefined;
95
+ comparator?: WorkerLabelComparator | undefined;
96
+ weight?: number | undefined;
97
+ } | undefined> | undefined;
50
98
  }>;
51
99
  export type JsonObject = {
52
100
  [Key in string]: JsonValue;
@@ -65,16 +113,25 @@ interface ContextData<T, K> {
65
113
  triggered_by: string;
66
114
  user_data: K;
67
115
  }
116
+ export declare class ContextWorker {
117
+ private worker;
118
+ constructor(worker: Worker);
119
+ id(): string | undefined;
120
+ hasWorkflow(workflowName: string): boolean;
121
+ labels(): WorkerLabels;
122
+ upsertLabels(labels: WorkerLabels): Promise<WorkerLabels>;
123
+ }
68
124
  export declare class Context<T, K = {}> {
69
125
  data: ContextData<T, K>;
70
126
  input: T;
71
127
  controller: AbortController;
72
128
  action: Action;
73
129
  client: HatchetClient;
130
+ worker: ContextWorker;
74
131
  overridesData: Record<string, any>;
75
132
  logger: Logger;
76
133
  spawnIndex: number;
77
- constructor(action: Action, client: HatchetClient);
134
+ constructor(action: Action, client: HatchetClient, worker: Worker);
78
135
  stepOutput(step: string): NextStep;
79
136
  triggeredByEvent(): boolean;
80
137
  workflowInput(): T;
@@ -94,7 +151,27 @@ export declare class Context<T, K = {}> {
94
151
  refreshTimeout(incrementBy: string): Promise<void>;
95
152
  releaseSlot(): Promise<void>;
96
153
  putStream(data: string | Uint8Array): Promise<void>;
97
- spawnWorkflow<Q = JsonValue, P = JsonValue>(workflowName: string, input: Q, key?: string): WorkflowRunRef<P>;
154
+ /**
155
+ * Spawns a new workflow.
156
+ *
157
+ * @param workflowName the name of the workflow to spawn
158
+ * @param input the input data for the workflow
159
+ * @param options additional options for spawning the workflow. If a string is provided, it is used as the key.
160
+ * If an object is provided, it can include:
161
+ * - key: a unique identifier for the workflow (deprecated, use options.key instead)
162
+ * - sticky: a boolean indicating whether to use sticky execution
163
+ * @param <Q> the type of the input data
164
+ * @param <P> the type of the output data
165
+ * @return a reference to the spawned workflow run
166
+ */
167
+ spawnWorkflow<Q = JsonValue, P = JsonValue>(workflow: string | Workflow, input: Q, options?: string | {
168
+ key?: string;
169
+ sticky?: boolean;
170
+ }): WorkflowRunRef<P>;
171
+ additionalMetadata(): Record<string, string>;
172
+ childIndex(): number | undefined;
173
+ childKey(): string | undefined;
174
+ parentWorkflowRunId(): string | undefined;
98
175
  }
99
176
  export type StepRunFunction<T, K> = (ctx: Context<T, K>) => Promise<NextStep | void> | NextStep | void;
100
177
  export interface CreateStep<T, K> extends z.infer<typeof CreateStepSchema> {
package/step.js CHANGED
@@ -35,26 +35,61 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
35
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
36
  };
37
37
  Object.defineProperty(exports, "__esModule", { value: true });
38
- exports.Context = exports.CreateStepSchema = exports.CreateRateLimitSchema = void 0;
38
+ exports.Context = exports.ContextWorker = exports.CreateStepSchema = exports.DesiredWorkerLabelSchema = exports.CreateRateLimitSchema = void 0;
39
39
  /* eslint-disable max-classes-per-file */
40
40
  const hatchet_error_1 = __importDefault(require("./util/errors/hatchet-error"));
41
41
  const z = __importStar(require("zod"));
42
42
  const workflow_1 = require("./workflow");
43
43
  const logger_1 = require("./util/logger");
44
44
  const parse_1 = require("./util/parse");
45
+ const workflows_1 = require("./protoc/workflows");
45
46
  exports.CreateRateLimitSchema = z.object({
46
47
  key: z.string(),
47
48
  units: z.number().min(1),
48
49
  });
50
+ exports.DesiredWorkerLabelSchema = z
51
+ .union([
52
+ z.string(),
53
+ z.number().int(),
54
+ z.object({
55
+ value: z.union([z.string(), z.number()]),
56
+ required: z.boolean().optional(),
57
+ weight: z.number().int().optional(),
58
+ // (optional) comparator for the label
59
+ // if not provided, the default is EQUAL
60
+ // desired COMPARATOR actual (i.e. desired > actual for GREATER_THAN)
61
+ comparator: z.nativeEnum(workflows_1.WorkerLabelComparator).optional(),
62
+ }),
63
+ ])
64
+ .optional();
49
65
  exports.CreateStepSchema = z.object({
50
66
  name: z.string(),
51
67
  parents: z.array(z.string()).optional(),
52
68
  timeout: workflow_1.HatchetTimeoutSchema.optional(),
53
69
  retries: z.number().optional(),
54
70
  rate_limits: z.array(exports.CreateRateLimitSchema).optional(),
71
+ worker_labels: z.record(z.lazy(() => exports.DesiredWorkerLabelSchema)).optional(),
55
72
  });
73
+ class ContextWorker {
74
+ constructor(worker) {
75
+ this.worker = worker;
76
+ }
77
+ id() {
78
+ return this.worker.workerId;
79
+ }
80
+ hasWorkflow(workflowName) {
81
+ return !!this.worker.workflow_registry.find((workflow) => workflow.id === workflowName);
82
+ }
83
+ labels() {
84
+ return this.worker.labels;
85
+ }
86
+ upsertLabels(labels) {
87
+ return this.worker.upsertLabels(labels);
88
+ }
89
+ }
90
+ exports.ContextWorker = ContextWorker;
56
91
  class Context {
57
- constructor(action, client) {
92
+ constructor(action, client, worker) {
58
93
  this.controller = new AbortController();
59
94
  this.overridesData = {};
60
95
  this.spawnIndex = 0;
@@ -63,6 +98,7 @@ class Context {
63
98
  this.data = data;
64
99
  this.action = action;
65
100
  this.client = client;
101
+ this.worker = new ContextWorker(worker);
66
102
  this.logger = new logger_1.Logger(`Context Logger`, client.config.log_level);
67
103
  // if this is a getGroupKeyRunId, the data is the workflow input
68
104
  if (action.getGroupKeyRunId !== '') {
@@ -164,15 +200,49 @@ class Context {
164
200
  yield this.client.event.putStream(stepRunId, data);
165
201
  });
166
202
  }
167
- spawnWorkflow(workflowName, input, key) {
203
+ /**
204
+ * Spawns a new workflow.
205
+ *
206
+ * @param workflowName the name of the workflow to spawn
207
+ * @param input the input data for the workflow
208
+ * @param options additional options for spawning the workflow. If a string is provided, it is used as the key.
209
+ * If an object is provided, it can include:
210
+ * - key: a unique identifier for the workflow (deprecated, use options.key instead)
211
+ * - sticky: a boolean indicating whether to use sticky execution
212
+ * @param <Q> the type of the input data
213
+ * @param <P> the type of the output data
214
+ * @return a reference to the spawned workflow run
215
+ */
216
+ spawnWorkflow(workflow, input, options) {
168
217
  const { workflowRunId, stepRunId } = this.action;
218
+ let workflowName = '';
219
+ if (typeof workflow === 'string') {
220
+ workflowName = workflow;
221
+ }
222
+ else {
223
+ workflowName = workflow.id;
224
+ }
169
225
  const name = this.client.config.namespace + workflowName;
226
+ let key = '';
227
+ let sticky = false;
228
+ if (typeof options === 'string') {
229
+ this.logger.warn('Using key param is deprecated and will be removed in a future release. Use options.key instead.');
230
+ key = options;
231
+ }
232
+ else {
233
+ key = options === null || options === void 0 ? void 0 : options.key;
234
+ sticky = options === null || options === void 0 ? void 0 : options.sticky;
235
+ }
236
+ if (sticky && !this.worker.hasWorkflow(name)) {
237
+ throw new hatchet_error_1.default(`cannot run with sticky: workflow ${name} is not registered on the worker`);
238
+ }
170
239
  try {
171
240
  const resp = this.client.admin.runWorkflow(name, input, {
172
241
  parentId: workflowRunId,
173
242
  parentStepRunId: stepRunId,
174
243
  childKey: key,
175
244
  childIndex: this.spawnIndex,
245
+ desiredWorkerId: sticky ? this.worker.id() : undefined,
176
246
  });
177
247
  this.spawnIndex += 1;
178
248
  return resp;
@@ -181,5 +251,22 @@ class Context {
181
251
  throw new hatchet_error_1.default(e.message);
182
252
  }
183
253
  }
254
+ additionalMetadata() {
255
+ if (!this.action.additionalMetadata) {
256
+ return {};
257
+ }
258
+ // parse the additional metadata
259
+ const res = (0, parse_1.parseJSON)(this.action.additionalMetadata);
260
+ return res;
261
+ }
262
+ childIndex() {
263
+ return this.action.childWorkflowIndex;
264
+ }
265
+ childKey() {
266
+ return this.action.childWorkflowKey;
267
+ }
268
+ parentWorkflowRunId() {
269
+ return this.action.parentWorkflowRunId;
270
+ }
184
271
  }
185
272
  exports.Context = Context;
@@ -34,7 +34,7 @@ const token_1 = require("./token");
34
34
  const DEFAULT_CONFIG_FILE = '.hatchet.yaml';
35
35
  class ConfigLoader {
36
36
  static loadClientConfig(override, config) {
37
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4;
37
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v, _w, _x, _y, _z, _0, _1, _2, _3, _4, _5, _6;
38
38
  const yaml = this.loadYamlConfig(config === null || config === void 0 ? void 0 : config.path);
39
39
  const tlsConfig = (_a = override === null || override === void 0 ? void 0 : override.tls_config) !== null && _a !== void 0 ? _a : {
40
40
  tls_strategy: (_d = (_c = (_b = yaml === null || yaml === void 0 ? void 0 : yaml.tls_config) === null || _b === void 0 ? void 0 : _b.tls_strategy) !== null && _c !== void 0 ? _c : this.env('HATCHET_CLIENT_TLS_STRATEGY')) !== null && _d !== void 0 ? _d : 'tls',
@@ -56,21 +56,22 @@ class ConfigLoader {
56
56
  try {
57
57
  const addresses = (0, token_1.getAddressesFromJWT)(token);
58
58
  grpcBroadcastAddress =
59
- (_r = (_q = yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _q !== void 0 ? _q : this.env('HATCHET_CLIENT_HOST_PORT')) !== null && _r !== void 0 ? _r : addresses.grpcBroadcastAddress;
59
+ (_s = (_r = (_q = override === null || override === void 0 ? void 0 : override.host_port) !== null && _q !== void 0 ? _q : yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _r !== void 0 ? _r : this.env('HATCHET_CLIENT_HOST_PORT')) !== null && _s !== void 0 ? _s : addresses.grpcBroadcastAddress;
60
60
  apiUrl =
61
- (_u = (_t = (_s = override === null || override === void 0 ? void 0 : override.api_url) !== null && _s !== void 0 ? _s : yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _t !== void 0 ? _t : this.env('HATCHET_CLIENT_API_URL')) !== null && _u !== void 0 ? _u : addresses.serverUrl;
61
+ (_v = (_u = (_t = override === null || override === void 0 ? void 0 : override.api_url) !== null && _t !== void 0 ? _t : yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _u !== void 0 ? _u : this.env('HATCHET_CLIENT_API_URL')) !== null && _v !== void 0 ? _v : addresses.serverUrl;
62
62
  }
63
63
  catch (e) {
64
- grpcBroadcastAddress = (_v = yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _v !== void 0 ? _v : this.env('HATCHET_CLIENT_HOST_PORT');
65
- apiUrl = (_x = (_w = override === null || override === void 0 ? void 0 : override.api_url) !== null && _w !== void 0 ? _w : yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _x !== void 0 ? _x : this.env('HATCHET_CLIENT_API_URL');
64
+ grpcBroadcastAddress =
65
+ (_x = (_w = override === null || override === void 0 ? void 0 : override.host_port) !== null && _w !== void 0 ? _w : yaml === null || yaml === void 0 ? void 0 : yaml.host_port) !== null && _x !== void 0 ? _x : this.env('HATCHET_CLIENT_HOST_PORT');
66
+ apiUrl = (_z = (_y = override === null || override === void 0 ? void 0 : override.api_url) !== null && _y !== void 0 ? _y : yaml === null || yaml === void 0 ? void 0 : yaml.api_url) !== null && _z !== void 0 ? _z : this.env('HATCHET_CLIENT_API_URL');
66
67
  }
67
- const namespace = (_z = (_y = override === null || override === void 0 ? void 0 : override.namespace) !== null && _y !== void 0 ? _y : yaml === null || yaml === void 0 ? void 0 : yaml.namespace) !== null && _z !== void 0 ? _z : this.env('HATCHET_CLIENT_NAMESPACE');
68
+ const namespace = (_1 = (_0 = override === null || override === void 0 ? void 0 : override.namespace) !== null && _0 !== void 0 ? _0 : yaml === null || yaml === void 0 ? void 0 : yaml.namespace) !== null && _1 !== void 0 ? _1 : this.env('HATCHET_CLIENT_NAMESPACE');
68
69
  return {
69
- token: (_1 = (_0 = override === null || override === void 0 ? void 0 : override.token) !== null && _0 !== void 0 ? _0 : yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _1 !== void 0 ? _1 : this.env('HATCHET_CLIENT_TOKEN'),
70
+ token: (_3 = (_2 = override === null || override === void 0 ? void 0 : override.token) !== null && _2 !== void 0 ? _2 : yaml === null || yaml === void 0 ? void 0 : yaml.token) !== null && _3 !== void 0 ? _3 : this.env('HATCHET_CLIENT_TOKEN'),
70
71
  host_port: grpcBroadcastAddress,
71
72
  api_url: apiUrl,
72
73
  tls_config: tlsConfig,
73
- log_level: (_4 = (_3 = (_2 = override === null || override === void 0 ? void 0 : override.log_level) !== null && _2 !== void 0 ? _2 : yaml === null || yaml === void 0 ? void 0 : yaml.log_level) !== null && _3 !== void 0 ? _3 : this.env('HATCHET_CLIENT_LOG_LEVEL')) !== null && _4 !== void 0 ? _4 : 'INFO',
74
+ log_level: (_6 = (_5 = (_4 = override === null || override === void 0 ? void 0 : override.log_level) !== null && _4 !== void 0 ? _4 : yaml === null || yaml === void 0 ? void 0 : yaml.log_level) !== null && _5 !== void 0 ? _5 : this.env('HATCHET_CLIENT_LOG_LEVEL')) !== null && _6 !== void 0 ? _6 : 'INFO',
74
75
  tenant_id: tenantId,
75
76
  namespace: namespace ? `${namespace}_`.toLowerCase() : '',
76
77
  };
@@ -2,6 +2,9 @@ import { ListenerClient, StepRunEvent } from '../clients/listener/listener-clien
2
2
  type EventualWorkflowRunId = string | Promise<string> | Promise<{
3
3
  workflowRunId: string;
4
4
  }>;
5
+ export declare class DedupeViolationErr extends Error {
6
+ constructor(message: string);
7
+ }
5
8
  export default class WorkflowRunRef<T> {
6
9
  workflowRunId: EventualWorkflowRunId;
7
10
  parentWorkflowRunId?: string;
@@ -16,18 +16,35 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
16
16
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
+ exports.DedupeViolationErr = void 0;
20
+ const nice_grpc_1 = require("nice-grpc");
19
21
  const dispatcher_1 = require("../protoc/dispatcher");
22
+ class DedupeViolationErr extends Error {
23
+ constructor(message) {
24
+ super(message);
25
+ this.name = 'DedupeViolationErr';
26
+ }
27
+ }
28
+ exports.DedupeViolationErr = DedupeViolationErr;
20
29
  function getWorkflowRunId(workflowRunId) {
21
30
  return __awaiter(this, void 0, void 0, function* () {
22
31
  if (typeof workflowRunId === 'string') {
23
32
  return workflowRunId;
24
33
  }
25
34
  if (workflowRunId instanceof Promise) {
26
- const resolved = yield workflowRunId;
27
- if (typeof resolved === 'string') {
28
- return resolved;
35
+ try {
36
+ const resolved = yield workflowRunId;
37
+ if (typeof resolved === 'string') {
38
+ return resolved;
39
+ }
40
+ return resolved.workflowRunId;
41
+ }
42
+ catch (e) {
43
+ if (e.code && e.code === nice_grpc_1.Status.ALREADY_EXISTS) {
44
+ throw new DedupeViolationErr(e.details);
45
+ }
46
+ throw new Error('Invalid workflowRunId');
29
47
  }
30
- return resolved.workflowRunId;
31
48
  }
32
49
  throw new Error('Invalid workflowRunId');
33
50
  });
package/workflow.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import * as z from 'zod';
2
2
  import { CreateStep } from './step';
3
- import { ConcurrencyLimitStrategy as PbConcurrencyLimitStrategy } from './protoc/workflows';
3
+ import { ConcurrencyLimitStrategy as PbConcurrencyLimitStrategy, StickyStrategy as PbStickyStrategy } from './protoc/workflows';
4
4
  declare const StepsSchema: z.ZodArray<z.ZodObject<{
5
5
  name: z.ZodString;
6
6
  parents: z.ZodOptional<z.ZodArray<z.ZodString, "many">>;
@@ -16,6 +16,22 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
16
16
  key: string;
17
17
  units: number;
18
18
  }>, "many">>;
19
+ worker_labels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodLazy<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodObject<{
20
+ value: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
21
+ required: z.ZodOptional<z.ZodBoolean>;
22
+ weight: z.ZodOptional<z.ZodNumber>;
23
+ comparator: z.ZodOptional<z.ZodNativeEnum<typeof import("./protoc/workflows").WorkerLabelComparator>>;
24
+ }, "strip", z.ZodTypeAny, {
25
+ value: string | number;
26
+ required?: boolean | undefined;
27
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
28
+ weight?: number | undefined;
29
+ }, {
30
+ value: string | number;
31
+ required?: boolean | undefined;
32
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
33
+ weight?: number | undefined;
34
+ }>]>>>>>;
19
35
  }, "strip", z.ZodTypeAny, {
20
36
  name: string;
21
37
  timeout?: string | undefined;
@@ -25,6 +41,12 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
25
41
  key: string;
26
42
  units: number;
27
43
  }[] | undefined;
44
+ worker_labels?: Record<string, string | number | {
45
+ value: string | number;
46
+ required?: boolean | undefined;
47
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
48
+ weight?: number | undefined;
49
+ } | undefined> | undefined;
28
50
  }, {
29
51
  name: string;
30
52
  timeout?: string | undefined;
@@ -34,6 +56,12 @@ declare const StepsSchema: z.ZodArray<z.ZodObject<{
34
56
  key: string;
35
57
  units: number;
36
58
  }[] | undefined;
59
+ worker_labels?: Record<string, string | number | {
60
+ value: string | number;
61
+ required?: boolean | undefined;
62
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
63
+ weight?: number | undefined;
64
+ } | undefined> | undefined;
37
65
  }>, "many">;
38
66
  export type Steps = z.infer<typeof StepsSchema>;
39
67
  export declare const ConcurrencyLimitStrategy: typeof PbConcurrencyLimitStrategy;
@@ -51,10 +79,15 @@ export declare const WorkflowConcurrency: z.ZodObject<{
51
79
  limitStrategy?: PbConcurrencyLimitStrategy | undefined;
52
80
  }>;
53
81
  export declare const HatchetTimeoutSchema: z.ZodString;
82
+ export declare const StickyStrategy: typeof PbStickyStrategy;
54
83
  export declare const CreateWorkflowSchema: z.ZodObject<{
55
84
  id: z.ZodString;
56
85
  description: z.ZodString;
57
86
  version: z.ZodOptional<z.ZodString>;
87
+ /**
88
+ * sticky will attempt to run all steps for workflow on the same worker
89
+ */
90
+ sticky: z.ZodOptional<z.ZodNativeEnum<typeof PbStickyStrategy>>;
58
91
  scheduleTimeout: z.ZodOptional<z.ZodString>;
59
92
  /**
60
93
  * @deprecated Workflow timeout is deprecated. Use step timeouts instead.
@@ -94,6 +127,22 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
94
127
  key: string;
95
128
  units: number;
96
129
  }>, "many">>;
130
+ worker_labels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodLazy<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodObject<{
131
+ value: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
132
+ required: z.ZodOptional<z.ZodBoolean>;
133
+ weight: z.ZodOptional<z.ZodNumber>;
134
+ comparator: z.ZodOptional<z.ZodNativeEnum<typeof import("./protoc/workflows").WorkerLabelComparator>>;
135
+ }, "strip", z.ZodTypeAny, {
136
+ value: string | number;
137
+ required?: boolean | undefined;
138
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
139
+ weight?: number | undefined;
140
+ }, {
141
+ value: string | number;
142
+ required?: boolean | undefined;
143
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
144
+ weight?: number | undefined;
145
+ }>]>>>>>;
97
146
  }, "strip", z.ZodTypeAny, {
98
147
  name: string;
99
148
  timeout?: string | undefined;
@@ -103,6 +152,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
103
152
  key: string;
104
153
  units: number;
105
154
  }[] | undefined;
155
+ worker_labels?: Record<string, string | number | {
156
+ value: string | number;
157
+ required?: boolean | undefined;
158
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
159
+ weight?: number | undefined;
160
+ } | undefined> | undefined;
106
161
  }, {
107
162
  name: string;
108
163
  timeout?: string | undefined;
@@ -112,6 +167,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
112
167
  key: string;
113
168
  units: number;
114
169
  }[] | undefined;
170
+ worker_labels?: Record<string, string | number | {
171
+ value: string | number;
172
+ required?: boolean | undefined;
173
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
174
+ weight?: number | undefined;
175
+ } | undefined> | undefined;
115
176
  }>, "many">;
116
177
  onFailure: z.ZodOptional<z.ZodObject<{
117
178
  name: z.ZodString;
@@ -128,6 +189,22 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
128
189
  key: string;
129
190
  units: number;
130
191
  }>, "many">>;
192
+ worker_labels: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodLazy<z.ZodOptional<z.ZodUnion<[z.ZodString, z.ZodNumber, z.ZodObject<{
193
+ value: z.ZodUnion<[z.ZodString, z.ZodNumber]>;
194
+ required: z.ZodOptional<z.ZodBoolean>;
195
+ weight: z.ZodOptional<z.ZodNumber>;
196
+ comparator: z.ZodOptional<z.ZodNativeEnum<typeof import("./protoc/workflows").WorkerLabelComparator>>;
197
+ }, "strip", z.ZodTypeAny, {
198
+ value: string | number;
199
+ required?: boolean | undefined;
200
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
201
+ weight?: number | undefined;
202
+ }, {
203
+ value: string | number;
204
+ required?: boolean | undefined;
205
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
206
+ weight?: number | undefined;
207
+ }>]>>>>>;
131
208
  }, "strip", z.ZodTypeAny, {
132
209
  name: string;
133
210
  timeout?: string | undefined;
@@ -137,6 +214,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
137
214
  key: string;
138
215
  units: number;
139
216
  }[] | undefined;
217
+ worker_labels?: Record<string, string | number | {
218
+ value: string | number;
219
+ required?: boolean | undefined;
220
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
221
+ weight?: number | undefined;
222
+ } | undefined> | undefined;
140
223
  }, {
141
224
  name: string;
142
225
  timeout?: string | undefined;
@@ -146,6 +229,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
146
229
  key: string;
147
230
  units: number;
148
231
  }[] | undefined;
232
+ worker_labels?: Record<string, string | number | {
233
+ value: string | number;
234
+ required?: boolean | undefined;
235
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
236
+ weight?: number | undefined;
237
+ } | undefined> | undefined;
149
238
  }>>;
150
239
  }, "strip", z.ZodTypeAny, {
151
240
  description: string;
@@ -158,10 +247,17 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
158
247
  key: string;
159
248
  units: number;
160
249
  }[] | undefined;
250
+ worker_labels?: Record<string, string | number | {
251
+ value: string | number;
252
+ required?: boolean | undefined;
253
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
254
+ weight?: number | undefined;
255
+ } | undefined> | undefined;
161
256
  }[];
162
257
  id: string;
163
258
  version?: string | undefined;
164
259
  scheduleTimeout?: string | undefined;
260
+ sticky?: PbStickyStrategy | undefined;
165
261
  timeout?: string | undefined;
166
262
  on?: {
167
263
  cron: string;
@@ -179,6 +275,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
179
275
  key: string;
180
276
  units: number;
181
277
  }[] | undefined;
278
+ worker_labels?: Record<string, string | number | {
279
+ value: string | number;
280
+ required?: boolean | undefined;
281
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
282
+ weight?: number | undefined;
283
+ } | undefined> | undefined;
182
284
  } | undefined;
183
285
  }, {
184
286
  description: string;
@@ -191,10 +293,17 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
191
293
  key: string;
192
294
  units: number;
193
295
  }[] | undefined;
296
+ worker_labels?: Record<string, string | number | {
297
+ value: string | number;
298
+ required?: boolean | undefined;
299
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
300
+ weight?: number | undefined;
301
+ } | undefined> | undefined;
194
302
  }[];
195
303
  id: string;
196
304
  version?: string | undefined;
197
305
  scheduleTimeout?: string | undefined;
306
+ sticky?: PbStickyStrategy | undefined;
198
307
  timeout?: string | undefined;
199
308
  on?: {
200
309
  cron: string;
@@ -212,6 +321,12 @@ export declare const CreateWorkflowSchema: z.ZodObject<{
212
321
  key: string;
213
322
  units: number;
214
323
  }[] | undefined;
324
+ worker_labels?: Record<string, string | number | {
325
+ value: string | number;
326
+ required?: boolean | undefined;
327
+ comparator?: import("./protoc/workflows").WorkerLabelComparator | undefined;
328
+ weight?: number | undefined;
329
+ } | undefined> | undefined;
215
330
  } | undefined;
216
331
  }>;
217
332
  export interface Workflow extends z.infer<typeof CreateWorkflowSchema> {
package/workflow.js CHANGED
@@ -23,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
23
23
  return result;
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.CreateWorkflowSchema = exports.HatchetTimeoutSchema = exports.WorkflowConcurrency = exports.ConcurrencyLimitStrategy = void 0;
26
+ exports.CreateWorkflowSchema = exports.StickyStrategy = exports.HatchetTimeoutSchema = exports.WorkflowConcurrency = exports.ConcurrencyLimitStrategy = void 0;
27
27
  const z = __importStar(require("zod"));
28
28
  const step_1 = require("./step");
29
29
  const workflows_1 = require("./protoc/workflows");
@@ -44,10 +44,15 @@ exports.WorkflowConcurrency = z.object({
44
44
  limitStrategy: z.nativeEnum(exports.ConcurrencyLimitStrategy).optional(),
45
45
  });
46
46
  exports.HatchetTimeoutSchema = z.string();
47
+ exports.StickyStrategy = workflows_1.StickyStrategy;
47
48
  exports.CreateWorkflowSchema = z.object({
48
49
  id: z.string(),
49
50
  description: z.string(),
50
51
  version: z.string().optional(),
52
+ /**
53
+ * sticky will attempt to run all steps for workflow on the same worker
54
+ */
55
+ sticky: z.nativeEnum(exports.StickyStrategy).optional(),
51
56
  scheduleTimeout: z.string().optional(),
52
57
  /**
53
58
  * @deprecated Workflow timeout is deprecated. Use step timeouts instead.