@hatchet-dev/typescript-sdk 1.9.0 → 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.
@@ -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 }; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hatchet-dev/typescript-sdk",
3
- "version": "1.9.0",
3
+ "version": "1.9.1",
4
4
  "description": "Background task orchestration & visibility for developers",
5
5
  "types": "dist/index.d.ts",
6
6
  "files": [
@@ -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;
@@ -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;
@@ -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
+ // !!
@@ -17,28 +17,26 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
17
17
  };
18
18
  Object.defineProperty(exports, "__esModule", { value: true });
19
19
  /* eslint-disable no-console */
20
- const child_listener_client_1 = require("../../../clients/listeners/run-listener/child-listener-client");
21
20
  const workflow_1 = require("./workflow");
21
+ const hatchet_client_1 = require("../hatchet-client");
22
22
  function main() {
23
23
  return __awaiter(this, void 0, void 0, function* () {
24
24
  var _a, e_1, _b, _c;
25
25
  // > Consume
26
26
  const ref = yield workflow_1.streamingTask.runNoWait({});
27
- const stream = yield ref.stream();
27
+ const id = yield ref.getWorkflowRunId();
28
28
  try {
29
- for (var _d = true, stream_1 = __asyncValues(stream), stream_1_1; stream_1_1 = yield stream_1.next(), _a = stream_1_1.done, !_a; _d = true) {
30
- _c = stream_1_1.value;
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
31
  _d = false;
32
- const event = _c;
33
- if (event.type === child_listener_client_1.RunEventType.STEP_RUN_EVENT_TYPE_STREAM) {
34
- process.stdout.write(event.payload);
35
- }
32
+ const content = _c;
33
+ process.stdout.write(content);
36
34
  }
37
35
  }
38
36
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
39
37
  finally {
40
38
  try {
41
- if (!_d && !_a && (_b = stream_1.return)) yield _b.call(stream_1);
39
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
42
40
  }
43
41
  finally { if (e_1) throw e_1.error; }
44
42
  }
@@ -29,6 +29,7 @@ function* createChunks(content, n) {
29
29
  exports.streamingTask = hatchet_client_1.hatchet.task({
30
30
  name: 'stream-example',
31
31
  fn: (_, ctx) => __awaiter(void 0, void 0, void 0, function* () {
32
+ yield (0, sleep_1.default)(2000);
32
33
  for (const chunk of createChunks(annaKarenina, 10)) {
33
34
  ctx.putStream(chunk);
34
35
  yield (0, sleep_1.default)(200);
package/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const HATCHET_VERSION = "1.9.0";
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.9.0';
4
+ exports.HATCHET_VERSION = '1.9.1';