text
stringlengths
0
59.1k
});
});
},
}),
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
📚 For detailed information about memory management, see the [Working Memory Guide](https://voltagent.dev/docs/agents/memory/working-memory/).
### Agent Configuration
The example configures an intelligent agent with specific instructions and tools:
```typescript
const agent = new Agent({
name: "with-whatsapp",
instructions: `You are a WhatsApp ordering AI agent. Your task is to take food orders from customers.
Order Flow:
- If orders array is empty, show menu
- Ask customer to select items
2. When customer orders:
- Get selected item details from menu
- Keep orderStatus as "selecting"
- Ask if they want anything else
3. When customer doesn't want more items:
- Change orderStatus to "address_needed"