You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This command will scaffold the entire Next.js example project described in this guide.
18
+
This command will scaffold an entire Next.js example for you. You can also peek at the full source code right here: [examples/with-nextjs](https://github.com/VoltAgent/voltagent/tree/main/examples/with-nextjs).
19
19
20
20
## Create a New Next.js Project
21
21
@@ -30,157 +30,250 @@ Follow the prompts, selecting TypeScript and App Router.
30
30
31
31
## Install VoltAgent Dependencies
32
32
33
-
Install the necessary VoltAgent packages and the ai-sdk provider:
33
+
Install the necessary VoltAgent packages and dependencies:
npm install @voltagent/core @ai-sdk/openai @ai-sdk/react ai zod@^3.25.76
37
37
```
38
38
39
39
-`@voltagent/core`: The core VoltAgent library.
40
40
-`@ai-sdk/openai`: The ai-sdk provider for OpenAI (or your preferred provider).
41
-
-`@voltagent/cli`: The command-line interface for VoltAgent tasks.
41
+
-`@ai-sdk/react`: React hooks for AI SDK integration.
42
+
-`ai`: Core AI SDK library for streaming and chat functionality.
42
43
-`zod`: Used when working with structured outputs.
43
44
44
-
## Configure `next.config.js`
45
+
## Configure `next.config.ts`
45
46
46
-
Next.js might try to bundle server-side packages by default. To prevent issues with VoltAgent, you need to mark its packages as external in your `next.config.mjs` (or `.js` / `.ts`) file:
47
+
Next.js might try to bundle server-side packages by default. To prevent issues with certain packages, you need to mark them as external in your `next.config.ts` file:
47
48
48
-
```typescript title="next.config.mjs // or next.config.ts"
// LibSQL client is safe to externalize; native platform packages are optional.
56
+
"@libsql/client",
57
+
],
59
58
};
60
59
61
60
exportdefaultnextConfig;
62
61
```
63
62
64
-
**Note:** The property was `serverExternalPackages` in older Next.js versions, but changed to `experimental.serverComponentsExternalPackages`. Ensure you use the correct one for your Next.js version.
65
-
66
-
## Initialize VoltAgent
67
-
68
-
Create a file to initialize an Agent, for example, `voltagent/index.ts` in your project root:
69
-
70
-
```typescript title="voltagent/index.ts"
71
-
import { Agent } from"@voltagent/core";
72
-
import { openai } from"@ai-sdk/openai"; // Or your preferred ai-sdk provider
63
+
**Note:** The property used to be `experimental.serverComponentsExternalPackages` in older Next.js versions, but is now `serverExternalPackages`. Ensure you use the correct one for your Next.js version.
73
64
74
-
exportconst agent =newAgent({
75
-
name: "nextjs-agent",
76
-
instructions: "You are a helpful assistant",
77
-
model: openai("gpt-4o"),
78
-
});
79
-
```
65
+
## Environment Variables
80
66
81
-
Remember to set up your environment variables (e.g., `OPENAI_API_KEY`) in a `.env.local` file.
82
-
Create a `.env.local` file in your project root if it doesn't exist, and add your necessary API keys:
67
+
Set up your environment variables (e.g., `OPENAI_API_KEY`) in a `.env.local` file:
83
68
84
69
```env title=".env.local"
85
70
OPENAI_API_KEY="your-openai-api-key-here"
86
71
# Add other environment variables if needed
87
72
```
88
73
89
-
## Create a Server Action
74
+
## Create API Route
75
+
76
+
Create the main chat API route with the agent and singleton defined inline in `app/api/chat/route.ts`:
Define a Server Action to interact with the VoltAgent agent. Create `app/actions.ts`:
104
+
// Main agent
105
+
exportconst agent =newAgent({
106
+
name: "CalculatorAgent",
107
+
instructions:
108
+
"You are a helpful calculator assistant. When users ask you to calculate something, use the calculate tool to perform the math and then explain the result clearly.",
109
+
model: openai("gpt-4o-mini"),
110
+
tools: [calculatorTool],
111
+
});
92
112
93
-
```typescript title="app/actions.ts"
94
-
"use server";
113
+
// VoltAgent singleton (augments global scope during dev to avoid re-instantiation)
114
+
declareglobal {
115
+
var voltAgentInstance:VoltAgent|undefined;
116
+
}
95
117
96
-
import { agent } from"@/voltagent"; // Adjust path if needed
Now you can run your Next.js development server (`npm run dev`) and test the AI calculator! This demonstrates a basic integration of VoltAgent within a Next.js application using Server Actions.
265
+
## Run the Application
266
+
267
+
Now you can run your Next.js development server:
268
+
269
+
```bash
270
+
npm run dev
271
+
```
272
+
273
+
This creates a simple but powerful VoltAgent application with:
274
+
275
+
-**Single agent** with a custom calculator tool
276
+
-**Streaming chat interface** with real-time updates
277
+
-**Tool integration** showing how agents use tools
278
+
279
+
The agent will use the calculator tool when users ask for mathematical calculations, demonstrating how VoltAgent integrates tools seamlessly into conversations.
0 commit comments