Cashy / src /tools /account_balance.py
GitHub Actions
Deploy to HF Spaces
17a78b5
import json
import logging
from langchain_core.tools import tool
from src.db.connection import get_connection
logger = logging.getLogger("cashy.tools")
@tool
def get_account_balance(account_name: str) -> str:
"""Get the current balance for a specific account by name (e.g., 'BBVA', 'NU', 'Santander')."""
logger.info("[get_account_balance] account_name=%s", account_name)
try:
with get_connection() as conn:
with conn.cursor() as cur:
cur.execute(
"""
SELECT a.name, a.current_balance, at.name as account_type,
a.currency, a.institution
FROM accounts a
JOIN account_types at ON a.account_type_id = at.id
WHERE a.name ILIKE %s AND a.is_active = true
""",
(f"%{account_name}%",),
)
result = cur.fetchone()
if result:
logger.info("[get_account_balance] Found: %s = %s %s", result[0], result[1], result[3])
return json.dumps(
{
"success": True,
"account_name": result[0],
"balance": float(result[1]),
"account_type": result[2],
"currency": result[3],
"institution": result[4] or "N/A",
},
default=str,
)
else:
logger.warning("[get_account_balance] Account '%s' not found", account_name)
return json.dumps(
{"success": False, "error": f"Account '{account_name}' not found"}
)
except Exception as e:
logger.error("[get_account_balance] Error: %s", e)
return json.dumps({"success": False, "error": str(e)})