@temporal-contract/worker 1.0.0 → 2.1.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.
@@ -1,47 +1,7 @@
1
+ let _temporal_contract_contract = require("@temporal-contract/contract");
1
2
  let _temporalio_workflow = require("@temporalio/workflow");
2
- //#region src/format.ts
3
- /**
4
- * Pattern for string keys safe to render with dot notation. A "safe" key is a
5
- * JavaScript identifier (letters/digits/underscore/$, not starting with a
6
- * digit). Anything else — keys containing dots, spaces, leading digits, the
7
- * empty string, the literal string `"0"` etc. — gets bracket-quoted so the
8
- * path is unambiguous. Reserved words are accepted: we are formatting a
9
- * diagnostic, not generating runnable code.
10
- */
11
- const SAFE_IDENTIFIER = /^[A-Za-z_$][A-Za-z0-9_$]*$/;
12
- /**
13
- * Render a Standard Schema {@link StandardSchemaV1.Issue} into a human-readable
14
- * string that includes the failing field's path.
15
- */
16
- function formatIssue(issue) {
17
- if (issue.path === void 0 || issue.path.length === 0) return issue.message;
18
- let path = "";
19
- for (let i = 0; i < issue.path.length; i++) {
20
- const segment = issue.path[i];
21
- const key = segment !== null && typeof segment === "object" && "key" in segment ? segment.key : segment;
22
- if (typeof key === "number") path += `[${key}]`;
23
- else if (typeof key === "string" && SAFE_IDENTIFIER.test(key)) path += i === 0 ? key : `.${key}`;
24
- else if (typeof key === "string") path += `[${JSON.stringify(key)}]`;
25
- else path += `[${String(key)}]`;
26
- }
27
- return `at ${path}: ${issue.message}`;
28
- }
29
- /**
30
- * Join a list of validation issues into a single message, with each issue
31
- * rendered via {@link formatIssue} so field paths surface in the error text.
32
- */
33
- function summarizeIssues(issues) {
34
- return issues.map(formatIssue).join("; ");
35
- }
36
- /**
37
- * Build the message attached to a `ChildWorkflowError` for input/output
38
- * validation failures. Centralized so the worker and any future call sites
39
- * format identically.
40
- */
41
- function formatChildWorkflowValidationMessage(workflowName, direction, issues) {
42
- return `Child workflow "${workflowName}" ${direction} validation failed: ${summarizeIssues(issues)}`;
43
- }
44
- //#endregion
3
+ let _temporalio_common = require("@temporalio/common");
4
+ require("@temporal-contract/contract/result-async");
45
5
  //#region src/errors.ts
46
6
  /**
47
7
  * Base error class for worker errors
@@ -70,7 +30,7 @@ var ActivityDefinitionNotFoundError = class extends WorkerError {
70
30
  */
71
31
  var ActivityInputValidationError = class extends WorkerError {
72
32
  constructor(activityName, issues) {
73
- const message = summarizeIssues(issues);
33
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
74
34
  super(`Activity "${activityName}" input validation failed: ${message}`);
75
35
  this.activityName = activityName;
76
36
  this.issues = issues;
@@ -82,7 +42,7 @@ var ActivityInputValidationError = class extends WorkerError {
82
42
  */
83
43
  var ActivityOutputValidationError = class extends WorkerError {
84
44
  constructor(activityName, issues) {
85
- const message = summarizeIssues(issues);
45
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
86
46
  super(`Activity "${activityName}" output validation failed: ${message}`);
87
47
  this.activityName = activityName;
88
48
  this.issues = issues;
@@ -94,7 +54,7 @@ var ActivityOutputValidationError = class extends WorkerError {
94
54
  */
95
55
  var WorkflowInputValidationError = class extends WorkerError {
96
56
  constructor(workflowName, issues) {
97
- const message = summarizeIssues(issues);
57
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
98
58
  super(`Workflow "${workflowName}" input validation failed: ${message}`);
99
59
  this.workflowName = workflowName;
100
60
  this.issues = issues;
@@ -106,7 +66,7 @@ var WorkflowInputValidationError = class extends WorkerError {
106
66
  */
107
67
  var WorkflowOutputValidationError = class extends WorkerError {
108
68
  constructor(workflowName, issues) {
109
- const message = summarizeIssues(issues);
69
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
110
70
  super(`Workflow "${workflowName}" output validation failed: ${message}`);
111
71
  this.workflowName = workflowName;
112
72
  this.issues = issues;
@@ -118,7 +78,7 @@ var WorkflowOutputValidationError = class extends WorkerError {
118
78
  */
119
79
  var SignalInputValidationError = class extends WorkerError {
120
80
  constructor(signalName, issues) {
121
- const message = summarizeIssues(issues);
81
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
122
82
  super(`Signal "${signalName}" input validation failed: ${message}`);
123
83
  this.signalName = signalName;
124
84
  this.issues = issues;
@@ -130,7 +90,7 @@ var SignalInputValidationError = class extends WorkerError {
130
90
  */
131
91
  var QueryInputValidationError = class extends WorkerError {
132
92
  constructor(queryName, issues) {
133
- const message = summarizeIssues(issues);
93
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
134
94
  super(`Query "${queryName}" input validation failed: ${message}`);
135
95
  this.queryName = queryName;
136
96
  this.issues = issues;
@@ -142,7 +102,7 @@ var QueryInputValidationError = class extends WorkerError {
142
102
  */
143
103
  var QueryOutputValidationError = class extends WorkerError {
144
104
  constructor(queryName, issues) {
145
- const message = summarizeIssues(issues);
105
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
146
106
  super(`Query "${queryName}" output validation failed: ${message}`);
147
107
  this.queryName = queryName;
148
108
  this.issues = issues;
@@ -154,7 +114,7 @@ var QueryOutputValidationError = class extends WorkerError {
154
114
  */
155
115
  var UpdateInputValidationError = class extends WorkerError {
156
116
  constructor(updateName, issues) {
157
- const message = summarizeIssues(issues);
117
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
158
118
  super(`Update "${updateName}" input validation failed: ${message}`);
159
119
  this.updateName = updateName;
160
120
  this.issues = issues;
@@ -166,7 +126,7 @@ var UpdateInputValidationError = class extends WorkerError {
166
126
  */
167
127
  var UpdateOutputValidationError = class extends WorkerError {
168
128
  constructor(updateName, issues) {
169
- const message = summarizeIssues(issues);
129
+ const message = (0, _temporal_contract_contract.summarizeIssues)(issues);
170
130
  super(`Update "${updateName}" output validation failed: ${message}`);
171
131
  this.updateName = updateName;
172
132
  this.issues = issues;
@@ -186,7 +146,13 @@ var ChildWorkflowNotFoundError = class extends WorkerError {
186
146
  }
187
147
  };
188
148
  /**
189
- * Generic error for child workflow operations
149
+ * Generic error for child workflow operations.
150
+ *
151
+ * When the child execution itself fails (Temporal's `ChildWorkflowFailure`),
152
+ * `cause` is set to the *unwrapped* underlying failure (`ApplicationFailure`,
153
+ * `TimeoutFailure`, `TerminatedFailure`, etc.) lifted from Temporal's wrapper —
154
+ * mirroring the client-side `WorkflowFailedError.cause` behavior, so callers
155
+ * can branch on the failure category in one step instead of unwrapping twice.
190
156
  */
191
157
  var ChildWorkflowError = class extends WorkerError {
192
158
  constructor(message, cause) {
@@ -195,13 +161,37 @@ var ChildWorkflowError = class extends WorkerError {
195
161
  }
196
162
  };
197
163
  /**
198
- * Error returned in the `Result.Error` branch when a typed cancellation
199
- * scope is cancelled via Temporal's cancellation propagation. Returned by
200
- * both `context.cancellableScope` (when the workflow or an ancestor scope
201
- * cancels) and `context.nonCancellableScope` (when cancellation is raised
202
- * from inside the scope). Distinct from arbitrary thrown errors so call
203
- * sites can branch on cancellation explicitly while still surfacing
204
- * non-cancellation errors as Future rejections.
164
+ * Discriminated variant of {@link ChildWorkflowError} surfaced when a child
165
+ * workflow operation (start, execute, or wait-for-result) was cancelled
166
+ * either because the parent workflow itself was cancelled, the child was
167
+ * explicitly cancelled, or its enclosing cancellation scope was. Detected via
168
+ * `@temporalio/workflow`'s `isCancellation(...)`, which sees through nested
169
+ * `ChildWorkflowFailure` / `CancelledFailure` chains.
170
+ *
171
+ * Extends {@link ChildWorkflowError} so existing `instanceof ChildWorkflowError`
172
+ * checks still match cancellation, while `instanceof ChildWorkflowCancelledError`
173
+ * lets call sites narrow further when they need to branch on cancellation
174
+ * explicitly without inspecting `error.cause` against a Temporal SDK class —
175
+ * the worker-side analogue of the client-side cause-forwarding pattern.
176
+ */
177
+ var ChildWorkflowCancelledError = class extends ChildWorkflowError {
178
+ constructor(workflowName, cause) {
179
+ super(`Child workflow "${workflowName}" was cancelled`, cause);
180
+ this.workflowName = workflowName;
181
+ this.name = "ChildWorkflowCancelledError";
182
+ }
183
+ };
184
+ /**
185
+ * Error surfaced in the `err(...)` branch of a `ResultAsync` when a typed
186
+ * cancellation scope is cancelled via Temporal's cancellation propagation.
187
+ * Returned by both `context.cancellableScope` (when the workflow or an
188
+ * ancestor scope cancels) and `context.nonCancellableScope` (when
189
+ * cancellation is raised from inside the scope). Distinct from arbitrary
190
+ * thrown errors so call sites can branch on cancellation explicitly.
191
+ *
192
+ * Non-cancellation errors thrown inside a scope surface as a sibling
193
+ * {@link WorkflowScopeError} on the same `err(...)` channel, so callers can
194
+ * use `instanceof` to discriminate without falling back to `try/catch`.
205
195
  */
206
196
  var WorkflowCancelledError = class extends WorkerError {
207
197
  constructor(cause) {
@@ -209,6 +199,42 @@ var WorkflowCancelledError = class extends WorkerError {
209
199
  this.name = "WorkflowCancelledError";
210
200
  }
211
201
  };
202
+ /**
203
+ * Error surfaced in the `err(...)` branch of a `ResultAsync` when the
204
+ * function passed to `cancellableScope` / `nonCancellableScope` throws a
205
+ * non-cancellation error.
206
+ *
207
+ * The original error is preserved on `cause` so call sites can introspect
208
+ * it without losing identity:
209
+ *
210
+ * @example
211
+ * ```ts
212
+ * const result = await context.cancellableScope(async () => {
213
+ * return await context.activities.processStep(args);
214
+ * });
215
+ *
216
+ * if (result.isErr()) {
217
+ * if (result.error instanceof WorkflowCancelledError) {
218
+ * // graceful cancellation
219
+ * } else if (result.error instanceof WorkflowScopeError) {
220
+ * // domain error — `result.error.cause` is the original throwable
221
+ * }
222
+ * }
223
+ * ```
224
+ *
225
+ * Introduced so the scope helpers route every failure through neverthrow's
226
+ * railway. Previously, non-cancellation errors were re-thrown out of the
227
+ * helper, which became a `ResultAsync` rejection (`new ResultAsync(promise)`
228
+ * does not catch) — they leaked as unhandled rejections rather than
229
+ * surfacing on the typed error channel callers actually inspect.
230
+ */
231
+ var WorkflowScopeError = class extends WorkerError {
232
+ constructor(cause) {
233
+ const message = cause instanceof Error ? `Workflow cancellation scope caught a non-cancellation error: ${cause.message}` : "Workflow cancellation scope caught a non-cancellation error";
234
+ super(message, cause);
235
+ this.name = "WorkflowScopeError";
236
+ }
237
+ };
212
238
  //#endregion
213
239
  //#region src/internal.ts
214
240
  /**
@@ -219,6 +245,15 @@ var WorkflowCancelledError = class extends WorkerError {
219
245
  * In-package tests import it directly via relative path.
220
246
  */
221
247
  /**
248
+ * Build the message attached to a `ChildWorkflowError` for input/output
249
+ * validation failures. Centralized so the worker formats child-workflow
250
+ * validation diagnostics identically across call sites. Composes the shared
251
+ * `summarizeIssues` from `@temporal-contract/contract`.
252
+ */
253
+ function formatChildWorkflowValidationMessage(workflowName, direction, issues) {
254
+ return `Child workflow "${workflowName}" ${direction} validation failed: ${(0, _temporal_contract_contract.summarizeIssues)(issues)}`;
255
+ }
256
+ /**
222
257
  * Extract the single payload from a Temporal handler's `...args` array.
223
258
  *
224
259
  * Temporal invokes handlers with whatever was passed via `args: [...]` at the
@@ -348,6 +383,49 @@ function looksLikeCrossContractCall(arg1, arg2) {
348
383
  const workflows = candidate["workflows"];
349
384
  return typeof workflows === "object" && workflows !== null;
350
385
  }
386
+ /**
387
+ * Map a thrown error from `startChild` / `executeChild` / `handle.result()`
388
+ * (the worker-side child-workflow API) into the discriminated union surfaced
389
+ * by the typed worker. Mirrors the client's `classifyResultError`:
390
+ *
391
+ * - Cancellation (detected via `@temporalio/workflow`'s `isCancellation`,
392
+ * which sees through nested `ChildWorkflowFailure → CancelledFailure`
393
+ * chains) → {@link ChildWorkflowCancelledError}, with the original error
394
+ * carried as `cause`.
395
+ * - Temporal's `ChildWorkflowFailure` (a wrapper whose actionable failure —
396
+ * `ApplicationFailure`, `TimeoutFailure`, `TerminatedFailure`, etc. — lives
397
+ * on its `cause` field) → {@link ChildWorkflowError}, with that *inner*
398
+ * cause forwarded so consumers can match `err.cause instanceof
399
+ * ApplicationFailure` without unwrapping twice. (If the wrapper's `cause`
400
+ * is `undefined`, the wrapper itself is forwarded so identity is
401
+ * preserved.)
402
+ * - Anything else → {@link ChildWorkflowError} carrying the raw thrown value
403
+ * as `cause`.
404
+ *
405
+ * The `operation` discriminator drives the human-readable error message so
406
+ * call sites don't have to format their own.
407
+ *
408
+ * Note: `ChildWorkflowNotFoundError` is *not* produced here — it's only
409
+ * thrown from the input-validation path when the workflow definition is
410
+ * missing on the contract, before any Temporal call happens.
411
+ */
412
+ function classifyChildWorkflowError(operation, error, childWorkflowName) {
413
+ if ((0, _temporalio_workflow.isCancellation)(error)) return new ChildWorkflowCancelledError(childWorkflowName, error);
414
+ if (error instanceof _temporalio_common.ChildWorkflowFailure) {
415
+ const inner = error.cause ?? error;
416
+ const innerMessage = inner instanceof Error ? inner.message : String(inner);
417
+ return new ChildWorkflowError(`${describeChildWorkflowOperation(operation, childWorkflowName)}: ${innerMessage}`, inner);
418
+ }
419
+ const message = error instanceof Error ? error.message : String(error);
420
+ return new ChildWorkflowError(`${describeChildWorkflowOperation(operation, childWorkflowName)}: ${message}`, error);
421
+ }
422
+ function describeChildWorkflowOperation(operation, childWorkflowName) {
423
+ switch (operation) {
424
+ case "startChild": return `Failed to start child workflow "${childWorkflowName}"`;
425
+ case "executeChild": return `Failed to execute child workflow "${childWorkflowName}"`;
426
+ case "result": return `Child workflow "${childWorkflowName}" execution failed`;
427
+ }
428
+ }
351
429
  //#endregion
352
430
  Object.defineProperty(exports, "ActivityDefinitionNotFoundError", {
353
431
  enumerable: true,
@@ -367,6 +445,12 @@ Object.defineProperty(exports, "ActivityOutputValidationError", {
367
445
  return ActivityOutputValidationError;
368
446
  }
369
447
  });
448
+ Object.defineProperty(exports, "ChildWorkflowCancelledError", {
449
+ enumerable: true,
450
+ get: function() {
451
+ return ChildWorkflowCancelledError;
452
+ }
453
+ });
370
454
  Object.defineProperty(exports, "ChildWorkflowError", {
371
455
  enumerable: true,
372
456
  get: function() {
@@ -427,12 +511,24 @@ Object.defineProperty(exports, "WorkflowOutputValidationError", {
427
511
  return WorkflowOutputValidationError;
428
512
  }
429
513
  });
514
+ Object.defineProperty(exports, "WorkflowScopeError", {
515
+ enumerable: true,
516
+ get: function() {
517
+ return WorkflowScopeError;
518
+ }
519
+ });
430
520
  Object.defineProperty(exports, "buildRawActivitiesProxy", {
431
521
  enumerable: true,
432
522
  get: function() {
433
523
  return buildRawActivitiesProxy;
434
524
  }
435
525
  });
526
+ Object.defineProperty(exports, "classifyChildWorkflowError", {
527
+ enumerable: true,
528
+ get: function() {
529
+ return classifyChildWorkflowError;
530
+ }
531
+ });
436
532
  Object.defineProperty(exports, "createContinueAsNew", {
437
533
  enumerable: true,
438
534
  get: function() {
package/dist/worker.d.mts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { ActivitiesHandler } from "./activity.mjs";
2
- import { Worker, WorkerOptions } from "@temporalio/worker";
3
2
  import { ContractDefinition } from "@temporal-contract/contract";
3
+ import { Worker, WorkerOptions } from "@temporalio/worker";
4
4
 
5
5
  //#region src/worker.d.ts
6
6
  /**