File size: 2,726 Bytes
9fbb3f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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>
  );
}