"""Custom tools used by the LangGraph agent."""
from __future__ import annotations
from typing import Any
def calculate_discount(price: float, discount_percent: float) -> float:
"""Calculate discounted price for the provided discount percent."""
if discount_percent < 0 or discount_percent > 100:
raise ValueError("Discount percent must be in range [0, 100]")
return round(price * (1 - discount_percent / 100), 2)
def format_product(product: dict[str, Any]) -> str:
"""Return a human-readable product representation."""
stock_text = "в наличии" if product.get("in_stock") else "нет в наличии"
return (
f"ID={product.get('id')} | {product.get('name')} | "
f"{product.get('price')} | {product.get('category')} | {stock_text}"
)
def format_products(products: list[dict[str, Any]]) -> str:
"""Format a product list into multiline text."""
if not products:
return "Продукты не найдены."
return "\n".join(format_product(product) for product in products)
def format_statistics(statistics: dict[str, Any]) -> str:
"""Format aggregate product statistics."""
count = statistics.get("count", 0)
average_price = statistics.get("average_price", 0)
return f"Всего продуктов: {count}. Средняя цена: {average_price}"