@hatchet-dev/typescript-sdk 1.28.2 → 1.29.1

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.
@@ -0,0 +1,280 @@
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.orderWorkflow = exports.callModel = exports.syncCustomer = exports.weeklyReport = exports.packOrder = exports.processItem = exports.approvalFlow = exports.orderFollowUp = exports.chargeOrderWithRetries = exports.onboardingFlow = exports.sendFollowupEmail = exports.sendWelcomeEmail = exports.processOrder = exports.fulfillOrder = exports.chargeOrder = exports.validateOrder = exports.hatchet = void 0;
13
+ // > Hatchet worker
14
+ const v1_1 = require("../..");
15
+ exports.hatchet = v1_1.HatchetClient.init();
16
+ function main() {
17
+ return __awaiter(this, void 0, void 0, function* () {
18
+ const worker = yield exports.hatchet.worker('order-worker', {
19
+ workflows: [exports.validateOrder, exports.chargeOrder, exports.fulfillOrder, exports.processOrder],
20
+ slots: 10,
21
+ });
22
+ yield worker.start();
23
+ });
24
+ }
25
+ const payments = {
26
+ charge: (orderId) => __awaiter(void 0, void 0, void 0, function* () { return 2500 + orderId.length; }),
27
+ };
28
+ const warehouse = {
29
+ reserve: (orderId) => __awaiter(void 0, void 0, void 0, function* () { return orderId.length > 0; }),
30
+ ship: (orderId) => __awaiter(void 0, void 0, void 0, function* () { return `shp_${orderId}`; }),
31
+ pack: (item) => __awaiter(void 0, void 0, void 0, function* () { return item.length > 0; }),
32
+ };
33
+ const emails = {
34
+ send: (address, template) => __awaiter(void 0, void 0, void 0, function* () { return address.includes('@') && template.length > 0; }),
35
+ };
36
+ const models = {
37
+ complete: (prompt) => __awaiter(void 0, void 0, void 0, function* () { return prompt.toUpperCase(); }),
38
+ };
39
+ const crm = {
40
+ sync: (customerId) => __awaiter(void 0, void 0, void 0, function* () { return customerId.length; }),
41
+ };
42
+ const reports = {
43
+ build: (kind) => __awaiter(void 0, void 0, void 0, function* () { return kind.length; }),
44
+ };
45
+ exports.validateOrder = exports.hatchet.task({
46
+ name: 'validate-order',
47
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
48
+ // > Hatchet context logging
49
+ ctx.logger.info(`validating order ${input.orderId}`);
50
+ // !!
51
+ return { valid: yield warehouse.reserve(input.orderId) };
52
+ }),
53
+ });
54
+ exports.chargeOrder = exports.hatchet.task({
55
+ name: 'charge-order',
56
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
57
+ const amountCents = yield payments.charge(input.orderId);
58
+ return { charged: amountCents > 0, amountCents };
59
+ }),
60
+ });
61
+ // !!
62
+ exports.fulfillOrder = exports.hatchet.task({
63
+ name: 'fulfill-order',
64
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
65
+ const shipmentId = yield warehouse.ship(input.orderId);
66
+ return { fulfilled: true, shipmentId };
67
+ }),
68
+ });
69
+ // > Hatchet workflow as durable task
70
+ exports.processOrder = exports.hatchet.durableTask({
71
+ name: 'ProcessOrder',
72
+ executionTimeout: '10m',
73
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
74
+ yield exports.validateOrder.run(input);
75
+ yield exports.chargeOrder.run(input);
76
+ return exports.fulfillOrder.run(input);
77
+ }),
78
+ });
79
+ // !!
80
+ exports.sendWelcomeEmail = exports.hatchet.task({
81
+ name: 'send-welcome-email',
82
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
83
+ return ({
84
+ sent: yield emails.send(input.email, 'welcome'),
85
+ });
86
+ }),
87
+ });
88
+ exports.sendFollowupEmail = exports.hatchet.task({
89
+ name: 'send-followup-email',
90
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
91
+ return ({
92
+ sent: yield emails.send(input.email, 'followup'),
93
+ });
94
+ }),
95
+ });
96
+ // > Hatchet durable task with sleep
97
+ exports.onboardingFlow = exports.hatchet.durableTask({
98
+ name: 'OnboardingFlow',
99
+ // The timeout has to cover the whole wall-clock span of the run, sleeps included.
100
+ executionTimeout: '168h',
101
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
102
+ yield exports.sendWelcomeEmail.run(input);
103
+ yield ctx.sleepFor('72h');
104
+ return exports.sendFollowupEmail.run(input);
105
+ }),
106
+ });
107
+ // !!
108
+ function invoke(input) {
109
+ return __awaiter(this, void 0, void 0, function* () {
110
+ // > Hatchet task invocation
111
+ const run = yield exports.processOrder.runNoWait(input);
112
+ // It may be helpful to store this run id somewhere durable.
113
+ const runId = yield run.getWorkflowRunId();
114
+ const result = yield run.output;
115
+ // !!
116
+ return { runId, result };
117
+ });
118
+ }
119
+ // > Hatchet retries and timeouts
120
+ exports.chargeOrderWithRetries = exports.hatchet.task({
121
+ name: 'charge-order-with-retries',
122
+ retries: 10,
123
+ backoff: {
124
+ // Factor to increase the wait time between retries.
125
+ factor: 2,
126
+ // Maximum number of seconds to wait between retries.
127
+ maxSeconds: 10,
128
+ },
129
+ executionTimeout: '30s',
130
+ scheduleTimeout: '10m',
131
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
132
+ const amountCents = yield payments.charge(input.orderId);
133
+ return { charged: amountCents > 0, amountCents };
134
+ }),
135
+ });
136
+ // !!
137
+ exports.orderFollowUp = exports.hatchet.durableTask({
138
+ name: 'OrderFollowUp',
139
+ executionTimeout: '48h',
140
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
141
+ // > Hatchet durable sleep
142
+ yield ctx.sleepFor('24h');
143
+ // !!
144
+ return { sent: yield emails.send(`${input.orderId}@example.com`, 'order-followup') };
145
+ }),
146
+ });
147
+ function grantApproval(input) {
148
+ return __awaiter(this, void 0, void 0, function* () {
149
+ // > Hatchet event push
150
+ yield exports.hatchet.events.push('approval:granted', {
151
+ correlationId: input.correlationId,
152
+ });
153
+ // !!
154
+ });
155
+ }
156
+ // > Hatchet durable event wait
157
+ exports.approvalFlow = exports.hatchet.durableTask({
158
+ name: 'ApprovalFlow',
159
+ executionTimeout: '10m',
160
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
161
+ // The expression is compiled as CEL, so correlate on an id that cannot contain a quote.
162
+ yield ctx.waitForEvent('approval:granted', `input.correlationId == '${input.correlationId}'`);
163
+ return exports.fulfillOrder.run(input);
164
+ }),
165
+ });
166
+ exports.processItem = exports.hatchet.task({
167
+ name: 'process-item',
168
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
169
+ return ({
170
+ packed: yield warehouse.pack(input.item),
171
+ });
172
+ }),
173
+ });
174
+ // > Hatchet fan out children
175
+ exports.packOrder = exports.hatchet.durableTask({
176
+ name: 'PackOrder',
177
+ executionTimeout: '30m',
178
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
179
+ const results = yield Promise.all(input.items.map((item) => exports.processItem.run({ item })));
180
+ return { packed: results.filter((result) => result.packed).length };
181
+ }),
182
+ });
183
+ // > Hatchet cron declaration
184
+ exports.weeklyReport = exports.hatchet.workflow({
185
+ name: 'weekly-report',
186
+ on: {
187
+ cron: '0 9 * * 1',
188
+ },
189
+ });
190
+ exports.weeklyReport.task({
191
+ name: 'generate',
192
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
193
+ return { rows: yield reports.build(input.kind) };
194
+ }),
195
+ });
196
+ // !!
197
+ function schedule() {
198
+ return __awaiter(this, void 0, void 0, function* () {
199
+ // > Hatchet runtime schedules
200
+ // A recurring schedule, created at runtime.
201
+ yield exports.hatchet.crons.create('weekly-report', {
202
+ name: 'weekly-report-acme',
203
+ expression: '0 9 * * 1',
204
+ input: { kind: 'weekly' },
205
+ });
206
+ // A one-shot future run.
207
+ yield exports.hatchet.schedules.create('weekly-report', {
208
+ triggerAt: new Date(Date.now() + 24 * 60 * 60 * 1000),
209
+ input: { kind: 'weekly' },
210
+ });
211
+ // !!
212
+ });
213
+ }
214
+ // > Hatchet concurrency and rate limits
215
+ const v1_2 = require("../..");
216
+ // One in-flight run per customer, newest cancels the oldest.
217
+ exports.syncCustomer = exports.hatchet.workflow({
218
+ name: 'SyncCustomer',
219
+ concurrency: {
220
+ expression: 'input.customerId',
221
+ maxRuns: 1,
222
+ limitStrategy: v1_2.ConcurrencyLimitStrategy.CANCEL_IN_PROGRESS,
223
+ },
224
+ });
225
+ exports.syncCustomer.task({
226
+ name: 'sync',
227
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
228
+ return { synced: yield crm.sync(input.customerId) };
229
+ }),
230
+ });
231
+ // A global budget shared by every worker, not per-process.
232
+ exports.callModel = exports.hatchet.task({
233
+ name: 'call-model',
234
+ rateLimits: [
235
+ {
236
+ staticKey: 'openai',
237
+ units: 1,
238
+ },
239
+ ],
240
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
241
+ return { completion: yield models.complete(input.prompt) };
242
+ }),
243
+ });
244
+ // > Hatchet DAG workflow
245
+ exports.orderWorkflow = exports.hatchet.workflow({
246
+ name: 'ProcessOrderDag',
247
+ });
248
+ const validate = exports.orderWorkflow.task({
249
+ name: 'validate',
250
+ executionTimeout: '30s',
251
+ fn: (input) => __awaiter(void 0, void 0, void 0, function* () {
252
+ return { valid: yield warehouse.reserve(input.orderId) };
253
+ }),
254
+ });
255
+ const charge = exports.orderWorkflow.task({
256
+ name: 'charge',
257
+ parents: [validate],
258
+ executionTimeout: '30s',
259
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
260
+ const validated = yield ctx.parentOutput(validate);
261
+ if (!validated.valid) {
262
+ return { charged: false, amountCents: 0 };
263
+ }
264
+ const amountCents = yield payments.charge(input.orderId);
265
+ return { charged: true, amountCents };
266
+ }),
267
+ });
268
+ exports.orderWorkflow.task({
269
+ name: 'fulfill',
270
+ parents: [charge],
271
+ executionTimeout: '30s',
272
+ fn: (input, ctx) => __awaiter(void 0, void 0, void 0, function* () {
273
+ const charged = yield ctx.parentOutput(charge);
274
+ if (!charged.charged) {
275
+ return { fulfilled: false, shipmentId: '' };
276
+ }
277
+ return { fulfilled: true, shipmentId: yield warehouse.ship(input.orderId) };
278
+ }),
279
+ });
280
+ // !!
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.28.2";
1
+ export declare const HATCHET_VERSION = "1.29.1";
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.28.2';
4
+ exports.HATCHET_VERSION = '1.29.1';