@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.
- package/README.md +173 -55
- package/dist/{app-CzPoP84f.js → app-He0GTrYZ.js} +1 -1
- package/dist/client-DkwOxEiT.js +707 -0
- package/dist/{cloudflare-nF4abPd4.js → cloudflare-BHfClPvT.js} +2 -2
- package/dist/{cloudflare-env-BNrYo29x.js → cloudflare-env-CGLx7lUd.js} +44 -42
- package/dist/cloudflare-env-lookup-CfX6wZKb.d.ts +4 -0
- package/dist/{cloudflare-env-CIjdFTv6.d.ts → cloudflare-shared-CTqh_a2s.d.ts} +2 -2
- package/dist/{config-IJMb0eeZ.js → config-C3DvtjfJ.js} +41 -40
- package/dist/definition.d.ts +18 -0
- package/dist/definition.js +33 -0
- package/dist/delay-D1-nq_VE.js +46 -0
- package/dist/dev-recorder-CTN5Fkao.js +10 -0
- package/dist/durability.d.ts +23 -0
- package/dist/durability.js +62 -0
- package/dist/error-diagnostics-0V2T3JFw.js +317 -0
- package/dist/error-diagnostics-CQ4BUqRe.js +270 -0
- package/dist/errors-DCqRnZpN.js +35 -0
- package/dist/errors-uhP8DZTi.d.ts +67 -0
- package/dist/index.d.ts +35 -63
- package/dist/index.js +15 -19
- package/dist/object-Da1zyPCR.js +11 -0
- package/dist/openworkflow-BhzxV_Y3.d.ts +52 -0
- package/dist/openworkflow-defaults-DDig2M4s.js +26 -0
- package/dist/openworkflow-storage-Dj9zUAsk.js +28 -0
- package/dist/openworkflow.d.ts +2 -0
- package/dist/openworkflow.js +2 -0
- package/dist/{operations-tNUdj3G4.d.ts → operation-definition-nmRu_O3V.d.ts} +42 -15
- package/dist/operations-D_WJwjrS.js +333 -0
- package/dist/operations.d.ts +40 -6
- package/dist/operations.js +163 -736
- package/dist/{out-CsaJxg8m.js → out-DxLKolA0.js} +32 -1430
- package/dist/{rolldown-runtime-CMFfr-1z.js → rolldown-runtime-B4iAMlE-.js} +10 -1
- package/dist/run-error-DBWqiJHH.js +110 -0
- package/dist/run-failed-gVGo1BaF.js +18 -0
- package/dist/runtime/cloudflare-runner.d.ts +3 -5
- package/dist/runtime/cloudflare-runner.js +39 -45
- package/dist/runtime/cloudflare-shared.d.ts +2 -2
- package/dist/runtime/cloudflare-shared.js +1 -1
- package/dist/runtime/cloudflare-vite.d.ts +3 -2
- package/dist/runtime/cloudflare-vite.js +47 -1
- package/dist/runtime/dev-runs.d.ts +4 -0
- package/dist/runtime/dev-runs.js +73 -0
- package/dist/runtime/execute.d.ts +42 -5
- package/dist/runtime/execute.js +216 -17
- package/dist/runtime/openworkflow-worker.d.ts +29 -2
- package/dist/runtime/openworkflow-worker.js +156 -38
- package/dist/runtime/openworkflow.d.ts +2 -21
- package/dist/runtime/openworkflow.js +307 -44
- package/dist/runtime/state.d.ts +3 -1
- package/dist/runtime/state.js +6 -6
- package/dist/runtime/vercel-vite.d.ts +1 -1
- package/dist/runtime/vercel-vite.js +7 -4
- package/dist/types-Dsmwsdlv.d.ts +282 -0
- package/dist/{vercel-D-ZNtB_u.js → vercel-DTvm61iY.js} +53 -24
- package/dist/vite.d.ts +228 -30
- package/dist/vite.js +2640 -1449
- package/package.json +28 -50
- package/dist/client-DsGM3M4U.js +0 -389
- package/dist/cloudflare-shared-CaxrMD1A.d.ts +0 -1
- package/dist/cloudflare-vite-B3IabBsv.js +0 -47
- package/dist/error-diagnostics-A_8Kp-48.js +0 -116
- package/dist/error-diagnostics-pwNFvAHP.js +0 -67
- package/dist/operations-B_rYG71W.js +0 -230
- package/dist/provider-operation-CW2cYfVy.js +0 -65
- package/dist/runtime-type-DBCvIh5x.js +0 -7
- package/dist/types-D82WmfZa.d.ts +0 -180
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
import { Duration, Result } from "@vite-hub/runtime";
|
|
2
|
+
//#region src/types.d.ts
|
|
3
|
+
type WorkflowDriver = "cloudflare" | "openworkflow" | "vercel";
|
|
4
|
+
interface CloudflareWorkflowBinding<TPayload = unknown> {
|
|
5
|
+
createBatch: (batch: Array<{
|
|
6
|
+
id: string;
|
|
7
|
+
params?: TPayload;
|
|
8
|
+
}>) => Promise<CloudflareWorkflowInstance[]>;
|
|
9
|
+
get: (id: string) => Promise<CloudflareWorkflowInstance>;
|
|
10
|
+
}
|
|
11
|
+
interface CloudflareWorkflowInstance {
|
|
12
|
+
id: string;
|
|
13
|
+
status: () => Promise<unknown>;
|
|
14
|
+
/** Stops the instance. Cloudflare rejects the call when the instance has already finished. */
|
|
15
|
+
terminate: () => Promise<void>;
|
|
16
|
+
}
|
|
17
|
+
interface WorkflowSharedOptions {
|
|
18
|
+
binding?: string;
|
|
19
|
+
name?: string;
|
|
20
|
+
}
|
|
21
|
+
interface CloudflareWorkflowDriverOptions extends WorkflowSharedOptions {
|
|
22
|
+
driver: "cloudflare";
|
|
23
|
+
}
|
|
24
|
+
interface VercelWorkflowDriverOptions extends WorkflowSharedOptions {
|
|
25
|
+
driver: "vercel";
|
|
26
|
+
}
|
|
27
|
+
interface OpenWorkflowPostgresOptions {
|
|
28
|
+
namespaceId?: string;
|
|
29
|
+
runMigrations?: boolean;
|
|
30
|
+
schema?: string;
|
|
31
|
+
url?: WorkflowRuntimeConfigValue;
|
|
32
|
+
}
|
|
33
|
+
interface WorkflowRuntimeEnvDeclarationLike {
|
|
34
|
+
default?: unknown;
|
|
35
|
+
kind: "env-variable";
|
|
36
|
+
source?: {
|
|
37
|
+
kind: "env";
|
|
38
|
+
name: string;
|
|
39
|
+
names?: string[];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
type WorkflowRuntimeConfigValue = string | WorkflowRuntimeEnvDeclarationLike;
|
|
43
|
+
interface OpenWorkflowSqliteOptions {
|
|
44
|
+
namespaceId?: string;
|
|
45
|
+
path?: WorkflowRuntimeConfigValue;
|
|
46
|
+
runMigrations?: boolean;
|
|
47
|
+
}
|
|
48
|
+
interface OpenWorkflowWorkerOptions {
|
|
49
|
+
/** How many runs one worker executes at once. Defaults to 10. Each Agent turn uses two runs. */
|
|
50
|
+
concurrency?: number;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* The OpenWorkflow worker that the Nitro server starts. `false` keeps the server from starting it, for apps that run
|
|
54
|
+
* a separate worker process with `startOpenWorkflowWorker()`.
|
|
55
|
+
*/
|
|
56
|
+
type OpenWorkflowWorkerOption = false | OpenWorkflowWorkerOptions;
|
|
57
|
+
interface OpenWorkflowDriverOptions extends WorkflowSharedOptions {
|
|
58
|
+
postgres?: OpenWorkflowPostgresOptions;
|
|
59
|
+
driver: "openworkflow";
|
|
60
|
+
/**
|
|
61
|
+
* `vite dev` keeps runs in the local SQLite file, even when `DATABASE_URL` or `OPENWORKFLOW_POSTGRES_URL` is set.
|
|
62
|
+
* `true` lets it use that storage, so the dev server can run the runs of a shared database. Builds ignore it.
|
|
63
|
+
*/
|
|
64
|
+
remote?: boolean;
|
|
65
|
+
sqlite?: OpenWorkflowSqliteOptions;
|
|
66
|
+
worker?: OpenWorkflowWorkerOption;
|
|
67
|
+
}
|
|
68
|
+
interface InferredWorkflowDriverOptions extends WorkflowSharedOptions {
|
|
69
|
+
postgres?: OpenWorkflowPostgresOptions;
|
|
70
|
+
driver?: undefined;
|
|
71
|
+
/** See `OpenWorkflowProviderOptions.remote`. */
|
|
72
|
+
remote?: boolean;
|
|
73
|
+
sqlite?: OpenWorkflowSqliteOptions;
|
|
74
|
+
worker?: OpenWorkflowWorkerOption;
|
|
75
|
+
}
|
|
76
|
+
type WorkflowDriverOptions = CloudflareWorkflowDriverOptions | OpenWorkflowDriverOptions | VercelWorkflowDriverOptions;
|
|
77
|
+
type WorkflowModuleDriverOptions = InferredWorkflowDriverOptions | WorkflowDriverOptions;
|
|
78
|
+
type WorkflowOptions = false | WorkflowModuleDriverOptions;
|
|
79
|
+
type ResolvedWorkflowOptions = WorkflowDriverOptions;
|
|
80
|
+
/** The failure of a failed Workflow Run. Every provider fills it, and it is safe to send as JSON. */
|
|
81
|
+
interface WorkflowRunError {
|
|
82
|
+
/** Code of the thrown error, when it has one. */
|
|
83
|
+
code?: string;
|
|
84
|
+
message: string;
|
|
85
|
+
/** Name of the thrown error, when it is not `Error`. */
|
|
86
|
+
name?: string;
|
|
87
|
+
}
|
|
88
|
+
interface WorkflowRun<TPayload = unknown, TResult = unknown> {
|
|
89
|
+
completedAt?: Date;
|
|
90
|
+
createdAt?: Date;
|
|
91
|
+
/** Set when `status` is `failed`. */
|
|
92
|
+
error?: WorkflowRunError;
|
|
93
|
+
id: string;
|
|
94
|
+
provider: WorkflowDriver;
|
|
95
|
+
result?: TResult;
|
|
96
|
+
startedAt?: Date;
|
|
97
|
+
status: WorkflowRunStatus;
|
|
98
|
+
steps?: WorkflowRunStep[];
|
|
99
|
+
metadata?: unknown;
|
|
100
|
+
payload?: TPayload;
|
|
101
|
+
}
|
|
102
|
+
type WorkflowRunStatus = "queued" | "running" | "completed" | "failed" | "cancelled" | "unknown";
|
|
103
|
+
interface WorkflowRunStep {
|
|
104
|
+
attempt: number;
|
|
105
|
+
completedAt?: Date;
|
|
106
|
+
error?: {
|
|
107
|
+
code?: string;
|
|
108
|
+
message: string;
|
|
109
|
+
};
|
|
110
|
+
id: string;
|
|
111
|
+
name: string;
|
|
112
|
+
/** The time a sleep step wakes. Only a `context.sleep()` step has it. */
|
|
113
|
+
sleepUntil?: Date;
|
|
114
|
+
startedAt?: Date;
|
|
115
|
+
status: WorkflowRunStatus;
|
|
116
|
+
}
|
|
117
|
+
/** How the wait between retries grows: `exponential` doubles it after each retry, `constant` keeps it. */
|
|
118
|
+
type WorkflowStepBackoff = "constant" | "exponential";
|
|
119
|
+
/** When a step runs again after it throws. The defaults are `{ limit: 3, delay: '10 seconds', backoff: 'exponential' }`. */
|
|
120
|
+
interface WorkflowStepRetries {
|
|
121
|
+
backoff?: WorkflowStepBackoff;
|
|
122
|
+
/** How long to wait before the first retry. */
|
|
123
|
+
delay?: WorkflowDuration;
|
|
124
|
+
/** How many times to run the step again after the first attempt fails. `0` runs it once. */
|
|
125
|
+
limit?: number;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* The options of one step. A step file exports them as `retries` and `timeout`. `timeout` is the time limit of one
|
|
129
|
+
* attempt: when it passes, the step's `signal` aborts and the attempt fails with `WORKFLOW_STEP_TIMEOUT`.
|
|
130
|
+
*/
|
|
131
|
+
interface WorkflowStepOptions {
|
|
132
|
+
retries?: WorkflowStepRetries;
|
|
133
|
+
timeout?: WorkflowDuration;
|
|
134
|
+
}
|
|
135
|
+
/** The second argument of a step function. */
|
|
136
|
+
interface WorkflowStepContext {
|
|
137
|
+
/** Aborts when the `timeout` of the step passes. Pass it to `fetch()` and other calls that take a signal. */
|
|
138
|
+
signal: AbortSignal;
|
|
139
|
+
}
|
|
140
|
+
type WorkflowStepFunction<TInput = unknown, TResult = unknown> = (input: TInput, context: WorkflowStepContext) => TResult | Promise<TResult>;
|
|
141
|
+
interface WorkflowProviderStep {
|
|
142
|
+
do?: <TResult>(name: string, options: WorkflowStepOptions, run: () => TResult | Promise<TResult>) => Promise<TResult>;
|
|
143
|
+
/** Sleeps for a number of milliseconds. ViteHub names each sleep by call order. */
|
|
144
|
+
sleep?: (name: string, milliseconds: number) => Promise<void>;
|
|
145
|
+
/** Sleeps until a date. Without it, ViteHub sleeps for the time that is left until the date. */
|
|
146
|
+
sleepUntil?: (name: string, date: Date) => Promise<void>;
|
|
147
|
+
}
|
|
148
|
+
type WorkflowDurationUnit = "second" | "minute" | "hour" | "day" | "week" | "month" | "year";
|
|
149
|
+
/**
|
|
150
|
+
* A sleep duration: milliseconds, or a string such as `'30 seconds'` or `'1 day'`. Cloudflare and OpenWorkflow read
|
|
151
|
+
* this form the same way. A month is 30 days and a year is 365.25 days.
|
|
152
|
+
*/
|
|
153
|
+
type WorkflowDuration = number | `${number} ${WorkflowDurationUnit}` | `${number} ${WorkflowDurationUnit}s`;
|
|
154
|
+
/**
|
|
155
|
+
* The step files of each folder Workflow, by Workflow name and step name. ViteHub writes it to
|
|
156
|
+
* `.vitehub/workflow.d.ts` from the numbered files in `server/workflows/<name>/`.
|
|
157
|
+
*/
|
|
158
|
+
interface WorkflowStepRegistry {}
|
|
159
|
+
/**
|
|
160
|
+
* The Workflow Definitions of the app, by name. ViteHub writes it to `.vitehub/workflow.d.ts` from the files in
|
|
161
|
+
* `server/workflows/`, so `startWorkflow()` checks the name and the payload.
|
|
162
|
+
*/
|
|
163
|
+
interface WorkflowRegistry {}
|
|
164
|
+
/** A Workflow name from `WorkflowRegistry`. Any string before ViteHub writes the registry. */
|
|
165
|
+
type WorkflowName = [keyof WorkflowRegistry] extends [never] ? string : Extract<keyof WorkflowRegistry, string>;
|
|
166
|
+
/** The payload type of the Workflow `TName`, from the `payload` that its handler reads. */
|
|
167
|
+
type WorkflowPayload<TName extends string> = TName extends keyof WorkflowRegistry ? WorkflowRegistry[TName] extends {
|
|
168
|
+
handler: (context: infer TContext) => unknown;
|
|
169
|
+
} ? TContext extends {
|
|
170
|
+
payload: infer TPayload;
|
|
171
|
+
} ? TPayload : unknown : unknown : unknown;
|
|
172
|
+
/** A step runs one input through its step file, inside a durable provider step. */
|
|
173
|
+
type WorkflowStep<TStep> = TStep extends ((...args: infer TArguments) => infer TResult) ? (...input: TArguments extends [] ? [] : TArguments extends [unknown, ...unknown[]] ? [input: TArguments[0]] : [input?: TArguments[0]]) => Promise<Awaited<TResult>> : never;
|
|
174
|
+
type StepsInFolder<TFolder> = TFolder extends unknown ? keyof TFolder : never;
|
|
175
|
+
type StepInFolder<TFolder, TStep> = TFolder extends unknown ? TStep extends keyof TFolder ? WorkflowStep<TFolder[TStep]> : never : never;
|
|
176
|
+
type WorkflowStepFolders = WorkflowStepRegistry[keyof WorkflowStepRegistry];
|
|
177
|
+
/**
|
|
178
|
+
* The step functions of the folder Workflows. A definition does not name its Workflow, so `steps` has the steps of
|
|
179
|
+
* every folder Workflow: a step name that no folder has is a type error. A shared step name accepts only inputs
|
|
180
|
+
* valid for all its implementations and returns any of their results. At runtime, a Workflow gets only its own steps.
|
|
181
|
+
*/
|
|
182
|
+
type WorkflowSteps = { readonly [TStep in StepsInFolder<WorkflowStepFolders>]: StepInFolder<WorkflowStepFolders, TStep>; };
|
|
183
|
+
/** Run data that a provider gives to ViteHub before ViteHub adds the step runner. */
|
|
184
|
+
interface WorkflowProviderContext<TPayload = unknown> {
|
|
185
|
+
id?: string;
|
|
186
|
+
name: string;
|
|
187
|
+
payload: TPayload;
|
|
188
|
+
provider: WorkflowDriver;
|
|
189
|
+
step?: WorkflowProviderStep;
|
|
190
|
+
}
|
|
191
|
+
/** The one object that a Workflow handler receives: the `payload` of the run, the run data, `sleep`, and `steps`. */
|
|
192
|
+
interface WorkflowExecutionContext<TPayload = unknown> extends Omit<WorkflowProviderContext<TPayload>, "step"> {
|
|
193
|
+
/** The provider step runner. Use `context.sleep()` to sleep. */
|
|
194
|
+
step?: Omit<WorkflowProviderStep, "sleep" | "sleepUntil">;
|
|
195
|
+
/**
|
|
196
|
+
* Sleeps for a duration such as `'1 day'` or a number of milliseconds, or until a `Date`. Only a folder Workflow's
|
|
197
|
+
* `index.ts` can sleep. In a single-file Workflow it throws `WORKFLOW_ROOT_STEP_SLEEP`. The sleep is durable on
|
|
198
|
+
* Cloudflare and OpenWorkflow. Inline runs (`vite dev` and Vercel handlers) wait with a timer, which is not durable.
|
|
199
|
+
*/
|
|
200
|
+
sleep: (until: WorkflowDuration | Date) => Promise<void>;
|
|
201
|
+
/** Step functions from the numbered files in the Workflow folder. Empty when the Workflow has no step files. */
|
|
202
|
+
steps: WorkflowSteps;
|
|
203
|
+
}
|
|
204
|
+
type WorkflowHandler<TPayload = unknown, TResult = unknown> = (context: WorkflowExecutionContext<TPayload>) => TResult | Promise<TResult>;
|
|
205
|
+
type WorkflowRunIdValue = boolean | null | number | string | undefined | {
|
|
206
|
+
readonly [key: string]: WorkflowRunIdValue;
|
|
207
|
+
} | readonly WorkflowRunIdValue[];
|
|
208
|
+
interface WorkflowCreateOptions<TPayload = unknown, TResult = unknown> {
|
|
209
|
+
handler?: WorkflowHandler<TPayload, TResult>;
|
|
210
|
+
id?: (context: {
|
|
211
|
+
name: string;
|
|
212
|
+
payload?: TPayload;
|
|
213
|
+
}) => Promise<WorkflowRunIdValue> | WorkflowRunIdValue;
|
|
214
|
+
/** @internal Identifies the Agent Workflow handler that the Agent package creates for an Agent turn. */
|
|
215
|
+
internalAgentWorkflow?: true;
|
|
216
|
+
name?: string;
|
|
217
|
+
rootStep?: boolean;
|
|
218
|
+
}
|
|
219
|
+
/** Options of `defineWorkflow()`, next to `handler`. */
|
|
220
|
+
interface WorkflowDefinitionOptions<TPayload = unknown, TResult = unknown> {
|
|
221
|
+
/** Vercel native entry. Vercel calls it with the provider context only, so it gets no step runner. */
|
|
222
|
+
native?: (context: WorkflowProviderContext<TPayload>) => TResult | Promise<TResult>;
|
|
223
|
+
rootStep?: boolean;
|
|
224
|
+
}
|
|
225
|
+
/** A Workflow Definition: `defineWorkflow({ handler, ...options })`. Its file path is its name. */
|
|
226
|
+
interface WorkflowDefinition<TPayload = unknown, TResult = unknown> extends WorkflowDefinitionOptions<TPayload, TResult> {
|
|
227
|
+
/** @internal Identifies provider-generated Agent invocation recovery definitions. */
|
|
228
|
+
internalAgentInvocationRecovery?: true;
|
|
229
|
+
/** @internal Identifies the inline Agent Workflow handler that the Agent package creates for an Agent turn. */
|
|
230
|
+
internalAgentWorkflow?: true;
|
|
231
|
+
handler: WorkflowHandler<TPayload, TResult>;
|
|
232
|
+
}
|
|
233
|
+
/** Omission is valid only when the handler accepts undefined. */
|
|
234
|
+
type WorkflowStartArguments<TPayload> = undefined extends TPayload ? [payload?: TPayload, options?: WorkflowStartOptions] : [payload: TPayload, options?: WorkflowStartOptions];
|
|
235
|
+
/**
|
|
236
|
+
* Codes that `startWorkflow()`, `getWorkflowRun()`, `cancelWorkflow()`, `resumeWorkflow()`, and the methods of a
|
|
237
|
+
* WorkflowHandle return in the `[error, data]` tuple. Config errors throw instead.
|
|
238
|
+
*/
|
|
239
|
+
type WorkflowResultErrorCode = "RUNTIME_DURATION_INVALID" | "WORKFLOW_CLOUDFLARE_RUN_ID_INVALID" | "WORKFLOW_OPERATION_UNSUPPORTED" | "WORKFLOW_PROVIDER_OPERATION_FAILED" | "WORKFLOW_R0014" | "WORKFLOW_RUN_ID_UNSUPPORTED" | "WORKFLOW_START_DELAY_INVALID";
|
|
240
|
+
/** `cancel()`, `getRun()`, and `start()` return `[error, data]`, as `cancelWorkflow()`, `getWorkflowRun()`, and `startWorkflow()` do. */
|
|
241
|
+
interface WorkflowHandle<TPayload = unknown, TResult = unknown> {
|
|
242
|
+
cancel: (id: string) => Promise<Result<WorkflowRun<unknown, unknown>, WorkflowResultErrorCode>>;
|
|
243
|
+
/** Starts a run after the response, through `waitUntil`. A failure goes to the log. */
|
|
244
|
+
defer: (...args: WorkflowStartArguments<TPayload>) => void;
|
|
245
|
+
getRun: (id: string) => Promise<Result<WorkflowRun<unknown, unknown>, WorkflowResultErrorCode>>;
|
|
246
|
+
name: string;
|
|
247
|
+
start: (...args: WorkflowStartArguments<TPayload>) => Promise<Result<WorkflowRun<TPayload, TResult>, WorkflowResultErrorCode>>;
|
|
248
|
+
}
|
|
249
|
+
/** A duration such as `"30m"` or `"3 days"`, or a `Date` that the run must not start before. */
|
|
250
|
+
type WorkflowStartDelay = Duration | Date;
|
|
251
|
+
interface WorkflowStartOptions {
|
|
252
|
+
/** Starts the run later. A `Date` in the past starts it now. */
|
|
253
|
+
delay?: WorkflowStartDelay;
|
|
254
|
+
id?: string;
|
|
255
|
+
}
|
|
256
|
+
interface WorkflowSignalResult {
|
|
257
|
+
id: string;
|
|
258
|
+
provider: WorkflowDriver;
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Maps Workflow names to module loaders. The registry does not know each
|
|
262
|
+
* payload type, so it uses `never` as the payload. A definition with any
|
|
263
|
+
* payload type fits without a cast.
|
|
264
|
+
*/
|
|
265
|
+
interface WorkflowDefinitionRegistry {
|
|
266
|
+
[name: string]: (() => Promise<{
|
|
267
|
+
default?: WorkflowDefinition<never, unknown>;
|
|
268
|
+
[exportName: string]: unknown;
|
|
269
|
+
} | WorkflowDefinition<never, unknown>>) & {
|
|
270
|
+
/** @internal Identifies provider-generated Agent invocation recovery definitions without evaluating their modules. */
|
|
271
|
+
internalAgentInvocationRecovery?: true;
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
interface DiscoveredWorkflowDefinition {
|
|
275
|
+
agentIdentity?: string;
|
|
276
|
+
handler: string;
|
|
277
|
+
name: string;
|
|
278
|
+
source?: "agent-workflow" | "agent-workflow-recovery" | "inline" | "server-workflows" | "vite-suffix";
|
|
279
|
+
steps?: string[];
|
|
280
|
+
}
|
|
281
|
+
//#endregion
|
|
282
|
+
export { WorkflowRun as A, WorkflowStep as B, WorkflowName as C, WorkflowProviderStep as D, WorkflowProviderContext as E, WorkflowSharedOptions as F, WorkflowStepRegistry as G, WorkflowStepContext as H, WorkflowSignalResult as I, WorkflowStepRetries as K, WorkflowStartArguments as L, WorkflowRunStatus as M, WorkflowRunStep as N, WorkflowRegistry as O, WorkflowRuntimeConfigValue as P, WorkflowStartDelay as R, WorkflowModuleDriverOptions as S, WorkflowPayload as T, WorkflowStepFunction as U, WorkflowStepBackoff as V, WorkflowStepOptions as W, WorkflowDuration as _, InferredWorkflowDriverOptions as a, WorkflowHandle as b, OpenWorkflowWorkerOptions as c, WorkflowCreateOptions as d, WorkflowDefinition as f, WorkflowDriverOptions as g, WorkflowDriver as h, DiscoveredWorkflowDefinition as i, WorkflowRunError as j, WorkflowResultErrorCode as k, ResolvedWorkflowOptions as l, WorkflowDefinitionRegistry as m, CloudflareWorkflowDriverOptions as n, OpenWorkflowDriverOptions as o, WorkflowDefinitionOptions as p, WorkflowSteps as q, CloudflareWorkflowInstance as r, OpenWorkflowPostgresOptions as s, CloudflareWorkflowBinding as t, VercelWorkflowDriverOptions as u, WorkflowDurationUnit as v, WorkflowOptions as w, WorkflowHandler as x, WorkflowExecutionContext as y, WorkflowStartOptions as z };
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-
|
|
2
|
-
import {
|
|
3
|
-
import { a as
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-0V2T3JFw.js";
|
|
2
|
+
import { n as createWorkflowError } from "./errors-DCqRnZpN.js";
|
|
3
|
+
import { a as workflowStartAtKey } from "./delay-D1-nq_VE.js";
|
|
4
|
+
import { a as safeWorkflowName, i as runWorkflowProviderOperation, r as isWorkflowBoundaryError, t as toWorkflowRunError } from "./run-error-DBWqiJHH.js";
|
|
4
5
|
//#region src/runtime/vercel.ts
|
|
5
6
|
async function loadVercelWorkflowRuntime() {
|
|
6
7
|
const importVercelWorkflow = new Function("specifier", "return import(specifier)");
|
|
7
8
|
const [api, runtime] = await Promise.all([importVercelWorkflow("workflow/api"), importVercelWorkflow("workflow/runtime")]);
|
|
8
9
|
return createVercelWorkflowRuntime(api, runtime);
|
|
9
10
|
}
|
|
11
|
+
/**
|
|
12
|
+
* The largest step page that Vercel's step listing accepts. A larger page fails with HTTP 400. Workflow DevKit's own
|
|
13
|
+
* CLI uses the same bound (`packages/cli/src/lib/inspect/flag-bounds.ts` in vercel/workflow).
|
|
14
|
+
*/
|
|
15
|
+
const vercelStepPageLimit = 100;
|
|
10
16
|
function createVercelWorkflowRuntime(api, runtime) {
|
|
11
17
|
const { getRun, resumeHook, start } = api;
|
|
12
18
|
const { getWorld } = runtime;
|
|
@@ -20,7 +26,7 @@ function createVercelWorkflowRuntime(api, runtime) {
|
|
|
20
26
|
const page = normalizeStepPage(await (await getWorld()).steps.list({
|
|
21
27
|
pagination: {
|
|
22
28
|
cursor,
|
|
23
|
-
limit:
|
|
29
|
+
limit: vercelStepPageLimit,
|
|
24
30
|
sortOrder: "asc"
|
|
25
31
|
},
|
|
26
32
|
resolveData: "none",
|
|
@@ -28,7 +34,7 @@ function createVercelWorkflowRuntime(api, runtime) {
|
|
|
28
34
|
}));
|
|
29
35
|
steps.push(...page.data);
|
|
30
36
|
if (!page.hasMore) break;
|
|
31
|
-
if (
|
|
37
|
+
if (typeof page.cursor !== "string" || !page.cursor || cursors.has(page.cursor)) throw invalidVercelResult("step pagination cursor");
|
|
32
38
|
cursors.add(page.cursor);
|
|
33
39
|
cursor = page.cursor;
|
|
34
40
|
} while (cursor);
|
|
@@ -65,11 +71,11 @@ function invalidVercelResult(field) {
|
|
|
65
71
|
return workflowErrorDiagnostics.WORKFLOW_R0026({ message: `Vercel Workflow provider returned an invalid ${field}.` });
|
|
66
72
|
}
|
|
67
73
|
function normalizeStepPage(page) {
|
|
68
|
-
if (
|
|
74
|
+
if (typeof page !== "object" || page === null || Array.isArray(page)) throw invalidVercelResult("step page");
|
|
69
75
|
const { cursor, data, hasMore } = page;
|
|
70
76
|
if (!Array.isArray(data)) throw invalidVercelResult("step page data");
|
|
71
|
-
if (
|
|
72
|
-
if (cursor !== void 0 && cursor !== null &&
|
|
77
|
+
if (typeof hasMore !== "boolean") throw invalidVercelResult("step page hasMore flag");
|
|
78
|
+
if (cursor !== void 0 && cursor !== null && typeof cursor !== "string") throw invalidVercelResult("step page cursor");
|
|
73
79
|
return {
|
|
74
80
|
...cursor === void 0 || cursor === null ? {} : { cursor },
|
|
75
81
|
data,
|
|
@@ -77,12 +83,12 @@ function normalizeStepPage(page) {
|
|
|
77
83
|
};
|
|
78
84
|
}
|
|
79
85
|
function normalizeStatus(status) {
|
|
80
|
-
if (
|
|
86
|
+
if (typeof status !== "string") throw invalidVercelResult("status");
|
|
81
87
|
const normalized = status.toLowerCase();
|
|
82
88
|
return Object.hasOwn(statusMap, normalized) ? statusMap[normalized] : "unknown";
|
|
83
89
|
}
|
|
84
90
|
function normalizeRunId(id) {
|
|
85
|
-
if (
|
|
91
|
+
if (typeof id !== "string" || !id) throw invalidVercelResult("run ID");
|
|
86
92
|
return id;
|
|
87
93
|
}
|
|
88
94
|
function normalizeDate(value, field) {
|
|
@@ -99,16 +105,16 @@ function normalizeDate(value, field) {
|
|
|
99
105
|
function normalizeStepError(error) {
|
|
100
106
|
if (error === void 0 || error === null) return void 0;
|
|
101
107
|
if (error instanceof Error) return { message: error.message };
|
|
102
|
-
if (
|
|
108
|
+
if (typeof error === "string") try {
|
|
103
109
|
return normalizeStepError(JSON.parse(error)) || { message: error };
|
|
104
110
|
} catch {
|
|
105
111
|
return { message: error };
|
|
106
112
|
}
|
|
107
|
-
if (
|
|
113
|
+
if (typeof error === "object" && error) {
|
|
108
114
|
const value = error;
|
|
109
|
-
if (
|
|
115
|
+
if (typeof value.message === "string") {
|
|
110
116
|
const normalized = { message: value.message };
|
|
111
|
-
if (
|
|
117
|
+
if (typeof value.code === "string") normalized.code = value.code;
|
|
112
118
|
return normalized;
|
|
113
119
|
}
|
|
114
120
|
if (value.error !== void 0) return normalizeStepError(value.error);
|
|
@@ -118,11 +124,11 @@ function normalizeStepError(error) {
|
|
|
118
124
|
function normalizeSteps(steps) {
|
|
119
125
|
if (!Array.isArray(steps)) throw invalidVercelResult("step list");
|
|
120
126
|
return steps.map((step) => {
|
|
121
|
-
if (
|
|
127
|
+
if (typeof step !== "object" || step === null) throw invalidVercelResult("step");
|
|
122
128
|
const value = step;
|
|
123
|
-
if (
|
|
124
|
-
if (
|
|
125
|
-
if (
|
|
129
|
+
if (typeof value.attempt !== "number" || !Number.isFinite(value.attempt)) throw invalidVercelResult("step attempt");
|
|
130
|
+
if (typeof value.stepId !== "string" || !value.stepId) throw invalidVercelResult("step ID");
|
|
131
|
+
if (typeof value.stepName !== "string" || !value.stepName) throw invalidVercelResult("step name");
|
|
126
132
|
return {
|
|
127
133
|
attempt: value.attempt,
|
|
128
134
|
completedAt: normalizeDate(value.completedAt, "step completion date"),
|
|
@@ -136,17 +142,30 @@ function normalizeSteps(steps) {
|
|
|
136
142
|
}
|
|
137
143
|
async function readRunIdentity(run) {
|
|
138
144
|
const exists = await run.exists;
|
|
139
|
-
if (
|
|
145
|
+
if (typeof exists !== "boolean") throw invalidVercelResult("existence state");
|
|
140
146
|
if (!exists) return { exists };
|
|
141
147
|
const workflowName = await run.workflowName;
|
|
142
|
-
if (
|
|
148
|
+
if (typeof workflowName !== "string") throw invalidVercelResult("workflow name");
|
|
143
149
|
return {
|
|
144
150
|
exists,
|
|
145
151
|
workflowName
|
|
146
152
|
};
|
|
147
153
|
}
|
|
154
|
+
/**
|
|
155
|
+
* The error of a failed native run. Workflow DevKit rejects `returnValue` of a failed run with
|
|
156
|
+
* `WorkflowRunFailedError`, which keeps the thrown value in `cause`.
|
|
157
|
+
*/
|
|
158
|
+
async function readVercelRunError(run) {
|
|
159
|
+
try {
|
|
160
|
+
await run.returnValue;
|
|
161
|
+
} catch (error) {
|
|
162
|
+
const cause = typeof error === "object" && error !== null && Reflect.get(error, "name") === "WorkflowRunFailedError" ? Reflect.get(error, "cause") : void 0;
|
|
163
|
+
return toWorkflowRunError(cause ?? error);
|
|
164
|
+
}
|
|
165
|
+
return toWorkflowRunError(void 0);
|
|
166
|
+
}
|
|
148
167
|
function getNativeWorkflowName(name, definition) {
|
|
149
|
-
const workflowName =
|
|
168
|
+
const workflowName = definition.native?.workflowId;
|
|
150
169
|
if (!workflowName) throw createWorkflowError({
|
|
151
170
|
code: "WORKFLOW_NATIVE_ENTRY_INVALID",
|
|
152
171
|
details: {
|
|
@@ -180,9 +199,11 @@ async function inspectVercelWorkflowRun(name, definition, id, payload) {
|
|
|
180
199
|
};
|
|
181
200
|
}), runWorkflowProviderOperation("vercel", "list-steps", async () => normalizeSteps(await runtime.listSteps(id)))]);
|
|
182
201
|
const result = snapshot.status === "completed" ? await runWorkflowProviderOperation("vercel", "get-run", () => run.returnValue) : void 0;
|
|
202
|
+
const error = snapshot.status === "failed" ? await readVercelRunError(run) : void 0;
|
|
183
203
|
return {
|
|
184
204
|
completedAt: snapshot.completedAt,
|
|
185
205
|
createdAt: snapshot.createdAt,
|
|
206
|
+
...error ? { error } : {},
|
|
186
207
|
id,
|
|
187
208
|
metadata: { workflow: name },
|
|
188
209
|
payload,
|
|
@@ -193,8 +214,8 @@ async function inspectVercelWorkflowRun(name, definition, id, payload) {
|
|
|
193
214
|
steps
|
|
194
215
|
};
|
|
195
216
|
}
|
|
196
|
-
async function startVercelWorkflow(name, definition, payload, observeSettlement, onDispatch) {
|
|
197
|
-
const native = definition.
|
|
217
|
+
async function startVercelWorkflow(name, definition, payload, observeSettlement, onDispatch, startAt) {
|
|
218
|
+
const native = definition.native;
|
|
198
219
|
if (!native) throw createWorkflowError({
|
|
199
220
|
code: "WORKFLOW_NATIVE_ENTRY_REQUIRED",
|
|
200
221
|
details: {
|
|
@@ -202,10 +223,18 @@ async function startVercelWorkflow(name, definition, payload, observeSettlement,
|
|
|
202
223
|
provider: "vercel"
|
|
203
224
|
}
|
|
204
225
|
});
|
|
226
|
+
if (startAt !== void 0 && Reflect.get(native, "~vitehubGenerated") !== true) throw createWorkflowError({
|
|
227
|
+
code: "WORKFLOW_OPERATION_UNSUPPORTED",
|
|
228
|
+
details: {
|
|
229
|
+
operation: "delay",
|
|
230
|
+
provider: "vercel"
|
|
231
|
+
}
|
|
232
|
+
});
|
|
205
233
|
const context = {
|
|
206
234
|
name,
|
|
207
235
|
payload,
|
|
208
|
-
provider: "vercel"
|
|
236
|
+
provider: "vercel",
|
|
237
|
+
...startAt === void 0 ? {} : { [workflowStartAtKey]: startAt }
|
|
209
238
|
};
|
|
210
239
|
const runtime = await getVercelWorkflowRuntime();
|
|
211
240
|
onDispatch?.();
|