import { Agent, MongoDBStorage, openai, defineTool } from "@radaros/core";
import { z } from "zod";
const searchOrders = defineTool({
name: "search_orders",
description: "Search orders by ID, email, or keyword",
parameters: z.object({ query: z.string() }),
execute: async ({ query }) => ({ orderId: "ORD-7890", status: "delayed", customer: "alice@example.com" }),
});
const checkDelivery = defineTool({
name: "check_delivery_status",
description: "Check delivery status and delay duration",
parameters: z.object({ orderId: z.string() }),
execute: async ({ orderId }) => ({ status: "delayed", delayDays: 5, carrier: "FedEx" }),
});
const processRefund = defineTool({
name: "process_refund",
description: "Issue a refund for an order",
parameters: z.object({ orderId: z.string(), amount: z.number() }),
execute: async ({ orderId, amount }) => ({ success: true, refundId: "REF-001" }),
});
const agent = new Agent({
name: "support-agent",
model: openai("gpt-4o"),
tools: [searchOrders, checkDelivery, processRefund],
memory: {
storage: new MongoDBStorage({ uri: "mongodb://localhost/radaros" }),
procedures: {
maxProcedures: 100,
matchThreshold: 0.7,
},
model: openai("gpt-4o-mini"),
},
});
// --- Run 1: Agent solves a refund from scratch ---
await agent.run({
input: "Order #7890 never arrived. I want a refund.",
userId: "user-alice",
});
// Agent reasons through: search_orders → check_delivery_status → process_refund
// After run: procedure extracted automatically
// --- Run 2: Similar request, procedure is suggested ---
await agent.run({
input: "My order #1234 is late. Can I get my money back?",
userId: "user-bob",
});
// Agent sees in its system prompt:
// "Suggested procedure (used successfully 1 time):
// 1. search_orders — look up the order
// 2. check_delivery_status — verify the delay
// 3. process_refund — issue the refund"
//
// Agent follows the procedure, completing faster with fewer tokens