@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
|
@@ -0,0 +1,307 @@
|
|
|
1
|
+
import { Client, QstashError } from "@upstash/qstash";
|
|
2
|
+
import { NotifyResponse, RawStep, Waiter } from "../types";
|
|
3
|
+
import { isInstanceOf } from "../error";
|
|
4
|
+
import { DispatchDebug } from "../middleware/types";
|
|
5
|
+
import { WorkflowDLQActionFilters, WorkflowRunCancelFilters } from "./filter-types";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Guards single-resource endpoints against an empty identifier.
|
|
9
|
+
*
|
|
10
|
+
* An empty id would otherwise either be joined into the path as a trailing
|
|
11
|
+
* slash (e.g. `v2/waiters/`) or sent as an empty value in a bulk action
|
|
12
|
+
* filter (e.g. `dlqIds=`), both of which the server resolves to a
|
|
13
|
+
* collection/bulk operation. For destructive methods this silently affects
|
|
14
|
+
* unintended resources, so we fail fast instead.
|
|
15
|
+
*
|
|
16
|
+
* The check is a falsy guard rather than `id.length === 0` so that a plain-JS
|
|
17
|
+
* consumer passing `undefined`/`null` gets the same `QstashError` instead of a
|
|
18
|
+
* `TypeError` from reading `.length`.
|
|
19
|
+
*/
|
|
20
|
+
export const assertNonEmptyId = (id: string, label = "id"): void => {
|
|
21
|
+
if (!id) {
|
|
22
|
+
throw new QstashError(`${label} cannot be empty`);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Normalizes a single id or array of ids into an array, asserting that every
|
|
28
|
+
* id in it is non-empty.
|
|
29
|
+
*
|
|
30
|
+
* An empty id placed in a bulk filter (e.g. `workflowRunIds=` or `dlqIds=`) is
|
|
31
|
+
* resolved server-side as a collection/bulk operation, so passing `[""]` would
|
|
32
|
+
* silently affect unintended resources. We fail fast on it instead. An empty
|
|
33
|
+
* array is left untouched (callers treat it as a no-op).
|
|
34
|
+
*/
|
|
35
|
+
export const toNonEmptyIdArray = (request: string | string[], label = "id"): string[] => {
|
|
36
|
+
const ids = typeof request === "string" ? [request] : request;
|
|
37
|
+
for (const id of ids) assertNonEmptyId(id, label);
|
|
38
|
+
return ids;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Makes a request to notify waiting workflows.
|
|
43
|
+
*
|
|
44
|
+
* @param requester QStash HTTP requester
|
|
45
|
+
* @param eventId event ID to notify
|
|
46
|
+
* @param eventData optional event data to send
|
|
47
|
+
* @param workflowRunId optional workflow run ID for lookback support
|
|
48
|
+
*/
|
|
49
|
+
export const makeNotifyRequest = async (
|
|
50
|
+
requester: Client["http"],
|
|
51
|
+
eventId: string,
|
|
52
|
+
eventData?: unknown,
|
|
53
|
+
workflowRunId?: string
|
|
54
|
+
): Promise<NotifyResponse[]> => {
|
|
55
|
+
assertNonEmptyId(eventId, "Event id");
|
|
56
|
+
if (workflowRunId !== undefined) assertNonEmptyId(workflowRunId, "Workflow run id");
|
|
57
|
+
const path =
|
|
58
|
+
workflowRunId === undefined
|
|
59
|
+
? ["v2", "notify", eventId]
|
|
60
|
+
: ["v2", "notify", workflowRunId, eventId];
|
|
61
|
+
|
|
62
|
+
const result = (await requester.request({
|
|
63
|
+
path,
|
|
64
|
+
method: "POST",
|
|
65
|
+
body: typeof eventData === "string" ? eventData : JSON.stringify(eventData),
|
|
66
|
+
})) as NotifyResponse[];
|
|
67
|
+
|
|
68
|
+
return result;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Gets waiters for a specific event.
|
|
73
|
+
*
|
|
74
|
+
* @param requester QStash HTTP requester
|
|
75
|
+
* @param eventId event ID to check waiters for
|
|
76
|
+
*/
|
|
77
|
+
export const makeGetWaitersRequest = async (
|
|
78
|
+
requester: Client["http"],
|
|
79
|
+
eventId: string
|
|
80
|
+
): Promise<Required<Waiter>[]> => {
|
|
81
|
+
assertNonEmptyId(eventId, "Event id");
|
|
82
|
+
const result = (await requester.request({
|
|
83
|
+
path: ["v2", "waiters", eventId],
|
|
84
|
+
method: "GET",
|
|
85
|
+
})) as Required<Waiter>[];
|
|
86
|
+
return result;
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Returns true if workflow is canceled succesfully. Otherwise, throws error.
|
|
91
|
+
*
|
|
92
|
+
* @param requester client.http
|
|
93
|
+
* @param workflowRunId workflow to cancel
|
|
94
|
+
* @returns true if workflow is canceled
|
|
95
|
+
*/
|
|
96
|
+
export const makeCancelRequest = async (requester: Client["http"], workflowRunId: string) => {
|
|
97
|
+
(await requester.request({
|
|
98
|
+
path: ["v2", "workflows", "runs", `${workflowRunId}?cancel=true`],
|
|
99
|
+
method: "DELETE",
|
|
100
|
+
parseResponseAsJson: false,
|
|
101
|
+
})) as undefined;
|
|
102
|
+
return true;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* fetches steps from QStash for lazy fetch feature.
|
|
107
|
+
*
|
|
108
|
+
* @param requester to call QStash
|
|
109
|
+
* @param workflowRunId id of the workflow run
|
|
110
|
+
* @param messageId message id being called. Only the steps until this messageId will be returned
|
|
111
|
+
* step with the provided messageId is included.
|
|
112
|
+
* @param debug logger
|
|
113
|
+
* @returns the steps if the run exists. otherwise returns workflowRunEnded: true.
|
|
114
|
+
*/
|
|
115
|
+
export const getSteps = async (
|
|
116
|
+
requester: Client["http"],
|
|
117
|
+
workflowRunId: string,
|
|
118
|
+
messageId?: string,
|
|
119
|
+
dispatchDebug?: DispatchDebug
|
|
120
|
+
): Promise<
|
|
121
|
+
{ steps: RawStep[]; workflowRunEnded: false } | { steps: undefined; workflowRunEnded: true }
|
|
122
|
+
> => {
|
|
123
|
+
try {
|
|
124
|
+
const steps = (await requester.request({
|
|
125
|
+
path: ["v2", "workflows", "runs", workflowRunId],
|
|
126
|
+
parseResponseAsJson: true,
|
|
127
|
+
})) as RawStep[];
|
|
128
|
+
|
|
129
|
+
if (steps.length === 1) {
|
|
130
|
+
/**
|
|
131
|
+
* If the user call /trigger and passes a large body, lazyFetch will be activated
|
|
132
|
+
* and getSteps will be called.
|
|
133
|
+
*
|
|
134
|
+
* In this case, there will be only the one step in the workflow run, but messageId
|
|
135
|
+
* will be diffent from the one in the headers.
|
|
136
|
+
*/
|
|
137
|
+
return {
|
|
138
|
+
steps,
|
|
139
|
+
workflowRunEnded: false,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (!messageId) {
|
|
144
|
+
await dispatchDebug?.("onInfo", {
|
|
145
|
+
info:
|
|
146
|
+
`Pulled ${steps.length} steps from QStash` +
|
|
147
|
+
`and returned them without filtering with messageId.`,
|
|
148
|
+
});
|
|
149
|
+
return { steps, workflowRunEnded: false };
|
|
150
|
+
} else {
|
|
151
|
+
const index = steps.findIndex((item) => item.messageId === messageId);
|
|
152
|
+
|
|
153
|
+
if (index === -1) {
|
|
154
|
+
// targetMessageId not found, return an empty array or handle it as needed
|
|
155
|
+
return { steps: [], workflowRunEnded: false };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const filteredSteps = steps.slice(0, index + 1);
|
|
159
|
+
await dispatchDebug?.("onInfo", {
|
|
160
|
+
info:
|
|
161
|
+
`Pulled ${steps.length} steps from QStash` +
|
|
162
|
+
` and filtered them to ${filteredSteps.length} using messageId.`,
|
|
163
|
+
});
|
|
164
|
+
return { steps: filteredSteps, workflowRunEnded: false };
|
|
165
|
+
}
|
|
166
|
+
} catch (error) {
|
|
167
|
+
if (isInstanceOf(error, QstashError) && error.status === 404) {
|
|
168
|
+
await dispatchDebug?.("onWarning", {
|
|
169
|
+
warning:
|
|
170
|
+
"Couldn't fetch workflow run steps. This can happen if the workflow run succesfully ends before some callback is executed.",
|
|
171
|
+
});
|
|
172
|
+
return { steps: undefined, workflowRunEnded: true };
|
|
173
|
+
} else {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Normalizes a response cursor: converts empty string to `undefined`
|
|
181
|
+
* so that callers can reliably use `cursor` as a boolean presence check.
|
|
182
|
+
*/
|
|
183
|
+
export function normalizeCursor<T>(response: T): T {
|
|
184
|
+
const cursor = (response as { cursor?: string }).cursor;
|
|
185
|
+
return { ...response, cursor: cursor || undefined };
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const DEFAULT_BULK_COUNT = 100;
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Builds query parameters for bulk actions (DLQ resume/restart/delete and workflow cancel).
|
|
192
|
+
*
|
|
193
|
+
* Validates that ID arrays are not empty and applies a default `count` of 100
|
|
194
|
+
* for filter-based and `{ all: true }` operations.
|
|
195
|
+
*
|
|
196
|
+
* @example DLQ action with dlqIds
|
|
197
|
+
* ```ts
|
|
198
|
+
* buildBulkActionQueryParameters({ dlqIds: ["dlq_1", "dlq_2"] })
|
|
199
|
+
* // => { dlqIds: ["dlq_1", "dlq_2"] }
|
|
200
|
+
* ```
|
|
201
|
+
*
|
|
202
|
+
* @example DLQ action targeting all with custom count
|
|
203
|
+
* ```ts
|
|
204
|
+
* buildBulkActionQueryParameters({ all: true, count: 50 })
|
|
205
|
+
* // => { all: true, count: 50 }
|
|
206
|
+
* ```
|
|
207
|
+
*
|
|
208
|
+
* @example Cancel with workflowRunIds
|
|
209
|
+
* ```ts
|
|
210
|
+
* buildBulkActionQueryParameters({ workflowRunIds: ["wfr_1", "wfr_2"] })
|
|
211
|
+
* // => { workflowRunIds: ["wfr_1", "wfr_2"] }
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @example Cancel targeting all (uses default count of 100)
|
|
215
|
+
* ```ts
|
|
216
|
+
* buildBulkActionQueryParameters({ all: true })
|
|
217
|
+
* // => { all: true, count: 100 }
|
|
218
|
+
* ```
|
|
219
|
+
*
|
|
220
|
+
* @throws {QstashError} If an empty `dlqIds` or `workflowRunIds` array is provided,
|
|
221
|
+
* or if any filter field is an empty array
|
|
222
|
+
*/
|
|
223
|
+
export function buildBulkActionQueryParameters(
|
|
224
|
+
request: WorkflowDLQActionFilters | WorkflowRunCancelFilters,
|
|
225
|
+
options?: { translateWorkflowUrl?: boolean }
|
|
226
|
+
) {
|
|
227
|
+
const cursor = "cursor" in request ? request.cursor : undefined;
|
|
228
|
+
|
|
229
|
+
if ("all" in request) {
|
|
230
|
+
return { count: request.count ?? DEFAULT_BULK_COUNT, cursor };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
if ("dlqIds" in request) {
|
|
234
|
+
const ids = request.dlqIds;
|
|
235
|
+
if (Array.isArray(ids) && ids.length === 0) {
|
|
236
|
+
throw new QstashError(
|
|
237
|
+
"Empty dlqIds array provided. If you intend to target all DLQ messages, use { all: true } explicitly."
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
return { dlqIds: ids, cursor };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
if ("workflowRunIds" in request && request.workflowRunIds) {
|
|
244
|
+
if (request.workflowRunIds.length === 0) {
|
|
245
|
+
throw new QstashError(
|
|
246
|
+
"Empty workflowRunIds array provided. If you intend to target all workflow runs, use { all: true } explicitly."
|
|
247
|
+
);
|
|
248
|
+
}
|
|
249
|
+
return { workflowRunIds: request.workflowRunIds };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
// Filter branch
|
|
253
|
+
const filter = request.filter as Record<string, unknown> | undefined;
|
|
254
|
+
|
|
255
|
+
if (!filter) {
|
|
256
|
+
throw new QstashError(
|
|
257
|
+
"No filter provided. Use { filter: { ... } } with at least one filter field, or { all: true }."
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// An empty array serializes to no query parameter at all, silently widening
|
|
262
|
+
// the scope of a destructive bulk action. Fail fast instead.
|
|
263
|
+
for (const [field, value] of Object.entries(filter)) {
|
|
264
|
+
if (Array.isArray(value) && value.length === 0) {
|
|
265
|
+
throw new QstashError(
|
|
266
|
+
`Empty array provided for filter field '${field}'. ` +
|
|
267
|
+
"If you intend to target all records, use { all: true } explicitly."
|
|
268
|
+
);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// When translateWorkflowUrl is set (cancel filters), translate
|
|
273
|
+
// workflowUrl/workflowUrlStartingWith into the server's query params:
|
|
274
|
+
// - workflowUrl → workflowUrl + workflowUrlExactMatch=true (exact match)
|
|
275
|
+
// - workflowUrlStartingWith → workflowUrl (prefix match, server default)
|
|
276
|
+
if (options?.translateWorkflowUrl) {
|
|
277
|
+
const { workflowUrlStartingWith, workflowUrl, ...rest } = filter;
|
|
278
|
+
|
|
279
|
+
if (workflowUrlStartingWith && workflowUrl) {
|
|
280
|
+
throw new QstashError(
|
|
281
|
+
"workflowUrl and workflowUrlStartingWith are mutually exclusive. " +
|
|
282
|
+
"Use workflowUrl for exact match or workflowUrlStartingWith for prefix match."
|
|
283
|
+
);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const urlParams: Record<string, string | string[] | boolean> = {};
|
|
287
|
+
if (workflowUrlStartingWith) {
|
|
288
|
+
urlParams.workflowUrl = workflowUrlStartingWith as string | string[];
|
|
289
|
+
} else if (workflowUrl) {
|
|
290
|
+
urlParams.workflowUrl = workflowUrl as string | string[];
|
|
291
|
+
urlParams.workflowUrlExactMatch = true;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
return {
|
|
295
|
+
...rest,
|
|
296
|
+
...urlParams,
|
|
297
|
+
count: request.count ?? DEFAULT_BULK_COUNT,
|
|
298
|
+
cursor,
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
return {
|
|
303
|
+
...filter,
|
|
304
|
+
count: request.count ?? DEFAULT_BULK_COUNT,
|
|
305
|
+
cursor,
|
|
306
|
+
};
|
|
307
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { Telemetry } from "./types";
|
|
2
|
+
|
|
3
|
+
export const WORKFLOW_ID_HEADER = "Upstash-Workflow-RunId";
|
|
4
|
+
export const WORKFLOW_INIT_HEADER = "Upstash-Workflow-Init";
|
|
5
|
+
export const WORKFLOW_URL_HEADER = "Upstash-Workflow-Url";
|
|
6
|
+
export const WORKFLOW_CREATED_AT_HEADER = "Upstash-Workflow-CreatedAt";
|
|
7
|
+
export const WORKFLOW_FAILURE_HEADER = "Upstash-Workflow-Is-Failure";
|
|
8
|
+
export const WORKFLOW_FAILURE_CALLBACK_HEADER = "Upstash-Workflow-Failure-Callback";
|
|
9
|
+
export const WORKFLOW_FEATURE_HEADER = "Upstash-Feature-Set";
|
|
10
|
+
export const WORKFLOW_FEATURE_SET = "LazyFetch,InitialBody,WF_DetectTrigger,WF_TriggerOnConfig";
|
|
11
|
+
/**
|
|
12
|
+
* call type of the step config request: a hidden helper request which
|
|
13
|
+
* makes QStash call the workflow endpoint again with step-level settings
|
|
14
|
+
* applied. QStash doesn't treat it as a step and hides it from the step
|
|
15
|
+
* logs.
|
|
16
|
+
*
|
|
17
|
+
* The SDK publishes one when it is about to execute a step which has
|
|
18
|
+
* step-level settings in an ordinary delivery.
|
|
19
|
+
*/
|
|
20
|
+
export const WORKFLOW_STEP_CONFIG_CALL_TYPE = "stepConfig";
|
|
21
|
+
/**
|
|
22
|
+
* header which marks a delivery whose message was published with
|
|
23
|
+
* step-level settings.
|
|
24
|
+
*
|
|
25
|
+
* Set by QStash from the message's own feature set, so it cannot
|
|
26
|
+
* disagree with what QStash actually did. Its presence forbids
|
|
27
|
+
* publishing another step config request: if the settings still don't
|
|
28
|
+
* match what the step asked for, the step is executed with the wrong
|
|
29
|
+
* settings (and a warning) instead of looping invisibly.
|
|
30
|
+
*/
|
|
31
|
+
export const WORKFLOW_STEP_CONFIG_HEADER = "Upstash-Workflow-Step-Config";
|
|
32
|
+
/**
|
|
33
|
+
* headers through which QStash reports the configuration it applied to
|
|
34
|
+
* the delivery in hand (the "effective configuration").
|
|
35
|
+
*/
|
|
36
|
+
export const FLOW_CONTROL_KEY_HEADER = "Upstash-Flow-Control-Key";
|
|
37
|
+
export const FLOW_CONTROL_VALUE_HEADER = "Upstash-Flow-Control-Value";
|
|
38
|
+
/**
|
|
39
|
+
* the retry limit of a message: it sets the limit when publishing, and
|
|
40
|
+
* reports the limit QStash applied when read off a delivery.
|
|
41
|
+
*
|
|
42
|
+
* Not to be confused with `WORKFLOW_RETRIED_HEADER`, which is how many
|
|
43
|
+
* retries have already happened. QStash sets this one last and
|
|
44
|
+
* unconditionally on a delivery, so an absent header means the QStash
|
|
45
|
+
* version does not report the limit rather than "the limit is zero" —
|
|
46
|
+
* zero being a valid limit.
|
|
47
|
+
*/
|
|
48
|
+
export const RETRIES_HEADER = "Upstash-Retries";
|
|
49
|
+
export const RETRY_DELAY_HEADER = "Upstash-Retry-Delay";
|
|
50
|
+
/**
|
|
51
|
+
* feature added to the feature set of a message which carries step-level
|
|
52
|
+
* settings (flow control, retries etc.). Signals QStash to use the message's
|
|
53
|
+
* own settings instead of the settings the workflow run was triggered with.
|
|
54
|
+
*/
|
|
55
|
+
export const WORKFLOW_STEP_CONFIG_FEATURE = "WF_StepConfig";
|
|
56
|
+
export const WORKFLOW_INVOKE_COUNT_HEADER = "Upstash-Workflow-Invoke-Count";
|
|
57
|
+
export const WORKFLOW_RETRIED_HEADER = "Upstash-Retried";
|
|
58
|
+
export const WORKFLOW_LABEL_HEADER = "Upstash-Label";
|
|
59
|
+
export const WORKFLOW_UNKOWN_SDK_VERSION_HEADER = "Upstash-Workflow-Unknown-Sdk";
|
|
60
|
+
export const WORKFLOW_UNKOWN_SDK_TRIGGER_HEADER = "upstash-workflow-trigger-by-sdk";
|
|
61
|
+
export const WORKFLOW_ERROR_STEP_NAME_HEADER = "Upstash-Error-Step-Name";
|
|
62
|
+
|
|
63
|
+
export const WORKFLOW_PROTOCOL_VERSION = "1";
|
|
64
|
+
export const WORKFLOW_PROTOCOL_VERSION_HEADER = "Upstash-Workflow-Sdk-Version";
|
|
65
|
+
|
|
66
|
+
export const DEFAULT_CONTENT_TYPE = "application/json";
|
|
67
|
+
|
|
68
|
+
export const NO_CONCURRENCY = 1;
|
|
69
|
+
export const NOT_SET = "not-set";
|
|
70
|
+
export const DEFAULT_RETRIES = 3;
|
|
71
|
+
|
|
72
|
+
export const VERSION = "v1.3.2";
|
|
73
|
+
export const SDK_TELEMETRY = `@upstash/workflow@${VERSION}`;
|
|
74
|
+
|
|
75
|
+
export const TELEMETRY_HEADER_SDK = "Upstash-Telemetry-Sdk" as const;
|
|
76
|
+
export const TELEMETRY_HEADER_FRAMEWORK = "Upstash-Telemetry-Framework" as const;
|
|
77
|
+
export const TELEMETRY_HEADER_RUNTIME = "Upstash-Telemetry-Runtime" as const;
|
|
78
|
+
|
|
79
|
+
export const MOCK_TELEMETRY: Telemetry = {
|
|
80
|
+
framework: "mock",
|
|
81
|
+
sdk: "mock",
|
|
82
|
+
};
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { anthropic } from "@upstash/qstash";
|
|
2
|
+
import { ApiCallSettings, BaseWorkflowApi } from "./base";
|
|
3
|
+
import { CallResponse } from "../../types";
|
|
4
|
+
|
|
5
|
+
type CreateChatCompletion = {
|
|
6
|
+
model: string;
|
|
7
|
+
messages: { role: "user" | "assistant"; content: unknown }[];
|
|
8
|
+
max_tokens: number;
|
|
9
|
+
metadata?: object;
|
|
10
|
+
stop_sequences?: string[];
|
|
11
|
+
/**
|
|
12
|
+
* streaming is not possible Upstash Workflow.
|
|
13
|
+
*/
|
|
14
|
+
stream?: false;
|
|
15
|
+
system?: string;
|
|
16
|
+
temparature?: number;
|
|
17
|
+
top_k?: number;
|
|
18
|
+
top_p?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type ChatCompletion = {
|
|
22
|
+
id: string;
|
|
23
|
+
type: "message";
|
|
24
|
+
role: "assistant";
|
|
25
|
+
content: { type: "text"; text: string }[];
|
|
26
|
+
model: string;
|
|
27
|
+
stop_reasong: string;
|
|
28
|
+
stop_sequence: string[];
|
|
29
|
+
usage: unknown;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export class AnthropicAPI extends BaseWorkflowApi {
|
|
33
|
+
/**
|
|
34
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
35
|
+
*
|
|
36
|
+
* @see node_modules/@upstash/workflow/docs/integrations/anthropic.mdx
|
|
37
|
+
*/
|
|
38
|
+
public async call<TResult = ChatCompletion, TBody = CreateChatCompletion>(
|
|
39
|
+
stepName: string,
|
|
40
|
+
settings: ApiCallSettings<
|
|
41
|
+
TBody,
|
|
42
|
+
{
|
|
43
|
+
token: string;
|
|
44
|
+
operation: "messages.create";
|
|
45
|
+
}
|
|
46
|
+
>
|
|
47
|
+
): Promise<CallResponse<TResult>> {
|
|
48
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
49
|
+
const { token, operation, ...parameters } = settings;
|
|
50
|
+
return await this.callApi<TResult, TBody>(stepName, {
|
|
51
|
+
api: {
|
|
52
|
+
name: "llm",
|
|
53
|
+
provider: anthropic({ token }),
|
|
54
|
+
},
|
|
55
|
+
...parameters,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { CallResponse, CallSettings } from "../../types";
|
|
2
|
+
import { WorkflowContext } from "../context";
|
|
3
|
+
import { getProviderInfo } from "../provider";
|
|
4
|
+
|
|
5
|
+
export type ApiCallSettings<TBody = unknown, TFields extends object = object> = Omit<
|
|
6
|
+
CallSettings,
|
|
7
|
+
"url" | "body"
|
|
8
|
+
> & { body: TBody } & TFields;
|
|
9
|
+
|
|
10
|
+
export abstract class BaseWorkflowApi {
|
|
11
|
+
protected context: WorkflowContext;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* @param context workflow context
|
|
15
|
+
*/
|
|
16
|
+
constructor({ context }: { context: WorkflowContext }) {
|
|
17
|
+
this.context = context;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* context.call which uses a QStash API
|
|
22
|
+
*
|
|
23
|
+
* @param stepName name of the step
|
|
24
|
+
* @param settings call settings including api configuration
|
|
25
|
+
* @returns call response
|
|
26
|
+
*/
|
|
27
|
+
protected async callApi<TResult = unknown, TBody = unknown>(
|
|
28
|
+
stepName: string,
|
|
29
|
+
settings: ApiCallSettings<
|
|
30
|
+
TBody,
|
|
31
|
+
{
|
|
32
|
+
api: Parameters<typeof getProviderInfo>[0];
|
|
33
|
+
}
|
|
34
|
+
>
|
|
35
|
+
): Promise<CallResponse<TResult>> {
|
|
36
|
+
const { url, appendHeaders, method } = getProviderInfo(settings.api);
|
|
37
|
+
const { method: userMethod, body, headers = {}, retries = 0, retryDelay, timeout } = settings;
|
|
38
|
+
|
|
39
|
+
return await this.context.call<TResult>(stepName, {
|
|
40
|
+
url,
|
|
41
|
+
method: userMethod ?? method,
|
|
42
|
+
body: typeof body === "string" ? body : JSON.stringify(body),
|
|
43
|
+
headers: {
|
|
44
|
+
...appendHeaders,
|
|
45
|
+
...headers,
|
|
46
|
+
},
|
|
47
|
+
retries,
|
|
48
|
+
retryDelay,
|
|
49
|
+
timeout,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { AnthropicAPI } from "./anthropic";
|
|
2
|
+
import { BaseWorkflowApi } from "./base";
|
|
3
|
+
import { OpenAIAPI } from "./openai";
|
|
4
|
+
import { ResendAPI } from "./resend";
|
|
5
|
+
|
|
6
|
+
export class WorkflowApi extends BaseWorkflowApi {
|
|
7
|
+
public get openai() {
|
|
8
|
+
return new OpenAIAPI({
|
|
9
|
+
context: this.context,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
public get resend() {
|
|
14
|
+
return new ResendAPI({
|
|
15
|
+
context: this.context,
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public get anthropic() {
|
|
20
|
+
return new AnthropicAPI({
|
|
21
|
+
context: this.context,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { custom, openai } from "@upstash/qstash";
|
|
2
|
+
import { ApiCallSettings, BaseWorkflowApi } from "./base";
|
|
3
|
+
import { CallResponse } from "../../types";
|
|
4
|
+
|
|
5
|
+
type Messages =
|
|
6
|
+
| {
|
|
7
|
+
content: string;
|
|
8
|
+
role: "developer" | "system";
|
|
9
|
+
name?: string;
|
|
10
|
+
}
|
|
11
|
+
| {
|
|
12
|
+
content: unknown;
|
|
13
|
+
role: "user";
|
|
14
|
+
name?: string;
|
|
15
|
+
}
|
|
16
|
+
| {
|
|
17
|
+
content: unknown;
|
|
18
|
+
refusal?: string;
|
|
19
|
+
role: "assistant";
|
|
20
|
+
name?: string;
|
|
21
|
+
audio?: unknown;
|
|
22
|
+
tool_calls?: unknown;
|
|
23
|
+
}
|
|
24
|
+
| {
|
|
25
|
+
role: "tool";
|
|
26
|
+
content: string | unknown;
|
|
27
|
+
tool_call_id: string;
|
|
28
|
+
}
|
|
29
|
+
| {
|
|
30
|
+
role: "function";
|
|
31
|
+
content: string | undefined;
|
|
32
|
+
name: string;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
type CreateChatCompletion = {
|
|
36
|
+
messages: Messages[];
|
|
37
|
+
model: string;
|
|
38
|
+
store?: boolean;
|
|
39
|
+
reasoning_effort?: string;
|
|
40
|
+
metadata?: unknown;
|
|
41
|
+
frequency_penalty?: number;
|
|
42
|
+
logit_bias?: Record<string, number>;
|
|
43
|
+
logprobs?: boolean;
|
|
44
|
+
top_logprobs?: number;
|
|
45
|
+
max_completion_tokens?: number;
|
|
46
|
+
n?: number;
|
|
47
|
+
modalities?: string[];
|
|
48
|
+
prediction?: unknown;
|
|
49
|
+
audio?: unknown;
|
|
50
|
+
presence_penalty?: number;
|
|
51
|
+
response_format?: unknown;
|
|
52
|
+
seed?: number;
|
|
53
|
+
service_tier?: string;
|
|
54
|
+
stop?: string | string[];
|
|
55
|
+
/**
|
|
56
|
+
* streaming is not supported in Upstash Workflow.
|
|
57
|
+
*/
|
|
58
|
+
stream?: false;
|
|
59
|
+
temperature?: number;
|
|
60
|
+
top_p?: number;
|
|
61
|
+
tools?: unknown;
|
|
62
|
+
tool_choice?: string;
|
|
63
|
+
parallel_tool_calls?: boolean;
|
|
64
|
+
user?: string;
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
type ChatCompletion = {
|
|
68
|
+
id: string;
|
|
69
|
+
choices: ChatCompletionChoice[];
|
|
70
|
+
created: number;
|
|
71
|
+
model: string;
|
|
72
|
+
object: "chat.completion";
|
|
73
|
+
service_tier?: "scale" | "default" | null;
|
|
74
|
+
system_fingerprint?: string;
|
|
75
|
+
usage?: unknown;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
type ChatCompletionChoice = {
|
|
79
|
+
finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | "function_call";
|
|
80
|
+
index: number;
|
|
81
|
+
logprobs: unknown;
|
|
82
|
+
message: {
|
|
83
|
+
content: string | null;
|
|
84
|
+
refusal: string | null;
|
|
85
|
+
role: "assistant";
|
|
86
|
+
audio?: unknown;
|
|
87
|
+
tool_calls?: unknown;
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export class OpenAIAPI extends BaseWorkflowApi {
|
|
92
|
+
/**
|
|
93
|
+
* Runs as a `context.call` step, so the `context.call` caveats apply.
|
|
94
|
+
*
|
|
95
|
+
* @see node_modules/@upstash/workflow/docs/integrations/openai.mdx
|
|
96
|
+
*/
|
|
97
|
+
public async call<TResult = ChatCompletion, TBody = CreateChatCompletion>(
|
|
98
|
+
stepName: string,
|
|
99
|
+
settings: ApiCallSettings<
|
|
100
|
+
TBody,
|
|
101
|
+
{
|
|
102
|
+
token: string;
|
|
103
|
+
organization?: string;
|
|
104
|
+
baseURL?: string;
|
|
105
|
+
operation: "chat.completions.create";
|
|
106
|
+
}
|
|
107
|
+
>
|
|
108
|
+
): Promise<CallResponse<TResult>> {
|
|
109
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
110
|
+
const { token, organization, operation, baseURL, ...parameters } = settings;
|
|
111
|
+
|
|
112
|
+
const useOpenAI = baseURL === undefined;
|
|
113
|
+
const provider = useOpenAI
|
|
114
|
+
? openai({ token, organization })
|
|
115
|
+
: custom({ baseUrl: baseURL, token });
|
|
116
|
+
|
|
117
|
+
return await this.callApi<TResult, TBody>(stepName, {
|
|
118
|
+
api: {
|
|
119
|
+
name: "llm",
|
|
120
|
+
provider,
|
|
121
|
+
},
|
|
122
|
+
...parameters,
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|