Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a4900a9

Browse files
committedDec 11, 2024·
billions
1 parent 2df768d commit a4900a9

File tree

6 files changed

+178
-64
lines changed

6 files changed

+178
-64
lines changed
 

‎packages/plugin-dominos/src/PizzaOrderManager.ts

+31-42
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,9 @@ export class PizzaOrderManager implements OrderManager {
228228
);
229229
}
230230

231-
// Get next required action based on order state
232231
getNextRequiredAction(order: Order, customer: Customer): string {
233-
console.log("getNextRequiredAction: ", order, customer);
234232
if (!order.items || order.items.length === 0) {
235-
return "Collect initial pizza order details - show size, crust, and topping options to customer";
233+
return "Collect initial pizza order details";
236234
}
237235

238236
if (!customer.name) {
@@ -251,28 +249,20 @@ export class PizzaOrderManager implements OrderManager {
251249
return "Request email for order confirmation";
252250
}
253251

254-
if (order.paymentStatus === PaymentStatus.NOT_PROVIDED) {
255-
return "Request credit card information";
252+
if (!customer.paymentMethod) {
253+
return "Request credit card information for payment";
256254
}
257255

258-
if (order.paymentStatus === PaymentStatus.INVALID) {
259-
return "Request alternative payment method";
260-
}
261-
262-
if (
263-
!order.progress.isConfirmed &&
264-
order.paymentStatus === PaymentStatus.VALID
265-
) {
256+
if (!order.progress.isConfirmed) {
266257
return "Review order details with customer and obtain final confirmation";
267258
}
268259

269260
return "Provide order confirmation number and estimated delivery time";
270261
}
271262

272263
getNextRequiredActionDialogue(order: Order, customer: Customer): string {
273-
console.log("getNextRequiredActionDialogue: ", order, customer);
274264
if (!order.items || order.items.length === 0) {
275-
return "Let me help you build your perfect pizza! What size would you like? We have Small, Medium, Large and Extra Large. Then I can help you choose your crust type and toppings.";
265+
return "Let me help you build your perfect pizza! What size would you like?";
276266
}
277267

278268
if (!customer.name) {
@@ -284,29 +274,22 @@ export class PizzaOrderManager implements OrderManager {
284274
}
285275

286276
if (!customer.address) {
287-
return "Where would you like your pizza delivered? Please provide your complete delivery address.";
277+
return "Where would you like your pizza delivered?";
288278
}
289279

290280
if (!customer.email) {
291281
return "What email address should we send your order confirmation to?";
292282
}
293283

294-
if (order.paymentStatus === PaymentStatus.NOT_PROVIDED) {
295-
return "Great! To process your order, I'll need your credit card information. Could you please provide your card number?";
284+
if (!customer.paymentMethod) {
285+
return "To complete your order, I'll need your credit card information. Could you please provide your card number, expiration date (MM/YY), CVV, and billing zip code?";
296286
}
297287

298-
if (order.paymentStatus === PaymentStatus.INVALID) {
299-
return "I apologize, but there seems to be an issue with that payment method. Could you please provide a different credit card?";
288+
if (!order.progress.isConfirmed) {
289+
return "Great! I have all your information. Would you like me to review everything before placing your order?";
300290
}
301291

302-
if (
303-
!order.progress.isConfirmed &&
304-
order.paymentStatus === PaymentStatus.VALID
305-
) {
306-
return "Perfect! I have all your order details. Would you like me to review everything with you before finalizing your order?";
307-
}
308-
309-
return "Great news! Your order is confirmed. Let me get your confirmation number and estimated delivery time for you.";
292+
return "Your order is confirmed! Let me get your confirmation number and estimated delivery time.";
310293
}
311294

312295
// Get topping category and price
@@ -628,13 +611,17 @@ export class PizzaOrderManager implements OrderManager {
628611

629612
// Calculate order progress
630613
calculateOrderProgress(order: Order, customer: Customer): OrderProgress {
631-
console.log("calculateOrderProgress: ", order, customer);
632614
return {
633-
hasCustomerInfo: !this.validateCustomerInfo(customer),
634-
hasPaymentMethod: order.paymentMethod !== undefined,
635-
hasValidPayment:
636-
order.paymentStatus === PaymentStatus.VALID ||
637-
order.paymentStatus === PaymentStatus.PROCESSED,
615+
hasCustomerInfo: Boolean(
616+
customer.name &&
617+
customer.phone &&
618+
customer.email &&
619+
customer.address
620+
),
621+
hasPaymentMethod: Boolean(customer.paymentMethod),
622+
hasValidPayment: Boolean(
623+
customer.paymentMethod && order.payments?.[0]?.isValid
624+
),
638625
isConfirmed: order.status === OrderStatus.CONFIRMED,
639626
};
640627
}
@@ -714,14 +701,16 @@ export class PizzaOrderManager implements OrderManager {
714701
order.progress = this.calculateOrderProgress(order, customer);
715702

716703
// Update order status based on current state
717-
if (!order.progress.hasCustomerInfo) {
718-
order.status = OrderStatus.AWAITING_CUSTOMER_INFO;
719-
} else if (!order.progress.hasValidPayment) {
720-
order.status = OrderStatus.AWAITING_PAYMENT;
721-
} else if (!order.progress.isConfirmed) {
722-
order.status = OrderStatus.PROCESSING;
723-
} else {
724-
order.status = OrderStatus.CONFIRMED;
704+
if (order.progress) {
705+
if (!order.progress.hasCustomerInfo) {
706+
order.status = OrderStatus.AWAITING_CUSTOMER_INFO;
707+
} else if (!order.progress.hasValidPayment) {
708+
order.status = OrderStatus.AWAITING_PAYMENT;
709+
} else if (!order.progress.isConfirmed) {
710+
order.status = OrderStatus.PROCESSING;
711+
} else {
712+
order.status = OrderStatus.CONFIRMED;
713+
}
725714
}
726715

727716
return order;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Action, IAgentRuntime, Memory } from "@ai16z/eliza";
2+
import { PizzaOrderManager } from "../PizzaOrderManager";
3+
import { OrderStatus } from "../types";
4+
5+
export const confirmOrder: Action = {
6+
name: "CONFIRM_ORDER",
7+
similes: ["FINALIZE_ORDER", "FINISH_ORDER", "PLACE_ORDER"],
8+
examples: [
9+
// TODO
10+
],
11+
description: "Confirms and places the final order with Dominos",
12+
validate: async (runtime: IAgentRuntime, message: Memory) => {
13+
const orderManager = new PizzaOrderManager(runtime);
14+
const userId = message.userId;
15+
const order = await orderManager.getOrder(userId);
16+
const customer = await orderManager.getCustomer(userId);
17+
18+
if (!order || !customer) return false;
19+
20+
// Only valid if we have complete customer info and valid payment
21+
return (
22+
order.progress &&
23+
order.progress.hasCustomerInfo &&
24+
order.progress.hasValidPayment &&
25+
!order.progress.isConfirmed
26+
);
27+
},
28+
handler: async (runtime: IAgentRuntime, message: Memory) => {
29+
const orderManager = new PizzaOrderManager(runtime);
30+
const userId = message.userId;
31+
const order = await orderManager.getOrder(userId);
32+
const customer = await orderManager.getCustomer(userId);
33+
34+
try {
35+
// Final validation with Dominos
36+
await order.validate();
37+
38+
// Get final pricing
39+
await order.price();
40+
41+
// Place the order
42+
await order.place();
43+
44+
// Update order status
45+
order.status = OrderStatus.CONFIRMED;
46+
await orderManager.saveOrder(userId, order);
47+
48+
return (
49+
`Great news! Your order has been confirmed and is being prepared.\n\n` +
50+
`Order Number: ${order.orderID}\n` +
51+
`Estimated Delivery Time: ${order.estimatedWaitMinutes} minutes\n\n` +
52+
orderManager.getOrderSummary(order, customer)
53+
);
54+
} catch (error) {
55+
return "There was an issue placing your order: " + error.message;
56+
}
57+
},
58+
};

‎packages/plugin-dominos/src/actions/updateCustomer.ts

+51-18
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
ModelClass,
1010
State,
1111
} from "@ai16z/eliza";
12-
import { Customer } from "dominos";
12+
import { Customer, Payment } from "dominos";
1313
import { z } from "zod";
1414
import { PizzaOrderManager } from "../PizzaOrderManager";
1515

@@ -22,6 +22,14 @@ const CustomerSchema = z.object({
2222
.optional(),
2323
email: z.string().email().optional(),
2424
address: z.string().min(10).optional(),
25+
paymentMethod: z
26+
.object({
27+
cardNumber: z.string().regex(/^\d{16}$/),
28+
expiryDate: z.string().regex(/^(0[1-9]|1[0-2])\/([0-9]{2})$/),
29+
cvv: z.string().regex(/^\d{3,4}$/),
30+
postalCode: z.string().regex(/^\d{5}$/),
31+
})
32+
.optional(),
2533
});
2634

2735
export const handler: Handler = async (
@@ -45,24 +53,31 @@ export const handler: Handler = async (
4553

4654
// Extract customer details using LLM and schema
4755
const extractionTemplate = `
48-
Extract customer information from the following conversation. Keep existing information if not mentioned in the update.
49-
50-
Current customer information:
51-
Name: ${customer.name || "Not provided"}
52-
Phone: ${customer.phone || "Not provided"}
53-
Email: ${customer.email || "Not provided"}
54-
Address: ${customer.address || "Not provided"}
55-
56-
{{recentConversation}}
57-
58-
Provide updated customer information as a JSON object, including only fields that should be changed:
59-
{
60-
"name": string (optional),
61-
"phone": string (optional, format: XXX-XXX-XXXX),
62-
"email": string (optional, valid email),
63-
"address": string (optional, full delivery address)
56+
Extract customer information from the following conversation. Keep existing information if not mentioned in the update.
57+
58+
Current customer information:
59+
Name: ${customer.name || "Not provided"}
60+
Phone: ${customer.phone || "Not provided"}
61+
Email: ${customer.email || "Not provided"}
62+
Address: ${customer.address || "Not provided"}
63+
Payment: ${customer.paymentMethod ? "Provided" : "Not provided"}
64+
65+
{{recentConversation}}
66+
67+
Provide updated customer information as a JSON object, including only fields that should be changed:
68+
{
69+
"name": string (optional),
70+
"phone": string (optional, format: XXX-XXX-XXXX),
71+
"email": string (optional, valid email),
72+
"address": string (optional, full delivery address),
73+
"paymentMethod": {
74+
"cardNumber": string (16 digits),
75+
"expiryDate": string (MM/YY),
76+
"cvv": string (3-4 digits),
77+
"postalCode": string (5 digits)
6478
}
65-
`;
79+
}
80+
`;
6681

6782
const context = composeContext({
6883
state,
@@ -83,6 +98,24 @@ export const handler: Handler = async (
8398
if (customerUpdates.email) customer.email = customerUpdates.email;
8499
if (customerUpdates.address) customer.address = customerUpdates.address;
85100

101+
// Update the handler logic
102+
if (customerUpdates.paymentMethod) {
103+
// Create Dominos Payment object
104+
const payment = new Payment({
105+
number: customerUpdates.paymentMethod.cardNumber,
106+
expiration: customerUpdates.paymentMethod.expiryDate,
107+
securityCode: customerUpdates.paymentMethod.cvv,
108+
postalCode: customerUpdates.paymentMethod.postalCode,
109+
amount: order.amountsBreakdown.customer,
110+
});
111+
112+
// Clear existing payments and add new one
113+
order.payments = [payment];
114+
115+
// Update customer payment method
116+
customer.paymentMethod = customerUpdates.paymentMethod;
117+
}
118+
86119
await orderManager.saveCustomer(userId, customer);
87120

88121
// Process updated order

‎packages/plugin-dominos/src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import { pizzaOrderProvider } from "./providers/pizzaOrder.ts";
44
import { endOrder } from "./actions/endOrder.ts";
55
import { updateCustomer } from "./actions/updateCustomer.ts";
66
import { updateOrder } from "./actions/updateOrder.ts";
7+
import { confirmOrder } from "./actions/confirmOrder.ts";
78

89
export * as actions from "./actions/index.ts";
910
export * as providers from "./providers/index.ts";
1011

1112
export const dominosPlugin: Plugin = {
1213
name: "dominos",
1314
description: "Order a dominos pizza",
14-
actions: [startOrder, endOrder, updateCustomer, updateOrder],
15+
actions: [startOrder, endOrder, updateCustomer, updateOrder, confirmOrder],
1516
providers: [pizzaOrderProvider],
1617
};

‎packages/plugin-dominos/src/providers/pizzaOrder.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IAgentRuntime, Memory, Provider } from "@ai16z/eliza";
2-
import { OrderStatus } from "../types";
32
import { PizzaOrderManager } from "../PizzaOrderManager";
3+
import { OrderStatus, PaymentStatus } from "../types";
44

55
export const pizzaOrderProvider: Provider = {
66
get: async (runtime: IAgentRuntime, message: Memory) => {
@@ -13,7 +13,26 @@ export const pizzaOrderProvider: Provider = {
1313
return "No active pizza order. The customer needs to start a new order.";
1414
}
1515

16-
let context = "=== PIZZA ORDER STATUS ===\n\n";
16+
// Add payment-specific status to context
17+
let context = "\nPAYMENT STATUS:\n";
18+
context += `Current Status: ${order.paymentStatus}\n`;
19+
if (order.paymentStatus === PaymentStatus.NOT_PROVIDED) {
20+
context += "Payment information needed to complete order.\n";
21+
} else if (order.paymentStatus === PaymentStatus.INVALID) {
22+
context +=
23+
"Previous payment method was invalid. Please provide new payment information.\n";
24+
}
25+
26+
// Add clearer next action guidance
27+
if (order.status === OrderStatus.AWAITING_PAYMENT) {
28+
context +=
29+
"\nREQUIRED: Please provide credit card information to complete your order.\n";
30+
} else if (order.status === OrderStatus.PROCESSING) {
31+
context +=
32+
"\nREQUIRED: Please review your order and confirm to place it.\n";
33+
}
34+
35+
context += "=== PIZZA ORDER STATUS ===\n\n";
1736

1837
// Add order summary
1938
context += orderManager.getOrderSummary(order, customer);

‎packages/plugin-dominos/src/types.ts

+15-1
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@ enum OrderStatus {
33
NEW = "NEW",
44
AWAITING_CUSTOMER_INFO = "AWAITING_CUSTOMER_INFO",
55
AWAITING_PAYMENT = "AWAITING_PAYMENT",
6+
AWAITING_CONFIRMATION = "AWAITING_CONFIRMATION",
67
PROCESSING = "PROCESSING",
78
CONFIRMED = "CONFIRMED",
89
FAILED = "FAILED",
910
}
1011

12+
interface Order {
13+
status: OrderStatus;
14+
paymentStatus: PaymentStatus;
15+
paymentMethod?: PaymentMethod;
16+
customer?: Customer;
17+
items?: OrderItem[];
18+
}
19+
1120
// Order progress tracking
1221
interface OrderProgress {
1322
hasCustomerInfo: boolean;
@@ -42,7 +51,12 @@ interface Customer {
4251
phone: string;
4352
email: string;
4453
address: string;
45-
paymentMethods?: PaymentMethod[];
54+
paymentMethod?: {
55+
cardNumber?: string;
56+
expiryDate?: string;
57+
cvv?: string;
58+
postalCode?: string;
59+
};
4660
isReturning: boolean;
4761
}
4862

0 commit comments

Comments
 (0)
Please sign in to comment.