-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhance existing actions and Order Manager
- Loading branch information
Showing
7 changed files
with
203 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.