@upstash/workflow 1.4.0 → 1.5.1-canary.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 +9 -0
- package/astro.d.mts +19 -2
- package/astro.d.ts +19 -2
- package/astro.js +670 -159
- package/astro.mjs +1 -1
- package/{chunk-DRHEHWMS.mjs → chunk-J3PXPHSB.mjs} +828 -317
- package/cloudflare.d.mts +17 -2
- package/cloudflare.d.ts +17 -2
- package/cloudflare.js +670 -159
- package/cloudflare.mjs +1 -1
- package/docs/agents/examples.mdx +8 -0
- package/docs/agents/features.mdx +375 -0
- package/docs/agents/getting-started.mdx +139 -0
- package/docs/agents/overview.mdx +40 -0
- package/docs/agents/patterns/evaluator-optimizer.mdx +72 -0
- package/docs/agents/patterns/orchestrator-workers.mdx +95 -0
- package/docs/agents/patterns/parallelization.mdx +77 -0
- package/docs/agents/patterns/prompt-chaining.mdx +84 -0
- package/docs/basics/best-practices/at-least-one-step.mdx +57 -0
- package/docs/basics/best-practices/business-logic-in-steps.mdx +52 -0
- package/docs/basics/best-practices/deterministic-code.mdx +132 -0
- package/docs/basics/best-practices/idempotency.mdx +38 -0
- package/docs/basics/best-practices/nested-context-methods.mdx +38 -0
- package/docs/basics/best-practices/overview.mdx +35 -0
- package/docs/basics/best-practices/promise-any.mdx +9 -0
- package/docs/basics/best-practices/return-step-results.mdx +80 -0
- package/docs/basics/best-practices/workflow-limits.mdx +62 -0
- package/docs/basics/client/cancel.mdx +137 -0
- package/docs/basics/client/dlq/callback.mdx +21 -0
- package/docs/basics/client/dlq/delete.mdx +120 -0
- package/docs/basics/client/dlq/list.mdx +96 -0
- package/docs/basics/client/dlq/restart.mdx +164 -0
- package/docs/basics/client/dlq/resume.mdx +167 -0
- package/docs/basics/client/logs.mdx +139 -0
- package/docs/basics/client/notify.mdx +79 -0
- package/docs/basics/client/trigger.mdx +139 -0
- package/docs/basics/client/waiters.mdx +31 -0
- package/docs/basics/client.mdx +40 -0
- package/docs/basics/how.mdx +65 -0
- package/docs/basics/serve/advanced.mdx +435 -0
- package/docs/basics/serve.mdx +142 -0
- package/docs/changelog.mdx +174 -0
- package/docs/examples/allInOne.mdx +377 -0
- package/docs/examples/authWebhook.mdx +575 -0
- package/docs/examples/customRetry.mdx +309 -0
- package/docs/examples/customerOnboarding.mdx +230 -0
- package/docs/examples/dynamicWorkflow.mdx +203 -0
- package/docs/examples/eCommerceOrderFulfillment.mdx +281 -0
- package/docs/examples/imageProcessing.mdx +339 -0
- package/docs/examples/paymentRetry.mdx +438 -0
- package/docs/examples/waitForEvent.mdx +201 -0
- package/docs/features/dlq/callback.mdx +42 -0
- package/docs/features/dlq/delete.mdx +29 -0
- package/docs/features/dlq/restart.mdx +33 -0
- package/docs/features/dlq/resume.mdx +37 -0
- package/docs/features/dlq.mdx +39 -0
- package/docs/features/failure-callback.mdx +102 -0
- package/docs/features/failureFunction/advanced.mdx +36 -0
- package/docs/features/failureFunction/reliability.mdx +33 -0
- package/docs/features/flow-control/monitor.mdx +22 -0
- package/docs/features/flow-control/parallelism.mdx +45 -0
- package/docs/features/flow-control/rate-period.mdx +46 -0
- package/docs/features/flow-control.mdx +164 -0
- package/docs/features/invoke/serveMany.mdx +109 -0
- package/docs/features/invoke.mdx +39 -0
- package/docs/features/notify.mdx +127 -0
- package/docs/features/parallel-steps.mdx +41 -0
- package/docs/features/retries/prevent-retries.mdx +140 -0
- package/docs/features/retries.mdx +93 -0
- package/docs/features/wait-for-event.mdx +104 -0
- package/docs/features/wait.mdx +44 -0
- package/docs/features/webhooks.mdx +116 -0
- package/docs/getstarted.mdx +200 -0
- package/docs/howto/cancel.mdx +50 -0
- package/docs/howto/changes.mdx +33 -0
- package/docs/howto/configure.mdx +42 -0
- package/docs/howto/failures.mdx +185 -0
- package/docs/howto/get-token.mdx +18 -0
- package/docs/howto/local-development/development-server.mdx +130 -0
- package/docs/howto/local-development/local-tunnel.mdx +73 -0
- package/docs/howto/middlewares.mdx +263 -0
- package/docs/howto/migrations.mdx +335 -0
- package/docs/howto/multi-region.mdx +107 -0
- package/docs/howto/parallel-runs.mdx +52 -0
- package/docs/howto/readonly-token.mdx +19 -0
- package/docs/howto/realtime/basic.mdx +315 -0
- package/docs/howto/realtime/human-in-the-loop.mdx +454 -0
- package/docs/howto/redact-fields.mdx +61 -0
- package/docs/howto/retry-now.mdx +41 -0
- package/docs/howto/schedule.mdx +114 -0
- package/docs/howto/security.mdx +143 -0
- package/docs/howto/singleton.mdx +137 -0
- package/docs/howto/start.mdx +69 -0
- package/docs/howto/use-webhooks.mdx +256 -0
- package/docs/integrations/aisdk.mdx +316 -0
- package/docs/integrations/anthropic.mdx +51 -0
- package/docs/integrations/datadog.mdx +69 -0
- package/docs/integrations/openai.mdx +87 -0
- package/docs/integrations/prometheus.mdx +65 -0
- package/docs/integrations/resend.mdx +82 -0
- package/docs/llms-txt.mdx +4 -0
- package/docs/openapi.yaml +2946 -0
- package/docs/pricing.mdx +22 -0
- package/docs/quickstarts/astro.mdx +185 -0
- package/docs/quickstarts/cloudflare-workers.mdx +190 -0
- package/docs/quickstarts/express.mdx +190 -0
- package/docs/quickstarts/fastapi.mdx +282 -0
- package/docs/quickstarts/flask.mdx +282 -0
- package/docs/quickstarts/hono.mdx +221 -0
- package/docs/quickstarts/nextjs-fastapi.mdx +147 -0
- package/docs/quickstarts/nextjs-flask.mdx +147 -0
- package/docs/quickstarts/nuxt.mdx +169 -0
- package/docs/quickstarts/platforms.mdx +78 -0
- package/docs/quickstarts/solidjs.mdx +161 -0
- package/docs/quickstarts/svelte.mdx +170 -0
- package/docs/quickstarts/tanstack-start.mdx +205 -0
- package/docs/quickstarts/vercel-nextjs.mdx +233 -0
- package/docs/roadmap.mdx +8 -0
- package/docs/sdk/workflow-js.mdx +4 -0
- package/docs/sdk/workflow-py.mdx +4 -0
- package/docs/steps/api.mdx +64 -0
- package/docs/steps/call.mdx +183 -0
- package/docs/steps/cancel.mdx +25 -0
- package/docs/steps/createWebhook.mdx +40 -0
- package/docs/steps/invoke.mdx +100 -0
- package/docs/steps/notify.mdx +105 -0
- package/docs/steps/run.mdx +150 -0
- package/docs/steps/sleep.mdx +97 -0
- package/docs/steps/sleepUntil.mdx +88 -0
- package/docs/steps/waitForEvent.mdx +60 -0
- package/docs/steps/waitForWebhook.mdx +78 -0
- package/docs/steps.mdx +108 -0
- package/docs/troubleshooting/general.mdx +331 -0
- package/docs/troubleshooting/vercel.mdx +82 -0
- package/express.d.mts +19 -2
- package/express.d.ts +19 -2
- package/express.js +670 -159
- package/express.mjs +1 -1
- package/h3.d.mts +19 -2
- package/h3.d.ts +19 -2
- package/h3.js +670 -159
- package/h3.mjs +1 -1
- package/hono.d.mts +17 -2
- package/hono.d.ts +17 -2
- package/hono.js +670 -159
- package/hono.mjs +1 -1
- package/index.d.mts +50 -21
- package/index.d.ts +50 -21
- package/index.js +686 -171
- package/index.mjs +17 -13
- package/nextjs.d.mts +40 -2
- package/nextjs.d.ts +40 -2
- package/nextjs.js +670 -159
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +16 -2
- package/react-router.d.ts +16 -2
- package/react-router.js +670 -159
- package/react-router.mjs +1 -1
- package/{serve-many-CG3BFvO3.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-iJF1IUXk.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +10 -1
- package/solidjs.d.ts +10 -1
- package/solidjs.js +670 -159
- package/solidjs.mjs +1 -1
- package/src/platforms/astro.ts +84 -0
- package/src/platforms/cloudflare.ts +127 -0
- package/src/platforms/express.ts +147 -0
- package/src/platforms/h3.ts +105 -0
- package/src/platforms/hono.ts +103 -0
- package/src/platforms/nextjs.ts +193 -0
- package/src/platforms/react-router.ts +92 -0
- package/src/platforms/solidjs.ts +59 -0
- package/src/platforms/svelte.ts +99 -0
- package/src/platforms/tanstack.ts +96 -0
- package/src/src/client/dlq.ts +387 -0
- package/src/src/client/filter-types.ts +166 -0
- package/src/src/client/index.ts +350 -0
- package/src/src/client/types.ts +501 -0
- package/src/src/client/utils.ts +307 -0
- package/src/src/constants.ts +82 -0
- package/src/src/context/api/anthropic.ts +58 -0
- package/src/src/context/api/base.ts +52 -0
- package/src/src/context/api/index.ts +24 -0
- package/src/src/context/api/openai.ts +125 -0
- package/src/src/context/api/resend.ts +57 -0
- package/src/src/context/auto-executor.ts +707 -0
- package/src/src/context/context.ts +681 -0
- package/src/src/context/index.ts +1 -0
- package/src/src/context/provider.ts +37 -0
- package/src/src/context/steps.ts +965 -0
- package/src/src/error.ts +203 -0
- package/src/src/index.ts +13 -0
- package/src/src/middleware/default-callbacks.ts +20 -0
- package/src/src/middleware/index.ts +2 -0
- package/src/src/middleware/logging.ts +39 -0
- package/src/src/middleware/manager.ts +148 -0
- package/src/src/middleware/middleware.ts +46 -0
- package/src/src/middleware/types.ts +72 -0
- package/src/src/qstash/headers.ts +397 -0
- package/src/src/qstash/step-config.ts +268 -0
- package/src/src/qstash/submit-steps.ts +284 -0
- package/src/src/serve/authorization.ts +118 -0
- package/src/src/serve/index.ts +475 -0
- package/src/src/serve/multi-region/handlers.ts +225 -0
- package/src/src/serve/multi-region/utils.ts +88 -0
- package/src/src/serve/options.ts +241 -0
- package/src/src/serve/serve-many.ts +117 -0
- package/src/src/serve/utils.ts +11 -0
- package/src/src/types.ts +590 -0
- package/src/src/utils.ts +141 -0
- package/src/src/workflow-parser.ts +472 -0
- package/src/src/workflow-requests.ts +575 -0
- package/svelte.d.mts +17 -2
- package/svelte.d.ts +17 -2
- package/svelte.js +670 -159
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +15 -2
- package/tanstack.d.ts +15 -2
- package/tanstack.js +670 -159
- package/tanstack.mjs +1 -1
- package/{types-CekOpKvz.d.ts → types-CrgK5P6p.d.mts} +372 -32
- package/{types-CekOpKvz.d.mts → types-CrgK5P6p.d.ts} +372 -32
package/solidjs.js
CHANGED
|
@@ -215,6 +215,14 @@ var WORKFLOW_CREATED_AT_HEADER = "Upstash-Workflow-CreatedAt";
|
|
|
215
215
|
var WORKFLOW_FAILURE_HEADER = "Upstash-Workflow-Is-Failure";
|
|
216
216
|
var WORKFLOW_FAILURE_CALLBACK_HEADER = "Upstash-Workflow-Failure-Callback";
|
|
217
217
|
var WORKFLOW_FEATURE_HEADER = "Upstash-Feature-Set";
|
|
218
|
+
var WORKFLOW_FEATURE_SET = "LazyFetch,InitialBody,WF_DetectTrigger,WF_TriggerOnConfig";
|
|
219
|
+
var WORKFLOW_STEP_CONFIG_CALL_TYPE = "stepConfig";
|
|
220
|
+
var WORKFLOW_STEP_CONFIG_HEADER = "Upstash-Workflow-Step-Config";
|
|
221
|
+
var FLOW_CONTROL_KEY_HEADER = "Upstash-Flow-Control-Key";
|
|
222
|
+
var FLOW_CONTROL_VALUE_HEADER = "Upstash-Flow-Control-Value";
|
|
223
|
+
var RETRIES_HEADER = "Upstash-Retries";
|
|
224
|
+
var RETRY_DELAY_HEADER = "Upstash-Retry-Delay";
|
|
225
|
+
var WORKFLOW_STEP_CONFIG_FEATURE = "WF_StepConfig";
|
|
218
226
|
var WORKFLOW_INVOKE_COUNT_HEADER = "Upstash-Workflow-Invoke-Count";
|
|
219
227
|
var WORKFLOW_RETRIED_HEADER = "Upstash-Retried";
|
|
220
228
|
var WORKFLOW_LABEL_HEADER = "Upstash-Label";
|
|
@@ -232,102 +240,6 @@ var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
|
|
|
232
240
|
var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
|
|
233
241
|
var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
|
|
234
242
|
|
|
235
|
-
// src/context/auto-executor.ts
|
|
236
|
-
var import_qstash5 = require("@upstash/qstash");
|
|
237
|
-
|
|
238
|
-
// src/qstash/headers.ts
|
|
239
|
-
var import_qstash4 = require("@upstash/qstash");
|
|
240
|
-
|
|
241
|
-
// src/utils.ts
|
|
242
|
-
var NANOID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
|
243
|
-
var NANOID_LENGTH = 21;
|
|
244
|
-
var RESOURCE_NAME_PATTERN = /^[a-zA-Z0-9\-_.]+$/;
|
|
245
|
-
function validateLabel(label) {
|
|
246
|
-
if (label === void 0) return;
|
|
247
|
-
const labels = Array.isArray(label) ? label : [label];
|
|
248
|
-
if (labels.length === 0) {
|
|
249
|
-
throw new WorkflowNonRetryableError("Invalid label: label array must not be empty.");
|
|
250
|
-
}
|
|
251
|
-
for (const value of labels) {
|
|
252
|
-
if (!RESOURCE_NAME_PATTERN.test(value)) {
|
|
253
|
-
throw new WorkflowNonRetryableError(
|
|
254
|
-
`Invalid label "${value}": must be alphanumeric, hyphen, underscore, or period.`
|
|
255
|
-
);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
}
|
|
259
|
-
function serializeLabel(label) {
|
|
260
|
-
return Array.isArray(label) ? label.join(",") : label;
|
|
261
|
-
}
|
|
262
|
-
function validateFlowControl(flowControl) {
|
|
263
|
-
if (flowControl?.key !== void 0 && !RESOURCE_NAME_PATTERN.test(flowControl.key)) {
|
|
264
|
-
throw new WorkflowNonRetryableError(
|
|
265
|
-
`Invalid flow control key "${flowControl.key}": must be alphanumeric, hyphen, underscore, or period.`
|
|
266
|
-
);
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
function getRandomInt() {
|
|
270
|
-
return Math.floor(Math.random() * NANOID_CHARS.length);
|
|
271
|
-
}
|
|
272
|
-
function nanoid(length = NANOID_LENGTH) {
|
|
273
|
-
return Array.from({ length }).map(() => NANOID_CHARS[getRandomInt()]).join("");
|
|
274
|
-
}
|
|
275
|
-
function getWorkflowRunId(id) {
|
|
276
|
-
return `wfr_${id ?? nanoid()}`;
|
|
277
|
-
}
|
|
278
|
-
function decodeBase64(base64) {
|
|
279
|
-
const binString = atob(base64);
|
|
280
|
-
try {
|
|
281
|
-
const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
282
|
-
return new TextDecoder().decode(intArray);
|
|
283
|
-
} catch (error) {
|
|
284
|
-
console.warn(
|
|
285
|
-
`Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
|
|
286
|
-
);
|
|
287
|
-
return binString;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
function getUserIdFromToken(qstashClient) {
|
|
291
|
-
try {
|
|
292
|
-
const token = qstashClient.token;
|
|
293
|
-
const decodedToken = decodeBase64(token);
|
|
294
|
-
const tokenPayload = JSON.parse(decodedToken);
|
|
295
|
-
const userId = tokenPayload.UserID;
|
|
296
|
-
if (!userId) {
|
|
297
|
-
throw new WorkflowError("QStash token payload does not contain userId");
|
|
298
|
-
}
|
|
299
|
-
return userId;
|
|
300
|
-
} catch (error) {
|
|
301
|
-
throw new WorkflowError(
|
|
302
|
-
`Failed to decode QStash token while running create webhook step: ${error.message}`
|
|
303
|
-
);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
function getQStashUrl(qstashClient) {
|
|
307
|
-
try {
|
|
308
|
-
const requester = qstashClient.http;
|
|
309
|
-
const baseUrl = requester.baseUrl;
|
|
310
|
-
if (!baseUrl) {
|
|
311
|
-
throw new WorkflowError("QStash client does not have a baseUrl");
|
|
312
|
-
}
|
|
313
|
-
return baseUrl;
|
|
314
|
-
} catch (error) {
|
|
315
|
-
throw new WorkflowError(`Failed to get QStash URL from client: ${error.message}`);
|
|
316
|
-
}
|
|
317
|
-
}
|
|
318
|
-
function getEventId() {
|
|
319
|
-
return `evt_${nanoid(15)}`;
|
|
320
|
-
}
|
|
321
|
-
function stringifyBody(body) {
|
|
322
|
-
if (body === void 0) {
|
|
323
|
-
return void 0;
|
|
324
|
-
}
|
|
325
|
-
if (typeof body === "string") {
|
|
326
|
-
return body;
|
|
327
|
-
}
|
|
328
|
-
return JSON.stringify(body);
|
|
329
|
-
}
|
|
330
|
-
|
|
331
243
|
// node_modules/neverthrow/dist/index.es.js
|
|
332
244
|
var defaultErrorConfig = {
|
|
333
245
|
withStackTrace: false
|
|
@@ -743,6 +655,102 @@ var Err = class {
|
|
|
743
655
|
};
|
|
744
656
|
var fromThrowable = Result.fromThrowable;
|
|
745
657
|
|
|
658
|
+
// src/context/auto-executor.ts
|
|
659
|
+
var import_qstash5 = require("@upstash/qstash");
|
|
660
|
+
|
|
661
|
+
// src/qstash/headers.ts
|
|
662
|
+
var import_qstash4 = require("@upstash/qstash");
|
|
663
|
+
|
|
664
|
+
// src/utils.ts
|
|
665
|
+
var NANOID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
|
666
|
+
var NANOID_LENGTH = 21;
|
|
667
|
+
var RESOURCE_NAME_PATTERN = /^[a-zA-Z0-9\-_.]+$/;
|
|
668
|
+
function validateLabel(label) {
|
|
669
|
+
if (label === void 0) return;
|
|
670
|
+
const labels = Array.isArray(label) ? label : [label];
|
|
671
|
+
if (labels.length === 0) {
|
|
672
|
+
throw new WorkflowNonRetryableError("Invalid label: label array must not be empty.");
|
|
673
|
+
}
|
|
674
|
+
for (const value of labels) {
|
|
675
|
+
if (!RESOURCE_NAME_PATTERN.test(value)) {
|
|
676
|
+
throw new WorkflowNonRetryableError(
|
|
677
|
+
`Invalid label "${value}": must be alphanumeric, hyphen, underscore, or period.`
|
|
678
|
+
);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
function serializeLabel(label) {
|
|
683
|
+
return Array.isArray(label) ? label.join(",") : label;
|
|
684
|
+
}
|
|
685
|
+
function validateFlowControl(flowControl) {
|
|
686
|
+
if (flowControl?.key !== void 0 && !RESOURCE_NAME_PATTERN.test(flowControl.key)) {
|
|
687
|
+
throw new WorkflowNonRetryableError(
|
|
688
|
+
`Invalid flow control key "${flowControl.key}": must be alphanumeric, hyphen, underscore, or period.`
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
function getRandomInt() {
|
|
693
|
+
return Math.floor(Math.random() * NANOID_CHARS.length);
|
|
694
|
+
}
|
|
695
|
+
function nanoid(length = NANOID_LENGTH) {
|
|
696
|
+
return Array.from({ length }).map(() => NANOID_CHARS[getRandomInt()]).join("");
|
|
697
|
+
}
|
|
698
|
+
function getWorkflowRunId(id) {
|
|
699
|
+
return `wfr_${id ?? nanoid()}`;
|
|
700
|
+
}
|
|
701
|
+
function decodeBase64(base64) {
|
|
702
|
+
const binString = atob(base64);
|
|
703
|
+
try {
|
|
704
|
+
const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
705
|
+
return new TextDecoder().decode(intArray);
|
|
706
|
+
} catch (error) {
|
|
707
|
+
console.warn(
|
|
708
|
+
`Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
|
|
709
|
+
);
|
|
710
|
+
return binString;
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
function getUserIdFromToken(qstashClient) {
|
|
714
|
+
try {
|
|
715
|
+
const token = qstashClient.token;
|
|
716
|
+
const decodedToken = decodeBase64(token);
|
|
717
|
+
const tokenPayload = JSON.parse(decodedToken);
|
|
718
|
+
const userId = tokenPayload.UserID;
|
|
719
|
+
if (!userId) {
|
|
720
|
+
throw new WorkflowError("QStash token payload does not contain userId");
|
|
721
|
+
}
|
|
722
|
+
return userId;
|
|
723
|
+
} catch (error) {
|
|
724
|
+
throw new WorkflowError(
|
|
725
|
+
`Failed to decode QStash token while running create webhook step: ${error.message}`
|
|
726
|
+
);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
function getQStashUrl(qstashClient) {
|
|
730
|
+
try {
|
|
731
|
+
const requester = qstashClient.http;
|
|
732
|
+
const baseUrl = requester.baseUrl;
|
|
733
|
+
if (!baseUrl) {
|
|
734
|
+
throw new WorkflowError("QStash client does not have a baseUrl");
|
|
735
|
+
}
|
|
736
|
+
return baseUrl;
|
|
737
|
+
} catch (error) {
|
|
738
|
+
throw new WorkflowError(`Failed to get QStash URL from client: ${error.message}`);
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
function getEventId() {
|
|
742
|
+
return `evt_${nanoid(15)}`;
|
|
743
|
+
}
|
|
744
|
+
function stringifyBody(body) {
|
|
745
|
+
if (body === void 0) {
|
|
746
|
+
return void 0;
|
|
747
|
+
}
|
|
748
|
+
if (typeof body === "string") {
|
|
749
|
+
return body;
|
|
750
|
+
}
|
|
751
|
+
return JSON.stringify(body);
|
|
752
|
+
}
|
|
753
|
+
|
|
746
754
|
// src/types.ts
|
|
747
755
|
var StepTypes = [
|
|
748
756
|
"Initial",
|
|
@@ -852,6 +860,10 @@ var triggerFirstInvocation = async (params) => {
|
|
|
852
860
|
return err(error_);
|
|
853
861
|
}
|
|
854
862
|
};
|
|
863
|
+
var flushPendingStep = async (workflowContext) => {
|
|
864
|
+
const { executor } = workflowContext;
|
|
865
|
+
return await executor.submitPendingStep();
|
|
866
|
+
};
|
|
855
867
|
var triggerRouteFunction = async ({
|
|
856
868
|
onCleanup,
|
|
857
869
|
onStep,
|
|
@@ -1064,8 +1076,29 @@ If you want to disable QStash Verification, you should clear env variables QSTAS
|
|
|
1064
1076
|
var BaseLazyStep = class _BaseLazyStep {
|
|
1065
1077
|
stepName;
|
|
1066
1078
|
context;
|
|
1067
|
-
|
|
1079
|
+
/**
|
|
1080
|
+
* step-level settings (flow control, retries etc.) which override the
|
|
1081
|
+
* settings the workflow run was triggered with, for this step only.
|
|
1082
|
+
*/
|
|
1083
|
+
stepSettings;
|
|
1084
|
+
/**
|
|
1085
|
+
* whether this step's result can be submitted after the route function
|
|
1086
|
+
* has moved on, instead of right away.
|
|
1087
|
+
*
|
|
1088
|
+
* Enabled for steps whose `getResultStep` produces the final `out` —
|
|
1089
|
+
* which is not the same as "needs no server round trip":
|
|
1090
|
+
* `LazyCreateWebhookStep` builds its result in `getBody` and returns
|
|
1091
|
+
* `out: undefined` from `getResultStep`, so deferring it would hand
|
|
1092
|
+
* `undefined` to the route function.
|
|
1093
|
+
*
|
|
1094
|
+
* Deferring lets the route function continue and reveal the next step,
|
|
1095
|
+
* so that step's settings can ride on this step's submission instead
|
|
1096
|
+
* of needing a step config request of their own.
|
|
1097
|
+
*/
|
|
1098
|
+
supportsDeferredSubmission = false;
|
|
1099
|
+
constructor(context, stepName, stepSettings) {
|
|
1068
1100
|
this.context = context;
|
|
1101
|
+
this.stepSettings = stepSettings;
|
|
1069
1102
|
if (!stepName) {
|
|
1070
1103
|
throw new WorkflowError(
|
|
1071
1104
|
"A workflow step name cannot be undefined or an empty string. Please provide a name for your workflow step."
|
|
@@ -1129,7 +1162,13 @@ var BaseLazyStep = class _BaseLazyStep {
|
|
|
1129
1162
|
step.out = JSON.stringify(step.out);
|
|
1130
1163
|
return JSON.stringify(step);
|
|
1131
1164
|
}
|
|
1132
|
-
getHeaders({
|
|
1165
|
+
getHeaders({
|
|
1166
|
+
context,
|
|
1167
|
+
telemetry,
|
|
1168
|
+
invokeCount,
|
|
1169
|
+
step,
|
|
1170
|
+
stepSettings
|
|
1171
|
+
}) {
|
|
1133
1172
|
return getHeaders({
|
|
1134
1173
|
initHeaderValue: "false",
|
|
1135
1174
|
workflowConfig: {
|
|
@@ -1143,7 +1182,8 @@ var BaseLazyStep = class _BaseLazyStep {
|
|
|
1143
1182
|
stepInfo: {
|
|
1144
1183
|
step,
|
|
1145
1184
|
lazyStep: this
|
|
1146
|
-
}
|
|
1185
|
+
},
|
|
1186
|
+
stepSettings
|
|
1147
1187
|
});
|
|
1148
1188
|
}
|
|
1149
1189
|
async submitStep({ context, body, headers }) {
|
|
@@ -1161,8 +1201,9 @@ var LazyFunctionStep = class extends BaseLazyStep {
|
|
|
1161
1201
|
stepFunction;
|
|
1162
1202
|
stepType = "Run";
|
|
1163
1203
|
allowUndefinedOut = true;
|
|
1164
|
-
|
|
1165
|
-
|
|
1204
|
+
supportsDeferredSubmission = true;
|
|
1205
|
+
constructor(context, stepName, stepFunction, stepSettings) {
|
|
1206
|
+
super(context, stepName, stepSettings);
|
|
1166
1207
|
this.stepFunction = stepFunction;
|
|
1167
1208
|
}
|
|
1168
1209
|
getPlanStep(concurrent, targetStep) {
|
|
@@ -1192,6 +1233,7 @@ var LazySleepStep = class extends BaseLazyStep {
|
|
|
1192
1233
|
sleep;
|
|
1193
1234
|
stepType = "SleepFor";
|
|
1194
1235
|
allowUndefinedOut = true;
|
|
1236
|
+
supportsDeferredSubmission = true;
|
|
1195
1237
|
constructor(context, stepName, sleep) {
|
|
1196
1238
|
super(context, stepName);
|
|
1197
1239
|
this.sleep = sleep;
|
|
@@ -1231,6 +1273,7 @@ var LazySleepUntilStep = class extends BaseLazyStep {
|
|
|
1231
1273
|
sleepUntil;
|
|
1232
1274
|
stepType = "SleepUntil";
|
|
1233
1275
|
allowUndefinedOut = true;
|
|
1276
|
+
supportsDeferredSubmission = true;
|
|
1234
1277
|
constructor(context, stepName, sleepUntil) {
|
|
1235
1278
|
super(context, stepName);
|
|
1236
1279
|
this.sleepUntil = sleepUntil;
|
|
@@ -1732,6 +1775,7 @@ var WorkflowHeaders = class {
|
|
|
1732
1775
|
invokeCount;
|
|
1733
1776
|
initHeaderValue;
|
|
1734
1777
|
stepInfo;
|
|
1778
|
+
stepSettings;
|
|
1735
1779
|
headers;
|
|
1736
1780
|
/**
|
|
1737
1781
|
* @param params workflow header parameters
|
|
@@ -1741,17 +1785,20 @@ var WorkflowHeaders = class {
|
|
|
1741
1785
|
workflowConfig,
|
|
1742
1786
|
invokeCount,
|
|
1743
1787
|
initHeaderValue,
|
|
1744
|
-
stepInfo
|
|
1788
|
+
stepInfo,
|
|
1789
|
+
stepSettings
|
|
1745
1790
|
}) {
|
|
1746
1791
|
this.userHeaders = userHeaders;
|
|
1747
1792
|
this.workflowConfig = workflowConfig;
|
|
1748
1793
|
this.invokeCount = invokeCount;
|
|
1749
1794
|
this.initHeaderValue = initHeaderValue;
|
|
1750
1795
|
this.stepInfo = stepInfo;
|
|
1796
|
+
this.stepSettings = stepSettings;
|
|
1751
1797
|
this.headers = {
|
|
1752
1798
|
rawHeaders: {},
|
|
1753
1799
|
workflowHeaders: {},
|
|
1754
|
-
failureHeaders: {}
|
|
1800
|
+
failureHeaders: {},
|
|
1801
|
+
stepSettingsHeaders: {}
|
|
1755
1802
|
};
|
|
1756
1803
|
}
|
|
1757
1804
|
getHeaders() {
|
|
@@ -1762,6 +1809,7 @@ var WorkflowHeaders = class {
|
|
|
1762
1809
|
this.addUserHeaders();
|
|
1763
1810
|
this.addInvokeCount();
|
|
1764
1811
|
this.addFailureUrl();
|
|
1812
|
+
this.addStepSettings();
|
|
1765
1813
|
const contentType = this.addContentType();
|
|
1766
1814
|
return this.prefixHeaders(contentType);
|
|
1767
1815
|
}
|
|
@@ -1771,7 +1819,7 @@ var WorkflowHeaders = class {
|
|
|
1771
1819
|
[WORKFLOW_INIT_HEADER]: this.initHeaderValue,
|
|
1772
1820
|
[WORKFLOW_ID_HEADER]: this.workflowConfig.workflowRunId,
|
|
1773
1821
|
[WORKFLOW_URL_HEADER]: this.workflowConfig.workflowUrl,
|
|
1774
|
-
[WORKFLOW_FEATURE_HEADER]:
|
|
1822
|
+
[WORKFLOW_FEATURE_HEADER]: WORKFLOW_FEATURE_SET,
|
|
1775
1823
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
1776
1824
|
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1777
1825
|
};
|
|
@@ -1852,7 +1900,7 @@ var WorkflowHeaders = class {
|
|
|
1852
1900
|
this.headers.failureHeaders["Workflow-Init"] = "false";
|
|
1853
1901
|
this.headers.failureHeaders["Workflow-Url"] = this.workflowConfig.workflowUrl;
|
|
1854
1902
|
this.headers.failureHeaders["Workflow-Calltype"] = "failureCall";
|
|
1855
|
-
this.headers.failureHeaders["Feature-Set"] =
|
|
1903
|
+
this.headers.failureHeaders["Feature-Set"] = WORKFLOW_FEATURE_SET;
|
|
1856
1904
|
if (this.workflowConfig.retries !== void 0 && this.workflowConfig.retries !== DEFAULT_RETRIES) {
|
|
1857
1905
|
this.headers.failureHeaders["Retries"] = this.workflowConfig.retries.toString();
|
|
1858
1906
|
}
|
|
@@ -1860,6 +1908,42 @@ var WorkflowHeaders = class {
|
|
|
1860
1908
|
this.headers.failureHeaders["Retry-Delay"] = this.workflowConfig.retryDelay.toString();
|
|
1861
1909
|
}
|
|
1862
1910
|
}
|
|
1911
|
+
/**
|
|
1912
|
+
* Applies the step-level settings of a step to the message.
|
|
1913
|
+
*
|
|
1914
|
+
* These headers configure the message itself, so that QStash uses them
|
|
1915
|
+
* instead of the settings the run was triggered with. The delivery of
|
|
1916
|
+
* the message is what executes the step, which is how a step's
|
|
1917
|
+
* settings reach it.
|
|
1918
|
+
*
|
|
1919
|
+
* When any setting is present the feature set is extended with
|
|
1920
|
+
* `WF_StepConfig`, which is what tells QStash to keep them. QStash
|
|
1921
|
+
* reports that back on the delivery as `Upstash-Workflow-Step-Config`.
|
|
1922
|
+
*/
|
|
1923
|
+
addStepSettings() {
|
|
1924
|
+
if (!this.stepSettings) {
|
|
1925
|
+
return;
|
|
1926
|
+
}
|
|
1927
|
+
const headers = {};
|
|
1928
|
+
if (this.stepSettings.flowControl) {
|
|
1929
|
+
const { flowControlKey, flowControlValue } = prepareFlowControl(
|
|
1930
|
+
this.stepSettings.flowControl
|
|
1931
|
+
);
|
|
1932
|
+
headers[FLOW_CONTROL_KEY_HEADER] = flowControlKey;
|
|
1933
|
+
headers[FLOW_CONTROL_VALUE_HEADER] = flowControlValue;
|
|
1934
|
+
}
|
|
1935
|
+
if (this.stepSettings.retries !== void 0) {
|
|
1936
|
+
headers[RETRIES_HEADER] = this.stepSettings.retries.toString();
|
|
1937
|
+
}
|
|
1938
|
+
if (this.stepSettings.retryDelay) {
|
|
1939
|
+
headers[RETRY_DELAY_HEADER] = this.stepSettings.retryDelay;
|
|
1940
|
+
}
|
|
1941
|
+
if (Object.keys(headers).length === 0) {
|
|
1942
|
+
return;
|
|
1943
|
+
}
|
|
1944
|
+
headers[WORKFLOW_FEATURE_HEADER] = `${WORKFLOW_FEATURE_SET},${WORKFLOW_STEP_CONFIG_FEATURE}`;
|
|
1945
|
+
this.headers.stepSettingsHeaders = headers;
|
|
1946
|
+
}
|
|
1863
1947
|
addContentType() {
|
|
1864
1948
|
if (this.workflowConfig.useJSONContent) {
|
|
1865
1949
|
this.headers.rawHeaders["content-type"] = "application/json";
|
|
@@ -1873,14 +1957,17 @@ var WorkflowHeaders = class {
|
|
|
1873
1957
|
return contentType;
|
|
1874
1958
|
}
|
|
1875
1959
|
prefixHeaders(contentType) {
|
|
1876
|
-
const { rawHeaders, workflowHeaders, failureHeaders } = this.headers;
|
|
1960
|
+
const { rawHeaders, workflowHeaders, failureHeaders, stepSettingsHeaders } = this.headers;
|
|
1877
1961
|
const isCall = this.stepInfo?.lazyStep.stepType === "Call";
|
|
1878
1962
|
return {
|
|
1879
1963
|
headers: {
|
|
1880
1964
|
...rawHeaders,
|
|
1881
1965
|
...addPrefixToHeaders(workflowHeaders, isCall ? "Upstash-Callback-" : "Upstash-"),
|
|
1882
1966
|
...addPrefixToHeaders(failureHeaders, "Upstash-Failure-Callback-"),
|
|
1883
|
-
...isCall ? addPrefixToHeaders(failureHeaders, "Upstash-Callback-Failure-Callback-") : {}
|
|
1967
|
+
...isCall ? addPrefixToHeaders(failureHeaders, "Upstash-Callback-Failure-Callback-") : {},
|
|
1968
|
+
// last, so they override the run's settings above. Never
|
|
1969
|
+
// prefixed: they configure this message, not its callback.
|
|
1970
|
+
...stepSettingsHeaders
|
|
1884
1971
|
},
|
|
1885
1972
|
contentType
|
|
1886
1973
|
};
|
|
@@ -1931,7 +2018,7 @@ var submitParallelSteps = async ({
|
|
|
1931
2018
|
info: `Submitting ${planSteps.length} parallel steps.`
|
|
1932
2019
|
});
|
|
1933
2020
|
const result = await context.qstashClient.batch(
|
|
1934
|
-
planSteps.map((planStep) => {
|
|
2021
|
+
planSteps.map((planStep, index) => {
|
|
1935
2022
|
const { headers } = getHeaders({
|
|
1936
2023
|
initHeaderValue: "false",
|
|
1937
2024
|
workflowConfig: {
|
|
@@ -1940,7 +2027,8 @@ var submitParallelSteps = async ({
|
|
|
1940
2027
|
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
1941
2028
|
telemetry
|
|
1942
2029
|
},
|
|
1943
|
-
invokeCount
|
|
2030
|
+
invokeCount,
|
|
2031
|
+
stepSettings: steps[index].stepSettings
|
|
1944
2032
|
});
|
|
1945
2033
|
return {
|
|
1946
2034
|
headers,
|
|
@@ -1959,31 +2047,38 @@ var submitParallelSteps = async ({
|
|
|
1959
2047
|
}
|
|
1960
2048
|
throw new WorkflowAbort(planSteps[0].stepName, planSteps[0]);
|
|
1961
2049
|
};
|
|
1962
|
-
var
|
|
1963
|
-
context,
|
|
2050
|
+
var executeStep = async ({
|
|
1964
2051
|
lazyStep,
|
|
1965
2052
|
stepId,
|
|
1966
|
-
invokeCount,
|
|
1967
2053
|
concurrency,
|
|
1968
|
-
telemetry,
|
|
1969
|
-
dispatchDebug,
|
|
1970
2054
|
dispatchLifecycle
|
|
1971
2055
|
}) => {
|
|
1972
2056
|
await dispatchLifecycle("beforeExecution", {
|
|
1973
2057
|
stepName: lazyStep.stepName
|
|
1974
2058
|
});
|
|
1975
|
-
let resultStep;
|
|
1976
2059
|
try {
|
|
1977
|
-
|
|
2060
|
+
return await lazyStep.getResultStep(concurrency, stepId);
|
|
1978
2061
|
} catch (error) {
|
|
1979
2062
|
attachStepNameToError(error, lazyStep.stepName);
|
|
1980
2063
|
throw error;
|
|
1981
2064
|
}
|
|
2065
|
+
};
|
|
2066
|
+
var submitStepResult = async ({
|
|
2067
|
+
context,
|
|
2068
|
+
lazyStep,
|
|
2069
|
+
resultStep,
|
|
2070
|
+
invokeCount,
|
|
2071
|
+
concurrency,
|
|
2072
|
+
telemetry,
|
|
2073
|
+
dispatchDebug,
|
|
2074
|
+
nextStepSettings
|
|
2075
|
+
}) => {
|
|
1982
2076
|
const { headers } = lazyStep.getHeaders({
|
|
1983
2077
|
context,
|
|
1984
2078
|
step: resultStep,
|
|
1985
2079
|
invokeCount,
|
|
1986
|
-
telemetry
|
|
2080
|
+
telemetry,
|
|
2081
|
+
stepSettings: nextStepSettings
|
|
1987
2082
|
});
|
|
1988
2083
|
const body = lazyStep.getBody({
|
|
1989
2084
|
context,
|
|
@@ -2008,6 +2103,154 @@ var submitSingleStep = async ({
|
|
|
2008
2103
|
}
|
|
2009
2104
|
return resultStep;
|
|
2010
2105
|
};
|
|
2106
|
+
var publishStepConfigRequest = async ({
|
|
2107
|
+
context,
|
|
2108
|
+
lazyStep,
|
|
2109
|
+
targetStep,
|
|
2110
|
+
invokeCount,
|
|
2111
|
+
telemetry,
|
|
2112
|
+
dispatchDebug
|
|
2113
|
+
}) => {
|
|
2114
|
+
const { headers } = getHeaders({
|
|
2115
|
+
initHeaderValue: "false",
|
|
2116
|
+
workflowConfig: {
|
|
2117
|
+
workflowRunId: context.workflowRunId,
|
|
2118
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
2119
|
+
workflowUrl: context.url,
|
|
2120
|
+
telemetry
|
|
2121
|
+
},
|
|
2122
|
+
invokeCount,
|
|
2123
|
+
stepSettings: lazyStep.stepSettings
|
|
2124
|
+
});
|
|
2125
|
+
await dispatchDebug("onInfo", {
|
|
2126
|
+
info: `Step "${lazyStep.stepName}" (${targetStep}) has step-level settings which the current delivery does not carry. Requesting a delivery which does.`
|
|
2127
|
+
});
|
|
2128
|
+
const result = await context.qstashClient.publishJSON({
|
|
2129
|
+
headers: {
|
|
2130
|
+
...headers,
|
|
2131
|
+
"Upstash-Workflow-CallType": WORKFLOW_STEP_CONFIG_CALL_TYPE,
|
|
2132
|
+
"Upstash-Forward-Upstash-Workflow-StepId": targetStep.toString(),
|
|
2133
|
+
"Upstash-Forward-Upstash-Workflow-StepName": lazyStep.stepName
|
|
2134
|
+
},
|
|
2135
|
+
method: "POST",
|
|
2136
|
+
body: { targetStep, invokeCount },
|
|
2137
|
+
url: context.url,
|
|
2138
|
+
contentBasedDeduplication: true
|
|
2139
|
+
});
|
|
2140
|
+
if (result?.deduplicated) {
|
|
2141
|
+
await dispatchDebug("onWarning", {
|
|
2142
|
+
warning: `A step config request for step "${lazyStep.stepName}" (${targetStep}) was already published and this one was deduplicated. Expected when the delivery which published it is being retried; otherwise the run stops here.`
|
|
2143
|
+
});
|
|
2144
|
+
} else if (result?.messageId) {
|
|
2145
|
+
await dispatchDebug("onInfo", {
|
|
2146
|
+
info: `Requested a delivery under the settings of step "${lazyStep.stepName}" with messageId: ${result.messageId}.`
|
|
2147
|
+
});
|
|
2148
|
+
}
|
|
2149
|
+
};
|
|
2150
|
+
|
|
2151
|
+
// src/qstash/step-config.ts
|
|
2152
|
+
var normalizeStepSettings = (settings) => {
|
|
2153
|
+
if (settings === void 0) {
|
|
2154
|
+
return void 0;
|
|
2155
|
+
}
|
|
2156
|
+
const { retries, retryDelay } = settings;
|
|
2157
|
+
if (retryDelay !== void 0 && typeof retryDelay !== "string") {
|
|
2158
|
+
throw new WorkflowNonRetryableError("Invalid step retryDelay: must be a string.");
|
|
2159
|
+
}
|
|
2160
|
+
if (retries !== void 0 && (!Number.isInteger(retries) || retries < 0)) {
|
|
2161
|
+
throw new WorkflowNonRetryableError("Invalid step retries: must be a non-negative integer.");
|
|
2162
|
+
}
|
|
2163
|
+
return { ...settings, retryDelay: retryDelay?.trim() || void 0 };
|
|
2164
|
+
};
|
|
2165
|
+
var DEFAULT_PARALLELISM = 0;
|
|
2166
|
+
var DEFAULT_RATE = 0;
|
|
2167
|
+
var DEFAULT_PERIOD_SECONDS = 1;
|
|
2168
|
+
var SECONDS_PER_UNIT = {
|
|
2169
|
+
s: 1,
|
|
2170
|
+
m: 60,
|
|
2171
|
+
h: 60 * 60,
|
|
2172
|
+
d: 24 * 60 * 60
|
|
2173
|
+
};
|
|
2174
|
+
var durationToSeconds = (duration) => {
|
|
2175
|
+
if (typeof duration === "number") {
|
|
2176
|
+
return Number.isFinite(duration) ? duration : void 0;
|
|
2177
|
+
}
|
|
2178
|
+
const match = /^(\d+)([smhd])$/.exec(duration.trim());
|
|
2179
|
+
if (match) {
|
|
2180
|
+
return Number(match[1]) * SECONDS_PER_UNIT[match[2]];
|
|
2181
|
+
}
|
|
2182
|
+
const seconds = Number(duration.trim());
|
|
2183
|
+
return Number.isFinite(seconds) ? seconds : void 0;
|
|
2184
|
+
};
|
|
2185
|
+
var normalizeFlowControl = (flowControl) => {
|
|
2186
|
+
const period = flowControl.period === void 0 ? void 0 : durationToSeconds(flowControl.period);
|
|
2187
|
+
return {
|
|
2188
|
+
key: flowControl.key,
|
|
2189
|
+
parallelism: flowControl.parallelism ?? DEFAULT_PARALLELISM,
|
|
2190
|
+
rate: flowControl.rate ?? flowControl.ratePerSecond ?? DEFAULT_RATE,
|
|
2191
|
+
period: period ?? DEFAULT_PERIOD_SECONDS
|
|
2192
|
+
};
|
|
2193
|
+
};
|
|
2194
|
+
var parseFlowControlHeaders = (key, value) => {
|
|
2195
|
+
if (!key) {
|
|
2196
|
+
return void 0;
|
|
2197
|
+
}
|
|
2198
|
+
const flowControl = {
|
|
2199
|
+
key,
|
|
2200
|
+
parallelism: DEFAULT_PARALLELISM,
|
|
2201
|
+
rate: DEFAULT_RATE,
|
|
2202
|
+
period: DEFAULT_PERIOD_SECONDS
|
|
2203
|
+
};
|
|
2204
|
+
for (const entry of (value ?? "").split(",")) {
|
|
2205
|
+
const [name, rawValue] = entry.split("=").map((part) => part.trim());
|
|
2206
|
+
if (!name || rawValue === void 0) {
|
|
2207
|
+
continue;
|
|
2208
|
+
}
|
|
2209
|
+
switch (name) {
|
|
2210
|
+
case "parallelism": {
|
|
2211
|
+
flowControl.parallelism = Number(rawValue);
|
|
2212
|
+
break;
|
|
2213
|
+
}
|
|
2214
|
+
case "rate": {
|
|
2215
|
+
flowControl.rate = Number(rawValue);
|
|
2216
|
+
break;
|
|
2217
|
+
}
|
|
2218
|
+
case "period": {
|
|
2219
|
+
flowControl.period = durationToSeconds(rawValue) ?? DEFAULT_PERIOD_SECONDS;
|
|
2220
|
+
break;
|
|
2221
|
+
}
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
return flowControl;
|
|
2225
|
+
};
|
|
2226
|
+
var getEffectiveConfig = (headers) => {
|
|
2227
|
+
const retriesHeader = headers.get(RETRIES_HEADER);
|
|
2228
|
+
return {
|
|
2229
|
+
flowControl: parseFlowControlHeaders(
|
|
2230
|
+
headers.get(FLOW_CONTROL_KEY_HEADER),
|
|
2231
|
+
headers.get(FLOW_CONTROL_VALUE_HEADER)
|
|
2232
|
+
),
|
|
2233
|
+
retries: retriesHeader === null ? void 0 : Number(retriesHeader),
|
|
2234
|
+
retryDelay: headers.get(RETRY_DELAY_HEADER) ?? void 0,
|
|
2235
|
+
hasStepConfig: headers.get(WORKFLOW_STEP_CONFIG_HEADER) === "true"
|
|
2236
|
+
};
|
|
2237
|
+
};
|
|
2238
|
+
var describeStepSettingsMismatch = (stepSettings, effectiveConfig) => {
|
|
2239
|
+
if (stepSettings.flowControl) {
|
|
2240
|
+
const wanted = normalizeFlowControl(stepSettings.flowControl);
|
|
2241
|
+
const applied = effectiveConfig.flowControl;
|
|
2242
|
+
if (!applied || applied.key !== wanted.key || applied.parallelism !== wanted.parallelism || applied.rate !== wanted.rate || applied.period !== wanted.period) {
|
|
2243
|
+
return `flow control: expected ${JSON.stringify(wanted)}, delivery has ${JSON.stringify(applied)}`;
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
if (stepSettings.retries !== void 0 && effectiveConfig.retries !== void 0 && stepSettings.retries !== effectiveConfig.retries) {
|
|
2247
|
+
return `retries: expected ${stepSettings.retries}, delivery has ${effectiveConfig.retries}`;
|
|
2248
|
+
}
|
|
2249
|
+
if (stepSettings.retryDelay !== void 0 && stepSettings.retryDelay !== effectiveConfig.retryDelay) {
|
|
2250
|
+
return `retry delay: expected '${stepSettings.retryDelay}', delivery has '${effectiveConfig.retryDelay ?? ""}'`;
|
|
2251
|
+
}
|
|
2252
|
+
return void 0;
|
|
2253
|
+
};
|
|
2011
2254
|
|
|
2012
2255
|
// src/context/auto-executor.ts
|
|
2013
2256
|
var AutoExecutor = class _AutoExecutor {
|
|
@@ -2024,6 +2267,25 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2024
2267
|
stepCount = 0;
|
|
2025
2268
|
planStepCount = 0;
|
|
2026
2269
|
executingStep = false;
|
|
2270
|
+
/**
|
|
2271
|
+
* an executed step whose result hasn't been submitted to QStash yet.
|
|
2272
|
+
*
|
|
2273
|
+
* Set when a step which can produce its result in-process executes. The
|
|
2274
|
+
* submission waits so that the route function can continue and reveal
|
|
2275
|
+
* what comes next: if that is a single step with step-level settings,
|
|
2276
|
+
* the settings ride on this submission and no step config request is
|
|
2277
|
+
* needed, since the delivery of the submission is what executes it.
|
|
2278
|
+
*
|
|
2279
|
+
* Flushed when the next step is reached (see `addStep`) or when the
|
|
2280
|
+
* route function ends (see `flushPendingStep` in `serve`).
|
|
2281
|
+
*/
|
|
2282
|
+
pendingStep;
|
|
2283
|
+
/**
|
|
2284
|
+
* configuration QStash applied to the delivery being handled. A step
|
|
2285
|
+
* with step-level settings is settled by comparing its settings against
|
|
2286
|
+
* this.
|
|
2287
|
+
*/
|
|
2288
|
+
effectiveConfig;
|
|
2027
2289
|
/**
|
|
2028
2290
|
* @param context workflow context
|
|
2029
2291
|
* @param steps list of steps
|
|
@@ -2031,14 +2293,17 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2031
2293
|
* @param dispatchLifecycle lifecycle event dispatcher
|
|
2032
2294
|
* @param telemetry optional telemetry information
|
|
2033
2295
|
* @param invokeCount optional invoke count
|
|
2296
|
+
* @param effectiveConfig configuration QStash applied to the delivery
|
|
2297
|
+
* being handled
|
|
2034
2298
|
*/
|
|
2035
|
-
constructor(context, steps, dispatchDebug, dispatchLifecycle, telemetry, invokeCount) {
|
|
2299
|
+
constructor(context, steps, dispatchDebug, dispatchLifecycle, telemetry, invokeCount, effectiveConfig) {
|
|
2036
2300
|
this.context = context;
|
|
2037
2301
|
this.steps = steps;
|
|
2038
2302
|
this.dispatchDebug = dispatchDebug;
|
|
2039
2303
|
this.dispatchLifecycle = dispatchLifecycle;
|
|
2040
2304
|
this.telemetry = telemetry;
|
|
2041
2305
|
this.invokeCount = invokeCount ?? 0;
|
|
2306
|
+
this.effectiveConfig = effectiveConfig ?? { hasStepConfig: false };
|
|
2042
2307
|
this.nonPlanStepCount = this.steps.filter((step) => !step.targetStep).length;
|
|
2043
2308
|
}
|
|
2044
2309
|
/**
|
|
@@ -2073,6 +2338,15 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2073
2338
|
lazyStepList.push(stepInfo);
|
|
2074
2339
|
const index = this.indexInCurrentList++;
|
|
2075
2340
|
const requestComplete = this.deferExecution().then(async () => {
|
|
2341
|
+
const submitted = await this.submitPendingStep(
|
|
2342
|
+
lazyStepList.length === 1 ? lazyStepList[0].stepSettings : void 0
|
|
2343
|
+
);
|
|
2344
|
+
if (submitted.isErr()) {
|
|
2345
|
+
throw submitted.error;
|
|
2346
|
+
}
|
|
2347
|
+
if (submitted.value.result === "submitted-step") {
|
|
2348
|
+
throw submitted.value.abort;
|
|
2349
|
+
}
|
|
2076
2350
|
if (!this.promises.has(lazyStepList)) {
|
|
2077
2351
|
const promise2 = this.getExecutionPromise(lazyStepList);
|
|
2078
2352
|
this.promises.set(lazyStepList, promise2);
|
|
@@ -2126,18 +2400,108 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2126
2400
|
}
|
|
2127
2401
|
return parsedOut;
|
|
2128
2402
|
}
|
|
2129
|
-
const
|
|
2130
|
-
|
|
2403
|
+
const mismatch = lazyStep.stepSettings ? describeStepSettingsMismatch(lazyStep.stepSettings, this.effectiveConfig) : void 0;
|
|
2404
|
+
if (mismatch) {
|
|
2405
|
+
if (!this.effectiveConfig.hasStepConfig) {
|
|
2406
|
+
await publishStepConfigRequest({
|
|
2407
|
+
context: this.context,
|
|
2408
|
+
lazyStep,
|
|
2409
|
+
targetStep: this.stepCount,
|
|
2410
|
+
invokeCount: this.invokeCount,
|
|
2411
|
+
telemetry: this.telemetry,
|
|
2412
|
+
dispatchDebug: this.dispatchDebug
|
|
2413
|
+
});
|
|
2414
|
+
throw new WorkflowAbort(lazyStep.stepName);
|
|
2415
|
+
} else {
|
|
2416
|
+
await this.dispatchDebug("onWarning", {
|
|
2417
|
+
warning: `The step-level settings of step '${lazyStep.stepName}' were published but are not recognized on the delivery which carries them (${mismatch}). Executing the step with the settings of the delivery instead. This is a bug in @upstash/workflow, please report it.`
|
|
2418
|
+
});
|
|
2419
|
+
}
|
|
2420
|
+
}
|
|
2421
|
+
const resultStep = await executeStep({
|
|
2131
2422
|
lazyStep,
|
|
2132
2423
|
stepId: this.stepCount,
|
|
2424
|
+
concurrency: NO_CONCURRENCY,
|
|
2425
|
+
dispatchLifecycle: this.dispatchLifecycle
|
|
2426
|
+
});
|
|
2427
|
+
if (lazyStep.supportsDeferredSubmission) {
|
|
2428
|
+
const serializedOut = JSON.stringify(resultStep.out);
|
|
2429
|
+
this.pendingStep = {
|
|
2430
|
+
status: "held",
|
|
2431
|
+
lazyStep,
|
|
2432
|
+
resultStep: {
|
|
2433
|
+
...resultStep,
|
|
2434
|
+
out: serializedOut === void 0 ? void 0 : JSON.parse(serializedOut)
|
|
2435
|
+
}
|
|
2436
|
+
};
|
|
2437
|
+
return lazyStep.parseOut({
|
|
2438
|
+
...resultStep,
|
|
2439
|
+
out: serializedOut
|
|
2440
|
+
});
|
|
2441
|
+
}
|
|
2442
|
+
await submitStepResult({
|
|
2443
|
+
context: this.context,
|
|
2444
|
+
lazyStep,
|
|
2445
|
+
resultStep,
|
|
2133
2446
|
invokeCount: this.invokeCount,
|
|
2134
|
-
concurrency:
|
|
2447
|
+
concurrency: NO_CONCURRENCY,
|
|
2135
2448
|
telemetry: this.telemetry,
|
|
2136
|
-
dispatchDebug: this.dispatchDebug
|
|
2137
|
-
dispatchLifecycle: this.dispatchLifecycle
|
|
2449
|
+
dispatchDebug: this.dispatchDebug
|
|
2138
2450
|
});
|
|
2139
2451
|
throw new WorkflowAbort(lazyStep.stepName, resultStep);
|
|
2140
2452
|
}
|
|
2453
|
+
/**
|
|
2454
|
+
* Submits the result of the step being held, if there is one, and
|
|
2455
|
+
* returns the abort the caller has to throw to end the invocation.
|
|
2456
|
+
*
|
|
2457
|
+
* Returns undefined when no step is being held, which is the caller's
|
|
2458
|
+
* signal that there is nothing to end. Throws instead of returning
|
|
2459
|
+
* when the submission itself fails, so that error reaches the caller
|
|
2460
|
+
* rather than an abort claiming the step was submitted.
|
|
2461
|
+
*
|
|
2462
|
+
* Callers which arrive while a submission is already under way (the
|
|
2463
|
+
* steps of a parallel group, added together) wait for that one and get
|
|
2464
|
+
* the same abort, rather than submitting a second time.
|
|
2465
|
+
*
|
|
2466
|
+
* The next step's settings are attached whenever it has any, without
|
|
2467
|
+
* checking them against the current delivery: the executor only knows
|
|
2468
|
+
* the configuration of the delivery in hand, which inside a
|
|
2469
|
+
* delivery is the *previous* step's, so such a check would be wrong
|
|
2470
|
+
* exactly when two steps with settings follow each other.
|
|
2471
|
+
*
|
|
2472
|
+
* @param nextStepSettings step-level settings of the next step
|
|
2473
|
+
* @returns `submitted-step` with the abort which ends this invocation,
|
|
2474
|
+
* or `no-pending-step` when nothing was held and there is nothing to
|
|
2475
|
+
* end
|
|
2476
|
+
*/
|
|
2477
|
+
async submitPendingStep(nextStepSettings) {
|
|
2478
|
+
if (!this.pendingStep) {
|
|
2479
|
+
return ok({ result: "no-pending-step" });
|
|
2480
|
+
}
|
|
2481
|
+
try {
|
|
2482
|
+
if (this.pendingStep.status === "submitting") {
|
|
2483
|
+
return ok({ result: "submitted-step", abort: await this.pendingStep.submitted });
|
|
2484
|
+
}
|
|
2485
|
+
const { lazyStep, resultStep } = this.pendingStep;
|
|
2486
|
+
const submitted = (async () => {
|
|
2487
|
+
await submitStepResult({
|
|
2488
|
+
context: this.context,
|
|
2489
|
+
lazyStep,
|
|
2490
|
+
resultStep,
|
|
2491
|
+
invokeCount: this.invokeCount,
|
|
2492
|
+
concurrency: NO_CONCURRENCY,
|
|
2493
|
+
telemetry: this.telemetry,
|
|
2494
|
+
dispatchDebug: this.dispatchDebug,
|
|
2495
|
+
nextStepSettings
|
|
2496
|
+
});
|
|
2497
|
+
return new WorkflowAbort(lazyStep.stepName, resultStep);
|
|
2498
|
+
})();
|
|
2499
|
+
this.pendingStep = { status: "submitting", submitted };
|
|
2500
|
+
return ok({ result: "submitted-step", abort: await submitted });
|
|
2501
|
+
} catch (error) {
|
|
2502
|
+
return err(error);
|
|
2503
|
+
}
|
|
2504
|
+
}
|
|
2141
2505
|
/**
|
|
2142
2506
|
* Runs steps in parallel.
|
|
2143
2507
|
*
|
|
@@ -2186,15 +2550,20 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2186
2550
|
validateStep(parallelSteps[stepIndex], planStep);
|
|
2187
2551
|
try {
|
|
2188
2552
|
const parallelStep = parallelSteps[stepIndex];
|
|
2189
|
-
const resultStep = await
|
|
2190
|
-
context: this.context,
|
|
2553
|
+
const resultStep = await executeStep({
|
|
2191
2554
|
lazyStep: parallelStep,
|
|
2192
2555
|
stepId: planStep.targetStep,
|
|
2556
|
+
concurrency: parallelSteps.length,
|
|
2557
|
+
dispatchLifecycle: this.dispatchLifecycle
|
|
2558
|
+
});
|
|
2559
|
+
await submitStepResult({
|
|
2560
|
+
context: this.context,
|
|
2561
|
+
lazyStep: parallelStep,
|
|
2562
|
+
resultStep,
|
|
2193
2563
|
invokeCount: this.invokeCount,
|
|
2194
2564
|
concurrency: parallelSteps.length,
|
|
2195
2565
|
telemetry: this.telemetry,
|
|
2196
|
-
dispatchDebug: this.dispatchDebug
|
|
2197
|
-
dispatchLifecycle: this.dispatchLifecycle
|
|
2566
|
+
dispatchDebug: this.dispatchDebug
|
|
2198
2567
|
});
|
|
2199
2568
|
throw new WorkflowAbort(parallelStep.stepName, resultStep);
|
|
2200
2569
|
} catch (error) {
|
|
@@ -2412,6 +2781,11 @@ var BaseWorkflowApi = class {
|
|
|
2412
2781
|
|
|
2413
2782
|
// src/context/api/anthropic.ts
|
|
2414
2783
|
var AnthropicAPI = class extends BaseWorkflowApi {
|
|
2784
|
+
/**
|
|
2785
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2786
|
+
*
|
|
2787
|
+
* @see node_modules/@upstash/workflow/docs/integrations/anthropic.mdx
|
|
2788
|
+
*/
|
|
2415
2789
|
async call(stepName, settings) {
|
|
2416
2790
|
const { token, operation, ...parameters } = settings;
|
|
2417
2791
|
return await this.callApi(stepName, {
|
|
@@ -2427,6 +2801,11 @@ var AnthropicAPI = class extends BaseWorkflowApi {
|
|
|
2427
2801
|
// src/context/api/openai.ts
|
|
2428
2802
|
var import_qstash7 = require("@upstash/qstash");
|
|
2429
2803
|
var OpenAIAPI = class extends BaseWorkflowApi {
|
|
2804
|
+
/**
|
|
2805
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2806
|
+
*
|
|
2807
|
+
* @see node_modules/@upstash/workflow/docs/integrations/openai.mdx
|
|
2808
|
+
*/
|
|
2430
2809
|
async call(stepName, settings) {
|
|
2431
2810
|
const { token, organization, operation, baseURL, ...parameters } = settings;
|
|
2432
2811
|
const useOpenAI = baseURL === void 0;
|
|
@@ -2444,6 +2823,11 @@ var OpenAIAPI = class extends BaseWorkflowApi {
|
|
|
2444
2823
|
// src/context/api/resend.ts
|
|
2445
2824
|
var import_qstash8 = require("@upstash/qstash");
|
|
2446
2825
|
var ResendAPI = class extends BaseWorkflowApi {
|
|
2826
|
+
/**
|
|
2827
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2828
|
+
*
|
|
2829
|
+
* @see node_modules/@upstash/workflow/docs/integrations/resend.mdx
|
|
2830
|
+
*/
|
|
2447
2831
|
async call(stepName, settings) {
|
|
2448
2832
|
const { token, batch = false, ...parameters } = settings;
|
|
2449
2833
|
return await this.callApi(stepName, {
|
|
@@ -2618,8 +3002,9 @@ var WorkflowContext = class {
|
|
|
2618
3002
|
*
|
|
2619
3003
|
* ```ts
|
|
2620
3004
|
* import { Client } from "@upstash/qstash"
|
|
3005
|
+
* import { serve } from "@upstash/workflow/nextjs"
|
|
2621
3006
|
*
|
|
2622
|
-
* export const POST = serve(
|
|
3007
|
+
* export const { POST } = serve(
|
|
2623
3008
|
* async (context) => {
|
|
2624
3009
|
* ...
|
|
2625
3010
|
* },
|
|
@@ -2644,7 +3029,7 @@ var WorkflowContext = class {
|
|
|
2644
3029
|
* Can be overwritten by passing a `url` parameter in `serve`:
|
|
2645
3030
|
*
|
|
2646
3031
|
* ```ts
|
|
2647
|
-
* export const POST = serve(
|
|
3032
|
+
* export const { POST } = serve(
|
|
2648
3033
|
* async (context) => {
|
|
2649
3034
|
* ...
|
|
2650
3035
|
* },
|
|
@@ -2662,7 +3047,7 @@ var WorkflowContext = class {
|
|
|
2662
3047
|
*
|
|
2663
3048
|
* ```ts
|
|
2664
3049
|
* // set requestPayload type to MyPayload:
|
|
2665
|
-
* export const POST = serve<MyPayload>(
|
|
3050
|
+
* export const { POST } = serve<MyPayload>(
|
|
2666
3051
|
* async (context) => {
|
|
2667
3052
|
* ...
|
|
2668
3053
|
* }
|
|
@@ -2674,7 +3059,7 @@ var WorkflowContext = class {
|
|
|
2674
3059
|
* the `initialPayloadParser` parameter:
|
|
2675
3060
|
*
|
|
2676
3061
|
* ```ts
|
|
2677
|
-
* export const POST = serve<MyPayload>(
|
|
3062
|
+
* export const { POST } = serve<MyPayload>(
|
|
2678
3063
|
* async (context) => {
|
|
2679
3064
|
* ...
|
|
2680
3065
|
* },
|
|
@@ -2683,6 +3068,13 @@ var WorkflowContext = class {
|
|
|
2683
3068
|
* }
|
|
2684
3069
|
* )
|
|
2685
3070
|
* ```
|
|
3071
|
+
*
|
|
3072
|
+
* When a `context.call` response comes back, the route runs up to its first step with this
|
|
3073
|
+
* set to the call-result envelope (and `workflowRunId` set to a random id) before the SDK
|
|
3074
|
+
* recognizes the request. Code before the first step must not dereference, validate or
|
|
3075
|
+
* return early on the payload; read it inside `context.run`.
|
|
3076
|
+
*
|
|
3077
|
+
* @see node_modules/@upstash/workflow/docs/basics/serve/advanced.mdx
|
|
2686
3078
|
*/
|
|
2687
3079
|
requestPayload;
|
|
2688
3080
|
/**
|
|
@@ -2695,7 +3087,7 @@ var WorkflowContext = class {
|
|
|
2695
3087
|
* Can be set using the `env` option of serve:
|
|
2696
3088
|
*
|
|
2697
3089
|
* ```ts
|
|
2698
|
-
* export const POST = serve<MyPayload>(
|
|
3090
|
+
* export const { POST } = serve<MyPayload>(
|
|
2699
3091
|
* async (context) => {
|
|
2700
3092
|
* const key = context.env["API_KEY"];
|
|
2701
3093
|
* },
|
|
@@ -2743,6 +3135,46 @@ var WorkflowContext = class {
|
|
|
2743
3135
|
* `1` on the first retry, `2` on the second, and so on.
|
|
2744
3136
|
*/
|
|
2745
3137
|
retried;
|
|
3138
|
+
/**
|
|
3139
|
+
* Configuration QStash applied to the request being handled.
|
|
3140
|
+
*
|
|
3141
|
+
* This is the configuration of *this delivery*, not of the run: it is
|
|
3142
|
+
* the configuration the run was triggered with, except inside the
|
|
3143
|
+
* delivery which executes a step that has step-level settings, where
|
|
3144
|
+
* it is that step's settings. The fields below expose it; the executor
|
|
3145
|
+
* gets it directly rather than reaching through the context.
|
|
3146
|
+
*/
|
|
3147
|
+
effectiveConfig;
|
|
3148
|
+
/**
|
|
3149
|
+
* Flow control QStash applied to the request being handled, if any.
|
|
3150
|
+
*
|
|
3151
|
+
* This is the flow control of the request in hand, which is the one the
|
|
3152
|
+
* run was triggered with except inside a step carrying its own.
|
|
3153
|
+
*/
|
|
3154
|
+
get flowControl() {
|
|
3155
|
+
return this.effectiveConfig.flowControl;
|
|
3156
|
+
}
|
|
3157
|
+
/**
|
|
3158
|
+
* Retry limit QStash applied to the request being handled, if it
|
|
3159
|
+
* reported one.
|
|
3160
|
+
*
|
|
3161
|
+
* Not to be confused with {@link retried}, which is how many retries
|
|
3162
|
+
* have already happened.
|
|
3163
|
+
*
|
|
3164
|
+
* @see {@link flowControl} for what "the request being handled" means
|
|
3165
|
+
*/
|
|
3166
|
+
get retries() {
|
|
3167
|
+
return this.effectiveConfig.retries;
|
|
3168
|
+
}
|
|
3169
|
+
/**
|
|
3170
|
+
* Retry delay expression QStash applied to the request being handled,
|
|
3171
|
+
* if any.
|
|
3172
|
+
*
|
|
3173
|
+
* @see {@link flowControl} for what "the request being handled" means
|
|
3174
|
+
*/
|
|
3175
|
+
get retryDelay() {
|
|
3176
|
+
return this.effectiveConfig.retryDelay;
|
|
3177
|
+
}
|
|
2746
3178
|
constructor({
|
|
2747
3179
|
qstashClient,
|
|
2748
3180
|
workflowRunId,
|
|
@@ -2756,7 +3188,8 @@ var WorkflowContext = class {
|
|
|
2756
3188
|
invokeCount,
|
|
2757
3189
|
label,
|
|
2758
3190
|
retried,
|
|
2759
|
-
middlewareManager
|
|
3191
|
+
middlewareManager,
|
|
3192
|
+
effectiveConfig
|
|
2760
3193
|
}) {
|
|
2761
3194
|
this.qstashClient = qstashClient;
|
|
2762
3195
|
this.workflowRunId = workflowRunId;
|
|
@@ -2768,6 +3201,7 @@ var WorkflowContext = class {
|
|
|
2768
3201
|
this.env = env ?? {};
|
|
2769
3202
|
this.labels = label === void 0 ? [] : Array.isArray(label) ? label : label ? label.split(",") : [];
|
|
2770
3203
|
this.retried = retried ?? 0;
|
|
3204
|
+
this.effectiveConfig = effectiveConfig ?? { hasStepConfig: false };
|
|
2771
3205
|
const middlewareManagerInstance = middlewareManager ?? new MiddlewareManager([]);
|
|
2772
3206
|
middlewareManagerInstance.assignContext(this);
|
|
2773
3207
|
this.executor = new AutoExecutor(
|
|
@@ -2776,7 +3210,8 @@ var WorkflowContext = class {
|
|
|
2776
3210
|
middlewareManagerInstance.dispatchDebug.bind(middlewareManagerInstance),
|
|
2777
3211
|
middlewareManagerInstance.dispatchLifecycle.bind(middlewareManagerInstance),
|
|
2778
3212
|
telemetry,
|
|
2779
|
-
invokeCount
|
|
3213
|
+
invokeCount,
|
|
3214
|
+
this.effectiveConfig
|
|
2780
3215
|
);
|
|
2781
3216
|
}
|
|
2782
3217
|
/**
|
|
@@ -2802,13 +3237,41 @@ var WorkflowContext = class {
|
|
|
2802
3237
|
* ])
|
|
2803
3238
|
* ```
|
|
2804
3239
|
*
|
|
3240
|
+
* Step-level settings can be passed as a third argument, overriding
|
|
3241
|
+
* the settings the workflow run was triggered with for this step only:
|
|
3242
|
+
*
|
|
3243
|
+
* ```typescript
|
|
3244
|
+
* const result = await context.run(
|
|
3245
|
+
* "step 1",
|
|
3246
|
+
* () => {
|
|
3247
|
+
* return "result"
|
|
3248
|
+
* },
|
|
3249
|
+
* {
|
|
3250
|
+
* flowControl: { key: "custom-key", parallelism: 3 },
|
|
3251
|
+
* retries: 5,
|
|
3252
|
+
* }
|
|
3253
|
+
* )
|
|
3254
|
+
* ```
|
|
3255
|
+
*
|
|
3256
|
+
* Do not wrap steps in try/catch or `.catch` without rethrowing `WorkflowAbort`: each step
|
|
3257
|
+
* throws it to end the request. To stop retrying a failed step, throw
|
|
3258
|
+
* `WorkflowNonRetryableError`.
|
|
3259
|
+
*
|
|
2805
3260
|
* @param stepName name of the step
|
|
2806
3261
|
* @param stepFunction step function to be executed
|
|
3262
|
+
* @param stepSettings step-level settings for this step
|
|
2807
3263
|
* @returns result of the step function
|
|
3264
|
+
* @see node_modules/@upstash/workflow/docs/steps/run.mdx
|
|
2808
3265
|
*/
|
|
2809
|
-
async run(stepName, stepFunction) {
|
|
3266
|
+
async run(stepName, stepFunction, stepSettings) {
|
|
3267
|
+
const normalizedSettings = normalizeStepSettings(stepSettings);
|
|
3268
|
+
if (stepSettings) {
|
|
3269
|
+
validateFlowControl(stepSettings.flowControl);
|
|
3270
|
+
}
|
|
2810
3271
|
const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
|
|
2811
|
-
return await this.addStep(
|
|
3272
|
+
return await this.addStep(
|
|
3273
|
+
new LazyFunctionStep(this, stepName, wrappedStepFunction, normalizedSettings)
|
|
3274
|
+
);
|
|
2812
3275
|
}
|
|
2813
3276
|
/**
|
|
2814
3277
|
* Stops the execution for the duration provided.
|
|
@@ -2818,8 +3281,9 @@ var WorkflowContext = class {
|
|
|
2818
3281
|
* ```
|
|
2819
3282
|
*
|
|
2820
3283
|
* @param stepName
|
|
2821
|
-
* @param duration sleep duration in seconds
|
|
3284
|
+
* @param duration sleep duration in seconds, or a duration string such as "10m"
|
|
2822
3285
|
* @returns undefined
|
|
3286
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleep.mdx
|
|
2823
3287
|
*/
|
|
2824
3288
|
async sleep(stepName, duration) {
|
|
2825
3289
|
await this.addStep(new LazySleepStep(this, stepName, duration));
|
|
@@ -2833,8 +3297,11 @@ var WorkflowContext = class {
|
|
|
2833
3297
|
*
|
|
2834
3298
|
* @param stepName
|
|
2835
3299
|
* @param datetime time to sleep until. Can be provided as a number (in unix seconds),
|
|
2836
|
-
* as a Date object or a string (passed to `new Date(datetimeString)`)
|
|
3300
|
+
* as a Date object or a string (passed to `new Date(datetimeString)`).
|
|
3301
|
+
* A millisecond timestamp such as `Date.now()` is too far in the future, so QStash
|
|
3302
|
+
* rejects the step and the run fails. Pass `new Date(ms)` instead.
|
|
2837
3303
|
* @returns undefined
|
|
3304
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleepUntil.mdx
|
|
2838
3305
|
*/
|
|
2839
3306
|
async sleepUntil(stepName, datetime) {
|
|
2840
3307
|
let time;
|
|
@@ -2885,7 +3352,7 @@ var WorkflowContext = class {
|
|
|
2885
3352
|
* Pauses workflow execution until a specific event occurs or a timeout is reached.
|
|
2886
3353
|
*
|
|
2887
3354
|
*```ts
|
|
2888
|
-
* const result = await
|
|
3355
|
+
* const result = await context.waitForEvent("payment-confirmed", "payment.confirmed", {
|
|
2889
3356
|
* timeout: "5m"
|
|
2890
3357
|
* });
|
|
2891
3358
|
*```
|
|
@@ -2909,11 +3376,14 @@ var WorkflowContext = class {
|
|
|
2909
3376
|
* Alternatively, you can use the `context.notify` method.
|
|
2910
3377
|
*
|
|
2911
3378
|
* @param stepName
|
|
2912
|
-
* @param eventId - Unique identifier for the event to wait for
|
|
3379
|
+
* @param eventId - Unique identifier for the event to wait for. Only letters, digits,
|
|
3380
|
+
* "-", "_" and "." are allowed. QStash rejects other characters, such as ":", which
|
|
3381
|
+
* stops the run with only a "Tried to append to a cancelled workflow" warning.
|
|
2913
3382
|
* @param options - Configuration options.
|
|
2914
3383
|
* @returns `{ timeout: boolean, eventData: TEventData }`.
|
|
2915
3384
|
* The `timeout` property specifies if the workflow has timed out. The `eventData`
|
|
2916
3385
|
* is the data passed when notifying this workflow of an event.
|
|
3386
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForEvent.mdx
|
|
2917
3387
|
*/
|
|
2918
3388
|
async waitForEvent(stepName, eventId, options = {}) {
|
|
2919
3389
|
const { timeout = "7d" } = options;
|
|
@@ -2935,28 +3405,31 @@ var WorkflowContext = class {
|
|
|
2935
3405
|
* will receive the given event data and resume execution.
|
|
2936
3406
|
*
|
|
2937
3407
|
* The response includes the same eventId and eventData. Additionally, there is
|
|
2938
|
-
* a notifyResponse field which contains a list of `
|
|
2939
|
-
* to a notified workflow run.
|
|
2940
|
-
*
|
|
2941
|
-
* Optionally, you can pass a workflowRunId to enable lookback functionality:
|
|
2942
|
-
*
|
|
2943
|
-
* ```ts
|
|
2944
|
-
* const { eventId, eventData, notifyResponse } = await context.notify(
|
|
2945
|
-
* "notify step", "event-id", "event-data", "wfr_123"
|
|
2946
|
-
* );
|
|
2947
|
-
* ```
|
|
3408
|
+
* a notifyResponse field which contains a list of `NotifyResponse` objects, each corresponding
|
|
3409
|
+
* to a notified workflow run. The notified `Waiter` is under `notifyResponse[i].waiter`.
|
|
2948
3410
|
*
|
|
2949
3411
|
* @param stepName
|
|
2950
|
-
* @param eventId event id to notify
|
|
3412
|
+
* @param eventId event id to notify. Only letters, digits, "-", "_" and "." are allowed.
|
|
3413
|
+
* QStash rejects other characters, such as ":", which stops the run with only a
|
|
3414
|
+
* "Tried to append to a cancelled workflow" warning.
|
|
2951
3415
|
* @param eventData event data to notify with
|
|
2952
|
-
* @param workflowRunId optional workflow run id for lookback
|
|
2953
|
-
*
|
|
3416
|
+
* @param workflowRunId optional workflow run id for lookback: the event is kept for the run
|
|
3417
|
+
* even if it has not reached `waitForEvent` yet
|
|
3418
|
+
* @returns notify response which has event id, event data and list of waiters which were
|
|
3419
|
+
* notified. With `workflowRunId`, QStash returns `notifyResponse` as a single object instead
|
|
3420
|
+
* of an array, despite the type.
|
|
3421
|
+
* @see node_modules/@upstash/workflow/docs/steps/notify.mdx
|
|
2954
3422
|
*/
|
|
2955
3423
|
async notify(stepName, eventId, eventData, workflowRunId) {
|
|
2956
3424
|
return await this.addStep(
|
|
2957
3425
|
new LazyNotifyStep(this, stepName, eventId, eventData, this.qstashClient.http, workflowRunId)
|
|
2958
3426
|
);
|
|
2959
3427
|
}
|
|
3428
|
+
/**
|
|
3429
|
+
* Starts another workflow run and waits for its result.
|
|
3430
|
+
*
|
|
3431
|
+
* @see node_modules/@upstash/workflow/docs/steps/invoke.mdx
|
|
3432
|
+
*/
|
|
2960
3433
|
async invoke(stepName, settings) {
|
|
2961
3434
|
validateLabel(settings.label);
|
|
2962
3435
|
validateFlowControl(settings.flowControl);
|
|
@@ -2964,9 +3437,15 @@ var WorkflowContext = class {
|
|
|
2964
3437
|
new LazyInvokeStep(this, stepName, settings)
|
|
2965
3438
|
);
|
|
2966
3439
|
}
|
|
3440
|
+
/**
|
|
3441
|
+
* @see node_modules/@upstash/workflow/docs/steps/createWebhook.mdx
|
|
3442
|
+
*/
|
|
2967
3443
|
async createWebhook(stepName) {
|
|
2968
3444
|
return await this.addStep(new LazyCreateWebhookStep(this, stepName));
|
|
2969
3445
|
}
|
|
3446
|
+
/**
|
|
3447
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForWebhook.mdx
|
|
3448
|
+
*/
|
|
2970
3449
|
async waitForWebhook(stepName, webhook, timeout) {
|
|
2971
3450
|
return await this.addStep(new LazyWaitForWebhookStep(this, stepName, webhook, timeout));
|
|
2972
3451
|
}
|
|
@@ -2975,6 +3454,8 @@ var WorkflowContext = class {
|
|
|
2975
3454
|
*
|
|
2976
3455
|
* Will throw WorkflowCancelAbort to stop workflow execution.
|
|
2977
3456
|
* Shouldn't be inside try/catch.
|
|
3457
|
+
*
|
|
3458
|
+
* @see node_modules/@upstash/workflow/docs/steps/cancel.mdx
|
|
2978
3459
|
*/
|
|
2979
3460
|
async cancel() {
|
|
2980
3461
|
throw new WorkflowCancelAbort();
|
|
@@ -2993,6 +3474,11 @@ var WorkflowContext = class {
|
|
|
2993
3474
|
throw error;
|
|
2994
3475
|
}
|
|
2995
3476
|
}
|
|
3477
|
+
/**
|
|
3478
|
+
* Calls OpenAI, Anthropic or Resend through QStash, like `context.call`.
|
|
3479
|
+
*
|
|
3480
|
+
* @see node_modules/@upstash/workflow/docs/steps/api.mdx
|
|
3481
|
+
*/
|
|
2996
3482
|
get api() {
|
|
2997
3483
|
return new WorkflowApi({
|
|
2998
3484
|
context: this
|
|
@@ -3031,7 +3517,7 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3031
3517
|
*
|
|
3032
3518
|
* @param routeFunction
|
|
3033
3519
|
*/
|
|
3034
|
-
static async tryAuthentication(routeFunction, context) {
|
|
3520
|
+
static async tryAuthentication(routeFunction, context, effectiveConfig) {
|
|
3035
3521
|
const disabledContext = new _DisabledWorkflowContext({
|
|
3036
3522
|
qstashClient: new import_qstash10.Client({
|
|
3037
3523
|
baseUrl: "disabled-client",
|
|
@@ -3046,7 +3532,10 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3046
3532
|
initialPayload: context.requestPayload,
|
|
3047
3533
|
env: context.env,
|
|
3048
3534
|
label: context.labels,
|
|
3049
|
-
retried: context.retried
|
|
3535
|
+
retried: context.retried,
|
|
3536
|
+
// the route function runs for real here until it reaches a step, so
|
|
3537
|
+
// it observes the same configuration as it will on the real context
|
|
3538
|
+
effectiveConfig
|
|
3050
3539
|
});
|
|
3051
3540
|
try {
|
|
3052
3541
|
await routeFunction(disabledContext);
|
|
@@ -3263,6 +3752,7 @@ var handleFailure = async ({
|
|
|
3263
3752
|
}
|
|
3264
3753
|
const userHeaders = recreateUserHeaders(request.headers);
|
|
3265
3754
|
const retried = Number(request.headers.get(WORKFLOW_RETRIED_HEADER) ?? "0");
|
|
3755
|
+
const effectiveConfig = getEffectiveConfig(request.headers);
|
|
3266
3756
|
const workflowContext = new WorkflowContext({
|
|
3267
3757
|
qstashClient,
|
|
3268
3758
|
workflowRunId,
|
|
@@ -3276,11 +3766,13 @@ var handleFailure = async ({
|
|
|
3276
3766
|
label: userHeaders.get(WORKFLOW_LABEL_HEADER) ?? void 0,
|
|
3277
3767
|
retried,
|
|
3278
3768
|
workflowRunCreatedAt: workflowCreatedAt,
|
|
3279
|
-
middlewareManager: void 0
|
|
3769
|
+
middlewareManager: void 0,
|
|
3770
|
+
effectiveConfig
|
|
3280
3771
|
});
|
|
3281
3772
|
const authCheck = await DisabledWorkflowContext.tryAuthentication(
|
|
3282
3773
|
routeFunction,
|
|
3283
|
-
workflowContext
|
|
3774
|
+
workflowContext,
|
|
3775
|
+
effectiveConfig
|
|
3284
3776
|
);
|
|
3285
3777
|
if (authCheck.isErr()) {
|
|
3286
3778
|
await dispatchDebug?.("onError", {
|
|
@@ -3771,12 +4263,14 @@ var serveBase = (routeFunction, telemetry, options, internalOptions) => {
|
|
|
3771
4263
|
const label = request.headers.get(WORKFLOW_LABEL_HEADER) ?? void 0;
|
|
3772
4264
|
const workflowRunCreatedAtHeader = request.headers.get(WORKFLOW_CREATED_AT_HEADER);
|
|
3773
4265
|
const workflowRunCreatedAt = workflowRunCreatedAtHeader ? Number(workflowRunCreatedAtHeader) : isFirstInvocation ? Date.now() : 0;
|
|
4266
|
+
const effectiveConfig = getEffectiveConfig(request.headers);
|
|
3774
4267
|
const workflowContext = new WorkflowContext({
|
|
3775
4268
|
qstashClient: regionalClient,
|
|
3776
4269
|
workflowRunId,
|
|
3777
4270
|
initialPayload: isThirdPartyCallResult(request) ? JSON.parse(rawInitialPayload) : initialPayloadParser(rawInitialPayload),
|
|
3778
4271
|
headers: recreateUserHeaders(request.headers),
|
|
3779
4272
|
steps,
|
|
4273
|
+
effectiveConfig,
|
|
3780
4274
|
url: workflowUrl,
|
|
3781
4275
|
env,
|
|
3782
4276
|
telemetry,
|
|
@@ -3788,7 +4282,8 @@ var serveBase = (routeFunction, telemetry, options, internalOptions) => {
|
|
|
3788
4282
|
});
|
|
3789
4283
|
const authCheck = await DisabledWorkflowContext.tryAuthentication(
|
|
3790
4284
|
routeFunction,
|
|
3791
|
-
workflowContext
|
|
4285
|
+
workflowContext,
|
|
4286
|
+
effectiveConfig
|
|
3792
4287
|
);
|
|
3793
4288
|
if (authCheck.isErr()) {
|
|
3794
4289
|
throw authCheck.error;
|
|
@@ -3825,7 +4320,23 @@ var serveBase = (routeFunction, telemetry, options, internalOptions) => {
|
|
|
3825
4320
|
if (steps.length === 1) {
|
|
3826
4321
|
await middlewareManager.dispatchLifecycle("runStarted", {});
|
|
3827
4322
|
}
|
|
3828
|
-
|
|
4323
|
+
let outcome;
|
|
4324
|
+
try {
|
|
4325
|
+
outcome = { ran: true, result: await routeFunction(workflowContext) };
|
|
4326
|
+
} catch (error) {
|
|
4327
|
+
outcome = { ran: false, error };
|
|
4328
|
+
}
|
|
4329
|
+
const submitted = await flushPendingStep(workflowContext);
|
|
4330
|
+
if (submitted.isErr()) {
|
|
4331
|
+
throw submitted.error;
|
|
4332
|
+
}
|
|
4333
|
+
if (submitted.value.result === "submitted-step") {
|
|
4334
|
+
throw submitted.value.abort;
|
|
4335
|
+
}
|
|
4336
|
+
if (!outcome.ran) {
|
|
4337
|
+
throw outcome.error;
|
|
4338
|
+
}
|
|
4339
|
+
return outcome.result;
|
|
3829
4340
|
},
|
|
3830
4341
|
onCleanup: async (result2) => {
|
|
3831
4342
|
await middlewareManager.dispatchLifecycle("runCompleted", {
|