@vite-hub/workflow 0.1.0 → 0.4.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.
Files changed (66) hide show
  1. package/README.md +173 -55
  2. package/dist/{app-CzPoP84f.js → app-He0GTrYZ.js} +1 -1
  3. package/dist/client-DkwOxEiT.js +707 -0
  4. package/dist/{cloudflare-nF4abPd4.js → cloudflare-BHfClPvT.js} +2 -2
  5. package/dist/{cloudflare-env-BNrYo29x.js → cloudflare-env-CGLx7lUd.js} +44 -42
  6. package/dist/cloudflare-env-lookup-CfX6wZKb.d.ts +4 -0
  7. package/dist/{cloudflare-env-CIjdFTv6.d.ts → cloudflare-shared-CTqh_a2s.d.ts} +2 -2
  8. package/dist/{config-IJMb0eeZ.js → config-C3DvtjfJ.js} +41 -40
  9. package/dist/definition.d.ts +18 -0
  10. package/dist/definition.js +33 -0
  11. package/dist/delay-D1-nq_VE.js +46 -0
  12. package/dist/dev-recorder-CTN5Fkao.js +10 -0
  13. package/dist/durability.d.ts +23 -0
  14. package/dist/durability.js +62 -0
  15. package/dist/error-diagnostics-0V2T3JFw.js +317 -0
  16. package/dist/error-diagnostics-CQ4BUqRe.js +270 -0
  17. package/dist/errors-DCqRnZpN.js +35 -0
  18. package/dist/errors-uhP8DZTi.d.ts +67 -0
  19. package/dist/index.d.ts +35 -63
  20. package/dist/index.js +15 -19
  21. package/dist/object-Da1zyPCR.js +11 -0
  22. package/dist/openworkflow-BhzxV_Y3.d.ts +52 -0
  23. package/dist/openworkflow-defaults-DDig2M4s.js +26 -0
  24. package/dist/openworkflow-storage-Dj9zUAsk.js +28 -0
  25. package/dist/openworkflow.d.ts +2 -0
  26. package/dist/openworkflow.js +2 -0
  27. package/dist/{operations-tNUdj3G4.d.ts → operation-definition-nmRu_O3V.d.ts} +42 -15
  28. package/dist/operations-D_WJwjrS.js +333 -0
  29. package/dist/operations.d.ts +40 -6
  30. package/dist/operations.js +163 -736
  31. package/dist/{out-CsaJxg8m.js → out-DxLKolA0.js} +32 -1430
  32. package/dist/{rolldown-runtime-CMFfr-1z.js → rolldown-runtime-B4iAMlE-.js} +10 -1
  33. package/dist/run-error-DBWqiJHH.js +110 -0
  34. package/dist/run-failed-gVGo1BaF.js +18 -0
  35. package/dist/runtime/cloudflare-runner.d.ts +3 -5
  36. package/dist/runtime/cloudflare-runner.js +39 -45
  37. package/dist/runtime/cloudflare-shared.d.ts +2 -2
  38. package/dist/runtime/cloudflare-shared.js +1 -1
  39. package/dist/runtime/cloudflare-vite.d.ts +3 -2
  40. package/dist/runtime/cloudflare-vite.js +47 -1
  41. package/dist/runtime/dev-runs.d.ts +4 -0
  42. package/dist/runtime/dev-runs.js +73 -0
  43. package/dist/runtime/execute.d.ts +42 -5
  44. package/dist/runtime/execute.js +216 -17
  45. package/dist/runtime/openworkflow-worker.d.ts +29 -2
  46. package/dist/runtime/openworkflow-worker.js +156 -38
  47. package/dist/runtime/openworkflow.d.ts +2 -21
  48. package/dist/runtime/openworkflow.js +307 -44
  49. package/dist/runtime/state.d.ts +3 -1
  50. package/dist/runtime/state.js +6 -6
  51. package/dist/runtime/vercel-vite.d.ts +1 -1
  52. package/dist/runtime/vercel-vite.js +7 -4
  53. package/dist/types-Dsmwsdlv.d.ts +282 -0
  54. package/dist/{vercel-D-ZNtB_u.js → vercel-DTvm61iY.js} +53 -24
  55. package/dist/vite.d.ts +228 -30
  56. package/dist/vite.js +2640 -1449
  57. package/package.json +28 -50
  58. package/dist/client-DsGM3M4U.js +0 -389
  59. package/dist/cloudflare-shared-CaxrMD1A.d.ts +0 -1
  60. package/dist/cloudflare-vite-B3IabBsv.js +0 -47
  61. package/dist/error-diagnostics-A_8Kp-48.js +0 -116
  62. package/dist/error-diagnostics-pwNFvAHP.js +0 -67
  63. package/dist/operations-B_rYG71W.js +0 -230
  64. package/dist/provider-operation-CW2cYfVy.js +0 -65
  65. package/dist/runtime-type-DBCvIh5x.js +0 -7
  66. package/dist/types-D82WmfZa.d.ts +0 -180
@@ -7,6 +7,15 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
7
7
  var __getProtoOf = Object.getPrototypeOf;
8
8
  var __hasOwnProp = Object.prototype.hasOwnProperty;
9
9
  var __commonJSMin = (cb, mod) => () => (mod || (cb((mod = { exports: {} }).exports, mod), cb = null), mod.exports);
10
+ var __exportAll = (all, no_symbols) => {
11
+ let target = {};
12
+ for (var name in all) __defProp(target, name, {
13
+ get: all[name],
14
+ enumerable: true
15
+ });
16
+ if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
17
+ return target;
18
+ };
10
19
  var __copyProps = (to, from, except, desc) => {
11
20
  if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
12
21
  key = keys[i];
@@ -23,4 +32,4 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
23
32
  }) : target, mod));
24
33
  var __require = /* #__PURE__ */ (() => createRequire(import.meta.url))();
25
34
  //#endregion
26
- export { __require as n, __toESM as r, __commonJSMin as t };
35
+ export { __toESM as i, __exportAll as n, __require as r, __commonJSMin as t };
@@ -0,0 +1,110 @@
1
+ import { n as createWorkflowError } from "./errors-DCqRnZpN.js";
2
+ import { t as getWorkflowDevRecorder } from "./dev-recorder-CTN5Fkao.js";
3
+ import { t as redactInspectionText } from "./inspect-B72iFyj-.js";
4
+ import { getViteHubErrorShape } from "@vite-hub/runtime";
5
+ //#region src/runtime/provider-operation.ts
6
+ function isWorkflowBoundaryError(error) {
7
+ if (getViteHubErrorShape(error)) return true;
8
+ if (typeof error !== "object" || error === null) return false;
9
+ try {
10
+ return "name" in error && error.name === "AbortError";
11
+ } catch {
12
+ return false;
13
+ }
14
+ }
15
+ function getWorkflowProviderStatus(error) {
16
+ if (typeof error !== "object" || error === null) return void 0;
17
+ try {
18
+ const value = error;
19
+ const response = typeof value.response === "object" && value.response !== null ? value.response : void 0;
20
+ const status = value.status ?? value.statusCode ?? response?.status;
21
+ return typeof status === "number" && Number.isInteger(status) && status >= 400 && status <= 599 ? status : void 0;
22
+ } catch {
23
+ return;
24
+ }
25
+ }
26
+ const maxReasonLength = 200;
27
+ /**
28
+ * A short reason that is safe to send to clients: the provider error name and the first line of its message. URLs,
29
+ * file paths, and credentials are removed, and long opaque values (ids, tokens) are replaced.
30
+ */
31
+ function getWorkflowProviderReason(error) {
32
+ if (typeof error !== "object" || error === null) return void 0;
33
+ let name;
34
+ let message;
35
+ try {
36
+ name = Reflect.get(error, "name");
37
+ message = Reflect.get(error, "message");
38
+ } catch {
39
+ return;
40
+ }
41
+ const firstLine = typeof message === "string" ? message.split(/\r?\n/, 1)[0].trim() : "";
42
+ const safeName = typeof name === "string" && /^[A-Za-z][\w$]{0,63}$/.test(name) ? name : void 0;
43
+ if (!firstLine) return safeName;
44
+ const text = redactInspectionText(firstLine).replace(/\b[a-z][a-z0-9+.-]*:\/\/\S+/gi, "[url]").replace(/(?:\b[A-Za-z]:)?(?:[\\/][\w.@~+-]+){2,}[\\/]?/g, "[path]").replace(/[\w+/=-]{32,}/g, "[redacted]");
45
+ const reason = safeName ? `${safeName}: ${text}` : text;
46
+ return reason.length > maxReasonLength ? `${reason.slice(0, 199)}…` : reason;
47
+ }
48
+ /** `vite dev` installs the run history. Elsewhere, `NODE_ENV` says whether the app runs a development server. */
49
+ function isDevelopment() {
50
+ return Boolean(getWorkflowDevRecorder()) || globalThis.process?.env?.NODE_ENV === "development";
51
+ }
52
+ async function runWorkflowProviderOperation(provider, operation, run, options = {}) {
53
+ try {
54
+ return await run();
55
+ } catch (error) {
56
+ if (options.boundaryError?.(error) || isWorkflowBoundaryError(error)) throw error;
57
+ const status = getWorkflowProviderStatus(error);
58
+ const acknowledgement = options.acknowledgement?.(error, status);
59
+ const reason = getWorkflowProviderReason(error);
60
+ const details = {
61
+ operation,
62
+ provider
63
+ };
64
+ if (acknowledgement) details.acknowledgement = acknowledgement;
65
+ if (reason) details.reason = reason;
66
+ if (status !== void 0) details.status = status;
67
+ if (isDevelopment()) console.error(`[vitehub:workflow] Workflow ${operation} failed in the ${provider} provider.`, error);
68
+ throw createWorkflowError({
69
+ cause: error,
70
+ code: "WORKFLOW_PROVIDER_OPERATION_FAILED",
71
+ details
72
+ });
73
+ }
74
+ }
75
+ function safeWorkflowName(name) {
76
+ return /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(name) ? name : void 0;
77
+ }
78
+ //#endregion
79
+ //#region src/runtime/run-error.ts
80
+ function ownString(value, key) {
81
+ try {
82
+ const member = Reflect.get(value, key);
83
+ return typeof member === "string" && member ? member : void 0;
84
+ } catch {
85
+ return;
86
+ }
87
+ }
88
+ /**
89
+ * Converts the failure that a provider stores to the JSON-safe `error` of a failed run. It keeps `message`, `code`,
90
+ * and a `name` other than `Error`, and drops the stack. A failure without a message gets a generic message, so every
91
+ * failed run has an `error`.
92
+ */
93
+ function toWorkflowRunError(value) {
94
+ if (typeof value === "string" && value) return { message: value };
95
+ if (typeof value === "object" && value !== null) {
96
+ const message = ownString(value, "message");
97
+ if (message !== void 0) {
98
+ const code = ownString(value, "code");
99
+ const name = ownString(value, "name");
100
+ return {
101
+ ...code ? { code } : {},
102
+ message,
103
+ ...name && name !== "Error" ? { name } : {}
104
+ };
105
+ }
106
+ }
107
+ return { message: "Workflow run failed." };
108
+ }
109
+ //#endregion
110
+ export { safeWorkflowName as a, runWorkflowProviderOperation as i, getWorkflowProviderStatus as n, isWorkflowBoundaryError as r, toWorkflowRunError as t };
@@ -0,0 +1,18 @@
1
+ import { emitRunFailed, toRunFailedError } from "@vite-hub/runtime/internal/run-failed";
2
+ //#region src/runtime/run-failed.ts
3
+ /**
4
+ * Sends `workflow.failed` to the listeners, such as Webhooks, when a run ends in failure with no retry left. An Agent
5
+ * turn runs in an internal Workflow and reports `invocation.failed` from its Invocation instead.
6
+ */
7
+ async function emitWorkflowFailed(name, runId, error, definition) {
8
+ if (definition?.internalAgentWorkflow || definition?.internalAgentInvocationRecovery) return;
9
+ await emitRunFailed({
10
+ event: "workflow.failed",
11
+ workflow: name,
12
+ runId,
13
+ error: toRunFailedError(error),
14
+ failedAt: (/* @__PURE__ */ new Date()).toISOString()
15
+ });
16
+ }
17
+ //#endregion
18
+ export { emitWorkflowFailed as t };
@@ -1,5 +1,5 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions, w as WorkflowProviderStep } from "../types-D82WmfZa.js";
2
- import { t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
1
+ import { D as WorkflowProviderStep, l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-Dsmwsdlv.js";
2
+ import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
3
3
  //#region src/runtime/cloudflare-runner.d.ts
4
4
  export interface CloudflareWorkflowEvent {
5
5
  id?: string;
@@ -8,9 +8,7 @@ export interface CloudflareWorkflowEvent {
8
8
  }
9
9
  export interface RunCloudflareWorkflowOptions {
10
10
  config: false | ResolvedWorkflowOptions | undefined;
11
- createNonRetryableError?: (error: Error & {
12
- isRetryable: false;
13
- }) => Error;
11
+ createNonRetryableError?: (error: Error) => Error;
14
12
  env: CloudflareWorkerEnv;
15
13
  event: CloudflareWorkflowEvent;
16
14
  name: string;
@@ -1,53 +1,25 @@
1
- import { t as workflowErrorDiagnostics } from "../error-diagnostics-pwNFvAHP.js";
2
- import { t as hasRuntimeType } from "../runtime-type-DBCvIh5x.js";
3
- import { i as runWithActiveCloudflareEnv } from "../cloudflare-env-BNrYo29x.js";
4
- import { runSerializedWorkflowHandler } from "./execute.js";
1
+ import { t as workflowErrorDiagnostics } from "../error-diagnostics-0V2T3JFw.js";
2
+ import { n as createWorkflowError, r as isNonRetryableError } from "../errors-DCqRnZpN.js";
3
+ import { r as runWithActiveCloudflareEnv } from "../cloudflare-env-CGLx7lUd.js";
4
+ import { t as fromCloudflareStartParams } from "../delay-D1-nq_VE.js";
5
+ import { resolveWorkflowStepOptions, runSerializedWorkflowHandler } from "./execute.js";
6
+ import { t as emitWorkflowFailed } from "../run-failed-gVGo1BaF.js";
5
7
  import { loadWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
8
+ import { hasRunFailedListeners } from "@vite-hub/runtime/internal/run-failed";
6
9
  //#region src/runtime/cloudflare-runner.ts
7
- function isNonRetryableError(error) {
8
- if (!hasRuntimeType(error, "object") || error === null) return false;
9
- try {
10
- const candidate = error;
11
- if (Reflect.get(candidate, "isRetryable") !== false) return false;
12
- const nativeIsError = Reflect.get(Error, "isError");
13
- if (hasRuntimeType(nativeIsError, "function")) return nativeIsError(candidate) === true;
14
- const clone = Reflect.get(globalThis, "structuredClone");
15
- if (!hasRuntimeType(clone, "function")) return false;
16
- try {
17
- return clone(candidate) instanceof Error;
18
- } catch {
19
- const descriptors = /* @__PURE__ */ new Map();
20
- try {
21
- for (const key of Reflect.ownKeys(candidate)) {
22
- const descriptor = Reflect.getOwnPropertyDescriptor(candidate, key);
23
- if (!descriptor?.configurable || !("value" in descriptor)) continue;
24
- descriptors.set(key, descriptor);
25
- Reflect.defineProperty(candidate, key, {
26
- ...descriptor,
27
- value: void 0
28
- });
29
- }
30
- try {
31
- return clone(candidate) instanceof Error;
32
- } catch {
33
- return false;
34
- }
35
- } finally {
36
- for (const [key, descriptor] of descriptors) Reflect.defineProperty(candidate, key, descriptor);
37
- }
38
- }
39
- } catch {
40
- return false;
41
- }
42
- }
10
+ /** The Cloudflare errors that a step already made. They pass the run boundary as they are, not wrapped again. */
11
+ const cloudflareNonRetryableErrors = /* @__PURE__ */ new WeakSet();
43
12
  function translateNonRetryableError(error, createNonRetryableError) {
44
- return createNonRetryableError && isNonRetryableError(error) ? createNonRetryableError(error) : error;
13
+ if (!createNonRetryableError || !isNonRetryableError(error) || cloudflareNonRetryableErrors.has(error)) return error;
14
+ const translated = createNonRetryableError(error);
15
+ cloudflareNonRetryableErrors.add(translated);
16
+ return translated;
45
17
  }
46
18
  function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeEvent) {
47
19
  if (!step?.do) return step;
48
20
  return {
49
21
  async do(name, options, run) {
50
- return await step.do(name, options, async () => await runWithActiveCloudflareEnv(env, async () => await runWithWorkflowRuntimeEvent(runtimeEvent(), async () => {
22
+ return await step.do(name, resolveWorkflowStepOptions(name, options), async () => await runWithActiveCloudflareEnv(env, async () => await runWithWorkflowRuntimeEvent(runtimeEvent(), async () => {
51
23
  try {
52
24
  return await run();
53
25
  } catch (error) {
@@ -55,7 +27,8 @@ function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeE
55
27
  }
56
28
  })));
57
29
  },
58
- ...step.sleep ? { sleep: step.sleep.bind(step) } : {}
30
+ ...step.sleep ? { sleep: async (name, milliseconds) => await step.sleep(name, milliseconds) } : {},
31
+ ...step.sleepUntil ? { sleepUntil: async (name, date) => await step.sleepUntil(name, date) } : {}
59
32
  };
60
33
  }
61
34
  async function runCloudflareWorkflow({ config, createNonRetryableError, env, event, name, registry, step }) {
@@ -70,15 +43,36 @@ async function runCloudflareWorkflow({ config, createNonRetryableError, env, eve
70
43
  return await runWithActiveCloudflareEnv(env, async () => {
71
44
  const definition = await loadWorkflowDefinition(name);
72
45
  if (!definition) throw workflowErrorDiagnostics.WORKFLOW_R0015({ message: `Missing workflow definition: ${name}` });
46
+ const { payload, startAt } = fromCloudflareStartParams(event?.payload);
47
+ if (startAt !== void 0) {
48
+ if (!step?.sleepUntil) throw createWorkflowError({
49
+ code: "WORKFLOW_OPERATION_UNSUPPORTED",
50
+ details: {
51
+ operation: "delay",
52
+ provider: "cloudflare"
53
+ }
54
+ });
55
+ await step.sleepUntil("vitehub-delay", new Date(startAt));
56
+ }
57
+ const id = event?.instanceId || event?.id;
73
58
  try {
74
59
  return await runWithWorkflowRuntimeEvent(runtimeEvent, () => runSerializedWorkflowHandler({
75
- id: event?.instanceId || event?.id,
60
+ id,
76
61
  name,
77
- payload: event?.payload,
62
+ payload,
78
63
  provider: "cloudflare",
79
64
  step: providerStep
80
65
  }, definition));
81
66
  } catch (error) {
67
+ if (id && hasRunFailedListeners() && !definition.internalAgentWorkflow && !definition.internalAgentInvocationRecovery) try {
68
+ if (providerStep?.do) await providerStep.do("vitehub-notify-terminal-failure", { retries: { limit: 0 } }, async () => {
69
+ await emitWorkflowFailed(name, id, error, definition);
70
+ return null;
71
+ });
72
+ else await emitWorkflowFailed(name, id, error, definition);
73
+ } catch (notificationError) {
74
+ console.error("[vitehub] Could not checkpoint the Workflow failure notification.", notificationError);
75
+ }
82
76
  throw translateNonRetryableError(error, createNonRetryableError);
83
77
  }
84
78
  });
@@ -1,3 +1,3 @@
1
- import { a as setActiveCloudflareEnv, i as runWithActiveCloudflareEnv, n as CloudflareWorkerExecutionContext, r as createCloudflareRuntimeEvent, t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
2
- import "../cloudflare-shared-CaxrMD1A.js";
1
+ import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
2
+ import { i as setActiveCloudflareEnv, n as createCloudflareRuntimeEvent, r as runWithActiveCloudflareEnv, t as CloudflareWorkerExecutionContext } from "../cloudflare-shared-CTqh_a2s.js";
3
3
  export { type CloudflareWorkerEnv, type CloudflareWorkerExecutionContext, createCloudflareRuntimeEvent, runWithActiveCloudflareEnv, setActiveCloudflareEnv };
@@ -1,2 +1,2 @@
1
- import { a as setActiveCloudflareEnv, i as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-BNrYo29x.js";
1
+ import { i as setActiveCloudflareEnv, r as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-CGLx7lUd.js";
2
2
  export { createCloudflareRuntimeEvent, runWithActiveCloudflareEnv, setActiveCloudflareEnv };
@@ -1,6 +1,7 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-D82WmfZa.js";
1
+ import { l as ResolvedWorkflowOptions, m as WorkflowDefinitionRegistry } from "../types-Dsmwsdlv.js";
2
+ import { t as CloudflareWorkerEnv } from "../cloudflare-env-lookup-CfX6wZKb.js";
3
+ import { t as CloudflareWorkerExecutionContext } from "../cloudflare-shared-CTqh_a2s.js";
2
4
  import { t as VitehubApp } from "../app-BojnC_yf.js";
3
- import { n as CloudflareWorkerExecutionContext, t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
4
5
  //#region src/runtime/cloudflare-vite.d.ts
5
6
  export type CloudflareWorkerApp = VitehubApp;
6
7
  export interface WorkflowCloudflareWorkerOptions {
@@ -1,2 +1,48 @@
1
- import { n as installWorkflowCloudflareRuntime, t as createWorkflowCloudflareWorker } from "../cloudflare-vite-B3IabBsv.js";
1
+ import { r as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-CGLx7lUd.js";
2
+ import { t as normalizeWorkflowOptions } from "../config-C3DvtjfJ.js";
3
+ import { runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
4
+ import { t as resolveAppFetch } from "../app-He0GTrYZ.js";
5
+ import "./cloudflare-shared.js";
6
+ //#region ../internal/src/runtime/cloudflare-hosted.ts
7
+ function createWebRequestHandler(appHandler) {
8
+ return async (request, context) => await Promise.resolve(appHandler ? appHandler(request, context) : new Response(JSON.stringify({
9
+ status: 404,
10
+ message: `Cannot find any route matching [${request.method}] ${request.url}`
11
+ }), {
12
+ headers: { "content-type": "application/json;charset=UTF-8" },
13
+ status: 404,
14
+ statusText: "Not Found"
15
+ }));
16
+ }
17
+ function createCloudflareHostedWorker(adapter) {
18
+ const requestHandler = createWebRequestHandler(resolveAppFetch(adapter.label, adapter.app));
19
+ return { async fetch(request, env, context) {
20
+ const handle = async (handlerContext) => await Promise.resolve(requestHandler(request, handlerContext));
21
+ if (adapter.onRequest) return await adapter.onRequest({
22
+ env,
23
+ executionContext: context,
24
+ handle
25
+ });
26
+ return await handle();
27
+ } };
28
+ }
29
+ //#endregion
30
+ //#region src/runtime/cloudflare-vite.ts
31
+ function installWorkflowCloudflareRuntime(options = {}) {
32
+ const workflowConfig = options.workflow === false ? false : normalizeWorkflowOptions(options.workflow, { hosting: "cloudflare" });
33
+ setWorkflowRuntimeConfig(workflowConfig);
34
+ setWorkflowRuntimeRegistry(options.registry);
35
+ }
36
+ function createWorkflowCloudflareWorker(options = {}) {
37
+ return createCloudflareHostedWorker({
38
+ app: options.app,
39
+ label: "workflow",
40
+ async onRequest({ env, executionContext, handle }) {
41
+ installWorkflowCloudflareRuntime(options);
42
+ const runtimeEvent = createCloudflareRuntimeEvent(env, executionContext);
43
+ return await runWithActiveCloudflareEnv(env, () => runWithWorkflowRuntimeEvent(runtimeEvent, () => handle(runtimeEvent.context)));
44
+ }
45
+ });
46
+ }
47
+ //#endregion
2
48
  export { createWorkflowCloudflareWorker, installWorkflowCloudflareRuntime };
@@ -0,0 +1,4 @@
1
+ //#region src/runtime/dev-runs.d.ts
2
+ /** Turns on the run history. Only the dev Nitro plugin calls it. A second call keeps the history. */
3
+ export declare function enableWorkflowDevRuns(): void;
4
+ //#endregion
@@ -0,0 +1,73 @@
1
+ import { n as setWorkflowDevRecorder, t as getWorkflowDevRecorder } from "../dev-recorder-CTN5Fkao.js";
2
+ import { AsyncLocalStorage } from "node:async_hooks";
3
+ //#region src/runtime/dev-runs.ts
4
+ function createWorkflowDevRecorder() {
5
+ const runs = /* @__PURE__ */ new Map();
6
+ const activeRun = new AsyncLocalStorage();
7
+ return {
8
+ clear() {
9
+ const count = runs.size;
10
+ runs.clear();
11
+ return count;
12
+ },
13
+ list: () => [...runs.values()].reverse(),
14
+ start(workflow, id, provider) {
15
+ const key = JSON.stringify([workflow, id]);
16
+ const record = {
17
+ createdAt: /* @__PURE__ */ new Date(),
18
+ id,
19
+ provider,
20
+ status: "queued",
21
+ steps: [],
22
+ workflow
23
+ };
24
+ runs.delete(key);
25
+ runs.set(key, record);
26
+ while (runs.size > 200) runs.delete(runs.keys().next().value);
27
+ return async (run) => {
28
+ record.startedAt = /* @__PURE__ */ new Date();
29
+ record.status = "running";
30
+ try {
31
+ const result = await activeRun.run(record, run);
32
+ record.result = result;
33
+ record.status = "completed";
34
+ return result;
35
+ } catch (error) {
36
+ record.error = error;
37
+ record.status = "failed";
38
+ throw error;
39
+ } finally {
40
+ record.completedAt = /* @__PURE__ */ new Date();
41
+ }
42
+ };
43
+ },
44
+ async step(name, run) {
45
+ const record = activeRun.getStore();
46
+ if (!record || name === record.workflow) return await run();
47
+ const step = {
48
+ name,
49
+ startedAt: /* @__PURE__ */ new Date(),
50
+ status: "running"
51
+ };
52
+ record.steps.push(step);
53
+ try {
54
+ const output = await run();
55
+ step.output = output;
56
+ step.status = "completed";
57
+ return output;
58
+ } catch (error) {
59
+ step.error = error;
60
+ step.status = "failed";
61
+ throw error;
62
+ } finally {
63
+ step.completedAt = /* @__PURE__ */ new Date();
64
+ }
65
+ }
66
+ };
67
+ }
68
+ /** Turns on the run history. Only the dev Nitro plugin calls it. A second call keeps the history. */
69
+ function enableWorkflowDevRuns() {
70
+ if (!getWorkflowDevRecorder()) setWorkflowDevRecorder(createWorkflowDevRecorder());
71
+ }
72
+ //#endregion
73
+ export { enableWorkflowDevRuns };
@@ -1,12 +1,49 @@
1
- import { M as WorkflowStepOptions, S as WorkflowProviderContext, j as WorkflowStepFunction, p as WorkflowDefinition, w as WorkflowProviderStep } from "../types-D82WmfZa.js";
2
- import { SerializedResponse } from "@vite-hub/runtime";
1
+ import { E as WorkflowProviderContext, H as WorkflowStepContext, K as WorkflowStepRetries, U as WorkflowStepFunction, W as WorkflowStepOptions, _ as WorkflowDuration, f as WorkflowDefinition } from "../types-Dsmwsdlv.js";
2
+ import { t as NonRetryableError } from "../errors-uhP8DZTi.js";
3
3
  //#region src/runtime/execute.d.ts
4
- export declare function runProviderStep<TResult>(step: WorkflowProviderStep | undefined, name: string, run: () => TResult | Promise<TResult>, options?: WorkflowStepOptions): Promise<TResult>;
4
+ /** The options of a step with the default retries filled in. */
5
+ export interface ResolvedWorkflowStepOptions {
6
+ retries: Required<WorkflowStepRetries>;
7
+ timeout?: WorkflowDuration;
8
+ }
9
+ /**
10
+ * Checks the options of a step and fills in the default retries. A step file, `context.step.do()`, and every provider
11
+ * read the options through this function, so an invalid value fails the same way everywhere.
12
+ */
13
+ export declare function resolveWorkflowStepOptions(step: string, options: unknown): ResolvedWorkflowStepOptions;
14
+ /**
15
+ * Runs one attempt of a step. When `timeout` passes, the step's `signal` aborts and the attempt fails with
16
+ * `WORKFLOW_STEP_TIMEOUT`, so the provider retries it. A step that ignores the signal keeps running in the background:
17
+ * JavaScript cannot stop a promise.
18
+ */
19
+ export declare function runWorkflowStepAttempt<TResult>(step: string, timeout: WorkflowDuration | undefined, run: (context: WorkflowStepContext) => TResult | Promise<TResult>): Promise<TResult>;
20
+ /** Adds a timeout once, including when a provider receives an already wrapped folder step. */
21
+ export declare function withWorkflowStepTimeout<TResult>(name: string, timeout: WorkflowDuration | undefined, run: () => TResult | Promise<TResult>): () => TResult | Promise<TResult>;
22
+ export declare function runProviderStep<TResult>(step: WorkflowProviderContext["step"], name: string, run: (context: WorkflowStepContext) => TResult | Promise<TResult>, options?: WorkflowStepOptions): Promise<TResult>;
23
+ /** `01.create-user.ts` becomes the `createUser` step. */
24
+ export declare function toStepPropertyName(file: string): string;
5
25
  export declare function createWorkflowSteps(context: WorkflowProviderContext, stepModules: Array<{
6
26
  name: string;
27
+ options?: WorkflowStepOptions;
7
28
  run: WorkflowStepFunction;
8
- }>): Record<string, WorkflowStepFunction>;
29
+ }>): Record<string, (input: unknown) => Promise<unknown>>;
30
+ /** The milliseconds of a `WorkflowDuration`: a number of 0 or more, or a string such as `'30 seconds'`. */
31
+ export declare function toDurationMilliseconds(value: unknown): number | undefined;
32
+ /**
33
+ * The step result that says the step threw a `NonRetryableError`. A provider that retries every error, such as
34
+ * OpenWorkflow, ends the step with it, so it does not run the step again. The caller throws the error outside the
35
+ * step, and a replay reads the stored result and throws the same error.
36
+ */
37
+ export interface NonRetryableStepResult {
38
+ "~vitehubNonRetryableError": {
39
+ code?: string;
40
+ message: string;
41
+ name: string;
42
+ };
43
+ }
44
+ export declare function toNonRetryableStepResult(error: unknown): NonRetryableStepResult | undefined;
45
+ export declare function readNonRetryableStepResult(value: unknown): NonRetryableError | undefined;
9
46
  export declare function runWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<Response>;
10
47
  /** Encode inside the root step so both checkpoints and final outputs survive replay. */
11
- export declare function runSerializedWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<SerializedResponse>;
48
+ export declare function runSerializedWorkflowHandler<TPayload, TResult>(context: WorkflowProviderContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<unknown>;
12
49
  //#endregion