@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
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Vercel AI SDK
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
<Note>
|
|
6
|
+
This feature is not yet available in
|
|
7
|
+
[workflow-py](https://github.com/upstash/workflow-py). See our
|
|
8
|
+
[Roadmap](/workflow/roadmap) for feature parity plans and
|
|
9
|
+
[Changelog](/workflow/changelog) for updates.
|
|
10
|
+
</Note>
|
|
11
|
+
|
|
12
|
+
<Card title="GitHub Repository" icon="github" href="https://github.com/upstash/workflow-js/blob/main/examples/nextjs/app/vercel-ai-sdk/route.ts" horizontal>
|
|
13
|
+
You can find the project source code which uses real APIs on Github.
|
|
14
|
+
</Card>
|
|
15
|
+
|
|
16
|
+
Upstash Workflow integrates with the Vercel AI SDK to provide durable and reliable AI applications. This allows you to:
|
|
17
|
+
|
|
18
|
+
- Build resilient AI applications with automatic retries
|
|
19
|
+
- Manage AI operations with workflow steps
|
|
20
|
+
- Implement tools and function calling with durability
|
|
21
|
+
- Handle errors gracefully across your AI operations
|
|
22
|
+
- Handle long-running AI operations with extended timeouts
|
|
23
|
+
|
|
24
|
+
This guide will walk you through setting up and implementing AI features using Upstash Workflow's durability guarantees with Vercel AI SDK's capabilities.
|
|
25
|
+
|
|
26
|
+
## Prerequisites
|
|
27
|
+
|
|
28
|
+
Before getting started, make sure you have:
|
|
29
|
+
|
|
30
|
+
- An OpenAI API key
|
|
31
|
+
- Basic familiarity with Upstash Workflow and Vercel AI SDK
|
|
32
|
+
- Vercel AI SDK version 4.0.12 or higher (required for ToolExecutionError handling)
|
|
33
|
+
|
|
34
|
+
## Installation
|
|
35
|
+
|
|
36
|
+
Install the required packages:
|
|
37
|
+
|
|
38
|
+
<CodeGroup>
|
|
39
|
+
```bash npm
|
|
40
|
+
npm install @ai-sdk/openai ai zod
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
```bash pnpm
|
|
44
|
+
pnpm install @ai-sdk/openai ai zod
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
```bash bun
|
|
48
|
+
bun install @ai-sdk/openai ai zod
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
</CodeGroup>
|
|
52
|
+
|
|
53
|
+
## Implementation
|
|
54
|
+
|
|
55
|
+
### Creating OpenAI client
|
|
56
|
+
|
|
57
|
+
AI SDKs (Vercel AI SDK, OpenAI SDK etc.) uses the client's default fetch implementation to make API requests, but allows you to provide a custom fetch implementation.
|
|
58
|
+
|
|
59
|
+
In the case of Upstash Workflow, we need to use the `context.call` method to make HTTP requests. We can create a custom fetch implementation that uses `context.call` to make requests. By using `context.call`, Upstash Workflow is the one making the HTTP request and waiting for the response, even if it takes too long to receive response from the LLM.
|
|
60
|
+
|
|
61
|
+
The following code snippet can also be generalized to work with other LLM SDKs, such as Anthropic or Google.
|
|
62
|
+
|
|
63
|
+
```typescript {18-24}
|
|
64
|
+
import { createOpenAI } from '@ai-sdk/openai';
|
|
65
|
+
import { HTTPMethods } from '@upstash/qstash';
|
|
66
|
+
import { WorkflowAbort, WorkflowContext } from '@upstash/workflow';
|
|
67
|
+
|
|
68
|
+
export const createWorkflowOpenAI = (context: WorkflowContext) => {
|
|
69
|
+
return createOpenAI({
|
|
70
|
+
compatibility: "strict",
|
|
71
|
+
fetch: async (input, init) => {
|
|
72
|
+
try {
|
|
73
|
+
// Prepare headers from init.headers
|
|
74
|
+
const headers = init?.headers
|
|
75
|
+
? Object.fromEntries(new Headers(init.headers).entries())
|
|
76
|
+
: {};
|
|
77
|
+
|
|
78
|
+
// Prepare body from init.body
|
|
79
|
+
const body = init?.body ? JSON.parse(init.body as string) : undefined;
|
|
80
|
+
|
|
81
|
+
// Make network call
|
|
82
|
+
const responseInfo = await context.call("openai-call-step", {
|
|
83
|
+
url: input.toString(),
|
|
84
|
+
method: init?.method as HTTPMethods,
|
|
85
|
+
headers,
|
|
86
|
+
body,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// Construct headers for the response
|
|
90
|
+
const responseHeaders = new Headers(
|
|
91
|
+
Object.entries(responseInfo.header).reduce((acc, [key, values]) => {
|
|
92
|
+
acc[key] = values.join(", ");
|
|
93
|
+
return acc;
|
|
94
|
+
}, {} as Record<string, string>)
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
// Return the constructed response
|
|
98
|
+
return new Response(JSON.stringify(responseInfo.body), {
|
|
99
|
+
status: responseInfo.status,
|
|
100
|
+
headers: responseHeaders,
|
|
101
|
+
});
|
|
102
|
+
} catch (error) {
|
|
103
|
+
if (error instanceof WorkflowAbort) {
|
|
104
|
+
throw error
|
|
105
|
+
} else {
|
|
106
|
+
console.error("Error in fetch implementation:", error);
|
|
107
|
+
throw error; // Rethrow error for further handling
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
},
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Using OpenAI client to generate text
|
|
116
|
+
|
|
117
|
+
Now that we've created the OpenAI client, we can use it to generate the text.
|
|
118
|
+
|
|
119
|
+
For that, we're going to create a new workflow endpoint that uses the payload as prompt to generate text using the OpenAI client.
|
|
120
|
+
|
|
121
|
+
```typescript {8, 16-20}
|
|
122
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
123
|
+
import { WorkflowAbort } from '@upstash/workflow';
|
|
124
|
+
import { generateText, ToolExecutionError } from 'ai';
|
|
125
|
+
|
|
126
|
+
import { createWorkflowOpenAI } from './model';
|
|
127
|
+
|
|
128
|
+
export const { POST } = serve<{ prompt: string }>(async (context) => {
|
|
129
|
+
const openai = createWorkflowOpenAI(context);
|
|
130
|
+
|
|
131
|
+
// Important: Must have a step before generateText
|
|
132
|
+
const prompt = await context.run("get prompt", async () => {
|
|
133
|
+
return context.requestPayload.prompt;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
try {
|
|
137
|
+
const result = await generateText({
|
|
138
|
+
model: openai('gpt-3.5-turbo'),
|
|
139
|
+
maxTokens: 2048,
|
|
140
|
+
prompt,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
await context.run("text", () => {
|
|
144
|
+
console.log(`TEXT: ${result.text}`);
|
|
145
|
+
return result.text;
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
} catch (error) {
|
|
149
|
+
if (error instanceof ToolExecutionError && error.cause instanceof WorkflowAbort) {
|
|
150
|
+
throw error.cause;
|
|
151
|
+
} else {
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
});
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
We can either [run the app locally](/workflow/howto/local-development/development-server) or deploy it. Once the app is running, we can trigger the workflow using the following code:
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { Client } from "@upstash/workflow";
|
|
162
|
+
|
|
163
|
+
const client = new Client({ token: "<QSTASH_TOKEN>" });
|
|
164
|
+
const { workflowRunId } = await client.trigger({
|
|
165
|
+
url: "https://<YOUR_WORKFLOW_ENDPOINT>/<YOUR-WORKFLOW-ROUTE>",
|
|
166
|
+
body: { "prompt": "How is the weather in San Francisco around this time?" }
|
|
167
|
+
});
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
The workflow will execute, and we can view the logs in `the Workflow dashboard`
|
|
171
|
+
|
|
172
|
+
<Frame>
|
|
173
|
+
<img src="/img/qstash-workflow/ai-sdk/without-tool.png" alt="Workflow logs in dashboard" />
|
|
174
|
+
</Frame>
|
|
175
|
+
|
|
176
|
+
### Advanced Implementation with Tools
|
|
177
|
+
|
|
178
|
+
Tools allow the AI model to perform specific actions during text generation. You can learn more about tools in the [Vercel AI SDK documentation](https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling).
|
|
179
|
+
|
|
180
|
+
When using tools with Upstash Workflow, each tool execution must be wrapped in a workflow step.
|
|
181
|
+
|
|
182
|
+
<Info>
|
|
183
|
+
The `maxSteps` parameter must be greater than 1 when using tools to allow the model to process tool results and generate final responses. See the [tool steps documentation](https://sdk.vercel.ai/docs/ai-sdk-core/tools-and-tool-calling#tool-steps) for detailed explanation.
|
|
184
|
+
</Info>
|
|
185
|
+
|
|
186
|
+
```typescript {24-30, 33}
|
|
187
|
+
import { z } from 'zod';
|
|
188
|
+
import { serve } from "@upstash/workflow/nextjs";
|
|
189
|
+
import { WorkflowAbort } from '@upstash/workflow';
|
|
190
|
+
import { generateText, ToolExecutionError, tool } from 'ai';
|
|
191
|
+
|
|
192
|
+
import { createWorkflowOpenAI } from './model';
|
|
193
|
+
|
|
194
|
+
export const { POST } = serve<{ prompt: string }>(async (context) => {
|
|
195
|
+
const openai = createWorkflowOpenAI(context);
|
|
196
|
+
|
|
197
|
+
const prompt = await context.run("get prompt", async () => {
|
|
198
|
+
return context.requestPayload.prompt;
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
try {
|
|
202
|
+
const result = await generateText({
|
|
203
|
+
model: openai('gpt-3.5-turbo'),
|
|
204
|
+
tools: {
|
|
205
|
+
weather: tool({
|
|
206
|
+
description: 'Get the weather in a location',
|
|
207
|
+
parameters: z.object({
|
|
208
|
+
location: z.string().describe('The location to get the weather for'),
|
|
209
|
+
}),
|
|
210
|
+
execute: ({ location }) => context.run("weather tool", () => {
|
|
211
|
+
// Mock data, replace with actual weather API call
|
|
212
|
+
return {
|
|
213
|
+
location,
|
|
214
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
215
|
+
};
|
|
216
|
+
})
|
|
217
|
+
}),
|
|
218
|
+
},
|
|
219
|
+
maxSteps: 2,
|
|
220
|
+
prompt,
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
await context.run("text", () => {
|
|
224
|
+
console.log(`TEXT: ${result.text}`);
|
|
225
|
+
return result.text;
|
|
226
|
+
});
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (error instanceof ToolExecutionError && error.cause instanceof WorkflowAbort) {
|
|
229
|
+
throw error.cause;
|
|
230
|
+
} else {
|
|
231
|
+
throw error;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
When called with the same prompt as above, we will see the following logs:
|
|
238
|
+
|
|
239
|
+
<Frame>
|
|
240
|
+
<img src="/img/qstash-workflow/ai-sdk/with-tool.png" alt="Workflow run that uses a Vercel AI SDK tool" />
|
|
241
|
+
</Frame>
|
|
242
|
+
|
|
243
|
+
## Important Considerations
|
|
244
|
+
|
|
245
|
+
When using Upstash Workflow with the Vercel AI SDK, there are several critical requirements that must be followed:
|
|
246
|
+
|
|
247
|
+
### Step Execution Order
|
|
248
|
+
|
|
249
|
+
The most critical requirement is that `generateText` cannot be called before any workflow step. Always have a step before `generateText`. This could be a step which gets the prompt:
|
|
250
|
+
|
|
251
|
+
<CodeGroup>
|
|
252
|
+
```typescript ❌ Wrong {4}
|
|
253
|
+
export const { POST } = serve<{ prompt: string }>(async (context) => {
|
|
254
|
+
const openai = createWorkflowOpenAI(context);
|
|
255
|
+
|
|
256
|
+
// Will throw "prompt is undefined"
|
|
257
|
+
const result = await generateText({
|
|
258
|
+
model: openai('gpt-3.5-turbo'),
|
|
259
|
+
prompt: context.requestPayload.prompt
|
|
260
|
+
});
|
|
261
|
+
});
|
|
262
|
+
```
|
|
263
|
+
|
|
264
|
+
```typescript ✅ Correct {3-7}
|
|
265
|
+
export const { POST } = serve<{ prompt: string }>(async (context) => {
|
|
266
|
+
const openai = createWorkflowOpenAI(context);
|
|
267
|
+
|
|
268
|
+
// Get prompt in a step first
|
|
269
|
+
const prompt = await context.run("get prompt", async () => {
|
|
270
|
+
return context.requestPayload.prompt;
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const result = await generateText({
|
|
274
|
+
model: openai('gpt-3.5-turbo'),
|
|
275
|
+
prompt
|
|
276
|
+
});
|
|
277
|
+
});
|
|
278
|
+
```
|
|
279
|
+
</CodeGroup>
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
### Error Handling Pattern
|
|
283
|
+
|
|
284
|
+
You must use the following error handling pattern exactly as shown. The conditions and their handling should not be modified:
|
|
285
|
+
|
|
286
|
+
```typescript {3-9}
|
|
287
|
+
try {
|
|
288
|
+
// Your generation code
|
|
289
|
+
} catch (error) {
|
|
290
|
+
if (error instanceof ToolExecutionError && error.cause instanceof WorkflowAbort) {
|
|
291
|
+
throw error.cause;
|
|
292
|
+
} else {
|
|
293
|
+
throw error;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
### Tool Implementation
|
|
300
|
+
|
|
301
|
+
When implementing tools:
|
|
302
|
+
- Each tool's `execute` function must be wrapped in a `context.run()` call
|
|
303
|
+
- Tool steps should have descriptive names for tracking
|
|
304
|
+
- Tools must follow the same error handling pattern as above
|
|
305
|
+
|
|
306
|
+
Example:
|
|
307
|
+
```typescript
|
|
308
|
+
execute: ({ location }) => context.run("weather tool", () => {
|
|
309
|
+
// Mock data, replace with actual weather API call
|
|
310
|
+
return {
|
|
311
|
+
location,
|
|
312
|
+
temperature: 72 + Math.floor(Math.random() * 21) - 10,
|
|
313
|
+
};
|
|
314
|
+
})
|
|
315
|
+
```
|
|
316
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Anthropic
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The standard way to call a third-party endpoint in your workflow is by using [`context.call`](/workflow/steps/call).
|
|
6
|
+
|
|
7
|
+
However, if you need to call the Anthropic endpoint for text generation ([`/v1/messages`](https://docs.anthropic.com/en/api/messages)), you can leverage the type-safe method `context.api.anthropic.call` method:
|
|
8
|
+
|
|
9
|
+
<Note>
|
|
10
|
+
`context.api.anthropic.call` is not yet available in
|
|
11
|
+
[workflow-py](https://github.com/upstash/workflow-py). You can use `context.call` instead to work with Anthropic. See our
|
|
12
|
+
[Roadmap](/workflow/roadmap) for feature parity plans and
|
|
13
|
+
[Changelog](/workflow/changelog) for updates.
|
|
14
|
+
</Note>
|
|
15
|
+
|
|
16
|
+
```ts
|
|
17
|
+
const { status, body } = await context.api.anthropic.call(
|
|
18
|
+
"Call Anthropic",
|
|
19
|
+
{
|
|
20
|
+
token: "<ANTHROPIC_API_KEY>",
|
|
21
|
+
operation: "messages.create",
|
|
22
|
+
body: {
|
|
23
|
+
model: "claude-3-5-sonnet-20241022",
|
|
24
|
+
max_tokens: 1024,
|
|
25
|
+
messages: [
|
|
26
|
+
{"role": "user", "content": "Hello, world"}
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
}
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
// get text:
|
|
33
|
+
console.log(body.content[0].text)
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
The SDK provides predefined types for the body field in both the request parameters and the response, simplifying common use cases. If you need to customize these types, you can override them as shown below:
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
type ResponseBodyType = { ... }; // Define your response body type
|
|
40
|
+
type RequestBodyType = { ... }; // Define your request body type
|
|
41
|
+
|
|
42
|
+
const { status, body } = await context.api.anthropic.call<
|
|
43
|
+
ResponseBodyType,
|
|
44
|
+
RequestBodyType
|
|
45
|
+
>(
|
|
46
|
+
"Call Anthropic",
|
|
47
|
+
{
|
|
48
|
+
...
|
|
49
|
+
}
|
|
50
|
+
);
|
|
51
|
+
```
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Datadog - Upstash Workflow & QStash Integration
|
|
3
|
+
sidebarTitle: "Datadog"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
This guide walks you through connecting your Datadog account with Upstash QStash for monitoring and analytics of your message delivery, retries, DLQ, and schedules.
|
|
7
|
+
|
|
8
|
+
<Check >
|
|
9
|
+
**Integration Scope**
|
|
10
|
+
|
|
11
|
+
Upstash Datadog Integration covers Prod Pack.
|
|
12
|
+
|
|
13
|
+
</Check>
|
|
14
|
+
|
|
15
|
+
## **Step 1: Log in to Your Datadog Account**
|
|
16
|
+
|
|
17
|
+
1. Go to [Datadog](https://www.datadoghq.com/) and sign in.
|
|
18
|
+
|
|
19
|
+
## **Step 2: Install Upstash Application**
|
|
20
|
+
|
|
21
|
+
1. In Datadog, open the Integrations page.
|
|
22
|
+
2. Search for "Upstash" and open the integration.
|
|
23
|
+
|
|
24
|
+

|
|
25
|
+
|
|
26
|
+
Click "Install" to add Upstash to your Datadog account.
|
|
27
|
+
|
|
28
|
+

|
|
29
|
+
|
|
30
|
+
## **Step 3: Connect Accounts**
|
|
31
|
+
|
|
32
|
+
After installing Upstash, click "Connect Accounts". Datadog will redirect you to Upstash to complete account linking.
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
## **Step 4: Select Account to Integrate**
|
|
37
|
+
|
|
38
|
+
1. On Upstash, select the Datadog account to integrate.
|
|
39
|
+
2. Personal and team accounts are supported.
|
|
40
|
+
|
|
41
|
+
**Caveats**
|
|
42
|
+
|
|
43
|
+
- The integration can be established once at a time. To change the account scope (e.g., add/remove teams), re-establish the integration from scratch.
|
|
44
|
+
|
|
45
|
+

|
|
46
|
+
|
|
47
|
+

|
|
48
|
+
|
|
49
|
+
## **Step 5: Wait for Metrics Availability**
|
|
50
|
+
|
|
51
|
+
Once the integration is completed, metrics from QStash (publish counts, success/error rates, retries, DLQ, schedule executions) will start appearing in Datadog dashboards shortly.
|
|
52
|
+
|
|
53
|
+

|
|
54
|
+
|
|
55
|
+
## **Step 6: Datadog Integration Removal Process**
|
|
56
|
+
|
|
57
|
+
From Datadog → Integrations → Upstash, press "Remove" to break the connection.
|
|
58
|
+
|
|
59
|
+
### Confirm Removal
|
|
60
|
+
|
|
61
|
+
Upstash will stop publishing metrics after removal. Ensure any Datadog API keys/configurations for this integration are also removed on the Datadog side.
|
|
62
|
+
|
|
63
|
+
## **Conclusion**
|
|
64
|
+
|
|
65
|
+
You’ve connected Datadog with Upstash QStash. Explore Datadog dashboards to monitor message delivery performance and reliability.
|
|
66
|
+
|
|
67
|
+
If you need help, contact support.
|
|
68
|
+
|
|
69
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: OpenAI
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
### Calling OpenAI
|
|
6
|
+
|
|
7
|
+
The standard way to call a third-party endpoint in your workflow is by using [`context.call`](/workflow/steps/call).
|
|
8
|
+
|
|
9
|
+
However, if you need to call the OpenAI endpoint for text generation ([`/v1/chat/completions`](https://platform.openai.com/docs/api-reference/chat)), you can leverage the type-safe method `context.api.openai.call` method:
|
|
10
|
+
|
|
11
|
+
<Note>
|
|
12
|
+
`context.api.openai.call` is not yet available in
|
|
13
|
+
[workflow-py](https://github.com/upstash/workflow-py). You can use `context.call` instead to work with OpenAI. See our
|
|
14
|
+
[Roadmap](/workflow/roadmap) for feature parity plans and
|
|
15
|
+
[Changelog](/workflow/changelog) for updates.
|
|
16
|
+
</Note>
|
|
17
|
+
|
|
18
|
+
```typescript OpenAI
|
|
19
|
+
const { status, body } = await context.api.openai.call(
|
|
20
|
+
"Call OpenAI",
|
|
21
|
+
{
|
|
22
|
+
token: "<OPENAI_API_KEY>",
|
|
23
|
+
operation: "chat.completions.create",
|
|
24
|
+
body: {
|
|
25
|
+
model: "gpt-4o",
|
|
26
|
+
messages: [
|
|
27
|
+
{
|
|
28
|
+
role: "system",
|
|
29
|
+
content: "Assistant says 'hello!'",
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
role: "user",
|
|
33
|
+
content: "User shouts back 'hi!'"
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
// get text:
|
|
41
|
+
console.log(body.content[0].text)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The SDK provides predefined types for the body field in both the request parameters and the response, simplifying common use cases. If you need to customize these types, you can override them as shown below:
|
|
45
|
+
|
|
46
|
+
```ts
|
|
47
|
+
type ResponseBodyType = { ... }; // Define your response body type
|
|
48
|
+
type RequestBodyType = { ... }; // Define your request body type
|
|
49
|
+
|
|
50
|
+
const { status, body } = await context.api.openai.call<
|
|
51
|
+
ResponseBodyType,
|
|
52
|
+
RequestBodyType
|
|
53
|
+
>(
|
|
54
|
+
"Call OpenAI",
|
|
55
|
+
{
|
|
56
|
+
...
|
|
57
|
+
}
|
|
58
|
+
);
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### OpenAI Compatible Provider
|
|
62
|
+
|
|
63
|
+
If you want to call an OpenAI compatible provider, you can do so using the `baseURL` parameter:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
const { status, body } = await context.api.openai.call(
|
|
67
|
+
"Call Deepseek",
|
|
68
|
+
{
|
|
69
|
+
baseURL: "https://api.deepseek.com",
|
|
70
|
+
token: process.env.DEEPSEEK_API_KEY,
|
|
71
|
+
operation: "chat.completions.create",
|
|
72
|
+
body: {
|
|
73
|
+
model: "deepseek-chat",
|
|
74
|
+
messages: [
|
|
75
|
+
{
|
|
76
|
+
role: "system",
|
|
77
|
+
content: "Assistant says 'hello!'",
|
|
78
|
+
},
|
|
79
|
+
{
|
|
80
|
+
role: "user",
|
|
81
|
+
content: "User shouts back 'hi!'"
|
|
82
|
+
}
|
|
83
|
+
],
|
|
84
|
+
},
|
|
85
|
+
}
|
|
86
|
+
);
|
|
87
|
+
```
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Prometheus - Upstash Workflow & QStash Integration
|
|
3
|
+
sidebarTitle: "Prometheus"
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
To monitor your QStash metrics in Prometheus and visualize in Grafana, follow these steps:
|
|
7
|
+
|
|
8
|
+
<Check >
|
|
9
|
+
**Integration Scope**
|
|
10
|
+
|
|
11
|
+
Upstash Prometheus Integration covers Prod Pack.
|
|
12
|
+
|
|
13
|
+
</Check>
|
|
14
|
+
|
|
15
|
+
## **Step 1: Enable Prometheus in Upstash Console**
|
|
16
|
+
|
|
17
|
+
1. Open the Upstash Console and navigate to QStash.
|
|
18
|
+
2. Go to Settings → Monitoring.
|
|
19
|
+
3. Enable Prometheus to allow scraping QStash metrics.
|
|
20
|
+
|
|
21
|
+

|
|
22
|
+
|
|
23
|
+
## **Step 2: Copy Monitoring Token**
|
|
24
|
+
|
|
25
|
+
1. After enabling, a monitoring token is generated and displayed.
|
|
26
|
+
2. Copy the token. It will be used to authenticate Prometheus requests.
|
|
27
|
+
|
|
28
|
+
<Check >
|
|
29
|
+
**Header Format**
|
|
30
|
+
|
|
31
|
+
Send the token as `Authorization: Bearer <MONITORING_TOKEN>`.
|
|
32
|
+
|
|
33
|
+
</Check>
|
|
34
|
+
|
|
35
|
+

|
|
36
|
+
|
|
37
|
+
## **Step 3: Configure Prometheus (via Grafana Data Source)**
|
|
38
|
+
|
|
39
|
+
1. In Grafana, add a Prometheus data source.
|
|
40
|
+
2. Set the address to `https://api.upstash.com/monitoring/prometheus`.
|
|
41
|
+
3. In HTTP headers, add the monitoring token.
|
|
42
|
+
|
|
43
|
+

|
|
44
|
+
|
|
45
|
+

|
|
46
|
+
|
|
47
|
+
Click <b>Test and Save</b>.
|
|
48
|
+
|
|
49
|
+

|
|
50
|
+
|
|
51
|
+
## **Step 4: Import Dashboard**
|
|
52
|
+
|
|
53
|
+
You can use the Upstash Grafana dashboard to visualize QStash metrics.
|
|
54
|
+
|
|
55
|
+
Open the import dialog and use: <a href="https://grafana.com/grafana/dashboards/24206-upstash-qstash-dashboard/">Upstash QStash Dashboard</a>
|
|
56
|
+
|
|
57
|
+

|
|
58
|
+
|
|
59
|
+
## **Conclusion**
|
|
60
|
+
|
|
61
|
+
You’ve integrated QStash with Prometheus. Use Grafana to explore message throughput, retries, DLQ, schedules, and Upstash Workflows.
|
|
62
|
+
|
|
63
|
+
If you encounter issues, contact support.
|
|
64
|
+
|
|
65
|
+
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Resend
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
The standard way to call a third-party endpoint in your workflow is by using [`context.call`](/workflow/steps/call).
|
|
6
|
+
|
|
7
|
+
However, if you need to call the Resend endpoint to send emails ([`/emails`](https://resend.com/docs/api-reference/emails/send-email) or [`/emails/batch`](https://resend.com/docs/api-reference/emails/send-batch-emails)), you can leverage the type-safe method `context.api.resend.call` method:
|
|
8
|
+
|
|
9
|
+
<Note>
|
|
10
|
+
`context.api.resend.call` is not yet available in
|
|
11
|
+
[workflow-py](https://github.com/upstash/workflow-py). You can use `context.call` instead to work with Resend. See our
|
|
12
|
+
[Roadmap](/workflow/roadmap) for feature parity plans and
|
|
13
|
+
[Changelog](/workflow/changelog) for updates.
|
|
14
|
+
</Note>
|
|
15
|
+
|
|
16
|
+
<CodeGroup>
|
|
17
|
+
|
|
18
|
+
```typescript Single Email
|
|
19
|
+
const { status, body } = await context.api.resend.call(
|
|
20
|
+
"Call Resend",
|
|
21
|
+
{
|
|
22
|
+
token: "<RESEND_API_KEY>",
|
|
23
|
+
body: {
|
|
24
|
+
from: "Acme <onboarding@resend.dev>",
|
|
25
|
+
to: ["delivered@resend.dev"],
|
|
26
|
+
subject: "Hello World",
|
|
27
|
+
html: "<p>It works!</p>",
|
|
28
|
+
},
|
|
29
|
+
headers: {
|
|
30
|
+
"content-type": "application/json",
|
|
31
|
+
},
|
|
32
|
+
}
|
|
33
|
+
);
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```typescript Batch Email {4}
|
|
37
|
+
const { status, body } = await context.api.resend.call(
|
|
38
|
+
"Call Resend",
|
|
39
|
+
{
|
|
40
|
+
batch: true,
|
|
41
|
+
token: "<RESEND_API_KEY>",
|
|
42
|
+
body: [
|
|
43
|
+
{
|
|
44
|
+
from: "Acme <onboarding@resend.dev>",
|
|
45
|
+
to: ["delivered@resend.dev"],
|
|
46
|
+
subject: "Hello World",
|
|
47
|
+
html: "<p>It works!</p>",
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
from: "Acme <onboarding@resend.dev>",
|
|
51
|
+
to: ["delivered@resend.dev"],
|
|
52
|
+
subject: "Hello World",
|
|
53
|
+
html: "<p>It works!</p>",
|
|
54
|
+
},
|
|
55
|
+
],
|
|
56
|
+
headers: {
|
|
57
|
+
"content-type": "application/json",
|
|
58
|
+
},
|
|
59
|
+
}
|
|
60
|
+
);
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
</CodeGroup>
|
|
64
|
+
|
|
65
|
+
The SDK provides predefined types for the body field in both the request parameters and the response, simplifying common use cases. If you need to customize these types, you can override them as shown below:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
type IsBatch = true; // Set to either true or false
|
|
69
|
+
type ResponseBodyType = { ... }; // Define your response body type
|
|
70
|
+
type RequestBodyType = { ... }; // Define your request body type
|
|
71
|
+
|
|
72
|
+
const { status, body } = await context.api.resend.call<
|
|
73
|
+
IsBatch,
|
|
74
|
+
ResponseBodyType,
|
|
75
|
+
RequestBodyType
|
|
76
|
+
>(
|
|
77
|
+
"Call Resend",
|
|
78
|
+
{
|
|
79
|
+
...
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
```
|