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
|
/* eslint-disable react/require-default-props */
import React from 'react';
import { observer } from 'mobx-react';
import { MathJax, MathJaxContext } from 'better-react-mathjax';
import ReactMarkdown from 'react-markdown';
import { AssistantMessage } from './types';
import { TbInfoCircleFilled } from 'react-icons/tb';
interface MessageComponentProps {
message: AssistantMessage;
toggleToolLogs: (index: number) => void;
expandedLogIndex: number | null;
index: number;
showModal: () => void;
goToLinkedDoc: (url: string) => void;
setCurrentFile: (file: { url: string }) => void;
onFollowUpClick: (question: string) => void; // New prop
isCurrent?: boolean;
}
const MessageComponent: React.FC<MessageComponentProps> = function ({
message,
toggleToolLogs,
expandedLogIndex,
goToLinkedDoc,
index,
showModal,
setCurrentFile,
onFollowUpClick, // New prop
isCurrent = false,
}) {
const LinkRenderer = ({ href, children }: { href: string; children: React.ReactNode }) => {
const regex = /([a-zA-Z0-9_.!-]+)~~~(citation|file_path)/;
const matches = href.match(regex);
const url = matches ? matches[1] : href;
const linkType = matches ? matches[2] : null;
if (linkType === 'citation') {
children = <TbInfoCircleFilled />;
}
const style = {
color: 'lightblue',
verticalAlign: linkType === 'citation' ? 'super' : 'baseline',
fontSize: linkType === 'citation' ? 'smaller' : 'inherit',
};
return (
<a
href="#"
onClick={e => {
e.preventDefault();
if (linkType === 'citation') {
goToLinkedDoc(url);
} else if (linkType === 'file_path') {
showModal();
setCurrentFile({ url });
}
}}
style={style}>
{children}
</a>
);
};
const parseMessage = (text: string) => {
const answerMatch = text.match(/<answer>([\s\S]*?)<\/answer>/);
const followUpMatch = text.match(/<follow_up_question>([\s\S]*?)<\/follow_up_question>/);
const answer = answerMatch ? answerMatch[1] : text;
const followUpQuestions = followUpMatch
? followUpMatch[1]
.split('\n')
.filter(q => q.trim())
.map(q => q.replace(/^\d+\.\s*/, '').trim())
: [];
return { answer, followUpQuestions };
};
const { answer, followUpQuestions } = parseMessage(message.text);
console.log('Parsed answer:', answer);
console.log('Parsed follow-up questions:', followUpQuestions);
return (
<div className={`message ${message.role}`}>
<ReactMarkdown components={{ a: LinkRenderer }}>{answer}</ReactMarkdown>
{message.image && <img src={message.image} alt="" />}
{followUpQuestions.length > 0 && (
<div className="follow-up-questions">
<h4>Follow-up Questions:</h4>
{followUpQuestions.map((question, idx) => (
<button key={idx} className="follow-up-button" onClick={() => onFollowUpClick(question)}>
{question}
</button>
))}
</div>
)}
<div className="message-footer">
{message.tool_logs && (
<button className="toggle-logs-button" onClick={() => toggleToolLogs(index)}>
{expandedLogIndex === index ? 'Hide Code Interpreter Logs' : 'Show Code Interpreter Logs'}
</button>
)}
{expandedLogIndex === index && (
<div className="tool-logs">
<pre>{message.tool_logs}</pre>
</div>
)}
</div>
</div>
);
};
export default observer(MessageComponent);
|