File size: 10,691 Bytes
4c34a21 bb65f54 4c34a21 0ef2671 4c34a21 bb65f54 4c34a21 bb65f54 4c34a21 bb65f54 4c34a21 | 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 | // frontend/src/components/ChatPanel.tsx
import React, { useState, useRef, useEffect } from 'react';
import { API_BASE } from '../config';
interface ChatMessage {
id: string;
role: 'user' | 'assistant' | 'system';
content: string;
timestamp: Date;
strategyConfig?: Record<string, unknown>;
executionPlan?: Array<{ step: number; tool: string; desc: string }>;
riskReport?: RiskReport | null;
}
interface RiskReport {
ticker: string;
strategy: string;
interval: string;
risk_score: number;
sections: Array<{
title: string;
icon: string;
insights: string[];
severity: string;
}>;
}
interface ChatPanelProps {
onRunBacktest: (config: Record<string, unknown>) => void;
isLoading: boolean;
activeTicker: string;
}
const EXAMPLE_PROMPTS = [
"Backtest TSLA with dynamic routing strategy on daily bars",
"Test NVDA with EMA crossover on 5-minute bars, ATR stop 1.5x",
"Run mean reversion on SPY with RSI oversold at 10",
"η¨ζ₯ηΊΏεζ΅ AAPL ηηͺη ΄ηη₯οΌATR ζ’ζ 2.5 ε",
"Compare Donchian breakout on AMD with 2x ATR trailing stop",
];
export const ChatPanel: React.FC<ChatPanelProps> = ({ onRunBacktest, isLoading, activeTicker }) => {
const [messages, setMessages] = useState<ChatMessage[]>([
{
id: 'welcome',
role: 'system',
content: `Welcome to Quant.ai Research Agent. Describe your trading research in natural language β I'll parse it into a strategy config, run the backtest, and generate a risk analysis report.\n\nTry: "Backtest TSLA with dynamic routing strategy on daily bars"`,
timestamp: new Date()
}
]);
const [input, setInput] = useState('');
const [reportLoading, setReportLoading] = useState(false);
const messagesEndRef = useRef<HTMLDivElement>(null);
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
const handleSend = async () => {
const trimmed = input.trim();
if (!trimmed || isLoading) return;
const userMsg: ChatMessage = {
id: Date.now().toString(),
role: 'user',
content: trimmed,
timestamp: new Date()
};
setMessages(prev => [...prev, userMsg]);
setInput('');
try {
// Step 1: Parse prompt β strategy config
const parseRes = await fetch(`${API_BASE}/api/agent/research`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ prompt: trimmed })
});
const parseData = await parseRes.json();
if (!parseData.success) {
setMessages(prev => [...prev, {
id: Date.now().toString(),
role: 'assistant',
content: `β Failed to parse: ${parseData.error}`,
timestamp: new Date()
}]);
return;
}
const config = parseData.strategy_config;
const plan = parseData.execution_plan;
// Step 2: Show parsed config and plan
const configMsg: ChatMessage = {
id: (Date.now() + 1).toString(),
role: 'assistant',
content: `**${parseData.parsed_intent}**\n\nI've parsed your request into the following configuration. Click "Run Backtest" to execute, or modify the parameters in the settings panel.`,
timestamp: new Date(),
strategyConfig: config,
executionPlan: plan
};
setMessages(prev => [...prev, configMsg]);
// Step 3: Auto-run backtest
onRunBacktest(config);
// Step 4: Generate risk report
setReportLoading(true);
try {
const reportRes = await fetch(`${API_BASE}/api/report/generate`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(config)
});
const reportData = await reportRes.json();
if (reportData.success && reportData.report) {
const reportMsg: ChatMessage = {
id: (Date.now() + 2).toString(),
role: 'assistant',
content: '',
timestamp: new Date(),
riskReport: reportData.report
};
setMessages(prev => [...prev, reportMsg]);
}
} catch {
// Risk report is optional β backtest still succeeded
} finally {
setReportLoading(false);
}
} catch (e) {
setMessages(prev => [...prev, {
id: Date.now().toString(),
role: 'assistant',
content: `β Connection failed: ${e}. Make sure the backend is running on ${API_BASE}`,
timestamp: new Date()
}]);
}
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
handleSend();
}
};
const renderRiskScore = (score: number) => {
const color = score >= 70 ? 'var(--color-green)' : score >= 40 ? '#f5a623' : 'var(--color-red)';
const label = score >= 70 ? 'Low Risk' : score >= 40 ? 'Moderate Risk' : 'High Risk';
return (
<div className="risk-score-badge" style={{ borderColor: color }}>
<div className="risk-score-value" style={{ color }}>{score}</div>
<div className="risk-score-label" style={{ color }}>{label}</div>
</div>
);
};
return (
<div className="chat-panel">
<div className="chat-header">
<div className="chat-header-title">
<span className="chat-icon">π€</span>
<span>AI Research Agent</span>
</div>
<span className="chat-status">{isLoading || reportLoading ? 'β Analyzing...' : 'β Ready'}</span>
</div>
<div className="chat-messages">
{messages.map(msg => (
<div key={msg.id} className={`chat-msg chat-msg-${msg.role}`}>
{msg.role === 'user' && (
<div className="chat-msg-bubble chat-msg-user-bubble">
{msg.content}
</div>
)}
{msg.role === 'system' && (
<div className="chat-msg-bubble chat-msg-system-bubble">
{msg.content.split('\n').map((line, i) => (
<span key={i}>{line}<br /></span>
))}
</div>
)}
{msg.role === 'assistant' && (
<div className="chat-msg-bubble chat-msg-assistant-bubble">
{msg.content && msg.content.split('\n').map((line, i) => (
<span key={i}>
{line.startsWith('**') && line.endsWith('**')
? <strong>{line.replace(/\*\*/g, '')}</strong>
: line}
<br />
</span>
))}
{msg.strategyConfig && (
<div className="chat-config-preview">
<div className="chat-config-title">π Strategy Configuration</div>
<div className="chat-config-grid">
{Object.entries(msg.strategyConfig).map(([key, val]) => (
<div key={key} className="chat-config-item">
<span className="chat-config-key">{key}</span>
<span className="chat-config-val">{String(val)}</span>
</div>
))}
</div>
</div>
)}
{msg.executionPlan && (
<div className="chat-plan">
<div className="chat-config-title">π§ Execution Plan</div>
{msg.executionPlan.map(step => (
<div key={step.step} className="chat-plan-step">
<span className="chat-plan-step-num">{step.step}</span>
<div>
<span className="chat-plan-tool">{step.tool}</span>
<span className="chat-plan-desc">{step.desc}</span>
</div>
</div>
))}
</div>
)}
{msg.riskReport && (
<div className="chat-risk-report">
<div className="chat-risk-header">
<div className="chat-config-title">π AI Risk Analysis Report</div>
{renderRiskScore(msg.riskReport.risk_score)}
</div>
{msg.riskReport.sections.map((section, i) => (
<div key={i} className={`chat-risk-section chat-risk-${section.severity}`}>
<div className="chat-risk-section-title">
{section.icon} {section.title}
</div>
<ul className="chat-risk-insights">
{section.insights.map((insight, j) => (
<li key={j} dangerouslySetInnerHTML={{
__html: insight
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/`(.*?)`/g, '<code>$1</code>')
}} />
))}
</ul>
</div>
))}
</div>
)}
</div>
)}
</div>
))}
{(isLoading || reportLoading) && (
<div className="chat-msg chat-msg-assistant">
<div className="chat-msg-bubble chat-msg-assistant-bubble chat-typing">
<span className="dot"></span>
<span className="dot"></span>
<span className="dot"></span>
</div>
</div>
)}
<div ref={messagesEndRef} />
</div>
{/* Example prompts */}
{messages.length <= 1 && (
<div className="chat-examples">
{EXAMPLE_PROMPTS.map((prompt, i) => (
<button
key={i}
className="chat-example-btn"
onClick={() => setInput(prompt)}
>
{prompt}
</button>
))}
</div>
)}
<div className="chat-input-area">
<textarea
className="chat-input"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder={`Describe your research goal... e.g. "Backtest ${activeTicker} with dynamic strategy on daily bars"`}
rows={1}
disabled={isLoading}
/>
<button
className="chat-send-btn"
onClick={handleSend}
disabled={!input.trim() || isLoading}
>
{isLoading ? 'β³' : 'βΆ'}
</button>
</div>
</div>
);
};
|