@diagonal-hq/workflows 0.6.0 → 0.7.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.
package/README.md CHANGED
@@ -76,7 +76,7 @@ Imported from this package, `runWorkflow` runs the handler in Node. A deployed s
76
76
 
77
77
  - It always runs the `validate` option of the workflow config, as a deployed run does. It runs a `validateInput` export only when the test passes that export as the `validateInput` option. Another validator, or a `validateInput` option for a workflow with a `validate` option, is refused.
78
78
  - Inputs, context, mock replies, and the result cross the engine boundary as JSON. A handler that returns `undefined` gives `null`, and a `Date` becomes a string.
79
- - A `WorkflowFailure` or `HostError` from the script is rethrown as that class. A mock that throws another error reaches the script as a `HostError` with the code `internal_error`.
79
+ - A `WorkflowFailure` or `HostError` from the script is rethrown as that class. A mock that throws another error reaches the script as a `HostError` with the code `internal_error` and `failure: null`. To test a script that reads `failure`, make the mock throw a `HostError` with the `failure` argument.
80
80
  - A workflow that the test defines itself has no compiled code, so it runs in Node.
81
81
 
82
82
  ### QuickJS limits
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  validationError
3
- } from "./chunk-OF5QITP7.js";
3
+ } from "./chunk-U2PBCCHZ.js";
4
4
 
5
5
  // packages/workflows/src/transport.ts
6
6
  var active = null;
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  invoke
3
- } from "./chunk-EU2ME5WR.js";
3
+ } from "./chunk-AQGV2DYG.js";
4
4
  import {
5
5
  HostError
6
- } from "./chunk-OF5QITP7.js";
6
+ } from "./chunk-U2PBCCHZ.js";
7
7
 
8
8
  // packages/workflows/src/transaction.ts
9
9
  async function transaction(fn) {
@@ -13,10 +13,11 @@ async function transaction(fn) {
13
13
  try {
14
14
  result = await fn();
15
15
  } catch (error) {
16
- if (await rollBack()) {
16
+ const rolledBack = await rollBack({ error: thrownError(error) });
17
+ if (rolledBack.retry) {
17
18
  continue;
18
19
  }
19
- throw error;
20
+ throw rollbackError(rolledBack.reply, error) ?? error;
20
21
  }
21
22
  try {
22
23
  await invoke("transaction.commit", {});
@@ -25,30 +26,78 @@ async function transaction(fn) {
25
26
  continue;
26
27
  }
27
28
  if (error instanceof HostError && error.code === "transaction_concurrent") {
28
- await rollBack();
29
+ await rollBack({});
29
30
  }
30
31
  throw error;
31
32
  }
32
33
  return result;
33
34
  }
34
35
  }
35
- async function rollBack() {
36
+ async function rollBack(input) {
36
37
  try {
37
- await invoke("transaction.rollback", {});
38
+ return { retry: false, reply: await invoke("transaction.rollback", input) };
38
39
  } catch (error) {
39
40
  if (isTransient(error)) {
40
- return true;
41
+ return { retry: true };
41
42
  }
42
43
  throw error;
43
44
  }
44
- return false;
45
45
  }
46
46
  function isTransient(error) {
47
47
  return error instanceof HostError && error.code === "transaction_transient";
48
48
  }
49
+ function thrownError(error) {
50
+ if (typeof error !== "object" || error === null) {
51
+ const value2 = typeof error === "bigint" ? String(error) : JSON.stringify(error);
52
+ return { name: "Error", message: `The workflow threw a non-error value: ${value2}` };
53
+ }
54
+ const name = Reflect.get(error, "name");
55
+ const failureId = Reflect.get(error, "failureId");
56
+ if (name === "HostError" && typeof failureId === "number" && Number.isInteger(failureId)) {
57
+ return { name, failure_id: failureId };
58
+ }
59
+ const message = Reflect.get(error, "message");
60
+ const code = Reflect.get(error, "code");
61
+ const status = Reflect.get(error, "status");
62
+ const details = Reflect.get(error, "details");
63
+ const errors = typeof details === "object" && details !== null ? Reflect.get(details, "errors") : void 0;
64
+ const thrown = {
65
+ name: typeof name === "string" ? name : "Error",
66
+ message: String(message ?? "The workflow threw an error.")
67
+ };
68
+ if (typeof code === "string") {
69
+ thrown.code = code;
70
+ }
71
+ if (typeof status === "number" && Number.isInteger(status)) {
72
+ thrown.status = status;
73
+ }
74
+ if (typeof errors === "object" && errors !== null && !Array.isArray(errors)) {
75
+ thrown.errors = { ...errors };
76
+ }
77
+ return thrown;
78
+ }
79
+ function rollbackError(reply, cause) {
80
+ if (typeof reply !== "object" || reply === null) {
81
+ return null;
82
+ }
83
+ const message = Reflect.get(reply, "message");
84
+ const code = Reflect.get(reply, "code");
85
+ const status = Reflect.get(reply, "status");
86
+ const failureId = Reflect.get(reply, "failure_id");
87
+ const failure = Reflect.get(reply, "failure");
88
+ if (typeof message !== "string" || code !== "transaction_rollback" || typeof status !== "number" || typeof failureId !== "number" || !isFailure(failure)) {
89
+ return null;
90
+ }
91
+ return new HostError(message, code, status, Reflect.get(reply, "details"), failureId, failure, {
92
+ cause
93
+ });
94
+ }
95
+ function isFailure(value2) {
96
+ return typeof value2 === "object" && value2 !== null && !Array.isArray(value2);
97
+ }
49
98
 
50
99
  // packages/workflows/src/generated/capabilities.ts
51
- var SDK_VERSION = 68;
100
+ var SDK_VERSION = 70;
52
101
  var ai = {
53
102
  /** Ask a decisions model typed questions about some content and get labelled answers with their probabilities */
54
103
  decide: (input) => invoke("ai.decide", input),
@@ -3,19 +3,25 @@ var HostError = class extends Error {
3
3
  /**
4
4
  * @param failureId The host's ID for the failure of the call. When the error ends the run, the
5
5
  * host records that failure, so a run records the failure of the call that it rethrows.
6
+ * @param failure The failure that a graph step records for the same error, or null for a
7
+ * refusal that only a script call can receive, such as an unknown capability.
8
+ * @param options `cause` is the error of a `transaction()` body that a `transaction_rollback`
9
+ * error replaces.
6
10
  */
7
- constructor(message, code, status, details, failureId = null) {
8
- super(message);
11
+ constructor(message, code, status, details, failureId = null, failure = null, options = {}) {
12
+ super(message, options);
9
13
  this.code = code;
10
14
  this.status = status;
11
15
  this.details = details;
12
16
  this.failureId = failureId;
17
+ this.failure = failure;
13
18
  this.name = "HostError";
14
19
  }
15
20
  code;
16
21
  status;
17
22
  details;
18
23
  failureId;
24
+ failure;
19
25
  };
20
26
  var WorkflowFailure = class extends Error {
21
27
  constructor(message, status, details = null) {
package/dist/errors.d.ts CHANGED
@@ -1,13 +1,33 @@
1
+ /**
2
+ * The failure that a graph step records when its task throws, which the step's error edge reads.
3
+ * A task failure has `type`, `message`, and `status_code`. An unexpected exception also has
4
+ * `original_type`, a validation failure has `errors`, and a refused input has `invalid_input`.
5
+ * A declared error is keyed by its type, and a failed child workflow gives its own failure.
6
+ */
7
+ export type HostFailure = {
8
+ type?: string;
9
+ message?: string;
10
+ original_type?: string;
11
+ status_code?: number;
12
+ errors?: Record<string, string[]>;
13
+ invalid_input?: unknown;
14
+ [key: string]: unknown;
15
+ };
1
16
  export declare class HostError extends Error {
2
17
  readonly code: string;
3
18
  readonly status: number;
4
19
  readonly details: unknown;
5
20
  readonly failureId: number | null;
21
+ readonly failure: HostFailure | null;
6
22
  /**
7
23
  * @param failureId The host's ID for the failure of the call. When the error ends the run, the
8
24
  * host records that failure, so a run records the failure of the call that it rethrows.
25
+ * @param failure The failure that a graph step records for the same error, or null for a
26
+ * refusal that only a script call can receive, such as an unknown capability.
27
+ * @param options `cause` is the error of a `transaction()` body that a `transaction_rollback`
28
+ * error replaces.
9
29
  */
10
- constructor(message: string, code: string, status: number, details: unknown, failureId?: number | null);
30
+ constructor(message: string, code: string, status: number, details: unknown, failureId?: number | null, failure?: HostFailure | null, options?: ErrorOptions);
11
31
  }
12
32
  export declare class WorkflowFailure extends Error {
13
33
  readonly status: number;
@@ -1,6 +1,6 @@
1
1
  import type { ResourceKeyWith } from "../data.js";
2
2
  import type { ContentDocument } from "../input.js";
3
- export declare const SDK_VERSION = 68;
3
+ export declare const SDK_VERSION = 70;
4
4
  export interface WorkspaceSystemFields {
5
5
  "ai_custom_system_prompt": string | null;
6
6
  "brand_kit": {
@@ -1868,8 +1868,23 @@ export interface TransactionCommitInput {
1868
1868
  }
1869
1869
  export type TransactionCommitOutput = null;
1870
1870
  export interface TransactionRollbackInput {
1871
+ error?: {
1872
+ name?: string;
1873
+ message?: string;
1874
+ code?: string;
1875
+ status?: number;
1876
+ errors?: Record<string, unknown>;
1877
+ failure_id?: number;
1878
+ };
1871
1879
  }
1872
- export type TransactionRollbackOutput = null;
1880
+ export type TransactionRollbackOutput = null | {
1881
+ message: string;
1882
+ code: string;
1883
+ status: number;
1884
+ details: Record<string, unknown>;
1885
+ failure: Record<string, unknown>;
1886
+ failure_id: number;
1887
+ };
1873
1888
  export interface UserAuthorizeInput {
1874
1889
  permission: string;
1875
1890
  user?: Record<string, unknown>;
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  HostError
3
- } from "./chunks/chunk-OF5QITP7.js";
3
+ } from "./chunks/chunk-U2PBCCHZ.js";
4
4
 
5
5
  // packages/workflows/src/http-transport.ts
6
6
  var HttpTransport = class {
@@ -39,7 +39,14 @@ var HttpTransport = class {
39
39
  status: response.status,
40
40
  details: null
41
41
  };
42
- throw new HostError(error.message, error.code, error.status, error.details);
42
+ throw new HostError(
43
+ error.message,
44
+ error.code,
45
+ error.status,
46
+ error.details,
47
+ null,
48
+ error.failure ?? null
49
+ );
43
50
  }
44
51
  return reply.value;
45
52
  }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import "./console.js";
2
2
  export { HostError, WorkflowFailure, fail, validationError } from "./errors.js";
3
+ export type { HostFailure } from "./errors.js";
3
4
  export { invoke, setTransport, type HostTransport } from "./transport.js";
4
5
  export { transaction } from "./transaction.js";
5
6
  export { data } from "./data.js";
package/dist/index.js CHANGED
@@ -37,7 +37,7 @@ import {
37
37
  web,
38
38
  workflow,
39
39
  workspace
40
- } from "./chunks/chunk-4YUQWAM6.js";
40
+ } from "./chunks/chunk-DRZ7TQYF.js";
41
41
  import {
42
42
  InputField,
43
43
  InputSchema,
@@ -49,13 +49,13 @@ import {
49
49
  setTransport,
50
50
  v,
51
51
  validateWorkflowInput
52
- } from "./chunks/chunk-EU2ME5WR.js";
52
+ } from "./chunks/chunk-AQGV2DYG.js";
53
53
  import {
54
54
  HostError,
55
55
  WorkflowFailure,
56
56
  fail,
57
57
  validationError
58
- } from "./chunks/chunk-OF5QITP7.js";
58
+ } from "./chunks/chunk-U2PBCCHZ.js";
59
59
 
60
60
  // packages/workflows/src/notifications.ts
61
61
  var mail2 = {
@@ -35,7 +35,7 @@ import {
35
35
  web,
36
36
  workflow,
37
37
  workspace
38
- } from "../chunks/chunk-4YUQWAM6.js";
38
+ } from "../chunks/chunk-DRZ7TQYF.js";
39
39
  import {
40
40
  InputField,
41
41
  InputSchema,
@@ -45,13 +45,13 @@ import {
45
45
  respond,
46
46
  v,
47
47
  validateWorkflowInput
48
- } from "../chunks/chunk-EU2ME5WR.js";
48
+ } from "../chunks/chunk-AQGV2DYG.js";
49
49
  import {
50
50
  HostError,
51
51
  WorkflowFailure,
52
52
  fail,
53
53
  validationError
54
- } from "../chunks/chunk-OF5QITP7.js";
54
+ } from "../chunks/chunk-U2PBCCHZ.js";
55
55
 
56
56
  // packages/workflows/src/generated/profiles/test.ts
57
57
  var ai2 = {
@@ -2,10 +2,10 @@ import {
2
2
  parseWorkflowInput,
3
3
  setTransport,
4
4
  validateWorkflowInput
5
- } from "../chunks/chunk-EU2ME5WR.js";
5
+ } from "../chunks/chunk-AQGV2DYG.js";
6
6
  import {
7
7
  HostError
8
- } from "../chunks/chunk-OF5QITP7.js";
8
+ } from "../chunks/chunk-U2PBCCHZ.js";
9
9
 
10
10
  // packages/workflows/src/testing/index.ts
11
11
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -28,15 +28,19 @@ var SerialTransactionTransport = class {
28
28
  const rollsBack = name === "transaction.rollback";
29
29
  const ends = rollsBack || name === "transaction.commit";
30
30
  if (begins || ends) {
31
- if (input === null || typeof input !== "object" || isArray(input) || objectKeys(input).length > 0) {
31
+ const keys = input !== null && typeof input === "object" && !isArray(input) ? objectKeys(input) : null;
32
+ const describesError = rollsBack && keys !== null && keys.length === 1 && keys[0] === "error";
33
+ if (keys === null || keys.length > 0 && !describesError) {
32
34
  throw new HostError(
33
- "Transaction controls accept an empty object.",
35
+ "Transaction controls accept an empty object. A rollback also accepts the error of the body.",
34
36
  "invalid_arguments",
35
37
  422,
36
38
  null
37
39
  );
38
40
  }
39
- input = {};
41
+ if (!describesError) {
42
+ input = {};
43
+ }
40
44
  }
41
45
  if (this.closing || (this.transactionOpen || begins) && this.pending > 0 && !rollsBack) {
42
46
  throw new HostError(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@diagonal-hq/workflows",
3
- "version": "0.6.0",
3
+ "version": "0.7.0",
4
4
  "description": "The authoring API of Diagonal managed workflows, their capabilities, and the workflow test helpers.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "repository": {