-
-
Notifications
You must be signed in to change notification settings - Fork 461
feat: update nextjs examples #933
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,2 @@ | ||
| /** | ||
| * Agent Configuration Exports | ||
| * | ||
| * Central export point for agent-related configurations. | ||
| * Implements singleton pattern for VoltAgent to enable VoltOps console debugging. | ||
| */ | ||
|
|
||
| import { VoltAgent } from "@voltagent/core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
| import { chatbotAgent } from "./agent"; | ||
|
|
||
| // Type declaration for global augmentation | ||
| declare global { | ||
| var voltAgentInstance: VoltAgent | undefined; | ||
| } | ||
|
|
||
| /** | ||
| * Singleton VoltAgent instance getter | ||
| * | ||
| * Creates a single VoltAgent instance with the Hono server for VoltOps console debugging. | ||
| * The singleton pattern ensures the debugging port (3141) is properly opened and maintained | ||
| * across hot reloads in Next.js development mode. | ||
| * | ||
| * @returns {VoltAgent} The singleton VoltAgent instance | ||
| */ | ||
| function getVoltAgentInstance() { | ||
| if (!globalThis.voltAgentInstance) { | ||
| globalThis.voltAgentInstance = new VoltAgent({ | ||
| agents: { | ||
| chatbotAgent, | ||
| }, | ||
| server: honoServer(), | ||
| }); | ||
| } | ||
| return globalThis.voltAgentInstance; | ||
| } | ||
|
|
||
| // Initialize the singleton | ||
| export const voltAgent = getVoltAgentInstance(); | ||
|
|
||
| // Export individual components | ||
| export { chatbotAgent } from "./agent"; | ||
| export { sharedMemory } from "./memory"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { chatbotAgent } from "../lib/agent"; | ||
| export { sharedMemory } from "../lib/agent"; | ||
| export { chatbotAgent as agent } from "../lib/agent"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| export { agent } from "./agents"; | ||
| export { chatbotAgent } from "./agents"; | ||
| export { sharedMemory } from "./agents"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { VoltAgent } from "@voltagent/core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
| import { agent } from "./agents"; | ||
|
|
||
| new VoltAgent({ | ||
| agents: { | ||
| agent, | ||
| }, | ||
| server: honoServer(), | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| import { openai } from "@ai-sdk/openai"; | ||
| import { Agent, createTool } from "@voltagent/core"; | ||
| import { z } from "zod"; | ||
| import { sharedMemory } from "./memory"; | ||
|
|
||
| const weatherOutputSchema = z.object({ | ||
| weather: z.object({ | ||
| location: z.string(), | ||
| temperature: z.number(), | ||
| condition: z.string(), | ||
| humidity: z.number(), | ||
| windSpeed: z.number(), | ||
| }), | ||
| message: z.string(), | ||
| }); | ||
|
|
||
| const weatherTool = createTool({ | ||
| name: "getWeather", | ||
| description: "Get the current weather for a specific location", | ||
| parameters: z.object({ | ||
| location: z.string().describe("The city or location to get weather for"), | ||
| }), | ||
| outputSchema: weatherOutputSchema, | ||
| execute: async ({ location }) => { | ||
| const mockWeatherData = { | ||
| location, | ||
| temperature: Math.floor(Math.random() * 30) + 5, | ||
| condition: ["Sunny", "Cloudy", "Rainy", "Snowy", "Partly Cloudy"][ | ||
| Math.floor(Math.random() * 5) | ||
| ], | ||
| humidity: Math.floor(Math.random() * 60) + 30, | ||
| windSpeed: Math.floor(Math.random() * 30), | ||
| }; | ||
|
|
||
| return { | ||
| weather: mockWeatherData, | ||
| message: `Current weather in ${location}: ${mockWeatherData.temperature}°C and ${mockWeatherData.condition.toLowerCase()} with ${mockWeatherData.humidity}% humidity and wind speed of ${mockWeatherData.windSpeed} km/h.`, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| export const assistantAgent = new Agent({ | ||
| name: "AssistantUIAgent", | ||
| instructions: | ||
| "You are a helpful AI that keeps responses concise, explains reasoning when useful, can gracefully describe any image or file attachments the user provides, and can call the getWeather tool for weather questions. Ask clarifying questions when context is missing.", | ||
| model: openai("gpt-4o-mini"), | ||
| tools: [weatherTool], | ||
| memory: sharedMemory, | ||
| }); | ||
|
|
||
| export const agent = assistantAgent; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,70 +1,2 @@ | ||
| import { openai } from "@ai-sdk/openai"; | ||
| import { Agent, VoltAgent, createTool } from "@voltagent/core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
| import { z } from "zod"; | ||
| import { sharedMemory } from "./memory"; | ||
|
|
||
| const weatherOutputSchema = z.object({ | ||
| weather: z.object({ | ||
| location: z.string(), | ||
| temperature: z.number(), | ||
| condition: z.string(), | ||
| humidity: z.number(), | ||
| windSpeed: z.number(), | ||
| }), | ||
| message: z.string(), | ||
| }); | ||
|
|
||
| const weatherTool = createTool({ | ||
| name: "getWeather", | ||
| description: "Get the current weather for a specific location", | ||
| parameters: z.object({ | ||
| location: z.string().describe("The city or location to get weather for"), | ||
| }), | ||
| outputSchema: weatherOutputSchema, | ||
| execute: async ({ location }) => { | ||
| const mockWeatherData = { | ||
| location, | ||
| temperature: Math.floor(Math.random() * 30) + 5, | ||
| condition: ["Sunny", "Cloudy", "Rainy", "Snowy", "Partly Cloudy"][ | ||
| Math.floor(Math.random() * 5) | ||
| ], | ||
| humidity: Math.floor(Math.random() * 60) + 30, | ||
| windSpeed: Math.floor(Math.random() * 30), | ||
| }; | ||
|
|
||
| return { | ||
| weather: mockWeatherData, | ||
| message: `Current weather in ${location}: ${mockWeatherData.temperature}°C and ${mockWeatherData.condition.toLowerCase()} with ${mockWeatherData.humidity}% humidity and wind speed of ${mockWeatherData.windSpeed} km/h.`, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| const assistantAgent = new Agent({ | ||
| name: "AssistantUIAgent", | ||
| instructions: | ||
| "You are a helpful AI that keeps responses concise, explains reasoning when useful, can gracefully describe any image or file attachments the user provides, and can call the getWeather tool for weather questions. Ask clarifying questions when context is missing.", | ||
| model: openai("gpt-4o-mini"), | ||
| tools: [weatherTool], | ||
| memory: sharedMemory, | ||
| }); | ||
|
|
||
| declare global { | ||
| // eslint-disable-next-line no-var | ||
| var voltAssistant: VoltAgent | undefined; | ||
| } | ||
|
|
||
| function getVoltAgentInstance() { | ||
| if (!globalThis.voltAssistant) { | ||
| globalThis.voltAssistant = new VoltAgent({ | ||
| agents: { | ||
| assistantAgent, | ||
| }, | ||
| server: honoServer(), | ||
| }); | ||
| } | ||
| return globalThis.voltAssistant; | ||
| } | ||
|
|
||
| export const voltAgent = getVoltAgentInstance(); | ||
| export const agent = assistantAgent; | ||
| export { agent } from "./agents"; | ||
| export { assistantAgent } from "./agents"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { VoltAgent } from "@voltagent/core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
| import { agent } from "./agents"; | ||
|
|
||
| new VoltAgent({ | ||
| agents: { | ||
| agent, | ||
| }, | ||
| server: honoServer(), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { agent } from "@/lib/agent"; | ||
| import { agent } from "@/voltagent"; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
set -euo pipefail
# 1) Read the route.ts file to see actual code and confirm JSON.stringify usage
echo "=== Reading route.ts file ==="
cat -n examples/with-client-side-tools/app/api/chat/route.ts | head -30
# 2) Check the voltagent module structure in the example
echo -e "\n=== Exploring voltagent module in example ==="
find examples/with-client-side-tools -name "*.ts" -o -name "*.js" | grep -E "(voltagent|index)" | head -20
# 3) Verify safeStringify is available in the codebase
echo -e "\n=== Searching for safeStringify usage ==="
rg -n "safeStringify" --type=ts -B2 -A2 | head -30Repository: VoltAgent/voltagent Length of output: 3331 🏁 Script executed: #!/bin/bash
set -euo pipefail
echo "=== Reading voltagent/index.ts ==="
cat -n examples/with-client-side-tools/voltagent/index.ts
echo -e "\n=== Reading voltagent/agents.ts (first 30 lines) ==="
head -30 examples/with-client-side-tools/voltagent/agents.ts
echo -e "\n=== Checking for safeStringify import patterns in examples ==="
rg -n "safeStringify" examples/ --type=ts || echo "No safeStringify found in examples"
echo -e "\n=== Checking @voltagent/internal availability in example ==="
fd -a package.json examples/with-client-side-tools | xargs cat | grep -A5 -B5 "@voltagent" || echo "Checking package structure..."Repository: VoltAgent/voltagent Length of output: 2851 Replace This file violates the TypeScript guideline which requires using Then replace 🤖 Prompt for AI Agents |
||
|
|
||
| export async function POST(req: Request) { | ||
| try { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { agent } from "./agents"; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { VoltAgent } from "@voltagent/core"; | ||
| import { honoServer } from "@voltagent/server-hono"; | ||
| import { agent } from "./agents"; | ||
|
|
||
| new VoltAgent({ | ||
| agents: { | ||
| agent, | ||
| }, | ||
| server: honoServer(), | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Add error handling and lifecycle management.
The VoltAgent instance is created but not assigned or managed, which can lead to issues:
♻️ Add error handling and lifecycle management
📝 Committable suggestion
🤖 Prompt for AI Agents