Add company analysis view with financials, price history, filings, and AI reports

This commit is contained in:
2026-02-27 09:57:44 -05:00
parent e7320f3bdb
commit 7c3836068f
9 changed files with 512 additions and 2 deletions

7
.gitignore vendored
View File

@@ -39,4 +39,11 @@ out/
# Local app runtime state # Local app runtime state
data/*.json data/*.json
data/*.sqlite
data/*.sqlite-shm
data/*.sqlite-wal
.workflow-data/ .workflow-data/
output/
# Local automation/test artifacts
.playwright-cli/

303
app/analysis/page.tsx Normal file
View File

@@ -0,0 +1,303 @@
'use client';
import Link from 'next/link';
import { Suspense, useCallback, useEffect, useMemo, useState } from 'react';
import { format } from 'date-fns';
import {
Bar,
BarChart,
CartesianGrid,
Legend,
Line,
LineChart,
ResponsiveContainer,
Tooltip,
XAxis,
YAxis
} from 'recharts';
import { BrainCircuit, ChartNoAxesCombined, RefreshCcw, Search } from 'lucide-react';
import { useSearchParams } from 'next/navigation';
import { AppShell } from '@/components/shell/app-shell';
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Panel } from '@/components/ui/panel';
import { useAuthGuard } from '@/hooks/use-auth-guard';
import { getCompanyAnalysis } from '@/lib/api';
import { asNumber, formatCompactCurrency, formatCurrency, formatPercent } from '@/lib/format';
import type { CompanyAnalysis } from '@/lib/types';
function formatShortDate(value: string) {
return format(new Date(value), 'MMM yyyy');
}
export default function AnalysisPage() {
return (
<Suspense fallback={<div className="flex min-h-screen items-center justify-center text-sm text-[color:var(--terminal-muted)]">Loading analysis desk...</div>}>
<AnalysisPageContent />
</Suspense>
);
}
function AnalysisPageContent() {
const { isPending, isAuthenticated } = useAuthGuard();
const searchParams = useSearchParams();
const [tickerInput, setTickerInput] = useState('MSFT');
const [ticker, setTicker] = useState('MSFT');
const [analysis, setAnalysis] = useState<CompanyAnalysis | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
useEffect(() => {
const fromQuery = searchParams.get('ticker');
if (!fromQuery) {
return;
}
const normalized = fromQuery.trim().toUpperCase();
if (!normalized) {
return;
}
setTickerInput(normalized);
setTicker(normalized);
}, [searchParams]);
const loadAnalysis = useCallback(async (symbol: string) => {
setLoading(true);
setError(null);
try {
const response = await getCompanyAnalysis(symbol);
setAnalysis(response.analysis);
} catch (err) {
setError(err instanceof Error ? err.message : 'Unable to load company analysis');
setAnalysis(null);
} finally {
setLoading(false);
}
}, []);
useEffect(() => {
if (!isPending && isAuthenticated) {
void loadAnalysis(ticker);
}
}, [isPending, isAuthenticated, ticker, loadAnalysis]);
const priceSeries = useMemo(() => {
return (analysis?.priceHistory ?? []).map((point) => ({
...point,
label: formatShortDate(point.date)
}));
}, [analysis?.priceHistory]);
const financialSeries = useMemo(() => {
return (analysis?.financials ?? [])
.slice()
.reverse()
.map((item) => ({
label: formatShortDate(item.filingDate),
revenue: item.revenue,
netIncome: item.netIncome,
assets: item.totalAssets
}));
}, [analysis?.financials]);
if (isPending || !isAuthenticated) {
return <div className="flex min-h-screen items-center justify-center text-sm text-[color:var(--terminal-muted)]">Loading analysis desk...</div>;
}
return (
<AppShell
title="Company Analysis"
subtitle="Research a single ticker across pricing, reported financials, filings, and generated AI reports."
actions={(
<Button variant="secondary" onClick={() => void loadAnalysis(ticker)}>
<RefreshCcw className="size-4" />
Refresh
</Button>
)}
>
<Panel title="Search Company" subtitle="Enter a ticker symbol to load the latest analysis view.">
<form
className="flex flex-wrap items-center gap-3"
onSubmit={(event) => {
event.preventDefault();
const normalized = tickerInput.trim().toUpperCase();
if (!normalized) {
return;
}
setTicker(normalized);
}}
>
<Input
value={tickerInput}
onChange={(event) => setTickerInput(event.target.value.toUpperCase())}
placeholder="Ticker (AAPL)"
className="max-w-xs"
/>
<Button type="submit">
<Search className="size-4" />
Analyze
</Button>
{analysis ? (
<Link href={`/filings?ticker=${analysis.company.ticker}`} className="text-sm text-[color:var(--accent)] hover:text-[color:var(--accent-strong)]">
Open filing stream
</Link>
) : null}
</form>
</Panel>
{error ? (
<Panel>
<p className="text-sm text-[#ffb5b5]">{error}</p>
</Panel>
) : null}
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 xl:grid-cols-4">
<Panel title="Company">
<p className="text-xl font-semibold text-[color:var(--terminal-bright)]">{analysis?.company.companyName ?? ticker}</p>
<p className="mt-1 text-xs uppercase tracking-[0.16em] text-[color:var(--terminal-muted)]">{analysis?.company.ticker ?? ticker}</p>
<p className="mt-2 text-sm text-[color:var(--terminal-muted)]">{analysis?.company.sector ?? 'Sector unavailable'}</p>
</Panel>
<Panel title="Live Price">
<p className="text-3xl font-semibold text-[color:var(--terminal-bright)]">{formatCurrency(analysis?.quote ?? 0)}</p>
<p className="mt-2 text-sm text-[color:var(--terminal-muted)]">CIK {analysis?.company.cik ?? 'n/a'}</p>
</Panel>
<Panel title="Position Value">
<p className="text-3xl font-semibold text-[color:var(--terminal-bright)]">{formatCurrency(analysis?.position?.market_value)}</p>
<p className="mt-2 text-sm text-[color:var(--terminal-muted)]">{analysis?.position ? `${asNumber(analysis.position.shares).toLocaleString()} shares` : 'Not held in portfolio'}</p>
</Panel>
<Panel title="Position P&L">
<p className={`text-3xl font-semibold ${asNumber(analysis?.position?.gain_loss) >= 0 ? 'text-[#96f5bf]' : 'text-[#ff9f9f]'}`}>
{formatCurrency(analysis?.position?.gain_loss)}
</p>
<p className="mt-2 text-sm text-[color:var(--terminal-muted)]">{formatPercent(analysis?.position?.gain_loss_pct)}</p>
</Panel>
</div>
<div className="grid grid-cols-1 gap-6 xl:grid-cols-2">
<Panel title="Price History" subtitle="Weekly close over the last year.">
{loading ? (
<p className="text-sm text-[color:var(--terminal-muted)]">Loading price history...</p>
) : priceSeries.length === 0 ? (
<p className="text-sm text-[color:var(--terminal-muted)]">No price history available.</p>
) : (
<div className="h-[320px]">
<ResponsiveContainer width="100%" height="100%">
<LineChart data={priceSeries}>
<CartesianGrid strokeDasharray="2 2" stroke="rgba(126, 217, 255, 0.2)" />
<XAxis dataKey="label" minTickGap={32} stroke="#8cb6c5" fontSize={12} />
<YAxis stroke="#8cb6c5" fontSize={12} tickFormatter={(value: number) => `$${value.toFixed(0)}`} />
<Tooltip formatter={(value: number | string | undefined) => formatCurrency(value)} />
<Line type="monotone" dataKey="close" stroke="#68ffd5" strokeWidth={2} dot={false} />
</LineChart>
</ResponsiveContainer>
</div>
)}
</Panel>
<Panel title="Financial Trend" subtitle="Filing snapshots for revenue, net income, and assets.">
{loading ? (
<p className="text-sm text-[color:var(--terminal-muted)]">Loading financials...</p>
) : financialSeries.length === 0 ? (
<p className="text-sm text-[color:var(--terminal-muted)]">No parsed filing metrics yet.</p>
) : (
<div className="h-[320px]">
<ResponsiveContainer width="100%" height="100%">
<BarChart data={financialSeries}>
<CartesianGrid strokeDasharray="2 2" stroke="rgba(126, 217, 255, 0.2)" />
<XAxis dataKey="label" minTickGap={20} stroke="#8cb6c5" fontSize={12} />
<YAxis stroke="#8cb6c5" fontSize={12} tickFormatter={(value: number) => `$${Math.round(value / 1_000_000_000)}B`} />
<Tooltip formatter={(value: number | string | undefined) => formatCompactCurrency(value)} />
<Legend />
<Bar dataKey="revenue" name="Revenue" fill="#68ffd5" radius={[4, 4, 0, 0]} />
<Bar dataKey="netIncome" name="Net Income" fill="#5fd3ff" radius={[4, 4, 0, 0]} />
<Bar dataKey="assets" name="Total Assets" fill="#9ac7ff" radius={[4, 4, 0, 0]} />
</BarChart>
</ResponsiveContainer>
</div>
)}
</Panel>
</div>
<Panel title="Filings" subtitle={`${analysis?.filings.length ?? 0} recent SEC records loaded.`}>
{loading ? (
<p className="text-sm text-[color:var(--terminal-muted)]">Loading filings...</p>
) : !analysis || analysis.filings.length === 0 ? (
<p className="text-sm text-[color:var(--terminal-muted)]">No filings available for this ticker.</p>
) : (
<div className="overflow-x-auto">
<table className="data-table min-w-[860px]">
<thead>
<tr>
<th>Filed</th>
<th>Type</th>
<th>Revenue</th>
<th>Net Income</th>
<th>Assets</th>
<th>Document</th>
</tr>
</thead>
<tbody>
{analysis.filings.map((filing) => (
<tr key={filing.accession_number}>
<td>{format(new Date(filing.filing_date), 'MMM dd, yyyy')}</td>
<td>{filing.filing_type}</td>
<td>{filing.metrics?.revenue ? formatCompactCurrency(filing.metrics.revenue) : 'n/a'}</td>
<td>{filing.metrics?.netIncome ? formatCompactCurrency(filing.metrics.netIncome) : 'n/a'}</td>
<td>{filing.metrics?.totalAssets ? formatCompactCurrency(filing.metrics.totalAssets) : 'n/a'}</td>
<td>
{filing.filing_url ? (
<a href={filing.filing_url} target="_blank" rel="noreferrer" className="text-xs text-[color:var(--accent)] hover:text-[color:var(--accent-strong)]">
SEC filing
</a>
) : (
'n/a'
)}
</td>
</tr>
))}
</tbody>
</table>
</div>
)}
</Panel>
<Panel title="AI Reports" subtitle="Generated filing analyses for this company.">
{loading ? (
<p className="text-sm text-[color:var(--terminal-muted)]">Loading AI reports...</p>
) : !analysis || analysis.aiReports.length === 0 ? (
<p className="text-sm text-[color:var(--terminal-muted)]">No AI reports generated yet. Run filing analysis from the filings stream.</p>
) : (
<div className="grid grid-cols-1 gap-3 lg:grid-cols-2">
{analysis.aiReports.map((report) => (
<article key={report.accessionNumber} className="rounded-xl border border-[color:var(--line-weak)] bg-[color:var(--panel-soft)] p-4">
<div className="flex items-start justify-between gap-3">
<div>
<p className="text-xs uppercase tracking-[0.16em] text-[color:var(--terminal-muted)]">
{report.filingType} · {format(new Date(report.filingDate), 'MMM dd, yyyy')}
</p>
<h4 className="mt-1 text-sm font-semibold text-[color:var(--terminal-bright)]">{report.provider} / {report.model}</h4>
</div>
<BrainCircuit className="size-4 text-[color:var(--accent)]" />
</div>
<p className="mt-3 line-clamp-6 whitespace-pre-wrap text-sm leading-6 text-[color:var(--terminal-bright)]">{report.summary}</p>
</article>
))}
</div>
)}
</Panel>
<Panel>
<div className="flex items-center gap-2 text-xs uppercase tracking-[0.24em] text-[color:var(--terminal-muted)]">
<ChartNoAxesCombined className="size-4" />
Analysis scope: price + filings + ai synthesis
</div>
</Panel>
</AppShell>
);
}

View File

@@ -202,7 +202,11 @@ export default function CommandCenterPage() {
</div> </div>
<Panel title="Quick Links" subtitle="Feature modules"> <Panel title="Quick Links" subtitle="Feature modules">
<div className="grid grid-cols-1 gap-3 md:grid-cols-3"> <div className="grid grid-cols-1 gap-3 md:grid-cols-4">
<Link className="rounded-xl border border-[color:var(--line-weak)] bg-[color:var(--panel-soft)] p-4 transition hover:border-[color:var(--line-strong)] hover:bg-[color:var(--panel-bright)]" href="/analysis">
<p className="panel-heading text-xs uppercase text-[color:var(--terminal-muted)]">Analysis</p>
<p className="mt-2 text-sm text-[color:var(--terminal-bright)]">Inspect one company across prices, filings, financials, and AI reports.</p>
</Link>
<Link className="rounded-xl border border-[color:var(--line-weak)] bg-[color:var(--panel-soft)] p-4 transition hover:border-[color:var(--line-strong)] hover:bg-[color:var(--panel-bright)]" href="/filings"> <Link className="rounded-xl border border-[color:var(--line-weak)] bg-[color:var(--panel-soft)] p-4 transition hover:border-[color:var(--line-strong)] hover:bg-[color:var(--panel-bright)]" href="/filings">
<p className="panel-heading text-xs uppercase text-[color:var(--terminal-muted)]">Filings</p> <p className="panel-heading text-xs uppercase text-[color:var(--terminal-muted)]">Filings</p>
<p className="mt-2 text-sm text-[color:var(--terminal-bright)]">Sync SEC filings and trigger AI memo analysis.</p> <p className="mt-2 text-sm text-[color:var(--terminal-bright)]">Sync SEC filings and trigger AI memo analysis.</p>

View File

@@ -133,6 +133,13 @@ export default function WatchlistPage() {
Open stream Open stream
<ArrowRight className="size-3" /> <ArrowRight className="size-3" />
</Link> </Link>
<Link
href={`/analysis?ticker=${item.ticker}`}
className="inline-flex items-center gap-1 text-xs text-[color:var(--accent)] hover:text-[color:var(--accent-strong)]"
>
Analyze
<ArrowRight className="size-3" />
</Link>
<Button <Button
variant="danger" variant="danger"
className="ml-auto px-2 py-1 text-xs" className="ml-auto px-2 py-1 text-xs"

View File

@@ -3,7 +3,7 @@
import Link from 'next/link'; import Link from 'next/link';
import { usePathname, useRouter } from 'next/navigation'; import { usePathname, useRouter } from 'next/navigation';
import { useState } from 'react'; import { useState } from 'react';
import { Activity, BookOpenText, ChartCandlestick, Eye, LogOut } from 'lucide-react'; import { Activity, BookOpenText, ChartCandlestick, Eye, LineChart, LogOut } from 'lucide-react';
import { authClient } from '@/lib/auth-client'; import { authClient } from '@/lib/auth-client';
import { Button } from '@/components/ui/button'; import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils'; import { cn } from '@/lib/utils';
@@ -17,6 +17,7 @@ type AppShellProps = {
const NAV_ITEMS = [ const NAV_ITEMS = [
{ href: '/', label: 'Command Center', icon: Activity }, { href: '/', label: 'Command Center', icon: Activity },
{ href: '/analysis', label: 'Company Analysis', icon: LineChart },
{ href: '/filings', label: 'Filings Stream', icon: BookOpenText }, { href: '/filings', label: 'Filings Stream', icon: BookOpenText },
{ href: '/portfolio', label: 'Portfolio Matrix', icon: ChartCandlestick }, { href: '/portfolio', label: 'Portfolio Matrix', icon: ChartCandlestick },
{ href: '/watchlist', label: 'Watchlist', icon: Eye } { href: '/watchlist', label: 'Watchlist', icon: Eye }

View File

@@ -1,6 +1,7 @@
import { edenTreaty } from '@elysiajs/eden'; import { edenTreaty } from '@elysiajs/eden';
import type { App } from '@/lib/server/api/app'; import type { App } from '@/lib/server/api/app';
import type { import type {
CompanyAnalysis,
Filing, Filing,
Holding, Holding,
PortfolioInsight, PortfolioInsight,
@@ -173,6 +174,16 @@ export async function listFilings(query?: { ticker?: string; limit?: number }) {
return await unwrapData<{ filings: Filing[] }>(result, 'Unable to fetch filings'); return await unwrapData<{ filings: Filing[] }>(result, 'Unable to fetch filings');
} }
export async function getCompanyAnalysis(ticker: string) {
const result = await client.api.analysis.company.get({
$query: {
ticker: ticker.trim().toUpperCase()
}
});
return await unwrapData<{ analysis: CompanyAnalysis }>(result, 'Unable to fetch company analysis');
}
export async function queueFilingSync(input: { ticker: string; limit?: number }) { export async function queueFilingSync(input: { ticker: string; limit?: number }) {
const result = await client.api.filings.sync.post(input); const result = await client.api.filings.sync.post(input);
return await unwrapData<{ task: Task }>(result, 'Unable to queue filing sync'); return await unwrapData<{ task: Task }>(result, 'Unable to queue filing sync');

View File

@@ -17,6 +17,7 @@ import {
listWatchlistItems, listWatchlistItems,
upsertWatchlistItemRecord upsertWatchlistItemRecord
} from '@/lib/server/repos/watchlist'; } from '@/lib/server/repos/watchlist';
import { getPriceHistory, getQuote } from '@/lib/server/prices';
import { import {
enqueueTask, enqueueTask,
getTaskById, getTaskById,
@@ -313,6 +314,78 @@ export const app = new Elysia({ prefix: '/api' })
return Response.json({ insight }); return Response.json({ insight });
}) })
.get('/analysis/company', async ({ query }) => {
const { session, response } = await requireAuthenticatedSession();
if (response) {
return response;
}
const ticker = typeof query.ticker === 'string' ? query.ticker.trim().toUpperCase() : '';
if (!ticker) {
return jsonError('ticker is required');
}
const [filings, holdings, watchlist, liveQuote, priceHistory] = await Promise.all([
listFilingsRecords({ ticker, limit: 40 }),
listUserHoldings(session.user.id),
listWatchlistItems(session.user.id),
getQuote(ticker),
getPriceHistory(ticker)
]);
const latestFiling = filings[0] ?? null;
const holding = holdings.find((entry) => entry.ticker === ticker) ?? null;
const watchlistItem = watchlist.find((entry) => entry.ticker === ticker) ?? null;
const companyName = latestFiling?.company_name
?? watchlistItem?.company_name
?? ticker;
const financials = filings
.filter((entry) => entry.metrics)
.map((entry) => ({
filingDate: entry.filing_date,
filingType: entry.filing_type,
revenue: entry.metrics?.revenue ?? null,
netIncome: entry.metrics?.netIncome ?? null,
totalAssets: entry.metrics?.totalAssets ?? null,
cash: entry.metrics?.cash ?? null,
debt: entry.metrics?.debt ?? null
}));
const aiReports = filings
.filter((entry) => entry.analysis?.text || entry.analysis?.legacyInsights)
.slice(0, 8)
.map((entry) => ({
accessionNumber: entry.accession_number,
filingDate: entry.filing_date,
filingType: entry.filing_type,
provider: entry.analysis?.provider ?? 'unknown',
model: entry.analysis?.model ?? 'unknown',
summary: entry.analysis?.text ?? entry.analysis?.legacyInsights ?? ''
}));
return Response.json({
analysis: {
company: {
ticker,
companyName,
sector: watchlistItem?.sector ?? null,
cik: latestFiling?.cik ?? null
},
quote: liveQuote,
position: holding,
priceHistory,
financials,
filings: filings.slice(0, 20),
aiReports
}
});
}, {
query: t.Object({
ticker: t.String({ minLength: 1 })
})
})
.get('/filings', async ({ query }) => { .get('/filings', async ({ query }) => {
const { response } = await requireAuthenticatedSession(); const { response } = await requireAuthenticatedSession();
if (response) { if (response) {

View File

@@ -42,3 +42,73 @@ export async function getQuote(ticker: string): Promise<number> {
return fallbackQuote(normalizedTicker); return fallbackQuote(normalizedTicker);
} }
} }
export async function getPriceHistory(ticker: string): Promise<Array<{ date: string; close: number }>> {
const normalizedTicker = ticker.trim().toUpperCase();
try {
const response = await fetch(`${YAHOO_BASE}/${normalizedTicker}?interval=1wk&range=1y`, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; FiscalClone/3.0)'
},
cache: 'no-store'
});
if (!response.ok) {
throw new Error('Quote history unavailable');
}
const payload = await response.json() as {
chart?: {
result?: Array<{
timestamp?: number[];
indicators?: {
quote?: Array<{
close?: Array<number | null>;
}>;
};
}>;
};
};
const result = payload.chart?.result?.[0];
const timestamps = result?.timestamp ?? [];
const closes = result?.indicators?.quote?.[0]?.close ?? [];
const points = timestamps
.map((timestamp, index) => {
const close = closes[index];
if (typeof close !== 'number' || !Number.isFinite(close)) {
return null;
}
return {
date: new Date(timestamp * 1000).toISOString(),
close
};
})
.filter((entry): entry is { date: string; close: number } => entry !== null);
if (points.length > 0) {
return points;
}
} catch {
// fall through to deterministic synthetic history
}
const now = Date.now();
const base = fallbackQuote(normalizedTicker);
return Array.from({ length: 26 }, (_, index) => {
const step = 25 - index;
const date = new Date(now - step * 14 * 24 * 60 * 60 * 1000).toISOString();
const wave = Math.sin(index / 3.5) * 0.05;
const trend = (index - 13) * 0.006;
const close = Math.max(base * (1 + wave + trend), 1);
return {
date,
close: Number(close.toFixed(2))
};
});
}

View File

@@ -93,3 +93,37 @@ export type PortfolioInsight = {
content: string; content: string;
created_at: string; created_at: string;
}; };
export type CompanyFinancialPoint = {
filingDate: string;
filingType: Filing['filing_type'];
revenue: number | null;
netIncome: number | null;
totalAssets: number | null;
cash: number | null;
debt: number | null;
};
export type CompanyAiReport = {
accessionNumber: string;
filingDate: string;
filingType: Filing['filing_type'];
provider: string;
model: string;
summary: string;
};
export type CompanyAnalysis = {
company: {
ticker: string;
companyName: string;
sector: string | null;
cik: string | null;
};
quote: number;
position: Holding | null;
priceHistory: Array<{ date: string; close: number }>;
financials: CompanyFinancialPoint[];
filings: Filing[];
aiReports: CompanyAiReport[];
};