Spaces:
Sleeping
Sleeping
File size: 9,155 Bytes
aa6cac1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | from agents import function_tool
from src.BusinessModel.models import OrderDatabase
# Initialize database connection
db = OrderDatabase()
# ============ PRODUCT AVAILABILITY TOOLS ============
@function_tool
def check_product_availability(product_id: str) -> str:
"""
Check if a product is in stock and get availability details.
Args:
product_id: The product ID (e.g., "PROD001")
Returns:
Product availability information including stock levels
"""
result = db.get_product(product_id)
if result["success"]:
p = result
status = "β
IN STOCK" if p["is_in_stock"] else "β OUT OF STOCK"
response = f"""Product: {p['name']} ({product_id})
{status}
π¦ Stock Information:
- Available: {p['available_quantity']} units
- Reserved: {p['reserved_quantity']} units
- Total Stock: {p['stock_quantity']} units
- Price: ${p['price']:.2f}
- Category: {p['category']}
"""
if p["is_low_stock"] and p["is_in_stock"]:
response += f"\nβ οΈ LOW STOCK WARNING: Only {p['available_quantity']} units remaining!"
return response
else:
return f"Sorry, I couldn't find product {product_id}. {result.get('error', '')}"
@function_tool
def search_products_by_name(search_term: str, in_stock_only: bool = False) -> str:
"""
Search for products by name.
Args:
search_term: Product name or keyword to search
in_stock_only: If True, only show products in stock
Returns:
List of matching products with availability
"""
result = db.search_products(search_term=search_term, in_stock_only=in_stock_only)
if result["success"]:
if not result["products"]:
return f"No products found matching '{search_term}'."
response = f"Found {len(result['products'])} product(s) matching '{search_term}':\n\n"
for p in result["products"]:
stock_icon = "β
" if p["is_in_stock"] else "β"
response += f"{stock_icon} {p['name']} (ID: {p['product_id']})\n"
response += f" Price: ${p['price']:.2f} | Available: {p['available_quantity']} units\n"
response += f" Category: {p['category']}\n\n"
return response
else:
return f"Error searching products: {result.get('error', 'Unknown error')}"
@function_tool
def search_products_by_category(category: str, in_stock_only: bool = False) -> str:
"""
Get all products in a specific category.
Args:
category: Category name (e.g., "Electronics", "Clothing")
in_stock_only: If True, only show products in stock
Returns:
List of products in the category
"""
result = db.search_products(category=category, in_stock_only=in_stock_only)
if result["success"]:
if not result["products"]:
return f"No products found in category '{category}'."
response = f"Products in '{category}' category ({len(result['products'])} items):\n\n"
for p in result["products"]:
stock_icon = "β
" if p["is_in_stock"] else "β"
response += f"{stock_icon} {p['name']} (ID: {p['product_id']})\n"
response += f" Price: ${p['price']:.2f} | Available: {p['available_quantity']} units\n\n"
return response
else:
return f"Error searching category: {result.get('error', 'Unknown error')}"
@function_tool
def get_product_details(product_id: str) -> str:
"""
Get complete product information including description and pricing.
Args:
product_id: The product ID
Returns:
Detailed product information
"""
result = db.get_product(product_id)
if result["success"]:
p = result
response = f"""π¦ Product Details
Name: {p['name']}
ID: {p['product_id']}
Price: ${p['price']:.2f}
Category: {p['category']}
Description:
{p['description']}
π Inventory Status:
- Available: {p['available_quantity']} units
- Reserved: {p['reserved_quantity']} units
- Total in Stock: {p['stock_quantity']} units
- Status: {'β
IN STOCK' if p['is_in_stock'] else 'β OUT OF STOCK'}
"""
if p["is_low_stock"] and p["is_in_stock"]:
response += f"\nβ οΈ LOW STOCK: Only {p['available_quantity']} units left!"
return response
else:
return f"Product {product_id} not found. {result.get('error', '')}"
# ============ STOCK RESERVATION TOOLS ============
@function_tool
def reserve_product(product_id: str, quantity: int, customer_id: str) -> str:
"""
Reserve stock for a customer during checkout (holds for 15 minutes).
Args:
product_id: The product to reserve
quantity: Number of units to reserve
customer_id: Customer ID making the reservation
Returns:
Reservation confirmation or error message
"""
result = db.reserve_stock(product_id, quantity, customer_id)
if result["success"]:
return f"""β
Reservation Successful!
Product ID: {product_id}
Quantity Reserved: {quantity} units
Customer: {customer_id}
Reservation ID: {result['reservation_id']}
β° This reservation will expire in 15 minutes: {result['expires_at']}
Please complete your purchase before the reservation expires."""
else:
return f"β Unable to reserve product: {result.get('error', 'Unknown error')}"
@function_tool
def cancel_reservation(reservation_id: int) -> str:
"""
Cancel a product reservation and return stock to available inventory.
Args:
reservation_id: The reservation ID to cancel
Returns:
Cancellation confirmation
"""
result = db.release_reservation(reservation_id)
if result["success"]:
return f"β
Reservation #{reservation_id} has been cancelled. Stock returned to inventory."
else:
return f"β Unable to cancel reservation: {result.get('error', 'Unknown error')}"
# ============ STOCK MANAGEMENT TOOLS ============
@function_tool
def update_product_stock(product_id: str, quantity_change: int, reason: str = "manual adjustment") -> str:
"""
Add or remove stock from inventory (for staff use).
Args:
product_id: The product ID
quantity_change: Positive number to add stock, negative to remove
reason: Reason for the change (e.g., "restock", "damaged goods")
Returns:
Stock update confirmation
"""
result = db.update_stock(product_id, quantity_change, reason)
if result["success"]:
action = "added to" if quantity_change > 0 else "removed from"
return f"""β
Stock Updated Successfully
Product ID: {product_id}
Previous Stock: {result['previous_stock']} units
Change: {abs(quantity_change)} units {action} inventory
New Stock: {result['new_stock']} units
Reason: {reason}
"""
else:
return f"β Unable to update stock: {result.get('error', 'Unknown error')}"
@function_tool
def get_low_stock_alerts() -> str:
"""
Get all products that are running low on stock.
Returns:
List of products below their low stock threshold
"""
result = db.get_low_stock_products()
if result["success"]:
if not result["products"]:
return "β
All products are adequately stocked!"
response = f"β οΈ LOW STOCK ALERT - {len(result['products'])} product(s) need restocking:\n\n"
for p in result["products"]:
response += f"π΄ {p['name']} (ID: {p['product_id']})\n"
response += f" Available: {p['available_quantity']} units\n"
response += f" Threshold: {p['threshold']} units\n"
response += f" Action needed: Restock recommended\n\n"
return response
else:
return f"Error checking stock levels: {result.get('error', 'Unknown error')}"
# ============ PRODUCT MANAGEMENT TOOLS ============
@function_tool
def add_new_product(product_id: str, name: str, description: str,
price: float, category: str, initial_stock: int = 0) -> str:
"""
Add a new product to the inventory system.
Args:
product_id: Unique product identifier
name: Product name
description: Product description
price: Product price
category: Product category
initial_stock: Starting stock quantity (default: 0)
Returns:
Confirmation of product creation
"""
result = db.add_product(
product_id=product_id,
name=name,
description=description,
price=price,
category=category,
stock_quantity=initial_stock
)
if result["success"]:
return f"""β
New Product Added Successfully!
Product ID: {product_id}
Name: {name}
Price: ${price:.2f}
Category: {category}
Initial Stock: {initial_stock} units
The product is now available in the inventory system."""
else:
return f"β Unable to add product: {result.get('error', 'Unknown error')}" |