@upstash/workflow 1.4.0-rc → 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 +18 -2
- package/astro.d.ts +18 -2
- package/astro.js +129 -29
- package/astro.mjs +1 -1
- package/{chunk-UH6O5IJY.mjs → chunk-J3PXPHSB.mjs} +129 -29
- package/cloudflare.d.mts +16 -2
- package/cloudflare.d.ts +16 -2
- package/cloudflare.js +129 -29
- 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 +18 -2
- package/express.d.ts +18 -2
- package/express.js +129 -29
- package/express.mjs +1 -1
- package/h3.d.mts +18 -2
- package/h3.d.ts +18 -2
- package/h3.js +129 -29
- package/h3.mjs +1 -1
- package/hono.d.mts +16 -2
- package/hono.d.ts +16 -2
- package/hono.js +129 -29
- package/hono.mjs +1 -1
- package/index.d.mts +49 -21
- package/index.d.ts +49 -21
- package/index.js +145 -41
- package/index.mjs +17 -13
- package/nextjs.d.mts +39 -2
- package/nextjs.d.ts +39 -2
- package/nextjs.js +129 -29
- package/nextjs.mjs +1 -1
- package/package.json +1 -1
- package/react-router.d.mts +15 -2
- package/react-router.d.ts +15 -2
- package/react-router.js +129 -29
- package/react-router.mjs +1 -1
- package/{serve-many-CbIV7145.d.mts → serve-many-BKukvLNl.d.mts} +1 -1
- package/{serve-many-BCtZg31b.d.ts → serve-many-CrLKQpuZ.d.ts} +1 -1
- package/solidjs.d.mts +9 -1
- package/solidjs.d.ts +9 -1
- package/solidjs.js +129 -29
- 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 +16 -2
- package/svelte.d.ts +16 -2
- package/svelte.js +129 -29
- package/svelte.mjs +1 -1
- package/tanstack.d.mts +14 -2
- package/tanstack.d.ts +14 -2
- package/tanstack.js +129 -29
- package/tanstack.mjs +1 -1
- package/{types-ffLPVG5_.d.ts → types-CrgK5P6p.d.mts} +124 -24
- package/{types-ffLPVG5_.d.mts → types-CrgK5P6p.d.ts} +124 -24
package/README.md
CHANGED
|
@@ -118,6 +118,15 @@ QSTASH_DEV=true
|
|
|
118
118
|
|
|
119
119
|
See [Local Development](https://upstash.com/docs/workflow/howto/local-development/development-server) for details.
|
|
120
120
|
|
|
121
|
+
## Docs
|
|
122
|
+
|
|
123
|
+
See [the documentation](https://upstash.com/docs/workflow) for details.
|
|
124
|
+
|
|
125
|
+
The npm package includes documentation in `node_modules/@upstash/workflow/docs/`
|
|
126
|
+
and TypeScript source in `node_modules/@upstash/workflow/src/` for offline reference
|
|
127
|
+
and coding agents. Start with `docs/getstarted.mdx`; the SDK entrypoint is
|
|
128
|
+
in `src/src/index.ts` and platform adapters are in `src/platforms/`.
|
|
129
|
+
|
|
121
130
|
## Telemetry
|
|
122
131
|
|
|
123
132
|
This sdk sends anonymous telemetry headers to help us improve your experience.
|
package/astro.d.mts
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import { APIContext, APIRoute } from 'astro';
|
|
2
|
-
import { v as WorkflowContext, d as WorkflowServeOptions, I as InvokableWorkflow } from './types-
|
|
3
|
-
import { s as serveManyBase } from './serve-many-
|
|
2
|
+
import { v as WorkflowContext, d as WorkflowServeOptions, I as InvokableWorkflow } from './types-CrgK5P6p.mjs';
|
|
3
|
+
import { s as serveManyBase } from './serve-many-BKukvLNl.mjs';
|
|
4
4
|
import '@upstash/qstash';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import 'neverthrow';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Code outside steps runs again on every request of the run. Anything it branches on or
|
|
10
|
+
* returns early on (Date.now(), database rows, random values) must give the same answer on
|
|
11
|
+
* every request, or later requests fail with "Incompatible step name" or "Failed to
|
|
12
|
+
* authenticate Workflow request". Read such values inside `context.run` and branch on its
|
|
13
|
+
* result.
|
|
14
|
+
*
|
|
15
|
+
* @see node_modules/@upstash/workflow/docs/basics/serve.mdx
|
|
16
|
+
* @see node_modules/@upstash/workflow/docs/quickstarts/astro.mdx
|
|
17
|
+
*/
|
|
8
18
|
declare function serve<TInitialPayload = unknown, TResult = unknown>(routeFunction: (workflowContext: WorkflowContext<TInitialPayload>, apiContext: APIContext) => Promise<TResult>, options?: WorkflowServeOptions<TInitialPayload, TResult>): {
|
|
9
19
|
POST: APIRoute;
|
|
10
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* @see node_modules/@upstash/workflow/docs/features/invoke.mdx
|
|
23
|
+
*/
|
|
11
24
|
declare const createWorkflow: <TInitialPayload, TResult>(...params: Parameters<typeof serve<TInitialPayload, TResult>>) => InvokableWorkflow<TInitialPayload, TResult>;
|
|
25
|
+
/**
|
|
26
|
+
* @see node_modules/@upstash/workflow/docs/features/invoke/serveMany.mdx
|
|
27
|
+
*/
|
|
12
28
|
declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => {
|
|
13
29
|
POST: (context: APIContext<Record<string, any>, Record<string, string | undefined>>) => Promise<any>;
|
|
14
30
|
};
|
package/astro.d.ts
CHANGED
|
@@ -1,14 +1,30 @@
|
|
|
1
1
|
import { APIContext, APIRoute } from 'astro';
|
|
2
|
-
import { v as WorkflowContext, d as WorkflowServeOptions, I as InvokableWorkflow } from './types-
|
|
3
|
-
import { s as serveManyBase } from './serve-many-
|
|
2
|
+
import { v as WorkflowContext, d as WorkflowServeOptions, I as InvokableWorkflow } from './types-CrgK5P6p.js';
|
|
3
|
+
import { s as serveManyBase } from './serve-many-CrLKQpuZ.js';
|
|
4
4
|
import '@upstash/qstash';
|
|
5
5
|
import 'zod';
|
|
6
6
|
import 'neverthrow';
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Code outside steps runs again on every request of the run. Anything it branches on or
|
|
10
|
+
* returns early on (Date.now(), database rows, random values) must give the same answer on
|
|
11
|
+
* every request, or later requests fail with "Incompatible step name" or "Failed to
|
|
12
|
+
* authenticate Workflow request". Read such values inside `context.run` and branch on its
|
|
13
|
+
* result.
|
|
14
|
+
*
|
|
15
|
+
* @see node_modules/@upstash/workflow/docs/basics/serve.mdx
|
|
16
|
+
* @see node_modules/@upstash/workflow/docs/quickstarts/astro.mdx
|
|
17
|
+
*/
|
|
8
18
|
declare function serve<TInitialPayload = unknown, TResult = unknown>(routeFunction: (workflowContext: WorkflowContext<TInitialPayload>, apiContext: APIContext) => Promise<TResult>, options?: WorkflowServeOptions<TInitialPayload, TResult>): {
|
|
9
19
|
POST: APIRoute;
|
|
10
20
|
};
|
|
21
|
+
/**
|
|
22
|
+
* @see node_modules/@upstash/workflow/docs/features/invoke.mdx
|
|
23
|
+
*/
|
|
11
24
|
declare const createWorkflow: <TInitialPayload, TResult>(...params: Parameters<typeof serve<TInitialPayload, TResult>>) => InvokableWorkflow<TInitialPayload, TResult>;
|
|
25
|
+
/**
|
|
26
|
+
* @see node_modules/@upstash/workflow/docs/features/invoke/serveMany.mdx
|
|
27
|
+
*/
|
|
12
28
|
declare const serveMany: (workflows: Parameters<typeof serveManyBase>[0]["workflows"], options?: Parameters<typeof serveManyBase>[0]["options"]) => {
|
|
13
29
|
POST: (context: APIContext<Record<string, any>, Record<string, string | undefined>>) => Promise<any>;
|
|
14
30
|
};
|
package/astro.js
CHANGED
|
@@ -792,6 +792,10 @@ var triggerFirstInvocation = async (params) => {
|
|
|
792
792
|
workflowConfig: {
|
|
793
793
|
workflowRunId: workflowContext.workflowRunId,
|
|
794
794
|
workflowUrl: workflowContext.url,
|
|
795
|
+
// the run doesn't exist yet, so the trigger time is sent when the creation time
|
|
796
|
+
// is not known. serve and client.trigger set it on the context already, so a
|
|
797
|
+
// retry of this request carries the same value and is deduplicated.
|
|
798
|
+
workflowRunCreatedAt: workflowContext.workflowRunCreatedAt || Date.now(),
|
|
795
799
|
failureUrl,
|
|
796
800
|
retries,
|
|
797
801
|
retryDelay,
|
|
@@ -899,7 +903,10 @@ var triggerWorkflowDelete = async (workflowContext, result, cancel = false, disp
|
|
|
899
903
|
path: ["v2", "workflows", "runs", `${workflowContext.workflowRunId}?cancel=${cancel}`],
|
|
900
904
|
method: "DELETE",
|
|
901
905
|
parseResponseAsJson: false,
|
|
902
|
-
body: JSON.stringify(result)
|
|
906
|
+
body: JSON.stringify(result),
|
|
907
|
+
// with the creation time, QStash deletes the run only if it is the same run.
|
|
908
|
+
// a late delete of a previous run can't delete a new run with the same id.
|
|
909
|
+
headers: workflowContext.workflowRunCreatedAt ? { [WORKFLOW_CREATED_AT_HEADER]: workflowContext.workflowRunCreatedAt.toString() } : void 0
|
|
903
910
|
});
|
|
904
911
|
await dispatchDebug?.("onInfo", {
|
|
905
912
|
info: `Workflow run ${workflowContext.workflowRunId} deleted from QStash successfully.`
|
|
@@ -976,6 +983,7 @@ ${atob(callbackMessage.body ?? "")}`
|
|
|
976
983
|
const concurrentString = request.headers.get("Upstash-Workflow-Concurrent");
|
|
977
984
|
const contentType = request.headers.get("Upstash-Workflow-ContentType");
|
|
978
985
|
const invokeCount = request.headers.get(WORKFLOW_INVOKE_COUNT_HEADER);
|
|
986
|
+
const workflowRunCreatedAt = request.headers.get(WORKFLOW_CREATED_AT_HEADER);
|
|
979
987
|
if (!(workflowRunId && stepIdString && stepName && StepTypes.includes(stepType) && concurrentString && contentType)) {
|
|
980
988
|
throw new Error(
|
|
981
989
|
`Missing info in callback message source header: ${JSON.stringify({
|
|
@@ -993,6 +1001,7 @@ ${atob(callbackMessage.body ?? "")}`
|
|
|
993
1001
|
workflowConfig: {
|
|
994
1002
|
workflowRunId,
|
|
995
1003
|
workflowUrl,
|
|
1004
|
+
workflowRunCreatedAt: Number(workflowRunCreatedAt ?? "0"),
|
|
996
1005
|
telemetry: telemetry2
|
|
997
1006
|
},
|
|
998
1007
|
invokeCount: Number(invokeCount)
|
|
@@ -1167,6 +1176,7 @@ var BaseLazyStep = class _BaseLazyStep {
|
|
|
1167
1176
|
workflowConfig: {
|
|
1168
1177
|
workflowRunId: context.workflowRunId,
|
|
1169
1178
|
workflowUrl: context.url,
|
|
1179
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
1170
1180
|
useJSONContent: false,
|
|
1171
1181
|
telemetry: telemetry2
|
|
1172
1182
|
},
|
|
@@ -1498,6 +1508,8 @@ var LazyWaitEventStep = class extends BaseLazyStep {
|
|
|
1498
1508
|
) : {},
|
|
1499
1509
|
// note: using WORKFLOW_ID_HEADER doesn't work, because Runid -> RunId:
|
|
1500
1510
|
"Upstash-Workflow-Runid": [context.workflowRunId],
|
|
1511
|
+
// same for WORKFLOW_CREATED_AT_HEADER, because Createdat -> CreatedAt:
|
|
1512
|
+
...context.workflowRunCreatedAt ? { "Upstash-Workflow-Createdat": [context.workflowRunCreatedAt.toString()] } : {},
|
|
1501
1513
|
[WORKFLOW_INIT_HEADER]: ["false"],
|
|
1502
1514
|
[WORKFLOW_URL_HEADER]: [context.url],
|
|
1503
1515
|
"Upstash-Workflow-CallType": ["step"]
|
|
@@ -1600,6 +1612,7 @@ var LazyInvokeStep = class extends BaseLazyStep {
|
|
|
1600
1612
|
workflowConfig: {
|
|
1601
1613
|
workflowRunId: context.workflowRunId,
|
|
1602
1614
|
workflowUrl: context.url,
|
|
1615
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
1603
1616
|
telemetry: telemetry2,
|
|
1604
1617
|
useJSONContent: false
|
|
1605
1618
|
},
|
|
@@ -1609,6 +1622,9 @@ var LazyInvokeStep = class extends BaseLazyStep {
|
|
|
1609
1622
|
invokerHeaders[key] = value;
|
|
1610
1623
|
});
|
|
1611
1624
|
invokerHeaders["Upstash-Workflow-Runid"] = context.workflowRunId;
|
|
1625
|
+
if (context.workflowRunCreatedAt) {
|
|
1626
|
+
invokerHeaders["Upstash-Workflow-Createdat"] = context.workflowRunCreatedAt.toString();
|
|
1627
|
+
}
|
|
1612
1628
|
const request = {
|
|
1613
1629
|
body: stringifyBody(this.params.body),
|
|
1614
1630
|
headers: Object.fromEntries(
|
|
@@ -1637,6 +1653,9 @@ var LazyInvokeStep = class extends BaseLazyStep {
|
|
|
1637
1653
|
workflowConfig: {
|
|
1638
1654
|
workflowRunId: getWorkflowRunId(workflowRunId),
|
|
1639
1655
|
workflowUrl: newUrl,
|
|
1656
|
+
// the invoked run doesn't exist yet, so the creation time of the invoker run is sent.
|
|
1657
|
+
// it separates the invocations made by different runs of the invoker.
|
|
1658
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
1640
1659
|
retries,
|
|
1641
1660
|
retryDelay,
|
|
1642
1661
|
telemetry: telemetry2,
|
|
@@ -1806,6 +1825,9 @@ var WorkflowHeaders = class {
|
|
|
1806
1825
|
[WORKFLOW_PROTOCOL_VERSION_HEADER]: WORKFLOW_PROTOCOL_VERSION,
|
|
1807
1826
|
...this.workflowConfig.telemetry ? getTelemetryHeaders(this.workflowConfig.telemetry) : {}
|
|
1808
1827
|
};
|
|
1828
|
+
if (this.workflowConfig.workflowRunCreatedAt) {
|
|
1829
|
+
this.headers.rawHeaders[WORKFLOW_CREATED_AT_HEADER] = this.workflowConfig.workflowRunCreatedAt.toString();
|
|
1830
|
+
}
|
|
1809
1831
|
if (this.stepInfo?.lazyStep.stepType !== "Call") {
|
|
1810
1832
|
this.headers.rawHeaders[`Upstash-Forward-${WORKFLOW_PROTOCOL_VERSION_HEADER}`] = WORKFLOW_PROTOCOL_VERSION;
|
|
1811
1833
|
}
|
|
@@ -2004,6 +2026,7 @@ var submitParallelSteps = async ({
|
|
|
2004
2026
|
workflowConfig: {
|
|
2005
2027
|
workflowRunId: context.workflowRunId,
|
|
2006
2028
|
workflowUrl: context.url,
|
|
2029
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
2007
2030
|
telemetry: telemetry2
|
|
2008
2031
|
},
|
|
2009
2032
|
invokeCount,
|
|
@@ -2094,6 +2117,7 @@ var publishStepConfigRequest = async ({
|
|
|
2094
2117
|
initHeaderValue: "false",
|
|
2095
2118
|
workflowConfig: {
|
|
2096
2119
|
workflowRunId: context.workflowRunId,
|
|
2120
|
+
workflowRunCreatedAt: context.workflowRunCreatedAt,
|
|
2097
2121
|
workflowUrl: context.url,
|
|
2098
2122
|
telemetry: telemetry2
|
|
2099
2123
|
},
|
|
@@ -2106,7 +2130,9 @@ var publishStepConfigRequest = async ({
|
|
|
2106
2130
|
const result = await context.qstashClient.publishJSON({
|
|
2107
2131
|
headers: {
|
|
2108
2132
|
...headers,
|
|
2109
|
-
"Upstash-Workflow-CallType": WORKFLOW_STEP_CONFIG_CALL_TYPE
|
|
2133
|
+
"Upstash-Workflow-CallType": WORKFLOW_STEP_CONFIG_CALL_TYPE,
|
|
2134
|
+
"Upstash-Forward-Upstash-Workflow-StepId": targetStep.toString(),
|
|
2135
|
+
"Upstash-Forward-Upstash-Workflow-StepName": lazyStep.stepName
|
|
2110
2136
|
},
|
|
2111
2137
|
method: "POST",
|
|
2112
2138
|
body: { targetStep, invokeCount },
|
|
@@ -2125,6 +2151,19 @@ var publishStepConfigRequest = async ({
|
|
|
2125
2151
|
};
|
|
2126
2152
|
|
|
2127
2153
|
// src/qstash/step-config.ts
|
|
2154
|
+
var normalizeStepSettings = (settings) => {
|
|
2155
|
+
if (settings === void 0) {
|
|
2156
|
+
return void 0;
|
|
2157
|
+
}
|
|
2158
|
+
const { retries, retryDelay } = settings;
|
|
2159
|
+
if (retryDelay !== void 0 && typeof retryDelay !== "string") {
|
|
2160
|
+
throw new WorkflowNonRetryableError("Invalid step retryDelay: must be a string.");
|
|
2161
|
+
}
|
|
2162
|
+
if (retries !== void 0 && (!Number.isInteger(retries) || retries < 0)) {
|
|
2163
|
+
throw new WorkflowNonRetryableError("Invalid step retries: must be a non-negative integer.");
|
|
2164
|
+
}
|
|
2165
|
+
return { ...settings, retryDelay: retryDelay?.trim() || void 0 };
|
|
2166
|
+
};
|
|
2128
2167
|
var DEFAULT_PARALLELISM = 0;
|
|
2129
2168
|
var DEFAULT_RATE = 0;
|
|
2130
2169
|
var DEFAULT_PERIOD_SECONDS = 1;
|
|
@@ -2388,10 +2427,18 @@ var AutoExecutor = class _AutoExecutor {
|
|
|
2388
2427
|
dispatchLifecycle: this.dispatchLifecycle
|
|
2389
2428
|
});
|
|
2390
2429
|
if (lazyStep.supportsDeferredSubmission) {
|
|
2391
|
-
|
|
2430
|
+
const serializedOut = JSON.stringify(resultStep.out);
|
|
2431
|
+
this.pendingStep = {
|
|
2432
|
+
status: "held",
|
|
2433
|
+
lazyStep,
|
|
2434
|
+
resultStep: {
|
|
2435
|
+
...resultStep,
|
|
2436
|
+
out: serializedOut === void 0 ? void 0 : JSON.parse(serializedOut)
|
|
2437
|
+
}
|
|
2438
|
+
};
|
|
2392
2439
|
return lazyStep.parseOut({
|
|
2393
2440
|
...resultStep,
|
|
2394
|
-
out:
|
|
2441
|
+
out: serializedOut
|
|
2395
2442
|
});
|
|
2396
2443
|
}
|
|
2397
2444
|
await submitStepResult({
|
|
@@ -2736,6 +2783,11 @@ var BaseWorkflowApi = class {
|
|
|
2736
2783
|
|
|
2737
2784
|
// src/context/api/anthropic.ts
|
|
2738
2785
|
var AnthropicAPI = class extends BaseWorkflowApi {
|
|
2786
|
+
/**
|
|
2787
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2788
|
+
*
|
|
2789
|
+
* @see node_modules/@upstash/workflow/docs/integrations/anthropic.mdx
|
|
2790
|
+
*/
|
|
2739
2791
|
async call(stepName, settings) {
|
|
2740
2792
|
const { token, operation, ...parameters } = settings;
|
|
2741
2793
|
return await this.callApi(stepName, {
|
|
@@ -2751,6 +2803,11 @@ var AnthropicAPI = class extends BaseWorkflowApi {
|
|
|
2751
2803
|
// src/context/api/openai.ts
|
|
2752
2804
|
var import_qstash7 = require("@upstash/qstash");
|
|
2753
2805
|
var OpenAIAPI = class extends BaseWorkflowApi {
|
|
2806
|
+
/**
|
|
2807
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2808
|
+
*
|
|
2809
|
+
* @see node_modules/@upstash/workflow/docs/integrations/openai.mdx
|
|
2810
|
+
*/
|
|
2754
2811
|
async call(stepName, settings) {
|
|
2755
2812
|
const { token, organization, operation, baseURL, ...parameters } = settings;
|
|
2756
2813
|
const useOpenAI = baseURL === void 0;
|
|
@@ -2768,6 +2825,11 @@ var OpenAIAPI = class extends BaseWorkflowApi {
|
|
|
2768
2825
|
// src/context/api/resend.ts
|
|
2769
2826
|
var import_qstash8 = require("@upstash/qstash");
|
|
2770
2827
|
var ResendAPI = class extends BaseWorkflowApi {
|
|
2828
|
+
/**
|
|
2829
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
2830
|
+
*
|
|
2831
|
+
* @see node_modules/@upstash/workflow/docs/integrations/resend.mdx
|
|
2832
|
+
*/
|
|
2771
2833
|
async call(stepName, settings) {
|
|
2772
2834
|
const { token, batch = false, ...parameters } = settings;
|
|
2773
2835
|
return await this.callApi(stepName, {
|
|
@@ -3009,8 +3071,9 @@ var WorkflowContext = class {
|
|
|
3009
3071
|
*
|
|
3010
3072
|
* ```ts
|
|
3011
3073
|
* import { Client } from "@upstash/qstash"
|
|
3074
|
+
* import { serve } from "@upstash/workflow/nextjs"
|
|
3012
3075
|
*
|
|
3013
|
-
* export const POST = serve(
|
|
3076
|
+
* export const { POST } = serve(
|
|
3014
3077
|
* async (context) => {
|
|
3015
3078
|
* ...
|
|
3016
3079
|
* },
|
|
@@ -3035,7 +3098,7 @@ var WorkflowContext = class {
|
|
|
3035
3098
|
* Can be overwritten by passing a `url` parameter in `serve`:
|
|
3036
3099
|
*
|
|
3037
3100
|
* ```ts
|
|
3038
|
-
* export const POST = serve(
|
|
3101
|
+
* export const { POST } = serve(
|
|
3039
3102
|
* async (context) => {
|
|
3040
3103
|
* ...
|
|
3041
3104
|
* },
|
|
@@ -3053,7 +3116,7 @@ var WorkflowContext = class {
|
|
|
3053
3116
|
*
|
|
3054
3117
|
* ```ts
|
|
3055
3118
|
* // set requestPayload type to MyPayload:
|
|
3056
|
-
* export const POST = serve<MyPayload>(
|
|
3119
|
+
* export const { POST } = serve<MyPayload>(
|
|
3057
3120
|
* async (context) => {
|
|
3058
3121
|
* ...
|
|
3059
3122
|
* }
|
|
@@ -3065,7 +3128,7 @@ var WorkflowContext = class {
|
|
|
3065
3128
|
* the `initialPayloadParser` parameter:
|
|
3066
3129
|
*
|
|
3067
3130
|
* ```ts
|
|
3068
|
-
* export const POST = serve<MyPayload>(
|
|
3131
|
+
* export const { POST } = serve<MyPayload>(
|
|
3069
3132
|
* async (context) => {
|
|
3070
3133
|
* ...
|
|
3071
3134
|
* },
|
|
@@ -3074,6 +3137,13 @@ var WorkflowContext = class {
|
|
|
3074
3137
|
* }
|
|
3075
3138
|
* )
|
|
3076
3139
|
* ```
|
|
3140
|
+
*
|
|
3141
|
+
* When a `context.call` response comes back, the route runs up to its first step with this
|
|
3142
|
+
* set to the call-result envelope (and `workflowRunId` set to a random id) before the SDK
|
|
3143
|
+
* recognizes the request. Code before the first step must not dereference, validate or
|
|
3144
|
+
* return early on the payload; read it inside `context.run`.
|
|
3145
|
+
*
|
|
3146
|
+
* @see node_modules/@upstash/workflow/docs/basics/serve/advanced.mdx
|
|
3077
3147
|
*/
|
|
3078
3148
|
requestPayload;
|
|
3079
3149
|
/**
|
|
@@ -3086,7 +3156,7 @@ var WorkflowContext = class {
|
|
|
3086
3156
|
* Can be set using the `env` option of serve:
|
|
3087
3157
|
*
|
|
3088
3158
|
* ```ts
|
|
3089
|
-
* export const POST = serve<MyPayload>(
|
|
3159
|
+
* export const { POST } = serve<MyPayload>(
|
|
3090
3160
|
* async (context) => {
|
|
3091
3161
|
* const key = context.env["API_KEY"];
|
|
3092
3162
|
* },
|
|
@@ -3252,18 +3322,24 @@ var WorkflowContext = class {
|
|
|
3252
3322
|
* )
|
|
3253
3323
|
* ```
|
|
3254
3324
|
*
|
|
3325
|
+
* Do not wrap steps in try/catch or `.catch` without rethrowing `WorkflowAbort`: each step
|
|
3326
|
+
* throws it to end the request. To stop retrying a failed step, throw
|
|
3327
|
+
* `WorkflowNonRetryableError`.
|
|
3328
|
+
*
|
|
3255
3329
|
* @param stepName name of the step
|
|
3256
3330
|
* @param stepFunction step function to be executed
|
|
3257
3331
|
* @param stepSettings step-level settings for this step
|
|
3258
3332
|
* @returns result of the step function
|
|
3333
|
+
* @see node_modules/@upstash/workflow/docs/steps/run.mdx
|
|
3259
3334
|
*/
|
|
3260
3335
|
async run(stepName, stepFunction, stepSettings) {
|
|
3336
|
+
const normalizedSettings = normalizeStepSettings(stepSettings);
|
|
3261
3337
|
if (stepSettings) {
|
|
3262
3338
|
validateFlowControl(stepSettings.flowControl);
|
|
3263
3339
|
}
|
|
3264
3340
|
const wrappedStepFunction = (() => this.executor.wrapStep(stepName, stepFunction));
|
|
3265
3341
|
return await this.addStep(
|
|
3266
|
-
new LazyFunctionStep(this, stepName, wrappedStepFunction,
|
|
3342
|
+
new LazyFunctionStep(this, stepName, wrappedStepFunction, normalizedSettings)
|
|
3267
3343
|
);
|
|
3268
3344
|
}
|
|
3269
3345
|
/**
|
|
@@ -3274,8 +3350,9 @@ var WorkflowContext = class {
|
|
|
3274
3350
|
* ```
|
|
3275
3351
|
*
|
|
3276
3352
|
* @param stepName
|
|
3277
|
-
* @param duration sleep duration in seconds
|
|
3353
|
+
* @param duration sleep duration in seconds, or a duration string such as "10m"
|
|
3278
3354
|
* @returns undefined
|
|
3355
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleep.mdx
|
|
3279
3356
|
*/
|
|
3280
3357
|
async sleep(stepName, duration) {
|
|
3281
3358
|
await this.addStep(new LazySleepStep(this, stepName, duration));
|
|
@@ -3289,8 +3366,11 @@ var WorkflowContext = class {
|
|
|
3289
3366
|
*
|
|
3290
3367
|
* @param stepName
|
|
3291
3368
|
* @param datetime time to sleep until. Can be provided as a number (in unix seconds),
|
|
3292
|
-
* as a Date object or a string (passed to `new Date(datetimeString)`)
|
|
3369
|
+
* as a Date object or a string (passed to `new Date(datetimeString)`).
|
|
3370
|
+
* A millisecond timestamp such as `Date.now()` is too far in the future, so QStash
|
|
3371
|
+
* rejects the step and the run fails. Pass `new Date(ms)` instead.
|
|
3293
3372
|
* @returns undefined
|
|
3373
|
+
* @see node_modules/@upstash/workflow/docs/steps/sleepUntil.mdx
|
|
3294
3374
|
*/
|
|
3295
3375
|
async sleepUntil(stepName, datetime) {
|
|
3296
3376
|
let time;
|
|
@@ -3341,7 +3421,7 @@ var WorkflowContext = class {
|
|
|
3341
3421
|
* Pauses workflow execution until a specific event occurs or a timeout is reached.
|
|
3342
3422
|
*
|
|
3343
3423
|
*```ts
|
|
3344
|
-
* const result = await
|
|
3424
|
+
* const result = await context.waitForEvent("payment-confirmed", "payment.confirmed", {
|
|
3345
3425
|
* timeout: "5m"
|
|
3346
3426
|
* });
|
|
3347
3427
|
*```
|
|
@@ -3365,11 +3445,14 @@ var WorkflowContext = class {
|
|
|
3365
3445
|
* Alternatively, you can use the `context.notify` method.
|
|
3366
3446
|
*
|
|
3367
3447
|
* @param stepName
|
|
3368
|
-
* @param eventId - Unique identifier for the event to wait for
|
|
3448
|
+
* @param eventId - Unique identifier for the event to wait for. Only letters, digits,
|
|
3449
|
+
* "-", "_" and "." are allowed. QStash rejects other characters, such as ":", which
|
|
3450
|
+
* stops the run with only a "Tried to append to a cancelled workflow" warning.
|
|
3369
3451
|
* @param options - Configuration options.
|
|
3370
3452
|
* @returns `{ timeout: boolean, eventData: TEventData }`.
|
|
3371
3453
|
* The `timeout` property specifies if the workflow has timed out. The `eventData`
|
|
3372
3454
|
* is the data passed when notifying this workflow of an event.
|
|
3455
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForEvent.mdx
|
|
3373
3456
|
*/
|
|
3374
3457
|
async waitForEvent(stepName, eventId, options = {}) {
|
|
3375
3458
|
const { timeout = "7d" } = options;
|
|
@@ -3391,28 +3474,31 @@ var WorkflowContext = class {
|
|
|
3391
3474
|
* will receive the given event data and resume execution.
|
|
3392
3475
|
*
|
|
3393
3476
|
* The response includes the same eventId and eventData. Additionally, there is
|
|
3394
|
-
* a notifyResponse field which contains a list of `
|
|
3395
|
-
* to a notified workflow run.
|
|
3396
|
-
*
|
|
3397
|
-
* Optionally, you can pass a workflowRunId to enable lookback functionality:
|
|
3398
|
-
*
|
|
3399
|
-
* ```ts
|
|
3400
|
-
* const { eventId, eventData, notifyResponse } = await context.notify(
|
|
3401
|
-
* "notify step", "event-id", "event-data", "wfr_123"
|
|
3402
|
-
* );
|
|
3403
|
-
* ```
|
|
3477
|
+
* a notifyResponse field which contains a list of `NotifyResponse` objects, each corresponding
|
|
3478
|
+
* to a notified workflow run. The notified `Waiter` is under `notifyResponse[i].waiter`.
|
|
3404
3479
|
*
|
|
3405
3480
|
* @param stepName
|
|
3406
|
-
* @param eventId event id to notify
|
|
3481
|
+
* @param eventId event id to notify. Only letters, digits, "-", "_" and "." are allowed.
|
|
3482
|
+
* QStash rejects other characters, such as ":", which stops the run with only a
|
|
3483
|
+
* "Tried to append to a cancelled workflow" warning.
|
|
3407
3484
|
* @param eventData event data to notify with
|
|
3408
|
-
* @param workflowRunId optional workflow run id for lookback
|
|
3409
|
-
*
|
|
3485
|
+
* @param workflowRunId optional workflow run id for lookback: the event is kept for the run
|
|
3486
|
+
* even if it has not reached `waitForEvent` yet
|
|
3487
|
+
* @returns notify response which has event id, event data and list of waiters which were
|
|
3488
|
+
* notified. With `workflowRunId`, QStash returns `notifyResponse` as a single object instead
|
|
3489
|
+
* of an array, despite the type.
|
|
3490
|
+
* @see node_modules/@upstash/workflow/docs/steps/notify.mdx
|
|
3410
3491
|
*/
|
|
3411
3492
|
async notify(stepName, eventId, eventData, workflowRunId) {
|
|
3412
3493
|
return await this.addStep(
|
|
3413
3494
|
new LazyNotifyStep(this, stepName, eventId, eventData, this.qstashClient.http, workflowRunId)
|
|
3414
3495
|
);
|
|
3415
3496
|
}
|
|
3497
|
+
/**
|
|
3498
|
+
* Starts another workflow run and waits for its result.
|
|
3499
|
+
*
|
|
3500
|
+
* @see node_modules/@upstash/workflow/docs/steps/invoke.mdx
|
|
3501
|
+
*/
|
|
3416
3502
|
async invoke(stepName, settings) {
|
|
3417
3503
|
validateLabel(settings.label);
|
|
3418
3504
|
validateFlowControl(settings.flowControl);
|
|
@@ -3420,9 +3506,15 @@ var WorkflowContext = class {
|
|
|
3420
3506
|
new LazyInvokeStep(this, stepName, settings)
|
|
3421
3507
|
);
|
|
3422
3508
|
}
|
|
3509
|
+
/**
|
|
3510
|
+
* @see node_modules/@upstash/workflow/docs/steps/createWebhook.mdx
|
|
3511
|
+
*/
|
|
3423
3512
|
async createWebhook(stepName) {
|
|
3424
3513
|
return await this.addStep(new LazyCreateWebhookStep(this, stepName));
|
|
3425
3514
|
}
|
|
3515
|
+
/**
|
|
3516
|
+
* @see node_modules/@upstash/workflow/docs/steps/waitForWebhook.mdx
|
|
3517
|
+
*/
|
|
3426
3518
|
async waitForWebhook(stepName, webhook, timeout) {
|
|
3427
3519
|
return await this.addStep(new LazyWaitForWebhookStep(this, stepName, webhook, timeout));
|
|
3428
3520
|
}
|
|
@@ -3431,6 +3523,8 @@ var WorkflowContext = class {
|
|
|
3431
3523
|
*
|
|
3432
3524
|
* Will throw WorkflowCancelAbort to stop workflow execution.
|
|
3433
3525
|
* Shouldn't be inside try/catch.
|
|
3526
|
+
*
|
|
3527
|
+
* @see node_modules/@upstash/workflow/docs/steps/cancel.mdx
|
|
3434
3528
|
*/
|
|
3435
3529
|
async cancel() {
|
|
3436
3530
|
throw new WorkflowCancelAbort();
|
|
@@ -3449,6 +3543,11 @@ var WorkflowContext = class {
|
|
|
3449
3543
|
throw error;
|
|
3450
3544
|
}
|
|
3451
3545
|
}
|
|
3546
|
+
/**
|
|
3547
|
+
* Calls OpenAI, Anthropic or Resend through QStash, like `context.call`.
|
|
3548
|
+
*
|
|
3549
|
+
* @see node_modules/@upstash/workflow/docs/steps/api.mdx
|
|
3550
|
+
*/
|
|
3452
3551
|
get api() {
|
|
3453
3552
|
return new WorkflowApi({
|
|
3454
3553
|
context: this
|
|
@@ -4231,7 +4330,8 @@ var serveBase = (routeFunction, telemetry2, options, internalOptions) => {
|
|
|
4231
4330
|
const invokeCount = Number(request.headers.get(WORKFLOW_INVOKE_COUNT_HEADER) ?? "0");
|
|
4232
4331
|
const retried = Number(request.headers.get(WORKFLOW_RETRIED_HEADER) ?? "0");
|
|
4233
4332
|
const label = request.headers.get(WORKFLOW_LABEL_HEADER) ?? void 0;
|
|
4234
|
-
const
|
|
4333
|
+
const workflowRunCreatedAtHeader = request.headers.get(WORKFLOW_CREATED_AT_HEADER);
|
|
4334
|
+
const workflowRunCreatedAt = workflowRunCreatedAtHeader ? Number(workflowRunCreatedAtHeader) : isFirstInvocation ? Date.now() : 0;
|
|
4235
4335
|
const effectiveConfig = getEffectiveConfig(request.headers);
|
|
4236
4336
|
const workflowContext = new WorkflowContext({
|
|
4237
4337
|
qstashClient: regionalClient,
|
|
@@ -4246,7 +4346,7 @@ var serveBase = (routeFunction, telemetry2, options, internalOptions) => {
|
|
|
4246
4346
|
invokeCount,
|
|
4247
4347
|
label,
|
|
4248
4348
|
retried,
|
|
4249
|
-
workflowRunCreatedAt
|
|
4349
|
+
workflowRunCreatedAt,
|
|
4250
4350
|
middlewareManager
|
|
4251
4351
|
});
|
|
4252
4352
|
const authCheck = await DisabledWorkflowContext.tryAuthentication(
|