Skip to content

Commit

Permalink
enhance existing actions and Order Manager
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfsayo committed Jan 6, 2025
1 parent f68c66f commit 2256335
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 46 deletions.
2 changes: 1 addition & 1 deletion packages/plugin-dominos/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@ai16z/plugin-dominos",
"name": "@elizaos/plugin-dominos",
"version": "0.1.5-alpha.5",
"main": "dist/index.js",
"type": "module",
Expand Down
18 changes: 18 additions & 0 deletions packages/plugin-dominos/src/PizzaOrderManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,4 +764,22 @@ export class PizzaOrderManager implements OrderManager {
};
}
}

getOrderSummary(order: Order, customer: Customer): string {
// Format order details into readable summary
return `Order Summary:\n${order.items.map(item =>
`- ${item.quantity}x ${item.size} ${item.crust} Pizza${
item.toppings?.length ? ` with ${item.toppings.map(t =>
`${t.amount}x ${t.code} (${t.portion})`).join(', ')}` : ''
}`
).join('\n')}`;
}

getNextRequiredActionDialogue(order: Order, customer: Customer): string {
// Return appropriate next step prompt
if (!order.items[0].size) return "What size pizza would you like?";
if (!order.items[0].crust) return "What type of crust would you prefer?";
if (!order.items[0].toppings?.length) return "What toppings would you like?";
return "Would you like to add any more items to your order?";
}
}
114 changes: 74 additions & 40 deletions packages/plugin-dominos/src/actions/confirmOrder.ts
Original file line number Diff line number Diff line change
@@ -1,58 +1,92 @@
import { Action, IAgentRuntime, Memory } from "@elizaos/core";
import { Action, ActionExample, IAgentRuntime, Memory } from "@elizaos/core";
import { PizzaOrderManager } from "../PizzaOrderManager";
import { OrderStatus } from "../types";
import { OrderStatus, PaymentStatus } from "../types";

export const confirmOrder: Action = {
name: "CONFIRM_ORDER",
similes: ["FINALIZE_ORDER", "FINISH_ORDER", "PLACE_ORDER"],
description: "Confirms and places the current pizza order.",
similes: ["PLACE_ORDER", "SUBMIT_ORDER", "FINALIZE_ORDER"],
examples: [
// TODO
],
description: "Confirms and places the final order with Dominos",
validate: async (runtime: IAgentRuntime, message: Memory) => {
[
{
user: "{{user1}}",
content: {
text: "Yes, please place my order",
},
},
{
user: "{{user2}}",
content: {
text: "Great! I'll place your order now. Your pizza will be ready soon!",
action: "CONFIRM_ORDER",
},
},
],
[
{
user: "{{user1}}",
content: {
text: "Everything looks good, confirm the order",
},
},
{
user: "{{user2}}",
content: {
text: "Perfect! I'm confirming your order now. You'll receive a confirmation shortly!",
action: "CONFIRM_ORDER",
},
},
],
] as ActionExample[][],
handler: async (runtime: IAgentRuntime, message: Memory) => {
const orderManager = new PizzaOrderManager(runtime);
const userId = message.userId;

// Get the active order and customer
const order = await orderManager.getOrder(userId);
if (!order) {
return "There is no active order to confirm. Please start a new order first.";
}

const customer = await orderManager.getCustomer(userId);
if (!customer) {
return "Customer details not found. Please provide your information before confirming the order.";
}

if (!order || !customer) return false;
// Validate order status
if (order.status !== OrderStatus.PROCESSING) {
return "The order is not ready to be confirmed. Please complete all required information first.";
}

// Only valid if we have complete customer info and valid payment
return (
order.progress &&
order.progress.hasCustomerInfo &&
order.progress.hasValidPayment &&
!order.progress.isConfirmed
);
},
handler: async (runtime: IAgentRuntime, message: Memory) => {
const orderManager = new PizzaOrderManager(runtime);
const userId = message.userId;
const order = await orderManager.getOrder(userId);
const customer = await orderManager.getCustomer(userId);
// Check payment status
if (order.paymentStatus !== PaymentStatus.VALID) {
return "Please provide valid payment information before confirming the order.";
}

try {
// Final validation with Dominos
await order.validate();
// Process and place the order
const processedOrder = await orderManager.processOrder(order, customer);
if ("type" in processedOrder) {
return `Unable to place order: ${processedOrder.message}`;
}

// Get final pricing
await order.price();
// Update order status
processedOrder.status = OrderStatus.CONFIRMED;
await orderManager.saveOrder(userId, processedOrder);

// Place the order
await order.place();
return "Your order has been confirmed and is being prepared! You'll receive updates on your order status.";
},
validate: async (runtime: IAgentRuntime, message: Memory) => {
const orderManager = new PizzaOrderManager(runtime);
const userId = message.userId;

// Update order status
order.status = OrderStatus.CONFIRMED;
await orderManager.saveOrder(userId, order);
// Get the active order
const order = await orderManager.getOrder(userId);
if (!order) return false;

return (
`Great news! Your order has been confirmed and is being prepared.\n\n` +
`Order Number: ${order.orderID}\n` +
`Estimated Delivery Time: ${order.estimatedWaitMinutes} minutes\n\n` +
orderManager.getOrderSummary(order, customer)
);
} catch (error) {
return "There was an issue placing your order: " + error.message;
}
// Check if order is in a state that can be confirmed
return (
order.status === OrderStatus.PROCESSING &&
order.paymentStatus === PaymentStatus.VALID
);
},
};
2 changes: 1 addition & 1 deletion packages/plugin-dominos/src/actions/startOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Action,
ActionExample,
composeContext,
generateObjectV2,
generateObject,
Handler,
IAgentRuntime,
Memory,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-dominos/src/actions/updateCustomer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Action,
ActionExample,
composeContext,
generateObjectV2,
generateObject,
Handler,
IAgentRuntime,
Memory,
Expand Down Expand Up @@ -85,7 +85,7 @@ Provide updated customer information as a JSON object, including only fields tha
});

try {
const customerUpdates = (await generateObjectV2({
const customerUpdates = (await generateObject({
runtime,
context,
modelClass: ModelClass.LARGE,
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-dominos/src/actions/updateOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
Action,
ActionExample,
composeContext,
generateObjectV2,
generateObject,
Handler,
IAgentRuntime,
Memory,
Expand Down Expand Up @@ -143,7 +143,7 @@ export const handler: Handler = async (
});

try {
const orderUpdates = (await generateObjectV2({
const orderUpdates = (await generateObject({
runtime,
context,
modelClass: ModelClass.LARGE,
Expand Down
Loading

0 comments on commit 2256335

Please sign in to comment.