Spaces:
Sleeping
Sleeping
File size: 6,119 Bytes
3be03dd | 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 | import { useEffect, useRef, useState } from 'react'
import anime from 'animejs'
import ReactMarkdown from 'react-markdown'
import remarkGfm from 'remark-gfm'
// Colour + label per AutoGen agent name
const AGENT_CFG = {
DataAgent: { color: '#44aaff', icon: 'ποΈ', label: 'Data Agent' },
TechnicalAnalyst: { color: '#00e87a', icon: 'π', label: 'Technical Analyst' },
FundamentalAnalyst: { color: '#ff9944', icon: 'π’', label: 'Fundamental Analyst' },
ReportWriter: { color: '#cc88ff', icon: 'π', label: 'Report Writer' },
PortfolioAgent: { color: '#44ffcc', icon: 'πΌ', label: 'Portfolio Agent' },
RiskAgent: { color: '#ff4466', icon: 'β οΈ', label: 'Risk Agent' },
system: { color: '#666688', icon: 'βοΈ', label: 'System' },
}
// ββ Single message bubble βββββββββββββββββββββββββββββββββββββββββββββββββββββ
function AgentMessage({ message }) {
const ref = useRef(null)
const cfg = AGENT_CFG[message.agent] || AGENT_CFG.system
useEffect(() => {
anime({
targets: ref.current,
translateX: [-18, 0],
opacity: [0, 1],
duration: 380,
easing: 'easeOutExpo',
})
}, [])
// Show up to 350 chars β enough to convey what the agent is doing
const preview = message.content?.slice(0, 350) ?? ''
const truncated = (message.content?.length ?? 0) > 350
return (
<div
ref={ref}
className="agent-message"
style={{ '--agent-color': cfg.color, opacity: 0 }}
>
<div className="agent-msg-header">
<span className="agent-icon">{cfg.icon}</span>
<span className="agent-name" style={{ color: cfg.color }}>
{cfg.label}
</span>
</div>
<p className="agent-msg-content">
{preview}
{truncated && (
<span style={{ color: 'var(--text-faint)' }}> β¦(truncated)</span>
)}
</p>
</div>
)
}
// ββ Agent pipeline progress bar βββββββββββββββββββββββββββββββββββββββββββββββ
const PIPELINE = [
'DataAgent',
'TechnicalAnalyst',
'FundamentalAnalyst',
'ReportWriter',
'RiskAgent',
]
function PipelineBar({ activeAgent, done }) {
return (
<div className="pipeline-bar">
{PIPELINE.map((name, i) => {
const cfg = AGENT_CFG[name]
const isActive = !done && activeAgent === name
const isDone = done || PIPELINE.indexOf(activeAgent) > i
return (
<div
key={name}
className={`pipeline-step ${isActive ? 'active' : ''} ${isDone ? 'done' : ''}`}
style={{ '--step-color': cfg.color }}
>
<span className="pipeline-icon">{cfg.icon}</span>
<span className="pipeline-label">{cfg.label}</span>
</div>
)
})}
</div>
)
}
// ββ Main component ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
export default function AgentStream({ ticker }) {
const [messages, setMessages] = useState([])
const [streaming, setStreaming] = useState(true)
const [report, setReport] = useState(null)
const [activeAgent, setActiveAgent] = useState('DataAgent')
const bottomRef = useRef(null)
useEffect(() => {
const es = new EventSource(`/stream/${ticker}`)
es.onmessage = (e) => {
if (e.data === '[DONE]') {
setStreaming(false)
es.close()
return
}
try {
const event = JSON.parse(e.data)
if (event.agent === '__complete__') {
setReport(event)
setStreaming(false)
} else {
if (event.agent) setActiveAgent(event.agent)
if (event.content?.trim()) {
setMessages((prev) => [...prev, event])
}
}
} catch {
// ignore parse errors
}
}
es.onerror = () => {
setStreaming(false)
es.close()
}
return () => es.close()
}, [ticker])
// Auto-scroll feed to bottom on each new message
useEffect(() => {
bottomRef.current?.scrollIntoView({ behavior: 'smooth' })
}, [messages])
const activeCfg = AGENT_CFG[activeAgent] || AGENT_CFG.system
return (
<div className="agent-stream">
{/* Header */}
<div className="agent-stream-header">
<h3>π€ AI Agents Workingβ¦</h3>
{streaming ? (
<div
className="active-agent-badge"
style={{ '--agent-color': activeCfg.color }}
>
<span className="thinking-dot" />
<span style={{ color: activeCfg.color }}>
{activeCfg.icon} {activeCfg.label}
</span>
<span className="thinking-text">thinkingβ¦</span>
</div>
) : (
<span className="complete-badge">β
Analysis complete</span>
)}
</div>
{/* Pipeline progress */}
<PipelineBar activeAgent={activeAgent} done={!streaming} />
{/* Scrollable message feed */}
<div className="agent-stream-feed">
{messages.length === 0 && streaming && (
<p
style={{
color: 'var(--text-faint)',
fontSize: '0.8rem',
padding: '0.5rem',
}}
>
Waiting for first agent responseβ¦
</p>
)}
{messages.map((msg, i) => (
<AgentMessage key={i} message={msg} />
))}
<div ref={bottomRef} />
</div>
{/* Final report */}
{report && (
<div className="final-report">
<h3>π Full Investment Report</h3>
<div className="report-content">
<ReactMarkdown remarkPlugins={[remarkGfm]}>{report.content?.replace('##ANALYSIS_DONE##', '').trim()}</ReactMarkdown>
</div>
</div>
)}
</div>
)
}
|