@vite-hub/workflow 0.0.3 → 0.0.4

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/dist/index.js CHANGED
@@ -1,12 +1,13 @@
1
- import { t as normalizeWorkflowOptions } from "./config-BDqB6I4E.js";
2
- import { a as encodeNameHex, i as getCloudflareWorkflowName, n as getCloudflareWorkflowBindingName, r as getCloudflareWorkflowClassName } from "./cloudflare-Cds1VIWa.js";
3
- import { i as createWorkflowError, n as runWorkflowProviderOperation, r as safeWorkflowName } from "./provider-operation-CWIV4k42.js";
4
- import { n as getCloudflareEnv, r as resolveWaitUntil } from "./cloudflare-env-mFp2Uj6Z.js";
1
+ import { t as normalizeWorkflowOptions } from "./config-BHknO_rT.js";
2
+ import { a as encodeNameHex, i as getCloudflareWorkflowName, n as getCloudflareWorkflowBindingName, r as getCloudflareWorkflowClassName } from "./cloudflare-D0Di-QYL.js";
3
+ import { t as hasRuntimeType } from "./runtime-type-DGUn8Ple.js";
4
+ import { a as createWorkflowError, i as safeWorkflowName, r as runWorkflowProviderOperation } from "./provider-operation-CW2cYfVy.js";
5
+ import { n as getCloudflareEnv, r as resolveWaitUntil } from "./cloudflare-env-DjxQNbrx.js";
5
6
  import { runWorkflowHandler } from "./runtime/execute.js";
6
7
  import { getOpenWorkflowRun, runOpenWorkflow } from "./runtime/openworkflow.js";
7
- import { getWorkflowRunState, getWorkflowRuntimeConfig, getWorkflowRuntimeEvent, loadWorkflowDefinition, registerInlineWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRun } from "./runtime/state.js";
8
- import { a as startVercelWorkflow, n as inspectVercelWorkflowRun, r as resumeVercelWorkflowSignal, t as cancelVercelWorkflow } from "./vercel-D2_J5D_Y.js";
9
- import { t as createWorkflowCloudflareWorker } from "./cloudflare-vite-B0zel2Do.js";
8
+ import { getWorkflowRunState, getWorkflowRuntimeConfig, getWorkflowRuntimeEvent, getWorkflowRuntimeRegistry, loadWorkflowDefinition, registerInlineWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRun } from "./runtime/state.js";
9
+ import { a as startVercelWorkflow, n as inspectVercelWorkflowRun, r as resumeVercelWorkflowSignal, t as cancelVercelWorkflow } from "./vercel-B0MuIVgJ.js";
10
+ import { t as createWorkflowCloudflareWorker } from "./cloudflare-vite-BkDgB6Sr.js";
10
11
  //#region src/definition.ts
11
12
  function defineWorkflow(handler, options) {
12
13
  if (typeof handler !== "function") throw new TypeError("`defineWorkflow()` requires a workflow handler.");
@@ -30,6 +31,15 @@ function randomId(prefix) {
30
31
  }
31
32
  //#endregion
32
33
  //#region src/runtime/adapters.ts
34
+ function resolveSettlementObserver(event) {
35
+ if (!event || !hasRuntimeType(event, "object")) return void 0;
36
+ const candidate = "settled" in event ? event.settled : void 0;
37
+ if (hasRuntimeType(candidate, "function")) return candidate;
38
+ const context = "context" in event ? event.context : void 0;
39
+ if (!context || !hasRuntimeType(context, "object")) return void 0;
40
+ const nested = "settled" in context ? context.settled : void 0;
41
+ return hasRuntimeType(nested, "function") ? nested : void 0;
42
+ }
33
43
  function unsupportedOperation(provider, operation) {
34
44
  throw createWorkflowError({
35
45
  code: "WORKFLOW_OPERATION_UNSUPPORTED",
@@ -39,9 +49,13 @@ function unsupportedOperation(provider, operation) {
39
49
  }
40
50
  });
41
51
  }
42
- function resolveCloudflareBinding(event, binding, name) {
43
- const bindingName = binding || getCloudflareWorkflowBindingName(name);
44
- return getCloudflareEnv(event)?.[bindingName];
52
+ function resolveCloudflareBinding(event, binding, name, definition) {
53
+ const env = getCloudflareEnv(event);
54
+ const bindingName = definition?.internalAgentInvocationRecovery ? getCloudflareWorkflowBindingName(name) : binding || getCloudflareWorkflowBindingName(name);
55
+ return env?.[bindingName];
56
+ }
57
+ function resolveCloudflareInspectionBinding(event, binding, name) {
58
+ return resolveCloudflareBinding(event, binding, name, getWorkflowRuntimeRegistry()?.[name]);
45
59
  }
46
60
  const cloudflareStatusMap = {
47
61
  cancelled: "cancelled",
@@ -55,14 +69,19 @@ const cloudflareStatusMap = {
55
69
  terminated: "failed"
56
70
  };
57
71
  function normalizeCloudflareStatus(status) {
58
- const value = typeof status === "object" && status ? status.status : status;
72
+ const value = hasRuntimeType(status, "object") && status ? status.status : status;
59
73
  return cloudflareStatusMap[String(value || "").toLowerCase()] || "unknown";
60
74
  }
75
+ function hasUnknownWorkflowAcknowledgement(error) {
76
+ if (!error || !hasRuntimeType(error, "object")) return false;
77
+ const details = error.details;
78
+ return Boolean(details && hasRuntimeType(details, "object") && details.acknowledgement === "unknown");
79
+ }
61
80
  function createCloudflareAdapter(config) {
62
81
  return {
63
82
  cancel: () => unsupportedOperation("cloudflare", "cancellation"),
64
83
  async get({ event, id, name }) {
65
- const binding = resolveCloudflareBinding(event, config.binding, name);
84
+ const binding = resolveCloudflareInspectionBinding(event, config.binding, name);
66
85
  if (binding) {
67
86
  const instance = await runWorkflowProviderOperation("cloudflare", "get", () => binding.get(id));
68
87
  const metadata = await runWorkflowProviderOperation("cloudflare", "status", () => instance.status());
@@ -80,18 +99,24 @@ function createCloudflareAdapter(config) {
80
99
  });
81
100
  },
82
101
  async run({ definition, event, id, name, options, payload }) {
83
- const binding = resolveCloudflareBinding(event, config.binding, name);
102
+ const binding = resolveCloudflareBinding(event, config.binding, name, definition);
84
103
  if (binding) {
85
- const start = runWorkflowProviderOperation("cloudflare", "create", () => binding.create({
104
+ const start = () => runWorkflowProviderOperation("cloudflare", "create", async () => (await binding.createBatch([{
86
105
  id,
87
106
  params: payload
88
- }));
107
+ }]))[0] || await binding.get(id), { acknowledgementUnknown: (_error, status) => status === void 0 });
108
+ const creation = start().catch(async (firstError) => {
109
+ try {
110
+ return await start();
111
+ } catch (retryError) {
112
+ if (hasUnknownWorkflowAcknowledgement(firstError)) throw firstError;
113
+ throw retryError;
114
+ }
115
+ });
89
116
  const waitUntil = options.deferred ? resolveWaitUntil(event) : void 0;
90
- if (waitUntil) waitUntil(start);
91
- const instance = await start;
117
+ if (waitUntil) waitUntil(creation);
92
118
  return {
93
- id: instance.id,
94
- metadata: await runWorkflowProviderOperation("cloudflare", "status", () => instance.status()),
119
+ id: (await creation).id,
95
120
  payload,
96
121
  provider: "cloudflare",
97
122
  status: "queued"
@@ -208,7 +233,7 @@ function createVercelAdapter(config) {
208
233
  provider: "vercel"
209
234
  }
210
235
  });
211
- return await startVercelWorkflow(name, definition, payload);
236
+ return await startVercelWorkflow(name, definition, payload, resolveSettlementObserver(event));
212
237
  }
213
238
  };
214
239
  }
@@ -234,7 +259,7 @@ async function loadRequiredWorkflowDefinition(name) {
234
259
  }
235
260
  function normalizeWorkflowRunIdValue(value) {
236
261
  if (value === void 0) return null;
237
- if (value === null || typeof value !== "object") return value;
262
+ if (value === null || !hasRuntimeType(value, "object")) return value;
238
263
  if (Array.isArray(value)) return value.map((item) => normalizeWorkflowRunIdValue(item));
239
264
  const record = value;
240
265
  return Object.fromEntries(Object.keys(record).filter((key) => record[key] !== void 0).sort().map((key) => [key, normalizeWorkflowRunIdValue(record[key])]));
@@ -259,34 +284,44 @@ async function resolveWorkflowStartOptions(name, payload, createOptions, startOp
259
284
  };
260
285
  }
261
286
  function createWorkflow(nameOrOptions, handlerOrOptions, options) {
262
- if (typeof nameOrOptions === "object" && nameOrOptions !== null) {
287
+ if (hasRuntimeType(nameOrOptions, "object") && nameOrOptions !== null) {
263
288
  const createOptions = nameOrOptions;
264
289
  const { handler, name } = createOptions;
265
- if (!name || typeof name !== "string") throw new TypeError("`createWorkflow()` requires a workflow name.");
266
- if (typeof handler !== "function") throw new TypeError("`createWorkflow()` requires a workflow handler.");
267
- registerInlineWorkflowDefinition(name, { handler });
290
+ if (!name || !hasRuntimeType(name, "string")) throw new TypeError("`createWorkflow()` requires a workflow name.");
291
+ if (!hasRuntimeType(handler, "function")) throw new TypeError("`createWorkflow()` requires a workflow handler.");
292
+ registerInlineWorkflowDefinition(name, {
293
+ handler,
294
+ ...createOptions.rootStep === void 0 ? {} : { options: { rootStep: createOptions.rootStep } }
295
+ });
268
296
  return {
269
297
  cancel: (id) => cancelWorkflow(name, id),
270
298
  name,
271
299
  defer: async (payload, options = {}) => deferWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options)),
272
300
  getRun: (id) => getWorkflowRun(name, id),
273
- run: async (payload, options = {}) => runWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options))
301
+ async run(payload, options = {}) {
302
+ return await runWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options));
303
+ }
274
304
  };
275
305
  }
276
306
  const name = nameOrOptions;
277
- if (!name || typeof name !== "string") throw new TypeError("`createWorkflow()` requires a workflow name.");
278
- const handler = typeof handlerOrOptions === "function" ? handlerOrOptions : typeof handlerOrOptions === "object" && handlerOrOptions !== null ? handlerOrOptions.handler : void 0;
279
- const createOptions = typeof handlerOrOptions === "function" ? options : handlerOrOptions;
307
+ if (!name || !hasRuntimeType(name, "string")) throw new TypeError("`createWorkflow()` requires a workflow name.");
308
+ const handler = hasRuntimeType(handlerOrOptions, "function") ? handlerOrOptions : hasRuntimeType(handlerOrOptions, "object") && handlerOrOptions !== null ? handlerOrOptions.handler : void 0;
309
+ const createOptions = hasRuntimeType(handlerOrOptions, "function") ? options : handlerOrOptions;
280
310
  if (handler !== void 0) {
281
- if (typeof handler !== "function") throw new TypeError("`createWorkflow()` requires a workflow handler.");
282
- registerInlineWorkflowDefinition(name, { handler });
283
- } else if (handlerOrOptions !== void 0 && (typeof handlerOrOptions !== "object" || handlerOrOptions === null)) throw new TypeError("`createWorkflow()` options must be an object.");
311
+ if (!hasRuntimeType(handler, "function")) throw new TypeError("`createWorkflow()` requires a workflow handler.");
312
+ registerInlineWorkflowDefinition(name, {
313
+ handler,
314
+ ...createOptions?.rootStep === void 0 ? {} : { options: { rootStep: createOptions.rootStep } }
315
+ });
316
+ } else if (handlerOrOptions !== void 0 && (!hasRuntimeType(handlerOrOptions, "object") || handlerOrOptions === null)) throw new TypeError("`createWorkflow()` options must be an object.");
284
317
  return {
285
318
  cancel: (id) => cancelWorkflow(name, id),
286
319
  name,
287
320
  defer: async (payload, options = {}) => deferWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options)),
288
321
  getRun: (id) => getWorkflowRun(name, id),
289
- run: async (payload, options = {}) => runWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options))
322
+ async run(payload, options = {}) {
323
+ return await runWorkflow(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options));
324
+ }
290
325
  };
291
326
  }
292
327
  async function runWorkflow(name, payload, options = {}) {
@@ -323,7 +358,7 @@ async function cancelWorkflow(name, id) {
323
358
  });
324
359
  }
325
360
  async function resumeWorkflowSignal(token, payload) {
326
- if (!token || typeof token !== "string") throw new TypeError("`resumeWorkflowSignal()` requires an opaque signal token.");
361
+ if (!token || !hasRuntimeType(token, "string")) throw new TypeError("`resumeWorkflowSignal()` requires an opaque signal token.");
327
362
  const config = getActiveWorkflowConfig();
328
363
  if (config === false) throw createWorkflowError({ code: "WORKFLOW_DISABLED" });
329
364
  return await getWorkflowRuntimeAdapter(config).resume(token, payload);
@@ -27,7 +27,7 @@ function isWorkflowBoundaryError(error) {
27
27
  return false;
28
28
  }
29
29
  }
30
- function getProviderStatus(error) {
30
+ function getWorkflowProviderStatus(error) {
31
31
  if (typeof error !== "object" || error === null) return void 0;
32
32
  try {
33
33
  const value = error;
@@ -38,20 +38,23 @@ function getProviderStatus(error) {
38
38
  return;
39
39
  }
40
40
  }
41
- async function runWorkflowProviderOperation(provider, operation, run) {
41
+ async function runWorkflowProviderOperation(provider, operation, run, options = {}) {
42
42
  try {
43
43
  return await run();
44
44
  } catch (error) {
45
- if (isWorkflowBoundaryError(error)) throw error;
46
- const status = getProviderStatus(error);
45
+ if (options.boundaryError?.(error) || isWorkflowBoundaryError(error)) throw error;
46
+ const status = getWorkflowProviderStatus(error);
47
+ const acknowledgement = options.acknowledgementUnknown?.(error, status) === true ? "unknown" : void 0;
48
+ const details = {
49
+ operation,
50
+ provider
51
+ };
52
+ if (acknowledgement) details.acknowledgement = acknowledgement;
53
+ if (status !== void 0) details.status = status;
47
54
  throw createWorkflowError({
48
55
  cause: error,
49
56
  code: "WORKFLOW_PROVIDER_OPERATION_FAILED",
50
- details: {
51
- operation,
52
- provider,
53
- ...status === void 0 ? {} : { status }
54
- }
57
+ details
55
58
  });
56
59
  }
57
60
  }
@@ -59,4 +62,4 @@ function safeWorkflowName(name) {
59
62
  return /^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$/.test(name) ? name : void 0;
60
63
  }
61
64
  //#endregion
62
- export { createWorkflowError as i, runWorkflowProviderOperation as n, safeWorkflowName as r, isWorkflowBoundaryError as t };
65
+ export { createWorkflowError as a, safeWorkflowName as i, isWorkflowBoundaryError as n, runWorkflowProviderOperation as r, getWorkflowProviderStatus as t };
@@ -1,4 +1,4 @@
1
- import { C as WorkflowProviderStep, h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-BUlYBZwH.js";
1
+ import { C as WorkflowProviderStep, h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-B9RdRkKh.js";
2
2
  import { t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
3
3
 
4
4
  //#region src/runtime/cloudflare-runner.d.ts
@@ -9,6 +9,9 @@ interface CloudflareWorkflowEvent {
9
9
  }
10
10
  interface RunCloudflareWorkflowOptions {
11
11
  config: false | ResolvedWorkflowOptions | undefined;
12
+ createNonRetryableError?: (error: Error & {
13
+ isRetryable: false;
14
+ }) => Error;
12
15
  env: CloudflareWorkerEnv;
13
16
  event: CloudflareWorkflowEvent;
14
17
  name: string;
@@ -17,6 +20,7 @@ interface RunCloudflareWorkflowOptions {
17
20
  }
18
21
  declare function runCloudflareWorkflow({
19
22
  config,
23
+ createNonRetryableError,
20
24
  env,
21
25
  event,
22
26
  name,
@@ -1,23 +1,51 @@
1
- import { i as runWithActiveCloudflareEnv } from "../cloudflare-env-mFp2Uj6Z.js";
1
+ import { i as runWithActiveCloudflareEnv } from "../cloudflare-env-DjxQNbrx.js";
2
2
  import { runWorkflowHandler } from "./execute.js";
3
3
  import { loadWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
4
4
  //#region src/runtime/cloudflare-runner.ts
5
- async function runCloudflareWorkflow({ config, env, event, name, registry, step }) {
5
+ function isNonRetryableError(error) {
6
+ return error instanceof Error && error.isRetryable === false;
7
+ }
8
+ function translateNonRetryableError(error, createNonRetryableError) {
9
+ return createNonRetryableError && isNonRetryableError(error) ? createNonRetryableError(error) : error;
10
+ }
11
+ function wrapCloudflareWorkflowStep(step, createNonRetryableError, env, runtimeEvent) {
12
+ if (!step?.do) return step;
13
+ return {
14
+ async do(name, options, run) {
15
+ return await step.do(name, options, async () => await runWithActiveCloudflareEnv(env, async () => await runWithWorkflowRuntimeEvent(runtimeEvent(), async () => {
16
+ try {
17
+ return await run();
18
+ } catch (error) {
19
+ throw translateNonRetryableError(error, createNonRetryableError);
20
+ }
21
+ })));
22
+ },
23
+ ...step.sleep ? { sleep: step.sleep.bind(step) } : {}
24
+ };
25
+ }
26
+ async function runCloudflareWorkflow({ config, createNonRetryableError, env, event, name, registry, step }) {
6
27
  setWorkflowRuntimeConfig(config);
7
28
  setWorkflowRuntimeRegistry(registry);
29
+ let runtimeEvent;
30
+ const providerStep = wrapCloudflareWorkflowStep(step, createNonRetryableError, env, () => runtimeEvent);
31
+ runtimeEvent = {
32
+ env,
33
+ step: providerStep
34
+ };
8
35
  return await runWithActiveCloudflareEnv(env, async () => {
9
36
  const definition = await loadWorkflowDefinition(name);
10
37
  if (!definition) throw new Error(`Missing workflow definition: ${name}`);
11
- return await runWithWorkflowRuntimeEvent({
12
- env,
13
- step
14
- }, () => runWorkflowHandler({
15
- id: event?.instanceId || event?.id,
16
- name,
17
- payload: event?.payload,
18
- provider: "cloudflare",
19
- step
20
- }, definition));
38
+ try {
39
+ return await runWithWorkflowRuntimeEvent(runtimeEvent, () => runWorkflowHandler({
40
+ id: event?.instanceId || event?.id,
41
+ name,
42
+ payload: event?.payload,
43
+ provider: "cloudflare",
44
+ step: providerStep
45
+ }, definition));
46
+ } catch (error) {
47
+ throw translateNonRetryableError(error, createNonRetryableError);
48
+ }
21
49
  });
22
50
  }
23
51
  //#endregion
@@ -1,2 +1,2 @@
1
- import { a as setActiveCloudflareEnv, i as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-mFp2Uj6Z.js";
1
+ import { a as setActiveCloudflareEnv, i as runWithActiveCloudflareEnv, t as createCloudflareRuntimeEvent } from "../cloudflare-env-DjxQNbrx.js";
2
2
  export { createCloudflareRuntimeEvent, runWithActiveCloudflareEnv, setActiveCloudflareEnv };
@@ -1,4 +1,4 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-BUlYBZwH.js";
1
+ import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-B9RdRkKh.js";
2
2
  import { t as WorkflowApp } from "../_app-DgwN4ulW.js";
3
3
  import { n as CloudflareWorkerExecutionContext, t as CloudflareWorkerEnv } from "../cloudflare-env-CIjdFTv6.js";
4
4
 
@@ -12,6 +12,7 @@ interface WorkflowCloudflareWorkerOptions {
12
12
  interface WorkflowCloudflareWorker {
13
13
  fetch: (request: Request, env: CloudflareWorkerEnv, context: CloudflareWorkerExecutionContext) => Promise<Response>;
14
14
  }
15
+ declare function installWorkflowCloudflareRuntime(options?: WorkflowCloudflareWorkerOptions): void;
15
16
  declare function createWorkflowCloudflareWorker(options?: WorkflowCloudflareWorkerOptions): WorkflowCloudflareWorker;
16
17
  //#endregion
17
- export { CloudflareWorkerApp, WorkflowCloudflareWorker, WorkflowCloudflareWorkerOptions, createWorkflowCloudflareWorker };
18
+ export { CloudflareWorkerApp, WorkflowCloudflareWorker, WorkflowCloudflareWorkerOptions, createWorkflowCloudflareWorker, installWorkflowCloudflareRuntime };
@@ -1,2 +1,2 @@
1
- import { t as createWorkflowCloudflareWorker } from "../cloudflare-vite-B0zel2Do.js";
2
- export { createWorkflowCloudflareWorker };
1
+ import { n as installWorkflowCloudflareRuntime, t as createWorkflowCloudflareWorker } from "../cloudflare-vite-BkDgB6Sr.js";
2
+ export { createWorkflowCloudflareWorker, installWorkflowCloudflareRuntime };
@@ -1,4 +1,4 @@
1
- import { A as WorkflowStepFunction, C as WorkflowProviderStep, g as WorkflowExecutionContext, j as WorkflowStepOptions, p as WorkflowDefinition } from "../types-BUlYBZwH.js";
1
+ import { A as WorkflowStepFunction, C as WorkflowProviderStep, g as WorkflowExecutionContext, j as WorkflowStepOptions, p as WorkflowDefinition } from "../types-B9RdRkKh.js";
2
2
 
3
3
  //#region src/runtime/execute.d.ts
4
4
  declare function runProviderStep<TResult>(step: WorkflowProviderStep | undefined, name: string, run: () => TResult | Promise<TResult>, options?: WorkflowStepOptions): Promise<TResult>;
@@ -1,4 +1,4 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-BUlYBZwH.js";
1
+ import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-B9RdRkKh.js";
2
2
  import { getOpenWorkflowRuntime } from "./openworkflow.js";
3
3
  import { ViteHubError } from "@vite-hub/runtime";
4
4
 
@@ -1,4 +1,5 @@
1
- import { i as createWorkflowError } from "../provider-operation-CWIV4k42.js";
1
+ import { n as isRuntimeRecord, t as hasRuntimeType } from "../runtime-type-DGUn8Ple.js";
2
+ import { a as createWorkflowError, r as runWorkflowProviderOperation } from "../provider-operation-CW2cYfVy.js";
2
3
  import { getOpenWorkflowRuntime, registerOpenWorkflowDefinition } from "./openworkflow.js";
3
4
  import { getInlineWorkflowDefinitions, getWorkflowRuntimeConfig, getWorkflowRuntimeRegistry, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry, takeInlineWorkflowDefinitionForModule } from "./state.js";
4
5
  import "@vite-hub/runtime";
@@ -6,12 +7,15 @@ import "@vite-hub/runtime";
6
7
  function getProcess() {
7
8
  return globalThis.process;
8
9
  }
10
+ function isWorkflowDefinition(value) {
11
+ return isRuntimeRecord(value) && hasRuntimeType(value.handler, "function");
12
+ }
9
13
  async function loadRegistryDefinitions(registry) {
10
14
  const definitions = /* @__PURE__ */ new Map();
11
15
  for (const name of Object.keys(registry).sort()) {
12
16
  const loaded = await registry[name]?.();
13
- const definition = loaded && typeof loaded === "object" && "default" in loaded ? loaded.default : loaded;
14
- if (definition && typeof definition === "object" && "handler" in definition && typeof definition.handler === "function") {
17
+ const definition = isRuntimeRecord(loaded) && "default" in loaded ? loaded.default : loaded;
18
+ if (isWorkflowDefinition(definition)) {
15
19
  definitions.set(name, definition);
16
20
  continue;
17
21
  }
@@ -41,7 +45,39 @@ async function createOpenWorkflowWorker(options = {}) {
41
45
  }
42
46
  async function startOpenWorkflowWorker(options = {}) {
43
47
  const worker = await createOpenWorkflowWorker(options);
44
- await worker.start();
48
+ await runWorkflowProviderOperation("openworkflow", "start", async () => {
49
+ let removeStartupAbortListener;
50
+ let startTask;
51
+ try {
52
+ if (options.signal?.aborted) throw options.signal.reason;
53
+ startTask = Promise.resolve(worker.start());
54
+ if (!options.signal) await startTask;
55
+ else {
56
+ const signal = options.signal;
57
+ const abortTask = new Promise((_resolve, reject) => {
58
+ const abort = () => reject(signal.reason);
59
+ removeStartupAbortListener = () => signal.removeEventListener("abort", abort);
60
+ signal.addEventListener("abort", abort, { once: true });
61
+ if (signal.aborted) abort();
62
+ });
63
+ await Promise.race([startTask, abortTask]);
64
+ }
65
+ } catch (startError) {
66
+ const stopTask = Promise.resolve().then(() => worker.stop());
67
+ if (startTask && options.signal?.aborted) startTask.then(async () => {
68
+ await stopTask.catch(() => {});
69
+ await worker.stop();
70
+ }).catch(() => {});
71
+ try {
72
+ await stopTask;
73
+ } catch (stopError) {
74
+ throw new AggregateError([startError, stopError], "OpenWorkflow worker startup and cleanup failed.");
75
+ }
76
+ throw startError;
77
+ } finally {
78
+ removeStartupAbortListener?.();
79
+ }
80
+ }, { boundaryError: (error) => options.signal?.aborted === true && error === options.signal.reason });
45
81
  const originalStop = worker.stop.bind(worker);
46
82
  const proc = getProcess();
47
83
  let abortListenerActive = false;
@@ -1,4 +1,4 @@
1
- import { f as WorkflowDeferOptions, l as ResolvedWorkflowOptions, p as WorkflowDefinition, w as WorkflowRun } from "../types-BUlYBZwH.js";
1
+ import { f as WorkflowDeferOptions, l as ResolvedWorkflowOptions, p as WorkflowDefinition, w as WorkflowRun } from "../types-B9RdRkKh.js";
2
2
 
3
3
  //#region src/runtime/openworkflow.d.ts
4
4
  type OpenWorkflowModule = typeof import("openworkflow");
@@ -1,4 +1,4 @@
1
- import { i as createWorkflowError, n as runWorkflowProviderOperation } from "../provider-operation-CWIV4k42.js";
1
+ import { a as createWorkflowError, r as runWorkflowProviderOperation, t as getWorkflowProviderStatus } from "../provider-operation-CW2cYfVy.js";
2
2
  import { runWorkflowHandler } from "./execute.js";
3
3
  //#region src/runtime/openworkflow.ts
4
4
  const defaultImporter = new Function("specifier", "return import(specifier)");
@@ -117,16 +117,20 @@ function toOpenWorkflowRetryPolicy(options) {
117
117
  };
118
118
  }
119
119
  function createOpenWorkflowProviderStep(step) {
120
- return { async do(name, options, run) {
121
- return await step.run({
122
- name,
123
- ...toOpenWorkflowRetryPolicy(options) ? { retryPolicy: toOpenWorkflowRetryPolicy(options) } : {}
124
- }, run);
125
- } };
120
+ return {
121
+ async do(name, options, run) {
122
+ return await step.run({
123
+ name,
124
+ ...toOpenWorkflowRetryPolicy(options) ? { retryPolicy: toOpenWorkflowRetryPolicy(options) } : {}
125
+ }, run);
126
+ },
127
+ sleep: async (name, duration) => await step.sleep(name, duration)
128
+ };
126
129
  }
127
130
  async function registerOpenWorkflowDefinition(runtime, name, definition) {
128
131
  const existing = runtime.workflows.get(name);
129
132
  if (existing) return existing;
133
+ const openWorkflowDefinition = definition;
130
134
  const workflow = runtime.client.defineWorkflow({ name }, async ({ input, run, step }) => {
131
135
  return await runWorkflowHandler({
132
136
  id: run.id,
@@ -134,7 +138,7 @@ async function registerOpenWorkflowDefinition(runtime, name, definition) {
134
138
  payload: input,
135
139
  provider: "openworkflow",
136
140
  step: createOpenWorkflowProviderStep(step)
137
- }, definition);
141
+ }, openWorkflowDefinition);
138
142
  });
139
143
  runtime.workflows.set(name, workflow);
140
144
  return workflow;
@@ -143,7 +147,7 @@ function normalizeOpenWorkflowStatus(status) {
143
147
  switch (String(status || "").toLowerCase()) {
144
148
  case "completed":
145
149
  case "succeeded": return "completed";
146
- case "canceled":
150
+ case "canceled": return "cancelled";
147
151
  case "failed": return "failed";
148
152
  case "pending": return "queued";
149
153
  case "running":
@@ -157,6 +161,7 @@ function serializeOpenWorkflowRun(run, name) {
157
161
  provider: "openworkflow",
158
162
  status: "unknown"
159
163
  };
164
+ const result = run.output;
160
165
  return {
161
166
  id: run.id,
162
167
  metadata: run.error || {
@@ -165,23 +170,30 @@ function serializeOpenWorkflowRun(run, name) {
165
170
  workflowName: run.workflowName
166
171
  },
167
172
  provider: "openworkflow",
168
- result: run.output ?? void 0,
173
+ result,
169
174
  status: normalizeOpenWorkflowStatus(run.status)
170
175
  };
171
176
  }
172
177
  async function runOpenWorkflow(config, name, payload, definition, options) {
173
178
  const workflow = await registerOpenWorkflowDefinition(await getOpenWorkflowRuntime(config), name, definition);
179
+ let firstAcknowledgementUnknown = false;
180
+ const handle = await runWorkflowProviderOperation("openworkflow", "run", async () => {
181
+ const start = () => workflow.run(payload, options.id ? { idempotencyKey: options.id } : void 0);
182
+ return await start().catch((error) => {
183
+ if (!options.id) throw error;
184
+ firstAcknowledgementUnknown = getWorkflowProviderStatus(error) === void 0;
185
+ return start();
186
+ });
187
+ }, { acknowledgementUnknown: (_error, status) => firstAcknowledgementUnknown || status === void 0 });
174
188
  return await runWorkflowProviderOperation("openworkflow", "run", async () => {
175
- const handle = await workflow.run(payload, options.id ? { idempotencyKey: options.id } : void 0);
189
+ const serialized = serializeOpenWorkflowRun(handle.workflowRun, name);
176
190
  return {
177
- id: handle.workflowRun.id,
178
- metadata: {
191
+ ...serialized,
192
+ metadata: serialized.status === "failed" ? serialized.metadata : {
179
193
  ...options.id ? { idempotencyKey: options.id } : {},
180
194
  workflow: name
181
195
  },
182
- payload,
183
- provider: "openworkflow",
184
- status: normalizeOpenWorkflowStatus(handle.workflowRun.status)
196
+ payload
185
197
  };
186
198
  });
187
199
  }
@@ -1,4 +1,4 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions, p as WorkflowDefinition } from "../types-BUlYBZwH.js";
1
+ import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions, p as WorkflowDefinition } from "../types-B9RdRkKh.js";
2
2
 
3
3
  //#region src/runtime/state.d.ts
4
4
  interface WorkflowRunState<TResult = unknown> {
@@ -1,4 +1,4 @@
1
- import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-BUlYBZwH.js";
1
+ import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-B9RdRkKh.js";
2
2
  import { t as WorkflowApp } from "../_app-DgwN4ulW.js";
3
3
  import { IncomingMessage, ServerResponse } from "node:http";
4
4
 
@@ -58,6 +58,7 @@ interface VercelWorkflowRuntimeModule {
58
58
  declare function setVercelWorkflowRuntimeModules(api: VercelWorkflowApiModule, runtime: VercelWorkflowRuntimeModule): void;
59
59
  //#endregion
60
60
  //#region src/runtime/vercel-vite.d.ts
61
+ declare function runWithVercelWorkflowRuntimeEvent<T>(req: unknown, res: unknown, run: () => T | Promise<T>): Promise<T>;
61
62
  interface WorkflowVercelServerOptions {
62
63
  app?: WorkflowApp;
63
64
  registry?: WorkflowDefinitionRegistry;
@@ -66,4 +67,4 @@ interface WorkflowVercelServerOptions {
66
67
  type WorkflowVercelServer = (req: IncomingMessage, res: ServerResponse) => unknown;
67
68
  declare function createWorkflowVercelServer(options?: WorkflowVercelServerOptions): WorkflowVercelServer;
68
69
  //#endregion
69
- export { WorkflowVercelServer, createWorkflowVercelServer, setVercelWorkflowRuntimeModules };
70
+ export { WorkflowVercelServer, createWorkflowVercelServer, runWithVercelWorkflowRuntimeEvent, setVercelWorkflowRuntimeModules };
@@ -1,5 +1,5 @@
1
1
  import { runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
2
- import { i as setVercelWorkflowRuntimeModules } from "../vercel-D2_J5D_Y.js";
2
+ import { i as setVercelWorkflowRuntimeModules } from "../vercel-B0MuIVgJ.js";
3
3
  import { t as resolveAppFetch } from "../app-BZpq2cwC.js";
4
4
  import { waitUntil } from "@vercel/functions";
5
5
  import { H3 } from "h3";
@@ -30,6 +30,13 @@ function createVercelHostedServer(adapter) {
30
30
  }
31
31
  //#endregion
32
32
  //#region src/runtime/vercel-vite.ts
33
+ async function runWithVercelWorkflowRuntimeEvent(req, res, run) {
34
+ return await runWithWorkflowRuntimeEvent({
35
+ req,
36
+ res,
37
+ waitUntil
38
+ }, run);
39
+ }
33
40
  function createWorkflowVercelServer(options = {}) {
34
41
  return createVercelHostedServer({
35
42
  app: options.app,
@@ -37,13 +44,9 @@ function createWorkflowVercelServer(options = {}) {
37
44
  onRequest({ handle, req, res }) {
38
45
  setWorkflowRuntimeConfig(options.workflow);
39
46
  setWorkflowRuntimeRegistry(options.registry);
40
- return runWithWorkflowRuntimeEvent({
41
- req,
42
- res,
43
- waitUntil
44
- }, handle);
47
+ return runWithVercelWorkflowRuntimeEvent(req, res, handle);
45
48
  }
46
49
  });
47
50
  }
48
51
  //#endregion
49
- export { createWorkflowVercelServer, setVercelWorkflowRuntimeModules };
52
+ export { createWorkflowVercelServer, runWithVercelWorkflowRuntimeEvent, setVercelWorkflowRuntimeModules };
@@ -0,0 +1,37 @@
1
+ //#region src/internal/runtime-type.ts
2
+ function isCallableRepresentation(value) {
3
+ if (value === null || value === void 0 || Object(value) !== value) return false;
4
+ try {
5
+ Function.prototype.toString.call(value);
6
+ return true;
7
+ } catch {
8
+ return false;
9
+ }
10
+ }
11
+ /** Parses JavaScript runtime representation categories at Workflow boundaries. */
12
+ function hasRuntimeType(value, expected) {
13
+ if (expected === "undefined") return value === void 0;
14
+ if (expected === "object" && value === null) return true;
15
+ if (value === null || value === void 0) return false;
16
+ const isPrimitive = Object(value) !== value;
17
+ if (!isPrimitive) {
18
+ if (expected === "function") return isCallableRepresentation(value);
19
+ return expected === "object" && !isCallableRepresentation(value);
20
+ }
21
+ const tag = Object.prototype.toString.call(value);
22
+ switch (expected) {
23
+ case "bigint": return isPrimitive && tag === "[object BigInt]";
24
+ case "boolean": return isPrimitive && tag === "[object Boolean]";
25
+ case "function": return false;
26
+ case "number": return isPrimitive && tag === "[object Number]";
27
+ case "object": return false;
28
+ case "string": return isPrimitive && tag === "[object String]";
29
+ case "symbol": return isPrimitive && tag === "[object Symbol]";
30
+ }
31
+ throw new TypeError(`Unsupported runtime type: ${expected}`);
32
+ }
33
+ function isRuntimeRecord(value) {
34
+ return value !== null && hasRuntimeType(value, "object") && !Array.isArray(value);
35
+ }
36
+ //#endregion
37
+ export { isRuntimeRecord as n, hasRuntimeType as t };