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_all_accounts(include_inactive: bool = False) -> str: """Get a list of all user accounts with their balances, types, and institutions.""" logger.info("[get_all_accounts] include_inactive=%s", include_inactive) try: with get_connection() as conn: with conn.cursor() as cur: query = """ SELECT a.name, a.current_balance, at.name as account_type, a.currency, a.institution, a.is_active FROM accounts a JOIN account_types at ON a.account_type_id = at.id """ if not include_inactive: query += " WHERE a.is_active = true" query += " ORDER BY at.name, a.name" cur.execute(query) columns = [desc[0] for desc in cur.description] rows = cur.fetchall() results = [dict(zip(columns, row)) for row in rows] logger.info("[get_all_accounts] Returned %d accounts", len(results)) return json.dumps( {"success": True, "count": len(results), "accounts": results}, default=str, ) except Exception as e: logger.error("[get_all_accounts] Error: %s", e) return json.dumps({"success": False, "error": str(e)})