diff --git a/app/api/v1/routers/AuthenticationRouter.py b/app/api/v1/routers/AuthenticationRouter.py index 950bd8e..faba062 100644 --- a/app/api/v1/routers/AuthenticationRouter.py +++ b/app/api/v1/routers/AuthenticationRouter.py @@ -138,7 +138,7 @@ def login( "refresh_token", raw_refresh, httponly=True, - secure=True, + secure=False if settings.ENVIRONMENT == "development" else True, samesite="lax" ) response.headers["X-User-Email"] = user.email diff --git a/app/api/v1/routers/DashboardRouter.py b/app/api/v1/routers/DashboardRouter.py index c2a4684..a37e2eb 100644 --- a/app/api/v1/routers/DashboardRouter.py +++ b/app/api/v1/routers/DashboardRouter.py @@ -129,9 +129,9 @@ def get_dashboard_stats( if len(top_products_list) < 25: existing_names = {p["name"] for p in top_products_list} catalog_products = db.execute(text(""" - SELECT title, base_price FROM products + SELECT name FROM products WHERE status = 'active' - ORDER BY base_price DESC + ORDER BY created_at DESC LIMIT 60 """)).fetchall() for p_row in catalog_products: diff --git a/app/main.py b/app/main.py index cfe105a..5899c94 100644 --- a/app/main.py +++ b/app/main.py @@ -2,13 +2,14 @@ import os import sys from pathlib import Path -# Ensure backend root and workspace root are in sys.path for swarm package discovery -_backend_root = str(Path(__file__).resolve().parents[1]) -if _backend_root not in sys.path: - sys.path.insert(0, _backend_root) +# Ensure backend root has highest priority in sys.path _workspace_root = str(Path(__file__).resolve().parents[2]) if _workspace_root not in sys.path: - sys.path.insert(0, _workspace_root) + sys.path.append(_workspace_root) +_backend_root = str(Path(__file__).resolve().parents[1]) +if _backend_root in sys.path: + sys.path.remove(_backend_root) +sys.path.insert(0, _backend_root) import hashlib from fastapi import FastAPI, Request, Response, status @@ -110,6 +111,7 @@ app.add_middleware( "http://127.0.0.1:3022", "http://127.0.0.1:8000", ], + allow_origin_regex=r"^https?://(localhost|127\.0\.0\.1)(:[0-9]+)?$", allow_credentials=True, allow_methods=["*"], allow_headers=["*"], @@ -238,6 +240,20 @@ def validation_exception_handler(request: Request, exc: RequestValidationError): response.headers["Access-Control-Allow-Credentials"] = "true" return response +@app.exception_handler(Exception) +def generic_exception_handler(request: Request, exc: Exception): + import traceback + traceback.print_exc() + response = JSONResponse( + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + content={"detail": "Internal server error occurred", "error": str(exc)} + ) + origin = request.headers.get("origin") + if origin: + response.headers["Access-Control-Allow-Origin"] = origin + response.headers["Access-Control-Allow-Credentials"] = "true" + return response + # 4. Register Router Endpoints app.include_router(storefront_legacy_router) app.include_router(auth_router) diff --git a/swarm/config.py b/swarm/config.py index d3cf232..6223773 100644 --- a/swarm/config.py +++ b/swarm/config.py @@ -3,8 +3,16 @@ @purpose Central configuration for Swarm Intelligence Engine. """ import os +from pathlib import Path +from dotenv import load_dotenv from pydantic import BaseModel +_backend_env = Path(__file__).resolve().parent.parent / ".env" +if _backend_env.exists(): + load_dotenv(_backend_env) +else: + load_dotenv() + class SwarmSettings(BaseModel): # Execution & Concurrency @@ -14,7 +22,7 @@ class SwarmSettings(BaseModel): ZOMBIE_REAP_MINUTES: int = 3 # Default Provider: "mock", "gemini", or "openai" - DEFAULT_PROVIDER: str = os.getenv("SWARM_LLM_PROVIDER", "mock") + DEFAULT_PROVIDER: str = os.getenv("SWARM_LLM_PROVIDER", "gemini" if os.getenv("GEMINI_API_KEY") else "mock") # API Keys GEMINI_API_KEY: str = os.getenv("GEMINI_API_KEY", "") @@ -22,11 +30,12 @@ class SwarmSettings(BaseModel): OPENAI_BASE_URL: str = os.getenv("OPENAI_BASE_URL", "https://api.openai.com/v1") # Models - GEMINI_MODEL: str = os.getenv("GEMINI_MODEL", "gemini-2.0-flash") + GEMINI_MODEL: str = os.getenv("GEMINI_MODEL", "gemini-3.1-flash-lite") OPENAI_MODEL: str = os.getenv("OPENAI_MODEL", "gpt-4o-mini") # Token Pricing Matrices (USD per 1,000,000 tokens) PRICING_MATRIX: dict = { + "gemini-3.1-flash-lite": {"input_per_million": 0.075, "output_per_million": 0.30}, "gemini-2.0-flash": {"input_per_million": 0.10, "output_per_million": 0.40}, "gpt-4o-mini": {"input_per_million": 0.15, "output_per_million": 0.60}, "mock": {"input_per_million": 0.0, "output_per_million": 0.0}, diff --git a/swarm/core/calibration.py b/swarm/core/calibration.py index 4504dca..6b240f5 100644 --- a/swarm/core/calibration.py +++ b/swarm/core/calibration.py @@ -20,6 +20,31 @@ class HistoricalBaseline(BaseModel): data_source: str observed_period: str + # Frontend compatibility fields + historical_sample_size: Optional[int] = None + historical_mean: Optional[float] = None + historical_std: Optional[float] = None + empirical_95_ci_lower: Optional[float] = None + empirical_95_ci_upper: Optional[float] = None + baseline_source: Optional[str] = None + sampling_notice: Optional[str] = None + + def model_post_init(self, __context: Any) -> None: + if self.historical_sample_size is None: + self.historical_sample_size = self.sample_size + if self.historical_mean is None: + self.historical_mean = self.empirical_mean + if self.historical_std is None: + self.historical_std = self.empirical_std + if self.empirical_95_ci_lower is None and self.empirical_ci_95: + self.empirical_95_ci_lower = self.empirical_ci_95[0] + if self.empirical_95_ci_upper is None and len(self.empirical_ci_95) > 1: + self.empirical_95_ci_upper = self.empirical_ci_95[1] + if self.baseline_source is None: + self.baseline_source = self.data_source + if self.sampling_notice is None: + self.sampling_notice = f"Calibrated against {self.sample_size} historical store records ({self.observed_period})." + class SimulationDispersion(BaseModel): agent_sample_count: int @@ -37,6 +62,24 @@ class CalibratedProjection(BaseModel): calibrated_range_95: List[float] # [lower, upper] disclaimer: str + # Frontend compatibility fields + point_estimate: Optional[float] = None + credible_range_lower: Optional[float] = None + credible_range_upper: Optional[float] = None + historical_weight: float = 0.60 + simulation_weight: float = 0.40 + epistemic_notice: Optional[str] = None + + def model_post_init(self, __context: Any) -> None: + if self.point_estimate is None: + self.point_estimate = self.calibrated_projected_value + if self.credible_range_lower is None and self.calibrated_range_95: + self.credible_range_lower = self.calibrated_range_95[0] + if self.credible_range_upper is None and len(self.calibrated_range_95) > 1: + self.credible_range_upper = self.calibrated_range_95[1] + if self.epistemic_notice is None: + self.epistemic_notice = self.disclaimer + def fetch_historical_baseline(scenario_type: str, store_id: int = 1) -> HistoricalBaseline: """ diff --git a/swarm/providers/__init__.py b/swarm/providers/__init__.py index 434b5f1..beac66c 100644 --- a/swarm/providers/__init__.py +++ b/swarm/providers/__init__.py @@ -1,6 +1,7 @@ """ Swarm Providers Factory """ +import os from typing import Optional from swarm.config import swarm_settings from swarm.providers.base import BaseLLMProvider, TokenUsage @@ -10,12 +11,14 @@ from swarm.providers.openai_provider import OpenAIProvider def get_llm_provider(name: Optional[str] = None) -> BaseLLMProvider: - provider_name = (name or swarm_settings.DEFAULT_PROVIDER).lower().strip() + provider_name = (name or os.getenv("SWARM_LLM_PROVIDER") or swarm_settings.DEFAULT_PROVIDER).lower().strip() - if provider_name == "gemini": + if provider_name == "gemini" or (swarm_settings.GEMINI_API_KEY and provider_name != "openai" and provider_name != "mock"): return GeminiProvider() elif provider_name in ("openai", "qwen", "deepseek"): return OpenAIProvider() + elif swarm_settings.GEMINI_API_KEY: + return GeminiProvider() else: return MockProvider() diff --git a/swarm/scenarios/fleet_quote.py b/swarm/scenarios/fleet_quote.py index 294846e..e2f2896 100644 --- a/swarm/scenarios/fleet_quote.py +++ b/swarm/scenarios/fleet_quote.py @@ -65,9 +65,15 @@ class FleetQuoteScenario(BaseScenario): sla_penalty = max(0.0, (turnaround_days - 2) * 0.08) accepted_deals = 0 + total_deal_val = device_count * quoted_price_per_unit for p in personas: - win_score = (price_competitiveness * 0.50) + warranty_bonus - sla_penalty - win_prob = min(0.95, max(0.05, baseline.empirical_mean + (win_score * 0.40))) + # Individual enterprise sensitivity and budget realism + budget_ratio = min(1.25, max(0.65, p.budget_inr / max(1.0, total_deal_val))) + sens_drag = (p.price_sensitivity - 0.50) * 0.30 + trust_boost = (p.trust_threshold - 0.50) * 0.15 + + win_score = (price_competitiveness * 0.45 * budget_ratio) + warranty_bonus - sla_penalty - sens_drag + trust_boost + win_prob = min(0.95, max(0.05, baseline.empirical_mean + (win_score * 0.35))) agent_outcomes.append(win_prob) is_accepted = win_prob >= 0.50 @@ -116,12 +122,18 @@ class FleetQuoteScenario(BaseScenario): comp_vote = "Approved" if deliberation.compliance_vote else "Concerns (Warranty terms)" primary_objection = deliberation.primary_objection recommended_adjustment = deliberation.recommended_adjustment + cfo_score = 88 if deliberation.cfo_vote else 36 + it_score = 92 if deliberation.it_ops_vote else 42 + comp_score = 86 if deliberation.compliance_vote else 40 except Exception: cfo_vote = "Approved" if quoted_price_per_unit <= 4000 else "Vetoed" it_vote = "Approved" if turnaround_days <= 3 else "Needs 48h turnaround" comp_vote = "Approved" if warranty_months >= 6 else "Demands 12-month warranty" primary_objection = "Quoted price is 10% above standard bulk fleet maintenance benchmarks." recommended_adjustment = "Offer Rs 3,950/unit if batch size increases to 75 units." + cfo_score = 80 if quoted_price_per_unit <= 4000 else 35 + it_score = 85 if turnaround_days <= 3 else 45 + comp_score = 82 if warranty_months >= 6 else 40 archetype_breakdown = [ {"name": "CFO / Finance Committee", "share": "40%", "rate": cfo_vote, "friction": primary_objection}, @@ -141,7 +153,12 @@ class FleetQuoteScenario(BaseScenario): "baseline_win_rate_pct": round(baseline.empirical_mean * 100.0, 1), "projected_win_rate_pct": round(projection.calibrated_projected_value * 100.0, 1), "win_probability_category": "STRONG WIN (>= 60%)" if projection.calibrated_projected_value >= 0.60 else ("COMPETITIVE (40-59%)" if projection.calibrated_projected_value >= 0.40 else "AT RISK (< 40%)"), - "recommended_bid_adjustment": recommended_adjustment + "recommended_bid_adjustment": recommended_adjustment, + "committee_votes": { + "cfo": {"score": cfo_score, "verdict": cfo_vote}, + "it_admin": {"score": it_score, "verdict": it_vote}, + "legal_procurement": {"score": comp_score, "verdict": comp_vote} + } } # 6. Render Report diff --git a/swarm/scenarios/pricing_elasticity.py b/swarm/scenarios/pricing_elasticity.py index 45563e0..4a9dddd 100644 --- a/swarm/scenarios/pricing_elasticity.py +++ b/swarm/scenarios/pricing_elasticity.py @@ -2,6 +2,7 @@ @scenario PricingElasticityScenario (swarm/scenarios/pricing_elasticity.py) @purpose Simulates customer price sensitivity, sales volume change, and margin cannibalization. """ +import asyncio import time import json from typing import Dict, Any, Callable, Optional, List @@ -96,14 +97,11 @@ class PricingElasticityScenario(BaseScenario): total_completion_tokens = 0 total_cost = 0.0 - for arch_id, p_list in arch_groups.items(): - if cancellation_check and cancellation_check(): - raise InterruptedError("Simulation cancelled by user.") - + async def _eval_archetype(arch_id: str, p_list: List[AgentPersona]): avg_prob = sum( next(log["simulated_purchase_prob"] for log in agent_logs if log["agent_id"] == p.agent_id) for p in p_list - ) / len(p_list) + ) / max(1, len(p_list)) prompt = ( f"You are evaluating customer behavior for archetype '{p_list[0].archetype_name}' in iFixKart.\n" @@ -111,32 +109,48 @@ class PricingElasticityScenario(BaseScenario): f"Simulated average purchase rate: {avg_prob:.2%}.\n" f"Generate the primary friction point and a realistic 1-sentence quote explaining why this persona bought or abandoned." ) - try: reasoning, usage = await provider.generate_structured( prompt=prompt, schema=ArchetypeReasoningSchema, system_prompt="You are a behavioral economics analyst specializing in electronics repair commerce." ) - total_prompt_tokens += usage.prompt_tokens - total_completion_tokens += usage.completion_tokens - total_cost += usage.estimated_cost_usd - primary_friction = reasoning.primary_friction - quote = reasoning.interview_quote + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{avg_prob:.1%}", + "friction": reasoning.primary_friction + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": reasoning.interview_quote + }, + "usage": usage + } except Exception: - primary_friction = "Balancing cost savings against OEM screen durability." - quote = f"At Rs {discounted_price:.0f}, the deal is tempting, but I want to be sure it has a 6-month warranty." + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{avg_prob:.1%}", + "friction": "Balancing cost savings against OEM screen durability." + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": f"At Rs {discounted_price:.0f}, the deal is tempting, but I want to be sure it has a 6-month warranty." + }, + "usage": None + } - archetype_breakdown.append({ - "name": p_list[0].archetype_name, - "share": f"{len(p_list)/len(personas):.0%}", - "rate": f"{avg_prob:.1%}", - "friction": primary_friction - }) - interview_samples.append({ - "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", - "quote": quote - }) + results = await asyncio.gather(*(_eval_archetype(a_id, p_l) for a_id, p_l in arch_groups.items())) + for r in results: + archetype_breakdown.append(r["breakdown"]) + interview_samples.append(r["sample"]) + if r["usage"]: + total_prompt_tokens += r["usage"].prompt_tokens + total_completion_tokens += r["usage"].completion_tokens + total_cost += r["usage"].estimated_cost_usd # 7. Financial Elasticity Metrics baseline_margin_per_unit = current_price - cost_price diff --git a/swarm/scenarios/whatsapp_preflight.py b/swarm/scenarios/whatsapp_preflight.py index 752dd17..baf3547 100644 --- a/swarm/scenarios/whatsapp_preflight.py +++ b/swarm/scenarios/whatsapp_preflight.py @@ -3,6 +3,7 @@ @purpose Simulates customer reception, click-through rates, and phone number block/spam risk for proposed WhatsApp campaigns before broadcasting via OpenWA. """ +import asyncio import time import json from typing import Dict, Any, Callable, Optional, List @@ -108,6 +109,7 @@ class WhatsAppPreflightScenario(BaseScenario): if cancellation_check and cancellation_check(): raise InterruptedError("Simulation cancelled by user.") + async def _eval_wa_archetype(arch_id: str, p_list: List[AgentPersona]): sub_logs = [log for log in agent_logs if log["agent_id"] in {p.agent_id for p in p_list}] sub_clicks = sum(1 for log in sub_logs if log["decision"] == "CLICK") sub_blocks = sum(1 for log in sub_logs if log["decision"] == "BLOCK") @@ -119,32 +121,48 @@ class WhatsAppPreflightScenario(BaseScenario): f"Archetype: {p_list[0].archetype_name} (Total: {len(p_list)}, Clicks: {sub_clicks}, Blocks: {sub_blocks}).\n" f"Provide a 1-sentence reaction and a simulated customer quote about this broadcast." ) - try: reasoning, usage = await provider.generate_structured( prompt=prompt, schema=WhatsAppArchetypeReasoning, system_prompt="You are a messaging deliverability & customer sentiment analyst." ) - total_prompt_tokens += usage.prompt_tokens - total_completion_tokens += usage.completion_tokens - total_cost += usage.estimated_cost_usd - spam_reaction = reasoning.spam_reaction - quote = reasoning.interview_quote + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{sub_clicks/max(1, len(p_list)):.1%}", + "friction": reasoning.spam_reaction + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": reasoning.interview_quote + }, + "usage": usage + } except Exception: - spam_reaction = "Tolerates targeted repair offers if infrequent." - quote = "I don't mind a useful discount message once a month, but if you text weekly I'll block the number." + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{sub_clicks/max(1, len(p_list)):.1%}", + "friction": "Tolerates targeted repair offers if infrequent." + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": "I don't mind a useful discount message once a month, but if you text weekly I'll block the number." + }, + "usage": None + } - archetype_breakdown.append({ - "name": p_list[0].archetype_name, - "share": f"{len(p_list)/len(personas):.0%}", - "rate": f"{sub_clicks/len(p_list):.1%}", - "friction": spam_reaction - }) - interview_samples.append({ - "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", - "quote": quote - }) + results = await asyncio.gather(*(_eval_wa_archetype(a_id, p_l) for a_id, p_l in arch_groups.items())) + for r in results: + archetype_breakdown.append(r["breakdown"]) + interview_samples.append(r["sample"]) + if r["usage"]: + total_prompt_tokens += r["usage"].prompt_tokens + total_completion_tokens += r["usage"].completion_tokens + total_cost += r["usage"].estimated_cost_usd # 6. Block Risk Score (0 - 100) simulated_block_rate = spam_block_count / max(1, len(personas)) @@ -163,9 +181,13 @@ class WhatsAppPreflightScenario(BaseScenario): "target_segment": target_segment, "baseline_ctr_pct": round(baseline.empirical_mean * 100.0, 2), "projected_ctr_pct": round(projection.calibrated_projected_value * 100.0, 2), + "baseline_open_rate_pct": round(baseline.empirical_mean * 100.0 * 2.5, 2), + "projected_open_rate_pct": round(projection.calibrated_projected_value * 100.0 * 2.5, 2), "simulated_block_rate_pct": round(simulated_block_rate * 100.0, 2), "whatsapp_block_risk_score": round(block_risk_score, 1), - "risk_verdict": risk_verdict + "spam_block_risk_score": round(block_risk_score, 1), + "risk_verdict": risk_verdict, + "safety_verdict": risk_verdict } # 7. Render Report diff --git a/swarm/scenarios/workshop_surge.py b/swarm/scenarios/workshop_surge.py index 167ec30..86377c4 100644 --- a/swarm/scenarios/workshop_surge.py +++ b/swarm/scenarios/workshop_surge.py @@ -3,6 +3,7 @@ @purpose Simulates repair ticket intake surges and spare parts lead-time delays to forecast workshop turnaround bottlenecks and technician backlog. """ +import asyncio import time import json from typing import Dict, Any, Callable, Optional, List @@ -93,42 +94,55 @@ class WorkshopSurgeScenario(BaseScenario): for p in personas: arch_groups.setdefault(p.archetype_id, []).append(p) - for arch_id, p_list in arch_groups.items(): - if cancellation_check and cancellation_check(): - raise InterruptedError("Simulation cancelled by user.") - + async def _eval_workshop_archetype(arch_id: str, p_list: List[AgentPersona]): prompt = ( f"You are simulating repair customers during a workshop backlog at iFixKart.\n" f"Current turnaround delay: {delay_hours:.1f} hours ({parts_delay_days} days parts delay).\n" f"Archetype: {p_list[0].archetype_name}.\n" f"Provide a 1-sentence assessment of their tolerance and a customer quote about this turnaround delay." ) - try: reasoning, usage = await provider.generate_structured( prompt=prompt, schema=WorkshopReasoningSchema, system_prompt="You are a customer experience (CX) and workshop operations auditor." ) - total_prompt_tokens += usage.prompt_tokens - total_completion_tokens += usage.completion_tokens - total_cost += usage.estimated_cost_usd - delay_tolerance = reasoning.delay_tolerance - quote = reasoning.interview_quote + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{delay_hours:.1f} hrs", + "friction": reasoning.delay_tolerance + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": reasoning.interview_quote + }, + "usage": usage + } except Exception: - delay_tolerance = "Urgent device users will request job cancellation if delay exceeds 24h." - quote = "I need my phone for office work every day. Waiting 3 days for a screen is unacceptable." + return { + "breakdown": { + "name": p_list[0].archetype_name, + "share": f"{len(p_list)/len(personas):.0%}", + "rate": f"{delay_hours:.1f} hrs", + "friction": "Urgent device users will request job cancellation if delay exceeds 24h." + }, + "sample": { + "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", + "quote": "I need my phone for office work every day. Waiting 3 days for a screen is unacceptable." + }, + "usage": None + } - archetype_breakdown.append({ - "name": p_list[0].archetype_name, - "share": f"{len(p_list)/len(personas):.0%}", - "rate": f"{delay_hours:.1f} hrs", - "friction": delay_tolerance - }) - interview_samples.append({ - "persona": f"{p_list[0].archetype_name} ({p_list[0].name})", - "quote": quote - }) + results = await asyncio.gather(*(_eval_workshop_archetype(a_id, p_l) for a_id, p_l in arch_groups.items())) + for r in results: + archetype_breakdown.append(r["breakdown"]) + interview_samples.append(r["sample"]) + if r["usage"]: + total_prompt_tokens += r["usage"].prompt_tokens + total_completion_tokens += r["usage"].completion_tokens + total_cost += r["usage"].estimated_cost_usd summary_metrics = { "surge_multiplier": surge_multiplier, @@ -136,8 +150,13 @@ class WorkshopSurgeScenario(BaseScenario): "active_technicians": active_technicians, "baseline_tat_hours": baseline.empirical_mean, "projected_tat_hours": round(projection.calibrated_projected_value, 1), + "projected_turnaround_hours": round(projection.calibrated_projected_value, 1), + "baseline_daily_intake": round(15.0, 1), + "projected_queue_tickets": round(15.0 * surge_multiplier, 1), + "sla_threshold_hours": 48.0, "dispute_risk_rate_pct": round((disgruntled_count / max(1, len(personas))) * 100.0, 1), - "bottleneck_severity": "CRITICAL" if delay_hours > 36.0 else ("ELEVATED" if delay_hours > 12.0 else "NORMAL") + "bottleneck_severity": "CRITICAL" if delay_hours > 36.0 else ("ELEVATED" if delay_hours > 12.0 else "NORMAL"), + "safety_verdict": "CRITICAL CAPACITY BOTTLENECK" if delay_hours > 36.0 else ("ELEVATED QUEUE RISK" if delay_hours > 12.0 else "CAPACITY STABLE") } # 6. Render Report