text
stringlengths
0
59.1k
new VoltAgent({
agents: {
agent,
},
server: honoServer({
configureApp: (app) => {
// WhatsApp webhook verification (GET)
app.get("/webhook/whatsapp", async (c) => {
return handleWhatsAppVerification(c);
});
// WhatsApp webhook message handler (POST)
app.post("/webhook/whatsapp", async (c) => {
return handleWhatsAppMessage(c, agent);
});
// Health check endpoint
app.get("/health", (c) => {
return c.json({
status: "healthy",
service: "whatsapp-ordering-agent",
timestamp: new Date().toISOString(),
});
});
},
}),
logger,
voltOpsClient: new VoltOpsClient({
publicKey: process.env.VOLTAGENT_PUBLIC_KEY || "",
secretKey: process.env.VOLTAGENT_SECRET_KEY || "",
}),
});
```
</details>
Let's understand each section of this code:
## Working Memory Schema
We defined the conversation state structure using Zod:
```typescript
// Define working memory schema with Zod
const workingMemorySchema = z.object({
orders: z
.array(
z.object({
menuItemId: z.number(),
itemName: z.string(),
quantity: z.number(),
price: z.number(),
})
)
.default([]),
deliveryAddress: z.string().default(""),
customerNotes: z.string().default(""),
orderStatus: z.enum(["selecting", "address_needed", "completed"]).default("selecting"),
});
```
**Key points:**
- `orders` array tracks selected menu items with quantities and prices
- `orderStatus` enum manages conversation flow through ordering states
- `deliveryAddress` captures customer location for order fulfillment
- `customerNotes` allows for special delivery instructions
- All fields have sensible defaults to handle incomplete conversations
- Zod schema provides runtime type validation and TypeScript integration
#### Memory Configuration
We set up persistent memory with conversation-scoped working memory:
```typescript
// Configure persistent memory with working memory enabled
const memory = new Memory({
storage: new LibSQLMemoryAdapter({
url: "file:./.voltagent/memory.db",
logger: logger.child({ component: "libsql" }),
}),
workingMemory: {
enabled: true,
scope: "conversation", // Store per conversation
schema: workingMemorySchema,
},
});
```
![Memory](https://cdn.voltagent.dev/examples/with-whatsapp/memory.png)
**Key points:**
- Uses `LibSQLMemoryAdapter` for lightweight local persistence
- `scope: "conversation"` isolates each customer's cart data
- Working memory automatically clears after order completion
- Persistent memory retains order history for status checks
- Session-specific storage prevents cart mixing between customers
- Local SQLite database ensures fast access and simple deployment