text
stringlengths
0
59.1k
}
} catch (error) {
return {
success: false,
error:
error instanceof Error ? error.message : "An error occurred while querying order status",
message: "Sorry, we cannot query your order status right now. Please try again later.",
};
}
},
});
```
</details>
![order-status tool](https://cdn.voltagent.dev/examples/with-whatsapp/order-status.png)
**What this tool does:** Pulls recent orders for the current WhatsApp user, optionally filtering by ID, so the agent can answer “Where is my order?” questions with up-to-date status information without exposing anyone else’s data.
## The Complete Application Structure
Now let's examine the complete `src/index.ts` file that brings everything together:
<details>
<summary>View code</summary>
```typescript
import "dotenv/config";
import { VoltAgent, VoltOpsClient, Agent, Memory } from "@voltagent/core";
import { LibSQLMemoryAdapter } from "@voltagent/libsql";
import { createPinoLogger } from "@voltagent/logger";
import { openai } from "@ai-sdk/openai";
import { honoServer } from "@voltagent/server-hono";
import { z } from "zod";
import { listMenuItemsTool, createOrderTool, checkOrderStatusTool } from "./tools";
import { handleWhatsAppMessage, handleWhatsAppVerification } from "./webhooks/whatsapp";
// Create a logger instance
const logger = createPinoLogger({
name: "with-whatsapp",
level: "info",
});
// 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"),
});
// 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,
},
});
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"
- Ask for delivery address
- Update deliveryAddress field when received
- Change orderStatus to "completed"
- Execute createOrder tool (with orders and deliveryAddress)
- Confirm order and clear working memory
Always be friendly and helpful. Start with "Welcome!" greeting.`,
model: openai("gpt-4o-mini"),
tools: [listMenuItemsTool, createOrderTool, checkOrderStatusTool],
memory,
});