@hatchet-dev/typescript-sdk 1.8.2 → 1.9.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.
@@ -33,6 +33,6 @@ export declare class EventClient {
33
33
  push<T>(type: string, input: T, options?: PushEventOptions): Promise<import("../../protoc/events/events").Event>;
34
34
  bulkPush<T>(type: string, inputs: EventWithMetadata<T>[], options?: PushEventOptions): Promise<import("../../protoc/events/events").Events>;
35
35
  putLog(stepRunId: string, log: string, level?: LogLevel, taskRetryCount?: number, metadata?: Record<string, any>): Promise<void>;
36
- putStream(stepRunId: string, data: string | Uint8Array): Promise<void>;
36
+ putStream(stepRunId: string, data: string | Uint8Array, index: number | undefined): Promise<void>;
37
37
  list(opts?: Parameters<typeof this.api.v1EventList>[1]): Promise<import("../rest/generated/data-contracts").V1EventList>;
38
38
  }
@@ -110,7 +110,7 @@ class EventClient {
110
110
  });
111
111
  });
112
112
  }
113
- putStream(stepRunId, data) {
113
+ putStream(stepRunId, data, index) {
114
114
  return __awaiter(this, void 0, void 0, function* () {
115
115
  const createdAt = new Date();
116
116
  let dataBytes;
@@ -128,6 +128,7 @@ class EventClient {
128
128
  stepRunId,
129
129
  createdAt,
130
130
  message: dataBytes,
131
+ eventIndex: index,
131
132
  });
132
133
  }), this.logger).catch((e) => {
133
134
  // log a warning, but this is not a fatal error
@@ -27,7 +27,7 @@ export declare class LegacyHatchetClient {
27
27
  tenantId: string;
28
28
  durableListener: DurableListenerClient;
29
29
  logger: Logger;
30
- constructor(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosOpts?: AxiosRequestConfig, runs?: RunsClient);
30
+ constructor(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosOpts?: AxiosRequestConfig, runs?: RunsClient, listener?: RunListenerClient);
31
31
  static init(config?: Partial<ClientConfig>, options?: HatchetClientOptions, axiosConfig?: AxiosRequestConfig): LegacyHatchetClient;
32
32
  run(workflow: string | V0Workflow): Promise<V0Worker>;
33
33
  worker(workflow: string | V0Workflow, opts?: Omit<WorkerOpts, 'name'> | number): Promise<V0Worker>;
@@ -27,7 +27,7 @@ const child_listener_client_1 = require("../listeners/run-listener/child-listene
27
27
  const rest_1 = __importDefault(require("../rest"));
28
28
  const durable_listener_client_1 = require("../listeners/durable-listener/durable-listener-client");
29
29
  class LegacyHatchetClient {
30
- constructor(config, options, axiosOpts, runs) {
30
+ constructor(config, options, axiosOpts, runs, listener) {
31
31
  // Initializes a new Client instance.
32
32
  // Loads config in the following order: config param > yaml file > env vars
33
33
  var _a;
@@ -56,7 +56,9 @@ class LegacyHatchetClient {
56
56
  this.api = (0, rest_1.default)(this.config.api_url, this.config.token, axiosOpts);
57
57
  this.event = new event_client_1.EventClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this);
58
58
  this.dispatcher = new dispatcher_client_1.DispatcherClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory);
59
- this.listener = new child_listener_client_1.RunListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
59
+ this.listener =
60
+ listener ||
61
+ new child_listener_client_1.RunListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
60
62
  this.admin = new admin_client_1.AdminClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api, this.tenantId, this.listener, this.runs);
61
63
  this.durableListener = new durable_listener_client_1.DurableListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, this.credentials), clientFactory, this.api);
62
64
  this.logger = this.config.logger('HatchetClient', this.config.log_level);
@@ -149,9 +149,11 @@ class RunEventListener {
149
149
  }
150
150
  finally { if (e_1) throw e_1.error; }
151
151
  }
152
+ this.eventEmitter.emit('complete');
152
153
  }
153
154
  catch (e) {
154
155
  if (e.code === nice_grpc_1.Status.CANCELLED) {
156
+ this.eventEmitter.emit('complete');
155
157
  return;
156
158
  }
157
159
  if (e.code === nice_grpc_1.Status.UNAVAILABLE) {
@@ -178,6 +180,11 @@ class RunEventListener {
178
180
  stream() {
179
181
  return __asyncGenerator(this, arguments, function* stream_1() {
180
182
  var _a, e_2, _b, _c;
183
+ let completed = false;
184
+ this.eventEmitter.once('complete', () => {
185
+ completed = true;
186
+ this.eventEmitter.emit('event');
187
+ });
181
188
  try {
182
189
  for (var _d = true, _e = __asyncValues((0, events_1.on)(this.eventEmitter, 'event')), _f; _f = yield __await(_e.next()), _a = _f.done, !_a; _d = true) {
183
190
  _c = _f.value;
@@ -189,6 +196,9 @@ class RunEventListener {
189
196
  yield yield __await(r);
190
197
  }
191
198
  }
199
+ if (completed && this.q.length === 0) {
200
+ break;
201
+ }
192
202
  }
193
203
  }
194
204
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
@@ -651,6 +651,7 @@ export interface WorkflowVersion {
651
651
  triggers?: WorkflowTriggers;
652
652
  scheduleTimeout?: string;
653
653
  jobs?: Job[];
654
+ workflowConfig?: object;
654
655
  }
655
656
  export interface WorkflowVersionDefinition {
656
657
  /** The raw YAML definition of the workflow. */
@@ -1482,6 +1483,7 @@ export interface V1TaskSummary {
1482
1483
  * @format uuid
1483
1484
  */
1484
1485
  workflowVersionId?: string;
1486
+ workflowConfig?: object;
1485
1487
  }
1486
1488
  export interface V1DagChildren {
1487
1489
  /** @format uuid */
@@ -1621,6 +1623,7 @@ export interface V1WorkflowRunDetails {
1621
1623
  taskName: string;
1622
1624
  }[];
1623
1625
  tasks: V1TaskSummary[];
1626
+ workflowConfig?: object;
1624
1627
  }
1625
1628
  export declare enum V1TaskRunStatus {
1626
1629
  PENDING = "PENDING",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.8.2",
3
+ "version": "1.9.1",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -252,6 +252,7 @@ export interface WorkflowEvent {
252
252
  stepRetries?: number | undefined;
253
253
  /** (optional) the retry count of this step */
254
254
  retryCount?: number | undefined;
255
+ eventIndex?: number | undefined;
255
256
  }
256
257
  export interface WorkflowRunEvent {
257
258
  /** the id of the workflow run */
@@ -2219,6 +2219,7 @@ function createBaseWorkflowEvent() {
2219
2219
  hangup: false,
2220
2220
  stepRetries: undefined,
2221
2221
  retryCount: undefined,
2222
+ eventIndex: undefined,
2222
2223
  };
2223
2224
  }
2224
2225
  exports.WorkflowEvent = {
@@ -2250,6 +2251,9 @@ exports.WorkflowEvent = {
2250
2251
  if (message.retryCount !== undefined) {
2251
2252
  writer.uint32(72).int32(message.retryCount);
2252
2253
  }
2254
+ if (message.eventIndex !== undefined) {
2255
+ writer.uint32(80).int64(message.eventIndex);
2256
+ }
2253
2257
  return writer;
2254
2258
  },
2255
2259
  decode(input, length) {
@@ -2322,6 +2326,13 @@ exports.WorkflowEvent = {
2322
2326
  message.retryCount = reader.int32();
2323
2327
  continue;
2324
2328
  }
2329
+ case 10: {
2330
+ if (tag !== 80) {
2331
+ break;
2332
+ }
2333
+ message.eventIndex = longToNumber(reader.int64());
2334
+ continue;
2335
+ }
2325
2336
  }
2326
2337
  if ((tag & 7) === 4 || tag === 0) {
2327
2338
  break;
@@ -2343,6 +2354,7 @@ exports.WorkflowEvent = {
2343
2354
  hangup: isSet(object.hangup) ? globalThis.Boolean(object.hangup) : false,
2344
2355
  stepRetries: isSet(object.stepRetries) ? globalThis.Number(object.stepRetries) : undefined,
2345
2356
  retryCount: isSet(object.retryCount) ? globalThis.Number(object.retryCount) : undefined,
2357
+ eventIndex: isSet(object.eventIndex) ? globalThis.Number(object.eventIndex) : undefined,
2346
2358
  };
2347
2359
  },
2348
2360
  toJSON(message) {
@@ -2374,13 +2386,16 @@ exports.WorkflowEvent = {
2374
2386
  if (message.retryCount !== undefined) {
2375
2387
  obj.retryCount = Math.round(message.retryCount);
2376
2388
  }
2389
+ if (message.eventIndex !== undefined) {
2390
+ obj.eventIndex = Math.round(message.eventIndex);
2391
+ }
2377
2392
  return obj;
2378
2393
  },
2379
2394
  create(base) {
2380
2395
  return exports.WorkflowEvent.fromPartial(base !== null && base !== void 0 ? base : {});
2381
2396
  },
2382
2397
  fromPartial(object) {
2383
- var _a, _b, _c, _d, _e, _f, _g, _h, _j;
2398
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
2384
2399
  const message = createBaseWorkflowEvent();
2385
2400
  message.workflowRunId = (_a = object.workflowRunId) !== null && _a !== void 0 ? _a : '';
2386
2401
  message.resourceType = (_b = object.resourceType) !== null && _b !== void 0 ? _b : 0;
@@ -2391,6 +2406,7 @@ exports.WorkflowEvent = {
2391
2406
  message.hangup = (_g = object.hangup) !== null && _g !== void 0 ? _g : false;
2392
2407
  message.stepRetries = (_h = object.stepRetries) !== null && _h !== void 0 ? _h : undefined;
2393
2408
  message.retryCount = (_j = object.retryCount) !== null && _j !== void 0 ? _j : undefined;
2409
+ message.eventIndex = (_k = object.eventIndex) !== null && _k !== void 0 ? _k : undefined;
2394
2410
  return message;
2395
2411
  },
2396
2412
  };
@@ -3202,6 +3218,16 @@ function fromJsonTimestamp(o) {
3202
3218
  return fromTimestamp(timestamp_1.Timestamp.fromJSON(o));
3203
3219
  }
3204
3220
  }
3221
+ function longToNumber(int64) {
3222
+ const num = globalThis.Number(int64.toString());
3223
+ if (num > globalThis.Number.MAX_SAFE_INTEGER) {
3224
+ throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER');
3225
+ }
3226
+ if (num < globalThis.Number.MIN_SAFE_INTEGER) {
3227
+ throw new globalThis.Error('Value is smaller than Number.MIN_SAFE_INTEGER');
3228
+ }
3229
+ return num;
3230
+ }
3205
3231
  function isObject(value) {
3206
3232
  return typeof value === 'object' && value !== null;
3207
3233
  }
@@ -45,6 +45,7 @@ export interface PutStreamEventRequest {
45
45
  message: Uint8Array;
46
46
  /** associated stream event metadata */
47
47
  metadata: string;
48
+ eventIndex?: number | undefined;
48
49
  }
49
50
  export interface PutStreamEventResponse {
50
51
  }
@@ -399,7 +399,13 @@ exports.PutLogResponse = {
399
399
  },
400
400
  };
401
401
  function createBasePutStreamEventRequest() {
402
- return { stepRunId: '', createdAt: undefined, message: new Uint8Array(0), metadata: '' };
402
+ return {
403
+ stepRunId: '',
404
+ createdAt: undefined,
405
+ message: new Uint8Array(0),
406
+ metadata: '',
407
+ eventIndex: undefined,
408
+ };
403
409
  }
404
410
  exports.PutStreamEventRequest = {
405
411
  encode(message, writer = new wire_1.BinaryWriter()) {
@@ -415,6 +421,9 @@ exports.PutStreamEventRequest = {
415
421
  if (message.metadata !== '') {
416
422
  writer.uint32(42).string(message.metadata);
417
423
  }
424
+ if (message.eventIndex !== undefined) {
425
+ writer.uint32(48).int64(message.eventIndex);
426
+ }
418
427
  return writer;
419
428
  },
420
429
  decode(input, length) {
@@ -452,6 +461,13 @@ exports.PutStreamEventRequest = {
452
461
  message.metadata = reader.string();
453
462
  continue;
454
463
  }
464
+ case 6: {
465
+ if (tag !== 48) {
466
+ break;
467
+ }
468
+ message.eventIndex = longToNumber(reader.int64());
469
+ continue;
470
+ }
455
471
  }
456
472
  if ((tag & 7) === 4 || tag === 0) {
457
473
  break;
@@ -466,6 +482,7 @@ exports.PutStreamEventRequest = {
466
482
  createdAt: isSet(object.createdAt) ? fromJsonTimestamp(object.createdAt) : undefined,
467
483
  message: isSet(object.message) ? bytesFromBase64(object.message) : new Uint8Array(0),
468
484
  metadata: isSet(object.metadata) ? globalThis.String(object.metadata) : '',
485
+ eventIndex: isSet(object.eventIndex) ? globalThis.Number(object.eventIndex) : undefined,
469
486
  };
470
487
  },
471
488
  toJSON(message) {
@@ -482,18 +499,22 @@ exports.PutStreamEventRequest = {
482
499
  if (message.metadata !== '') {
483
500
  obj.metadata = message.metadata;
484
501
  }
502
+ if (message.eventIndex !== undefined) {
503
+ obj.eventIndex = Math.round(message.eventIndex);
504
+ }
485
505
  return obj;
486
506
  },
487
507
  create(base) {
488
508
  return exports.PutStreamEventRequest.fromPartial(base !== null && base !== void 0 ? base : {});
489
509
  },
490
510
  fromPartial(object) {
491
- var _a, _b, _c, _d;
511
+ var _a, _b, _c, _d, _e;
492
512
  const message = createBasePutStreamEventRequest();
493
513
  message.stepRunId = (_a = object.stepRunId) !== null && _a !== void 0 ? _a : '';
494
514
  message.createdAt = (_b = object.createdAt) !== null && _b !== void 0 ? _b : undefined;
495
515
  message.message = (_c = object.message) !== null && _c !== void 0 ? _c : new Uint8Array(0);
496
516
  message.metadata = (_d = object.metadata) !== null && _d !== void 0 ? _d : '';
517
+ message.eventIndex = (_e = object.eventIndex) !== null && _e !== void 0 ? _e : undefined;
497
518
  return message;
498
519
  },
499
520
  };
@@ -875,6 +896,16 @@ function fromJsonTimestamp(o) {
875
896
  return fromTimestamp(timestamp_1.Timestamp.fromJSON(o));
876
897
  }
877
898
  }
899
+ function longToNumber(int64) {
900
+ const num = globalThis.Number(int64.toString());
901
+ if (num > globalThis.Number.MAX_SAFE_INTEGER) {
902
+ throw new globalThis.Error('Value is larger than Number.MAX_SAFE_INTEGER');
903
+ }
904
+ if (num < globalThis.Number.MIN_SAFE_INTEGER) {
905
+ throw new globalThis.Error('Value is smaller than Number.MIN_SAFE_INTEGER');
906
+ }
907
+ return num;
908
+ }
878
909
  function isSet(value) {
879
910
  return value !== null && value !== undefined;
880
911
  }
@@ -111,7 +111,7 @@ export interface DefaultFilter {
111
111
  /** (required) the scope of the filter */
112
112
  scope: string;
113
113
  /** (optional) the payload for the filter, if any. A JSON object as a string. */
114
- payload?: string | undefined;
114
+ payload?: Uint8Array | undefined;
115
115
  }
116
116
  export interface Concurrency {
117
117
  /** (required) the expression to use for concurrency */
@@ -1047,7 +1047,7 @@ exports.DefaultFilter = {
1047
1047
  writer.uint32(18).string(message.scope);
1048
1048
  }
1049
1049
  if (message.payload !== undefined) {
1050
- writer.uint32(26).string(message.payload);
1050
+ writer.uint32(26).bytes(message.payload);
1051
1051
  }
1052
1052
  return writer;
1053
1053
  },
@@ -1076,7 +1076,7 @@ exports.DefaultFilter = {
1076
1076
  if (tag !== 26) {
1077
1077
  break;
1078
1078
  }
1079
- message.payload = reader.string();
1079
+ message.payload = reader.bytes();
1080
1080
  continue;
1081
1081
  }
1082
1082
  }
@@ -1091,7 +1091,7 @@ exports.DefaultFilter = {
1091
1091
  return {
1092
1092
  expression: isSet(object.expression) ? globalThis.String(object.expression) : '',
1093
1093
  scope: isSet(object.scope) ? globalThis.String(object.scope) : '',
1094
- payload: isSet(object.payload) ? globalThis.String(object.payload) : undefined,
1094
+ payload: isSet(object.payload) ? bytesFromBase64(object.payload) : undefined,
1095
1095
  };
1096
1096
  },
1097
1097
  toJSON(message) {
@@ -1103,7 +1103,7 @@ exports.DefaultFilter = {
1103
1103
  obj.scope = message.scope;
1104
1104
  }
1105
1105
  if (message.payload !== undefined) {
1106
- obj.payload = message.payload;
1106
+ obj.payload = base64FromBytes(message.payload);
1107
1107
  }
1108
1108
  return obj;
1109
1109
  },
package/step.js CHANGED
@@ -345,7 +345,7 @@ class V0Context {
345
345
  this.logger.warn('cannot log from context without stepRunId');
346
346
  return;
347
347
  }
348
- yield this.v0.event.putStream(stepRunId, data);
348
+ yield this.v0.event.putStream(stepRunId, data, undefined);
349
349
  });
350
350
  }
351
351
  /**
@@ -4,6 +4,7 @@ import WorkflowRunRef from '../../util/workflow-run-ref';
4
4
  import { Workflow as V0Workflow } from '../../workflow';
5
5
  import { V0DurableContext } from '../../step';
6
6
  import { Api } from '../../clients/rest';
7
+ import { RunListenerClient } from '../../clients/listeners/run-listener/child-listener-client';
7
8
  import { CreateTaskWorkflowOpts, CreateWorkflowOpts, RunOpts, BaseWorkflowDeclaration, WorkflowDeclaration, TaskWorkflowDeclaration, CreateDurableTaskWorkflowOpts } from '../declaration';
8
9
  import { IHatchetClient } from './client.interface';
9
10
  import { CreateWorkerOpts, Worker } from './worker/worker';
@@ -26,6 +27,7 @@ export declare class HatchetClient implements IHatchetClient {
26
27
  /** The underlying v0 client instance */
27
28
  _v0: LegacyHatchetClient;
28
29
  _api: Api;
30
+ _listener: RunListenerClient;
29
31
  /**
30
32
  * @deprecated v0 client will be removed in a future release, please upgrade to v1
31
33
  */
@@ -16,12 +16,14 @@ exports.HatchetClient = void 0;
16
16
  /* eslint-disable no-dupe-class-members */
17
17
  /* eslint-disable no-underscore-dangle */
18
18
  const hatchet_client_1 = require("../../clients/hatchet-client");
19
- const workflow_run_ref_1 = __importDefault(require("../../util/workflow-run-ref"));
20
19
  const rest_1 = __importDefault(require("../../clients/rest"));
21
20
  const config_loader_1 = require("../../util/config-loader");
22
21
  const hatchet_logger_1 = require("../../clients/hatchet-client/hatchet-logger");
23
22
  const zod_1 = require("zod");
24
23
  const event_client_1 = require("../../clients/event/event-client");
24
+ const child_listener_client_1 = require("../../clients/listeners/run-listener/child-listener-client");
25
+ const grpc_helpers_1 = require("../../util/grpc-helpers");
26
+ const nice_grpc_1 = require("nice-grpc");
25
27
  const declaration_1 = require("../declaration");
26
28
  const worker_1 = require("./worker/worker");
27
29
  const metrics_1 = require("./features/metrics");
@@ -55,6 +57,7 @@ class HatchetClient {
55
57
  * @param axiosConfig - Optional Axios configuration for HTTP requests
56
58
  */
57
59
  constructor(config, options, axiosConfig) {
60
+ var _a;
58
61
  this._isV1 = true;
59
62
  try {
60
63
  const loaded = config_loader_1.ConfigLoader.loadClientConfig(config, {
@@ -69,7 +72,10 @@ class HatchetClient {
69
72
  this._config = clientConfig;
70
73
  this.tenantId = clientConfig.tenant_id;
71
74
  this._api = (0, rest_1.default)(clientConfig.api_url, clientConfig.token, axiosConfig);
72
- this._v0 = new hatchet_client_1.LegacyHatchetClient(clientConfig, options, axiosConfig, this.runs);
75
+ const clientFactory = (0, nice_grpc_1.createClientFactory)().use((0, grpc_helpers_1.addTokenMiddleware)(this.config.token));
76
+ const credentials = (_a = options === null || options === void 0 ? void 0 : options.credentials) !== null && _a !== void 0 ? _a : config_loader_1.ConfigLoader.createCredentials(this.config.tls_config);
77
+ this._listener = new child_listener_client_1.RunListenerClient(this.config, (0, grpc_helpers_1.channelFactory)(this.config, credentials), clientFactory, this.api);
78
+ this._v0 = new hatchet_client_1.LegacyHatchetClient(clientConfig, options, axiosConfig, this.runs, this._listener);
73
79
  }
74
80
  catch (e) {
75
81
  if (e instanceof zod_1.z.ZodError) {
@@ -361,7 +367,7 @@ class HatchetClient {
361
367
  return this._v0.webhooks(workflows);
362
368
  }
363
369
  runRef(id) {
364
- return new workflow_run_ref_1.default(id, this.v0.listener, this.runs);
370
+ return this.runs.runRef(id);
365
371
  }
366
372
  }
367
373
  exports.HatchetClient = HatchetClient;
@@ -13,7 +13,8 @@ export declare class FiltersClient {
13
13
  list(opts?: {
14
14
  limit?: number;
15
15
  offset?: number;
16
- workflowIdsAndScopes?: WorkflowIdScopePair[];
16
+ workflowIds?: string[];
17
+ scopes?: string[];
17
18
  }): Promise<import("../../../clients/rest/generated/data-contracts").V1FilterList>;
18
19
  get(filterId: Parameters<typeof this.api.v1FilterGet>[1]): Promise<import("../../../clients/rest/generated/data-contracts").V1Filter>;
19
20
  create(opts: Parameters<typeof this.api.v1FilterCreate>[1]): Promise<import("../../../clients/rest/generated/data-contracts").V1Filter>;
@@ -20,19 +20,11 @@ class FiltersClient {
20
20
  }
21
21
  list(opts) {
22
22
  return __awaiter(this, void 0, void 0, function* () {
23
- var _a, _b;
24
- const hasWorkflowIdsAndScopes = (opts === null || opts === void 0 ? void 0 : opts.workflowIdsAndScopes) !== undefined;
25
- const workflowIds = hasWorkflowIdsAndScopes
26
- ? (_a = opts.workflowIdsAndScopes) === null || _a === void 0 ? void 0 : _a.map((pair) => pair.workflowId)
27
- : undefined;
28
- const scopes = hasWorkflowIdsAndScopes
29
- ? (_b = opts.workflowIdsAndScopes) === null || _b === void 0 ? void 0 : _b.map((pair) => pair.scope)
30
- : undefined;
31
23
  const { data } = yield this.api.v1FilterList(this.tenantId, {
32
24
  limit: opts === null || opts === void 0 ? void 0 : opts.limit,
33
25
  offset: opts === null || opts === void 0 ? void 0 : opts.offset,
34
- workflowIds,
35
- scopes,
26
+ workflowIds: opts === null || opts === void 0 ? void 0 : opts.workflowIds,
27
+ scopes: opts === null || opts === void 0 ? void 0 : opts.scopes,
36
28
  });
37
29
  return data;
38
30
  });
@@ -1,5 +1,6 @@
1
1
  import WorkflowRunRef from '../../../util/workflow-run-ref';
2
2
  import { V1TaskStatus } from '../../../clients/rest/generated/data-contracts';
3
+ import { RunListenerClient } from '../../../clients/listeners/run-listener/child-listener-client';
3
4
  import { WorkflowsClient } from './workflows';
4
5
  import { HatchetClient } from '../client';
5
6
  export type RunFilter = {
@@ -60,6 +61,7 @@ export declare class RunsClient {
60
61
  api: HatchetClient['api'];
61
62
  tenantId: string;
62
63
  workflows: WorkflowsClient;
64
+ listener: RunListenerClient;
63
65
  constructor(client: HatchetClient);
64
66
  get<T = any>(run: string | WorkflowRunRef<T>): Promise<import("../../../clients/rest/generated/data-contracts").V1WorkflowRunDetails>;
65
67
  get_status<T = any>(run: string | WorkflowRunRef<T>): Promise<V1TaskStatus>;
@@ -68,4 +70,6 @@ export declare class RunsClient {
68
70
  replay(opts: ReplayRunOpts): Promise<import("axios").AxiosResponse<void, any>>;
69
71
  private prepareFilter;
70
72
  private prepareListFilter;
73
+ runRef<T extends Record<string, any> = any>(id: string): WorkflowRunRef<T>;
74
+ subscribeToStream(workflowRunId: string): AsyncIterableIterator<string>;
71
75
  }
@@ -8,8 +8,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
12
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
13
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
14
+ var m = o[Symbol.asyncIterator], i;
15
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
16
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
17
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
18
+ };
19
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
20
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
21
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
22
+ return i = Object.create((typeof AsyncIterator === "function" ? AsyncIterator : Object).prototype), verb("next"), verb("throw"), verb("return", awaitReturn), i[Symbol.asyncIterator] = function () { return this; }, i;
23
+ function awaitReturn(f) { return function (v) { return Promise.resolve(v).then(f, reject); }; }
24
+ function verb(n, f) { if (g[n]) { i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; if (f) i[n] = f(i[n]); } }
25
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
26
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
27
+ function fulfill(value) { resume("next", value); }
28
+ function reject(value) { resume("throw", value); }
29
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
30
+ };
31
+ var __importDefault = (this && this.__importDefault) || function (mod) {
32
+ return (mod && mod.__esModule) ? mod : { "default": mod };
33
+ };
11
34
  Object.defineProperty(exports, "__esModule", { value: true });
12
35
  exports.RunsClient = void 0;
36
+ const workflow_run_ref_1 = __importDefault(require("../../../util/workflow-run-ref"));
37
+ const child_listener_client_1 = require("../../../clients/listeners/run-listener/child-listener-client");
13
38
  /**
14
39
  * RunsClient is used to list and manage runs
15
40
  */
@@ -18,6 +43,8 @@ class RunsClient {
18
43
  this.api = client.api;
19
44
  this.tenantId = client.tenantId;
20
45
  this.workflows = client.workflows;
46
+ // eslint-disable-next-line no-underscore-dangle
47
+ this.listener = client._listener;
21
48
  }
22
49
  get(run) {
23
50
  return __awaiter(this, void 0, void 0, function* () {
@@ -89,5 +116,32 @@ class RunsClient {
89
116
  };
90
117
  });
91
118
  }
119
+ runRef(id) {
120
+ return new workflow_run_ref_1.default(id, this.listener, this);
121
+ }
122
+ subscribeToStream(workflowRunId) {
123
+ return __asyncGenerator(this, arguments, function* subscribeToStream_1() {
124
+ var _a, e_1, _b, _c;
125
+ const ref = this.runRef(workflowRunId);
126
+ const stream = yield __await(ref.stream());
127
+ try {
128
+ for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield __await(stream_1.next()), _a = stream_1_1.done, !_a; _d = true) {
129
+ _c = stream_1_1.value;
130
+ _d = false;
131
+ const event = _c;
132
+ if (event.type === child_listener_client_1.RunEventType.STEP_RUN_EVENT_TYPE_STREAM) {
133
+ yield yield __await(event.payload);
134
+ }
135
+ }
136
+ }
137
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
138
+ finally {
139
+ try {
140
+ if (!_d && !_a && (_b = stream_1.return)) yield __await(_b.call(stream_1));
141
+ }
142
+ finally { if (e_1) throw e_1.error; }
143
+ }
144
+ });
145
+ }
92
146
  }
93
147
  exports.RunsClient = RunsClient;
@@ -38,6 +38,7 @@ export declare class Context<T, K = {}> {
38
38
  overridesData: Record<string, any>;
39
39
  _logger: Logger;
40
40
  spawnIndex: number;
41
+ streamIndex: number;
41
42
  constructor(action: Action, v1: HatchetClient, worker: V1Worker);
42
43
  get abortController(): AbortController;
43
44
  get cancelled(): boolean;
@@ -253,6 +254,7 @@ export declare class Context<T, K = {}> {
253
254
  * @deprecated Use runChild or runNoWaitChild instead.
254
255
  */
255
256
  spawnWorkflow<Q extends JsonObject, P extends JsonObject>(workflow: string | Workflow | WorkflowV1<Q, P> | TaskWorkflowDeclaration<Q, P>, input: Q, options?: ChildRunOpts): Promise<WorkflowRunRef<P>>;
257
+ _incrementStreamIndex(): number;
256
258
  }
257
259
  export declare class DurableContext<T, K = {}> extends Context<T, K> {
258
260
  waitKey: number;
@@ -29,6 +29,7 @@ class Context {
29
29
  this.controller = new AbortController();
30
30
  this.overridesData = {};
31
31
  this.spawnIndex = 0;
32
+ this.streamIndex = 0;
32
33
  try {
33
34
  const data = (0, parse_1.parseJSON)(action.actionPayload);
34
35
  this.data = data;
@@ -282,7 +283,8 @@ class Context {
282
283
  this._logger.warn('cannot log from context without stepRunId');
283
284
  return;
284
285
  }
285
- yield this.v1._v0.event.putStream(stepRunId, data);
286
+ const index = this._incrementStreamIndex();
287
+ yield this.v1._v0.event.putStream(stepRunId, data, index);
286
288
  });
287
289
  }
288
290
  spawnOptions(workflow, options) {
@@ -535,6 +537,11 @@ class Context {
535
537
  }
536
538
  });
537
539
  }
540
+ _incrementStreamIndex() {
541
+ const index = this.streamIndex;
542
+ this.streamIndex += 1;
543
+ return index;
544
+ }
538
545
  }
539
546
  exports.Context = Context;
540
547
  class DurableContext extends Context {
@@ -244,7 +244,7 @@ class V1Worker {
244
244
  defaultFilters: (_x = (_w = workflow.defaultFilters) === null || _w === void 0 ? void 0 : _w.map((f) => ({
245
245
  scope: f.scope,
246
246
  expression: f.expression,
247
- payload: f.payload ? JSON.stringify(f.payload) : undefined,
247
+ payload: f.payload ? new TextEncoder().encode(JSON.stringify(f.payload)) : undefined,
248
248
  }))) !== null && _x !== void 0 ? _x : [],
249
249
  });
250
250
  this.registeredWorkflowPromises.push(registeredWorkflow);
@@ -0,0 +1 @@
1
+ export declare function GET(): Promise<Response>;
@@ -0,0 +1,37 @@
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.GET = GET;
13
+ const stream_1 = require("stream");
14
+ const hatchet_client_1 = require("../hatchet-client");
15
+ const workflow_1 = require("./workflow");
16
+ // > NextJS Proxy
17
+ function GET() {
18
+ return __awaiter(this, void 0, void 0, function* () {
19
+ try {
20
+ const ref = yield workflow_1.streamingTask.runNoWait({});
21
+ const workflowRunId = yield ref.getWorkflowRunId();
22
+ const stream = stream_1.Readable.from(hatchet_client_1.hatchet.runs.subscribeToStream(workflowRunId));
23
+ // @ts-ignore
24
+ return new Response(stream_1.Readable.toWeb(stream), {
25
+ headers: {
26
+ 'Content-Type': 'text/plain',
27
+ 'Cache-Control': 'no-cache',
28
+ Connection: 'keep-alive',
29
+ },
30
+ });
31
+ }
32
+ catch (error) {
33
+ return new Response('Internal Server Error', { status: 500 });
34
+ }
35
+ });
36
+ }
37
+ // !!
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,52 @@
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
+ var __asyncValues = (this && this.__asyncValues) || function (o) {
12
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
+ var m = o[Symbol.asyncIterator], i;
14
+ return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
15
+ function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
+ function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
+ };
18
+ Object.defineProperty(exports, "__esModule", { value: true });
19
+ /* eslint-disable no-console */
20
+ const workflow_1 = require("./workflow");
21
+ const hatchet_client_1 = require("../hatchet-client");
22
+ function main() {
23
+ return __awaiter(this, void 0, void 0, function* () {
24
+ var _a, e_1, _b, _c;
25
+ // > Consume
26
+ const ref = yield workflow_1.streamingTask.runNoWait({});
27
+ const id = yield ref.getWorkflowRunId();
28
+ try {
29
+ for (var _d = true, _e = __asyncValues(hatchet_client_1.hatchet.runs.subscribeToStream(id)), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
30
+ _c = _f.value;
31
+ _d = false;
32
+ const content = _c;
33
+ process.stdout.write(content);
34
+ }
35
+ }
36
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
37
+ finally {
38
+ try {
39
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
40
+ }
41
+ finally { if (e_1) throw e_1.error; }
42
+ }
43
+ // !!
44
+ });
45
+ }
46
+ if (require.main === module) {
47
+ main()
48
+ .catch(console.error)
49
+ .finally(() => {
50
+ process.exit(0);
51
+ });
52
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,24 @@
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
+ const hatchet_client_1 = require("../hatchet-client");
13
+ const workflow_1 = require("./workflow");
14
+ function main() {
15
+ return __awaiter(this, void 0, void 0, function* () {
16
+ const worker = yield hatchet_client_1.hatchet.worker('streaming-worker', {
17
+ workflows: [workflow_1.streamingTask],
18
+ });
19
+ yield worker.start();
20
+ });
21
+ }
22
+ if (require.main === module) {
23
+ main();
24
+ }
@@ -0,0 +1 @@
1
+ export declare const streamingTask: import("../..").TaskWorkflowDeclaration<import("../..").UnknownInputType, void>;
@@ -0,0 +1,39 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.streamingTask = void 0;
16
+ const sleep_1 = __importDefault(require("../../../util/sleep"));
17
+ const hatchet_client_1 = require("../hatchet-client");
18
+ // > Streaming
19
+ const annaKarenina = `
20
+ Happy families are all alike; every unhappy family is unhappy in its own way.
21
+
22
+ Everything was in confusion in the Oblonskys' house. The wife had discovered that the husband was carrying on an intrigue with a French girl, who had been a governess in their family, and she had announced to her husband that she could not go on living in the same house with him.
23
+ `;
24
+ function* createChunks(content, n) {
25
+ for (let i = 0; i < content.length; i += n) {
26
+ yield content.slice(i, i + n);
27
+ }
28
+ }
29
+ exports.streamingTask = hatchet_client_1.hatchet.task({
30
+ name: 'stream-example',
31
+ fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
32
+ yield (0, sleep_1.default)(2000);
33
+ for (const chunk of createChunks(annaKarenina, 10)) {
34
+ ctx.putStream(chunk);
35
+ yield (0, sleep_1.default)(200);
36
+ }
37
+ }),
38
+ });
39
+ // !!
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.8.2";
1
+ export declare const HATCHET_VERSION = "1.9.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.8.2';
4
+ exports.HATCHET_VERSION = '1.9.1';