aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/MessageComponent.tsx
blob: 38faf7e0061353ffa061a040d866cd3efa130858 (plain)
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
import React from 'react';
import { observer } from 'mobx-react';
import { AssistantMessage, CHUNK_TYPE, Citation } from './types';
import { TbInfoCircleFilled } from 'react-icons/tb';
import { Docs } from '../../../documents/Documents';
import { DocumentType } from '../../../documents/DocumentTypes';

interface MessageComponentProps {
    message: AssistantMessage;
    index: number;
    onFollowUpClick: (question: string) => void;
    onCitationClick: (citation: Citation) => void;
    updateMessageCitations: (index: number, citations: Citation[]) => void;
}

const MessageComponentBox: React.FC<MessageComponentProps> = function ({ message, index, onFollowUpClick, onCitationClick, updateMessageCitations }) {
    // public static LayoutString(fieldKey: string) {
    //     return FieldView.LayoutString(MessageComponentBox, fieldKey);
    // }

    // the presentation view that renders this slide

    // @computed
    // get chatBoxView() {
    //     return this.DocumentView?.().containerViewPath?.().lastElement()?.ComponentView as ChatBox;
    // }

    const renderContent = (text: string) => {
        const citationRegex = /<citation chunk_id="([^"]*)" type="([^"]*)">([^<]*)<\/citation>/g;
        const parts = [];
        let lastIndex = 0;
        let match;
        const citations: Citation[] = [];

        while ((match = citationRegex.exec(text)) !== null) {
            const [fullMatch, chunkId, type, content] = match;
            const citation: Citation = { chunk_id: chunkId, type: type as CHUNK_TYPE, text: content };
            citations.push(citation);

            parts.push(text.slice(lastIndex, match.index));
            parts.push(
                <a
                    key={chunkId}
                    href="#"
                    onClick={e => {
                        e.preventDefault();
                        onCitationClick(citation);
                    }}
                    style={{
                        color: 'lightblue',
                        verticalAlign: 'super',
                        fontSize: 'smaller',
                    }}>
                    <TbInfoCircleFilled />
                </a>
            );
            lastIndex = match.index + fullMatch.length;
        }

        parts.push(text.slice(lastIndex));

        updateMessageCitations(index, citations);

        return <>{parts}</>;
    };

    return (
        <div className={`message ${message.role}`}>
            <div>{renderContent(message.text)}</div>
            {message.follow_up_questions && message.follow_up_questions.length > 0 && (
                <div className="follow-up-questions">
                    <h4>Follow-up Questions:</h4>
                    {message.follow_up_questions.map((question, idx) => (
                        <button key={idx} className="follow-up-button" onClick={() => onFollowUpClick(question)}>
                            {question}
                        </button>
                    ))}
                </div>
            )}
        </div>
    );
};

// Docs.Prototypes.TemplateMap.set(DocumentType.MESSAGE, {
//     layout: { view: MessageComponentBox, dataField: 'data' },
//     options: {
//         acl: '',
//         _height: 35,
//         _xMargin: 10,
//         _yMargin: 10,
//         _layout_nativeDimEditable: true,
//         _layout_reflowVertical: true,
//         _layout_reflowHorizontal: true,
//         defaultDoubleClick: 'ignore',
//         systemIcon: 'BsFileEarmarkTextFill',
//         layout_borderRounding: '10px',
//     },
// });

export default observer(MessageComponentBox);