@vite-hub/workflow 0.3.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -44
- package/dist/{app-CmMq8l4t.js → app-2Y7sBAG6.js} +1 -1
- package/dist/{client-CU5rELC0.js → client-CgBcDlGn.js} +155 -80
- package/dist/{cloudflare-BfFnNWB0.js → cloudflare-Datj_znU.js} +1 -1
- package/dist/{config-DCvRDlxA.js → config-z1651xc2.js} +1 -1
- package/dist/definition.d.ts +10 -11
- package/dist/definition.js +24 -22
- package/dist/{delay--ESDybVT.js → delay-CHN4tv3i.js} +6 -42
- package/dist/durability.d.ts +1 -1
- package/dist/{error-diagnostics-DSkCJC_L.js → error-diagnostics-C0KyI408.js} +10 -2
- package/dist/{error-diagnostics-8YNJ37rP.js → error-diagnostics-DMeaFmQD.js} +68 -19
- package/dist/{errors-DCqRnZpN.js → errors-BNxEeeCO.js} +0 -4
- package/dist/{errors-DVwFb6Fd.d.ts → errors-D-fNDVg0.d.ts} +12 -6
- package/dist/index.d.ts +32 -11
- package/dist/index.js +13 -6
- package/dist/{openworkflow-TEaNk0LV.d.ts → openworkflow-BoaENgh9.d.ts} +8 -7
- package/dist/{operation-definition-8gOe-ubD.d.ts → operation-definition-CI2PJGqu.d.ts} +13 -2
- package/dist/{operations-B0L8kV5s.js → operations-CYntpf-T.js} +34 -41
- package/dist/operations.d.ts +5 -8
- package/dist/operations.js +27 -32
- package/dist/{out-fRNaVslw.js → out-DxLKolA0.js} +1 -1
- package/dist/{rolldown-runtime-CMFfr-1z.js → rolldown-runtime-B4iAMlE-.js} +10 -1
- package/dist/{run-error-DBWqiJHH.js → run-error-Ctn50EYz.js} +1 -2
- package/dist/run-failed-gVGo1BaF.js +18 -0
- package/dist/runtime/cloudflare-runner.d.ts +12 -2
- package/dist/runtime/cloudflare-runner.js +28 -9
- package/dist/runtime/cloudflare-vite.d.ts +1 -1
- package/dist/runtime/cloudflare-vite.js +2 -2
- package/dist/runtime/dev-runs.js +1 -0
- package/dist/runtime/execute.d.ts +25 -4
- package/dist/runtime/execute.js +168 -43
- package/dist/runtime/openworkflow-worker.d.ts +2 -2
- package/dist/runtime/openworkflow-worker.js +4 -9
- package/dist/runtime/openworkflow.d.ts +2 -2
- package/dist/runtime/openworkflow.js +60 -49
- package/dist/runtime/state.d.ts +1 -1
- package/dist/runtime/state.js +4 -6
- package/dist/runtime/vercel-vite.d.ts +1 -1
- package/dist/runtime/vercel-vite.js +2 -2
- package/dist/signals-C1iolifd.js +80 -0
- package/dist/{types-CnKVDnEp.d.ts → types-BWPHeugk.d.ts} +99 -28
- package/dist/{vercel-BZU_O6ay.js → vercel-BIkBFrQQ.js} +6 -14
- package/dist/vite.d.ts +27 -16
- package/dist/vite.js +3404 -961
- package/package.json +4 -4
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-
|
|
2
|
-
import { n as createWorkflowError } from "./errors-
|
|
3
|
-
import { a as encodeNameHex, n as getCloudflareWorkflowBindingName } from "./cloudflare-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
|
+
import { n as createWorkflowError } from "./errors-BNxEeeCO.js";
|
|
3
|
+
import { a as encodeNameHex, n as getCloudflareWorkflowBindingName } from "./cloudflare-Datj_znU.js";
|
|
4
4
|
import { a as getCloudflareEnv, n as resolveWaitUntil } from "./cloudflare-env-CGLx7lUd.js";
|
|
5
|
-
import { t as normalizeWorkflowOptions } from "./config-
|
|
6
|
-
import { i as toCloudflareStartParams, r as resolveWorkflowStartAt } from "./delay
|
|
5
|
+
import { t as normalizeWorkflowOptions } from "./config-z1651xc2.js";
|
|
6
|
+
import { i as toCloudflareStartParams, r as resolveWorkflowStartAt } from "./delay-CHN4tv3i.js";
|
|
7
7
|
import { t as getWorkflowDevRecorder } from "./dev-recorder-CTN5Fkao.js";
|
|
8
|
+
import { i as readWorkflowSignalToken, o as resumeInlineWorkflowSignal } from "./signals-C1iolifd.js";
|
|
8
9
|
import { runSerializedWorkflowHandler } from "./runtime/execute.js";
|
|
9
|
-
import {
|
|
10
|
-
import { cancelOpenWorkflowRun, getOpenWorkflowRun, runOpenWorkflow } from "./runtime/openworkflow.js";
|
|
10
|
+
import { t as emitWorkflowFailed } from "./run-failed-gVGo1BaF.js";
|
|
11
11
|
import { getInlineWorkflowDefinitions, getWorkflowRunState, getWorkflowRuntimeConfig, getWorkflowRuntimeEvent, getWorkflowRuntimeRegistry, loadWorkflowDefinition, registerInlineWorkflowDefinition, runWithWorkflowRuntimeEvent, setWorkflowRun } from "./runtime/state.js";
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
12
|
+
import { cloudflareSignalEventType } from "./runtime/cloudflare-runner.js";
|
|
13
|
+
import { a as safeWorkflowName, i as runWorkflowProviderOperation, t as toWorkflowRunError } from "./run-error-Ctn50EYz.js";
|
|
14
|
+
import { cancelOpenWorkflowRun, getOpenWorkflowRun, resumeOpenWorkflowSignal, runOpenWorkflow } from "./runtime/openworkflow.js";
|
|
15
|
+
import { a as startVercelWorkflow, n as inspectVercelWorkflowRun, r as resumeVercelWorkflowSignal, t as cancelVercelWorkflow } from "./vercel-BIkBFrQQ.js";
|
|
16
|
+
import { getViteHubErrorShape, isRuntimeDiagnostic, toResultError } from "@vite-hub/runtime";
|
|
14
17
|
import { getViteHubTestMode } from "@vite-hub/runtime/internal/test-mode";
|
|
18
|
+
import { deserializeResponse, isSerializedResponse } from "@vite-hub/runtime/internal/response";
|
|
15
19
|
//#region ../internal/src/runtime/random.ts
|
|
16
20
|
function randomId(prefix) {
|
|
17
21
|
const body = `${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 10)}`;
|
|
@@ -24,6 +28,17 @@ function getVercelWorkflowName(name) {
|
|
|
24
28
|
}
|
|
25
29
|
//#endregion
|
|
26
30
|
//#region src/runtime/adapters.ts
|
|
31
|
+
async function resumeViteHubSignal(config, token, payload, durable) {
|
|
32
|
+
const address = readWorkflowSignalToken(token);
|
|
33
|
+
if (!address) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_TOKEN_INVALID({ provider: config.driver });
|
|
34
|
+
if (address.host === "inline") resumeInlineWorkflowSignal(token, address, payload);
|
|
35
|
+
else if (address.host === config.driver && durable) await durable(address);
|
|
36
|
+
else throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_TOKEN_INVALID({ provider: config.driver });
|
|
37
|
+
return {
|
|
38
|
+
id: address.run,
|
|
39
|
+
provider: config.driver
|
|
40
|
+
};
|
|
41
|
+
}
|
|
27
42
|
function resolveDispatchObserver(event) {
|
|
28
43
|
if (!event || typeof event !== "object") return void 0;
|
|
29
44
|
const candidate = Object.hasOwn(event, "onDispatch") ? Reflect.get(event, "onDispatch") : void 0;
|
|
@@ -48,19 +63,11 @@ function unsupportedOperation(provider, operation) {
|
|
|
48
63
|
}
|
|
49
64
|
});
|
|
50
65
|
}
|
|
51
|
-
/**
|
|
52
|
-
* True on a development server: `vite dev` installs the run history. Code that ViteHub did not start, such as a test
|
|
53
|
-
* runner, has no history, so `NODE_ENV` decides there.
|
|
54
|
-
*/
|
|
55
66
|
function isDevelopmentServer() {
|
|
56
67
|
if (getWorkflowDevRecorder()) return true;
|
|
57
68
|
const mode = globalThis.process?.env?.NODE_ENV;
|
|
58
69
|
return mode === "development" || mode === "test";
|
|
59
70
|
}
|
|
60
|
-
/**
|
|
61
|
-
* The Cloudflare Workflow binding of `name`. Only a development server runs the Workflow inline without it. Elsewhere,
|
|
62
|
-
* a missing binding fails, because an inline run is not durable.
|
|
63
|
-
*/
|
|
64
71
|
function resolveCloudflareBinding(event, binding, name, definition) {
|
|
65
72
|
const env = getCloudflareEnv(event);
|
|
66
73
|
const bindingName = definition?.internalAgentInvocationRecovery ? getCloudflareWorkflowBindingName(name) : binding || getCloudflareWorkflowBindingName(name);
|
|
@@ -74,7 +81,6 @@ function resolveCloudflareBinding(event, binding, name, definition) {
|
|
|
74
81
|
function resolveCloudflareInspectionBinding(event, binding, name) {
|
|
75
82
|
return resolveCloudflareBinding(event, binding, name, getWorkflowRuntimeRegistry()?.[name]);
|
|
76
83
|
}
|
|
77
|
-
/** The start can have reached Cloudflare, so the acknowledgement is unknown. */
|
|
78
84
|
function invalidCloudflareWorkflowStart(cause) {
|
|
79
85
|
return createWorkflowError({
|
|
80
86
|
...cause === void 0 ? {} : { cause },
|
|
@@ -99,7 +105,6 @@ function normalizeCloudflareWorkflowStart(value, expectedId) {
|
|
|
99
105
|
return { id };
|
|
100
106
|
}
|
|
101
107
|
const cloudflareInstanceIdPattern = /^[a-zA-Z0-9_][a-zA-Z0-9_-]{0,99}$/;
|
|
102
|
-
/** The array-form `createBatch` accepts reserved ids, unlike the object form. */
|
|
103
108
|
function isCloudflareInstanceId(id) {
|
|
104
109
|
return cloudflareInstanceIdPattern.test(id);
|
|
105
110
|
}
|
|
@@ -122,18 +127,15 @@ function normalizeCloudflareStatus(status) {
|
|
|
122
127
|
const normalized = String(value || "").toLowerCase();
|
|
123
128
|
return Object.hasOwn(cloudflareStatusMap, normalized) ? cloudflareStatusMap[normalized] : "unknown";
|
|
124
129
|
}
|
|
125
|
-
/** The run result from Cloudflare status: the stored value, or a `Response` when the handler returned one. */
|
|
126
130
|
function cloudflareRunResult(metadata) {
|
|
127
131
|
if (typeof metadata !== "object" || metadata === null || !Object.hasOwn(metadata, "output")) return void 0;
|
|
128
132
|
const output = metadata.output;
|
|
129
133
|
return isSerializedResponse(output) ? deserializeResponse(output) : output;
|
|
130
134
|
}
|
|
131
|
-
/** The `error` member of a Cloudflare `status()` record. */
|
|
132
135
|
function ownStatusError(metadata) {
|
|
133
136
|
if (typeof metadata !== "object" || metadata === null || !Object.hasOwn(metadata, "error")) return void 0;
|
|
134
137
|
return Reflect.get(metadata, "error");
|
|
135
138
|
}
|
|
136
|
-
/** Cloudflare `get(id)` rejects with `instance.not_found` when no instance has the id. */
|
|
137
139
|
function isCloudflareInstanceNotFound(error) {
|
|
138
140
|
if (typeof error !== "object" || error === null) return false;
|
|
139
141
|
try {
|
|
@@ -143,10 +145,6 @@ function isCloudflareInstanceNotFound(error) {
|
|
|
143
145
|
return false;
|
|
144
146
|
}
|
|
145
147
|
}
|
|
146
|
-
/**
|
|
147
|
-
* The Cloudflare Workflows service refused the call: its error carries a service code such as `(instance.already_exists)`,
|
|
148
|
-
* or local Miniflare rejected the instance id. A lost connection has neither.
|
|
149
|
-
*/
|
|
150
148
|
function isCloudflareRejection(error) {
|
|
151
149
|
if (typeof error !== "object" || error === null) return false;
|
|
152
150
|
try {
|
|
@@ -161,7 +159,6 @@ function hasUnknownWorkflowAcknowledgement(error) {
|
|
|
161
159
|
const details = error.details;
|
|
162
160
|
return Boolean(details && typeof details === "object" && details.acknowledgement === "unknown");
|
|
163
161
|
}
|
|
164
|
-
/** Reads a Cloudflare Workflow instance and its run. A missing instance has no `instance` and reports `unknown`. */
|
|
165
162
|
async function readCloudflareRun(binding, id) {
|
|
166
163
|
const instance = await runWorkflowProviderOperation("cloudflare", "get", async () => {
|
|
167
164
|
try {
|
|
@@ -195,10 +192,6 @@ const finishedRunStatuses = /* @__PURE__ */ new Set([
|
|
|
195
192
|
"completed",
|
|
196
193
|
"failed"
|
|
197
194
|
]);
|
|
198
|
-
/**
|
|
199
|
-
* Terminates a Cloudflare Workflow instance that has not finished and returns its run afterwards. A finished run keeps
|
|
200
|
-
* and reports its status, and a missing run reports `unknown`, as on OpenWorkflow.
|
|
201
|
-
*/
|
|
202
195
|
async function cancelCloudflareRun(binding, id) {
|
|
203
196
|
const { instance, run } = await readCloudflareRun(binding, id);
|
|
204
197
|
if (!instance || run.status === "unknown" || finishedRunStatuses.has(run.status)) return run;
|
|
@@ -267,18 +260,28 @@ function createCloudflareAdapter(config) {
|
|
|
267
260
|
}
|
|
268
261
|
return await inlineAdapter(config).run(context);
|
|
269
262
|
},
|
|
270
|
-
resume: () =>
|
|
263
|
+
resume: async (token, payload) => await resumeViteHubSignal(config, token, payload, async ({ run, signal, workflow }) => {
|
|
264
|
+
const binding = resolveCloudflareInspectionBinding(getWorkflowRuntimeEvent(), config.binding, workflow);
|
|
265
|
+
if (!binding) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_NOT_WAITING({ run });
|
|
266
|
+
const { instance, run: current } = await readCloudflareRun(binding, run);
|
|
267
|
+
if (!instance || finishedRunStatuses.has(current.status)) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_NOT_WAITING({ run });
|
|
268
|
+
await runWorkflowProviderOperation("cloudflare", "resume-signal", async () => {
|
|
269
|
+
if (!instance.sendEvent) unsupportedOperation("cloudflare", "signals");
|
|
270
|
+
try {
|
|
271
|
+
await instance.sendEvent({
|
|
272
|
+
payload,
|
|
273
|
+
type: cloudflareSignalEventType(signal)
|
|
274
|
+
});
|
|
275
|
+
} catch (error) {
|
|
276
|
+
if (isCloudflareInstanceNotFound(error)) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_NOT_WAITING({ run });
|
|
277
|
+
throw error;
|
|
278
|
+
}
|
|
279
|
+
});
|
|
280
|
+
})
|
|
271
281
|
};
|
|
272
282
|
return adapter;
|
|
273
283
|
}
|
|
274
284
|
const warnedInlineOpenWorkflowNames = /* @__PURE__ */ new Set();
|
|
275
|
-
/**
|
|
276
|
-
* Every OpenWorkflow worker registers the Workflow Definitions of the build registry when it starts: discovered
|
|
277
|
-
* Workflows and discovered Agent Workflows. Any worker can claim a run, so a run of an inline
|
|
278
|
-
* `createWorkflow(name, handler)` could wait in storage on a worker that never registers it. Those runs execute inline
|
|
279
|
-
* in this process. A turn of an Agent that is not discovered fails with `WORKFLOW_AGENT_NOT_DISCOVERED`, so Agent turns are
|
|
280
|
-
* never inline.
|
|
281
|
-
*/
|
|
282
285
|
function isDurableOpenWorkflowDefinition(name) {
|
|
283
286
|
const registry = getWorkflowRuntimeRegistry();
|
|
284
287
|
return Boolean(registry && Object.hasOwn(registry, name));
|
|
@@ -296,7 +299,9 @@ function createOpenWorkflowAdapter(config) {
|
|
|
296
299
|
id,
|
|
297
300
|
name
|
|
298
301
|
}),
|
|
299
|
-
resume: () =>
|
|
302
|
+
resume: async (token, payload) => await resumeViteHubSignal(config, token, payload, async ({ run, workflow }) => {
|
|
303
|
+
if (!await resumeOpenWorkflowSignal(config, workflow, run, token, payload)) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_NOT_WAITING({ run });
|
|
304
|
+
}),
|
|
300
305
|
run: async (context) => {
|
|
301
306
|
const { definition, event, name, options, payload, startAt } = context;
|
|
302
307
|
if (isDurableOpenWorkflowDefinition(name)) return await runOpenWorkflow(config, name, payload, definition, options, resolveDispatchObserver(event), startAt);
|
|
@@ -315,11 +320,27 @@ function createOpenWorkflowAdapter(config) {
|
|
|
315
320
|
}
|
|
316
321
|
};
|
|
317
322
|
}
|
|
318
|
-
/** `setTimeout` fires at once for a delay above 2^31 - 1 ms (about 24.8 days), so a long wait uses several timers. */
|
|
319
323
|
const maximumTimerDelay = 2147483647;
|
|
324
|
+
const warnedDelayedInlineWorkflowNames = /* @__PURE__ */ new Set();
|
|
325
|
+
function warnDelayedInlineRun(name, startAt) {
|
|
326
|
+
if (startAt === void 0 || startAt <= Date.now() || isDevelopmentServer() || warnedDelayedInlineWorkflowNames.has(name)) return;
|
|
327
|
+
warnedDelayedInlineWorkflowNames.add(name);
|
|
328
|
+
console.warn(`[vitehub:workflow] The Workflow ${JSON.stringify(name)} starts later in an inline run. A timer in this process holds the run, so a restart loses it. Use OpenWorkflow, Cloudflare, or a Vercel \`native\` entry for a durable delay.`);
|
|
329
|
+
}
|
|
320
330
|
async function waitUntilTime(startAt) {
|
|
321
331
|
while (startAt !== void 0 && startAt > Date.now()) await new Promise((resolve) => setTimeout(resolve, Math.min(startAt - Date.now(), maximumTimerDelay)));
|
|
322
332
|
}
|
|
333
|
+
function recordedDevRun(config, name, id) {
|
|
334
|
+
const record = getWorkflowDevRecorder()?.get(name, id);
|
|
335
|
+
if (record?.status !== "completed" && record?.status !== "failed") return;
|
|
336
|
+
return {
|
|
337
|
+
...record.status === "failed" ? { error: toWorkflowRunError(record.error) } : {},
|
|
338
|
+
id,
|
|
339
|
+
provider: config.driver,
|
|
340
|
+
result: isSerializedResponse(record.result) ? deserializeResponse(record.result) : record.result,
|
|
341
|
+
status: record.status
|
|
342
|
+
};
|
|
343
|
+
}
|
|
323
344
|
function inlineAdapter(config) {
|
|
324
345
|
return {
|
|
325
346
|
cancel: () => unsupportedOperation(config.driver, "cancellation"),
|
|
@@ -339,6 +360,8 @@ function inlineAdapter(config) {
|
|
|
339
360
|
status: run.status
|
|
340
361
|
};
|
|
341
362
|
}
|
|
363
|
+
const recorded = recordedDevRun(config, name, id);
|
|
364
|
+
if (recorded) return recorded;
|
|
342
365
|
return {
|
|
343
366
|
id,
|
|
344
367
|
provider: config.driver,
|
|
@@ -347,6 +370,14 @@ function inlineAdapter(config) {
|
|
|
347
370
|
},
|
|
348
371
|
async run({ definition, event, id, name, payload, startAt }) {
|
|
349
372
|
const existing = getWorkflowRunState(name, id);
|
|
373
|
+
const recorded = existing ? void 0 : recordedDevRun(config, name, id);
|
|
374
|
+
if (recorded) return {
|
|
375
|
+
...recorded,
|
|
376
|
+
metadata: config.driver === "vercel" ? {
|
|
377
|
+
mode: "inline",
|
|
378
|
+
workflow: getVercelWorkflowName(name)
|
|
379
|
+
} : void 0
|
|
380
|
+
};
|
|
350
381
|
if (existing) return {
|
|
351
382
|
...existing.status === "failed" ? { error: toWorkflowRunError(existing.error) } : {},
|
|
352
383
|
id,
|
|
@@ -367,6 +398,7 @@ function inlineAdapter(config) {
|
|
|
367
398
|
payload,
|
|
368
399
|
provider: config.driver
|
|
369
400
|
}, definition);
|
|
401
|
+
warnDelayedInlineRun(name, startAt);
|
|
370
402
|
const run = waitUntilTime(startAt).then(() => {
|
|
371
403
|
runState.startAt = void 0;
|
|
372
404
|
return recordRun ? recordRun(execute) : execute();
|
|
@@ -379,8 +411,10 @@ function inlineAdapter(config) {
|
|
|
379
411
|
}));
|
|
380
412
|
const runState = setWorkflowRun(name, id, run, payload, true);
|
|
381
413
|
runState.startAt = startAt;
|
|
382
|
-
const
|
|
383
|
-
|
|
414
|
+
const notify = runState.promise.then(async (outcome) => {
|
|
415
|
+
if (outcome.status === "failed") await emitWorkflowFailed(name, id, outcome.error, definition);
|
|
416
|
+
});
|
|
417
|
+
resolveWaitUntil(event)?.(notify);
|
|
384
418
|
return {
|
|
385
419
|
id,
|
|
386
420
|
metadata: config.driver === "vercel" ? {
|
|
@@ -392,20 +426,15 @@ function inlineAdapter(config) {
|
|
|
392
426
|
status: "queued"
|
|
393
427
|
};
|
|
394
428
|
},
|
|
395
|
-
resume: () =>
|
|
429
|
+
resume: async (token, payload) => await resumeViteHubSignal(config, token, payload)
|
|
396
430
|
};
|
|
397
431
|
}
|
|
398
|
-
/**
|
|
399
|
-
* True when Workflow DevKit runs the `native` entry. `vite dev` does not run the Workflow DevKit transform, so the
|
|
400
|
-
* dev server runs the normal handler inline, as for every other Workflow there.
|
|
401
|
-
*/
|
|
402
432
|
function runsNatively(definition) {
|
|
403
|
-
return Boolean(definition?.
|
|
433
|
+
return Boolean(definition?.native) && !getWorkflowDevRecorder();
|
|
404
434
|
}
|
|
405
|
-
/** Whether a registered Workflow has a native Vercel entry, so a run of it can wait for a signal. */
|
|
406
435
|
async function hasNativeWorkflowDefinition() {
|
|
407
436
|
const names = /* @__PURE__ */ new Set([...Object.keys(getWorkflowRuntimeRegistry() ?? {}), ...getInlineWorkflowDefinitions().keys()]);
|
|
408
|
-
for (const name of names) if ((await loadWorkflowDefinition(name))?.
|
|
437
|
+
for (const name of names) if ((await loadWorkflowDefinition(name))?.native) return true;
|
|
409
438
|
return false;
|
|
410
439
|
}
|
|
411
440
|
function createVercelAdapter(config) {
|
|
@@ -429,6 +458,7 @@ function createVercelAdapter(config) {
|
|
|
429
458
|
});
|
|
430
459
|
},
|
|
431
460
|
async resume(token, payload) {
|
|
461
|
+
if (readWorkflowSignalToken(token)) throw workflowErrorDiagnostics.WORKFLOW_SIGNAL_TOKEN_INVALID({ provider: config.driver });
|
|
432
462
|
if (getWorkflowDevRecorder()) unsupportedOperation(config.driver, "signals");
|
|
433
463
|
try {
|
|
434
464
|
return await resumeVercelWorkflowSignal(token, payload);
|
|
@@ -491,6 +521,44 @@ function getWorkflowTestRun(test, provider, name, id, cancel = false) {
|
|
|
491
521
|
}
|
|
492
522
|
//#endregion
|
|
493
523
|
//#region src/runtime/client.ts
|
|
524
|
+
const resultErrorCodes = /* @__PURE__ */ new Set([
|
|
525
|
+
"RUNTIME_DURATION_INVALID",
|
|
526
|
+
"WORKFLOW_CLOUDFLARE_RUN_ID_INVALID",
|
|
527
|
+
"WORKFLOW_OPERATION_UNSUPPORTED",
|
|
528
|
+
"WORKFLOW_PROVIDER_OPERATION_FAILED",
|
|
529
|
+
"WORKFLOW_R0014",
|
|
530
|
+
"WORKFLOW_RUN_ID_UNSUPPORTED",
|
|
531
|
+
"WORKFLOW_SIGNAL_NOT_WAITING",
|
|
532
|
+
"WORKFLOW_SIGNAL_TOKEN_INVALID",
|
|
533
|
+
"WORKFLOW_START_DELAY_INVALID"
|
|
534
|
+
]);
|
|
535
|
+
function resultError(diagnostic) {
|
|
536
|
+
return toResultError(diagnostic);
|
|
537
|
+
}
|
|
538
|
+
function toWorkflowResultError(error) {
|
|
539
|
+
if (isRuntimeDiagnostic(error) && resultErrorCodes.has(error.code)) return resultError(error);
|
|
540
|
+
const shape = getViteHubErrorShape(error);
|
|
541
|
+
if (!shape || !resultErrorCodes.has(shape.code)) throw error;
|
|
542
|
+
const cause = error instanceof Error ? error.cause : void 0;
|
|
543
|
+
const details = shape.details ?? {};
|
|
544
|
+
if (shape.code === "WORKFLOW_PROVIDER_OPERATION_FAILED") return resultError(workflowErrorDiagnostics.WORKFLOW_PROVIDER_OPERATION_FAILED({
|
|
545
|
+
...details,
|
|
546
|
+
cause
|
|
547
|
+
}));
|
|
548
|
+
if (shape.code === "WORKFLOW_RUN_ID_UNSUPPORTED") return resultError(workflowErrorDiagnostics.WORKFLOW_RUN_ID_UNSUPPORTED({ cause }));
|
|
549
|
+
return resultError(workflowErrorDiagnostics.WORKFLOW_OPERATION_UNSUPPORTED({
|
|
550
|
+
cause,
|
|
551
|
+
operation: details.operation,
|
|
552
|
+
provider: details.provider
|
|
553
|
+
}));
|
|
554
|
+
}
|
|
555
|
+
async function toWorkflowResult(run) {
|
|
556
|
+
try {
|
|
557
|
+
return [void 0, await run()];
|
|
558
|
+
} catch (error) {
|
|
559
|
+
return [toWorkflowResultError(error), void 0];
|
|
560
|
+
}
|
|
561
|
+
}
|
|
494
562
|
function getActiveWorkflowConfig() {
|
|
495
563
|
const config = getWorkflowRuntimeConfig();
|
|
496
564
|
if (config === false) throw createWorkflowError({ code: "WORKFLOW_DISABLED" });
|
|
@@ -551,7 +619,7 @@ function createWorkflow(nameOrOptions, handlerOrOptions, options) {
|
|
|
551
619
|
if (handler !== void 0) registerInlineWorkflowDefinition(name, {
|
|
552
620
|
handler,
|
|
553
621
|
...createOptions?.internalAgentWorkflow ? { internalAgentWorkflow: true } : {},
|
|
554
|
-
...createOptions?.rootStep === void 0 ? {} : {
|
|
622
|
+
...createOptions?.rootStep === void 0 ? {} : { rootStep: createOptions.rootStep }
|
|
555
623
|
});
|
|
556
624
|
return {
|
|
557
625
|
cancel: (id) => cancelWorkflow(name, id),
|
|
@@ -559,20 +627,20 @@ function createWorkflow(nameOrOptions, handlerOrOptions, options) {
|
|
|
559
627
|
defer: (payload, options = {}) => deferWorkflowStart(name, async () => await startWorkflowRun(name, payload, await resolveWorkflowStartOptions(name, payload, createOptions, options), true)),
|
|
560
628
|
getRun: (id) => getWorkflowRun(name, id),
|
|
561
629
|
async start(payload, options = {}) {
|
|
562
|
-
|
|
630
|
+
const startOptions = await resolveWorkflowStartOptions(name, payload, createOptions, options);
|
|
631
|
+
return await toWorkflowResult(() => startWorkflowRun(name, payload, startOptions));
|
|
563
632
|
}
|
|
564
633
|
};
|
|
565
634
|
}
|
|
566
|
-
/** Starts one durable run of a Workflow Definition and returns its id and status. */
|
|
567
635
|
async function startWorkflow(name, payload, options = {}) {
|
|
568
|
-
return await startWorkflowRun(name, payload, options);
|
|
636
|
+
return await toWorkflowResult(() => startWorkflowRun(name, payload, options));
|
|
569
637
|
}
|
|
570
638
|
async function startWorkflowRun(name, payload, options, deferred = false) {
|
|
571
639
|
const config = getActiveWorkflowConfig();
|
|
572
640
|
const id = options.id || randomId("wrun");
|
|
573
641
|
const startAt = resolveWorkflowStartAt(options.delay);
|
|
574
642
|
const definition = await loadRequiredWorkflowDefinition(name);
|
|
575
|
-
if (config.driver === "vercel" && definition.
|
|
643
|
+
if (config.driver === "vercel" && definition.native && options.id) throw createWorkflowError({
|
|
576
644
|
code: "WORKFLOW_RUN_ID_UNSUPPORTED",
|
|
577
645
|
details: {
|
|
578
646
|
...safeWorkflowName(name) ? { name } : {},
|
|
@@ -596,48 +664,55 @@ async function startWorkflowRun(name, payload, options, deferred = false) {
|
|
|
596
664
|
startAt
|
|
597
665
|
});
|
|
598
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Reads one run and returns `[error, run]`. A provider failure is `error`. A run that the provider does not know has
|
|
669
|
+
* `status: "unknown"`. Config errors throw.
|
|
670
|
+
*/
|
|
599
671
|
async function getWorkflowRun(name, id) {
|
|
600
672
|
const config = getActiveWorkflowConfig();
|
|
601
673
|
const test = getViteHubTestMode();
|
|
602
|
-
if (test) return getWorkflowTestRun(test, config.driver, name, id);
|
|
603
|
-
return await getWorkflowRuntimeAdapter(config).get({
|
|
674
|
+
if (test) return [void 0, getWorkflowTestRun(test, config.driver, name, id)];
|
|
675
|
+
return await toWorkflowResult(() => getWorkflowRuntimeAdapter(config).get({
|
|
604
676
|
event: getWorkflowRuntimeEvent(),
|
|
605
677
|
id,
|
|
606
678
|
name
|
|
607
|
-
});
|
|
679
|
+
}));
|
|
608
680
|
}
|
|
681
|
+
/**
|
|
682
|
+
* Cancels one run and returns `[error, run]`. A provider failure, or a provider that cannot cancel the run, is
|
|
683
|
+
* `error`. Config errors, for example an unknown Workflow name, throw.
|
|
684
|
+
*/
|
|
609
685
|
async function cancelWorkflow(name, id) {
|
|
610
686
|
const config = getActiveWorkflowConfig();
|
|
611
687
|
const definition = await loadRequiredWorkflowDefinition(name);
|
|
612
688
|
const test = getViteHubTestMode();
|
|
613
689
|
if (test) {
|
|
614
|
-
if (config.driver === "vercel" && !definition.
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
}
|
|
620
|
-
});
|
|
621
|
-
return getWorkflowTestRun(test, config.driver, name, id, true);
|
|
690
|
+
if (config.driver === "vercel" && !definition.native) return [resultError(workflowErrorDiagnostics.WORKFLOW_OPERATION_UNSUPPORTED({
|
|
691
|
+
operation: "cancellation",
|
|
692
|
+
provider: config.driver
|
|
693
|
+
})), void 0];
|
|
694
|
+
return [void 0, getWorkflowTestRun(test, config.driver, name, id, true)];
|
|
622
695
|
}
|
|
623
|
-
return await getWorkflowRuntimeAdapter(config).cancel({
|
|
696
|
+
return await toWorkflowResult(() => getWorkflowRuntimeAdapter(config).cancel({
|
|
624
697
|
event: getWorkflowRuntimeEvent(),
|
|
625
698
|
id,
|
|
626
699
|
name
|
|
627
|
-
});
|
|
700
|
+
}));
|
|
628
701
|
}
|
|
629
|
-
/**
|
|
702
|
+
/**
|
|
703
|
+
* Resumes a run that waits for a signal and returns `[error, signal]`. `token` is `signal.token` from
|
|
704
|
+
* `context.createSignal()`, or the hook token of a native Vercel entry. `payload` is the value that `signal.wait()`
|
|
705
|
+
* returns: send a JSON value. An invalid token, a run that does not wait for the token, or a provider failure is
|
|
706
|
+
* `error`. Config errors throw.
|
|
707
|
+
*/
|
|
630
708
|
async function resumeWorkflow(token, payload) {
|
|
631
|
-
if (!token || typeof token !== "string")
|
|
709
|
+
if (!token || typeof token !== "string") return [resultError(workflowErrorDiagnostics.WORKFLOW_R0014({ message: "`resumeWorkflow()` requires an opaque signal token." })), void 0];
|
|
632
710
|
const config = getActiveWorkflowConfig();
|
|
633
|
-
if (getViteHubTestMode())
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
return await getWorkflowRuntimeAdapter(config).resume(token, payload);
|
|
711
|
+
if (getViteHubTestMode()) return [resultError(workflowErrorDiagnostics.WORKFLOW_OPERATION_UNSUPPORTED({
|
|
712
|
+
operation: "signals",
|
|
713
|
+
provider: config.driver
|
|
714
|
+
})), void 0];
|
|
715
|
+
return await toWorkflowResult(() => getWorkflowRuntimeAdapter(config).resume(token, payload));
|
|
641
716
|
}
|
|
642
717
|
function deferWorkflowStart(name, start) {
|
|
643
718
|
const request = getWorkflowRuntimeEvent();
|
|
@@ -651,7 +726,7 @@ function deferWorkflowStart(name, start) {
|
|
|
651
726
|
* Starts a run after the response, through `waitUntil`. It returns nothing: a failure goes to the log.
|
|
652
727
|
* Call `startWorkflow()` when the caller needs the run ID or the start error.
|
|
653
728
|
*/
|
|
654
|
-
function deferWorkflow(name, payload, options = {}) {
|
|
729
|
+
function deferWorkflow(...[name, payload, options = {}]) {
|
|
655
730
|
deferWorkflowStart(name, () => startWorkflowRun(name, payload, options, true));
|
|
656
731
|
}
|
|
657
732
|
//#endregion
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
2
|
//#region ../internal/src/integrations/hex.ts
|
|
3
3
|
const textEncoder = new TextEncoder();
|
|
4
4
|
new TextDecoder();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-8YNJ37rP.js";
|
|
2
1
|
import { t as isRecord } from "./object-Da1zyPCR.js";
|
|
2
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
3
3
|
import { defu } from "defu";
|
|
4
4
|
/** Maps a host name or a Nitro preset name, such as `cloudflare-module` or `node-server`, to its ViteHub host. */
|
|
5
5
|
function hostFromNitroPreset(value) {
|
package/dist/definition.d.ts
CHANGED
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import { f as WorkflowDefinition
|
|
1
|
+
import { f as WorkflowDefinition } from "./types-BWPHeugk.js";
|
|
2
2
|
//#region src/definition.d.ts
|
|
3
3
|
/**
|
|
4
|
-
* Defines
|
|
5
|
-
*
|
|
4
|
+
* Defines a Workflow from one object: `defineWorkflow({ handler, ...options })`. The file path is the Workflow name.
|
|
5
|
+
* The handler receives one object, `{ payload, steps, sleep, ...context }`. Pass the payload type as the type argument.
|
|
6
|
+
* It types `startWorkflow()` for this Workflow.
|
|
6
7
|
*
|
|
7
8
|
* ```ts
|
|
8
|
-
* export default defineWorkflow
|
|
9
|
-
*
|
|
10
|
-
*
|
|
9
|
+
* export default defineWorkflow<{ email: string }>({
|
|
10
|
+
* handler: async ({ payload, steps }) => {
|
|
11
|
+
* const user = await steps.createUser(payload)
|
|
12
|
+
* return await steps.sendWelcome(user)
|
|
13
|
+
* },
|
|
11
14
|
* })
|
|
12
15
|
* ```
|
|
13
16
|
*/
|
|
14
|
-
export declare function defineWorkflow<
|
|
15
|
-
/** Throws when a definition names another Workflow than the one ViteHub discovered at its path. */
|
|
16
|
-
export declare function assertWorkflowDefinitionName(discoveredName: string, definition: {
|
|
17
|
-
name?: string;
|
|
18
|
-
} | undefined): void;
|
|
17
|
+
export declare function defineWorkflow<TPayload = undefined, TResult = unknown>(definition: WorkflowDefinition<TPayload, TResult>, ...removed: unknown[]): WorkflowDefinition<TPayload, TResult>;
|
|
19
18
|
//#endregion
|
package/dist/definition.js
CHANGED
|
@@ -1,31 +1,33 @@
|
|
|
1
|
-
import { t as
|
|
1
|
+
import { t as isRecord } from "./object-Da1zyPCR.js";
|
|
2
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
3
|
//#region src/definition.ts
|
|
4
|
+
const workflowDefinitionKeys = /* @__PURE__ */ new Set([
|
|
5
|
+
"handler",
|
|
6
|
+
"native",
|
|
7
|
+
"rootStep"
|
|
8
|
+
]);
|
|
3
9
|
/**
|
|
4
|
-
* Defines
|
|
5
|
-
*
|
|
10
|
+
* Defines a Workflow from one object: `defineWorkflow({ handler, ...options })`. The file path is the Workflow name.
|
|
11
|
+
* The handler receives one object, `{ payload, steps, sleep, ...context }`. Pass the payload type as the type argument.
|
|
12
|
+
* It types `startWorkflow()` for this Workflow.
|
|
6
13
|
*
|
|
7
14
|
* ```ts
|
|
8
|
-
* export default defineWorkflow
|
|
9
|
-
*
|
|
10
|
-
*
|
|
15
|
+
* export default defineWorkflow<{ email: string }>({
|
|
16
|
+
* handler: async ({ payload, steps }) => {
|
|
17
|
+
* const user = await steps.createUser(payload)
|
|
18
|
+
* return await steps.sendWelcome(user)
|
|
19
|
+
* },
|
|
11
20
|
* })
|
|
12
21
|
* ```
|
|
13
22
|
*/
|
|
14
|
-
function defineWorkflow(
|
|
15
|
-
if (typeof
|
|
16
|
-
if (
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
options
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
/** Throws when a definition names another Workflow than the one ViteHub discovered at its path. */
|
|
26
|
-
function assertWorkflowDefinitionName(discoveredName, definition) {
|
|
27
|
-
if (definition?.name === void 0 || definition.name === discoveredName) return;
|
|
28
|
-
throw workflowErrorDiagnostics.WORKFLOW_C0019({ message: `[vitehub] The Workflow at ${JSON.stringify(discoveredName)} calls \`defineWorkflow(${JSON.stringify(definition.name)})\`. Pass ${JSON.stringify(discoveredName)}, the name that ViteHub reads from its folder or file name.` });
|
|
23
|
+
function defineWorkflow(definition, ...removed) {
|
|
24
|
+
if (typeof definition === "string" || typeof definition === "function" || removed.length) throw workflowErrorDiagnostics.WORKFLOW_C0015({ message: "`defineWorkflow(name, handler, options)` was removed. The file path is the Workflow name. Pass one object: `defineWorkflow({ handler, ...options })`." });
|
|
25
|
+
if (!isRecord(definition)) throw workflowErrorDiagnostics.WORKFLOW_C0015({ message: "`defineWorkflow()` expects one object with `handler`." });
|
|
26
|
+
const unknownKey = Object.keys(definition).find((key) => !workflowDefinitionKeys.has(key));
|
|
27
|
+
if (unknownKey) throw workflowErrorDiagnostics.WORKFLOW_C0015({ message: `\`defineWorkflow()\` does not support the ${JSON.stringify(unknownKey)} option.` });
|
|
28
|
+
if (typeof definition.handler !== "function") throw workflowErrorDiagnostics.WORKFLOW_C0014({ message: "`defineWorkflow()` requires a workflow handler." });
|
|
29
|
+
if (definition.native !== void 0 && typeof definition.native !== "function") throw workflowErrorDiagnostics.WORKFLOW_C0016({ message: "`defineWorkflow()` native entry must be a workflow handler." });
|
|
30
|
+
return definition;
|
|
29
31
|
}
|
|
30
32
|
//#endregion
|
|
31
|
-
export {
|
|
33
|
+
export { defineWorkflow };
|
|
@@ -1,55 +1,19 @@
|
|
|
1
|
-
import { t as workflowErrorDiagnostics } from "./error-diagnostics-
|
|
1
|
+
import { t as workflowErrorDiagnostics } from "./error-diagnostics-DMeaFmQD.js";
|
|
2
|
+
import { parseDuration } from "@vite-hub/runtime";
|
|
2
3
|
//#region src/runtime/delay.ts
|
|
3
|
-
const durationUnits = {
|
|
4
|
-
ms: 1,
|
|
5
|
-
millisecond: 1,
|
|
6
|
-
milliseconds: 1,
|
|
7
|
-
s: 1e3,
|
|
8
|
-
sec: 1e3,
|
|
9
|
-
secs: 1e3,
|
|
10
|
-
second: 1e3,
|
|
11
|
-
seconds: 1e3,
|
|
12
|
-
m: 6e4,
|
|
13
|
-
min: 6e4,
|
|
14
|
-
mins: 6e4,
|
|
15
|
-
minute: 6e4,
|
|
16
|
-
minutes: 6e4,
|
|
17
|
-
h: 36e5,
|
|
18
|
-
hr: 36e5,
|
|
19
|
-
hrs: 36e5,
|
|
20
|
-
hour: 36e5,
|
|
21
|
-
hours: 36e5,
|
|
22
|
-
d: 864e5,
|
|
23
|
-
day: 864e5,
|
|
24
|
-
days: 864e5,
|
|
25
|
-
w: 6048e5,
|
|
26
|
-
week: 6048e5,
|
|
27
|
-
weeks: 6048e5
|
|
28
|
-
};
|
|
29
|
-
/** The largest time that a JavaScript Date can hold. */
|
|
30
4
|
const maximumTime = 864e13;
|
|
31
5
|
function isDate(value) {
|
|
32
6
|
return Object.prototype.toString.call(value) === "[object Date]";
|
|
33
7
|
}
|
|
34
|
-
function parseDelayMilliseconds(delay) {
|
|
35
|
-
if (typeof delay === "number") return Number.isFinite(delay) && delay >= 0 ? delay : void 0;
|
|
36
|
-
const match = /^(\d+(?:\.\d+)?)\s*([a-z]+)$/i.exec(delay.trim());
|
|
37
|
-
const unit = match && durationUnits[match[2].toLowerCase()];
|
|
38
|
-
return unit ? Number(match[1]) * unit : void 0;
|
|
39
|
-
}
|
|
40
8
|
/**
|
|
41
9
|
* The time in milliseconds when a delayed run starts, or `undefined` when the run starts now. A `Date` in the past
|
|
42
|
-
* starts now. A
|
|
10
|
+
* starts now. A duration that is not valid, such as a number, throws `RUNTIME_DURATION_INVALID`. An invalid `Date`, or
|
|
11
|
+
* a time that a `Date` cannot hold, throws `WORKFLOW_START_DELAY_INVALID`.
|
|
43
12
|
*/
|
|
44
13
|
function resolveWorkflowStartAt(delay, now = Date.now()) {
|
|
45
14
|
if (delay === void 0) return void 0;
|
|
46
|
-
|
|
47
|
-
if (isDate(delay)
|
|
48
|
-
else if (typeof delay === "number" || typeof delay === "string") {
|
|
49
|
-
const milliseconds = parseDelayMilliseconds(delay);
|
|
50
|
-
startAt = milliseconds === void 0 ? void 0 : now + milliseconds;
|
|
51
|
-
}
|
|
52
|
-
if (startAt === void 0 || !Number.isFinite(startAt) || startAt > maximumTime) throw workflowErrorDiagnostics.WORKFLOW_START_DELAY_INVALID({ delay: isDate(delay) ? "Invalid Date" : String(delay) });
|
|
15
|
+
const startAt = isDate(delay) ? Date.prototype.getTime.call(delay) : now + parseDuration(delay, "delay");
|
|
16
|
+
if (!Number.isFinite(startAt) || startAt > maximumTime) throw workflowErrorDiagnostics.WORKFLOW_START_DELAY_INVALID({ delay: isDate(delay) ? "Invalid Date" : String(delay) });
|
|
53
17
|
return startAt > now ? startAt : void 0;
|
|
54
18
|
}
|
|
55
19
|
/**
|
package/dist/durability.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { P as WorkflowRuntimeConfigValue } from "./types-BWPHeugk.js";
|
|
2
2
|
import { Durability, DurabilityContext } from "@vite-hub/runtime/durability";
|
|
3
3
|
//#region src/durability.d.ts
|
|
4
4
|
/** What the build passes to `workflowDurability()`. The storage values may name runtime environment variables. */
|