Download frontend/src/App.jsx from bhuvi3/CIOAgent: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/frontend/src/App.jsx
- Command line
-
hf download hf://spaces/bhuvi3/CIOAgent/frontend/src/App.jsx
-
curl -L -o App.jsx https://huggingface.co/spaces/bhuvi3/CIOAgent/resolve/main/frontend/src/App.jsx
12.5 kB
| import React, { useState, useCallback, useMemo, memo } from "react"; | |
| import AuthScreen from "./components/AuthScreen"; | |
| import Sidebar from "./components/Sidebar"; | |
| import Dashboard from "./components/Dashboard"; | |
| import Frontier from "./components/Frontier"; | |
| import GoalProjection from "./components/GoalProjection"; | |
| import AgentTabs from "./components/AgentTabs"; | |
| import CIOChat from "./components/CIOChat"; | |
| import UserProfile from "./components/UserProfile"; | |
| import SectorExposure from "./components/SectorExposure"; | |
| import DividendAnalysis from "./components/DividendAnalysis"; | |
| import Rebalancing from "./components/Rebalancing"; | |
| import MacroCorrelations from "./components/MacroCorrelations"; | |
| import TaxHarvest from "./components/TaxHarvest"; | |
| import PortfolioVersions from "./components/PortfolioVersions"; | |
| import AlertsPanel from "./components/AlertsPanel"; | |
| // Memoize heavy components so they skip re-renders when sidebar inputs change | |
| const MemoDashboard = memo(Dashboard); | |
| const MemoFrontier = memo(Frontier); | |
| const MemoGoalProjection = memo(GoalProjection); | |
| const MemoAgentTabs = memo(AgentTabs); | |
| const MemoCIOChat = memo(CIOChat); | |
| const MemoUserProfile = memo(UserProfile); | |
| const MemoSectorExposure = memo(SectorExposure); | |
| const MemoDividendAnalysis = memo(DividendAnalysis); | |
| const MemoRebalancing = memo(Rebalancing); | |
| const MemoMacroCorrelations = memo(MacroCorrelations); | |
| const MemoTaxHarvest = memo(TaxHarvest); | |
| const MemoPortfolioVersions = memo(PortfolioVersions); | |
| const MemoAlertsPanel = memo(AlertsPanel); | |
| import { | |
| setToken, | |
| computeFrontier, | |
| runAgents, | |
| loadUserData, | |
| saveUserData, | |
| fetchSectorExposure, | |
| fetchDividends, | |
| fetchRebalancing, | |
| fetchMacroCorrelations, | |
| fetchTaxHarvest, | |
| generatePDFReport, | |
| savePortfolioSnapshot, | |
| listPortfolioSnapshots, | |
| } from "./api"; | |
| const today = new Date().toISOString().split("T")[0]; | |
| const DEFAULT_PORTFOLIO = { | |
| tickers: ["AAPL", "MSFT", "NVDA", "AMZN"], | |
| benchmarks: ["SPY", "QQQ"], | |
| weights: [0.3, 0.25, 0.3, 0.15], | |
| start_date: "2020-01-01", | |
| rf: 0.04, | |
| sims: 5000, | |
| horizon: 252, | |
| }; | |
| const DEFAULT_PROFILE = { | |
| total_assets: 500000, | |
| total_liabilities: 100000, | |
| risk_tolerance: "Moderate", | |
| }; | |
| const DEFAULT_GOAL = { | |
| goal_type: "Retirement", | |
| target_amount: 1000000, | |
| time_horizon_years: 20, | |
| initial_investment: 50000, | |
| monthly_contribution: 1000, | |
| }; | |
| export default function App() { | |
| // Auth state | |
| const [authed, setAuthed] = useState(false); | |
| const [username, setUsername] = useState(""); | |
| // Core state | |
| const [portfolio, setPortfolio] = useState(DEFAULT_PORTFOLIO); | |
| const [profile, setProfile] = useState(DEFAULT_PROFILE); | |
| const [financialGoal, setFinancialGoal] = useState(DEFAULT_GOAL); | |
| const [analysis, setAnalysis] = useState(null); | |
| const [frontier, setFrontier] = useState(null); | |
| const [agentData, setAgentData] = useState(null); | |
| const [chatHistory, setChatHistory] = useState([]); | |
| const [loading, setLoading] = useState(""); | |
| const [error, setError] = useState(""); | |
| // Insights state | |
| const [sectorData, setSectorData] = useState(null); | |
| const [dividendData, setDividendData] = useState(null); | |
| const [rebalancingData, setRebalancingData] = useState(null); | |
| const [macroData, setMacroData] = useState(null); | |
| const [taxData, setTaxData] = useState(null); | |
| const [snapshots, setSnapshots] = useState([]); | |
| const persist = useCallback( | |
| (overrides = {}) => { | |
| const data = { | |
| portfolio, | |
| profile, | |
| financial_goal: financialGoal, | |
| ...(analysis ? { analysis } : {}), | |
| ...(frontier ? { frontier } : {}), | |
| ...(agentData ? { agent_data: agentData } : {}), | |
| chat_history: chatHistory, | |
| ...overrides, | |
| }; | |
| saveUserData(data).catch(() => { }); | |
| }, | |
| [portfolio, profile, financialGoal, analysis, frontier, agentData, chatHistory], | |
| ); | |
| const handleAuth = async (token, user) => { | |
| setToken(token); | |
| setUsername(user); | |
| try { | |
| const saved = await loadUserData(); | |
| if (saved.portfolio) setPortfolio(saved.portfolio); | |
| if (saved.profile) setProfile(saved.profile); | |
| if (saved.financial_goal) setFinancialGoal(saved.financial_goal); | |
| if (saved.analysis) setAnalysis(saved.analysis); | |
| if (saved.frontier) setFrontier(saved.frontier); | |
| if (saved.agent_data) setAgentData(saved.agent_data); | |
| if (saved.chat_history) setChatHistory(saved.chat_history); | |
| } catch { | |
| // first login | |
| } | |
| // Load snapshots | |
| try { | |
| const snaps = await listPortfolioSnapshots(); | |
| setSnapshots(snaps); | |
| } catch { /* no snapshots yet */ } | |
| setAuthed(true); | |
| }; | |
| const handleLogout = () => { | |
| setToken(null); | |
| setAuthed(false); | |
| setUsername(""); | |
| setPortfolio(DEFAULT_PORTFOLIO); | |
| setProfile(DEFAULT_PROFILE); | |
| setFinancialGoal(DEFAULT_GOAL); | |
| setAnalysis(null); | |
| setFrontier(null); | |
| setAgentData(null); | |
| setChatHistory([]); | |
| setSectorData(null); | |
| setDividendData(null); | |
| setRebalancingData(null); | |
| setMacroData(null); | |
| setTaxData(null); | |
| setSnapshots([]); | |
| }; | |
| const handleRunAnalysis = async () => { | |
| setError(""); | |
| setLoading("Computing metrics & efficient frontier..."); | |
| try { | |
| const data = await computeFrontier(portfolio); | |
| const newAnalysis = data.analysis; | |
| const newFrontier = { | |
| frontier_points: data.frontier_points, | |
| max_sharpe: data.max_sharpe, | |
| min_vol: data.min_vol, | |
| }; | |
| setAnalysis(newAnalysis); | |
| setFrontier(newFrontier); | |
| setAgentData(null); | |
| // Clear insights on new analysis | |
| setSectorData(null); | |
| setDividendData(null); | |
| setRebalancingData(null); | |
| setMacroData(null); | |
| setTaxData(null); | |
| persist({ analysis: newAnalysis, frontier: newFrontier, agent_data: null }); | |
| } catch (e) { | |
| setError(e.message); | |
| } finally { | |
| setLoading(""); | |
| } | |
| }; | |
| const handleRunAgents = async () => { | |
| if (!analysis) { | |
| setError("Run analysis first."); | |
| return; | |
| } | |
| setError(""); | |
| setLoading("Running multi-agent LLM analysis..."); | |
| try { | |
| const data = await runAgents(portfolio, profile, financialGoal); | |
| setAgentData(data); | |
| persist({ agent_data: data }); | |
| } catch (e) { | |
| setError(e.message); | |
| } finally { | |
| setLoading(""); | |
| } | |
| }; | |
| const handleRunInsights = async () => { | |
| setError(""); | |
| setLoading("Loading portfolio insights..."); | |
| try { | |
| const [sector, divs, rebal, macro, tax] = await Promise.allSettled([ | |
| fetchSectorExposure(portfolio.tickers, portfolio.weights), | |
| fetchDividends(portfolio.tickers, portfolio.weights, profile.total_assets), | |
| fetchRebalancing(portfolio.tickers, portfolio.weights, profile.total_assets), | |
| fetchMacroCorrelations(portfolio.tickers, portfolio.weights, portfolio.start_date), | |
| fetchTaxHarvest(portfolio.tickers, portfolio.weights, profile.total_assets, portfolio.start_date), | |
| ]); | |
| if (sector.status === "fulfilled") setSectorData(sector.value); | |
| if (divs.status === "fulfilled") setDividendData(divs.value); | |
| if (rebal.status === "fulfilled") setRebalancingData(rebal.value); | |
| if (macro.status === "fulfilled") setMacroData(macro.value); | |
| if (tax.status === "fulfilled") setTaxData(tax.value); | |
| } catch (e) { | |
| setError(e.message); | |
| } finally { | |
| setLoading(""); | |
| } | |
| }; | |
| const handleSaveSnapshot = async () => { | |
| try { | |
| const snap = await savePortfolioSnapshot( | |
| portfolio.tickers, | |
| portfolio.weights, | |
| "", | |
| analysis?.performance || null, | |
| ); | |
| setSnapshots((prev) => [...prev, snap]); | |
| } catch (e) { | |
| setError(e.message); | |
| } | |
| }; | |
| const handleRestoreSnapshot = useCallback((snap) => { | |
| setPortfolio((p) => ({ | |
| ...p, | |
| tickers: snap.tickers, | |
| weights: snap.weights, | |
| })); | |
| setAnalysis(null); | |
| setFrontier(null); | |
| setAgentData(null); | |
| }, []); | |
| const handleExportPDF = async () => { | |
| if (!analysis) return; | |
| setLoading("Generating PDF report..."); | |
| try { | |
| const blob = await generatePDFReport({ | |
| performance: analysis.performance, | |
| risk: analysis.risk, | |
| benchmark: analysis.benchmark, | |
| stress_test: analysis.stress_test, | |
| frontier_summary: agentData?.frontier_summary || null, | |
| inferences: agentData?.inferences || null, | |
| user_profile: { | |
| ...profile, | |
| net_worth: profile.total_assets - profile.total_liabilities, | |
| }, | |
| }); | |
| const url = URL.createObjectURL(blob); | |
| const a = document.createElement("a"); | |
| a.href = url; | |
| a.download = "portfolio_report.pdf"; | |
| a.click(); | |
| URL.revokeObjectURL(url); | |
| } catch (e) { | |
| setError(e.message); | |
| } finally { | |
| setLoading(""); | |
| } | |
| }; | |
| const handleChatUpdate = useCallback((newHistory) => { | |
| setChatHistory(newHistory); | |
| persist({ chat_history: newHistory }); | |
| }, [persist]); | |
| const netWorth = useMemo(() => profile.total_assets - profile.total_liabilities, [profile.total_assets, profile.total_liabilities]); | |
| const debtRatio = useMemo(() => | |
| profile.total_assets > 0 ? profile.total_liabilities / profile.total_assets : 0, | |
| [profile.total_assets, profile.total_liabilities], | |
| ); | |
| const cioMetrics = useMemo(() => analysis ? { | |
| performance: analysis.performance, | |
| risk: analysis.risk, | |
| benchmark: analysis.benchmark, | |
| stress_test: analysis.stress_test, | |
| } : null, [analysis]); | |
| if (!authed) { | |
| return <AuthScreen onAuth={handleAuth} />; | |
| } | |
| return ( | |
| <div className="app"> | |
| <Sidebar | |
| portfolio={portfolio} | |
| setPortfolio={setPortfolio} | |
| profile={profile} | |
| setProfile={setProfile} | |
| financialGoal={financialGoal} | |
| setFinancialGoal={setFinancialGoal} | |
| onRunAnalysis={handleRunAnalysis} | |
| onRunAgents={handleRunAgents} | |
| onRunInsights={handleRunInsights} | |
| onSaveSnapshot={handleSaveSnapshot} | |
| onExportPDF={handleExportPDF} | |
| loading={loading} | |
| hasAnalysis={!!analysis} | |
| /> | |
| <main className="main"> | |
| <div className="main-header"> | |
| <h1>Portfolio CIO</h1> | |
| <div className="user-badge"> | |
| <span>{username}</span> | |
| <button className="btn logout" onClick={handleLogout}> | |
| Logout | |
| </button> | |
| </div> | |
| </div> | |
| {error && <div className="error">{error}</div>} | |
| {loading && <div className="loading">{loading}</div>} | |
| {analysis && ( | |
| <> | |
| <MemoUserProfile profile={profile} netWorth={netWorth} debtRatio={debtRatio} /> | |
| <MemoDashboard | |
| performance={analysis.performance} | |
| risk={analysis.risk} | |
| benchmark={analysis.benchmark} | |
| stressTest={analysis.stress_test} | |
| tickers={portfolio.tickers} | |
| weights={portfolio.weights} | |
| /> | |
| </> | |
| )} | |
| {frontier && analysis && ( | |
| <MemoFrontier | |
| frontier={frontier} | |
| currentPerformance={analysis.performance} | |
| tickers={portfolio.tickers} | |
| weights={portfolio.weights} | |
| /> | |
| )} | |
| {agentData?.goal_projection && ( | |
| <MemoGoalProjection | |
| goalProjection={agentData.goal_projection} | |
| financialGoal={financialGoal} | |
| /> | |
| )} | |
| {agentData && <MemoAgentTabs inferences={agentData.inferences} />} | |
| {agentData && analysis && ( | |
| <MemoCIOChat | |
| metrics={cioMetrics} | |
| inferences={agentData.inferences} | |
| frontierSummary={agentData.frontier_summary} | |
| userProfile={profile} | |
| goalProjection={agentData.goal_projection} | |
| chatHistory={chatHistory} | |
| onChatUpdate={handleChatUpdate} | |
| /> | |
| )} | |
| {/* ── Insights Section ── */} | |
| {sectorData && <MemoSectorExposure data={sectorData} />} | |
| {dividendData && <MemoDividendAnalysis data={dividendData} />} | |
| {macroData && <MemoMacroCorrelations data={macroData} />} | |
| {rebalancingData && <MemoRebalancing data={rebalancingData} />} | |
| {taxData && <MemoTaxHarvest data={taxData} />} | |
| {/* ── Alerts & Versioning ── */} | |
| <MemoAlertsPanel portfolio={portfolio} /> | |
| <MemoPortfolioVersions snapshots={snapshots} onRestore={handleRestoreSnapshot} /> | |
| </main> | |
| </div> | |
| ); | |
| } | |