@hatchet-dev/typescript-sdk 1.0.0-alpha1 → 1.0.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 (194) hide show
  1. package/clients/admin/admin-client.d.ts +15 -20
  2. package/clients/admin/admin-client.js +21 -17
  3. package/clients/hatchet-client/hatchet-client.d.ts +5 -4
  4. package/clients/hatchet-client/hatchet-client.js +6 -4
  5. package/clients/listeners/durable-listener/durable-listener-client.d.ts +25 -0
  6. package/clients/listeners/durable-listener/durable-listener-client.js +30 -0
  7. package/clients/listeners/durable-listener/pooled-durable-listener-client.d.ts +43 -0
  8. package/clients/listeners/durable-listener/pooled-durable-listener-client.js +241 -0
  9. package/clients/{listener/listener-client.d.ts → listeners/run-listener/child-listener-client.d.ts} +8 -8
  10. package/clients/{listener/listener-client.js → listeners/run-listener/child-listener-client.js} +8 -19
  11. package/clients/{listener/child-listener-client.d.ts → listeners/run-listener/pooled-child-listener-client.d.ts} +5 -5
  12. package/clients/{listener/child-listener-client.js → listeners/run-listener/pooled-child-listener-client.js} +5 -5
  13. package/clients/rest/generated/Api.d.ts +49 -2
  14. package/clients/rest/generated/Api.js +30 -0
  15. package/clients/rest/generated/data-contracts.d.ts +88 -82
  16. package/clients/rest/generated/data-contracts.js +8 -1
  17. package/clients/worker/worker.d.ts +5 -1
  18. package/clients/worker/worker.js +173 -2
  19. package/examples/affinity-workers.js +5 -1
  20. package/examples/api.js +1 -1
  21. package/examples/bulk-fanout-worker.js +1 -1
  22. package/examples/byo-logger.js +4 -0
  23. package/examples/concurrency/cancel-in-progress/concurrency-event.js +2 -2
  24. package/examples/concurrency/group-round-robin/concurrency-event.js +1 -1
  25. package/examples/crons/programatic-crons.js +4 -4
  26. package/examples/dag-worker.js +0 -1
  27. package/examples/example-event.js +2 -2
  28. package/examples/logger.js +2 -0
  29. package/examples/on-failure.js +1 -1
  30. package/examples/rate-limit/events.js +3 -3
  31. package/examples/scheduled-runs/programatic-schedules.js +4 -4
  32. package/examples/sticky-worker-with-check.js +2 -2
  33. package/examples/sticky-worker.js +5 -0
  34. package/index.d.ts +1 -3
  35. package/index.js +1 -3
  36. package/package.json +2 -2
  37. package/protoc/dispatcher/dispatcher.d.ts +2 -0
  38. package/protoc/dispatcher/dispatcher.js +1 -1
  39. package/protoc/events/events.js +1 -1
  40. package/protoc/google/protobuf/timestamp.js +1 -1
  41. package/protoc/v1/dispatcher.d.ts +77 -0
  42. package/protoc/v1/dispatcher.js +341 -0
  43. package/protoc/v1/shared/condition.d.ts +59 -0
  44. package/protoc/v1/shared/condition.js +549 -0
  45. package/protoc/v1/workflows.d.ts +263 -0
  46. package/protoc/v1/workflows.js +1823 -0
  47. package/protoc/workflows/workflows.js +1 -1
  48. package/step.d.ts +173 -47
  49. package/step.js +208 -64
  50. package/util/sleep.d.ts +7 -0
  51. package/util/sleep.js +7 -0
  52. package/util/workflow-run-ref.d.ts +8 -2
  53. package/util/workflow-run-ref.js +13 -0
  54. package/v1/client/client.d.ts +125 -7
  55. package/v1/client/client.interface.d.ts +9 -1
  56. package/v1/client/client.js +148 -17
  57. package/v1/client/duration.d.ts +7 -0
  58. package/v1/client/duration.js +2 -0
  59. package/v1/client/features/index.d.ts +5 -0
  60. package/v1/client/features/index.js +21 -0
  61. package/v1/client/features/metrics.d.ts +14 -0
  62. package/v1/client/features/metrics.js +45 -0
  63. package/v1/client/features/ratelimits.d.ts +20 -0
  64. package/v1/client/features/ratelimits.js +40 -0
  65. package/v1/client/features/runs.d.ts +15 -0
  66. package/v1/client/features/runs.js +60 -0
  67. package/v1/client/features/workers.d.ts +14 -0
  68. package/v1/client/features/workers.js +56 -0
  69. package/v1/client/features/workflows.d.ts +17 -0
  70. package/v1/client/features/workflows.js +93 -0
  71. package/v1/client/worker.d.ts +25 -15
  72. package/v1/client/worker.js +76 -47
  73. package/v1/conditions/base.d.ts +18 -0
  74. package/v1/conditions/base.js +18 -0
  75. package/v1/conditions/index.d.ts +31 -0
  76. package/v1/conditions/index.js +91 -0
  77. package/v1/conditions/parent-condition.d.ts +40 -0
  78. package/v1/conditions/parent-condition.js +36 -0
  79. package/v1/conditions/sleep-condition.d.ts +47 -0
  80. package/v1/conditions/sleep-condition.js +38 -0
  81. package/v1/conditions/transformer.d.ts +5 -0
  82. package/v1/conditions/transformer.js +52 -0
  83. package/v1/conditions/user-event-condition.d.ts +50 -0
  84. package/v1/conditions/user-event-condition.js +39 -0
  85. package/v1/declaration.d.ts +309 -0
  86. package/v1/declaration.js +296 -0
  87. package/v1/examples/child_workflows/worker.js +2 -2
  88. package/v1/examples/child_workflows/workflow.d.ts +2 -2
  89. package/v1/examples/child_workflows/workflow.js +5 -3
  90. package/v1/examples/concurrency-rr/load.js +2 -2
  91. package/v1/examples/concurrency-rr/worker.js +2 -2
  92. package/v1/examples/concurrency-rr/workflow.d.ts +1 -1
  93. package/v1/examples/concurrency-rr/workflow.js +2 -3
  94. package/v1/examples/dag/worker.js +2 -2
  95. package/v1/examples/dag/workflow.d.ts +1 -1
  96. package/v1/examples/dag/workflow.js +3 -3
  97. package/v1/examples/dag_match_condition/event.js +28 -0
  98. package/v1/examples/dag_match_condition/run.d.ts +1 -0
  99. package/v1/examples/dag_match_condition/run.js +25 -0
  100. package/v1/examples/dag_match_condition/worker.d.ts +1 -0
  101. package/{examples/playground.js → v1/examples/dag_match_condition/worker.js} +7 -22
  102. package/v1/examples/dag_match_condition/workflow.d.ts +11 -0
  103. package/v1/examples/dag_match_condition/workflow.js +41 -0
  104. package/v1/examples/deep/worker.js +2 -2
  105. package/v1/examples/deep/workflow.d.ts +6 -6
  106. package/v1/examples/deep/workflow.js +7 -7
  107. package/v1/examples/durable-sleep/event.d.ts +1 -0
  108. package/v1/examples/durable-sleep/event.js +28 -0
  109. package/v1/examples/durable-sleep/run.d.ts +1 -0
  110. package/v1/examples/durable-sleep/run.js +30 -0
  111. package/v1/examples/durable-sleep/worker.d.ts +1 -0
  112. package/v1/examples/durable-sleep/worker.js +24 -0
  113. package/v1/examples/durable-sleep/workflow.d.ts +1 -0
  114. package/v1/examples/durable-sleep/workflow.js +37 -0
  115. package/v1/examples/inferred-typing/run.d.ts +1 -0
  116. package/v1/examples/inferred-typing/run.js +41 -0
  117. package/v1/examples/inferred-typing/worker.d.ts +1 -0
  118. package/v1/examples/inferred-typing/worker.js +24 -0
  119. package/v1/examples/inferred-typing/workflow.d.ts +15 -0
  120. package/v1/examples/inferred-typing/workflow.js +44 -0
  121. package/v1/examples/landing_page/durable-excution.d.ts +3 -0
  122. package/v1/examples/landing_page/durable-excution.js +40 -0
  123. package/v1/examples/landing_page/event-signaling.d.ts +6 -0
  124. package/v1/examples/landing_page/event-signaling.js +16 -0
  125. package/v1/examples/landing_page/flow-control.d.ts +6 -0
  126. package/v1/examples/landing_page/flow-control.js +26 -0
  127. package/v1/examples/landing_page/queues.d.ts +3 -0
  128. package/v1/examples/landing_page/queues.js +33 -0
  129. package/v1/examples/landing_page/scheduling.d.ts +1 -0
  130. package/v1/examples/landing_page/scheduling.js +12 -0
  131. package/v1/examples/landing_page/task-routing.d.ts +6 -0
  132. package/v1/examples/landing_page/task-routing.js +25 -0
  133. package/v1/examples/legacy/run.js +2 -2
  134. package/v1/examples/legacy/worker.js +2 -2
  135. package/v1/examples/on_cron/worker.d.ts +1 -0
  136. package/v1/examples/on_cron/worker.js +24 -0
  137. package/v1/examples/on_cron/workflow.d.ts +10 -0
  138. package/v1/examples/on_cron/workflow.js +21 -0
  139. package/v1/examples/on_event/event.js +3 -2
  140. package/v1/examples/on_event/worker.js +2 -2
  141. package/v1/examples/on_event/workflow.d.ts +4 -4
  142. package/v1/examples/on_event/workflow.js +6 -3
  143. package/v1/examples/on_event copy/event.d.ts +1 -0
  144. package/v1/examples/on_event copy/event.js +26 -0
  145. package/v1/examples/on_event copy/worker.d.ts +1 -0
  146. package/v1/examples/on_event copy/worker.js +24 -0
  147. package/v1/examples/on_event copy/workflow.d.ts +16 -0
  148. package/v1/examples/on_event copy/workflow.js +35 -0
  149. package/v1/examples/on_failure/worker.js +2 -2
  150. package/v1/examples/on_failure/workflow.d.ts +1 -1
  151. package/v1/examples/on_failure/workflow.js +18 -8
  152. package/v1/examples/on_success/run.d.ts +1 -0
  153. package/v1/examples/on_success/run.js +31 -0
  154. package/v1/examples/on_success/worker.d.ts +1 -0
  155. package/v1/examples/on_success/worker.js +24 -0
  156. package/v1/examples/on_success/workflow.d.ts +4 -0
  157. package/v1/examples/on_success/workflow.js +59 -0
  158. package/v1/examples/retries/run.d.ts +1 -0
  159. package/v1/examples/retries/run.js +29 -0
  160. package/v1/examples/retries/worker.d.ts +1 -0
  161. package/v1/examples/retries/worker.js +24 -0
  162. package/v1/examples/retries/workflow.d.ts +5 -0
  163. package/v1/examples/retries/workflow.js +53 -0
  164. package/v1/examples/simple/client-run.d.ts +1 -0
  165. package/v1/examples/simple/client-run.js +16 -0
  166. package/v1/examples/simple/cron.js +6 -2
  167. package/v1/examples/simple/delay.js +2 -2
  168. package/v1/examples/simple/enqueue.d.ts +1 -0
  169. package/v1/examples/simple/enqueue.js +43 -0
  170. package/v1/examples/simple/run.js +8 -3
  171. package/v1/examples/simple/schedule.js +11 -5
  172. package/v1/examples/simple/stub-workflow.d.ts +9 -0
  173. package/v1/examples/simple/stub-workflow.js +17 -0
  174. package/v1/examples/simple/worker.js +7 -2
  175. package/v1/examples/simple/workflow.d.ts +4 -3
  176. package/v1/examples/simple/workflow.js +5 -5
  177. package/v1/examples/sticky/run.d.ts +1 -0
  178. package/v1/examples/sticky/run.js +29 -0
  179. package/v1/examples/sticky/worker.d.ts +1 -0
  180. package/v1/examples/sticky/worker.js +24 -0
  181. package/v1/examples/sticky/workflow.d.ts +7 -0
  182. package/v1/examples/sticky/workflow.js +32 -0
  183. package/v1/examples/with_timeouts/workflow.d.ts +9 -0
  184. package/v1/examples/with_timeouts/workflow.js +62 -0
  185. package/v1/index.d.ts +5 -0
  186. package/v1/index.js +20 -0
  187. package/v1/task.d.ts +144 -14
  188. package/version.d.ts +1 -1
  189. package/version.js +1 -1
  190. package/v1/workflow.d.ts +0 -158
  191. package/v1/workflow.js +0 -145
  192. /package/{examples/playground.d.ts → v1/examples/dag_match_condition/event.d.ts} +0 -0
  193. /package/v1/examples/{client.d.ts → hatchet-client.d.ts} +0 -0
  194. /package/v1/examples/{client.js → hatchet-client.js} +0 -0
@@ -0,0 +1,309 @@
1
+ import WorkflowRunRef from '../util/workflow-run-ref';
2
+ import { Context, DurableContext, JsonObject } from '../step';
3
+ import { CronWorkflows, ScheduledWorkflows } from '../clients/rest/generated/data-contracts';
4
+ import { Workflow as WorkflowV0 } from '../workflow';
5
+ import { IHatchetClient } from './client/client.interface';
6
+ import { CreateWorkflowTaskOpts, CreateOnFailureTaskOpts, TaskConcurrency, TaskFn, CreateWorkflowDurableTaskOpts, CreateBaseTaskOpts, CreateOnSuccessTaskOpts } from './task';
7
+ import { Duration } from './client/duration';
8
+ import { MetricsClient } from './client/features/metrics';
9
+ /**
10
+ * Additional metadata that can be attached to a workflow run.
11
+ */
12
+ type AdditionalMetadata = Record<string, string>;
13
+ /**
14
+ * Options for running a workflow.
15
+ */
16
+ export type RunOpts = {
17
+ /**
18
+ * Additional metadata to attach to the workflow run.
19
+ */
20
+ additionalMetadata?: AdditionalMetadata;
21
+ };
22
+ /**
23
+ * Extracts a property from an object type based on task name, or falls back to inferred type
24
+ */
25
+ export type TaskOutputType<K, TaskName extends string, InferredType> = TaskName extends keyof K ? K[TaskName] : InferredType;
26
+ export type CreateBaseWorkflowOpts = {
27
+ /**
28
+ * The name of the workflow.
29
+ */
30
+ name: WorkflowV0['id'];
31
+ /**
32
+ * (optional) description of the workflow.
33
+ */
34
+ description?: WorkflowV0['description'];
35
+ /**
36
+ * (optional) version of the workflow.
37
+ */
38
+ version?: WorkflowV0['version'];
39
+ /**
40
+ * (optional) sticky strategy for the workflow.
41
+ */
42
+ sticky?: WorkflowV0['sticky'];
43
+ /**
44
+ * (optional) on config for the workflow.
45
+ * @deprecated use onCrons and onEvents instead
46
+ */
47
+ on?: WorkflowV0['on'];
48
+ /**
49
+ * (optional) cron config for the workflow.
50
+ */
51
+ onCrons?: string[];
52
+ /**
53
+ * (optional) event config for the workflow.
54
+ */
55
+ onEvents?: string[];
56
+ concurrency?: TaskConcurrency;
57
+ /**
58
+ * (optional) onFailure handler for the workflow.
59
+ * Invoked when any task in the workflow fails.
60
+ * @param ctx The context of the workflow.
61
+ */
62
+ onFailure?: TaskFn<any, any> | CreateOnFailureTaskOpts<any, any>;
63
+ /**
64
+ * (optional) onSuccess handler for the workflow.
65
+ * Invoked when all tasks in the workflow complete successfully.
66
+ * @param ctx The context of the workflow.
67
+ */
68
+ onSuccess?: TaskFn<any, any> | CreateOnSuccessTaskOpts<any, any>;
69
+ };
70
+ export type CreateTaskWorkflowOpts<T extends JsonObject = any, K extends JsonObject = any> = CreateBaseWorkflowOpts & CreateBaseTaskOpts<T, K, TaskFn<T, K>>;
71
+ /**
72
+ * Options for creating a new workflow.
73
+ */
74
+ export type CreateWorkflowOpts = CreateBaseWorkflowOpts & {
75
+ /**
76
+ * (optional) default configuration for all tasks in the workflow.
77
+ */
78
+ taskDefaults?: TaskDefaults;
79
+ };
80
+ /**
81
+ * Default configuration for all tasks in the workflow.
82
+ * Can be overridden by task-specific options.
83
+ */
84
+ export type TaskDefaults = {
85
+ /**
86
+ * (optional) execution timeout duration for the task after it starts running
87
+ * go duration format (e.g., "1s", "5m", "1h").
88
+ *
89
+ * default: 60s
90
+ */
91
+ executionTimeout?: Duration;
92
+ /**
93
+ * (optional) schedule timeout for the task (max duration to allow the task to wait in the queue)
94
+ * go duration format (e.g., "1s", "5m", "1h").
95
+ *
96
+ * default: 5m
97
+ */
98
+ scheduleTimeout?: Duration;
99
+ /**
100
+ * (optional) number of retries for the task.
101
+ *
102
+ * default: 0
103
+ */
104
+ retries?: CreateWorkflowTaskOpts<any, any>['retries'];
105
+ /**
106
+ * (optional) backoff strategy configuration for retries.
107
+ * - factor: Base of the exponential backoff (base ^ retry count)
108
+ * - maxSeconds: Maximum backoff duration in seconds
109
+ */
110
+ backoff?: CreateWorkflowTaskOpts<any, any>['backoff'];
111
+ /**
112
+ * (optional) rate limits for the task.
113
+ */
114
+ rateLimits?: CreateWorkflowTaskOpts<any, any>['rateLimits'];
115
+ /**
116
+ * (optional) worker labels for task routing and scheduling.
117
+ * Each label can be a simple string/number value or an object with additional configuration:
118
+ * - value: The label value (string or number)
119
+ * - required: Whether the label is required for worker matching
120
+ * - weight: Priority weight for worker selection
121
+ * - comparator: Custom comparison logic for label matching
122
+ */
123
+ workerLabels?: CreateWorkflowTaskOpts<any, any>['desiredWorkerLabels'];
124
+ /**
125
+ * (optional) the concurrency options for the task.
126
+ */
127
+ concurrency?: TaskConcurrency | TaskConcurrency[];
128
+ };
129
+ /**
130
+ * Internal definition of a workflow and its tasks.
131
+ */
132
+ export type WorkflowDefinition = CreateWorkflowOpts & {
133
+ /**
134
+ * The tasks that make up this workflow.
135
+ */
136
+ _tasks: CreateWorkflowTaskOpts<any, any>[];
137
+ /**
138
+ * The durable tasks that make up this workflow.
139
+ */
140
+ _durableTasks: CreateWorkflowDurableTaskOpts<any, any>[];
141
+ };
142
+ /**
143
+ * Represents a workflow that can be executed by Hatchet.
144
+ * @template T The input type for the workflow.
145
+ * @template K The return type of the workflow.
146
+ */
147
+ export declare class BaseWorkflowDeclaration<T extends JsonObject, K extends JsonObject> {
148
+ /**
149
+ * The Hatchet client instance used to execute the workflow.
150
+ */
151
+ client: IHatchetClient | undefined;
152
+ /**
153
+ * The internal workflow definition.
154
+ */
155
+ definition: WorkflowDefinition;
156
+ /**
157
+ * Creates a new workflow instance.
158
+ * @param options The options for creating the workflow.
159
+ * @param client Optional Hatchet client instance.
160
+ */
161
+ constructor(options: CreateWorkflowOpts, client?: IHatchetClient);
162
+ /**
163
+ * Triggers a workflow run without waiting for completion.
164
+ * @param input The input data for the workflow.
165
+ * @param options Optional configuration for this workflow run.
166
+ * @returns A WorkflowRunRef containing the run ID and methods to get results and interact with the run.
167
+ * @throws Error if the workflow is not bound to a Hatchet client.
168
+ */
169
+ runNoWait(input: T, options?: RunOpts): WorkflowRunRef<K>;
170
+ /**
171
+ * @alias run
172
+ * Triggers a workflow run and waits for the result.
173
+ * @template T - The input type for the workflow
174
+ * @template K - The return type of the workflow
175
+ * @param input - The input data for the workflow
176
+ * @param options - Configuration options for the workflow run
177
+ * @returns A promise that resolves with the workflow result
178
+ */
179
+ runAndWait(input: T, options?: RunOpts): Promise<K>;
180
+ runAndWait(input: T[], options?: RunOpts): Promise<K[]>;
181
+ /**
182
+ * Executes the workflow with the given input and awaits the results.
183
+ * @param input The input data for the workflow.
184
+ * @param options Optional configuration for this workflow run.
185
+ * @returns A promise that resolves with the workflow result.
186
+ * @throws Error if the workflow is not bound to a Hatchet client.
187
+ */
188
+ run(input: T, options?: RunOpts): Promise<K>;
189
+ run(input: T[], options?: RunOpts): Promise<K[]>;
190
+ /**
191
+ * Schedules a workflow to run at a specific date and time in the future.
192
+ * @param enqueueAt The date when the workflow should be triggered.
193
+ * @param input The input data for the workflow.
194
+ * @param options Optional configuration for this workflow run.
195
+ * @returns A promise that resolves with the scheduled workflow details.
196
+ * @throws Error if the workflow is not bound to a Hatchet client.
197
+ */
198
+ schedule(enqueueAt: Date, input: T, options?: RunOpts): Promise<ScheduledWorkflows>;
199
+ /**
200
+ * Schedules a workflow to run after a specified delay.
201
+ * @param duration The delay in seconds before the workflow should run.
202
+ * @param input The input data for the workflow.
203
+ * @param options Optional configuration for this workflow run.
204
+ * @returns A promise that resolves with the scheduled workflow details.
205
+ * @throws Error if the workflow is not bound to a Hatchet client.
206
+ */
207
+ delay(duration: number, input: T, options?: RunOpts): Promise<ScheduledWorkflows>;
208
+ /**
209
+ * Creates a cron schedule for the workflow.
210
+ * @param name The name of the cron schedule.
211
+ * @param expression The cron expression defining the schedule.
212
+ * @param input The input data for the workflow.
213
+ * @param options Optional configuration for this workflow run.
214
+ * @returns A promise that resolves with the cron workflow details.
215
+ * @throws Error if the workflow is not bound to a Hatchet client.
216
+ */
217
+ cron(name: string, expression: string, input: T, options?: RunOpts): Promise<CronWorkflows>;
218
+ /**
219
+ * Get metrics for the workflow.
220
+ * @param opts Optional configuration for the metrics request.
221
+ * @returns A promise that resolves with the workflow metrics.
222
+ * @throws Error if the workflow is not bound to a Hatchet client.
223
+ */
224
+ metrics(opts?: Parameters<MetricsClient['getWorkflowMetrics']>[1]): Promise<import("../clients/rest/generated/data-contracts").WorkflowMetrics>;
225
+ /**
226
+ * Get queue metrics for the workflow.
227
+ * @param opts Optional configuration for the metrics request.
228
+ * @returns A promise that resolves with the workflow metrics.
229
+ * @throws Error if the workflow is not bound to a Hatchet client.
230
+ */
231
+ queueMetrics(opts?: Omit<Parameters<MetricsClient['getQueueMetrics']>[0], 'workflows'>): Promise<import("../clients/rest/generated/data-contracts").TenantQueueMetrics>;
232
+ /**
233
+ * Get the current state of the workflow.
234
+ * @returns A promise that resolves with the workflow state.
235
+ * @throws Error if the workflow is not bound to a Hatchet client.
236
+ */
237
+ get(): Promise<any>;
238
+ get id(): string;
239
+ /**
240
+ * Get the friendly name of the workflow.
241
+ * @returns The name of the workflow.
242
+ */
243
+ get name(): string;
244
+ }
245
+ export declare class WorkflowDeclaration<T extends JsonObject, K extends JsonObject> extends BaseWorkflowDeclaration<T, K> {
246
+ /**
247
+ * Adds a task to the workflow.
248
+ * The return type will be either the property on K that corresponds to the task name,
249
+ * or if there is no matching property, the inferred return type of the function.
250
+ * @template Name The literal string name of the task.
251
+ * @template L The inferred return type of the task function.
252
+ * @param options The task configuration options.
253
+ * @returns The task options that were added.
254
+ */
255
+ task<Name extends string, L>(options: (Omit<CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>, 'fn'> & {
256
+ name: Name;
257
+ fn: (input: T, ctx: Context<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
258
+ }) | TaskWorkflowDeclaration<any, any>): CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>;
259
+ /**
260
+ * Adds a durable task to the workflow.
261
+ * The return type will be either the property on K that corresponds to the task name,
262
+ * or if there is no matching property, the inferred return type of the function.
263
+ * @template Name The literal string name of the task.
264
+ * @template L The inferred return type of the task function.
265
+ * @param options The task configuration options.
266
+ * @returns The task options that were added.
267
+ */
268
+ durableTask<Name extends string, L>(options: Omit<CreateWorkflowTaskOpts<T, TaskOutputType<K, Name, L>>, 'fn'> & {
269
+ name: Name;
270
+ fn: (input: T, ctx: DurableContext<T>) => TaskOutputType<K, Name, L> | Promise<TaskOutputType<K, Name, L>>;
271
+ }): CreateWorkflowDurableTaskOpts<T, TaskOutputType<K, Name, L>>;
272
+ }
273
+ export declare class TaskWorkflowDeclaration<T extends JsonObject, K extends JsonObject> extends BaseWorkflowDeclaration<T, K> {
274
+ private _standalone_task_name;
275
+ constructor(options: CreateTaskWorkflowOpts<T, K>, client?: IHatchetClient);
276
+ run(input: T, options?: RunOpts): Promise<K>;
277
+ run(input: T[], options?: RunOpts): Promise<K[]>;
278
+ get taskDef(): CreateWorkflowTaskOpts<any, any>;
279
+ }
280
+ /**
281
+ * Creates a new task workflow declaration with types inferred from the function parameter.
282
+ * @template Fn The type of the task function
283
+ * @param options The task configuration options.
284
+ * @param client Optional Hatchet client instance.
285
+ * @returns A new TaskWorkflowDeclaration with inferred types.
286
+ */
287
+ export declare function CreateTaskWorkflow<Fn extends (input: I, ctx?: any) => O | Promise<O>, I extends JsonObject = Parameters<Fn>[0], O extends JsonObject = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
288
+ fn: Fn;
289
+ } & Omit<CreateTaskWorkflowOpts<I, O>, 'fn'>, client?: IHatchetClient): TaskWorkflowDeclaration<I, O>;
290
+ /**
291
+ * Creates a new workflow instance.
292
+ * @template T The input type for the workflow.
293
+ * @template K The return type of the workflow.
294
+ * @param options The options for creating the workflow.
295
+ * @param client Optional Hatchet client instance.
296
+ * @returns A new Workflow instance.
297
+ */
298
+ export declare function CreateWorkflow<T extends JsonObject = any, K extends JsonObject = any>(options: CreateWorkflowOpts, client?: IHatchetClient): WorkflowDeclaration<T, K>;
299
+ /**
300
+ * Creates a new durable task workflow declaration with types inferred from the function parameter.
301
+ * @template Fn The type of the durable task function
302
+ * @param options The durable task configuration options.
303
+ * @param client Optional Hatchet client instance.
304
+ * @returns A new TaskWorkflowDeclaration with inferred types.
305
+ */
306
+ export declare function CreateDurableTaskWorkflow<Fn extends (input: I, ctx: DurableContext<I>) => O | Promise<O>, I extends JsonObject = Parameters<Fn>[0], O extends JsonObject = ReturnType<Fn> extends Promise<infer P> ? P extends JsonObject ? P : never : ReturnType<Fn> extends JsonObject ? ReturnType<Fn> : never>(options: {
307
+ fn: Fn;
308
+ } & Omit<CreateWorkflowDurableTaskOpts<I, O>, 'fn'>, client?: IHatchetClient): TaskWorkflowDeclaration<I, O>;
309
+ export {};
@@ -0,0 +1,296 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.TaskWorkflowDeclaration = exports.WorkflowDeclaration = exports.BaseWorkflowDeclaration = void 0;
13
+ exports.CreateTaskWorkflow = CreateTaskWorkflow;
14
+ exports.CreateWorkflow = CreateWorkflow;
15
+ exports.CreateDurableTaskWorkflow = CreateDurableTaskWorkflow;
16
+ const UNBOUND_ERR = new Error('workflow unbound to hatchet client, hint: use client.run instead');
17
+ /**
18
+ * Represents a workflow that can be executed by Hatchet.
19
+ * @template T The input type for the workflow.
20
+ * @template K The return type of the workflow.
21
+ */
22
+ class BaseWorkflowDeclaration {
23
+ /**
24
+ * Creates a new workflow instance.
25
+ * @param options The options for creating the workflow.
26
+ * @param client Optional Hatchet client instance.
27
+ */
28
+ constructor(options, client) {
29
+ this.definition = Object.assign(Object.assign({}, options), { _tasks: [], _durableTasks: [] });
30
+ this.client = client;
31
+ }
32
+ /**
33
+ * Triggers a workflow run without waiting for completion.
34
+ * @param input The input data for the workflow.
35
+ * @param options Optional configuration for this workflow run.
36
+ * @returns A WorkflowRunRef containing the run ID and methods to get results and interact with the run.
37
+ * @throws Error if the workflow is not bound to a Hatchet client.
38
+ */
39
+ runNoWait(input, options) {
40
+ if (!this.client) {
41
+ throw UNBOUND_ERR;
42
+ }
43
+ return this.client._v0.admin.runWorkflow(this.name, input, options);
44
+ }
45
+ runAndWait(input, options) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ if (!this.client) {
48
+ throw UNBOUND_ERR;
49
+ }
50
+ if (Array.isArray(input)) {
51
+ return Promise.all(input.map((i) => this.runAndWait(i, options)));
52
+ }
53
+ return this.run(input, options);
54
+ });
55
+ }
56
+ run(input, options) {
57
+ return __awaiter(this, void 0, void 0, function* () {
58
+ if (!this.client) {
59
+ throw UNBOUND_ERR;
60
+ }
61
+ if (Array.isArray(input)) {
62
+ // FIXME use bulk endpoint?
63
+ return Promise.all(input.map((i) => this.run(i, options)));
64
+ }
65
+ const res = this.client._v0.admin.runWorkflow(this.definition.name, input, options);
66
+ return res.result();
67
+ });
68
+ }
69
+ /**
70
+ * Schedules a workflow to run at a specific date and time in the future.
71
+ * @param enqueueAt The date when the workflow should be triggered.
72
+ * @param input The input data for the workflow.
73
+ * @param options Optional configuration for this workflow run.
74
+ * @returns A promise that resolves with the scheduled workflow details.
75
+ * @throws Error if the workflow is not bound to a Hatchet client.
76
+ */
77
+ schedule(enqueueAt, input, options) {
78
+ return __awaiter(this, void 0, void 0, function* () {
79
+ if (!this.client) {
80
+ throw UNBOUND_ERR;
81
+ }
82
+ const scheduled = this.client._v0.schedule.create(this.definition.name, {
83
+ triggerAt: enqueueAt,
84
+ input,
85
+ additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
86
+ });
87
+ return scheduled;
88
+ });
89
+ }
90
+ /**
91
+ * Schedules a workflow to run after a specified delay.
92
+ * @param duration The delay in seconds before the workflow should run.
93
+ * @param input The input data for the workflow.
94
+ * @param options Optional configuration for this workflow run.
95
+ * @returns A promise that resolves with the scheduled workflow details.
96
+ * @throws Error if the workflow is not bound to a Hatchet client.
97
+ */
98
+ delay(duration, input, options) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ const now = Date.now();
101
+ const triggerAt = new Date(now + duration * 1000);
102
+ return this.schedule(triggerAt, input, options);
103
+ });
104
+ }
105
+ /**
106
+ * Creates a cron schedule for the workflow.
107
+ * @param name The name of the cron schedule.
108
+ * @param expression The cron expression defining the schedule.
109
+ * @param input The input data for the workflow.
110
+ * @param options Optional configuration for this workflow run.
111
+ * @returns A promise that resolves with the cron workflow details.
112
+ * @throws Error if the workflow is not bound to a Hatchet client.
113
+ */
114
+ cron(name, expression, input, options) {
115
+ return __awaiter(this, void 0, void 0, function* () {
116
+ if (!this.client) {
117
+ throw UNBOUND_ERR;
118
+ }
119
+ const cronDef = this.client._v0.cron.create(this.definition.name, {
120
+ expression,
121
+ input,
122
+ additionalMetadata: options === null || options === void 0 ? void 0 : options.additionalMetadata,
123
+ name,
124
+ });
125
+ return cronDef;
126
+ });
127
+ }
128
+ /**
129
+ * Get metrics for the workflow.
130
+ * @param opts Optional configuration for the metrics request.
131
+ * @returns A promise that resolves with the workflow metrics.
132
+ * @throws Error if the workflow is not bound to a Hatchet client.
133
+ */
134
+ metrics(opts) {
135
+ if (!this.client) {
136
+ throw UNBOUND_ERR;
137
+ }
138
+ return this.client.metrics.getWorkflowMetrics(this.definition.name, opts);
139
+ }
140
+ /**
141
+ * Get queue metrics for the workflow.
142
+ * @param opts Optional configuration for the metrics request.
143
+ * @returns A promise that resolves with the workflow metrics.
144
+ * @throws Error if the workflow is not bound to a Hatchet client.
145
+ */
146
+ queueMetrics(opts) {
147
+ if (!this.client) {
148
+ throw UNBOUND_ERR;
149
+ }
150
+ return this.client.metrics.getQueueMetrics(Object.assign(Object.assign({}, opts), { workflows: [this.definition.name] }));
151
+ }
152
+ /**
153
+ * Get the current state of the workflow.
154
+ * @returns A promise that resolves with the workflow state.
155
+ * @throws Error if the workflow is not bound to a Hatchet client.
156
+ */
157
+ get() {
158
+ if (!this.client) {
159
+ throw UNBOUND_ERR;
160
+ }
161
+ return this.client.workflows.get(this);
162
+ }
163
+ // // gets the pause state of the workflow
164
+ // isPaused() {
165
+ // if (!this.client) {
166
+ // throw UNBOUND_ERR;
167
+ // }
168
+ // return this.client.workflows.isPaused(this);
169
+ // }
170
+ // // pause assignment of workflow
171
+ // pause() {
172
+ // if (!this.client) {
173
+ // throw UNBOUND_ERR;
174
+ // }
175
+ // return this.client.workflows.pause(this);
176
+ // }
177
+ // // unpause assignment of workflow
178
+ // unpause() {
179
+ // if (!this.client) {
180
+ // throw UNBOUND_ERR;
181
+ // }
182
+ // return this.client.workflows.unpause(this);
183
+ // }
184
+ // @deprecated use definition.name instead
185
+ get id() {
186
+ return this.definition.name;
187
+ }
188
+ /**
189
+ * Get the friendly name of the workflow.
190
+ * @returns The name of the workflow.
191
+ */
192
+ get name() {
193
+ return this.definition.name;
194
+ }
195
+ }
196
+ exports.BaseWorkflowDeclaration = BaseWorkflowDeclaration;
197
+ class WorkflowDeclaration extends BaseWorkflowDeclaration {
198
+ /**
199
+ * Adds a task to the workflow.
200
+ * The return type will be either the property on K that corresponds to the task name,
201
+ * or if there is no matching property, the inferred return type of the function.
202
+ * @template Name The literal string name of the task.
203
+ * @template L The inferred return type of the task function.
204
+ * @param options The task configuration options.
205
+ * @returns The task options that were added.
206
+ */
207
+ task(options) {
208
+ let typedOptions;
209
+ if (options instanceof TaskWorkflowDeclaration) {
210
+ typedOptions = options.taskDef;
211
+ }
212
+ else {
213
+ typedOptions = options;
214
+ }
215
+ this.definition._tasks.push(typedOptions);
216
+ return typedOptions;
217
+ }
218
+ /**
219
+ * Adds a durable task to the workflow.
220
+ * The return type will be either the property on K that corresponds to the task name,
221
+ * or if there is no matching property, the inferred return type of the function.
222
+ * @template Name The literal string name of the task.
223
+ * @template L The inferred return type of the task function.
224
+ * @param options The task configuration options.
225
+ * @returns The task options that were added.
226
+ */
227
+ durableTask(options) {
228
+ const typedOptions = options;
229
+ this.definition._durableTasks.push(typedOptions);
230
+ return typedOptions;
231
+ }
232
+ }
233
+ exports.WorkflowDeclaration = WorkflowDeclaration;
234
+ class TaskWorkflowDeclaration extends BaseWorkflowDeclaration {
235
+ constructor(options, client) {
236
+ super(Object.assign({}, options), client);
237
+ this._standalone_task_name = options.name;
238
+ this.definition._tasks.push(Object.assign({}, options));
239
+ }
240
+ run(input, options) {
241
+ const _super = Object.create(null, {
242
+ run: { get: () => super.run }
243
+ });
244
+ return __awaiter(this, void 0, void 0, function* () {
245
+ const res = yield _super.run.call(this, input, options);
246
+ if (Array.isArray(res)) {
247
+ return res.map((r) => r[this._standalone_task_name]);
248
+ }
249
+ return res[this._standalone_task_name];
250
+ });
251
+ }
252
+ get taskDef() {
253
+ return this.definition._tasks[0];
254
+ }
255
+ }
256
+ exports.TaskWorkflowDeclaration = TaskWorkflowDeclaration;
257
+ /**
258
+ * Creates a new task workflow declaration with types inferred from the function parameter.
259
+ * @template Fn The type of the task function
260
+ * @param options The task configuration options.
261
+ * @param client Optional Hatchet client instance.
262
+ * @returns A new TaskWorkflowDeclaration with inferred types.
263
+ */
264
+ function CreateTaskWorkflow(options, client) {
265
+ return new TaskWorkflowDeclaration(options, client);
266
+ }
267
+ /**
268
+ * Creates a new workflow instance.
269
+ * @template T The input type for the workflow.
270
+ * @template K The return type of the workflow.
271
+ * @param options The options for creating the workflow.
272
+ * @param client Optional Hatchet client instance.
273
+ * @returns A new Workflow instance.
274
+ */
275
+ function CreateWorkflow(options, client) {
276
+ return new WorkflowDeclaration(options, client);
277
+ }
278
+ /**
279
+ * Creates a new durable task workflow declaration with types inferred from the function parameter.
280
+ * @template Fn The type of the durable task function
281
+ * @param options The durable task configuration options.
282
+ * @param client Optional Hatchet client instance.
283
+ * @returns A new TaskWorkflowDeclaration with inferred types.
284
+ */
285
+ function CreateDurableTaskWorkflow(options, client) {
286
+ // Note: We're using TaskWorkflowDeclaration here since task and durableTask
287
+ // share the same declaration structure but with different task types
288
+ const taskWorkflow = new TaskWorkflowDeclaration(options, client);
289
+ // Move the task from tasks to durableTasks
290
+ if (taskWorkflow.definition._tasks.length > 0) {
291
+ const task = taskWorkflow.definition._tasks[0];
292
+ taskWorkflow.definition._tasks = [];
293
+ taskWorkflow.definition._durableTasks.push(task);
294
+ }
295
+ return taskWorkflow;
296
+ }
@@ -9,11 +9,11 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
- const client_1 = require("../client");
12
+ const hatchet_client_1 = require("../hatchet-client");
13
13
  const workflow_1 = require("./workflow");
14
14
  function main() {
15
15
  return __awaiter(this, void 0, void 0, function* () {
16
- const worker = yield client_1.hatchet.worker('child-workflow-worker', {
16
+ const worker = yield hatchet_client_1.hatchet.worker('child-workflow-worker', {
17
17
  workflows: [workflow_1.parent, workflow_1.child],
18
18
  });
19
19
  yield worker.start();
@@ -6,7 +6,7 @@ type ChildOutput = {
6
6
  Value: number;
7
7
  };
8
8
  };
9
- export declare const child: import("../../workflow").WorkflowDeclaration<ChildInput, ChildOutput>;
9
+ export declare const child: import("../..").WorkflowDeclaration<ChildInput, ChildOutput>;
10
10
  type ParentInput = {
11
11
  N: number;
12
12
  };
@@ -15,5 +15,5 @@ type ParentOutput = {
15
15
  Result: number;
16
16
  };
17
17
  };
18
- export declare const parent: import("../../workflow").WorkflowDeclaration<ParentInput, ParentOutput>;
18
+ export declare const parent: import("../..").WorkflowDeclaration<ParentInput, ParentOutput>;
19
19
  export {};
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
+ // ❓ Declaring a Child
2
3
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
4
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
5
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -10,8 +11,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
11
  };
11
12
  Object.defineProperty(exports, "__esModule", { value: true });
12
13
  exports.parent = exports.child = void 0;
13
- const client_1 = require("../client");
14
- exports.child = client_1.hatchet.workflow({
14
+ const hatchet_client_1 = require("../hatchet-client");
15
+ exports.child = hatchet_client_1.hatchet.workflow({
15
16
  name: 'child',
16
17
  });
17
18
  exports.child.task({
@@ -22,7 +23,7 @@ exports.child.task({
22
23
  };
23
24
  },
24
25
  });
25
- exports.parent = client_1.hatchet.workflow({
26
+ exports.parent = hatchet_client_1.hatchet.workflow({
26
27
  name: 'parent',
27
28
  });
28
29
  exports.parent.task({
@@ -41,3 +42,4 @@ exports.parent.task({
41
42
  };
42
43
  }),
43
44
  });
45
+ // !!
@@ -10,7 +10,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  /* eslint-disable no-plusplus */
13
- const client_1 = require("../client");
13
+ const hatchet_client_1 = require("../hatchet-client");
14
14
  const workflow_1 = require("./workflow");
15
15
  function generateRandomString(length) {
16
16
  const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
@@ -45,7 +45,7 @@ function main() {
45
45
  // Process workflows in batches
46
46
  for (let i = 0; i < workflowRuns.length; i += BATCH_SIZE) {
47
47
  const batch = workflowRuns.slice(i, i + BATCH_SIZE);
48
- yield client_1.hatchet.admin.runWorkflows(batch);
48
+ yield hatchet_client_1.hatchet.admin.runWorkflows(batch);
49
49
  }
50
50
  });
51
51
  }