The CRM Agent is an AI-powered service that enables natural language interactions with CRM data. It understands queries like “Show me all tech companies with revenue over $10M” and translates them into database operations.Location: scripts/crm_agent.py (51KB, 1,300+ lines)
# Natural language input"Find all technology companies in San Francisco with revenue over $5M"# Generated SQLSELECT * FROM companiesWHERE industry = 'Technology'AND location LIKE '%San Francisco%'AND revenue > 5000000
# Natural language input"Create a new company called Acme Corp in the software industry"# Generated operationINSERT INTO companies (name, industry, created_at)VALUES ('Acme Corp', 'Software', NOW())
# Natural language input"What's the average deal size for Q4 2024?"# Generated SQLSELECT AVG(deal_value) as avg_deal_sizeFROM dealsWHERE created_at >= '2024-10-01'AND created_at < '2025-01-01'
# Natural language input"Show me all contacts at Acme Corp who are decision makers"# Generated SQLSELECT c.* FROM contacts cJOIN companies co ON c.company_id = co.idWHERE co.name = 'Acme Corp'AND c.role IN ('CEO', 'CTO', 'VP', 'Director')
# "Find all companies in healthcare, then show their active deals"# Step 1: Query companies# Step 2: Query deals for those companies# Step 3: Aggregate results
try: result = agent.execute_query(query)except SchemaError: return "I couldn't understand that query. Could you rephrase?"except PermissionError: return "You don't have permission to access that data."except DatabaseError: return "There was an error accessing the database."
# Ambiguous query"Show me deals"# Agent response"I found multiple interpretations:1. All deals in your CRM2. Active deals only3. Recent deals (last 30 days)Which would you like to see?"
# ✅ Safe - Parameterizedcursor.execute("SELECT * FROM companies WHERE name = %s", (company_name,))# ❌ Unsafe - String interpolationcursor.execute(f"SELECT * FROM companies WHERE name = '{company_name}'")
from scripts.crm_agent import CRMAgentagent = CRMAgent(supabase, anthropic)# Execute queryresult = agent.execute_query( "Find all companies with revenue over $5M", firm_id="uuid")print(result['summary'])print(result['results'])
# RequiredANTHROPIC_API_KEY=sk-ant-xxxxxSUPABASE_URL=https://project.supabase.coSUPABASE_KEY=your-key# OptionalCRM_AGENT_CACHE_TTL=3600 # Cache TTL in secondsCRM_AGENT_MAX_RESULTS=100 # Max results per query