CIOAgent / frontend /src /components /CIOChat.jsx
bhuvighosh3
Deployment with restored Stress Specialist data
9fbb3f2
Raw History Blame Contribute Delete
2.73 kB
import React, { useState } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { askCIO } from "../api";
export default function CIOChat({
metrics,
inferences,
frontierSummary,
userProfile,
goalProjection,
chatHistory,
onChatUpdate,
}) {
const [input, setInput] = useState("");
const [loading, setLoading] = useState(false);
const messages = chatHistory || [];
const send = async () => {
const q = input.trim();
if (!q || loading) return;
const userMsg = { role: "user", content: q };
const updated = [...messages, userMsg];
onChatUpdate(updated);
setInput("");
setLoading(true);
try {
const data = await askCIO(q, metrics, inferences, frontierSummary, userProfile, goalProjection);
const withReply = [...updated, { role: "assistant", content: data.answer }];
onChatUpdate(withReply);
} catch (e) {
const withError = [...updated, { role: "assistant", content: `Error: ${e.message}` }];
onChatUpdate(withError);
} finally {
setLoading(false);
}
};
const handleKeyDown = (e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
send();
}
};
return (
<section className="section">
<h2>Ask the CIO</h2>
<div className="chat-container">
<div className="chat-messages">
{messages.length === 0 && (
<p className="caption">
Ask about allocation, risk, performance, efficient frontier, your
risk profile...
</p>
)}
{messages.map((m, i) => (
<div key={i} className={`chat-msg ${m.role}`}>
<span className="chat-role">
{m.role === "user" ? "You" : "CIO"}
</span>
<div className="chat-text markdown-body">
<ReactMarkdown remarkPlugins={[remarkGfm]}>{m.content}</ReactMarkdown>
</div>
</div>
))}
{loading && (
<div className="chat-msg assistant">
<span className="chat-role">CIO</span>
<span className="chat-text thinking">Thinking...</span>
</div>
)}
</div>
<div className="chat-input-row">
<input
className="chat-input"
value={input}
onChange={(e) => setInput(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Ask about your portfolio..."
disabled={loading}
/>
<button className="btn primary" onClick={send} disabled={loading}>
Send
</button>
</div>
</div>
</section>
);
}