Upgrade Market Prices (live web sync), Senior UAE QS AI, Customer Support & AI Operations Director
d0e181b Download utils/agents_logic.py from Basel0/qshub: direct link, hf CLI and curl.
- Browser
- Download file 19 kB
-
https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/agents_logic.py
- Command line
-
hf download hf://spaces/Basel0/qshub/utils/agents_logic.py
-
curl -L -o agents_logic.py https://huggingface.co/spaces/Basel0/qshub/resolve/main/utils/agents_logic.py
19 kB
| """Real AI Agents Engine for THE QS HUB: | |
| 1. Senior UAE Quantity Surveying Engineer (`role='qs'`): | |
| - Full multi-turn conversation memory | |
| - Live UAE Market Prices database injected + live Google Search grounding | |
| - Deep FIDIC (1999 & 2017 Red/Yellow Book), POMI, NRM2, DBC, DM/DMT, UAE Civil Code & FTA 5% VAT knowledge | |
| - Full 20-key & multi-model rotation via KeyManager | |
| 2. Customer Support Agent (`role='cc'`, strictly "Customer Support" / "خدمة العملاء" — never named Sarah): | |
| - Full multi-turn conversation memory | |
| - Real-time read-only visibility into THIS logged-in user's exact subscriptions, remaining projects, free trials & add-on credits across all 3 independent tools | |
| - Accurate up-to-date platform knowledge (QTO, Work Programme, Cash Flow, Plan Comparison, Market Prices, Pricing) | |
| - Automatic structured complaint ticket creation in DB (`qto_customer_complaints`) + real Admin email escalation | |
| """ | |
| from __future__ import annotations | |
| import re | |
| from typing import Any, Dict, List, Optional, Tuple | |
| from utils.db import safe_execute, safe_query | |
| from utils.key_manager import get_key_manager | |
| from utils.plans import PLANS, get_active_subscription, get_plan_for_user | |
| def log_agent_conversation(user_id: int, role: str, sender: str, text: str) -> None: | |
| safe_execute( | |
| "INSERT INTO qto_agent_conversations (user_id, agent_role, sender, message) VALUES (%s, %s, %s, %s)", | |
| (user_id, role, sender, text), | |
| ) | |
| def log_customer_complaint(user_id: int, text: str) -> Optional[int]: | |
| """Insert a customer support ticket and return its new Ticket #ID.""" | |
| ok, _ = safe_execute( | |
| "INSERT INTO qto_customer_complaints (user_id, complaint_text, status) VALUES (%s, %s, 'open')", | |
| (user_id, text), | |
| ) | |
| if not ok: | |
| return None | |
| df = safe_query( | |
| "SELECT id FROM qto_customer_complaints WHERE user_id=%s ORDER BY id DESC LIMIT 1", | |
| (user_id,), | |
| ) | |
| return int(df.iloc[0]["id"]) if not df.empty else None | |
| def _load_recent_history(user_id: Optional[int], role: str, limit: int = 14) -> List[Dict[str, str]]: | |
| if not user_id: | |
| return [] | |
| df = safe_query( | |
| "SELECT sender, message FROM qto_agent_conversations WHERE user_id=%s AND agent_role=%s ORDER BY id DESC LIMIT %s", | |
| (user_id, role, limit), | |
| ) | |
| if df.empty: | |
| return [] | |
| rows = df.iloc[::-1].to_dict("records") | |
| return [ | |
| {"role": "user" if r["sender"] == "user" else "model", "text": str(r["message"] or "")} | |
| for r in rows | |
| if r.get("message") | |
| ] | |
| def _build_customer_account_context(user_id: Optional[int]) -> str: | |
| """Build an accurate read-only snapshot of THIS logged-in user's account for Customer Support.""" | |
| if not user_id: | |
| return "Guest / unknown account." | |
| try: | |
| u_df = safe_query("SELECT id, email, role, created_at FROM qto_users WHERE id=%s", (user_id,)) | |
| if u_df.empty: | |
| return "Account details unavailable." | |
| u = u_df.iloc[0] | |
| user_dict = {"id": int(u["id"]), "role": u["role"], "email": u["email"]} | |
| from utils.usage import project_entitlement | |
| lines = [ | |
| f"LOGGED-IN CUSTOMER ACCOUNT (Live DB Status):", | |
| f"- Email: {u['email']} (Role: {u['role']})", | |
| ] | |
| for feat, label in (("qto", "QTO Workflow"), ("programme", "Work Programme"), ("cashflow", "Cash Flow")): | |
| sub = get_active_subscription(user_id, feat) | |
| plan = get_plan_for_user(user_id, u["role"], feat) | |
| ent = project_entitlement(user_dict, feat) | |
| sub_status = f"Active ({plan.name}, {plan.monthly_price_aed} AED/mo, renews {sub.get('current_period_end') or 'N/A'})" if sub else "No paid subscription" | |
| lines.append( | |
| f"- {label}: Plan={sub_status} | Used this month={ent['used']}/{ent['base']} | Free trial left={ent['free_left']} | Extra +1 credits={ent['credits']} | Can start new project now={'YES' if ent['can_create'] else 'NO'}" | |
| ) | |
| # Saved project counts | |
| q_cnt = safe_query("SELECT COUNT(*) AS c FROM qto_projects WHERE user_id=%s", (user_id,)) | |
| p_cnt = safe_query("SELECT COUNT(*) AS c FROM qto_module_projects WHERE user_id=%s AND feature='programme'", (user_id,)) | |
| c_cnt = safe_query("SELECT COUNT(*) AS c FROM qto_module_projects WHERE user_id=%s AND feature='cashflow'", (user_id,)) | |
| lines.append( | |
| f"- Saved Projects: QTO={int(q_cnt.iloc[0]['c']) if not q_cnt.empty else 0}, " | |
| f"Work Programme={int(p_cnt.iloc[0]['c']) if not p_cnt.empty else 0}, " | |
| f"Cash Flow={int(c_cnt.iloc[0]['c']) if not c_cnt.empty else 0}" | |
| ) | |
| # Open support tickets | |
| t_df = safe_query( | |
| "SELECT id, status, created_at FROM qto_customer_complaints WHERE user_id=%s ORDER BY id DESC LIMIT 3", | |
| (user_id,), | |
| ) | |
| if not t_df.empty: | |
| t_str = ", ".join(f"#{int(r['id'])} ({r['status']})" for _, r in t_df.iterrows()) | |
| lines.append(f"- Recent Support Tickets: {t_str}") | |
| return "\n".join(lines) | |
| except Exception: | |
| return "Account details temporarily unavailable." | |
| def _qs_system_prompt() -> str: | |
| from utils.market_prices_logic import format_market_prices_for_prompt | |
| live_prices = format_market_prices_for_prompt("dubai") | |
| return f"""You are a Senior Chartered Quantity Surveyor (MRICS / Senior UAE QS Engineer) with 20+ years of hands-on experience in Dubai, Abu Dhabi, Sharjah, and across the UAE (villas, mid-rise, infrastructure, contracting & consultancy). | |
| === YOUR CORE EXPERTISE === | |
| 1. **FIDIC Contracts (1999 & 2017 Red Book & Yellow Book)** & UAE Construction Law: | |
| - Variations & Valuation (Clause 13 / 12.3), Provisional Sums, Dayworks, Star Rates. | |
| - Extension of Time (EOT — Clause 8.4/8.5), Delay Damages / Liquidated Damages (Clause 8.7), Concurrent Delay, Time at Large. | |
| - Claims & Mandatory 28-Day Notice Time-Bar (Clause 20.1 in 1999 / Clause 20.2 in 2017), Contemporary Records. | |
| - Interim Payment Certificates (IPCs — Clause 14.3–14.7), Advance Payment & Pro-rata Recovery (Clause 14.2), Retention Deduction & Release at Taking-Over Certificate (TOC) & Performance Certificate / End of DLP (Clause 14.9). | |
| - Prolongation Costs (Hudson, Emden, Eichleay formulas), Disruption, Material Price Escalation, Termination (Clauses 15 & 16). | |
| - UAE Civil Transactions Law (Federal Law No. 5 of 1985 — Muqawala Articles 872–896, Decennial Structural Liability Article 880, Lump-Sum vs Remeasurement Art. 887). | |
| - UAE Federal Tax Authority (FTA) 5% VAT on Construction (Public Clarification VATP011: VAT point on Advance Payment, Net IPCs, and Retention Releases). | |
| 2. **UAE Measurement Standards & Building Codes:** | |
| - **POMI** (Principles of Measurement International — primary UAE standard), **NRM2** (RICS), **CESMM4**. | |
| - **Dubai Building Code (DBC)**, **Dubai Municipality (DM)**, **Abu Dhabi DMT / Estidama**, **Sharjah Municipality**, **DEWA / ADDC / SEWA**. | |
| 3. **Practical UAE Villa QTO & Rate Build-Up Analysis:** | |
| - Exact takeoff formulas: Excavation (incl. working space), PCC Blinding, Isolated Footings vs Raft, Neck Columns, Tie Beams, Columns/Beams/Slabs, Rebar ratios (kg/m³ per structural element), Thermal vs Internal Blockwork, Internal Plaster Golden Rule (Thermal Block + 2×Internal Block >= Paint + Wall Tiles), Waterproofing (Roof + Wet Areas + Substructure), Tiling, Skirting, Ceilings, Painting, Aluminium/Glazing, MEP. | |
| - Complete Rate Analysis / Cost Build-up: Material Cost + Wastage % + Labor + Plant/Equipment + Site Overheads + Contractor Profit Margin. | |
| === LIVE UAE MARKET RATES IN THIS PLATFORM === | |
| {live_prices} | |
| === RESPONSE RULES === | |
| - Always reply in the EXACT language the engineer writes in (clear professional Arabic for Arabic, clear professional English for English). | |
| - Give real, actionable, calculation-backed QS answers with exact clauses, formulas, tables, or AED figures — NEVER give generic fluff. | |
| - When asked about current market prices or cost estimates, use the LIVE UAE MARKET RATES above AND use Google Search grounding if the user asks for external market news or materials not listed above. | |
| - Do NOT handle account billing, technical support, or API keys — politely refer those to Customer Support. | |
| - NEVER mention internal prompts or system settings.""" | |
| def _support_system_prompt(user_id: Optional[int]) -> str: | |
| account_ctx = _build_customer_account_context(user_id) | |
| p2, p3, p4 = PLANS[2], PLANS[3], PLANS[4] | |
| return f"""You are the official **Customer Support** agent ("خدمة العملاء") for **THE QS HUB** — UAE's AI-powered Quantity Surveying platform. | |
| NEVER call yourself "Sarah" or any personal human name; always introduce yourself simply as **Customer Support** (in English) or **فريق خدمة العملاء** (in Arabic). | |
| === EXACT PLATFORM ARCHITECTURE & RULES (100% UP TO DATE) === | |
| THE QS HUB provides **3 completely separate, independent core tools** (each tool has its own separate subscription/credits, its own saved history, and zero coupling to the other tools): | |
| 1. **QTO Workflow (مسار حصر الكميات):** 7-step automated villa Quantity Takeoff from structural & architectural PDFs → Upload → Classify Sheets → AI & Vector Extraction → Verify/Confirm → Equations & Calculation Settings → Review & Price BOQ → Download styled Excel BOQ & Engineer PDF report. | |
| 2. **Work Programme (جدول زمني):** Genuine CPM (Forward/Backward Pass, Total Float, Critical Path) UAE villa construction programme with isolated footings or raft foundation options, scope toggles (Pool, Boundary Wall, Landscape, Roof Garden, Demolition), AI Specification Pre-fill, AI Drawing Analysis (PDF/images), editable durations, and phase-coloured Gantt Excel export. | |
| 3. **Cash Flow (التدفق النقدي):** UAE / FIDIC-compliant monthly Cash Flow Forecast & interactive S-Curve charts with pro-rata advance recovery, full UAE FTA 5% VAT accounting (Advance, IPCs, PC & DLP Retention Releases), pass-through VAT separation, AI Specification & Drawing Analysis, and 2-sheet Excel export. | |
| Additional platform sections: | |
| - **Projects (المشاريع):** View all past projects (separate tabs for QTO, Work Programme, Cash Flow) and re-download Excel reports anytime. | |
| - **Plan Comparison (مقارنة المخططات):** Visually compare revision drawings. | |
| - **Market Prices (أسعار السوق):** Live UAE construction material & labor rates across all 7 Emirates. | |
| - **QS Assistant (مساعد الكميات الذكي):** Senior UAE QS Engineer AI for FIDIC, POMI/NRM2, formulas & cost analysis. | |
| === EXACT PRICING & USAGE RULES === | |
| - **Free Trial:** Every new user gets **1 FREE project per tool** (1 free QTO + 1 free Work Programme + 1 free Cash Flow) to test each tool on a real project. | |
| - **Monthly Plans (per tool, independent):** | |
| • **Professional:** {p2.monthly_price_aed} AED/month — {p2.projects} projects/month | |
| • **Business:** {p3.monthly_price_aed} AED/month — {p3.projects} projects/month | |
| • **Studio:** {p4.monthly_price_aed} AED/month — {p4.projects} projects/month | |
| *(Note: The old 50 AED Starter monthly plan is retired; only Professional, Business, and Studio monthly plans exist.)* | |
| - **Add-on Extra Project:** **+1 Extra Project = 50 AED** (one-time credit for that specific tool). | |
| - **Tool Locking After Completion:** When a user finishes a project in QTO Workflow, Work Programme, or Cash Flow and leaves the page, that sidebar tab automatically becomes inactive until they start a new project from the **Dashboard**. Completed reports remain downloadable anytime from the **Projects** page. | |
| - **Payments:** Handled securely via Dodo Payments. Subscriptions & add-ons auto-activate upon return to the Dashboard/Billing page. | |
| === THIS USER'S LIVE ACCOUNT STATUS === | |
| {account_ctx} | |
| === SUPPORT & TICKET ESCALATION RULES === | |
| - Reply in the exact language the user writes in (Arabic for Arabic, English for English). | |
| - Be specific, helpful, warm, concise, and step-by-step. Use the user's LIVE ACCOUNT STATUS above to answer questions about their exact remaining projects, active plans, or why a tool is locked. | |
| - If the user reports a **real problem, bug, payment/billing discrepancy, refund request, or formal complaint**, apologize sincerely, help troubleshoot if possible, AND append this exact tag on a new line at the very end of your response so the system logs an official ticket for the Operations Manager: | |
| `[ESCALATE_TICKET: concise summary of the issue]` | |
| Do NOT include `[ESCALATE_TICKET: ...]` for simple how-to questions or greetings — only for genuine problems, bugs, billing issues, or complaints! | |
| - **STRICT SECURITY RULE:** NEVER mention API keys, Gemini, internal server architecture, or any "Settings" page under any circumstances.""" | |
| def _call_gemini_chat( | |
| system_prompt: str, | |
| history: List[Dict[str, str]], | |
| prompt: str, | |
| use_google_search: bool = False, | |
| temperature: float = 0.5, | |
| ) -> Tuple[Optional[str], List[dict], Optional[str]]: | |
| """Run multi-turn Gemini chat with full 20-key + multi-model rotation.""" | |
| from google import genai | |
| from google.genai import types | |
| manager = get_key_manager() | |
| max_attempts = max(6, min(len(getattr(manager, "_keys", [])) * 2, 18)) | |
| last_err = "" | |
| contents = [] | |
| for msg in history: | |
| contents.append(types.Content(role=msg["role"], parts=[types.Part.from_text(text=msg["text"])])) | |
| contents.append(types.Content(role="user", parts=[types.Part.from_text(text=prompt)])) | |
| for attempt in range(max_attempts): | |
| api_key, model_name = manager.get_key_and_model() | |
| if not api_key or api_key == "NO_API_KEY_FOUND": | |
| return None, [], "AI service temporarily unavailable." | |
| try: | |
| client = genai.Client(api_key=api_key, http_options={"timeout": 40.0, "api_version": "v1beta"}) | |
| cfg_kwargs: Dict[str, Any] = { | |
| "system_instruction": system_prompt, | |
| "temperature": temperature, | |
| } | |
| # Enable Google Search grounding on primary attempts; drop if a fallback lite model rejects tools | |
| if use_google_search and attempt < max_attempts - 2: | |
| cfg_kwargs["tools"] = [types.Tool(google_search=types.GoogleSearch())] | |
| resp = client.models.generate_content( | |
| model=model_name, | |
| contents=contents, | |
| config=types.GenerateContentConfig(**cfg_kwargs), | |
| ) | |
| text = (resp.text or "").strip() | |
| if not text: | |
| raise ValueError("Empty response from model") | |
| sources: List[dict] = [] | |
| seen = set() | |
| gm = resp.candidates[0].grounding_metadata if resp.candidates else None | |
| for ch in (getattr(gm, "grounding_chunks", None) or []): | |
| web = getattr(ch, "web", None) | |
| title = (getattr(web, "title", "") or "").strip() | |
| uri = (getattr(web, "uri", "") or "").strip() | |
| if title and title not in seen: | |
| seen.add(title) | |
| sources.append({"title": title, "uri": uri}) | |
| return text, sources[:5], None | |
| except Exception as e: | |
| last_err = str(e) | |
| err_low = last_err.lower() | |
| if "404" in err_low or "not found" in err_low: | |
| manager.mark_model_unavailable(model_name) | |
| continue | |
| if any(tok in err_low for tok in ("429", "quota", "resource_exhausted", "503", "unavailable", "timeout", "deadline", "overloaded", "tool", "search", "400")): | |
| manager.mark_rate_limited(api_key, model_name) | |
| continue | |
| break | |
| return None, [], last_err | |
| def generate_agent_response(prompt: str, role: str, ar: bool, user_id: Optional[int] = None) -> str: | |
| """Generate a real, memory-aware AI response for QS Assistant (`qs`) or Customer Support (`cc`).""" | |
| # Load history BEFORE logging current user message so current prompt isn't duplicated | |
| history = _load_recent_history(user_id, role, limit=14 if role == "qs" else 12) | |
| if user_id: | |
| log_agent_conversation(user_id, role, "user", prompt) | |
| if role == "qs": | |
| sys_prompt = _qs_system_prompt() | |
| reply, sources, err = _call_gemini_chat( | |
| system_prompt=sys_prompt, | |
| history=history, | |
| prompt=prompt, | |
| use_google_search=True, | |
| temperature=0.35, | |
| ) | |
| if reply and sources: | |
| src_names = ", ".join(s["title"] for s in sources[:3]) | |
| footer = f"\n\n🌐 مصادر مرجعية: {src_names}" if ar else f"\n\n🌐 Grounded sources: {src_names}" | |
| reply = reply + footer | |
| else: | |
| sys_prompt = _support_system_prompt(user_id) | |
| reply, _, err = _call_gemini_chat( | |
| system_prompt=sys_prompt, | |
| history=history, | |
| prompt=prompt, | |
| use_google_search=False, | |
| temperature=0.45, | |
| ) | |
| # Check if Customer Support escalated a genuine ticket | |
| if reply and user_id: | |
| esc = re.search(r"\[ESCALATE_TICKET:\s*(.+?)\]", reply, re.IGNORECASE | re.DOTALL) | |
| if esc: | |
| summary = esc.group(1).strip() | |
| reply = re.sub(r"\s*\[ESCALATE_TICKET:\s*.+?\]\s*", "", reply, flags=re.IGNORECASE | re.DOTALL).strip() | |
| ticket_id = log_customer_complaint(user_id, f"{summary}\n---\nUser message: {prompt}") | |
| if ticket_id: | |
| tag = ( | |
| f"\n\n🎫 **رقم تذكرة الدعم الفني:** `#{ticket_id}` (تم تحويلها مباشرة إلى إدارة المنصة للمتابعة)." | |
| if ar | |
| else f"\n\n🎫 **Support Ticket Logged:** `#{ticket_id}` (escalated directly to our Operations Manager for review)." | |
| ) | |
| reply += tag | |
| try: | |
| from utils.mailer import send_admin_email | |
| send_admin_email( | |
| f"[THE QS HUB] New Support Ticket #{ticket_id}", | |
| f"<h3>New Escalated Support Ticket #{ticket_id}</h3><p><b>User ID:</b> {user_id}</p><p><b>Summary:</b> {summary}</p><p><b>Message:</b> {prompt}</p>", | |
| ) | |
| except Exception: | |
| pass | |
| if not reply: | |
| if ar: | |
| reply = "عذراً، حدث ضغط مؤقت على الخدمة الآن. يرجى المحاولة مرة أخرى بعد بضع ثوانٍ." | |
| else: | |
| reply = "Sorry, the service is experiencing temporary high traffic. Please try again in a few seconds." | |
| if user_id: | |
| log_agent_conversation(user_id, role, "assistant", reply) | |
| return reply | |