Spaces:
Sleeping
Sleeping
File size: 37,276 Bytes
0c591a7 3e0da39 0c591a7 3e0da39 0c591a7 8aa87db c1e5f17 0c591a7 5766b78 0c591a7 0fb19bd 6fd7fcd 1f4f8e3 6fd7fcd 1f4f8e3 6fd7fcd 1f4f8e3 6fd7fcd 0c591a7 3e0da39 0c591a7 1ad22ef 0c591a7 5766b78 0c591a7 de4ad41 0c591a7 3e0da39 0c591a7 3e0da39 0c591a7 de4ad41 0c591a7 faf1006 1ad22ef 0c591a7 faf1006 1ad22ef 0c591a7 faf1006 1ad22ef 0c591a7 3e0da39 b717808 3e0da39 b717808 3e0da39 0c591a7 0fb19bd 0c591a7 0fb19bd 0c591a7 0fb19bd 0c591a7 0fb19bd 0c591a7 3e0da39 0c591a7 86951ae 0c591a7 1ad22ef 0c591a7 3e0da39 0c591a7 8aa87db 0c591a7 faf1006 0c591a7 e7ed678 0c591a7 e7ed678 0c591a7 c1e5f17 2cf130c e7ed678 8aa87db e7ed678 c1e5f17 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 6fd7fcd e7ed678 0c591a7 6fd7fcd 0c591a7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 | import { useState, useEffect, useRef, useMemo } from "react"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"
import { Badge } from "@/components/ui/badge"
import { Toaster } from "@/components/ui/toaster"
import { TooltipProvider } from "@/components/ui/tooltip"
import { QueryClient, QueryClientProvider } from "@tanstack/react-query"
import { BrowserRouter, Routes, Route } from "react-router-dom"
import {
startAnalysis,
getWorkflowStatus,
getWorkflowResult,
StockResult,
WorkflowStatus,
ActivityLogEntry,
MCPStatus,
LLMStatus,
MetricEntry,
UserApiKeys,
} from "@/lib/api"
import { AnalysisResponse } from "@/lib/types"
import {
TrendingUp,
TrendingDown,
Target,
AlertTriangle,
CheckCircle,
XCircle,
BarChart3,
RefreshCw,
Play,
Copy,
Download,
Printer,
Check,
Pause,
X,
Loader2,
Settings,
Key,
ChevronDown,
ChevronUp,
Database,
} from "lucide-react"
// Import new components
import { ProcessFlow } from "@/components/ProcessFlow"
import { StockSearch } from "@/components/StockSearch"
import { ActivityLog } from "@/components/ActivityLog"
import { MCPDataPanel } from "@/components/MCPDataPanel"
const queryClient = new QueryClient()
const App = () => (
<QueryClientProvider client={queryClient}>
<TooltipProvider>
<Toaster />
<BrowserRouter>
<Routes>
<Route path="/" element={<Index />} />
<Route path="*" element={<NotFound />} />
</Routes>
</BrowserRouter>
</TooltipProvider>
</QueryClientProvider>
)
export default App
const defaultMCPStatus: MCPStatus = {
fundamentals: 'idle',
valuation: 'idle',
volatility: 'idle',
macro: 'idle',
news: 'idle',
sentiment: 'idle',
}
const defaultLLMStatus: LLMStatus = {
groq: 'idle',
gemini: 'idle',
openrouter: 'idle',
}
// Helper to clean markdown formatting (strip asterisks, bold markers)
const cleanMarkdown = (text: string): string => {
return text
.replace(/\*\*([^*]+)\*\*/g, '$1') // Remove **bold**
.replace(/\*([^*]+)\*/g, '$1') // Remove *italic*
.replace(/^\*\s*/gm, '') // Remove bullet asterisks at line start
.trim()
}
// Parse structured SWOT line: "[M01] Revenue: $394.3B - Strong market position"
// Returns { ref, metric, insight } or null if line doesn't match pattern
interface SwotRow {
ref: string
metric: string
insight: string
}
const parseSwotLine = (line: string): SwotRow | null => {
// Clean markdown first
const cleaned = cleanMarkdown(line)
// Pattern: [M##] Metric: Value - Insight
// Use " - " (space-hyphen-space) to avoid matching hyphens in dates like "2024-12-31"
// Also handle: [M##] Metric: Value | Insight (pipe separator)
const match = cleaned.match(/^\[?(M\d+)\]?\s*(.+?)\s+[-|]\s+(.+)$/i)
if (match) {
return {
ref: match[1],
metric: match[2].trim(),
insight: match[3].trim()
}
}
// Fallback: no ref pattern, just "Metric: Value - Insight"
const fallback = cleaned.match(/^(.+?)\s+[-|]\s+(.+)$/)
if (fallback) {
return {
ref: '',
metric: fallback[1].trim(),
insight: fallback[2].trim()
}
}
// Last resort: just return the cleaned line as insight
if (cleaned.length > 0) {
return {
ref: '',
metric: '',
insight: cleaned
}
}
return null
}
const Index = () => {
const [selectedStock, setSelectedStock] = useState<StockResult | null>(null)
const [isLoading, setIsLoading] = useState(false)
const [showResults, setShowResults] = useState(false)
const [mainTab, setMainTab] = useState<"flow" | "results">("flow")
const [analysisResult, setAnalysisResult] = useState<AnalysisResponse | null>(null)
const [workflowId, setWorkflowId] = useState<string | null>(null)
// Workflow tracking
const [currentStep, setCurrentStep] = useState<string>('idle')
const [completedSteps, setCompletedSteps] = useState<string[]>([])
const [mcpStatus, setMcpStatus] = useState<MCPStatus>(defaultMCPStatus)
const [llmStatus, setLlmStatus] = useState<LLMStatus>(defaultLLMStatus)
const [activityLog, setActivityLog] = useState<ActivityLogEntry[]>([])
const [metrics, setMetrics] = useState<MetricEntry[]>([])
const [revisionCount, setRevisionCount] = useState(0)
const [score, setScore] = useState(0)
const [llmProvider, setLlmProvider] = useState<string>('')
const [cacheHit, setCacheHit] = useState(false)
const [isSearching, setIsSearching] = useState(false)
const [isPaused, setIsPaused] = useState(false)
const [hasError, setHasError] = useState(false)
const [isAborted, setIsAborted] = useState(false)
const [abortReason, setAbortReason] = useState<string>('')
const [userEvents, setUserEvents] = useState<Array<{timestamp: string; message: string}>>([])
// User API keys (optional - for when server keys hit rate limits)
const [userApiKeys, setUserApiKeys] = useState<UserApiKeys>({})
const [showApiKeySettings, setShowApiKeySettings] = useState(false)
// Cache dialog state
const [showCacheDialog, setShowCacheDialog] = useState(false)
const [pendingCacheWorkflowId, setPendingCacheWorkflowId] = useState<string | null>(null)
const [copied, setCopied] = useState(false)
const pollingRef = useRef<ReturnType<typeof setInterval> | null>(null)
// Helper to add user events to log
const addUserEvent = (message: string) => {
setUserEvents(prev => [...prev, { timestamp: new Date().toISOString(), message }])
}
// Browser Notification helper
const notify = (title: string, body: string, type: 'success' | 'error' = 'success') => {
if (Notification.permission === 'granted') {
new Notification(title, {
body,
icon: type === 'success' ? '/favicon.ico' : undefined,
tag: 'swot-notification', // Prevents duplicate notifications
})
}
}
// Request notification permission on first stock selection
const requestNotificationPermission = () => {
if ('Notification' in window && Notification.permission === 'default') {
Notification.requestPermission()
}
}
// Extracted polling logic to avoid duplication
const startPolling = (workflowIdToUse: string) => {
if (pollingRef.current) {
clearInterval(pollingRef.current)
}
pollingRef.current = setInterval(async () => {
try {
const status = await getWorkflowStatus(workflowIdToUse)
setRevisionCount(status.revision_count)
setScore(status.score)
setActivityLog(status.activity_log || [])
setMetrics(status.metrics || [])
// Merge MCP status - preserve failed/partial states (they persist for session)
setMcpStatus(prev => {
const newStatus = status.mcp_status || defaultMCPStatus
return {
fundamentals: prev.fundamentals === 'failed' || prev.fundamentals === 'partial' ? prev.fundamentals : newStatus.fundamentals,
valuation: prev.valuation === 'failed' || prev.valuation === 'partial' ? prev.valuation : newStatus.valuation,
volatility: prev.volatility === 'failed' || prev.volatility === 'partial' ? prev.volatility : newStatus.volatility,
macro: prev.macro === 'failed' || prev.macro === 'partial' ? prev.macro : newStatus.macro,
news: prev.news === 'failed' || prev.news === 'partial' ? prev.news : newStatus.news,
sentiment: prev.sentiment === 'failed' || prev.sentiment === 'partial' ? prev.sentiment : newStatus.sentiment,
}
})
// Merge LLM status - preserve failed states (they persist for session)
setLlmStatus(prev => {
const newStatus = status.llm_status || defaultLLMStatus
return {
groq: prev.groq === 'failed' ? prev.groq : newStatus.groq,
gemini: prev.gemini === 'failed' ? prev.gemini : newStatus.gemini,
openrouter: prev.openrouter === 'failed' ? prev.openrouter : newStatus.openrouter,
}
})
if (status.provider_used) setLlmProvider(status.provider_used)
// Update completed steps - accumulate rather than recalculate to handle loops
const stepOrder = ['input', 'cache', 'researcher', 'analyzer', 'critic', 'output']
setCompletedSteps(prev => {
const newCompleted = new Set(prev)
const currentIdx = stepOrder.indexOf(status.current_step)
// Mark all steps before current as completed
for (let i = 0; i < currentIdx; i++) {
newCompleted.add(stepOrder[i])
}
return Array.from(newCompleted)
})
// Only update currentStep for in-progress workflows to prevent output glow flash
if (status.status !== 'completed') {
setCurrentStep(status.current_step)
}
// Set cacheHit flag for ProcessFlow visualization
if (status.data_source === 'cache') {
setCacheHit(true)
}
if (status.status === "completed") {
clearInterval(pollingRef.current!)
pollingRef.current = null
// Check if this was a cache hit - show dialog to let user choose
if (status.data_source === 'cache') {
setCacheHit(true)
setCurrentStep('cache')
setCompletedSteps(['input', 'cache'])
setPendingCacheWorkflowId(workflowIdToUse)
setShowCacheDialog(true)
// Don't auto-proceed - wait for user choice
return
}
// Normal flow - all steps completed
// Set completed steps BEFORE the async fetch to prevent output from glowing prematurely
setCompletedSteps(stepOrder)
setCurrentStep('completed')
const result = await getWorkflowResult(workflowIdToUse)
setAnalysisResult(result)
setIsLoading(false)
setShowResults(true)
setMainTab("results")
// Notify user that report is ready (works even if tab is in background)
notify("Analysis complete!", `SWOT report for ${selectedStock?.symbol || 'company'} is ready.`, 'success')
} else if (status.status === "aborted") {
clearInterval(pollingRef.current!)
pollingRef.current = null
setIsLoading(false)
setIsAborted(true)
setAbortReason(status.error || 'Critical failure - workflow aborted')
// Notify user of abort (works even if tab is in background)
notify("Analysis aborted", status.error || "Critical failure - workflow aborted", 'error')
} else if (status.status === "error") {
clearInterval(pollingRef.current!)
pollingRef.current = null
setIsLoading(false)
setHasError(true)
// Notify user of error (works even if tab is in background)
notify("Analysis failed", "An error occurred during analysis.", 'error')
}
} catch (error) {
console.error("Polling error:", error)
}
}, 700)
}
// Button state logic
const buttonState = useMemo(() => {
if (isAborted) return 'aborted'
if (hasError) return 'error'
if (analysisResult && !isLoading) return 'complete'
if (isPaused) return 'paused'
if (isLoading) return 'analyzing'
return 'ready'
}, [isAborted, hasError, analysisResult, isLoading, isPaused])
// Pause handler - stop polling
const handlePause = () => {
setIsPaused(true)
if (pollingRef.current) {
clearInterval(pollingRef.current)
pollingRef.current = null
}
}
// Resume handler - restart polling
const handleResume = () => {
if (!workflowId) return
setIsPaused(false)
startPolling(workflowId)
}
// Abort handler - cancel workflow
const handleAbort = () => {
if (pollingRef.current) {
clearInterval(pollingRef.current)
pollingRef.current = null
}
setIsLoading(false)
setIsPaused(false)
setHasError(false)
setIsAborted(false)
setAbortReason('')
setCurrentStep('idle')
setCompletedSteps([])
setAnalysisResult(null)
setShowResults(false)
setMcpStatus(defaultMCPStatus)
setLlmStatus(defaultLLMStatus)
}
// Cache dialog: Use cached data
const handleUseCached = async () => {
if (!pendingCacheWorkflowId) return
setShowCacheDialog(false)
addUserEvent('Using cached analysis')
// Animate cache → output transition
setCurrentStep('output')
setTimeout(async () => {
setCompletedSteps(['input', 'cache', 'output'])
setCurrentStep('completed')
// Fetch both result and status (for metrics)
const [result, status] = await Promise.all([
getWorkflowResult(pendingCacheWorkflowId),
getWorkflowStatus(pendingCacheWorkflowId)
])
setAnalysisResult(result)
setMetrics(status.metrics || [])
setIsLoading(false)
setShowResults(true)
setMainTab("results")
setPendingCacheWorkflowId(null)
}, 800)
}
// Cache dialog: Run fresh analysis
const handleRunFresh = async () => {
if (!selectedStock) return
setShowCacheDialog(false)
setPendingCacheWorkflowId(null)
addUserEvent('Running fresh analysis (cache bypassed)')
// Reset state for fresh run
setCurrentStep('input')
setCompletedSteps([])
setMcpStatus(defaultMCPStatus)
setLlmStatus(defaultLLMStatus)
setActivityLog([])
setMetrics([])
setRevisionCount(0)
setScore(0)
setCacheHit(false)
setAnalysisResult(null)
try {
const { workflow_id } = await startAnalysis(
selectedStock.name,
selectedStock.symbol,
'Competitive Position',
true, // skipCache = true
userApiKeys
)
setWorkflowId(workflow_id)
setCompletedSteps(['input'])
setCurrentStep('cache')
startPolling(workflow_id)
} catch (error) {
console.error("Error starting fresh analysis:", error)
setIsLoading(false)
setHasError(true)
}
}
// Force dark mode
useEffect(() => {
document.documentElement.classList.add("dark")
}, [])
// Export functions
const formatSwotForClipboard = () => {
if (!analysisResult) return ''
return `SWOT Analysis: ${analysisResult.company_name}
Quality Score: ${analysisResult.score}/10
Revisions: ${analysisResult.revision_count}
STRENGTHS:
${analysisResult.swot_data.strengths.map(s => `- ${cleanMarkdown(s)}`).join('\n')}
WEAKNESSES:
${analysisResult.swot_data.weaknesses.map(w => `- ${cleanMarkdown(w)}`).join('\n')}
OPPORTUNITIES:
${analysisResult.swot_data.opportunities.map(o => `- ${cleanMarkdown(o)}`).join('\n')}
THREATS:
${analysisResult.swot_data.threats.map(t => `- ${cleanMarkdown(t)}`).join('\n')}
QUALITY EVALUATION:
${analysisResult.critique}
---
Generated by Instant SWOT Agent`
}
const copyToClipboard = async () => {
try {
await navigator.clipboard.writeText(formatSwotForClipboard())
setCopied(true)
setTimeout(() => setCopied(false), 2000)
} catch (err) {
console.error('Failed to copy:', err)
}
}
const downloadAsJson = () => {
if (!analysisResult) return
const exportData = {
...analysisResult,
exported_at: new Date().toISOString()
}
const blob = new Blob([JSON.stringify(exportData, null, 2)], { type: 'application/json' })
const url = URL.createObjectURL(blob)
const a = document.createElement('a')
a.href = url
a.download = `swot-analysis-${analysisResult.company_name.toLowerCase().replace(/\s+/g, '-')}.json`
document.body.appendChild(a)
a.click()
document.body.removeChild(a)
URL.revokeObjectURL(url)
}
const handleGenerate = async () => {
if (!selectedStock) return
addUserEvent(`Analysis started for ${selectedStock.symbol}`)
setIsLoading(true)
setShowResults(false)
setCurrentStep('input')
setCompletedSteps([])
setMcpStatus(defaultMCPStatus)
setLlmStatus(defaultLLMStatus)
setActivityLog([])
setMetrics([])
setRevisionCount(0)
setScore(0)
setCacheHit(false)
setIsPaused(false)
setHasError(false)
setIsAborted(false)
setAbortReason('')
setAnalysisResult(null)
try {
const { workflow_id } = await startAnalysis(
selectedStock.name,
selectedStock.symbol,
'Competitive Position',
false, // skipCache = false (check cache first)
userApiKeys
)
setWorkflowId(workflow_id)
setCompletedSteps(['input'])
setCurrentStep('cache')
startPolling(workflow_id)
} catch (error) {
console.error("Error starting analysis:", error)
setIsLoading(false)
setHasError(true)
}
}
useEffect(() => {
return () => {
if (pollingRef.current) {
clearInterval(pollingRef.current)
}
}
}, [])
const getScoreColor = (score: number) => {
if (score >= 7) return "text-emerald-400"
if (score >= 5) return "text-yellow-400"
return "text-red-400"
}
const getScoreBadge = (score: number) => {
if (score >= 6)
return { label: "", variant: "default" as const, icon: CheckCircle }
return { label: "", variant: "destructive" as const, icon: XCircle }
}
const handleStockClear = () => {
setSelectedStock(null)
setShowResults(false)
setAnalysisResult(null)
setCurrentStep('idle')
setCompletedSteps([])
setActivityLog([])
setMetrics([])
setUserEvents([])
setHasError(false)
setIsAborted(false)
setAbortReason('')
setMcpStatus(defaultMCPStatus)
setLlmStatus(defaultLLMStatus)
}
return (
<Tabs value={mainTab} onValueChange={(v) => setMainTab(v as "flow" | "results")} className="min-h-screen bg-background">
{/* Header */}
<header className="border-b border-border bg-card sticky top-0 z-40">
<div className="container mx-auto px-4 sm:px-6 py-3">
<div className="flex items-center gap-3">
<div className="shrink-0">
<h1 className="text-lg font-semibold text-foreground">
Instant SWOT Agent
</h1>
<p className="text-xs text-muted-foreground hidden sm:block">
with self-correcting feedback
</p>
</div>
<div className="flex-1 max-w-xl">
<StockSearch
onSelect={(stock) => {
setSelectedStock(stock)
addUserEvent(`Selected: ${stock.name} (${stock.symbol})`)
requestNotificationPermission()
}}
selectedStock={selectedStock}
onClear={handleStockClear}
disabled={isLoading}
onSearchChange={setIsSearching}
/>
</div>
{/* Dynamic Submit/Control Buttons */}
<div className="flex items-center gap-2 shrink-0">
{buttonState === 'ready' && (
<Button
onClick={handleGenerate}
disabled={!selectedStock}
className="gap-2"
>
<Play className="h-4 w-4" />
Submit
</Button>
)}
{buttonState === 'analyzing' && (
<>
<Button onClick={handlePause} className="gap-2 btn-amber btn-amber-pulse">
<Pause className="h-4 w-4" />
Pause
</Button>
<Button variant="destructive" onClick={handleAbort} className="gap-2">
<X className="h-4 w-4" />
Abort
</Button>
</>
)}
{buttonState === 'paused' && (
<>
<Button onClick={handleResume} className="gap-2 btn-amber">
<Play className="h-4 w-4" />
Resume
</Button>
<Button variant="destructive" onClick={handleAbort} className="gap-2">
<X className="h-4 w-4" />
Abort
</Button>
</>
)}
{buttonState === 'complete' && (
<Button className="gap-2 btn-green" disabled>
<Check className="h-4 w-4" />
Complete
</Button>
)}
{buttonState === 'error' && (
<Button variant="destructive" onClick={handleGenerate} className="gap-2">
<X className="h-4 w-4" />
Failed - Retry
</Button>
)}
{buttonState === 'aborted' && (
<Button variant="destructive" onClick={handleStockClear} className="gap-2" title={abortReason}>
<AlertTriangle className="h-4 w-4" />
Aborted
</Button>
)}
</div>
</div>
</div>
</header>
{/* Cache Hit Dialog */}
{showCacheDialog && (
<div className="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
<Card className="w-full max-w-md mx-4">
<CardHeader>
<CardTitle className="flex items-center gap-2">
<Database className="h-5 w-5 text-blue-500" />
Cached Analysis Found
</CardTitle>
</CardHeader>
<CardContent className="space-y-4">
<p className="text-sm text-muted-foreground">
A recent analysis for <span className="font-medium text-foreground">{selectedStock?.symbol}</span> was found in cache.
Would you like to use the cached result or run a fresh analysis?
</p>
<div className="flex gap-3">
<Button onClick={handleUseCached} className="flex-1 gap-2">
<Database className="h-4 w-4" />
Use Cached
</Button>
<Button onClick={handleRunFresh} variant="outline" className="flex-1 gap-2">
<RefreshCw className="h-4 w-4" />
Run Fresh
</Button>
</div>
</CardContent>
</Card>
</div>
)}
{/* API Key Settings (Expandable) */}
<div className="container mx-auto px-4 sm:px-6 pt-2">
<button
onClick={() => setShowApiKeySettings(!showApiKeySettings)}
className="flex items-center gap-2 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
<Key className="h-3 w-3" />
<span>API Keys (Optional)</span>
{showApiKeySettings ? <ChevronUp className="h-3 w-3" /> : <ChevronDown className="h-3 w-3" />}
</button>
{showApiKeySettings && (
<Card className="mt-2 p-3">
<p className="text-xs text-muted-foreground mb-3">
Provide your own API keys if server keys hit rate limits. Keys are not stored.
</p>
<div className="grid grid-cols-1 sm:grid-cols-3 gap-3">
<div>
<label className="text-xs text-muted-foreground block mb-1">Groq</label>
<input
type="password"
placeholder="gsk_..."
value={userApiKeys.groq || ''}
onChange={(e) => setUserApiKeys(prev => ({ ...prev, groq: e.target.value || undefined }))}
className="w-full px-2 py-1 text-xs bg-background border rounded focus:outline-none focus:ring-1 focus:ring-ring"
/>
</div>
<div>
<label className="text-xs text-muted-foreground block mb-1">Gemini</label>
<input
type="password"
placeholder="AI..."
value={userApiKeys.gemini || ''}
onChange={(e) => setUserApiKeys(prev => ({ ...prev, gemini: e.target.value || undefined }))}
className="w-full px-2 py-1 text-xs bg-background border rounded focus:outline-none focus:ring-1 focus:ring-ring"
/>
</div>
<div>
<label className="text-xs text-muted-foreground block mb-1">OpenRouter</label>
<input
type="password"
placeholder="sk-or-..."
value={userApiKeys.openrouter || ''}
onChange={(e) => setUserApiKeys(prev => ({ ...prev, openrouter: e.target.value || undefined }))}
className="w-full px-2 py-1 text-xs bg-background border rounded focus:outline-none focus:ring-1 focus:ring-ring"
/>
</div>
</div>
</Card>
)}
</div>
<main className="container mx-auto px-4 sm:px-6 pt-4 pb-6 space-y-6 overflow-visible">
{/* Process Flow + Metrics Panel */}
<div className="flex gap-4">
<div className="shrink-0">
<ProcessFlow
currentStep={currentStep}
completedSteps={completedSteps}
mcpStatus={mcpStatus}
llmStatus={llmStatus}
llmProvider={llmProvider}
cacheHit={cacheHit}
stockSelected={!!selectedStock}
isSearching={isSearching}
revisionCount={revisionCount}
isAborted={isAborted || hasError}
/>
</div>
<div className="flex-1 min-w-0 h-[260px]">
<ActivityLog
metrics={metrics}
activityLog={activityLog}
currentStep={currentStep}
revisionCount={revisionCount}
score={score}
isTyping={isSearching}
userEvents={userEvents}
/>
</div>
</div>
{/* Results Tab - SWOT cards + metrics */}
{(isLoading || showResults) && (
<TabsContent value="results" className="mt-0">
{analysisResult && (
<div className="space-y-6 animate-slide-up">
{/* Results Header */}
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
<div>
<h2 className="text-2xl font-semibold text-foreground">
{analysisResult.company_name} ({selectedStock?.symbol})
</h2>
<p className="text-sm text-muted-foreground">
{selectedStock?.exchange}
</p>
{analysisResult.business_address && (
<p className="text-sm text-muted-foreground mt-1">
{analysisResult.business_address}
</p>
)}
</div>
<div className="flex flex-wrap items-center gap-4">
{/* Metrics */}
<div className="flex items-center gap-4">
<div className="text-center px-4 py-2 bg-card rounded-lg border">
<p className="text-xs text-muted-foreground">Score</p>
<p className={`text-xl font-bold ${getScoreColor(analysisResult.score)}`}>
{analysisResult.score}/10
</p>
</div>
<div className="text-center px-4 py-2 bg-card rounded-lg border">
<p className="text-xs text-muted-foreground">Revisions</p>
<p className="text-xl font-bold text-foreground">
{analysisResult.revision_count}
</p>
</div>
</div>
<Badge variant={getScoreBadge(analysisResult.score).variant} className="gap-1.5">
{(() => {
const BadgeIcon = getScoreBadge(analysisResult.score).icon
return <BadgeIcon className="h-4 w-4" />
})()}
{getScoreBadge(analysisResult.score).label}
</Badge>
</div>
</div>
{/* Export Buttons */}
<div className="flex flex-wrap gap-2 print:hidden">
<Button variant="outline" size="sm" onClick={() => window.print()} className="gap-1.5">
<Download className="h-4 w-4" />
Download
</Button>
<Button variant="outline" size="sm" onClick={() => window.print()} className="gap-1.5">
<Printer className="h-4 w-4" />
Print
</Button>
</div>
{/* MCP Source Data */}
{metrics.length > 0 && (
<MCPDataPanel
metrics={metrics}
rawData={analysisResult.raw_data}
companyName={analysisResult.company_name}
ticker={selectedStock?.symbol}
exchange={selectedStock?.exchange}
/>
)}
{/* SWOT Analysis - Tables */}
<div className="space-y-6">
{/* Strengths Table */}
<div>
<h3 className="flex items-center gap-2 text-base font-semibold text-emerald-500 mb-3 border-b border-emerald-500/30 pb-2">
<TrendingUp className="h-5 w-5" />
Strengths
</h3>
<table className="w-full text-sm border-collapse">
<thead>
<tr className="border-b border-border">
<th className="text-left py-2 px-2 w-12 text-muted-foreground font-medium">Ref</th>
<th className="text-left py-2 px-2 w-1/3 text-muted-foreground font-medium">Metric</th>
<th className="text-left py-2 px-2 text-muted-foreground font-medium">Insight</th>
</tr>
</thead>
<tbody>
{analysisResult.swot_data.strengths.map((item, i) => {
const parsed = parseSwotLine(item)
if (!parsed) return null
return (
<tr key={i} className="border-b border-border/50 hover:bg-emerald-500/5">
<td className="py-2 px-2 text-emerald-500 font-mono text-xs">{parsed.ref}</td>
<td className="py-2 px-2 text-foreground">{parsed.metric}</td>
<td className="py-2 px-2 text-muted-foreground">{parsed.insight}</td>
</tr>
)
})}
</tbody>
</table>
</div>
{/* Weaknesses Table */}
<div>
<h3 className="flex items-center gap-2 text-base font-semibold text-red-500 mb-3 border-b border-red-500/30 pb-2">
<TrendingDown className="h-5 w-5" />
Weaknesses
</h3>
<table className="w-full text-sm border-collapse">
<thead>
<tr className="border-b border-border">
<th className="text-left py-2 px-2 w-12 text-muted-foreground font-medium">Ref</th>
<th className="text-left py-2 px-2 w-1/3 text-muted-foreground font-medium">Metric</th>
<th className="text-left py-2 px-2 text-muted-foreground font-medium">Insight</th>
</tr>
</thead>
<tbody>
{analysisResult.swot_data.weaknesses.map((item, i) => {
const parsed = parseSwotLine(item)
if (!parsed) return null
return (
<tr key={i} className="border-b border-border/50 hover:bg-red-500/5">
<td className="py-2 px-2 text-red-500 font-mono text-xs">{parsed.ref}</td>
<td className="py-2 px-2 text-foreground">{parsed.metric}</td>
<td className="py-2 px-2 text-muted-foreground">{parsed.insight}</td>
</tr>
)
})}
</tbody>
</table>
</div>
{/* Opportunities Table */}
<div>
<h3 className="flex items-center gap-2 text-base font-semibold text-blue-500 mb-3 border-b border-blue-500/30 pb-2">
<Target className="h-5 w-5" />
Opportunities
</h3>
<table className="w-full text-sm border-collapse">
<thead>
<tr className="border-b border-border">
<th className="text-left py-2 px-2 w-12 text-muted-foreground font-medium">Ref</th>
<th className="text-left py-2 px-2 w-1/3 text-muted-foreground font-medium">Metric</th>
<th className="text-left py-2 px-2 text-muted-foreground font-medium">Insight</th>
</tr>
</thead>
<tbody>
{analysisResult.swot_data.opportunities.map((item, i) => {
const parsed = parseSwotLine(item)
if (!parsed) return null
return (
<tr key={i} className="border-b border-border/50 hover:bg-blue-500/5">
<td className="py-2 px-2 text-blue-500 font-mono text-xs">{parsed.ref}</td>
<td className="py-2 px-2 text-foreground">{parsed.metric}</td>
<td className="py-2 px-2 text-muted-foreground">{parsed.insight}</td>
</tr>
)
})}
</tbody>
</table>
</div>
{/* Threats Table */}
<div>
<h3 className="flex items-center gap-2 text-base font-semibold text-yellow-500 mb-3 border-b border-yellow-500/30 pb-2">
<AlertTriangle className="h-5 w-5" />
Threats
</h3>
<table className="w-full text-sm border-collapse">
<thead>
<tr className="border-b border-border">
<th className="text-left py-2 px-2 w-12 text-muted-foreground font-medium">Ref</th>
<th className="text-left py-2 px-2 w-1/3 text-muted-foreground font-medium">Metric</th>
<th className="text-left py-2 px-2 text-muted-foreground font-medium">Insight</th>
</tr>
</thead>
<tbody>
{analysisResult.swot_data.threats.map((item, i) => {
const parsed = parseSwotLine(item)
if (!parsed) return null
return (
<tr key={i} className="border-b border-border/50 hover:bg-yellow-500/5">
<td className="py-2 px-2 text-yellow-500 font-mono text-xs">{parsed.ref}</td>
<td className="py-2 px-2 text-foreground">{parsed.metric}</td>
<td className="py-2 px-2 text-muted-foreground">{parsed.insight}</td>
</tr>
)
})}
</tbody>
</table>
</div>
</div>
{/* Data Quality Notes - Hidden for now, will revisit later */}
</div>
)}
</TabsContent>
)}
</main>
</Tabs>
)
}
const NotFound = () => (
<div className="min-h-screen bg-background flex flex-col items-center justify-center">
<div className="text-center space-y-4">
<h1 className="text-4xl font-bold text-foreground">404</h1>
<p className="text-xl text-muted-foreground">Page Not Found</p>
<Button onClick={() => window.location.href = '/'}>Go Home</Button>
</div>
</div>
)
|