@vite-hub/workflow 0.0.0 → 0.0.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.
- package/README.md +60 -1
- package/dist/_app-BVWpnNHo.d.ts +11 -0
- package/dist/app-BE3EYOWn.js +10 -0
- package/dist/cloudflare-CeHVSHoN.js +42 -0
- package/dist/cloudflare-env-BVUMOgFK.d.ts +21 -0
- package/dist/cloudflare-env-g0Rf3ClH.js +49 -0
- package/dist/cloudflare-shared-BIeY2kak.d.ts +1 -0
- package/dist/cloudflare-vite-DcM5YVTP.js +40 -0
- package/dist/config-DkgsQuXz.js +92 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +369 -0
- package/dist/module-UYEnVfxy.js +1546 -0
- package/dist/nitro.d.ts +19 -0
- package/dist/nitro.js +2 -0
- package/dist/openworkflow-amN7IkGO.d.ts +24 -0
- package/dist/runtime/cloudflare-runner.d.ts +27 -0
- package/dist/runtime/cloudflare-runner.js +23 -0
- package/dist/runtime/cloudflare-shared.d.ts +2 -0
- package/dist/runtime/cloudflare-shared.js +2 -0
- package/dist/runtime/cloudflare-vite.d.ts +17 -0
- package/dist/runtime/cloudflare-vite.js +2 -0
- package/dist/runtime/execute.d.ts +11 -0
- package/dist/runtime/execute.js +25 -0
- package/dist/runtime/nitro-plugin.d.ts +6 -0
- package/dist/runtime/nitro-plugin.js +14 -0
- package/dist/runtime/openworkflow-worker.d.ts +18 -0
- package/dist/runtime/openworkflow-worker.js +55 -0
- package/dist/runtime/openworkflow.d.ts +2 -0
- package/dist/runtime/openworkflow.js +143 -0
- package/dist/runtime/state.d.ts +35 -0
- package/dist/runtime/state.js +179 -0
- package/dist/runtime/vercel-vite.d.ts +14 -0
- package/dist/runtime/vercel-vite.js +48 -0
- package/dist/types-ThDitg7Z.d.ts +121 -0
- package/dist/vite.d.ts +16 -0
- package/dist/vite.js +503 -0
- package/package.json +93 -5
package/dist/nitro.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { l as ResolvedWorkflowOptions, y as WorkflowModuleOptions } from "./types-ThDitg7Z.js";
|
|
2
|
+
import { NitroModule } from "nitro/types";
|
|
3
|
+
|
|
4
|
+
//#region src/nitro/module.d.ts
|
|
5
|
+
declare const workflowNitroModule: NitroModule;
|
|
6
|
+
declare module "nitro/types" {
|
|
7
|
+
interface NitroConfig {
|
|
8
|
+
workflow?: WorkflowModuleOptions;
|
|
9
|
+
}
|
|
10
|
+
interface NitroOptions {
|
|
11
|
+
workflow?: WorkflowModuleOptions;
|
|
12
|
+
}
|
|
13
|
+
interface NitroRuntimeConfig {
|
|
14
|
+
hosting?: string;
|
|
15
|
+
workflow?: false | ResolvedWorkflowOptions;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
//#endregion
|
|
19
|
+
export { workflowNitroModule as default };
|
package/dist/nitro.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { f as WorkflowDeferOptions, l as ResolvedWorkflowOptions, p as WorkflowDefinition, w as WorkflowRun } from "./types-ThDitg7Z.js";
|
|
2
|
+
import * as _$openworkflow from "openworkflow";
|
|
3
|
+
import * as _$openworkflow_postgres0 from "openworkflow/postgres";
|
|
4
|
+
|
|
5
|
+
//#region src/runtime/openworkflow.d.ts
|
|
6
|
+
type OpenWorkflowModule = typeof _$openworkflow;
|
|
7
|
+
type OpenWorkflowPostgresModule = typeof _$openworkflow_postgres0;
|
|
8
|
+
type OpenWorkflowClient = InstanceType<OpenWorkflowModule["OpenWorkflow"]>;
|
|
9
|
+
type OpenWorkflowBackend = Awaited<ReturnType<OpenWorkflowPostgresModule["BackendPostgres"]["connect"]>>;
|
|
10
|
+
type OpenWorkflowRunnable = ReturnType<OpenWorkflowClient["defineWorkflow"]>;
|
|
11
|
+
type OpenWorkflowImporter = <T>(specifier: string) => Promise<T>;
|
|
12
|
+
interface OpenWorkflowRuntime {
|
|
13
|
+
backend: OpenWorkflowBackend;
|
|
14
|
+
client: OpenWorkflowClient;
|
|
15
|
+
workflows: Map<string, OpenWorkflowRunnable>;
|
|
16
|
+
}
|
|
17
|
+
declare function getOpenWorkflowRuntime(config: ResolvedWorkflowOptions): Promise<OpenWorkflowRuntime>;
|
|
18
|
+
declare function registerOpenWorkflowDefinition(runtime: OpenWorkflowRuntime, name: string, definition: WorkflowDefinition): Promise<OpenWorkflowRunnable>;
|
|
19
|
+
declare function runOpenWorkflow<TPayload = unknown, TResult = unknown>(config: ResolvedWorkflowOptions, name: string, payload: TPayload | undefined, definition: WorkflowDefinition<TPayload, TResult>, options: WorkflowDeferOptions): Promise<WorkflowRun<TPayload, TResult>>;
|
|
20
|
+
declare function getOpenWorkflowRun<TPayload = unknown, TResult = unknown>(config: ResolvedWorkflowOptions, name: string, id: string): Promise<WorkflowRun<TPayload, TResult>>;
|
|
21
|
+
declare function resetOpenWorkflowRuntime(): Promise<void>;
|
|
22
|
+
declare function setOpenWorkflowImporter(importer: OpenWorkflowImporter): void;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { runOpenWorkflow as a, resetOpenWorkflowRuntime as i, getOpenWorkflowRuntime as n, setOpenWorkflowImporter as o, registerOpenWorkflowDefinition as r, getOpenWorkflowRun as t };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { C as WorkflowProviderStep, h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-ThDitg7Z.js";
|
|
2
|
+
import { t as CloudflareWorkerEnv } from "../cloudflare-env-BVUMOgFK.js";
|
|
3
|
+
|
|
4
|
+
//#region src/runtime/cloudflare-runner.d.ts
|
|
5
|
+
interface CloudflareWorkflowEvent {
|
|
6
|
+
id?: string;
|
|
7
|
+
instanceId?: string;
|
|
8
|
+
payload?: unknown;
|
|
9
|
+
}
|
|
10
|
+
interface RunCloudflareWorkflowOptions {
|
|
11
|
+
config: false | ResolvedWorkflowOptions | undefined;
|
|
12
|
+
env: CloudflareWorkerEnv;
|
|
13
|
+
event: CloudflareWorkflowEvent;
|
|
14
|
+
name: string;
|
|
15
|
+
registry: WorkflowDefinitionRegistry;
|
|
16
|
+
step?: WorkflowProviderStep;
|
|
17
|
+
}
|
|
18
|
+
declare function runCloudflareWorkflow({
|
|
19
|
+
config,
|
|
20
|
+
env,
|
|
21
|
+
event,
|
|
22
|
+
name,
|
|
23
|
+
registry,
|
|
24
|
+
step
|
|
25
|
+
}: RunCloudflareWorkflowOptions): Promise<unknown>;
|
|
26
|
+
//#endregion
|
|
27
|
+
export { CloudflareWorkflowEvent, RunCloudflareWorkflowOptions, runCloudflareWorkflow };
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { i as setActiveCloudflareEnv } from "../cloudflare-env-g0Rf3ClH.js";
|
|
2
|
+
import { runWorkflowHandler } from "./execute.js";
|
|
3
|
+
import { loadWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
4
|
+
//#region src/runtime/cloudflare-runner.ts
|
|
5
|
+
async function runCloudflareWorkflow({ config, env, event, name, registry, step }) {
|
|
6
|
+
setWorkflowRuntimeConfig(config);
|
|
7
|
+
setWorkflowRuntimeRegistry(registry);
|
|
8
|
+
setActiveCloudflareEnv(env);
|
|
9
|
+
const definition = await loadWorkflowDefinition(name);
|
|
10
|
+
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));
|
|
21
|
+
}
|
|
22
|
+
//#endregion
|
|
23
|
+
export { runCloudflareWorkflow };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { i as setActiveCloudflareEnv, n as CloudflareWorkerExecutionContext, r as createCloudflareRuntimeEvent, t as CloudflareWorkerEnv } from "../cloudflare-env-BVUMOgFK.js";
|
|
2
|
+
export { CloudflareWorkerEnv, CloudflareWorkerExecutionContext, createCloudflareRuntimeEvent, setActiveCloudflareEnv };
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-ThDitg7Z.js";
|
|
2
|
+
import { t as WorkflowApp } from "../_app-BVWpnNHo.js";
|
|
3
|
+
import { n as CloudflareWorkerExecutionContext, t as CloudflareWorkerEnv } from "../cloudflare-env-BVUMOgFK.js";
|
|
4
|
+
|
|
5
|
+
//#region src/runtime/cloudflare-vite.d.ts
|
|
6
|
+
type CloudflareWorkerApp = WorkflowApp;
|
|
7
|
+
interface WorkflowCloudflareWorkerOptions {
|
|
8
|
+
app?: CloudflareWorkerApp;
|
|
9
|
+
registry?: WorkflowDefinitionRegistry;
|
|
10
|
+
workflow?: false | ResolvedWorkflowOptions;
|
|
11
|
+
}
|
|
12
|
+
interface WorkflowCloudflareWorker {
|
|
13
|
+
fetch: (request: Request, env: CloudflareWorkerEnv, context: CloudflareWorkerExecutionContext) => Promise<Response>;
|
|
14
|
+
}
|
|
15
|
+
declare function createWorkflowCloudflareWorker(options?: WorkflowCloudflareWorkerOptions): WorkflowCloudflareWorker;
|
|
16
|
+
//#endregion
|
|
17
|
+
export { CloudflareWorkerApp, WorkflowCloudflareWorker, WorkflowCloudflareWorkerOptions, createWorkflowCloudflareWorker };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { C as WorkflowProviderStep, O as WorkflowStepFunction, g as WorkflowExecutionContext, k as WorkflowStepOptions, p as WorkflowDefinition } from "../types-ThDitg7Z.js";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/execute.d.ts
|
|
4
|
+
declare function runProviderStep<TResult>(step: WorkflowProviderStep | undefined, name: string, run: () => TResult | Promise<TResult>, options?: WorkflowStepOptions): Promise<TResult>;
|
|
5
|
+
declare function createWorkflowSteps(context: WorkflowExecutionContext, stepModules: Array<{
|
|
6
|
+
name: string;
|
|
7
|
+
run: WorkflowStepFunction;
|
|
8
|
+
}>): Record<string, WorkflowStepFunction>;
|
|
9
|
+
declare function runWorkflowHandler<TPayload, TResult>(context: WorkflowExecutionContext<TPayload>, definition: WorkflowDefinition<TPayload, TResult>): Promise<TResult>;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { createWorkflowSteps, runProviderStep, runWorkflowHandler };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
//#region src/runtime/execute.ts
|
|
2
|
+
const defaultStepOptions = { retries: {
|
|
3
|
+
backoff: "exponential",
|
|
4
|
+
delay: "10 seconds",
|
|
5
|
+
limit: 3
|
|
6
|
+
} };
|
|
7
|
+
async function runProviderStep(step, name, run, options = defaultStepOptions) {
|
|
8
|
+
if (typeof step?.do === "function") return await step.do(name, options, run);
|
|
9
|
+
return await run();
|
|
10
|
+
}
|
|
11
|
+
function toStepPropertyName(file) {
|
|
12
|
+
return file.split("/").pop().replace(/\.(?:c|m)?[jt]s$/i, "").replace(/^\d+[.-]?/, "").replace(/[-_.]+([a-z0-9])/gi, (_, char) => char.toUpperCase());
|
|
13
|
+
}
|
|
14
|
+
function createWorkflowSteps(context, stepModules) {
|
|
15
|
+
return Object.fromEntries(stepModules.map((module) => {
|
|
16
|
+
return [toStepPropertyName(module.name), async (input) => await runProviderStep(context.step, `${context.name}/${module.name}`, () => module.run(input))];
|
|
17
|
+
}));
|
|
18
|
+
}
|
|
19
|
+
async function runWorkflowHandler(context, definition) {
|
|
20
|
+
const run = () => definition.handler(context);
|
|
21
|
+
if (definition.options?.rootStep === false) return await run();
|
|
22
|
+
return await runProviderStep(context.step, context.name, run);
|
|
23
|
+
}
|
|
24
|
+
//#endregion
|
|
25
|
+
export { createWorkflowSteps, runProviderStep, runWorkflowHandler };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { enterWorkflowRuntimeEvent, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry } from "./state.js";
|
|
2
|
+
import { definePlugin } from "nitro";
|
|
3
|
+
import { useRuntimeConfig } from "nitro/runtime-config";
|
|
4
|
+
import workflowRegistry from "#vitehub/workflow/registry";
|
|
5
|
+
//#region src/runtime/nitro-plugin.ts
|
|
6
|
+
const workflowNitroPlugin = definePlugin((nitroApp) => {
|
|
7
|
+
setWorkflowRuntimeConfig(useRuntimeConfig().workflow);
|
|
8
|
+
setWorkflowRuntimeRegistry(workflowRegistry);
|
|
9
|
+
nitroApp.hooks.hook("request", (event) => {
|
|
10
|
+
enterWorkflowRuntimeEvent(event);
|
|
11
|
+
});
|
|
12
|
+
});
|
|
13
|
+
//#endregion
|
|
14
|
+
export { workflowNitroPlugin as default };
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-ThDitg7Z.js";
|
|
2
|
+
import { n as getOpenWorkflowRuntime } from "../openworkflow-amN7IkGO.js";
|
|
3
|
+
|
|
4
|
+
//#region src/runtime/openworkflow-worker.d.ts
|
|
5
|
+
type OpenWorkflowWorker = ReturnType<Awaited<ReturnType<typeof getOpenWorkflowRuntime>>["client"]["newWorker"]>;
|
|
6
|
+
interface CreateOpenWorkflowWorkerOptions {
|
|
7
|
+
concurrency?: number;
|
|
8
|
+
config?: false | ResolvedWorkflowOptions;
|
|
9
|
+
registry?: WorkflowDefinitionRegistry;
|
|
10
|
+
}
|
|
11
|
+
interface StartOpenWorkflowWorkerOptions extends CreateOpenWorkflowWorkerOptions {
|
|
12
|
+
signal?: AbortSignal;
|
|
13
|
+
signals?: boolean;
|
|
14
|
+
}
|
|
15
|
+
declare function createOpenWorkflowWorker(options?: CreateOpenWorkflowWorkerOptions): Promise<OpenWorkflowWorker>;
|
|
16
|
+
declare function startOpenWorkflowWorker(options?: StartOpenWorkflowWorkerOptions): Promise<OpenWorkflowWorker>;
|
|
17
|
+
//#endregion
|
|
18
|
+
export { CreateOpenWorkflowWorkerOptions, StartOpenWorkflowWorkerOptions, createOpenWorkflowWorker, startOpenWorkflowWorker };
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { getOpenWorkflowRuntime, registerOpenWorkflowDefinition } from "./openworkflow.js";
|
|
2
|
+
import { getInlineWorkflowDefinitions, getWorkflowRuntimeConfig, getWorkflowRuntimeRegistry, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry, takeInlineWorkflowDefinitionForModule } from "./state.js";
|
|
3
|
+
//#region src/runtime/openworkflow-worker.ts
|
|
4
|
+
function getProcess() {
|
|
5
|
+
return globalThis.process;
|
|
6
|
+
}
|
|
7
|
+
async function loadRegistryDefinitions(registry) {
|
|
8
|
+
const definitions = /* @__PURE__ */ new Map();
|
|
9
|
+
for (const name of Object.keys(registry).sort()) {
|
|
10
|
+
const loaded = await registry[name]?.();
|
|
11
|
+
const definition = loaded && typeof loaded === "object" && "default" in loaded ? loaded.default : loaded;
|
|
12
|
+
if (definition && typeof definition === "object" && "handler" in definition && typeof definition.handler === "function") {
|
|
13
|
+
definitions.set(name, definition);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
const inlineDefinition = takeInlineWorkflowDefinitionForModule(name, loaded);
|
|
17
|
+
if (inlineDefinition) definitions.set(name, inlineDefinition);
|
|
18
|
+
}
|
|
19
|
+
return definitions;
|
|
20
|
+
}
|
|
21
|
+
function mergeInlineDefinitions(definitions) {
|
|
22
|
+
for (const [name, definition] of getInlineWorkflowDefinitions()) {
|
|
23
|
+
const existing = definitions.get(name);
|
|
24
|
+
if (existing && existing !== definition) throw new Error(`Duplicate workflow name "${name}" from inline and discovered definitions.`);
|
|
25
|
+
definitions.set(name, definition);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
async function createOpenWorkflowWorker(options = {}) {
|
|
29
|
+
const config = options.config ?? getWorkflowRuntimeConfig();
|
|
30
|
+
if (!config || config.provider !== "openworkflow") throw new Error("OpenWorkflow worker requires workflow.provider \"openworkflow\".");
|
|
31
|
+
const registry = options.registry ?? getWorkflowRuntimeRegistry() ?? {};
|
|
32
|
+
setWorkflowRuntimeConfig(config);
|
|
33
|
+
setWorkflowRuntimeRegistry(registry);
|
|
34
|
+
const runtime = await getOpenWorkflowRuntime(config);
|
|
35
|
+
const definitions = await loadRegistryDefinitions(registry);
|
|
36
|
+
mergeInlineDefinitions(definitions);
|
|
37
|
+
for (const [name, definition] of definitions) await registerOpenWorkflowDefinition(runtime, name, definition);
|
|
38
|
+
return runtime.client.newWorker({ concurrency: options.concurrency ?? config.worker?.concurrency });
|
|
39
|
+
}
|
|
40
|
+
async function startOpenWorkflowWorker(options = {}) {
|
|
41
|
+
const worker = await createOpenWorkflowWorker(options);
|
|
42
|
+
await worker.start();
|
|
43
|
+
const stop = () => {
|
|
44
|
+
worker.stop();
|
|
45
|
+
};
|
|
46
|
+
options.signal?.addEventListener("abort", stop, { once: true });
|
|
47
|
+
const proc = getProcess();
|
|
48
|
+
if (options.signals !== false) {
|
|
49
|
+
proc?.on?.("SIGINT", stop);
|
|
50
|
+
proc?.on?.("SIGTERM", stop);
|
|
51
|
+
}
|
|
52
|
+
return worker;
|
|
53
|
+
}
|
|
54
|
+
//#endregion
|
|
55
|
+
export { createOpenWorkflowWorker, startOpenWorkflowWorker };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import { a as runOpenWorkflow, i as resetOpenWorkflowRuntime, n as getOpenWorkflowRuntime, o as setOpenWorkflowImporter, r as registerOpenWorkflowDefinition, t as getOpenWorkflowRun } from "../openworkflow-amN7IkGO.js";
|
|
2
|
+
export { getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, runOpenWorkflow, setOpenWorkflowImporter };
|
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
import { runWorkflowHandler } from "./execute.js";
|
|
2
|
+
//#region src/runtime/openworkflow.ts
|
|
3
|
+
const defaultImporter = new Function("specifier", "return import(specifier)");
|
|
4
|
+
let openWorkflowImporter = defaultImporter;
|
|
5
|
+
const runtimes = /* @__PURE__ */ new Map();
|
|
6
|
+
function readEnv(name) {
|
|
7
|
+
const value = (globalThis.process?.env)?.[name];
|
|
8
|
+
return typeof value === "string" && value.trim() ? value.trim() : void 0;
|
|
9
|
+
}
|
|
10
|
+
function getOpenWorkflowConfig(config) {
|
|
11
|
+
if (config.provider !== "openworkflow") throw new Error(`OpenWorkflow runtime requires workflow.provider "openworkflow", received "${config.provider}".`);
|
|
12
|
+
const postgres = config.postgres || {};
|
|
13
|
+
const url = postgres.url || readEnv("OPENWORKFLOW_POSTGRES_URL") || readEnv("DATABASE_URL");
|
|
14
|
+
if (!url) throw new Error("Missing OpenWorkflow Postgres URL. Set workflow.postgres.url, OPENWORKFLOW_POSTGRES_URL, or DATABASE_URL.");
|
|
15
|
+
return {
|
|
16
|
+
namespaceId: postgres.namespaceId || readEnv("OPENWORKFLOW_NAMESPACE_ID") || "production",
|
|
17
|
+
runMigrations: postgres.runMigrations,
|
|
18
|
+
schema: postgres.schema || readEnv("OPENWORKFLOW_SCHEMA") || "openworkflow",
|
|
19
|
+
url
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function getRuntimeKey(config) {
|
|
23
|
+
return JSON.stringify(getOpenWorkflowConfig(config));
|
|
24
|
+
}
|
|
25
|
+
async function createOpenWorkflowRuntime(config) {
|
|
26
|
+
const options = getOpenWorkflowConfig(config);
|
|
27
|
+
const [{ OpenWorkflow }, { BackendPostgres }] = await Promise.all([openWorkflowImporter("openworkflow"), openWorkflowImporter("openworkflow/postgres")]);
|
|
28
|
+
const backend = await BackendPostgres.connect(options.url, {
|
|
29
|
+
namespaceId: options.namespaceId,
|
|
30
|
+
...typeof options.runMigrations === "boolean" ? { runMigrations: options.runMigrations } : {},
|
|
31
|
+
schema: options.schema
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
backend,
|
|
35
|
+
client: new OpenWorkflow({ backend }),
|
|
36
|
+
workflows: /* @__PURE__ */ new Map()
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
async function getOpenWorkflowRuntime(config) {
|
|
40
|
+
const key = getRuntimeKey(config);
|
|
41
|
+
let runtime = runtimes.get(key);
|
|
42
|
+
if (!runtime) {
|
|
43
|
+
runtime = createOpenWorkflowRuntime(config);
|
|
44
|
+
runtimes.set(key, runtime);
|
|
45
|
+
}
|
|
46
|
+
return await runtime;
|
|
47
|
+
}
|
|
48
|
+
function toOpenWorkflowRetryPolicy(options) {
|
|
49
|
+
const retries = options.retries;
|
|
50
|
+
if (!retries) return;
|
|
51
|
+
return {
|
|
52
|
+
...retries.backoff ? { backoffCoefficient: retries.backoff === "exponential" ? 2 : 1 } : {},
|
|
53
|
+
...retries.delay ? { initialInterval: retries.delay } : {},
|
|
54
|
+
...typeof retries.limit === "number" ? { maximumAttempts: retries.limit } : {}
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function createOpenWorkflowProviderStep(step) {
|
|
58
|
+
return { async do(name, options, run) {
|
|
59
|
+
return await step.run({
|
|
60
|
+
name,
|
|
61
|
+
...toOpenWorkflowRetryPolicy(options) ? { retryPolicy: toOpenWorkflowRetryPolicy(options) } : {}
|
|
62
|
+
}, run);
|
|
63
|
+
} };
|
|
64
|
+
}
|
|
65
|
+
async function registerOpenWorkflowDefinition(runtime, name, definition) {
|
|
66
|
+
const existing = runtime.workflows.get(name);
|
|
67
|
+
if (existing) return existing;
|
|
68
|
+
const workflow = runtime.client.defineWorkflow({ name }, async ({ input, run, step }) => {
|
|
69
|
+
return await runWorkflowHandler({
|
|
70
|
+
id: run.id,
|
|
71
|
+
name,
|
|
72
|
+
payload: input,
|
|
73
|
+
provider: "openworkflow",
|
|
74
|
+
step: createOpenWorkflowProviderStep(step)
|
|
75
|
+
}, definition);
|
|
76
|
+
});
|
|
77
|
+
runtime.workflows.set(name, workflow);
|
|
78
|
+
return workflow;
|
|
79
|
+
}
|
|
80
|
+
function normalizeOpenWorkflowStatus(status) {
|
|
81
|
+
switch (String(status || "").toLowerCase()) {
|
|
82
|
+
case "completed":
|
|
83
|
+
case "succeeded": return "completed";
|
|
84
|
+
case "canceled":
|
|
85
|
+
case "failed": return "failed";
|
|
86
|
+
case "pending": return "queued";
|
|
87
|
+
case "running":
|
|
88
|
+
case "sleeping": return "running";
|
|
89
|
+
default: return "unknown";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
function serializeOpenWorkflowRun(run, name) {
|
|
93
|
+
if (!run || run.workflowName !== name) return {
|
|
94
|
+
id: run?.id || "",
|
|
95
|
+
provider: "openworkflow",
|
|
96
|
+
status: "unknown"
|
|
97
|
+
};
|
|
98
|
+
return {
|
|
99
|
+
id: run.id,
|
|
100
|
+
metadata: run.error || {
|
|
101
|
+
namespaceId: run.namespaceId,
|
|
102
|
+
version: run.version,
|
|
103
|
+
workflowName: run.workflowName
|
|
104
|
+
},
|
|
105
|
+
provider: "openworkflow",
|
|
106
|
+
result: run.output ?? void 0,
|
|
107
|
+
status: normalizeOpenWorkflowStatus(run.status)
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
async function runOpenWorkflow(config, name, payload, definition, options) {
|
|
111
|
+
const handle = await (await registerOpenWorkflowDefinition(await getOpenWorkflowRuntime(config), name, definition)).run(payload, options.id ? { idempotencyKey: options.id } : void 0);
|
|
112
|
+
return {
|
|
113
|
+
id: handle.workflowRun.id,
|
|
114
|
+
metadata: {
|
|
115
|
+
...options.id ? { idempotencyKey: options.id } : {},
|
|
116
|
+
workflow: name
|
|
117
|
+
},
|
|
118
|
+
payload,
|
|
119
|
+
provider: "openworkflow",
|
|
120
|
+
status: normalizeOpenWorkflowStatus(handle.workflowRun.status)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
async function getOpenWorkflowRun(config, name, id) {
|
|
124
|
+
const serialized = serializeOpenWorkflowRun(await (await getOpenWorkflowRuntime(config)).backend.getWorkflowRun({ workflowRunId: id }), name);
|
|
125
|
+
return serialized.id ? serialized : {
|
|
126
|
+
id,
|
|
127
|
+
provider: "openworkflow",
|
|
128
|
+
status: "unknown"
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
async function resetOpenWorkflowRuntime() {
|
|
132
|
+
const settled = await Promise.allSettled(runtimes.values());
|
|
133
|
+
runtimes.clear();
|
|
134
|
+
openWorkflowImporter = defaultImporter;
|
|
135
|
+
await Promise.all(settled.map(async (entry) => {
|
|
136
|
+
if (entry.status === "fulfilled") await entry.value.backend.stop();
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
function setOpenWorkflowImporter(importer) {
|
|
140
|
+
openWorkflowImporter = importer;
|
|
141
|
+
}
|
|
142
|
+
//#endregion
|
|
143
|
+
export { getOpenWorkflowRun, getOpenWorkflowRuntime, registerOpenWorkflowDefinition, resetOpenWorkflowRuntime, runOpenWorkflow, setOpenWorkflowImporter };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions, p as WorkflowDefinition } from "../types-ThDitg7Z.js";
|
|
2
|
+
|
|
3
|
+
//#region src/runtime/state.d.ts
|
|
4
|
+
interface WorkflowRunState<TResult = unknown> {
|
|
5
|
+
error?: unknown;
|
|
6
|
+
expiresAt?: number;
|
|
7
|
+
promise: Promise<{
|
|
8
|
+
result?: TResult;
|
|
9
|
+
status: "completed" | "failed";
|
|
10
|
+
error?: unknown;
|
|
11
|
+
}>;
|
|
12
|
+
result?: TResult;
|
|
13
|
+
status: "running" | "completed" | "failed";
|
|
14
|
+
}
|
|
15
|
+
declare function setWorkflowRuntimeConfig(config: false | ResolvedWorkflowOptions | undefined): void;
|
|
16
|
+
declare function getWorkflowRuntimeConfig(): false | ResolvedWorkflowOptions | undefined;
|
|
17
|
+
declare function setWorkflowRuntimeRegistry(registry: WorkflowDefinitionRegistry | undefined): void;
|
|
18
|
+
declare function getWorkflowRuntimeRegistry(): WorkflowDefinitionRegistry | undefined;
|
|
19
|
+
declare function getInlineWorkflowDefinitions(): ReadonlyMap<string, WorkflowDefinition>;
|
|
20
|
+
declare function takeInlineWorkflowDefinition(name: string): WorkflowDefinition | undefined;
|
|
21
|
+
declare function takeInlineWorkflowDefinitionForModule(name: string, loaded: unknown): WorkflowDefinition | undefined;
|
|
22
|
+
declare function registerInlineWorkflowDefinition(name: string, definition: WorkflowDefinition): void;
|
|
23
|
+
declare function enterWorkflowRuntimeEvent(event: unknown): void;
|
|
24
|
+
declare function getWorkflowRuntimeEvent(): unknown;
|
|
25
|
+
declare function runWithWorkflowRuntimeEvent<T>(event: unknown, run: () => T | Promise<T>): Promise<T>;
|
|
26
|
+
declare function loadWorkflowDefinition(name: string): Promise<WorkflowDefinition | undefined>;
|
|
27
|
+
declare function setWorkflowRun<TResult = unknown>(name: string, id: string, promise: Promise<{
|
|
28
|
+
result?: TResult;
|
|
29
|
+
status: "completed" | "failed";
|
|
30
|
+
error?: unknown;
|
|
31
|
+
}>): WorkflowRunState<TResult>;
|
|
32
|
+
declare function getWorkflowRunState(name: string, id: string): WorkflowRunState | undefined;
|
|
33
|
+
declare function resetWorkflowRuntime(): void;
|
|
34
|
+
//#endregion
|
|
35
|
+
export { WorkflowRunState, enterWorkflowRuntimeEvent, getInlineWorkflowDefinitions, getWorkflowRunState, getWorkflowRuntimeConfig, getWorkflowRuntimeEvent, getWorkflowRuntimeRegistry, loadWorkflowDefinition, registerInlineWorkflowDefinition, resetWorkflowRuntime, runWithWorkflowRuntimeEvent, setWorkflowRun, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry, takeInlineWorkflowDefinition, takeInlineWorkflowDefinitionForModule };
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
2
|
+
//#region src/runtime/state.ts
|
|
3
|
+
const RUNS_LIMIT = 1024;
|
|
4
|
+
const RUNS_TTL_MS = 300 * 1e3;
|
|
5
|
+
let runtimeConfig;
|
|
6
|
+
let runtimeRegistry;
|
|
7
|
+
const inlineRegistry = /* @__PURE__ */ new Map();
|
|
8
|
+
const loadingRegistryEntries = /* @__PURE__ */ new Map();
|
|
9
|
+
const loadedRegistryEntries = /* @__PURE__ */ new Map();
|
|
10
|
+
let fallbackEvent;
|
|
11
|
+
const eventStorage = new AsyncLocalStorage();
|
|
12
|
+
const loadingRegistryStorage = new AsyncLocalStorage();
|
|
13
|
+
const loadingInlineRegistryStorage = new AsyncLocalStorage();
|
|
14
|
+
const runs = /* @__PURE__ */ new Map();
|
|
15
|
+
function getRunKey(name, id) {
|
|
16
|
+
return `${name}\0${id}`;
|
|
17
|
+
}
|
|
18
|
+
function pruneWorkflowRuns() {
|
|
19
|
+
const now = Date.now();
|
|
20
|
+
for (const [key, run] of runs) if (run.expiresAt && run.expiresAt <= now) runs.delete(key);
|
|
21
|
+
}
|
|
22
|
+
function setWorkflowRuntimeConfig(config) {
|
|
23
|
+
runtimeConfig = config;
|
|
24
|
+
}
|
|
25
|
+
function getWorkflowRuntimeConfig() {
|
|
26
|
+
return runtimeConfig;
|
|
27
|
+
}
|
|
28
|
+
function setWorkflowRuntimeRegistry(registry) {
|
|
29
|
+
runtimeRegistry = registry;
|
|
30
|
+
loadedRegistryEntries.clear();
|
|
31
|
+
}
|
|
32
|
+
function getWorkflowRuntimeRegistry() {
|
|
33
|
+
return runtimeRegistry;
|
|
34
|
+
}
|
|
35
|
+
function getInlineWorkflowDefinitions() {
|
|
36
|
+
return inlineRegistry;
|
|
37
|
+
}
|
|
38
|
+
function takeInlineWorkflowDefinition(name) {
|
|
39
|
+
const definition = inlineRegistry.get(name);
|
|
40
|
+
inlineRegistry.delete(name);
|
|
41
|
+
loadingInlineRegistryStorage.getStore()?.delete(name);
|
|
42
|
+
return definition;
|
|
43
|
+
}
|
|
44
|
+
function isWorkflowHandle(value) {
|
|
45
|
+
return typeof value === "object" && value !== null && typeof value.name === "string" && typeof value.defer === "function" && typeof value.getRun === "function" && typeof value.run === "function";
|
|
46
|
+
}
|
|
47
|
+
function findExportedInlineWorkflowDefinition(name, loaded, definitions) {
|
|
48
|
+
const namedDefinition = definitions.get(name);
|
|
49
|
+
if (namedDefinition) return {
|
|
50
|
+
definition: namedDefinition,
|
|
51
|
+
name
|
|
52
|
+
};
|
|
53
|
+
if (!loaded || typeof loaded !== "object") return void 0;
|
|
54
|
+
if ("default" in loaded && isWorkflowHandle(loaded.default)) {
|
|
55
|
+
const definition = definitions.get(loaded.default.name);
|
|
56
|
+
if (definition) return {
|
|
57
|
+
definition,
|
|
58
|
+
name: loaded.default.name
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
const matches = /* @__PURE__ */ new Map();
|
|
62
|
+
for (const value of Object.values(loaded)) {
|
|
63
|
+
if (!isWorkflowHandle(value)) continue;
|
|
64
|
+
const definition = definitions.get(value.name);
|
|
65
|
+
if (definition) matches.set(value.name, definition);
|
|
66
|
+
}
|
|
67
|
+
if (matches.size !== 1) return void 0;
|
|
68
|
+
const [matchedName, definition] = matches.entries().next().value;
|
|
69
|
+
return {
|
|
70
|
+
definition,
|
|
71
|
+
name: matchedName
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
function takeInlineWorkflowDefinitionForModule(name, loaded) {
|
|
75
|
+
const match = findExportedInlineWorkflowDefinition(name, loaded, inlineRegistry);
|
|
76
|
+
if (!match) return void 0;
|
|
77
|
+
inlineRegistry.delete(match.name);
|
|
78
|
+
return match.definition;
|
|
79
|
+
}
|
|
80
|
+
function consumeInlineWorkflowDefinition(name, expected) {
|
|
81
|
+
const definition = inlineRegistry.get(name);
|
|
82
|
+
if (!expected || definition === expected) inlineRegistry.delete(name);
|
|
83
|
+
return definition;
|
|
84
|
+
}
|
|
85
|
+
function registerInlineWorkflowDefinition(name, definition) {
|
|
86
|
+
if (!name || typeof name !== "string") throw new TypeError("`createWorkflow()` requires a workflow name.");
|
|
87
|
+
const loadingDefinitions = loadingInlineRegistryStorage.getStore();
|
|
88
|
+
const existing = inlineRegistry.get(name);
|
|
89
|
+
if (existing && existing !== definition) {
|
|
90
|
+
if (!loadingDefinitions) throw new Error(`Duplicate workflow name "${name}" from inline definitions.`);
|
|
91
|
+
}
|
|
92
|
+
inlineRegistry.set(name, definition);
|
|
93
|
+
loadingDefinitions?.set(name, definition);
|
|
94
|
+
}
|
|
95
|
+
function enterWorkflowRuntimeEvent(event) {
|
|
96
|
+
fallbackEvent = event;
|
|
97
|
+
try {
|
|
98
|
+
eventStorage.enterWith(event);
|
|
99
|
+
} catch {}
|
|
100
|
+
}
|
|
101
|
+
function getWorkflowRuntimeEvent() {
|
|
102
|
+
return eventStorage.getStore() ?? fallbackEvent;
|
|
103
|
+
}
|
|
104
|
+
async function runWithWorkflowRuntimeEvent(event, run) {
|
|
105
|
+
return await eventStorage.run(event, run);
|
|
106
|
+
}
|
|
107
|
+
async function loadWorkflowDefinition(name) {
|
|
108
|
+
const inlineDefinition = inlineRegistry.get(name);
|
|
109
|
+
const entry = runtimeRegistry?.[name];
|
|
110
|
+
if (entry && loadedRegistryEntries.has(name)) return loadedRegistryEntries.get(name);
|
|
111
|
+
if (inlineDefinition) {
|
|
112
|
+
if (entry) throw new Error(`Duplicate workflow name "${name}" from inline and discovered definitions.`);
|
|
113
|
+
return inlineDefinition;
|
|
114
|
+
}
|
|
115
|
+
if (!entry) return;
|
|
116
|
+
const activeLoads = loadingRegistryStorage.getStore();
|
|
117
|
+
const inFlightEntry = loadingRegistryEntries.get(name);
|
|
118
|
+
if (inFlightEntry) return activeLoads?.has(name) ? void 0 : await inFlightEntry;
|
|
119
|
+
const nextActiveLoads = new Set(activeLoads);
|
|
120
|
+
nextActiveLoads.add(name);
|
|
121
|
+
const loadingEntry = Promise.resolve().then(() => loadingRegistryStorage.run(nextActiveLoads, async () => {
|
|
122
|
+
const loadingInlineDefinitions = /* @__PURE__ */ new Map();
|
|
123
|
+
const loaded = await loadingInlineRegistryStorage.run(loadingInlineDefinitions, entry);
|
|
124
|
+
if (!loaded || typeof loaded !== "object") return;
|
|
125
|
+
const registeredInlineDefinition = loadingInlineDefinitions.get(name) ?? consumeInlineWorkflowDefinition(name);
|
|
126
|
+
if (registeredInlineDefinition) {
|
|
127
|
+
consumeInlineWorkflowDefinition(name, registeredInlineDefinition);
|
|
128
|
+
return registeredInlineDefinition;
|
|
129
|
+
}
|
|
130
|
+
const definition = "default" in loaded ? loaded.default : loaded;
|
|
131
|
+
if (definition && typeof definition.handler === "function") return definition;
|
|
132
|
+
const exportedInlineDefinition = findExportedInlineWorkflowDefinition(name, loaded, loadingInlineDefinitions);
|
|
133
|
+
if (!exportedInlineDefinition) return void 0;
|
|
134
|
+
consumeInlineWorkflowDefinition(exportedInlineDefinition.name, exportedInlineDefinition.definition);
|
|
135
|
+
return exportedInlineDefinition.definition;
|
|
136
|
+
}));
|
|
137
|
+
loadingRegistryEntries.set(name, loadingEntry);
|
|
138
|
+
try {
|
|
139
|
+
const loaded = await loadingEntry;
|
|
140
|
+
loadedRegistryEntries.set(name, loaded);
|
|
141
|
+
return loaded;
|
|
142
|
+
} finally {
|
|
143
|
+
loadingRegistryEntries.delete(name);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function setWorkflowRun(name, id, promise) {
|
|
147
|
+
pruneWorkflowRuns();
|
|
148
|
+
if (runs.size >= RUNS_LIMIT) {
|
|
149
|
+
const oldest = runs.keys().next().value;
|
|
150
|
+
if (oldest !== void 0) runs.delete(oldest);
|
|
151
|
+
}
|
|
152
|
+
const state = {
|
|
153
|
+
promise: promise.then((resolved) => {
|
|
154
|
+
state.status = resolved.status;
|
|
155
|
+
state.result = resolved.result;
|
|
156
|
+
state.error = resolved.error;
|
|
157
|
+
state.expiresAt = Date.now() + RUNS_TTL_MS;
|
|
158
|
+
return resolved;
|
|
159
|
+
}),
|
|
160
|
+
status: "running"
|
|
161
|
+
};
|
|
162
|
+
runs.set(getRunKey(name, id), state);
|
|
163
|
+
return state;
|
|
164
|
+
}
|
|
165
|
+
function getWorkflowRunState(name, id) {
|
|
166
|
+
pruneWorkflowRuns();
|
|
167
|
+
return runs.get(getRunKey(name, id));
|
|
168
|
+
}
|
|
169
|
+
function resetWorkflowRuntime() {
|
|
170
|
+
runtimeConfig = void 0;
|
|
171
|
+
runtimeRegistry = void 0;
|
|
172
|
+
inlineRegistry.clear();
|
|
173
|
+
loadingRegistryEntries.clear();
|
|
174
|
+
loadedRegistryEntries.clear();
|
|
175
|
+
fallbackEvent = void 0;
|
|
176
|
+
runs.clear();
|
|
177
|
+
}
|
|
178
|
+
//#endregion
|
|
179
|
+
export { enterWorkflowRuntimeEvent, getInlineWorkflowDefinitions, getWorkflowRunState, getWorkflowRuntimeConfig, getWorkflowRuntimeEvent, getWorkflowRuntimeRegistry, loadWorkflowDefinition, registerInlineWorkflowDefinition, resetWorkflowRuntime, runWithWorkflowRuntimeEvent, setWorkflowRun, setWorkflowRuntimeConfig, setWorkflowRuntimeRegistry, takeInlineWorkflowDefinition, takeInlineWorkflowDefinitionForModule };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { h as WorkflowDefinitionRegistry, l as ResolvedWorkflowOptions } from "../types-ThDitg7Z.js";
|
|
2
|
+
import { t as WorkflowApp } from "../_app-BVWpnNHo.js";
|
|
3
|
+
import { IncomingMessage, ServerResponse } from "node:http";
|
|
4
|
+
|
|
5
|
+
//#region src/runtime/vercel-vite.d.ts
|
|
6
|
+
interface WorkflowVercelServerOptions {
|
|
7
|
+
app?: WorkflowApp;
|
|
8
|
+
registry?: WorkflowDefinitionRegistry;
|
|
9
|
+
workflow?: false | ResolvedWorkflowOptions;
|
|
10
|
+
}
|
|
11
|
+
type WorkflowVercelServer = (req: IncomingMessage, res: ServerResponse) => unknown;
|
|
12
|
+
declare function createWorkflowVercelServer(options?: WorkflowVercelServerOptions): WorkflowVercelServer;
|
|
13
|
+
//#endregion
|
|
14
|
+
export { WorkflowVercelServer, createWorkflowVercelServer };
|