openworkflow 0.4.0 → 0.5.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.
Files changed (143) hide show
  1. package/README.md +57 -46
  2. package/dist/backend-postgres/index.d.ts +44 -0
  3. package/dist/backend-postgres/index.d.ts.map +1 -0
  4. package/dist/backend-postgres/index.js +535 -0
  5. package/dist/backend-postgres/index.js.map +1 -0
  6. package/dist/backend-postgres/postgres.d.ts +42 -0
  7. package/dist/backend-postgres/postgres.d.ts.map +1 -0
  8. package/dist/backend-postgres/postgres.js +234 -0
  9. package/dist/backend-postgres/postgres.js.map +1 -0
  10. package/dist/backend-sqlite/index.d.ts +42 -0
  11. package/dist/backend-sqlite/index.d.ts.map +1 -0
  12. package/dist/backend-sqlite/index.js +655 -0
  13. package/dist/backend-sqlite/index.js.map +1 -0
  14. package/dist/backend-sqlite/sqlite.d.ts +61 -0
  15. package/dist/backend-sqlite/sqlite.d.ts.map +1 -0
  16. package/dist/backend-sqlite/sqlite.js +247 -0
  17. package/dist/backend-sqlite/sqlite.js.map +1 -0
  18. package/dist/backend.d.ts +21 -89
  19. package/dist/backend.d.ts.map +1 -1
  20. package/dist/backend.js +0 -21
  21. package/dist/backend.js.map +1 -1
  22. package/dist/bin/openworkflow.d.ts +3 -0
  23. package/dist/bin/openworkflow.d.ts.map +1 -0
  24. package/dist/bin/openworkflow.js +44 -0
  25. package/dist/bin/openworkflow.js.map +1 -0
  26. package/dist/client.d.ts +68 -80
  27. package/dist/client.d.ts.map +1 -1
  28. package/dist/client.js +83 -53
  29. package/dist/client.js.map +1 -1
  30. package/dist/config.d.ts +34 -0
  31. package/dist/config.d.ts.map +1 -0
  32. package/dist/config.js +49 -0
  33. package/dist/config.js.map +1 -0
  34. package/dist/{duration.d.ts → core/duration.d.ts} +5 -2
  35. package/dist/core/duration.d.ts.map +1 -0
  36. package/dist/{duration.js → core/duration.js} +9 -6
  37. package/dist/core/duration.js.map +1 -0
  38. package/dist/core/error.d.ts +14 -0
  39. package/dist/core/error.d.ts.map +1 -0
  40. package/dist/core/error.js +18 -0
  41. package/dist/core/error.js.map +1 -0
  42. package/dist/core/json.d.ts +5 -0
  43. package/dist/core/json.d.ts.map +1 -0
  44. package/dist/core/json.js +2 -0
  45. package/dist/core/json.js.map +1 -0
  46. package/dist/core/result.d.ts +22 -0
  47. package/dist/core/result.d.ts.map +1 -0
  48. package/dist/core/result.js +17 -0
  49. package/dist/core/result.js.map +1 -0
  50. package/dist/core/retry.d.ts +7 -0
  51. package/dist/core/retry.d.ts.map +1 -0
  52. package/dist/core/retry.js +7 -0
  53. package/dist/core/retry.js.map +1 -0
  54. package/dist/core/schema.d.ts.map +1 -0
  55. package/dist/core/schema.js.map +1 -0
  56. package/dist/core/step.d.ts +89 -0
  57. package/dist/core/step.d.ts.map +1 -0
  58. package/dist/core/step.js +66 -0
  59. package/dist/core/step.js.map +1 -0
  60. package/dist/core/workflow.d.ts +63 -0
  61. package/dist/core/workflow.d.ts.map +1 -0
  62. package/dist/core/workflow.js +35 -0
  63. package/dist/core/workflow.js.map +1 -0
  64. package/dist/execution.d.ts +69 -0
  65. package/dist/execution.d.ts.map +1 -0
  66. package/dist/execution.js +188 -0
  67. package/dist/execution.js.map +1 -0
  68. package/dist/index.d.ts +2 -1
  69. package/dist/index.d.ts.map +1 -1
  70. package/dist/index.js +2 -1
  71. package/dist/index.js.map +1 -1
  72. package/dist/internal.d.ts +8 -0
  73. package/dist/internal.d.ts.map +1 -0
  74. package/dist/internal.js +5 -0
  75. package/dist/internal.js.map +1 -0
  76. package/dist/pg/backend.d.ts +42 -0
  77. package/dist/pg/backend.d.ts.map +1 -0
  78. package/dist/pg/backend.js +534 -0
  79. package/dist/pg/backend.js.map +1 -0
  80. package/dist/pg/index.d.ts +3 -0
  81. package/dist/pg/index.d.ts.map +1 -0
  82. package/dist/pg/index.js +3 -0
  83. package/dist/pg/index.js.map +1 -0
  84. package/dist/pg/postgres.d.ts +42 -0
  85. package/dist/pg/postgres.d.ts.map +1 -0
  86. package/dist/pg/postgres.js +234 -0
  87. package/dist/pg/postgres.js.map +1 -0
  88. package/dist/pg/scripts/db-migrate.d.ts +2 -0
  89. package/dist/pg/scripts/db-migrate.d.ts.map +1 -0
  90. package/dist/pg/scripts/db-migrate.js +5 -0
  91. package/dist/pg/scripts/db-migrate.js.map +1 -0
  92. package/dist/pg/scripts/db-reset.d.ts +2 -0
  93. package/dist/pg/scripts/db-reset.d.ts.map +1 -0
  94. package/dist/pg/scripts/db-reset.js +6 -0
  95. package/dist/pg/scripts/db-reset.js.map +1 -0
  96. package/dist/pg/scripts/squawk.d.ts +2 -0
  97. package/dist/pg/scripts/squawk.d.ts.map +1 -0
  98. package/dist/pg/scripts/squawk.js +17 -0
  99. package/dist/pg/scripts/squawk.js.map +1 -0
  100. package/dist/pg/vitest.global-setup.d.ts +3 -0
  101. package/dist/pg/vitest.global-setup.d.ts.map +1 -0
  102. package/dist/pg/vitest.global-setup.js +8 -0
  103. package/dist/pg/vitest.global-setup.js.map +1 -0
  104. package/dist/registry.d.ts +27 -0
  105. package/dist/registry.d.ts.map +1 -0
  106. package/dist/registry.js +49 -0
  107. package/dist/registry.js.map +1 -0
  108. package/dist/sqlite/backend.d.ts +40 -0
  109. package/dist/sqlite/backend.d.ts.map +1 -0
  110. package/dist/sqlite/backend.js +654 -0
  111. package/dist/sqlite/backend.js.map +1 -0
  112. package/dist/sqlite/index.d.ts +3 -0
  113. package/dist/sqlite/index.d.ts.map +1 -0
  114. package/dist/sqlite/index.js +3 -0
  115. package/dist/sqlite/index.js.map +1 -0
  116. package/dist/sqlite/sqlite.d.ts +61 -0
  117. package/dist/sqlite/sqlite.d.ts.map +1 -0
  118. package/dist/sqlite/sqlite.js +247 -0
  119. package/dist/sqlite/sqlite.js.map +1 -0
  120. package/dist/testing/backend.testsuite.d.ts +20 -0
  121. package/dist/testing/backend.testsuite.d.ts.map +1 -0
  122. package/dist/testing/backend.testsuite.js +1091 -0
  123. package/dist/testing/backend.testsuite.js.map +1 -0
  124. package/dist/testing/index.d.ts +2 -0
  125. package/dist/testing/index.d.ts.map +1 -0
  126. package/dist/testing/index.js +2 -0
  127. package/dist/testing/index.js.map +1 -0
  128. package/dist/tsconfig.tsbuildinfo +1 -1
  129. package/dist/worker.d.ts +10 -3
  130. package/dist/worker.d.ts.map +1 -1
  131. package/dist/worker.js +29 -195
  132. package/dist/worker.js.map +1 -1
  133. package/dist/workflow.d.ts +60 -0
  134. package/dist/workflow.d.ts.map +1 -0
  135. package/dist/workflow.js +49 -0
  136. package/dist/workflow.js.map +1 -0
  137. package/package.json +16 -6
  138. package/dist/duration.d.ts.map +0 -1
  139. package/dist/duration.js.map +0 -1
  140. package/dist/schema.d.ts.map +0 -1
  141. package/dist/schema.js.map +0 -1
  142. /package/dist/{schema.d.ts → core/schema.d.ts} +0 -0
  143. /package/dist/{schema.js → core/schema.js} +0 -0
@@ -0,0 +1,5 @@
1
+ export type JsonPrimitive = string | number | boolean | null;
2
+ export type JsonValue = JsonPrimitive | JsonValue[] | {
3
+ [key: string]: JsonValue;
4
+ };
5
+ //# sourceMappingURL=json.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.d.ts","sourceRoot":"","sources":["../../core/json.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,aAAa,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC;AAC7D,MAAM,MAAM,SAAS,GACjB,aAAa,GACb,SAAS,EAAE,GACX;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,SAAS,CAAA;CAAE,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=json.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"json.js","sourceRoot":"","sources":["../../core/json.ts"],"names":[],"mappings":""}
@@ -0,0 +1,22 @@
1
+ export type Result<T> = Ok<T> | Err;
2
+ export interface Ok<T> {
3
+ ok: true;
4
+ value: T;
5
+ }
6
+ export interface Err {
7
+ ok: false;
8
+ error: Error;
9
+ }
10
+ /**
11
+ * Create an Ok result.
12
+ * @param value - Result value
13
+ * @returns Ok result
14
+ */
15
+ export declare function ok<T>(value: T): Ok<T>;
16
+ /**
17
+ * Create an Err result.
18
+ * @param error - Result error
19
+ * @returns Err result
20
+ */
21
+ export declare function err(error: Readonly<Error>): Err;
22
+ //# sourceMappingURL=result.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../core/result.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,MAAM,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC;AAEpC,MAAM,WAAW,EAAE,CAAC,CAAC;IACnB,EAAE,EAAE,IAAI,CAAC;IACT,KAAK,EAAE,CAAC,CAAC;CACV;AAED,MAAM,WAAW,GAAG;IAClB,EAAE,EAAE,KAAK,CAAC;IACV,KAAK,EAAE,KAAK,CAAC;CACd;AAED;;;;GAIG;AACH,wBAAgB,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAErC;AAED;;;;GAIG;AACH,wBAAgB,GAAG,CAAC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC,GAAG,GAAG,CAE/C"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Create an Ok result.
3
+ * @param value - Result value
4
+ * @returns Ok result
5
+ */
6
+ export function ok(value) {
7
+ return { ok: true, value };
8
+ }
9
+ /**
10
+ * Create an Err result.
11
+ * @param error - Result error
12
+ * @returns Err result
13
+ */
14
+ export function err(error) {
15
+ return { ok: false, error };
16
+ }
17
+ //# sourceMappingURL=result.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"result.js","sourceRoot":"","sources":["../../core/result.ts"],"names":[],"mappings":"AAYA;;;;GAIG;AACH,MAAM,UAAU,EAAE,CAAI,KAAQ;IAC5B,OAAO,EAAE,EAAE,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;AAC7B,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAC,KAAsB;IACxC,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AAC9B,CAAC"}
@@ -0,0 +1,7 @@
1
+ export declare const DEFAULT_RETRY_POLICY: {
2
+ readonly initialIntervalMs: 1000;
3
+ readonly backoffCoefficient: 2;
4
+ readonly maximumIntervalMs: number;
5
+ readonly maximumAttempts: number;
6
+ };
7
+ //# sourceMappingURL=retry.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.d.ts","sourceRoot":"","sources":["../../core/retry.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,oBAAoB;;;;;CAKvB,CAAC"}
@@ -0,0 +1,7 @@
1
+ export const DEFAULT_RETRY_POLICY = {
2
+ initialIntervalMs: 1000, // 1s
3
+ backoffCoefficient: 2,
4
+ maximumIntervalMs: 100 * 1000, // 100s
5
+ maximumAttempts: Infinity, // unlimited
6
+ };
7
+ //# sourceMappingURL=retry.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"retry.js","sourceRoot":"","sources":["../../core/retry.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,iBAAiB,EAAE,IAAI,EAAE,KAAK;IAC9B,kBAAkB,EAAE,CAAC;IACrB,iBAAiB,EAAE,GAAG,GAAG,IAAI,EAAE,OAAO;IACtC,eAAe,EAAE,QAAQ,EAAE,YAAY;CAC/B,CAAC"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../core/schema.ts"],"names":[],"mappings":"AAAA,gEAAgE;AAChE,MAAM,WAAW,gBAAgB,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;IAC/D,sCAAsC;IACtC,QAAQ,CAAC,WAAW,EAAE,gBAAgB,CAAC,KAAK,CAAC,KAAK,EAAE,MAAM,CAAC,CAA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;KAC3B;IAED,2CAA2C;IAC3C,MAAM,WAAW,KAAK,CAAC,KAAK,GAAG,OAAO,EAAE,MAAM,GAAG,KAAK;QACpD,oCAAoC;QACpC,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC;QACtB,qCAAqC;QACrC,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;KACzB;IAED,kDAAkD;IAClD,MAAM,MAAM,UAAU,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CACnE,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,OAAO,CAAC,CAAC;IAEX,mDAAmD;IACnD,MAAM,MAAM,WAAW,CAAC,MAAM,SAAS,gBAAgB,IAAI,WAAW,CACpE,MAAM,CAAC,WAAW,CAAC,CAAC,OAAO,CAAC,CAC7B,CAAC,QAAQ,CAAC,CAAC;IAIZ,OAAO,EAAE,CAAC;CACX"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../../core/schema.ts"],"names":[],"mappings":""}
@@ -0,0 +1,89 @@
1
+ import type { DurationString } from "./duration.js";
2
+ import type { JsonValue } from "./json.js";
3
+ import type { Result } from "./result.js";
4
+ /**
5
+ * The kind of step in a workflow.
6
+ */
7
+ export type StepKind = "function" | "sleep";
8
+ /**
9
+ * Status of a step attempt through its lifecycle.
10
+ */
11
+ export type StepAttemptStatus = "running" | "succeeded" | "completed" | "failed";
12
+ /**
13
+ * Context for a step attempt (currently only used for sleep steps).
14
+ */
15
+ export interface StepAttemptContext {
16
+ kind: "sleep";
17
+ resumeAt: string;
18
+ }
19
+ /**
20
+ * StepAttempt represents a single attempt of a step within a workflow.
21
+ */
22
+ export interface StepAttempt {
23
+ namespaceId: string;
24
+ id: string;
25
+ workflowRunId: string;
26
+ stepName: string;
27
+ kind: StepKind;
28
+ status: StepAttemptStatus;
29
+ config: JsonValue;
30
+ context: StepAttemptContext | null;
31
+ output: JsonValue | null;
32
+ error: JsonValue | null;
33
+ childWorkflowRunNamespaceId: string | null;
34
+ childWorkflowRunId: string | null;
35
+ startedAt: Date | null;
36
+ finishedAt: Date | null;
37
+ createdAt: Date;
38
+ updatedAt: Date;
39
+ }
40
+ /**
41
+ * Immutable cache for step attempts, keyed by step name.
42
+ */
43
+ export type StepAttemptCache = ReadonlyMap<string, StepAttempt>;
44
+ /**
45
+ * Create a step attempt cache from an array of attempts. Only includes
46
+ * successful attempts (completed or succeeded status).
47
+ * @param attempts - Array of step attempts to cache
48
+ * @returns An immutable map of step name to successful attempt
49
+ */
50
+ export declare function createStepAttemptCacheFromAttempts(attempts: readonly StepAttempt[]): StepAttemptCache;
51
+ /**
52
+ * Get a cached step attempt by name.
53
+ * @param cache - The step attempt cache
54
+ * @param stepName - The name of the step to look up
55
+ * @returns The cached attempt or undefined if not found
56
+ */
57
+ export declare function getCachedStepAttempt(cache: StepAttemptCache, stepName: string): StepAttempt | undefined;
58
+ /**
59
+ * Add a step attempt to the cache (returns new cache, original unchanged). This
60
+ * is an immutable operation.
61
+ * @param cache - The existing step attempt cache
62
+ * @param attempt - The attempt to add
63
+ * @returns A new cache with the attempt added
64
+ */
65
+ export declare function addToStepAttemptCache(cache: StepAttemptCache, attempt: Readonly<StepAttempt>): StepAttemptCache;
66
+ /**
67
+ * Convert a step function result to a JSON-compatible value. Undefined values
68
+ * are converted to null for JSON serialization.
69
+ * @param result - The result from a step function
70
+ * @returns A JSON-serializable value
71
+ */
72
+ export declare function normalizeStepOutput(result: unknown): JsonValue;
73
+ /**
74
+ * Calculate the resume time for a sleep step.
75
+ * @param duration - The duration string to sleep for
76
+ * @param now - The current timestamp (defaults to Date.now())
77
+ * @returns A Result containing the resume Date or an Error
78
+ */
79
+ export declare function calculateSleepResumeAt(duration: DurationString, now?: number): Result<Date>;
80
+ /**
81
+ * Create the context object for a sleep step attempt.
82
+ * @param resumeAt - The time when the sleep should resume
83
+ * @returns The context object for the sleep step
84
+ */
85
+ export declare function createSleepContext(resumeAt: Readonly<Date>): {
86
+ kind: "sleep";
87
+ resumeAt: string;
88
+ };
89
+ //# sourceMappingURL=step.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"step.d.ts","sourceRoot":"","sources":["../../core/step.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAEpD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAG1C;;GAEG;AACH,MAAM,MAAM,QAAQ,GAAG,UAAU,GAAG,OAAO,CAAC;AAE5C;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,SAAS,GACT,WAAW,GACX,WAAW,GACX,QAAQ,CAAC;AAEb;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,QAAQ,CAAC;IACf,MAAM,EAAE,iBAAiB,CAAC;IAC1B,MAAM,EAAE,SAAS,CAAC;IAClB,OAAO,EAAE,kBAAkB,GAAG,IAAI,CAAC;IACnC,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACxB,2BAA2B,EAAE,MAAM,GAAG,IAAI,CAAC;IAC3C,kBAAkB,EAAE,MAAM,GAAG,IAAI,CAAC;IAClC,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;AAEhE;;;;;GAKG;AACH,wBAAgB,kCAAkC,CAChD,QAAQ,EAAE,SAAS,WAAW,EAAE,GAC/B,gBAAgB,CAUlB;AAED;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,gBAAgB,EACvB,QAAQ,EAAE,MAAM,GACf,WAAW,GAAG,SAAS,CAEzB;AAED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,gBAAgB,EACvB,OAAO,EAAE,QAAQ,CAAC,WAAW,CAAC,GAC7B,gBAAgB,CAElB;AAED;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,OAAO,GAAG,SAAS,CAE9D;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,cAAc,EACxB,GAAG,GAAE,MAAmB,GACvB,MAAM,CAAC,IAAI,CAAC,CAQd;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,CAAC,IAAI,CAAC,GAAG;IAC5D,IAAI,EAAE,OAAO,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;CAClB,CAKA"}
@@ -0,0 +1,66 @@
1
+ import { parseDuration } from "./duration.js";
2
+ import { err, ok } from "./result.js";
3
+ /**
4
+ * Create a step attempt cache from an array of attempts. Only includes
5
+ * successful attempts (completed or succeeded status).
6
+ * @param attempts - Array of step attempts to cache
7
+ * @returns An immutable map of step name to successful attempt
8
+ */
9
+ export function createStepAttemptCacheFromAttempts(attempts) {
10
+ // 'succeeded' status is deprecated in favor of 'completed'
11
+ const successfulAttempts = attempts.filter((attempt) => attempt.status === "succeeded" || attempt.status === "completed");
12
+ return new Map(successfulAttempts.map((attempt) => [attempt.stepName, attempt]));
13
+ }
14
+ /**
15
+ * Get a cached step attempt by name.
16
+ * @param cache - The step attempt cache
17
+ * @param stepName - The name of the step to look up
18
+ * @returns The cached attempt or undefined if not found
19
+ */
20
+ export function getCachedStepAttempt(cache, stepName) {
21
+ return cache.get(stepName);
22
+ }
23
+ /**
24
+ * Add a step attempt to the cache (returns new cache, original unchanged). This
25
+ * is an immutable operation.
26
+ * @param cache - The existing step attempt cache
27
+ * @param attempt - The attempt to add
28
+ * @returns A new cache with the attempt added
29
+ */
30
+ export function addToStepAttemptCache(cache, attempt) {
31
+ return new Map([...cache, [attempt.stepName, attempt]]);
32
+ }
33
+ /**
34
+ * Convert a step function result to a JSON-compatible value. Undefined values
35
+ * are converted to null for JSON serialization.
36
+ * @param result - The result from a step function
37
+ * @returns A JSON-serializable value
38
+ */
39
+ export function normalizeStepOutput(result) {
40
+ return (result ?? null);
41
+ }
42
+ /**
43
+ * Calculate the resume time for a sleep step.
44
+ * @param duration - The duration string to sleep for
45
+ * @param now - The current timestamp (defaults to Date.now())
46
+ * @returns A Result containing the resume Date or an Error
47
+ */
48
+ export function calculateSleepResumeAt(duration, now = Date.now()) {
49
+ const result = parseDuration(duration);
50
+ if (!result.ok) {
51
+ return err(result.error);
52
+ }
53
+ return ok(new Date(now + result.value));
54
+ }
55
+ /**
56
+ * Create the context object for a sleep step attempt.
57
+ * @param resumeAt - The time when the sleep should resume
58
+ * @returns The context object for the sleep step
59
+ */
60
+ export function createSleepContext(resumeAt) {
61
+ return {
62
+ kind: "sleep",
63
+ resumeAt: resumeAt.toISOString(),
64
+ };
65
+ }
66
+ //# sourceMappingURL=step.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"step.js","sourceRoot":"","sources":["../../core/step.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,eAAe,CAAC;AAG9C,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAmDtC;;;;;GAKG;AACH,MAAM,UAAU,kCAAkC,CAChD,QAAgC;IAEhC,2DAA2D;IAC3D,MAAM,kBAAkB,GAAG,QAAQ,CAAC,MAAM,CACxC,CAAC,OAA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@ -0,0 +1,63 @@
1
+ import type { SerializedError } from "./error.js";
2
+ import { JsonValue } from "./json.js";
3
+ import type { StandardSchemaV1 } from "./schema.js";
4
+ /**
5
+ * Status of a workflow run through its lifecycle.
6
+ */
7
+ export type WorkflowRunStatus = "pending" | "running" | "sleeping" | "succeeded" | "completed" | "failed" | "canceled";
8
+ /**
9
+ * WorkflowRun represents a single execution instance of a workflow.
10
+ */
11
+ export interface WorkflowRun {
12
+ namespaceId: string;
13
+ id: string;
14
+ workflowName: string;
15
+ version: string | null;
16
+ status: WorkflowRunStatus;
17
+ idempotencyKey: string | null;
18
+ config: JsonValue;
19
+ context: JsonValue | null;
20
+ input: JsonValue | null;
21
+ output: JsonValue | null;
22
+ error: SerializedError | null;
23
+ attempts: number;
24
+ parentStepAttemptNamespaceId: string | null;
25
+ parentStepAttemptId: string | null;
26
+ workerId: string | null;
27
+ availableAt: Date | null;
28
+ deadlineAt: Date | null;
29
+ startedAt: Date | null;
30
+ finishedAt: Date | null;
31
+ createdAt: Date;
32
+ updatedAt: Date;
33
+ }
34
+ /**
35
+ * Infers the input type from a Standard Schema.
36
+ */
37
+ export type SchemaInput<TSchema, Fallback> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferInput<TSchema> : Fallback;
38
+ /**
39
+ * Infers the output type from a Standard Schema.
40
+ */
41
+ export type SchemaOutput<TSchema, Fallback> = TSchema extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<TSchema> : Fallback;
42
+ /**
43
+ * Result of input validation - either success with a value or failure with an
44
+ * error message.
45
+ */
46
+ export type ValidationResult<T> = {
47
+ success: true;
48
+ value: T;
49
+ } | {
50
+ success: false;
51
+ error: string;
52
+ };
53
+ /**
54
+ * Validate input against a Standard Schema. Pure async function that validates
55
+ * input and returns a ValidationResult.
56
+ * @param schema - The Standard Schema to validate against (or null/undefined
57
+ * for no validation)
58
+ * @param input - The input value to validate
59
+ * @returns A ValidationResult containing either the validated value or an error
60
+ * message
61
+ */
62
+ export declare function validateInput<RunInput, Input>(schema: StandardSchemaV1<RunInput, Input> | null | undefined, input: RunInput | undefined): Promise<ValidationResult<Input>>;
63
+ //# sourceMappingURL=workflow.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workflow.d.ts","sourceRoot":"","sources":["../../core/workflow.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD;;GAEG;AACH,MAAM,MAAM,iBAAiB,GACzB,SAAS,GACT,SAAS,GACT,UAAU,GACV,WAAW,GACX,WAAW,GACX,QAAQ,GACR,UAAU,CAAC;AAEf;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,WAAW,EAAE,MAAM,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,YAAY,EAAE,MAAM,CAAC;IACrB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,MAAM,EAAE,iBAAiB,CAAC;IAC1B,cAAc,EAAE,MAAM,GAAG,IAAI,CAAC;IAC9B,MAAM,EAAE,SAAS,CAAC;IAClB,OAAO,EAAE,SAAS,GAAG,IAAI,CAAC;IAC1B,KAAK,EAAE,SAAS,GAAG,IAAI,CAAC;IACxB,MAAM,EAAE,SAAS,GAAG,IAAI,CAAC;IACzB,KAAK,EAAE,eAAe,GAAG,IAAI,CAAC;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,4BAA4B,EAAE,MAAM,GAAG,IAAI,CAAC;IAC5C,mBAAmB,EAAE,MAAM,GAAG,IAAI,CAAC;IACnC,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAC;IACxB,WAAW,EAAE,IAAI,GAAG,IAAI,CAAC;IACzB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,IAAI,GAAG,IAAI,CAAC;IACvB,UAAU,EAAE,IAAI,GAAG,IAAI,CAAC;IACxB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;CACjB;AAED;;GAEG;AACH,MAAM,MAAM,WAAW,CAAC,OAAO,EAAE,QAAQ,IAAI,OAAO,SAAS,gBAAgB,GACzE,gBAAgB,CAAC,UAAU,CAAC,OAAO,CAAC,GACpC,QAAQ,CAAC;AAEb;;GAEG;AACH,MAAM,MAAM,YAAY,CAAC,OAAO,EAAE,QAAQ,IAAI,OAAO,SAAS,gBAAgB,GAC1E,gBAAgB,CAAC,WAAW,CAAC,OAAO,CAAC,GACrC,QAAQ,CAAC;AAEb;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,CAAC,IAC1B;IAAE,OAAO,EAAE,IAAI,CAAC;IAAC,KAAK,EAAE,CAAC,CAAA;CAAE,GAC3B;IAAE,OAAO,EAAE,KAAK,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEtC;;;;;;;;GAQG;AACH,wBAAsB,aAAa,CAAC,QAAQ,EAAE,KAAK,EACjD,MAAM,EAAE,gBAAgB,CAAC,QAAQ,EAAE,KAAK,CAAC,GAAG,IAAI,GAAG,SAAS,EAC5D,KAAK,EAAE,QAAQ,GAAG,SAAS,GAC1B,OAAO,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CA4BlC"}
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Validate input against a Standard Schema. Pure async function that validates
3
+ * input and returns a ValidationResult.
4
+ * @param schema - The Standard Schema to validate against (or null/undefined
5
+ * for no validation)
6
+ * @param input - The input value to validate
7
+ * @returns A ValidationResult containing either the validated value or an error
8
+ * message
9
+ */
10
+ export async function validateInput(schema, input) {
11
+ // No schema means no validation - pass through as-is
12
+ if (!schema) {
13
+ return {
14
+ success: true,
15
+ value: input,
16
+ };
17
+ }
18
+ // Validate using Standard Schema v1 protocol https://standardschema.dev
19
+ const result = schema["~standard"].validate(input);
20
+ const resolved = await Promise.resolve(result);
21
+ if (resolved.issues) {
22
+ const messages = resolved.issues.length > 0
23
+ ? resolved.issues.map((issue) => issue.message).join("; ")
24
+ : "Validation failed";
25
+ return {
26
+ success: false,
27
+ error: messages,
28
+ };
29
+ }
30
+ return {
31
+ success: true,
32
+ value: resolved.value,
33
+ };
34
+ }
35
+ //# sourceMappingURL=workflow.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workflow.js","sourceRoot":"","sources":["../../core/workflow.ts"],"names":[],"mappings":"AAiEA;;;;;;;;GAQG;AACH,MAAM,CAAC,KAAK,UAAU,aAAa,CACjC,MAA4D,EAC5D,KAA2B;IAE3B,qDAAqD;IACrD,IAAI,CAAC,MAAM,EAAE,CAAC;QACZ,OAAO;YACL,OAAO,EAAE,IAAI;YACb,KAAK,EAAE,KAAyB;SACjC,CAAC;IACJ,CAAC;IAED,wEAAwE;IACxE,MAAM,MAAM,GAAG,MAAM,CAAC,WAAW,CAAC,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IACnD,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC;IAE/C,IAAI,QAAQ,CAAC,MAAM,EAAE,CAAC;QACpB,MAAM,QAAQ,GACZ,QAAQ,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC;YACxB,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC;YAC1D,CAAC,CAAC,mBAAmB,CAAC;QAC1B,OAAO;YACL,OAAO,EAAE,KAAK;YACd,KAAK,EAAE,QAAQ;SAChB,CAAC;IACJ,CAAC;IAED,OAAO;QACL,OAAO,EAAE,IAAI;QACb,KAAK,EAAE,QAAQ,CAAC,KAAK;KACtB,CAAC;AACJ,CAAC"}
@@ -0,0 +1,69 @@
1
+ import type { Backend } from "./backend.js";
2
+ import type { DurationString } from "./core/duration.js";
3
+ import type { StepAttempt } from "./core/step.js";
4
+ import type { WorkflowRun } from "./core/workflow.js";
5
+ /**
6
+ * Config for an individual step defined with `step.run()`.
7
+ */
8
+ export interface StepFunctionConfig {
9
+ /**
10
+ * The name of the step.
11
+ */
12
+ name: string;
13
+ }
14
+ /**
15
+ * Represents the API for defining steps within a workflow. Used within a
16
+ * workflow handler to define steps by calling `step.run()`.
17
+ */
18
+ export interface StepApi {
19
+ run<Output>(config: Readonly<StepFunctionConfig>, fn: StepFunction<Output>): Promise<Output>;
20
+ sleep(name: string, duration: DurationString): Promise<void>;
21
+ }
22
+ /**
23
+ * The step definition (defined by the user) that executes user code. Can return
24
+ * undefined (e.g., when using `return;`) which will be converted to null.
25
+ */
26
+ export type StepFunction<Output> = () => Promise<Output | undefined> | Output | undefined;
27
+ /**
28
+ * Params passed to a workflow function for the user to use when defining steps.
29
+ */
30
+ export interface WorkflowFunctionParams<Input> {
31
+ input: Input;
32
+ step: StepApi;
33
+ version: string | null;
34
+ }
35
+ /**
36
+ * The workflow definition's function (defined by the user) that the user uses
37
+ * to define the workflow's steps.
38
+ */
39
+ export type WorkflowFunction<Input, Output> = (params: Readonly<WorkflowFunctionParams<Input>>) => Promise<Output> | Output;
40
+ /**
41
+ * Configures the options for a StepExecutor.
42
+ */
43
+ export interface StepExecutorOptions {
44
+ backend: Backend;
45
+ workflowRunId: string;
46
+ workerId: string;
47
+ attempts: StepAttempt[];
48
+ }
49
+ /**
50
+ * Parameters for the workflow execution use case.
51
+ */
52
+ export interface ExecuteWorkflowParams {
53
+ backend: Backend;
54
+ workflowRun: WorkflowRun;
55
+ workflowFn: WorkflowFunction<unknown, unknown>;
56
+ workflowVersion: string | null;
57
+ workerId: string;
58
+ }
59
+ /**
60
+ * Execute a workflow run. This is the core application use case that handles:
61
+ * - Loading step history
62
+ * - Handling sleeping steps
63
+ * - Creating the step executor
64
+ * - Executing the workflow function
65
+ * - Completing, failing, or sleeping the workflow run based on the outcome
66
+ * @param params - The execution parameters
67
+ */
68
+ export declare function executeWorkflow(params: Readonly<ExecuteWorkflowParams>): Promise<void>;
69
+ //# sourceMappingURL=execution.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution.d.ts","sourceRoot":"","sources":["../execution.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAC5C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAC;AAGzD,OAAO,KAAK,EAAE,WAAW,EAAoB,MAAM,gBAAgB,CAAC;AASpE,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEtD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAC;CACd;AAED;;;GAGG;AACH,MAAM,WAAW,OAAO;IACtB,GAAG,CAAC,MAAM,EACR,MAAM,EAAE,QAAQ,CAAC,kBAAkB,CAAC,EACpC,EAAE,EAAE,YAAY,CAAC,MAAM,CAAC,GACvB,OAAO,CAAC,MAAM,CAAC,CAAC;IACnB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,QAAQ,EAAE,cAAc,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9D;AAED;;;GAGG;AACH,MAAM,MAAM,YAAY,CAAC,MAAM,IAAI,MAC/B,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,GAC3B,MAAM,GACN,SAAS,CAAC;AAEd;;GAEG;AACH,MAAM,WAAW,sBAAsB,CAAC,KAAK;IAC3C,KAAK,EAAE,KAAK,CAAC;IACb,IAAI,EAAE,OAAO,CAAC;IACd,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;CACxB;AAED;;;GAGG;AACH,MAAM,MAAM,gBAAgB,CAAC,KAAK,EAAE,MAAM,IAAI,CAC5C,MAAM,EAAE,QAAQ,CAAC,sBAAsB,CAAC,KAAK,CAAC,CAAC,KAC5C,OAAO,CAAC,MAAM,CAAC,GAAG,MAAM,CAAC;AAgB9B;;GAEG;AACH,MAAM,WAAW,mBAAmB;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,WAAW,EAAE,CAAC;CACzB;AAoGD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,OAAO,EAAE,OAAO,CAAC;IACjB,WAAW,EAAE,WAAW,CAAC;IACzB,UAAU,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;IAC/C,eAAe,EAAE,MAAM,GAAG,IAAI,CAAC;IAC/B,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;;GAQG;AACH,wBAAsB,eAAe,CACnC,MAAM,EAAE,QAAQ,CAAC,qBAAqB,CAAC,GACtC,OAAO,CAAC,IAAI,CAAC,CA4Ff"}
@@ -0,0 +1,188 @@
1
+ import { serializeError } from "./core/error.js";
2
+ import { createStepAttemptCacheFromAttempts, getCachedStepAttempt, addToStepAttemptCache, normalizeStepOutput, calculateSleepResumeAt, createSleepContext, } from "./core/step.js";
3
+ /**
4
+ * Signal thrown when a workflow needs to sleep. Contains the time when the
5
+ * workflow should resume.
6
+ */
7
+ class SleepSignal extends Error {
8
+ resumeAt;
9
+ constructor(resumeAt) {
10
+ super("SleepSignal");
11
+ this.name = "SleepSignal";
12
+ this.resumeAt = resumeAt;
13
+ }
14
+ }
15
+ /**
16
+ * Replays prior step attempts and persists new ones while memoizing
17
+ * deterministic step outputs.
18
+ */
19
+ class StepExecutor {
20
+ backend;
21
+ workflowRunId;
22
+ workerId;
23
+ cache;
24
+ constructor(options) {
25
+ this.backend = options.backend;
26
+ this.workflowRunId = options.workflowRunId;
27
+ this.workerId = options.workerId;
28
+ this.cache = createStepAttemptCacheFromAttempts(options.attempts);
29
+ }
30
+ async run(config, fn) {
31
+ const { name } = config;
32
+ // return cached result if available
33
+ const existingAttempt = getCachedStepAttempt(this.cache, name);
34
+ if (existingAttempt) {
35
+ return existingAttempt.output;
36
+ }
37
+ // not in cache, create new step attempt
38
+ const attempt = await this.backend.createStepAttempt({
39
+ workflowRunId: this.workflowRunId,
40
+ workerId: this.workerId,
41
+ stepName: name,
42
+ kind: "function",
43
+ config: {},
44
+ context: null,
45
+ });
46
+ try {
47
+ // execute step function
48
+ const result = await fn();
49
+ const output = normalizeStepOutput(result);
50
+ // mark success
51
+ const savedAttempt = await this.backend.completeStepAttempt({
52
+ workflowRunId: this.workflowRunId,
53
+ stepAttemptId: attempt.id,
54
+ workerId: this.workerId,
55
+ output,
56
+ });
57
+ // cache result
58
+ this.cache = addToStepAttemptCache(this.cache, savedAttempt);
59
+ return savedAttempt.output;
60
+ }
61
+ catch (error) {
62
+ // mark failure
63
+ await this.backend.failStepAttempt({
64
+ workflowRunId: this.workflowRunId,
65
+ stepAttemptId: attempt.id,
66
+ workerId: this.workerId,
67
+ error: serializeError(error),
68
+ });
69
+ throw error;
70
+ }
71
+ }
72
+ async sleep(name, duration) {
73
+ // return cached result if this sleep already completed
74
+ const existingAttempt = getCachedStepAttempt(this.cache, name);
75
+ if (existingAttempt)
76
+ return;
77
+ // create new step attempt for the sleep
78
+ const result = calculateSleepResumeAt(duration);
79
+ if (!result.ok) {
80
+ throw result.error;
81
+ }
82
+ const resumeAt = result.value;
83
+ const context = createSleepContext(resumeAt);
84
+ await this.backend.createStepAttempt({
85
+ workflowRunId: this.workflowRunId,
86
+ workerId: this.workerId,
87
+ stepName: name,
88
+ kind: "sleep",
89
+ config: {},
90
+ context,
91
+ });
92
+ // throw sleep signal to trigger postponement
93
+ // we do not mark the step as completed here; it will be updated
94
+ // when the workflow resumes
95
+ throw new SleepSignal(resumeAt);
96
+ }
97
+ }
98
+ /**
99
+ * Execute a workflow run. This is the core application use case that handles:
100
+ * - Loading step history
101
+ * - Handling sleeping steps
102
+ * - Creating the step executor
103
+ * - Executing the workflow function
104
+ * - Completing, failing, or sleeping the workflow run based on the outcome
105
+ * @param params - The execution parameters
106
+ */
107
+ export async function executeWorkflow(params) {
108
+ const { backend, workflowRun, workflowFn, workflowVersion, workerId } = params;
109
+ try {
110
+ // load all pages of step history
111
+ const attempts = [];
112
+ let cursor;
113
+ do {
114
+ const response = await backend.listStepAttempts({
115
+ workflowRunId: workflowRun.id,
116
+ ...(cursor ? { after: cursor } : {}),
117
+ limit: 1000,
118
+ });
119
+ attempts.push(...response.data);
120
+ cursor = response.pagination.next ?? undefined;
121
+ } while (cursor);
122
+ // mark any sleep steps as completed if their sleep duration has elapsed,
123
+ // or rethrow SleepSignal if still sleeping
124
+ for (let i = 0; i < attempts.length; i++) {
125
+ const attempt = attempts[i];
126
+ if (!attempt)
127
+ continue;
128
+ if (attempt.status === "running" &&
129
+ attempt.kind === "sleep" &&
130
+ attempt.context?.kind === "sleep") {
131
+ const now = Date.now();
132
+ const resumeAt = new Date(attempt.context.resumeAt);
133
+ const resumeAtMs = resumeAt.getTime();
134
+ if (now < resumeAtMs) {
135
+ // sleep duration HAS NOT elapsed yet, throw signal to put workflow
136
+ // back to sleep
137
+ throw new SleepSignal(resumeAt);
138
+ }
139
+ // sleep duration HAS elapsed, mark the step as completed and continue
140
+ const completed = await backend.completeStepAttempt({
141
+ workflowRunId: workflowRun.id,
142
+ stepAttemptId: attempt.id,
143
+ workerId,
144
+ output: null,
145
+ });
146
+ // update cache w/ completed attempt
147
+ attempts[i] = completed;
148
+ }
149
+ }
150
+ // create step executor
151
+ const executor = new StepExecutor({
152
+ backend,
153
+ workflowRunId: workflowRun.id,
154
+ workerId,
155
+ attempts,
156
+ });
157
+ // execute workflow
158
+ const output = await workflowFn({
159
+ input: workflowRun.input,
160
+ step: executor,
161
+ version: workflowVersion,
162
+ });
163
+ // mark success
164
+ await backend.completeWorkflowRun({
165
+ workflowRunId: workflowRun.id,
166
+ workerId,
167
+ output: (output ?? null),
168
+ });
169
+ }
170
+ catch (error) {
171
+ // handle sleep signal by setting workflow to sleeping status
172
+ if (error instanceof SleepSignal) {
173
+ await backend.sleepWorkflowRun({
174
+ workflowRunId: workflowRun.id,
175
+ workerId,
176
+ availableAt: error.resumeAt,
177
+ });
178
+ return;
179
+ }
180
+ // mark failure
181
+ await backend.failWorkflowRun({
182
+ workflowRunId: workflowRun.id,
183
+ workerId,
184
+ error: serializeError(error),
185
+ });
186
+ }
187
+ }
188
+ //# sourceMappingURL=execution.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execution.js","sourceRoot":"","sources":["../execution.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAGjD,OAAO,EACL,kCAAkC,EAClC,oBAAoB,EACpB,qBAAqB,EACrB,mBAAmB,EACnB,sBAAsB,EACtB,kBAAkB,GACnB,MAAM,gBAAgB,CAAC;AAmDxB;;;GAGG;AACH,MAAM,WAAY,SAAQ,KAAK;IACpB,QAAQ,CAAO;IAExB,YAAY,QAAwB;QAClC,KAAK,CAAC,aAAa,CAAC,CAAC;QACrB,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;QAC1B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;CACF;AAYD;;;GAGG;AACH,MAAM,YAAY;IACC,OAAO,CAAU;IACjB,aAAa,CAAS;IACtB,QAAQ,CAAS;IAC1B,KAAK,CAAmB;IAEhC,YAAY,OAAsC;QAChD,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;QAC/B,IAAI,CAAC,aAAa,GAAG,OAAO,CAAC,aAAa,CAAC;QAC3C,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC,QAAQ,CAAC;QAEjC,IAAI,CAAC,KAAK,GAAG,kCAAkC,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC;IACpE,CAAC;IAED,KAAK,CAAC,GAAG,CACP,MAAoC,EACpC,EAAwB;QAExB,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM,CAAC;QAExB,oCAAoC;QACpC,M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