from enum import Enum from pydantic import BaseModel, field_validator, model_validator class GoalType(str, Enum): RETIREMENT = "Retirement" HOUSE_DOWN_PAYMENT = "House Down Payment" EDUCATION_FUND = "Education Fund" EMERGENCY_FUND = "Emergency Fund" WEALTH_BUILDING = "Wealth Building" DEBT_PAYOFF = "Debt Payoff" CUSTOM = "Custom" class FinancialGoal(BaseModel): goal_type: GoalType = GoalType.RETIREMENT target_amount: float = 1_000_000 time_horizon_years: int = 20 initial_investment: float = 50_000 monthly_contribution: float = 1_000 class GoalProjectionResult(BaseModel): probability_of_success: float median_outcome: float surplus_or_shortfall: float required_cagr: float | None = None method: str yearly_projections: list[dict] class UserProfile(BaseModel): total_assets: float = 500_000 total_liabilities: float = 100_000 risk_tolerance: str = "Moderate" @property def net_worth(self) -> float: return self.total_assets - self.total_liabilities @property def debt_to_asset_ratio(self) -> float: return ( self.total_liabilities / self.total_assets if self.total_assets > 0 else 0.0 ) class PortfolioRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] benchmarks: list[str] = ["SPY", "QQQ"] weights: list[float] = [0.30, 0.25, 0.30, 0.15] start_date: str = "2020-01-01" rf: float = 0.04 sims: int = 5000 horizon: int = 252 @field_validator("weights") @classmethod def weights_sum_to_one(cls, v): if abs(sum(v) - 1.0) > 1e-4: raise ValueError(f"Weights must sum to 1. Current sum = {sum(v):.4f}") return v @field_validator("tickers") @classmethod def tickers_not_empty(cls, v): if len(v) == 0: raise ValueError("At least one ticker required.") return [t.strip().upper() for t in v] @field_validator("benchmarks") @classmethod def benchmarks_not_empty(cls, v): if len(v) == 0: raise ValueError("At least one benchmark required.") return [b.strip().upper() for b in v] @model_validator(mode="after") def tickers_match_weights(self): if len(self.tickers) != len(self.weights): raise ValueError( f"Number of tickers ({len(self.tickers)}) must match " f"number of weights ({len(self.weights)})." ) return self class AnalysisResponse(BaseModel): performance: dict risk: dict benchmark: dict stress_test: dict class FrontierPoint(BaseModel): weights: dict[str, float] annual_return: float annual_volatility: float sharpe_ratio: float class FrontierResponse(BaseModel): frontier_points: list[dict] max_sharpe: FrontierPoint min_vol: FrontierPoint analysis: AnalysisResponse | None = None class AgentRunRequest(BaseModel): portfolio: PortfolioRequest user_profile: UserProfile = UserProfile() financial_goal: FinancialGoal | None = None class AgentInferences(BaseModel): performance: str risk: str benchmark: str stress: str = "" allocation: str = "" goal_planning: str = "" class AgentRunResponse(BaseModel): metrics: AnalysisResponse inferences: AgentInferences frontier_summary: dict | None = None goal_projection: GoalProjectionResult | None = None class CIORequest(BaseModel): question: str metrics: dict inferences: AgentInferences frontier_summary: dict | None = None user_profile: UserProfile = UserProfile() goal_projection: dict | None = None class CIOResponse(BaseModel): answer: str class TTSRequest(BaseModel): text: str voice: str = "Rachel" # ── Insights schemas ────────────────────────────────────────────── class SectorExposureRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] weights: list[float] = [0.30, 0.25, 0.30, 0.15] class DividendRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] weights: list[float] = [0.30, 0.25, 0.30, 0.15] investment_amount: float = 100_000 class RebalancingRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] target_weights: list[float] = [0.30, 0.25, 0.30, 0.15] portfolio_value: float = 100_000 drift_threshold: float = 0.05 class MacroCorrelationRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] weights: list[float] = [0.30, 0.25, 0.30, 0.15] start_date: str = "2020-01-01" class TaxHarvestRequest(BaseModel): tickers: list[str] = ["AAPL", "MSFT", "NVDA", "AMZN"] weights: list[float] = [0.30, 0.25, 0.30, 0.15] portfolio_value: float = 100_000 cost_basis_date: str = "2020-01-01" tax_rate: float = 0.15 # ── Portfolio versioning ────────────────────────────────────────── class PortfolioSnapshot(BaseModel): timestamp: float label: str = "" tickers: list[str] weights: list[float] performance: dict | None = None # ── Alerts ──────────────────────────────────────────────────────── class AlertRule(BaseModel): id: str = "" metric: str # e.g. "sharpe_ratio", "max_drawdown", "annual_volatility" operator: str # "lt", "gt", "lte", "gte" threshold: float label: str = "" class AlertCheckRequest(BaseModel): portfolio: PortfolioRequest rules: list[AlertRule]