@upstash/workflow 1.4.0 → 1.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 +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 +374 -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 +567 -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 +104 -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 +2944 -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/h3.js
CHANGED
|
@@ -526,6 +526,14 @@ var WORKFLOW_CREATED_AT_HEADER = "Upstash-Workflow-CreatedAt";
|
|
|
526
526
|
var WORKFLOW_FAILURE_HEADER = "Upstash-Workflow-Is-Failure";
|
|
527
527
|
var WORKFLOW_FAILURE_CALLBACK_HEADER = "Upstash-Workflow-Failure-Callback";
|
|
528
528
|
var WORKFLOW_FEATURE_HEADER = "Upstash-Feature-Set";
|
|
529
|
+
var WORKFLOW_FEATURE_SET = "LazyFetch,InitialBody,WF_DetectTrigger,WF_TriggerOnConfig";
|
|
530
|
+
var WORKFLOW_STEP_CONFIG_CALL_TYPE = "stepConfig";
|
|
531
|
+
var WORKFLOW_STEP_CONFIG_HEADER = "Upstash-Workflow-Step-Config";
|
|
532
|
+
var FLOW_CONTROL_KEY_HEADER = "Upstash-Flow-Control-Key";
|
|
533
|
+
var FLOW_CONTROL_VALUE_HEADER = "Upstash-Flow-Control-Value";
|
|
534
|
+
var RETRIES_HEADER = "Upstash-Retries";
|
|
535
|
+
var RETRY_DELAY_HEADER = "Upstash-Retry-Delay";
|
|
536
|
+
var WORKFLOW_STEP_CONFIG_FEATURE = "WF_StepConfig";
|
|
529
537
|
var WORKFLOW_INVOKE_COUNT_HEADER = "Upstash-Workflow-Invoke-Count";
|
|
530
538
|
var WORKFLOW_RETRIED_HEADER = "Upstash-Retried";
|
|
531
539
|
var WORKFLOW_LABEL_HEADER = "Upstash-Label";
|
|
@@ -543,102 +551,6 @@ var TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk";
|
|
|
543
551
|
var TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework";
|
|
544
552
|
var TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime";
|
|
545
553
|
|
|
546
|
-
// src/context/auto-executor.ts
|
|
547
|
-
var import_qstash5 = require("@upstash/qstash");
|
|
548
|
-
|
|
549
|
-
// src/qstash/headers.ts
|
|
550
|
-
var import_qstash4 = require("@upstash/qstash");
|
|
551
|
-
|
|
552
|
-
// src/utils.ts
|
|
553
|
-
var NANOID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
|
554
|
-
var NANOID_LENGTH = 21;
|
|
555
|
-
var RESOURCE_NAME_PATTERN = /^[a-zA-Z0-9\-_.]+$/;
|
|
556
|
-
function validateLabel(label) {
|
|
557
|
-
if (label === void 0) return;
|
|
558
|
-
const labels = Array.isArray(label) ? label : [label];
|
|
559
|
-
if (labels.length === 0) {
|
|
560
|
-
throw new WorkflowNonRetryableError("Invalid label: label array must not be empty.");
|
|
561
|
-
}
|
|
562
|
-
for (const value of labels) {
|
|
563
|
-
if (!RESOURCE_NAME_PATTERN.test(value)) {
|
|
564
|
-
throw new WorkflowNonRetryableError(
|
|
565
|
-
`Invalid label "${value}": must be alphanumeric, hyphen, underscore, or period.`
|
|
566
|
-
);
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
function serializeLabel(label) {
|
|
571
|
-
return Array.isArray(label) ? label.join(",") : label;
|
|
572
|
-
}
|
|
573
|
-
function validateFlowControl(flowControl) {
|
|
574
|
-
if (flowControl?.key !== void 0 && !RESOURCE_NAME_PATTERN.test(flowControl.key)) {
|
|
575
|
-
throw new WorkflowNonRetryableError(
|
|
576
|
-
`Invalid flow control key "${flowControl.key}": must be alphanumeric, hyphen, underscore, or period.`
|
|
577
|
-
);
|
|
578
|
-
}
|
|
579
|
-
}
|
|
580
|
-
function getRandomInt() {
|
|
581
|
-
return Math.floor(Math.random() * NANOID_CHARS.length);
|
|
582
|
-
}
|
|
583
|
-
function nanoid(length = NANOID_LENGTH) {
|
|
584
|
-
return Array.from({ length }).map(() => NANOID_CHARS[getRandomInt()]).join("");
|
|
585
|
-
}
|
|
586
|
-
function getWorkflowRunId(id) {
|
|
587
|
-
return `wfr_${id ?? nanoid()}`;
|
|
588
|
-
}
|
|
589
|
-
function decodeBase64(base64) {
|
|
590
|
-
const binString = atob(base64);
|
|
591
|
-
try {
|
|
592
|
-
const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
593
|
-
return new TextDecoder().decode(intArray);
|
|
594
|
-
} catch (error) {
|
|
595
|
-
console.warn(
|
|
596
|
-
`Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
|
|
597
|
-
);
|
|
598
|
-
return binString;
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
function getUserIdFromToken(qstashClient) {
|
|
602
|
-
try {
|
|
603
|
-
const token = qstashClient.token;
|
|
604
|
-
const decodedToken = decodeBase64(token);
|
|
605
|
-
const tokenPayload = JSON.parse(decodedToken);
|
|
606
|
-
const userId = tokenPayload.UserID;
|
|
607
|
-
if (!userId) {
|
|
608
|
-
throw new WorkflowError("QStash token payload does not contain userId");
|
|
609
|
-
}
|
|
610
|
-
return userId;
|
|
611
|
-
} catch (error) {
|
|
612
|
-
throw new WorkflowError(
|
|
613
|
-
`Failed to decode QStash token while running create webhook step: ${error.message}`
|
|
614
|
-
);
|
|
615
|
-
}
|
|
616
|
-
}
|
|
617
|
-
function getQStashUrl(qstashClient) {
|
|
618
|
-
try {
|
|
619
|
-
const requester = qstashClient.http;
|
|
620
|
-
const baseUrl = requester.baseUrl;
|
|
621
|
-
if (!baseUrl) {
|
|
622
|
-
throw new WorkflowError("QStash client does not have a baseUrl");
|
|
623
|
-
}
|
|
624
|
-
return baseUrl;
|
|
625
|
-
} catch (error) {
|
|
626
|
-
throw new WorkflowError(`Failed to get QStash URL from client: ${error.message}`);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
function getEventId() {
|
|
630
|
-
return `evt_${nanoid(15)}`;
|
|
631
|
-
}
|
|
632
|
-
function stringifyBody(body) {
|
|
633
|
-
if (body === void 0) {
|
|
634
|
-
return void 0;
|
|
635
|
-
}
|
|
636
|
-
if (typeof body === "string") {
|
|
637
|
-
return body;
|
|
638
|
-
}
|
|
639
|
-
return JSON.stringify(body);
|
|
640
|
-
}
|
|
641
|
-
|
|
642
554
|
// node_modules/neverthrow/dist/index.es.js
|
|
643
555
|
var defaultErrorConfig = {
|
|
644
556
|
withStackTrace: false
|
|
@@ -1054,6 +966,102 @@ var Err = class {
|
|
|
1054
966
|
};
|
|
1055
967
|
var fromThrowable = Result.fromThrowable;
|
|
1056
968
|
|
|
969
|
+
// src/context/auto-executor.ts
|
|
970
|
+
var import_qstash5 = require("@upstash/qstash");
|
|
971
|
+
|
|
972
|
+
// src/qstash/headers.ts
|
|
973
|
+
var import_qstash4 = require("@upstash/qstash");
|
|
974
|
+
|
|
975
|
+
// src/utils.ts
|
|
976
|
+
var NANOID_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_";
|
|
977
|
+
var NANOID_LENGTH = 21;
|
|
978
|
+
var RESOURCE_NAME_PATTERN = /^[a-zA-Z0-9\-_.]+$/;
|
|
979
|
+
function validateLabel(label) {
|
|
980
|
+
if (label === void 0) return;
|
|
981
|
+
const labels = Array.isArray(label) ? label : [label];
|
|
982
|
+
if (labels.length === 0) {
|
|
983
|
+
throw new WorkflowNonRetryableError("Invalid label: label array must not be empty.");
|
|
984
|
+
}
|
|
985
|
+
for (const value of labels) {
|
|
986
|
+
if (!RESOURCE_NAME_PATTERN.test(value)) {
|
|
987
|
+
throw new WorkflowNonRetryableError(
|
|
988
|
+
`Invalid label "${value}": must be alphanumeric, hyphen, underscore, or period.`
|
|
989
|
+
);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
function serializeLabel(label) {
|
|
994
|
+
return Array.isArray(label) ? label.join(",") : label;
|
|
995
|
+
}
|
|
996
|
+
function validateFlowControl(flowControl) {
|
|
997
|
+
if (flowControl?.key !== void 0 && !RESOURCE_NAME_PATTERN.test(flowControl.key)) {
|
|
998
|
+
throw new WorkflowNonRetryableError(
|
|
999
|
+
`Invalid flow control key "${flowControl.key}": must be alphanumeric, hyphen, underscore, or period.`
|
|
1000
|
+
);
|
|
1001
|
+
}
|
|
1002
|
+
}
|
|
1003
|
+
function getRandomInt() {
|
|
1004
|
+
return Math.floor(Math.random() * NANOID_CHARS.length);
|
|
1005
|
+
}
|
|
1006
|
+
function nanoid(length = NANOID_LENGTH) {
|
|
1007
|
+
return Array.from({ length }).map(() => NANOID_CHARS[getRandomInt()]).join("");
|
|
1008
|
+
}
|
|
1009
|
+
function getWorkflowRunId(id) {
|
|
1010
|
+
return `wfr_${id ?? nanoid()}`;
|
|
1011
|
+
}
|
|
1012
|
+
function decodeBase64(base64) {
|
|
1013
|
+
const binString = atob(base64);
|
|
1014
|
+
try {
|
|
1015
|
+
const intArray = Uint8Array.from(binString, (m) => m.codePointAt(0));
|
|
1016
|
+
return new TextDecoder().decode(intArray);
|
|
1017
|
+
} catch (error) {
|
|
1018
|
+
console.warn(
|
|
1019
|
+
`Upstash Qstash: Failed while decoding base64 "${base64}". Decoding with atob and returning it instead. ${error}`
|
|
1020
|
+
);
|
|
1021
|
+
return binString;
|
|
1022
|
+
}
|
|
1023
|
+
}
|
|
1024
|
+
function getUserIdFromToken(qstashClient) {
|
|
1025
|
+
try {
|
|
1026
|
+
const token = qstashClient.token;
|
|
1027
|
+
const decodedToken = decodeBase64(token);
|
|
1028
|
+
const tokenPayload = JSON.parse(decodedToken);
|
|
1029
|
+
const userId = tokenPayload.UserID;
|
|
1030
|
+
if (!userId) {
|
|
1031
|
+
throw new WorkflowError("QStash token payload does not contain userId");
|
|
1032
|
+
}
|
|
1033
|
+
return userId;
|
|
1034
|
+
} catch (error) {
|
|
1035
|
+
throw new WorkflowError(
|
|
1036
|
+
`Failed to decode QStash token while running create webhook step: ${error.message}`
|
|
1037
|
+
);
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
function getQStashUrl(qstashClient) {
|
|
1041
|
+
try {
|
|
1042
|
+
const requester = qstashClient.http;
|
|
1043
|
+
const baseUrl = requester.baseUrl;
|
|
1044
|
+
if (!baseUrl) {
|
|
1045
|
+
throw new WorkflowError("QStash client does not have a baseUrl");
|
|
1046
|
+
}
|
|
1047
|
+
return baseUrl;
|
|
1048
|
+
} catch (error) {
|
|
1049
|
+
throw new WorkflowError(`Failed to get QStash URL from client: ${error.message}`);
|
|
1050
|
+
}
|
|
1051
|
+
}
|
|
1052
|
+
function getEventId() {
|
|
1053
|
+
return `evt_${nanoid(15)}`;
|
|
1054
|
+
}
|
|
1055
|
+
function stringifyBody(body) {
|
|
1056
|
+
if (body === void 0) {
|
|
1057
|
+
return void 0;
|
|
1058
|
+
}
|
|
1059
|
+
if (typeof body === "string") {
|
|
1060
|
+
return body;
|
|
1061
|
+
}
|
|
1062
|
+
return JSON.stringify(body);
|
|
1063
|
+
}
|
|
1064
|
+
|
|
1057
1065
|
// src/types.ts
|
|
1058
1066
|
var StepTypes = [
|
|
1059
1067
|
"Initial",
|
|
@@ -1163,6 +1171,10 @@ var triggerFirstInvocation = async (params) => {
|
|
|
1163
1171
|
return err(error_);
|
|
1164
1172
|
}
|
|
1165
1173
|
};
|
|
1174
|
+
var flushPendingStep = async (workflowContext) => {
|
|
1175
|
+
const { executor } = workflowContext;
|
|
1176
|
+
return await executor.submitPendingStep();
|
|
1177
|
+
};
|
|
1166
1178
|
var triggerRouteFunction = async ({
|
|
1167
1179
|
onCleanup,
|
|
1168
1180
|
onStep,
|
|
@@ -1375,8 +1387,29 @@ If you want to disable QStash Verification, you should clear env variables QSTAS
|
|
|
1375
1387
|
var BaseLazyStep = class _BaseLazyStep {
|
|
1376
1388
|
stepName;
|
|
1377
1389
|
context;
|
|
1378
|
-
|
|
1390
|
+
/**
|
|
1391
|
+
* step-level settings (flow control, retries etc.) which override the
|
|
1392
|
+
* settings the workflow run was triggered with, for this step only.
|
|
1393
|
+
*/
|
|
1394
|
+
stepSettings;
|
|
1395
|
+
/**
|
|
1396
|
+
* whether this step's result can be submitted after the route function
|
|
1397
|
+
* has moved on, instead of right away.
|
|
1398
|
+
*
|
|
1399
|
+
* Enabled for steps whose `getResultStep` produces the final `out` —
|
|
1400
|
+
* which is not the same as "needs no server round trip":
|
|
1401
|
+
* `LazyCreateWebhookStep` builds its result in `getBody` and returns
|
|
1402
|
+
* `out: undefined` from `getResultStep`, so deferring it would hand
|
|
1403
|
+
* `undefined` to the route function.
|
|
1404
|
+
*
|
|
1405
|
+
* Deferring lets the route function continue and reveal the next step,
|
|
1406
|
+
* so that step's settings can ride on this step's submission instead
|
|
1407
|
+
* of needing a step config request of their own.
|
|
1408
|
+
*/
|
|
1409
|
+
supportsDeferredSubmission = false;
|
|
1410
|
+
constructor(context, stepName, stepSettings) {
|
|
1379
1411
|
this.context = context;
|
|
1412
|
+
this.stepSettings = stepSettings;
|
|
1380
1413
|
if (!stepName) {
|
|
1381
1414
|
throw new WorkflowError(
|
|
1382
1415
|
"A workflow step name cannot be undefined or an empty string. Please provide a name for your workflow step."
|
|
@@ -1440,7 +1473,13 @@ var BaseLazyStep = class _BaseLazyStep {
|
|
|
1440
1473
|
step.out = JSON.stringify(step.out);
|
|
1441
1474
|
return JSON.stringify(step);
|
|
1442
1475
|
}
|
|
1443
|
-
getHeaders({
|
|
1476
|
+
getHeaders({
|
|
1477
|
+
context,
|
|
1478
|
+
telemetry: telemetry2,
|
|
1479
|
+
invokeCount,
|
|
1480
|
+
step,
|
|
1481
|
+
stepSettings
|
|
1482
|
+
}) {
|
|
1444
1483
|
return getHeaders({
|
|
1445
1484
|
initHeaderValue: "false",
|
|
1446
1485
|
workflowConfig: {
|
|
@@ -1454,7 +1493,8 @@ var BaseLazyStep = class _BaseLazyStep {
|
|
|
1454
1493
|
stepInfo: {
|
|
1455
1494
|
step,
|
|
1456
1495
|
lazyStep: this
|
|
1457
|
-
}
|
|
1496
|
+
},
|
|
1497
|
+
stepSettings
|
|
1458
1498
|
});
|
|
1459
1499
|
}
|
|
1460
1500
|
async submitStep({ context, body, headers }) {
|
|
@@ -1472,8 +1512,9 @@ var LazyFunctionStep = class extends BaseLazyStep {
|
|
|
1472
1512
|
stepFunction;
|
|
1473
1513
|
stepType = "Run";
|
|
1474
1514
|
allowUndefinedOut = true;
|
|
1475
|
-
|
|
1476
|
-
|
|
1515
|
+
supportsDeferredSubmission = true;
|
|
1516
|
+
constructor(context, stepName, stepFunction, stepSettings) {
|
|
1517
|
+
super(context, stepName, stepSettings);
|
|
1477
1518
|
this.stepFunction = stepFunction;
|
|
1478
1519
|
}
|
|
1479
1520
|
getPlanStep(concurrent, targetStep) {
|
|
@@ -1503,6 +1544,7 @@ var LazySleepStep = class extends BaseLazyStep {
|
|
|
1503
1544
|
sleep;
|
|
1504
1545
|
stepType = "SleepFor";
|
|
1505
1546
|
allowUndefinedOut = true;
|
|
1547
|
+
supportsDeferredSubmission = true;
|
|
1506
1548
|
constructor(context, stepName, sleep) {
|
|
1507
1549
|
super(context, stepName);
|
|
1508
1550
|
this.sleep = sleep;
|
|
@@ -1542,6 +1584,7 @@ var LazySleepUntilStep = class extends BaseLazyStep {
|
|
|
1542
1584
|
sleepUntil;
|
|
1543
1585
|
stepType = "SleepUntil";
|
|
1544
1586
|
allowUndefinedOut = true;
|
|
1587
|
+
supportsDeferredSubmission = true;
|
|
1545
1588
|
constructor(context, stepName, sleepUntil) {
|
|
1546
1589
|
super(context, stepName);
|
|
1547
1590
|
this.sleepUntil = sleepUntil;
|
|
@@ -2043,6 +2086,7 @@ var WorkflowHeaders = class {
|
|
|
2043
2086
|
invokeCount;
|
|
2044
2087
|
initHeaderValue;
|
|
2045
2088
|
stepInfo;
|
|
2089
|
+
stepSettings;
|
|
2046
2090
|
headers;
|
|
2047
2091
|
/**
|
|
2048
2092
|
* @param params workflow header parameters
|
|
@@ -2052,17 +2096,20 @@ var WorkflowHeaders = class {
|
|
|
2052
2096
|
workflowConfig,
|
|
2053
2097
|
invokeCount,
|
|
2054
2098
|
initHeaderValue,
|
|
2055
|
-
stepInfo
|
|
2099
|
+
stepInfo,
|
|
2100
|
+
stepSettings
|
|
2056
2101
|
}) {
|
|
2057
2102
|
this.userHeaders = userHeaders;
|
|
2058
2103
|
this.workflowConfig = workflowConfig;
|
|
2059
2104
|
this.invokeCount = invokeCount;
|
|
2060
2105
|
this.initHeaderValue = initHeaderValue;
|
|
2061
2106
|
this.stepInfo = stepInfo;
|
|
2107
|
+
this.stepSettings = stepSettings;
|
|
2062
2108
|
this.headers = {
|
|
2063
2109
|
rawHeaders: {},
|
|
2064
2110
|
workflowHeaders: {},
|
|
2065
|
-
failureHeaders: {}
|
|
2111
|
+
failureHeaders: {},
|
|
2112
|
+
stepSettingsHeaders: {}
|
|
2066
2113
|
};
|
|
2067
2114
|
}
|
|
2068
2115
|
getHeaders() {
|
|
@@ -2073,6 +2120,7 @@ var WorkflowHeaders = class {
|
|
|
2073
2120
|
this.addUserHeaders();
|
|
2074
2121
|
this.addInvokeCount();
|
|
2075
2122
|
this.addFailureUrl();
|
|
2123
|
+
this.addStepSettings();
|
|
2076
2124
|
const contentType = this.addContentType();
|
|
2077
2125
|
return this.prefixHeaders(contentType);
|
|
2078
2126
|
}
|
|
@@ -2082,7 +2130,7 @@ var WorkflowHeaders = class {
|
|
|
2082
2130
|
[WORKFLOW_INIT_HEADER]: this.initHeaderValue,
|
|
2083
2131
|
[WORKFLOW_ID_HEADER]: this.workflowConfig.workflowRunId,
|
|
2084
2132
|
[WORKFLOW_URL_HEADER]: this.workflowConfig.workflowUrl,
|
|
2085
|
-
[WORKFLOW_FEATURE_HEADER]:
|
|
2133
|
+
[WORKFLOW_FEATURE_HEADER]: WORKFLOW_FEATURE_SET,
|
|
2086
2134
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
2087
2135
|
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
2088
2136
|
};
|
|
@@ -2163,7 +2211,7 @@ var WorkflowHeaders = class {
|
|
|
2163
2211
|
this.headers.failureHeaders["Workflow-Init"] = "false";
|
|
2164
2212
|
this.headers.failureHeaders["Workflow-Url"] = this.workflowConfig.workflowUrl;
|
|
2165
2213
|
this.headers.failureHeaders["Workflow-Calltype"] = "failureCall";
|
|
2166
|
-
this.headers.failureHeaders["Feature-Set"] =
|
|
2214
|
+
this.headers.failureHeaders["Feature-Set"] = WORKFLOW_FEATURE_SET;
|
|
2167
2215
|
if (this.workflowConfig.retries !== void 0 && this.workflowConfig.retries !== DEFAULT_RETRIES) {
|
|
2168
2216
|
this.headers.failureHeaders["Retries"] = this.workflowConfig.retries.toString();
|
|
2169
2217
|
}
|
|
@@ -2171,6 +2219,42 @@ var WorkflowHeaders = class {
|
|
|
2171
2219
|
this.headers.failureHeaders["Retry-Delay"] = this.workflowConfig.retryDelay.toString();
|
|
2172
2220
|
}
|
|
2173
2221
|
}
|
|
2222
|
+
/**
|
|
2223
|
+
* Applies the step-level settings of a step to the message.
|
|
2224
|
+
*
|
|
2225
|
+
* These headers configure the message itself, so that QStash uses them
|
|
2226
|
+
* instead of the settings the run was triggered with. The delivery of
|
|
2227
|
+
* the message is what executes the step, which is how a step's
|
|
2228
|
+
* settings reach it.
|
|
2229
|
+
*
|
|
2230
|
+
* When any setting is present the feature set is extended with
|
|
2231
|
+
* `WF_StepConfig`, which is what tells QStash to keep them. QStash
|
|
2232
|
+
* reports that back on the delivery as `Upstash-Workflow-Step-Config`.
|
|
2233
|
+
*/
|
|
2234
|
+
addStepSettings() {
|
|
2235
|
+
if (!this.stepSettings) {
|
|
2236
|
+
return;
|
|
2237
|
+
}
|
|
2238
|
+
const headers = {};
|
|
2239
|
+
if (this.stepSettings.flowControl) {
|
|
2240
|
+
const { flowControlKey, flowControlValue } = prepareFlowControl(
|
|
2241
|
+
this.stepSettings.flowControl
|
|
2242
|
+
);
|
|
2243
|
+
headers[FLOW_CONTROL_KEY_HEADER] = flowControlKey;
|
|
2244
|
+
headers[FLOW_CONTROL_VALUE_HEADER] = flowControlValue;
|
|
2245
|
+
}
|
|
2246
|
+
if (this.stepSettings.retries !== void 0) {
|
|
2247
|
+
headers[RETRIES_HEADER] = this.stepSettings.retries.toString();
|
|
2248
|
+
}
|
|
2249
|
+
if (this.stepSettings.retryDelay) {
|
|
2250
|
+
headers[RETRY_DELAY_HEADER] = this.stepSettings.retryDelay;
|
|
2251
|
+
}
|
|
2252
|
+
if (Object.keys(headers).length === 0) {
|
|
2253
|
+
return;
|
|
2254
|
+
}
|
|
2255
|
+
headers[WORKFLOW_FEATURE_HEADER] = `${WORKFLOW_FEATURE_SET},${WORKFLOW_STEP_CONFIG_FEATURE}`;
|
|
2256
|
+
this.headers.stepSettingsHeaders = headers;
|
|
2257
|
+
}
|
|
2174
2258
|
addContentType() {
|
|
2175
2259
|
if (this.workflowConfig.useJSONContent) {
|
|
2176
2260
|
this.headers.rawHeaders["content-type"] = "application/json";
|
|
@@ -2184,14 +2268,17 @@ var WorkflowHeaders = class {
|
|
|
2184
2268
|
return contentType;
|
|
2185
2269
|
}
|
|
2186
2270
|
prefixHeaders(contentType) {
|
|
2187
|
-
const { rawHeaders, workflowHeaders, failureHeaders } = this.headers;
|
|
2271
|
+
const { rawHeaders, workflowHeaders, failureHeaders, stepSettingsHeaders } = this.headers;
|
|
2188
2272
|
const isCall = this.stepInfo?.lazyStep.stepType === "Call";
|
|
2189
2273
|
return {
|
|
2190
2274
|
headers: {
|
|
2191
2275
|
...rawHeaders,
|
|
2192
2276
|
...addPrefixToHeaders(workflowHeaders, isCall ? "Upstash-Callback-" : "Upstash-"),
|
|
2193
2277
|
...addPrefixToHeaders(failureHeaders, "Upstash-Failure-Callback-"),
|
|
2194
|
-
...isCall ? addPrefixToHeaders(failureHeaders, "Upstash-Callback-Failure-Callback-") : {}
|
|
2278
|
+
...isCall ? addPrefixToHeaders(failureHeaders, "Upstash-Callback-Failure-Callback-") : {},
|
|
2279
|
+
// last, so they override the run's settings above. Never
|
|
2280
|
+
// prefixed: they configure this message, not its callback.
|
|
2281
|
+
...stepSettingsHeaders
|
|
2195
2282
|
},
|
|
2196
2283
|
contentType
|
|
2197
2284
|
};
|
|
@@ -2242,7 +2329,7 @@ var submitParallelSteps = async ({
|
|
|
2242
2329
|
info: `Submitting ${planSteps.length} parallel steps.`
|
|
2243
2330
|
});
|
|
2244
2331
|
const result = await context.qstashClient.batch(
|
|
2245
|
-
planSteps.map((planStep) => {
|
|
2332
|
+
planSteps.map((planStep, index) => {
|
|
2246
2333
|
const { headers } = getHeaders({
|
|
2247
2334
|
initHeaderValue: "false",
|
|
2248
2335
|
workflowConfig: {
|
|
@@ -2251,7 +2338,8 @@ var submitParallelSteps = async ({
|
|
|
2251
2338
|
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
2252
2339
|
telemetry: telemetry2
|
|
2253
2340
|
},
|
|
2254
|
-
invokeCount
|
|
2341
|
+
invokeCount,
|
|
2342
|
+
stepSettings: steps[index].stepSettings
|
|
2255
2343
|
});
|
|
2256
2344
|
return {
|
|
2257
2345
|
headers,
|
|
@@ -2270,31 +2358,38 @@ var submitParallelSteps = async ({
|
|
|
2270
2358
|
}
|
|
2271
2359
|
throw new WorkflowAbort(planSteps[0].stepName, planSteps[0]);
|
|
2272
2360
|
};
|
|
2273
|
-
var
|
|
2274
|
-
context,
|
|
2361
|
+
var executeStep = async ({
|
|
2275
2362
|
lazyStep,
|
|
2276
2363
|
stepId,
|
|
2277
|
-
invokeCount,
|
|
2278
2364
|
concurrency,
|
|
2279
|
-
telemetry: telemetry2,
|
|
2280
|
-
dispatchDebug,
|
|
2281
2365
|
dispatchLifecycle
|
|
2282
2366
|
}) => {
|
|
2283
2367
|
await dispatchLifecycle("beforeExecution", {
|
|
2284
2368
|
stepName: lazyStep.stepName
|
|
2285
2369
|
});
|
|
2286
|
-
let resultStep;
|
|
2287
2370
|
try {
|
|
2288
|
-
|
|
2371
|
+
return await lazyStep.getResultStep(concurrency, stepId);
|
|
2289
2372
|
} catch (error) {
|
|
2290
2373
|
attachStepNameToError(error, lazyStep.stepName);
|
|
2291
2374
|
throw error;
|
|
2292
2375
|
}
|
|
2376
|
+
};
|
|
2377
|
+
var submitStepResult = async ({
|
|
2378
|
+
context,
|
|
2379
|
+
lazyStep,
|
|
2380
|
+
resultStep,
|
|
2381
|
+
invokeCount,
|
|
2382
|
+
concurrency,
|
|
2383
|
+
telemetry: telemetry2,
|
|
2384
|
+
dispatchDebug,
|
|
2385
|
+
nextStepSettings
|
|
2386
|
+
}) => {
|
|
2293
2387
|
const { headers } = lazyStep.getHeaders({
|
|
2294
2388
|
context,
|
|
2295
2389
|
step: resultStep,
|
|
2296
2390
|
invokeCount,
|
|
2297
|
-
telemetry: telemetry2
|
|
2391
|
+
telemetry: telemetry2,
|
|
2392
|
+
stepSettings: nextStepSettings
|
|
2298
2393
|
});
|
|
2299
2394
|
const body = lazyStep.getBody({
|
|
2300
2395
|
context,
|
|
@@ -2319,6 +2414,154 @@ var submitSingleStep = async ({
|
|
|
2319
2414
|
}
|
|
2320
2415
|
return resultStep;
|
|
2321
2416
|
};
|
|
2417
|
+
var publishStepConfigRequest = async ({
|
|
2418
|
+
context,
|
|
2419
|
+
lazyStep,
|
|
2420
|
+
targetStep,
|
|
2421
|
+
invokeCount,
|
|
2422
|
+
telemetry: telemetry2,
|
|
2423
|
+
dispatchDebug
|
|
2424
|
+
}) => {
|
|
2425
|
+
const { headers } = getHeaders({
|
|
2426
|
+
initHeaderValue: "false",
|
|
2427
|
+
workflowConfig: {
|
|
2428
|
+
workflowRunId: context.workflowRunId,
|
|
2429
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
2430
|
+
workflowUrl: context.url,
|
|
2431
|
+
telemetry: telemetry2
|
|
2432
|
+
},
|
|
2433
|
+
invokeCount,
|
|
2434
|
+
stepSettings: lazyStep.stepSettings
|
|
2435
|
+
});
|
|
2436
|
+
await dispatchDebug("onInfo", {
|
|
2437
|
+
info: `Step "${lazyStep.stepName}" (${targetStep}) has step-level settings which the current delivery does not carry. Requesting a delivery which does.`
|
|
2438
|
+
});
|
|
2439
|
+
const result = await context.qstashClient.publishJSON({
|
|
2440
|
+
headers: {
|
|
2441
|
+
...headers,
|
|
2442
|
+
"Upstash-Workflow-CallType": WORKFLOW_STEP_CONFIG_CALL_TYPE,
|
|
2443
|
+
"Upstash-Forward-Upstash-Workflow-StepId": targetStep.toString(),
|
|
2444
|
+
"Upstash-Forward-Upstash-Workflow-StepName": lazyStep.stepName
|
|
2445
|
+
},
|
|
2446
|
+
method: "POST",
|
|
2447
|
+
body: { targetStep, invokeCount },
|
|
2448
|
+
url: context.url,
|
|
2449
|
+
contentBasedDeduplication: true
|
|
2450
|
+
});
|
|
2451
|
+
if (result?.deduplicated) {
|
|
2452
|
+
await dispatchDebug("onWarning", {
|
|
2453
|
+
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.`
|
|
2454
|
+
});
|
|
2455
|
+
} else if (result?.messageId) {
|
|
2456
|
+
await dispatchDebug("onInfo", {
|
|
2457
|
+
info: `Requested a delivery under the settings of step "${lazyStep.stepName}" with messageId: ${result.messageId}.`
|
|
2458
|
+
});
|
|
2459
|
+
}
|
|
2460
|
+
};
|
|
2461
|
+
|
|
2462
|
+
// src/qstash/step-config.ts
|
|
2463
|
+
var normalizeStepSettings = (settings) => {
|
|
2464
|
+
if (settings === void 0) {
|
|
2465
|
+
return void 0;
|
|
2466
|
+
}
|
|
2467
|
+
const { retries, retryDelay } = settings;
|
|
2468
|
+
if (retryDelay !== void 0 && typeof retryDelay !== "string") {
|
|
2469
|
+
throw new WorkflowNonRetryableError("Invalid step retryDelay: must be a string.");
|
|
2470
|
+
}
|
|
2471
|
+
if (retries !== void 0 && (!Number.isInteger(retries) || retries < 0)) {
|
|
2472
|
+
throw new WorkflowNonRetryableError("Invalid step retries: must be a non-negative integer.");
|
|
2473
|
+
}
|
|
2474
|
+
return { ...settings, retryDelay: retryDelay?.trim() || void 0 };
|
|
2475
|
+
};
|
|
2476
|
+
var DEFAULT_PARALLELISM = 0;
|
|
2477
|
+
var DEFAULT_RATE = 0;
|
|
2478
|
+
var DEFAULT_PERIOD_SECONDS = 1;
|
|
2479
|
+
var SECONDS_PER_UNIT = {
|
|
2480
|
+
s: 1,
|
|
2481
|
+
m: 60,
|
|
2482
|
+
h: 60 * 60,
|
|
2483
|
+
d: 24 * 60 * 60
|
|
2484
|
+
};
|
|
2485
|
+
var durationToSeconds = (duration) => {
|
|
2486
|
+
if (typeof duration === "number") {
|
|
2487
|
+
return Number.isFinite(duration) ? duration : void 0;
|
|
2488
|
+
}
|
|
2489
|
+
const match = /^(\d+)([smhd])$/.exec(duration.trim());
|
|
2490
|
+
if (match) {
|
|
2491
|
+
return Number(match[1]) * SECONDS_PER_UNIT[match[2]];
|
|
2492
|
+
}
|
|
2493
|
+
const seconds = Number(duration.trim());
|
|
2494
|
+
return Number.isFinite(seconds) ? seconds : void 0;
|
|
2495
|
+
};
|
|
2496
|
+
var normalizeFlowControl = (flowControl) => {
|
|
2497
|
+
const period = flowControl.period === void 0 ? void 0 : durationToSeconds(flowControl.period);
|
|
2498
|
+
return {
|
|
2499
|
+
key: flowControl.key,
|
|
2500
|
+
parallelism: flowControl.parallelism ?? DEFAULT_PARALLELISM,
|
|
2501
|
+
rate: flowControl.rate ?? flowControl.ratePerSecond ?? DEFAULT_RATE,
|
|
2502
|
+
period: period ?? DEFAULT_PERIOD_SECONDS
|
|
2503
|
+
};
|
|
2504
|
+
};
|
|
2505
|
+
var parseFlowControlHeaders = (key, value) => {
|
|
2506
|
+
if (!key) {
|
|
2507
|
+
return void 0;
|
|
2508
|
+
}
|
|
2509
|
+
const flowControl = {
|
|
2510
|
+
key,
|
|
2511
|
+
parallelism: DEFAULT_PARALLELISM,
|
|
2512
|
+
rate: DEFAULT_RATE,
|
|
2513
|
+
period: DEFAULT_PERIOD_SECONDS
|
|
2514
|
+
};
|
|
2515
|
+
for (const entry of (value ?? "").split(",")) {
|
|
2516
|
+
const [name, rawValue] = entry.split("=").map((part) => part.trim());
|
|
2517
|
+
if (!name || rawValue === void 0) {
|
|
2518
|
+
continue;
|
|
2519
|
+
}
|
|
2520
|
+
switch (name) {
|
|
2521
|
+
case "parallelism": {
|
|
2522
|
+
flowControl.parallelism = Number(rawValue);
|
|
2523
|
+
break;
|
|
2524
|
+
}
|
|
2525
|
+
case "rate": {
|
|
2526
|
+
flowControl.rate = Number(rawValue);
|
|
2527
|
+
break;
|
|
2528
|
+
}
|
|
2529
|
+
case "period": {
|
|
2530
|
+
flowControl.period = durationToSeconds(rawValue) ?? DEFAULT_PERIOD_SECONDS;
|
|
2531
|
+
break;
|
|
2532
|
+
}
|
|
2533
|
+
}
|
|
2534
|
+
}
|
|
2535
|
+
return flowControl;
|
|
2536
|
+
};
|
|
2537
|
+
var getEffectiveConfig = (headers) => {
|
|
2538
|
+
const retriesHeader = headers.get(RETRIES_HEADER);
|
|
2539
|
+
return {
|
|
2540
|
+
flowControl: parseFlowControlHeaders(
|
|
2541
|
+
headers.get(FLOW_CONTROL_KEY_HEADER),
|
|
2542
|
+
headers.get(FLOW_CONTROL_VALUE_HEADER)
|
|
2543
|
+
),
|
|
2544
|
+
retries: retriesHeader === null ? void 0 : Number(retriesHeader),
|
|
2545
|
+
retryDelay: headers.get(RETRY_DELAY_HEADER) ?? void 0,
|
|
2546
|
+
hasStepConfig: headers.get(WORKFLOW_STEP_CONFIG_HEADER) === "true"
|
|
2547
|
+
};
|
|
2548
|
+
};
|
|
2549
|
+
var describeStepSettingsMismatch = (stepSettings, effectiveConfig) => {
|
|
2550
|
+
if (stepSettings.flowControl) {
|
|
2551
|
+
const wanted = normalizeFlowControl(stepSettings.flowControl);
|
|
2552
|
+
const applied = effectiveConfig.flowControl;
|
|
2553
|
+
if (!applied || applied.key !== wanted.key || applied.parallelism !== wanted.parallelism || applied.rate !== wanted.rate || applied.period !== wanted.period) {
|
|
2554
|
+
return `flow control: expected ${JSON.stringify(wanted)}, delivery has ${JSON.stringify(applied)}`;
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
if (stepSettings.retries !== void 0 && effectiveConfig.retries !== void 0 && stepSettings.retries !== effectiveConfig.retries) {
|
|
2558
|
+
return `retries: expected ${stepSettings.retries}, delivery has ${effectiveConfig.retries}`;
|
|
2559
|
+
}
|
|
2560
|
+
if (stepSettings.retryDelay !== void 0 && stepSettings.retryDelay !== effectiveConfig.retryDelay) {
|
|
2561
|
+
return `retry delay: expected '${stepSettings.retryDelay}', delivery has '${effectiveConfig.retryDelay ?? ""}'`;
|
|
2562
|
+
}
|
|
2563
|
+
return void 0;
|
|
2564
|
+
};
|
|
2322
2565
|
|
|
2323
2566
|
// src/context/auto-executor.ts
|
|
2324
2567
|
var AutoExecutor = class _AutoExecutor {
|
|
@@ -2335,6 +2578,25 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2335
2578
|
stepCount = 0;
|
|
2336
2579
|
planStepCount = 0;
|
|
2337
2580
|
executingStep = false;
|
|
2581
|
+
/**
|
|
2582
|
+
* an executed step whose result hasn't been submitted to QStash yet.
|
|
2583
|
+
*
|
|
2584
|
+
* Set when a step which can produce its result in-process executes. The
|
|
2585
|
+
* submission waits so that the route function can continue and reveal
|
|
2586
|
+
* what comes next: if that is a single step with step-level settings,
|
|
2587
|
+
* the settings ride on this submission and no step config request is
|
|
2588
|
+
* needed, since the delivery of the submission is what executes it.
|
|
2589
|
+
*
|
|
2590
|
+
* Flushed when the next step is reached (see `addStep`) or when the
|
|
2591
|
+
* route function ends (see `flushPendingStep` in `serve`).
|
|
2592
|
+
*/
|
|
2593
|
+
pendingStep;
|
|
2594
|
+
/**
|
|
2595
|
+
* configuration QStash applied to the delivery being handled. A step
|
|
2596
|
+
* with step-level settings is settled by comparing its settings against
|
|
2597
|
+
* this.
|
|
2598
|
+
*/
|
|
2599
|
+
effectiveConfig;
|
|
2338
2600
|
/**
|
|
2339
2601
|
* @param context workflow context
|
|
2340
2602
|
* @param steps list of steps
|
|
@@ -2342,14 +2604,17 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2342
2604
|
* @param dispatchLifecycle lifecycle event dispatcher
|
|
2343
2605
|
* @param telemetry optional telemetry information
|
|
2344
2606
|
* @param invokeCount optional invoke count
|
|
2607
|
+
* @param effectiveConfig configuration QStash applied to the delivery
|
|
2608
|
+
* being handled
|
|
2345
2609
|
*/
|
|
2346
|
-
constructor(context, steps, dispatchDebug, dispatchLifecycle, telemetry2, invokeCount) {
|
|
2610
|
+
constructor(context, steps, dispatchDebug, dispatchLifecycle, telemetry2, invokeCount, effectiveConfig) {
|
|
2347
2611
|
this.context = context;
|
|
2348
2612
|
this.steps = steps;
|
|
2349
2613
|
this.dispatchDebug = dispatchDebug;
|
|
2350
2614
|
this.dispatchLifecycle = dispatchLifecycle;
|
|
2351
2615
|
this.telemetry = telemetry2;
|
|
2352
2616
|
this.invokeCount = invokeCount ?? 0;
|
|
2617
|
+
this.effectiveConfig = effectiveConfig ?? { hasStepConfig: false };
|
|
2353
2618
|
this.nonPlanStepCount = this.steps.filter((step) => !step.targetStep).length;
|
|
2354
2619
|
}
|
|
2355
2620
|
/**
|
|
@@ -2384,6 +2649,15 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2384
2649
|
lazyStepList.push(stepInfo);
|
|
2385
2650
|
const index = this.indexInCurrentList++;
|
|
2386
2651
|
const requestComplete = this.deferExecution().then(async () => {
|
|
2652
|
+
const submitted = await this.submitPendingStep(
|
|
2653
|
+
lazyStepList.length === 1 ? lazyStepList[0].stepSettings : void 0
|
|
2654
|
+
);
|
|
2655
|
+
if (submitted.isErr()) {
|
|
2656
|
+
throw submitted.error;
|
|
2657
|
+
}
|
|
2658
|
+
if (submitted.value.result === "submitted-step") {
|
|
2659
|
+
throw submitted.value.abort;
|
|
2660
|
+
}
|
|
2387
2661
|
if (!this.promises.has(lazyStepList)) {
|
|
2388
2662
|
const promise2 = this.getExecutionPromise(lazyStepList);
|
|
2389
2663
|
this.promises.set(lazyStepList, promise2);
|
|
@@ -2437,18 +2711,108 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2437
2711
|
}
|
|
2438
2712
|
return parsedOut;
|
|
2439
2713
|
}
|
|
2440
|
-
const
|
|
2441
|
-
|
|
2714
|
+
const mismatch = lazyStep.stepSettings ? describeStepSettingsMismatch(lazyStep.stepSettings, this.effectiveConfig) : void 0;
|
|
2715
|
+
if (mismatch) {
|
|
2716
|
+
if (!this.effectiveConfig.hasStepConfig) {
|
|
2717
|
+
await publishStepConfigRequest({
|
|
2718
|
+
context: this.context,
|
|
2719
|
+
lazyStep,
|
|
2720
|
+
targetStep: this.stepCount,
|
|
2721
|
+
invokeCount: this.invokeCount,
|
|
2722
|
+
telemetry: this.telemetry,
|
|
2723
|
+
dispatchDebug: this.dispatchDebug
|
|
2724
|
+
});
|
|
2725
|
+
throw new WorkflowAbort(lazyStep.stepName);
|
|
2726
|
+
} else {
|
|
2727
|
+
await this.dispatchDebug("onWarning", {
|
|
2728
|
+
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.`
|
|
2729
|
+
});
|
|
2730
|
+
}
|
|
2731
|
+
}
|
|
2732
|
+
const resultStep = await executeStep({
|
|
2442
2733
|
lazyStep,
|
|
2443
2734
|
stepId: this.stepCount,
|
|
2735
|
+
concurrency: NO_CONCURRENCY,
|
|
2736
|
+
dispatchLifecycle: this.dispatchLifecycle
|
|
2737
|
+
});
|
|
2738
|
+
if (lazyStep.supportsDeferredSubmission) {
|
|
2739
|
+
const serializedOut = JSON.stringify(resultStep.out);
|
|
2740
|
+
this.pendingStep = {
|
|
2741
|
+
status: "held",
|
|
2742
|
+
lazyStep,
|
|
2743
|
+
resultStep: {
|
|
2744
|
+
...resultStep,
|
|
2745
|
+
out: serializedOut === void 0 ? void 0 : JSON.parse(serializedOut)
|
|
2746
|
+
}
|
|
2747
|
+
};
|
|
2748
|
+
return lazyStep.parseOut({
|
|
2749
|
+
...resultStep,
|
|
2750
|
+
out: serializedOut
|
|
2751
|
+
});
|
|
2752
|
+
}
|
|
2753
|
+
await submitStepResult({
|
|
2754
|
+
context: this.context,
|
|
2755
|
+
lazyStep,
|
|
2756
|
+
resultStep,
|
|
2444
2757
|
invokeCount: this.invokeCount,
|
|
2445
|
-
concurrency:
|
|
2758
|
+
concurrency: NO_CONCURRENCY,
|
|
2446
2759
|
telemetry: this.telemetry,
|
|
2447
|
-
dispatchDebug: this.dispatchDebug
|
|
2448
|
-
dispatchLifecycle: this.dispatchLifecycle
|
|
2760
|
+
dispatchDebug: this.dispatchDebug
|
|
2449
2761
|
});
|
|
2450
2762
|
throw new WorkflowAbort(lazyStep.stepName, resultStep);
|
|
2451
2763
|
}
|
|
2764
|
+
/**
|
|
2765
|
+
* Submits the result of the step being held, if there is one, and
|
|
2766
|
+
* returns the abort the caller has to throw to end the invocation.
|
|
2767
|
+
*
|
|
2768
|
+
* Returns undefined when no step is being held, which is the caller's
|
|
2769
|
+
* signal that there is nothing to end. Throws instead of returning
|
|
2770
|
+
* when the submission itself fails, so that error reaches the caller
|
|
2771
|
+
* rather than an abort claiming the step was submitted.
|
|
2772
|
+
*
|
|
2773
|
+
* Callers which arrive while a submission is already under way (the
|
|
2774
|
+
* steps of a parallel group, added together) wait for that one and get
|
|
2775
|
+
* the same abort, rather than submitting a second time.
|
|
2776
|
+
*
|
|
2777
|
+
* The next step's settings are attached whenever it has any, without
|
|
2778
|
+
* checking them against the current delivery: the executor only knows
|
|
2779
|
+
* the configuration of the delivery in hand, which inside a
|
|
2780
|
+
* delivery is the *previous* step's, so such a check would be wrong
|
|
2781
|
+
* exactly when two steps with settings follow each other.
|
|
2782
|
+
*
|
|
2783
|
+
* @param nextStepSettings step-level settings of the next step
|
|
2784
|
+
* @returns `submitted-step` with the abort which ends this invocation,
|
|
2785
|
+
* or `no-pending-step` when nothing was held and there is nothing to
|
|
2786
|
+
* end
|
|
2787
|
+
*/
|
|
2788
|
+
async submitPendingStep(nextStepSettings) {
|
|
2789
|
+
if (!this.pendingStep) {
|
|
2790
|
+
return ok({ result: "no-pending-step" });
|
|
2791
|
+
}
|
|
2792
|
+
try {
|
|
2793
|
+
if (this.pendingStep.status === "submitting") {
|
|
2794
|
+
return ok({ result: "submitted-step", abort: await this.pendingStep.submitted });
|
|
2795
|
+
}
|
|
2796
|
+
const { lazyStep, resultStep } = this.pendingStep;
|
|
2797
|
+
const submitted = (async () => {
|
|
2798
|
+
await submitStepResult({
|
|
2799
|
+
context: this.context,
|
|
2800
|
+
lazyStep,
|
|
2801
|
+
resultStep,
|
|
2802
|
+
invokeCount: this.invokeCount,
|
|
2803
|
+
concurrency: NO_CONCURRENCY,
|
|
2804
|
+
telemetry: this.telemetry,
|
|
2805
|
+
dispatchDebug: this.dispatchDebug,
|
|
2806
|
+
nextStepSettings
|
|
2807
|
+
});
|
|
2808
|
+
return new WorkflowAbort(lazyStep.stepName, resultStep);
|
|
2809
|
+
})();
|
|
2810
|
+
this.pendingStep = { status: "submitting", submitted };
|
|
2811
|
+
return ok({ result: "submitted-step", abort: await submitted });
|
|
2812
|
+
} catch (error) {
|
|
2813
|
+
return err(error);
|
|
2814
|
+
}
|
|
2815
|
+
}
|
|
2452
2816
|
/**
|
|
2453
2817
|
* Runs steps in parallel.
|
|
2454
2818
|
*
|
|
@@ -2497,15 +2861,20 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2497
2861
|
validateStep(parallelSteps[stepIndex], planStep);
|
|
2498
2862
|
try {
|
|
2499
2863
|
const parallelStep = parallelSteps[stepIndex];
|
|
2500
|
-
const resultStep = await
|
|
2501
|
-
context: this.context,
|
|
2864
|
+
const resultStep = await executeStep({
|
|
2502
2865
|
lazyStep: parallelStep,
|
|
2503
2866
|
stepId: planStep.targetStep,
|
|
2867
|
+
concurrency: parallelSteps.length,
|
|
2868
|
+
dispatchLifecycle: this.dispatchLifecycle
|
|
2869
|
+
});
|
|
2870
|
+
await submitStepResult({
|
|
2871
|
+
context: this.context,
|
|
2872
|
+
lazyStep: parallelStep,
|
|
2873
|
+
resultStep,
|
|
2504
2874
|
invokeCount: this.invokeCount,
|
|
2505
2875
|
concurrency: parallelSteps.length,
|
|
2506
2876
|
telemetry: this.telemetry,
|
|
2507
|
-
dispatchDebug: this.dispatchDebug
|
|
2508
|
-
dispatchLifecycle: this.dispatchLifecycle
|
|
2877
|
+
dispatchDebug: this.dispatchDebug
|
|
2509
2878
|
});
|
|
2510
2879
|
throw new WorkflowAbort(parallelStep.stepName, resultStep);
|
|
2511
2880
|
} catch (error) {
|
|
@@ -2723,6 +3092,11 @@ var BaseWorkflowApi = class {
|
|
|
2723
3092
|
|
|
2724
3093
|
// src/context/api/anthropic.ts
|
|
2725
3094
|
var AnthropicAPI = class extends BaseWorkflowApi {
|
|
3095
|
+
/**
|
|
3096
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
3097
|
+
*
|
|
3098
|
+
* @see node_modules/@upstash/workflow/docs/integrations/anthropic.mdx
|
|
3099
|
+
*/
|
|
2726
3100
|
async call(stepName, settings) {
|
|
2727
3101
|
const { token, operation, ...parameters } = settings;
|
|
2728
3102
|
return await this.callApi(stepName, {
|
|
@@ -2738,6 +3112,11 @@ var AnthropicAPI = class extends BaseWorkflowApi {
|
|
|
2738
3112
|
// src/context/api/openai.ts
|
|
2739
3113
|
var import_qstash7 = require("@upstash/qstash");
|
|
2740
3114
|
var OpenAIAPI = class extends BaseWorkflowApi {
|
|
3115
|
+
/**
|
|
3116
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
3117
|
+
*
|
|
3118
|
+
* @see node_modules/@upstash/workflow/docs/integrations/openai.mdx
|
|
3119
|
+
*/
|
|
2741
3120
|
async call(stepName, settings) {
|
|
2742
3121
|
const { token, organization, operation, baseURL, ...parameters } = settings;
|
|
2743
3122
|
const useOpenAI = baseURL === void 0;
|
|
@@ -2755,6 +3134,11 @@ var OpenAIAPI = class extends BaseWorkflowApi {
|
|
|
2755
3134
|
// src/context/api/resend.ts
|
|
2756
3135
|
var import_qstash8 = require("@upstash/qstash");
|
|
2757
3136
|
var ResendAPI = class extends BaseWorkflowApi {
|
|
3137
|
+
/**
|
|
3138
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
3139
|
+
*
|
|
3140
|
+
* @see node_modules/@upstash/workflow/docs/integrations/resend.mdx
|
|
3141
|
+
*/
|
|
2758
3142
|
async call(stepName, settings) {
|
|
2759
3143
|
const { token, batch = false, ...parameters } = settings;
|
|
2760
3144
|
return await this.callApi(stepName, {
|
|
@@ -2996,8 +3380,9 @@ var WorkflowContext = class {
|
|
|
2996
3380
|
*
|
|
2997
3381
|
* ```ts
|
|
2998
3382
|
* import { Client } from "@upstash/qstash"
|
|
3383
|
+
* import { serve } from "@upstash/workflow/nextjs"
|
|
2999
3384
|
*
|
|
3000
|
-
* export const POST = serve(
|
|
3385
|
+
* export const { POST } = serve(
|
|
3001
3386
|
* async (context) => {
|
|
3002
3387
|
* ...
|
|
3003
3388
|
* },
|
|
@@ -3022,7 +3407,7 @@ var WorkflowContext = class {
|
|
|
3022
3407
|
* Can be overwritten by passing a `url` parameter in `serve`:
|
|
3023
3408
|
*
|
|
3024
3409
|
* ```ts
|
|
3025
|
-
* export const POST = serve(
|
|
3410
|
+
* export const { POST } = serve(
|
|
3026
3411
|
* async (context) => {
|
|
3027
3412
|
* ...
|
|
3028
3413
|
* },
|
|
@@ -3040,7 +3425,7 @@ var WorkflowContext = class {
|
|
|
3040
3425
|
*
|
|
3041
3426
|
* ```ts
|
|
3042
3427
|
* // set requestPayload type to MyPayload:
|
|
3043
|
-
* export const POST = serve<MyPayload>(
|
|
3428
|
+
* export const { POST } = serve<MyPayload>(
|
|
3044
3429
|
* async (context) => {
|
|
3045
3430
|
* ...
|
|
3046
3431
|
* }
|
|
@@ -3052,7 +3437,7 @@ var WorkflowContext = class {
|
|
|
3052
3437
|
* the `initialPayloadParser` parameter:
|
|
3053
3438
|
*
|
|
3054
3439
|
* ```ts
|
|
3055
|
-
* export const POST = serve<MyPayload>(
|
|
3440
|
+
* export const { POST } = serve<MyPayload>(
|
|
3056
3441
|
* async (context) => {
|
|
3057
3442
|
* ...
|
|
3058
3443
|
* },
|
|
@@ -3061,6 +3446,13 @@ var WorkflowContext = class {
|
|
|
3061
3446
|
* }
|
|
3062
3447
|
* )
|
|
3063
3448
|
* ```
|
|
3449
|
+
*
|
|
3450
|
+
* When a `context.call` response comes back, the route runs up to its first step with this
|
|
3451
|
+
* set to the call-result envelope (and `workflowRunId` set to a random id) before the SDK
|
|
3452
|
+
* recognizes the request. Code before the first step must not dereference, validate or
|
|
3453
|
+
* return early on the payload; read it inside `context.run`.
|
|
3454
|
+
*
|
|
3455
|
+
* @see node_modules/@upstash/workflow/docs/basics/serve/advanced.mdx
|
|
3064
3456
|
*/
|
|
3065
3457
|
requestPayload;
|
|
3066
3458
|
/**
|
|
@@ -3073,7 +3465,7 @@ var WorkflowContext = class {
|
|
|
3073
3465
|
* Can be set using the `env` option of serve:
|
|
3074
3466
|
*
|
|
3075
3467
|
* ```ts
|
|
3076
|
-
* export const POST = serve<MyPayload>(
|
|
3468
|
+
* export const { POST } = serve<MyPayload>(
|
|
3077
3469
|
* async (context) => {
|
|
3078
3470
|
* const key = context.env["API_KEY"];
|
|
3079
3471
|
* },
|
|
@@ -3121,6 +3513,46 @@ var WorkflowContext = class {
|
|
|
3121
3513
|
* `1` on the first retry, `2` on the second, and so on.
|
|
3122
3514
|
*/
|
|
3123
3515
|
retried;
|
|
3516
|
+
/**
|
|
3517
|
+
* Configuration QStash applied to the request being handled.
|
|
3518
|
+
*
|
|
3519
|
+
* This is the configuration of *this delivery*, not of the run: it is
|
|
3520
|
+
* the configuration the run was triggered with, except inside the
|
|
3521
|
+
* delivery which executes a step that has step-level settings, where
|
|
3522
|
+
* it is that step's settings. The fields below expose it; the executor
|
|
3523
|
+
* gets it directly rather than reaching through the context.
|
|
3524
|
+
*/
|
|
3525
|
+
effectiveConfig;
|
|
3526
|
+
/**
|
|
3527
|
+
* Flow control QStash applied to the request being handled, if any.
|
|
3528
|
+
*
|
|
3529
|
+
* This is the flow control of the request in hand, which is the one the
|
|
3530
|
+
* run was triggered with except inside a step carrying its own.
|
|
3531
|
+
*/
|
|
3532
|
+
get flowControl() {
|
|
3533
|
+
return this.effectiveConfig.flowControl;
|
|
3534
|
+
}
|
|
3535
|
+
/**
|
|
3536
|
+
* Retry limit QStash applied to the request being handled, if it
|
|
3537
|
+
* reported one.
|
|
3538
|
+
*
|
|
3539
|
+
* Not to be confused with {@link retried}, which is how many retries
|
|
3540
|
+
* have already happened.
|
|
3541
|
+
*
|
|
3542
|
+
* @see {@link flowControl} for what "the request being handled" means
|
|
3543
|
+
*/
|
|
3544
|
+
get retries() {
|
|
3545
|
+
return this.effectiveConfig.retries;
|
|
3546
|
+
}
|
|
3547
|
+
/**
|
|
3548
|
+
* Retry delay expression QStash applied to the request being handled,
|
|
3549
|
+
* if any.
|
|
3550
|
+
*
|
|
3551
|
+
* @see {@link flowControl} for what "the request being handled" means
|
|
3552
|
+
*/
|
|
3553
|
+
get retryDelay() {
|
|
3554
|
+
return this.effectiveConfig.retryDelay;
|
|
3555
|
+
}
|
|
3124
3556
|
constructor({
|
|
3125
3557
|
qstashClient,
|
|
3126
3558
|
workflowRunId,
|
|
@@ -3134,7 +3566,8 @@ var WorkflowContext = class {
|
|
|
3134
3566
|
invokeCount,
|
|
3135
3567
|
label,
|
|
3136
3568
|
retried,
|
|
3137
|
-
middlewareManager
|
|
3569
|
+
middlewareManager,
|
|
3570
|
+
effectiveConfig
|
|
3138
3571
|
}) {
|
|
3139
3572
|
this.qstashClient = qstashClient;
|
|
3140
3573
|
this.workflowRunId = workflowRunId;
|
|
@@ -3146,6 +3579,7 @@ var WorkflowContext = class {
|
|
|
3146
3579
|
this.env = env ?? {};
|
|
3147
3580
|
this.labels = label === void 0 ? [] : Array.isArray(label) ? label : label ? label.split(",") : [];
|
|
3148
3581
|
this.retried = retried ?? 0;
|
|
3582
|
+
this.effectiveConfig = effectiveConfig ?? { hasStepConfig: false };
|
|
3149
3583
|
const middlewareManagerInstance = middlewareManager ?? new MiddlewareManager([]);
|
|
3150
3584
|
middlewareManagerInstance.assignContext(this);
|
|
3151
3585
|
this.executor = new AutoExecutor(
|
|
@@ -3154,7 +3588,8 @@ var WorkflowContext = class {
|
|
|
3154
3588
|
middlewareManagerInstance.dispatchDebug.bind(middlewareManagerInstance),
|
|
3155
3589
|
middlewareManagerInstance.dispatchLifecycle.bind(middlewareManagerInstance),
|
|
3156
3590
|
telemetry2,
|
|
3157
|
-
invokeCount
|
|
3591
|
+
invokeCount,
|
|
3592
|
+
this.effectiveConfig
|
|
3158
3593
|
);
|
|
3159
3594
|
}
|
|
3160
3595
|
/**
|
|
@@ -3180,13 +3615,41 @@ var WorkflowContext = class {
|
|
|
3180
3615
|
* ])
|
|
3181
3616
|
* ```
|
|
3182
3617
|
*
|
|
3618
|
+
* Step-level settings can be passed as a third argument, overriding
|
|
3619
|
+
* the settings the workflow run was triggered with for this step only:
|
|
3620
|
+
*
|
|
3621
|
+
* ```typescript
|
|
3622
|
+
* const result = await context.run(
|
|
3623
|
+
* "step 1",
|
|
3624
|
+
* () => {
|
|
3625
|
+
* return "result"
|
|
3626
|
+
* },
|
|
3627
|
+
* {
|
|
3628
|
+
* flowControl: { key: "custom-key", parallelism: 3 },
|
|
3629
|
+
* retries: 5,
|
|
3630
|
+
* }
|
|
3631
|
+
* )
|
|
3632
|
+
* ```
|
|
3633
|
+
*
|
|
3634
|
+
* Do not wrap steps in try/catch or `.catch` without rethrowing `WorkflowAbort`: each step
|
|
3635
|
+
* throws it to end the request. To stop retrying a failed step, throw
|
|
3636
|
+
* `WorkflowNonRetryableError`.
|
|
3637
|
+
*
|
|
3183
3638
|
* @param stepName name of the step
|
|
3184
3639
|
* @param stepFunction step function to be executed
|
|
3640
|
+
* @param stepSettings step-level settings for this step
|
|
3185
3641
|
* @returns result of the step function
|
|
3642
|
+
* @see node_modules/@upstash/workflow/docs/steps/run.mdx
|
|
3186
3643
|
*/
|
|
3187
|
-
async run(stepName, stepFunction) {
|
|
3644
|
+
async run(stepName, stepFunction, stepSettings) {
|
|
3645
|
+
const normalizedSettings = normalizeStepSettings(stepSettings);
|
|
3646
|
+
if (stepSettings) {
|
|
3647
|
+
validateFlowControl(stepSettings.flowControl);
|
|
3648
|
+
}
|
|
3188
3649
|
const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
|
|
3189
|
-
return await this.addStep(
|
|
3650
|
+
return await this.addStep(
|
|
3651
|
+
new LazyFunctionStep(this, stepName, wrappedStepFunction, normalizedSettings)
|
|
3652
|
+
);
|
|
3190
3653
|
}
|
|
3191
3654
|
/**
|
|
3192
3655
|
* Stops the execution for the duration provided.
|
|
@@ -3196,8 +3659,9 @@ var WorkflowContext = class {
|
|
|
3196
3659
|
* ```
|
|
3197
3660
|
*
|
|
3198
3661
|
* @param stepName
|
|
3199
|
-
* @param duration sleep duration in seconds
|
|
3662
|
+
* @param duration sleep duration in seconds, or a duration string such as "10m"
|
|
3200
3663
|
* @returns undefined
|
|
3664
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleep.mdx
|
|
3201
3665
|
*/
|
|
3202
3666
|
async sleep(stepName, duration) {
|
|
3203
3667
|
await this.addStep(new LazySleepStep(this, stepName, duration));
|
|
@@ -3211,8 +3675,11 @@ var WorkflowContext = class {
|
|
|
3211
3675
|
*
|
|
3212
3676
|
* @param stepName
|
|
3213
3677
|
* @param datetime time to sleep until. Can be provided as a number (in unix seconds),
|
|
3214
|
-
* as a Date object or a string (passed to `new Date(datetimeString)`)
|
|
3678
|
+
* as a Date object or a string (passed to `new Date(datetimeString)`).
|
|
3679
|
+
* A millisecond timestamp such as `Date.now()` is too far in the future, so QStash
|
|
3680
|
+
* rejects the step and the run fails. Pass `new Date(ms)` instead.
|
|
3215
3681
|
* @returns undefined
|
|
3682
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleepUntil.mdx
|
|
3216
3683
|
*/
|
|
3217
3684
|
async sleepUntil(stepName, datetime) {
|
|
3218
3685
|
let time;
|
|
@@ -3263,7 +3730,7 @@ var WorkflowContext = class {
|
|
|
3263
3730
|
* Pauses workflow execution until a specific event occurs or a timeout is reached.
|
|
3264
3731
|
*
|
|
3265
3732
|
*```ts
|
|
3266
|
-
* const result = await
|
|
3733
|
+
* const result = await context.waitForEvent("payment-confirmed", "payment.confirmed", {
|
|
3267
3734
|
* timeout: "5m"
|
|
3268
3735
|
* });
|
|
3269
3736
|
*```
|
|
@@ -3287,11 +3754,14 @@ var WorkflowContext = class {
|
|
|
3287
3754
|
* Alternatively, you can use the `context.notify` method.
|
|
3288
3755
|
*
|
|
3289
3756
|
* @param stepName
|
|
3290
|
-
* @param eventId - Unique identifier for the event to wait for
|
|
3757
|
+
* @param eventId - Unique identifier for the event to wait for. Only letters, digits,
|
|
3758
|
+
* "-", "_" and "." are allowed. QStash rejects other characters, such as ":", which
|
|
3759
|
+
* stops the run with only a "Tried to append to a cancelled workflow" warning.
|
|
3291
3760
|
* @param options - Configuration options.
|
|
3292
3761
|
* @returns `{ timeout: boolean, eventData: TEventData }`.
|
|
3293
3762
|
* The `timeout` property specifies if the workflow has timed out. The `eventData`
|
|
3294
3763
|
* is the data passed when notifying this workflow of an event.
|
|
3764
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForEvent.mdx
|
|
3295
3765
|
*/
|
|
3296
3766
|
async waitForEvent(stepName, eventId, options = {}) {
|
|
3297
3767
|
const { timeout = "7d" } = options;
|
|
@@ -3313,28 +3783,31 @@ var WorkflowContext = class {
|
|
|
3313
3783
|
* will receive the given event data and resume execution.
|
|
3314
3784
|
*
|
|
3315
3785
|
* The response includes the same eventId and eventData. Additionally, there is
|
|
3316
|
-
* a notifyResponse field which contains a list of `
|
|
3317
|
-
* to a notified workflow run.
|
|
3318
|
-
*
|
|
3319
|
-
* Optionally, you can pass a workflowRunId to enable lookback functionality:
|
|
3320
|
-
*
|
|
3321
|
-
* ```ts
|
|
3322
|
-
* const { eventId, eventData, notifyResponse } = await context.notify(
|
|
3323
|
-
* "notify step", "event-id", "event-data", "wfr_123"
|
|
3324
|
-
* );
|
|
3325
|
-
* ```
|
|
3786
|
+
* a notifyResponse field which contains a list of `NotifyResponse` objects, each corresponding
|
|
3787
|
+
* to a notified workflow run. The notified `Waiter` is under `notifyResponse[i].waiter`.
|
|
3326
3788
|
*
|
|
3327
3789
|
* @param stepName
|
|
3328
|
-
* @param eventId event id to notify
|
|
3790
|
+
* @param eventId event id to notify. Only letters, digits, "-", "_" and "." are allowed.
|
|
3791
|
+
* QStash rejects other characters, such as ":", which stops the run with only a
|
|
3792
|
+
* "Tried to append to a cancelled workflow" warning.
|
|
3329
3793
|
* @param eventData event data to notify with
|
|
3330
|
-
* @param workflowRunId optional workflow run id for lookback
|
|
3331
|
-
*
|
|
3794
|
+
* @param workflowRunId optional workflow run id for lookback: the event is kept for the run
|
|
3795
|
+
* even if it has not reached `waitForEvent` yet
|
|
3796
|
+
* @returns notify response which has event id, event data and list of waiters which were
|
|
3797
|
+
* notified. With `workflowRunId`, QStash returns `notifyResponse` as a single object instead
|
|
3798
|
+
* of an array, despite the type.
|
|
3799
|
+
* @see node_modules/@upstash/workflow/docs/steps/notify.mdx
|
|
3332
3800
|
*/
|
|
3333
3801
|
async notify(stepName, eventId, eventData, workflowRunId) {
|
|
3334
3802
|
return await this.addStep(
|
|
3335
3803
|
new LazyNotifyStep(this, stepName, eventId, eventData, this.qstashClient.http, workflowRunId)
|
|
3336
3804
|
);
|
|
3337
3805
|
}
|
|
3806
|
+
/**
|
|
3807
|
+
* Starts another workflow run and waits for its result.
|
|
3808
|
+
*
|
|
3809
|
+
* @see node_modules/@upstash/workflow/docs/steps/invoke.mdx
|
|
3810
|
+
*/
|
|
3338
3811
|
async invoke(stepName, settings) {
|
|
3339
3812
|
validateLabel(settings.label);
|
|
3340
3813
|
validateFlowControl(settings.flowControl);
|
|
@@ -3342,9 +3815,15 @@ var WorkflowContext = class {
|
|
|
3342
3815
|
new LazyInvokeStep(this, stepName, settings)
|
|
3343
3816
|
);
|
|
3344
3817
|
}
|
|
3818
|
+
/**
|
|
3819
|
+
* @see node_modules/@upstash/workflow/docs/steps/createWebhook.mdx
|
|
3820
|
+
*/
|
|
3345
3821
|
async createWebhook(stepName) {
|
|
3346
3822
|
return await this.addStep(new LazyCreateWebhookStep(this, stepName));
|
|
3347
3823
|
}
|
|
3824
|
+
/**
|
|
3825
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForWebhook.mdx
|
|
3826
|
+
*/
|
|
3348
3827
|
async waitForWebhook(stepName, webhook, timeout) {
|
|
3349
3828
|
return await this.addStep(new LazyWaitForWebhookStep(this, stepName, webhook, timeout));
|
|
3350
3829
|
}
|
|
@@ -3353,6 +3832,8 @@ var WorkflowContext = class {
|
|
|
3353
3832
|
*
|
|
3354
3833
|
* Will throw WorkflowCancelAbort to stop workflow execution.
|
|
3355
3834
|
* Shouldn't be inside try/catch.
|
|
3835
|
+
*
|
|
3836
|
+
* @see node_modules/@upstash/workflow/docs/steps/cancel.mdx
|
|
3356
3837
|
*/
|
|
3357
3838
|
async cancel() {
|
|
3358
3839
|
throw new WorkflowCancelAbort();
|
|
@@ -3371,6 +3852,11 @@ var WorkflowContext = class {
|
|
|
3371
3852
|
throw error;
|
|
3372
3853
|
}
|
|
3373
3854
|
}
|
|
3855
|
+
/**
|
|
3856
|
+
* Calls OpenAI, Anthropic or Resend through QStash, like `context.call`.
|
|
3857
|
+
*
|
|
3858
|
+
* @see node_modules/@upstash/workflow/docs/steps/api.mdx
|
|
3859
|
+
*/
|
|
3374
3860
|
get api() {
|
|
3375
3861
|
return new WorkflowApi({
|
|
3376
3862
|
context: this
|
|
@@ -3409,7 +3895,7 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3409
3895
|
*
|
|
3410
3896
|
* @param routeFunction
|
|
3411
3897
|
*/
|
|
3412
|
-
static async tryAuthentication(routeFunction, context) {
|
|
3898
|
+
static async tryAuthentication(routeFunction, context, effectiveConfig) {
|
|
3413
3899
|
const disabledContext = new _DisabledWorkflowContext({
|
|
3414
3900
|
qstashClient: new import_qstash10.Client({
|
|
3415
3901
|
baseUrl: "disabled-client",
|
|
@@ -3424,7 +3910,10 @@ var DisabledWorkflowContext = class _DisabledWorkflowContext extends WorkflowCon
|
|
|
3424
3910
|
initialPayload: context.requestPayload,
|
|
3425
3911
|
env: context.env,
|
|
3426
3912
|
label: context.labels,
|
|
3427
|
-
retried: context.retried
|
|
3913
|
+
retried: context.retried,
|
|
3914
|
+
// the route function runs for real here until it reaches a step, so
|
|
3915
|
+
// it observes the same configuration as it will on the real context
|
|
3916
|
+
effectiveConfig
|
|
3428
3917
|
});
|
|
3429
3918
|
try {
|
|
3430
3919
|
await routeFunction(disabledContext);
|
|
@@ -3641,6 +4130,7 @@ var handleFailure = async ({
|
|
|
3641
4130
|
}
|
|
3642
4131
|
const userHeaders = recreateUserHeaders(request.headers);
|
|
3643
4132
|
const retried = Number(request.headers.get(WORKFLOW_RETRIED_HEADER) ?? "0");
|
|
4133
|
+
const effectiveConfig = getEffectiveConfig(request.headers);
|
|
3644
4134
|
const workflowContext = new WorkflowContext({
|
|
3645
4135
|
qstashClient,
|
|
3646
4136
|
workflowRunId,
|
|
@@ -3654,11 +4144,13 @@ var handleFailure = async ({
|
|
|
3654
4144
|
label: userHeaders.get(WORKFLOW_LABEL_HEADER) ?? void 0,
|
|
3655
4145
|
retried,
|
|
3656
4146
|
workflowRunCreatedAt: workflowCreatedAt,
|
|
3657
|
-
middlewareManager: void 0
|
|
4147
|
+
middlewareManager: void 0,
|
|
4148
|
+
effectiveConfig
|
|
3658
4149
|
});
|
|
3659
4150
|
const authCheck = await DisabledWorkflowContext.tryAuthentication(
|
|
3660
4151
|
routeFunction,
|
|
3661
|
-
workflowContext
|
|
4152
|
+
workflowContext,
|
|
4153
|
+
effectiveConfig
|
|
3662
4154
|
);
|
|
3663
4155
|
if (authCheck.isErr()) {
|
|
3664
4156
|
await dispatchDebug?.("onError", {
|
|
@@ -4149,12 +4641,14 @@ var serveBase = (routeFunction, telemetry2, options, internalOptions) => {
|
|
|
4149
4641
|
const label = request.headers.get(WORKFLOW_LABEL_HEADER) ?? void 0;
|
|
4150
4642
|
const workflowRunCreatedAtHeader = request.headers.get(WORKFLOW_CREATED_AT_HEADER);
|
|
4151
4643
|
const workflowRunCreatedAt = workflowRunCreatedAtHeader ? Number(workflowRunCreatedAtHeader) : isFirstInvocation ? Date.now() : 0;
|
|
4644
|
+
const effectiveConfig = getEffectiveConfig(request.headers);
|
|
4152
4645
|
const workflowContext = new WorkflowContext({
|
|
4153
4646
|
qstashClient: regionalClient,
|
|
4154
4647
|
workflowRunId,
|
|
4155
4648
|
initialPayload: isThirdPartyCallResult(request) ? JSON.parse(rawInitialPayload) : initialPayloadParser(rawInitialPayload),
|
|
4156
4649
|
headers: recreateUserHeaders(request.headers),
|
|
4157
4650
|
steps,
|
|
4651
|
+
effectiveConfig,
|
|
4158
4652
|
url: workflowUrl,
|
|
4159
4653
|
env,
|
|
4160
4654
|
telemetry: telemetry2,
|
|
@@ -4166,7 +4660,8 @@ var serveBase = (routeFunction, telemetry2, options, internalOptions) => {
|
|
|
4166
4660
|
});
|
|
4167
4661
|
const authCheck = await DisabledWorkflowContext.tryAuthentication(
|
|
4168
4662
|
routeFunction,
|
|
4169
|
-
workflowContext
|
|
4663
|
+
workflowContext,
|
|
4664
|
+
effectiveConfig
|
|
4170
4665
|
);
|
|
4171
4666
|
if (authCheck.isErr()) {
|
|
4172
4667
|
throw authCheck.error;
|
|
@@ -4203,7 +4698,23 @@ var serveBase = (routeFunction, telemetry2, options, internalOptions) => {
|
|
|
4203
4698
|
if (steps.length === 1) {
|
|
4204
4699
|
await middlewareManager.dispatchLifecycle("runStarted", {});
|
|
4205
4700
|
}
|
|
4206
|
-
|
|
4701
|
+
let outcome;
|
|
4702
|
+
try {
|
|
4703
|
+
outcome = { ran: true, result: await routeFunction(workflowContext) };
|
|
4704
|
+
} catch (error) {
|
|
4705
|
+
outcome = { ran: false, error };
|
|
4706
|
+
}
|
|
4707
|
+
const submitted = await flushPendingStep(workflowContext);
|
|
4708
|
+
if (submitted.isErr()) {
|
|
4709
|
+
throw submitted.error;
|
|
4710
|
+
}
|
|
4711
|
+
if (submitted.value.result === "submitted-step") {
|
|
4712
|
+
throw submitted.value.abort;
|
|
4713
|
+
}
|
|
4714
|
+
if (!outcome.ran) {
|
|
4715
|
+
throw outcome.error;
|
|
4716
|
+
}
|
|
4717
|
+
return outcome.result;
|
|
4207
4718
|
},
|
|
4208
4719
|
onCleanup: async (result2) => {
|
|
4209
4720
|
await middlewareManager.dispatchLifecycle("runCompleted", {
|