aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
blob: 065c2780c74587cffad91c91e33628091c152363 (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
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
/**
 * @file ChatBox.tsx
 * @description This file defines the ChatBox component, which manages user interactions with
 * an AI assistant. It handles document uploads, chat history, message input, and integration
 * with the OpenAI API. The ChatBox is MobX-observable and tracks the progress of tasks such as
 * document analysis and AI-driven summaries. It also maintains real-time chat functionality
 * with support for follow-up questions and citation management.
 */

import dotenv from 'dotenv';
import { ObservableSet, action, computed, makeObservable, observable, observe, reaction, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import OpenAI, { ClientOptions } from 'openai';
import * as React from 'react';
import { v4 as uuidv4 } from 'uuid';
import { ClientUtils, OmitKeys } from '../../../../../ClientUtils';
import { Doc, DocListCast, Opt } from '../../../../../fields/Doc';
import { DocData, DocViews } from '../../../../../fields/DocSymbols';
import { RichTextField } from '../../../../../fields/RichTextField';
import { ScriptField } from '../../../../../fields/ScriptField';
import { CsvCast, DocCast, NumCast, PDFCast, RTFCast, StrCast } from '../../../../../fields/Types';
import { DocUtils } from '../../../../documents/DocUtils';
import { CollectionViewType, DocumentType } from '../../../../documents/DocumentTypes';
import { Docs, DocumentOptions } from '../../../../documents/Documents';
import { DocumentManager } from '../../../../util/DocumentManager';
import { ImageUtils } from '../../../../util/Import & Export/ImageUtils';
import { LinkManager } from '../../../../util/LinkManager';
import { CompileError, CompileScript } from '../../../../util/Scripting';
import { DictationButton } from '../../../DictationButton';
import { ViewBoxAnnotatableComponent } from '../../../DocComponent';
import { AudioBox } from '../../AudioBox';
import { DocumentView, DocumentViewInternal } from '../../DocumentView';
import { FieldView, FieldViewProps } from '../../FieldView';
import { PDFBox } from '../../PDFBox';
import { ScriptingBox } from '../../ScriptingBox';
import { VideoBox } from '../../VideoBox';
import { Agent } from '../agentsystem/Agent';
import { supportedDocTypes } from '../tools/CreateDocumentTool';
import { ASSISTANT_ROLE, AssistantMessage, CHUNK_TYPE, Citation, ProcessingInfo, SimplifiedChunk, TEXT_TYPE } from '../types/types';
import { Vectorstore } from '../vectorstore/Vectorstore';
import './ChatBox.scss';
import MessageComponentBox from './MessageComponent';
import { ProgressBar } from './ProgressBar';
import { OpenWhere } from '../../OpenWhere';
import { Upload } from '../../../../../server/SharedMediaTypes';
import { DocumentMetadataTool } from '../tools/DocumentMetadataTool';

dotenv.config();

export type parsedDocData = { doc_type: string; data: unknown };
export type parsedDoc = DocumentOptions & parsedDocData;
/**
 * ChatBox is the main class responsible for managing the interaction between the user and the assistant,
 * handling documents, and integrating with OpenAI for tasks such as document analysis, chat functionality,
 * and vector store interactions.
 */
@observer
export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
    // MobX observable properties to track UI state and data
    @observable private _history: AssistantMessage[] = [];
    @observable.deep private _current_message: AssistantMessage | undefined = undefined;
    @observable private _isLoading: boolean = false;
    @observable private _uploadProgress: number = 0;
    @observable private _currentStep: string = '';
    @observable private _expandedScratchpadIndex: number | null = null;
    @observable private _inputValue: string = '';
    @observable private _linked_docs_to_add: ObservableSet = observable.set();
    @observable private _linked_csv_files: { filename: string; id: string; text: string }[] = [];
    @observable private _isUploadingDocs: boolean = false;
    @observable private _citationPopup: { text: string; visible: boolean } = { text: '', visible: false };

    // Private properties for managing OpenAI API, vector store, agent, and UI elements
    private openai: OpenAI;
    private vectorstore_id: string;
    private vectorstore: Vectorstore;
    private agent: Agent;
    private messagesRef: React.RefObject<HTMLDivElement>;
    private _textInputRef: HTMLInputElement | undefined | null;

    /**
     * Static method that returns the layout string for the field.
     * @param fieldKey Key to get the layout string.
     */
    public static LayoutString(fieldKey: string) {
        return FieldView.LayoutString(ChatBox, fieldKey);
    }

    setChatInput = action((input: string) => {
        this._inputValue = input;
    });

    /**
     * Constructor initializes the component, sets up OpenAI, vector store, and agent instances,
     * and observes changes in the chat history to save the state in dataDoc.
     * @param props The properties passed to the component.
     */
    constructor(props: FieldViewProps) {
        super(props);
        makeObservable(this); // Enable MobX observables

        // Initialize OpenAI, vectorstore, and agent
        this.openai = this.initializeOpenAI();
        if (StrCast(this.dataDoc.vectorstore_id) == '') {
            this.vectorstore_id = uuidv4();
            this.dataDoc.vectorstore_id = this.vectorstore_id;
        } else {
            this.vectorstore_id = StrCast(this.dataDoc.vectorstore_id);
        }
        this.vectorstore = new Vectorstore(this.vectorstore_id, this.retrieveDocIds);
        this.agent = new Agent(
            this.vectorstore, 
            this.retrieveSummaries, 
            this.retrieveFormattedHistory, 
            this.retrieveCSVData, 
            this.addLinkedUrlDoc, 
            this.createImageInDash, 
            this.createDocInDash, 
            this.createCSVInDash
        );
        
        // Reinitialize the DocumentMetadataTool with a direct reference to this ChatBox instance
        // This ensures the tool can properly access documents in the same Freeform view
        this.agent.reinitializeDocumentMetadataTool(this);
        
        this.messagesRef = React.createRef<HTMLDivElement>();

        // Reaction to update dataDoc when chat history changes
        reaction(
            () =>
                this._history.map((msg: AssistantMessage) => ({
                    role: msg.role,
                    content: msg.content,
                    follow_up_questions: msg.follow_up_questions,
                    citations: msg.citations,
                })),
            serializableHistory => {
                this.dataDoc.data = JSON.stringify(serializableHistory);
            }
        );
    }

    /**
     * Adds a document to the vectorstore for AI-based analysis.
     * Handles the upload progress and errors during the process.
     * @param newLinkedDoc The new document to add.
     */
    @action
    addDocToVectorstore = async (newLinkedDoc: Doc) => {
        this._uploadProgress = 0;
        this._currentStep = 'Initializing...';
        this._isUploadingDocs = true;

        try {
            // Add the document to the vectorstore
            await this.vectorstore.addAIDoc(newLinkedDoc, this.updateProgress);
        } catch (error) {
            console.error('Error uploading document:', error);
            this._currentStep = 'Error during upload';
        } finally {
            runInAction(() => {
                this._isUploadingDocs = false;
                this._uploadProgress = 0;
                this._currentStep = '';
            });
        }
    };

    /**
     * Updates the upload progress and the current step in the UI.
     * @param progress The percentage of the progress.
     * @param step The current step name.
     */
    @action
    updateProgress = (progress: number, step: string) => {
        this._uploadProgress = progress;
        this._currentStep = step;
    };

    /**
     * Adds a CSV file for analysis by sending it to OpenAI and generating a summary.
     * @param newLinkedDoc The linked document representing the CSV file.
     * @param id Optional ID for the document.
     */
    @action
    addCSVForAnalysis = async (newLinkedDoc: Doc, id?: string) => {
        if (!newLinkedDoc.chunk_simpl) {
            // Convert document text to CSV data
            const csvData: string = StrCast(newLinkedDoc.text);

            // Generate a summary using OpenAI API
            const completion = await this.openai.chat.completions.create({
                messages: [
                    {
                        role: 'system',
                        content:
                            'You are an AI assistant tasked with summarizing the content of a CSV file. You will be provided with the data from the CSV file and your goal is to generate a concise summary that captures the main themes, trends, and key points represented in the data.',
                    },
                    {
                        role: 'user',
                        content: `Please provide a comprehensive summary of the CSV file based on the provided data. Ensure the summary highlights the most important information, patterns, and insights. Your response should be in paragraph form and be concise.
                    CSV Data:
                    ${csvData}
                    **********
                    Summary:`,
                    },
                ],
                model: 'gpt-3.5-turbo',
            });

            const csvId = id ?? uuidv4();

            // Add CSV details to linked files
            this._linked_csv_files.push({
                filename: CsvCast(newLinkedDoc.data).url.pathname,
                id: csvId,
                text: csvData,
            });

            // Add a chunk for the CSV and assign the summary
            const chunkToAdd = {
                chunkId: csvId,
                chunkType: CHUNK_TYPE.CSV,
            };
            newLinkedDoc.chunk_simpl = JSON.stringify({ chunks: [chunkToAdd] });
            newLinkedDoc.summary = completion.choices[0].message.content!;
        }
    };

    /**
     * Toggles the tool logs, expanding or collapsing the scratchpad at the given index.
     * @param index Index of the tool log to toggle.
     */
    @action
    toggleToolLogs = (index: number) => {
        this._expandedScratchpadIndex = this._expandedScratchpadIndex === index ? null : index;
    };

    /**
     * Initializes the OpenAI API client using the API key from environment variables.
     * @returns OpenAI client instance.
     */
    initializeOpenAI() {
        const configuration: ClientOptions = {
            apiKey: process.env.OPENAI_KEY,
            dangerouslyAllowBrowser: true,
        };
        return new OpenAI(configuration);
    }

    /**
     * Adds a scroll event listener to detect user scrolling and handle passive wheel events.
     */
    addScrollListener = () => {
        if (this.messagesRef.current) {
            this.messagesRef.current.addEventListener('wheel', this.onPassiveWheel, { passive: false });
        }
    };

    /**
     * Removes the scroll event listener from the chat messages container.
     */
    removeScrollListener = () => {
        if (this.messagesRef.current) {
            this.messagesRef.current.removeEventListener('wheel', this.onPassiveWheel);
        }
    };

    /**
     * Scrolls the chat messages container to the bottom, ensuring the latest message is visible.
     */
    scrollToBottom = () => {
        // if (this.messagesRef.current) {
        //     this.messagesRef.current.scrollTop = this.messagesRef.current.scrollHeight;
        // }
    };

    /**
     * Event handler for detecting wheel scrolling and stopping the event propagation.
     * @param e The wheel event.
     */
    onPassiveWheel = (e: WheelEvent) => {
        if (this._props.isContentActive()) {
            e.stopPropagation();
        }
    };

    /**
     * Sends the user's input to OpenAI, displays the loading indicator, and updates the chat history.
     * @param event The form submission event.
     */
    @action
    askGPT = async (event: React.FormEvent): Promise<void> => {
        event.preventDefault();
        this._inputValue = '';

        // Extract the user's message
        const textInput = (event.currentTarget as HTMLFormElement).elements.namedItem('messageInput') as HTMLInputElement;
        const trimmedText = textInput.value.trim();

        if (trimmedText) {
            try {
                textInput.value = '';
                // Add the user's message to the history
                this._history.push({
                    role: ASSISTANT_ROLE.USER,
                    content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: trimmedText, citation_ids: null }],
                    processing_info: [],
                });
                this._isLoading = true;
                this._current_message = {
                    role: ASSISTANT_ROLE.ASSISTANT,
                    content: [],
                    citations: [],
                    processing_info: [],
                };

                // Define callbacks for real-time processing updates
                const onProcessingUpdate = (processingUpdate: ProcessingInfo[]) => {
                    runInAction(() => {
                        if (this._current_message) {
                            this._current_message = {
                                ...this._current_message,
                                processing_info: processingUpdate,
                            };
                        }
                    });
                    this.scrollToBottom();
                };

                const onAnswerUpdate = (answerUpdate: string) => {
                    runInAction(() => {
                        if (this._current_message) {
                            this._current_message = {
                                ...this._current_message,
                                content: [{ text: answerUpdate, type: TEXT_TYPE.NORMAL, index: 0, citation_ids: [] }],
                            };
                        }
                    });
                };

                // Send the user's question to the assistant and get the final message
                const finalMessage = await this.agent.askAgent(trimmedText, onProcessingUpdate, onAnswerUpdate);

                // Update the history with the final assistant message
                runInAction(() => {
                    if (this._current_message) {
                        this._history.push({ ...finalMessage });
                        this._current_message = undefined;
                        this.dataDoc.data = JSON.stringify(this._history);
                    }
                });
            } catch (err) {
                console.error('Error:', err);
                // Handle error in processing
                runInAction(() =>
                    this._history.push({
                        role: ASSISTANT_ROLE.ASSISTANT,
                        content: [{ index: 0, type: TEXT_TYPE.ERROR, text: `Sorry, I encountered an error while processing your request: ${err} `, citation_ids: null }],
                        processing_info: [],
                    })
                );
            } finally {
                runInAction(() => {
                    this._isLoading = false;
                });
                this.scrollToBottom();
            }
        }
        this.scrollToBottom();
    };

    /**
     * Updates the citations for a given message in the chat history.
     * @param index The index of the message in the history.
     * @param citations The list of citations to add to the message.
     */
    @action
    updateMessageCitations = (index: number, citations: Citation[]) => {
        if (this._history[index]) {
            this._history[index].citations = citations;
        }
    };

    /**
     * Adds a linked document from a URL for future reference and analysis.
     * @param url The URL of the document to add.
     * @param id The unique identifier for the document.
     */
    @action
    addLinkedUrlDoc = async (url: string, id: string) => {
        const doc = Docs.Create.WebDocument(url, { data_useCors: true });

        const linkDoc = Docs.Create.LinkDocument(this.Document, doc);
        LinkManager.Instance.addLink(linkDoc);

        const chunkToAdd = {
            chunkId: id,
            chunkType: CHUNK_TYPE.URL,
            url: url,
        };

        doc.chunk_simpl = JSON.stringify({ chunks: [chunkToAdd] });
    };

    /**
     * Getter to retrieve the current user's name from the client utils.
     */
    @computed
    get userName() {
        return ClientUtils.CurrentUserEmail;
    }

    /**
     * Creates a CSV document in the dashboard and adds it for analysis.
     * @param url The URL of the CSV.
     * @param title The title of the CSV document.
     * @param id The unique ID for the document.
     * @param data The CSV data content.
     */
    @action
    createCSVInDash = (url: string, title: string, id: string, data: string) =>
        DocUtils.DocumentFromType('csv', url, { title: title, text: RTFCast(data) }).then(doc => {
            if (doc) {
                LinkManager.Instance.addLink(Docs.Create.LinkDocument(this.Document, doc));
                this._props.addDocument?.(doc);
                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => this.addCSVForAnalysis(doc, id));
            }
        });

    @action
    createImageInDash = async (result: Upload.FileInformation & Upload.InspectionResults, options: DocumentOptions) => {
        const newImgSrc =
            result.accessPaths.agnostic.client.indexOf('dashblobstore') === -1 //
                ? ClientUtils.prepend(result.accessPaths.agnostic.client)
                : result.accessPaths.agnostic.client;
        const doc = Docs.Create.ImageDocument(newImgSrc, options);
        this.addDocument(ImageUtils.AssignImgInfo(doc, result));
        const linkDoc = Docs.Create.LinkDocument(this.Document, doc);
        LinkManager.Instance.addLink(linkDoc);
        if (doc) {
            if (this._props.addDocument) this._props.addDocument(doc);
            else DocumentViewInternal.addDocTabFunc(doc, OpenWhere.addRight);
        }
        await DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
    };

    /**
     * Creates a text document in the dashboard and adds it for analysis.
     * @param title The title of the doc.
     * @param text_content The text of the document.
     * @param options Other optional document options (e.g. color)
     * @param id The unique ID for the document.
     */
    @action
    private createCollectionWithChildren = (data: parsedDoc[], insideCol: boolean): Opt<Doc>[] => data.map(doc => this.whichDoc(doc, insideCol));

    @action
    whichDoc = (doc: parsedDoc, insideCol: boolean): Opt<Doc> => {
        const options = OmitKeys(doc, ['doct_type', 'data']).omit as DocumentOptions;
        const data = (doc as parsedDocData).data;
        const ndoc = (() => {
            switch (doc.doc_type) {
                default:           
                case supportedDocTypes.text:       return Docs.Create.TextDocument(data as string, options);
                case supportedDocTypes.comparison: return this.createComparison(JSON.parse(data as string) as parsedDoc[], options);
                case supportedDocTypes.flashcard:  return this.createFlashcard(JSON.parse(data as string) as  parsedDoc[], options);
                case supportedDocTypes.deck:       return this.createDeck(JSON.parse(data as string) as  parsedDoc[], options);
                case supportedDocTypes.image:      return Docs.Create.ImageDocument(data as string, options);
                case supportedDocTypes.equation:   return Docs.Create.EquationDocument(data as string, options);
                case supportedDocTypes.notetaking: return Docs.Create.NoteTakingDocument([], options);
                case supportedDocTypes.web:        return Docs.Create.WebDocument(data as string, { ...options, data_useCors: true });
                case supportedDocTypes.dataviz:    return Docs.Create.DataVizDocument('/users/rz/Downloads/addresses.csv', options);
                case supportedDocTypes.pdf:        return Docs.Create.PdfDocument(data as string, options);
                case supportedDocTypes.video:      return Docs.Create.VideoDocument(data as string, options);
                case supportedDocTypes.diagram:    return Docs.Create.DiagramDocument(undefined, { text: data as unknown as RichTextField, ...options}); // text: can take a string or RichTextField but it's typed for RichTextField.
                
                // case supportedDocumentTypes.dataviz: 
                // {
                //     const { fileUrl, id } = await Networking.PostToServer('/createCSV', {
                //         filename: (options.title as string).replace(/\s+/g, '') + '.csv',
                //         data: data,
                //     });
                //     const doc = Docs.Create.DataVizDocument(fileUrl, { ...options, text: RTFCast(data as string) });
                //     this.addCSVForAnalysis(doc, id);
                //     return doc;
                // }
                case supportedDocTypes.script: {
                    const result = !(data as string).trim() ? ({ compiled: false, errors: [] } as CompileError) : CompileScript(data as string, {});
                    const script_field = result.compiled ? new ScriptField(result, undefined, data as string) : undefined;
                    const sdoc = Docs.Create.ScriptingDocument(script_field, options);
                    DocumentManager.Instance.showDocument(sdoc, { willZoomCentered: true }, () => {
                        const firstView = Array.from(sdoc[DocViews])[0] as DocumentView;
                        (firstView.ComponentView as ScriptingBox)?.onApply?.();
                        (firstView.ComponentView as ScriptingBox)?.onRun?.();
                    });
                    return sdoc;
                }
                case supportedDocTypes.collection: {
                    const arr = this.createCollectionWithChildren(JSON.parse(data as string) as parsedDoc[], true).filter(d=>d).map(d => d!);
                    const collOpts = {  _width:300, _height: 300, _layout_fitWidth: true,  _freeform_backgroundGrid: true, ...options,  };
                    return (() => {
                            switch (options.type_collection) {
                                case CollectionViewType.Tree:        return Docs.Create.TreeDocument(arr, collOpts);
                                case CollectionViewType.Stacking:    return Docs.Create.StackingDocument(arr, collOpts);
                                case CollectionViewType.Masonry:     return Docs.Create.MasonryDocument(arr, collOpts);
                                case CollectionViewType.Card:        return Docs.Create.CardDeckDocument(arr, collOpts);
                                case CollectionViewType.Carousel:    return Docs.Create.CarouselDocument(arr, collOpts);
                                case CollectionViewType.Carousel3D:  return Docs.Create.Carousel3DDocument(arr, collOpts);
                                case CollectionViewType.Multicolumn: return Docs.Create.CarouselDocument(arr, collOpts);
                                default:                             return Docs.Create.FreeformDocument(arr, collOpts);
                            }
                        })();
                    }
                // case supportedDocumentTypes.map:        return Docs.Create.MapDocument([], options);
                // case supportedDocumentTypes.button:     return Docs.Create.ButtonDocument(options);
                // case supportedDocumentTypes.trail:      return Docs.Create.PresDocument(options);
            } // prettier-ignore
        })();

        if (ndoc) {
            ndoc.x = NumCast((options.x as number) ?? 0) + (insideCol ? 0 : NumCast(this.layoutDoc.x) + NumCast(this.layoutDoc.width)) + 100;
            ndoc.y = NumCast(options.y as number) + (insideCol ? 0 : NumCast(this.layoutDoc.y));
        }
        return ndoc;
    };

    /**
     * Creates a document in the dashboard.
     *
     * @param {string} doc_type - The type of document to create.
     * @param {string} data - The data used to generate the document.
     * @param {DocumentOptions} options - Configuration options for the document.
     * @returns {Promise<void>} A promise that resolves once the document is created and displayed.
     */
    @action
    createDocInDash = (pdoc: parsedDoc) => {
        const linkAndShowDoc = (doc: Opt<Doc>) => {
            if (doc) {
                LinkManager.Instance.addLink(Docs.Create.LinkDocument(this.Document, doc));
                this._props.addDocument?.(doc);
                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
            }
        };
        const doc = this.whichDoc(pdoc, false);
        if (doc) linkAndShowDoc(doc);
        return doc;
    };

    /**
     * Creates a deck of flashcards.
     *
     * @param {any} data - The data used to generate the flashcards. Can be a string or an object.
     * @param {DocumentOptions} options - Configuration options for the flashcard deck.
     * @returns {Doc} A carousel document containing the flashcard deck.
     */
    @action
    createDeck = (data: parsedDoc[], options: DocumentOptions) => {
        const flashcardDeck: Doc[] = [];
        // Process each flashcard document in the `deckData` array
        if (data.length == 2 && data[0].doc_type == 'text' && data[1].doc_type == 'text') {
            this.createFlashcard(data, options);
        } else {
            data.forEach(doc => {
                const flashcardDoc = this.createFlashcard((doc as parsedDocData).data as parsedDoc[] | string[], options);
                if (flashcardDoc) flashcardDeck.push(flashcardDoc);
            });
        }

        // Create a carousel to contain the flashcard deck
        return Docs.Create.CarouselDocument(flashcardDeck, {
            title: options.title || 'Flashcard Deck',
            _width: options._width || 300,
            _height: options._height || 300,
            _layout_fitWidth: false,
            _layout_autoHeight: true,
        });
    };

    /**
     * Creates a single flashcard document.
     *
     * @param {any} data - The data used to generate the flashcard. Can be a string or an object.
     * @param {any} options - Configuration options for the flashcard.
     * @returns {Doc | undefined} The created flashcard document, or undefined if the flashcard cannot be created.
     */
    @action
    createFlashcard = (data: parsedDoc[] | string[], options: DocumentOptions) => {
        const [front, back] = data;
        const sideOptions = { _height: 300, ...options };

        // Create front and back text documents
        const side1 = typeof front === 'string' ? Docs.Create.CenteredTextCreator('question', front as string, sideOptions) : this.whichDoc(front, false);
        const side2 = typeof back === 'string' ? Docs.Create.CenteredTextCreator('answer', back as string, sideOptions) : this.whichDoc(back, false);

        // Create the flashcard document with both sides
        return Docs.Create.FlashcardDocument('flashcard', side1, side2, sideOptions);
    };

    /**
     * Creates a comparison document.
     *
     * @param {any} doc - The document data containing left and right components for comparison.
     * @param {any} options - Configuration options for the comparison document.
     * @returns {Doc} The created comparison document.
     */
    @action
    createComparison = (doc: parsedDoc[], options: DocumentOptions) =>
        Docs.Create.ComparisonDocument(options.title as string, {
            data_back: this.whichDoc(doc[0], false),
            data_front: this.whichDoc(doc[1], false),
            _width: options._width,
            _height: options._height || 300,
            backgroundColor: options.backgroundColor,
        });

    /**
     * Event handler to manage citations click in the message components.
     * @param citation The citation object clicked by the user.
     */
    @action
    handleCitationClick = async (citation: Citation) => {
        const currentLinkedDocs: Doc[] = this.linkedDocs;
        const chunkId = citation.chunk_id;

        for (const doc of currentLinkedDocs) {
            if (doc.chunk_simpl) {
                const docChunkSimpl = JSON.parse(StrCast(doc.chunk_simpl)) as { chunks: SimplifiedChunk[] };
                const foundChunk = docChunkSimpl.chunks.find(chunk => chunk.chunkId === chunkId);

                if (foundChunk) {
                    // Handle media chunks specifically

                    if (doc.ai_type == 'video' || doc.ai_type == 'audio') {
                        const directMatchSegmentStart = this.getDirectMatchingSegmentStart(doc, citation.direct_text || '', foundChunk.indexes || []);

                        if (directMatchSegmentStart) {
                            // Navigate to the segment's start time in the media player
                            await this.goToMediaTimestamp(doc, directMatchSegmentStart, doc.ai_type);
                        } else {
                            console.error('No direct matching segment found for the citation.');
                        }
                    } else {
                        // Handle other chunk types as before
                        this.handleOtherChunkTypes(foundChunk, citation, doc);
                    }
                }
            }
        }
    };

    getDirectMatchingSegmentStart = (doc: Doc, citationText: string, indexesOfSegments: string[]): number => {
        const originalSegments = JSON.parse(StrCast(doc.original_segments!)).map((segment: any, index: number) => ({
            index: index.toString(),
            text: segment.text,
            start: segment.start,
            end: segment.end,
        }));

        if (!Array.isArray(originalSegments) || originalSegments.length === 0 || !Array.isArray(indexesOfSegments)) {
            return 0;
        }

        // Create itemsToSearch array based on indexesOfSegments
        const itemsToSearch = indexesOfSegments.map((indexStr: string) => {
            const index = parseInt(indexStr, 10);
            const segment = originalSegments[index];
            return { text: segment.text, start: segment.start };
        });

        console.log('Constructed itemsToSearch:', itemsToSearch);

        // Helper function to calculate word overlap score
        const calculateWordOverlap = (text1: string, text2: string): number => {
            const words1 = new Set(text1.toLowerCase().split(/\W+/));
            const words2 = new Set(text2.toLowerCase().split(/\W+/));
            const intersection = new Set([...words1].filter(word => words2.has(word)));
            return intersection.size / Math.max(words1.size, words2.size); // Jaccard similarity
        };

        // Search for the best matching segment
        let bestMatchStart = 0;
        let bestScore = 0;

        console.log(`Searching for best match for query: "${citationText}"`);
        itemsToSearch.forEach(item => {
            const score = calculateWordOverlap(citationText, item.text);
            console.log(`Comparing query to segment: "${item.text}" | Score: ${score}`);
            if (score > bestScore) {
                bestScore = score;
                bestMatchStart = item.start;
            }
        });

        console.log('Best match found with score:', bestScore, '| Start time:', bestMatchStart);

        // Return the start time of the best match
        return bestMatchStart;
    };

    /**
     * Navigates to the given timestamp in the media player.
     * @param doc The document containing the media file.
     * @param timestamp The timestamp to navigate to.
     */
    goToMediaTimestamp = async (doc: Doc, timestamp: number, type: 'video' | 'audio') => {
        try {
            // Show the media document in the viewer
            if (type == 'video') {
                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {
                    const firstView = Array.from(doc[DocViews])[0] as DocumentView;
                    (firstView.ComponentView as VideoBox)?.Seek?.(timestamp);
                });
            } else {
                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {
                    const firstView = Array.from(doc[DocViews])[0] as DocumentView;
                    (firstView.ComponentView as AudioBox)?.playFrom?.(timestamp);
                });
            }
            console.log(`Navigated to timestamp: ${timestamp}s in document ${doc.id}`);
        } catch (error) {
            console.error('Error navigating to media timestamp:', error);
        }
    };

    /**
     * Handles non-media chunk types as before.
     * @param foundChunk The chunk object.
     * @param citation The citation object.
     * @param doc The document containing the chunk.
     */
    handleOtherChunkTypes = (foundChunk: SimplifiedChunk, citation: Citation, doc: Doc) => {
        switch (foundChunk.chunkType) {
            case CHUNK_TYPE.IMAGE:
            case CHUNK_TYPE.TABLE:
                {
                    const values = foundChunk.location?.replace(/[[\]]/g, '').split(',');

                    if (values?.length !== 4) {
                        console.error('Location string must contain exactly 4 numbers');
                        return;
                    }
                    if (foundChunk.startPage === undefined || foundChunk.endPage === undefined) {
                        DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
                        return;
                    }
                    const x1 = parseFloat(values[0]) * Doc.NativeWidth(doc);
                    const y1 = parseFloat(values[1]) * Doc.NativeHeight(doc) + foundChunk.startPage * Doc.NativeHeight(doc);
                    const x2 = parseFloat(values[2]) * Doc.NativeWidth(doc);
                    const y2 = parseFloat(values[3]) * Doc.NativeHeight(doc) + foundChunk.startPage * Doc.NativeHeight(doc);

                    const annotationKey = Doc.LayoutFieldKey(doc) + '_annotations';

                    const existingDoc = DocListCast(doc[DocData][annotationKey]).find(d => d.citation_id === citation.citation_id);
                    const highlightDoc = existingDoc ?? this.createImageCitationHighlight(x1, y1, x2, y2, citation, annotationKey, doc);

                    DocumentManager.Instance.showDocument(highlightDoc, { willZoomCentered: true }, () => {});
                }
                break;
            case CHUNK_TYPE.TEXT:
                this._citationPopup = { text: citation.direct_text ?? 'No text available', visible: true };
                setTimeout(() => (this._citationPopup.visible = false), 3000);

                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {
                    const firstView = Array.from(doc[DocViews])[0] as DocumentView;
                    (firstView.ComponentView as PDFBox)?.gotoPage?.(foundChunk.startPage ?? 0);
                    (firstView.ComponentView as PDFBox)?.search?.(citation.direct_text ?? '');
                });
                break;
            case CHUNK_TYPE.CSV:
            case CHUNK_TYPE.URL:
                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true });
                break;
            default:
                console.error('Unhandled chunk type:', foundChunk.chunkType);
                break;
        }
    };
    /**
     * Creates an annotation highlight on a PDF document for image citations.
     * @param x1 X-coordinate of the top-left corner of the highlight.
     * @param y1 Y-coordinate of the top-left corner of the highlight.
     * @param x2 X-coordinate of the bottom-right corner of the highlight.
     * @param y2 Y-coordinate of the bottom-right corner of the highlight.
     * @param citation The citation object to associate with the highlight.
     * @param annotationKey The key used to store the annotation.
     * @param pdfDoc The document where the highlight is created.
     * @returns The highlighted document.
     */
    createImageCitationHighlight = (x1: number, y1: number, x2: number, y2: number, citation: Citation, annotationKey: string, pdfDoc: Doc): Doc => {
        const highlight_doc = Docs.Create.FreeformDocument([], {
            x: x1,
            y: y1,
            _width: x2 - x1,
            _height: y2 - y1,
            backgroundColor: 'rgba(255, 255, 0, 0.5)',
        });
        highlight_doc[DocData].citation_id = citation.citation_id;
        Doc.AddDocToList(pdfDoc[DocData], annotationKey, highlight_doc);
        highlight_doc.annotationOn = pdfDoc;
        Doc.SetContainer(highlight_doc, pdfDoc);
        return highlight_doc;
    };

    /**
     * Lifecycle method that triggers when the component updates.
     * Ensures the chat is scrolled to the bottom when new messages are added.
     */
    componentDidUpdate() {
        this.scrollToBottom();
    }

    /**
     * Lifecycle method that triggers when the component mounts.
     * Initializes scroll listeners, sets up document reactions, and loads chat history from dataDoc if available.
     */
    componentDidMount() {
        this._props.setContentViewBox?.(this);
        if (this.dataDoc.data) {
            try {
                const storedHistory = JSON.parse(StrCast(this.dataDoc.data));
                runInAction(() => {
                    this._history.push(
                        ...storedHistory.map((msg: AssistantMessage) => ({
                            role: msg.role,
                            content: msg.content,
                            follow_up_questions: msg.follow_up_questions,
                            citations: msg.citations,
                        }))
                    );
                });
            } catch (e) {
                console.error('Failed to parse history from dataDoc:', e);
            }
        } else {
            // Default welcome message
            runInAction(() => {
                this._history.push({
                    role: ASSISTANT_ROLE.ASSISTANT,
                    content: [
                        {
                            index: 0,
                            type: TEXT_TYPE.NORMAL,
                            text: `Hey, ${this.userName()}! Welcome to Your Friendly Assistant. Link a document or ask questions to get started.`,
                            citation_ids: null,
                        },
                    ],
                    processing_info: [],
                });
            });
        }

        // Set up reactions for linked documents
        reaction(
            () => {
                const linkedDocs = LinkManager.Instance.getAllRelatedLinks(this.Document)
                    .map(d => DocCast(LinkManager.getOppositeAnchor(d, this.Document)))
                    .map(d => DocCast(d?.annotationOn, d))
                    .filter(d => d);
                return linkedDocs;
            },
            linked => linked.forEach(doc => this._linked_docs_to_add.add(doc))
        );

        // Observe changes to linked documents and handle document addition
        observe(this._linked_docs_to_add, change => {
            if (change.type === 'add') {
                if (CsvCast(change.newValue.data)) {
                    this.addCSVForAnalysis(change.newValue);
                } else {
                    this.addDocToVectorstore(change.newValue);
                }
            } else if (change.type === 'delete') {
                // Handle document removal
            }
        });
        this.addScrollListener();
    }

    /**
     * Lifecycle method that triggers when the component unmounts.
     * Removes scroll listeners to avoid memory leaks.
     */
    componentWillUnmount() {
        this.removeScrollListener();
    }

    /**
     * Getter that retrieves all linked documents for the current document.
     */
    @computed
    get linkedDocs() {
        return LinkManager.Instance.getAllRelatedLinks(this.Document)
            .map(d => DocCast(LinkManager.getOppositeAnchor(d, this.Document)))
            .map(d => DocCast(d?.annotationOn, d))
            .filter(d => d);
    }

    /**
     * Getter that retrieves document IDs of linked documents that have AI-related content.
     */
    @computed
    get docIds() {
        return LinkManager.Instance.getAllRelatedLinks(this.Document)
            .map(d => DocCast(LinkManager.getOppositeAnchor(d, this.Document)))
            .map(d => DocCast(d?.annotationOn, d))
            .filter(d => d)
            .filter(d => {
                console.log(d.ai_doc_id);
                return d.ai_doc_id;
            })
            .map(d => StrCast(d.ai_doc_id));
    }

    /**
     * Getter that retrieves summaries of all linked documents.
     */
    @computed
    get summaries(): string {
        return (
            LinkManager.Instance.getAllRelatedLinks(this.Document)
                .map(d => DocCast(LinkManager.getOppositeAnchor(d, this.Document)))
                .map(d => DocCast(d?.annotationOn, d))
                .filter(d => d)
                .filter(d => d.summary)
                .map((doc, index) => {
                    if (PDFCast(doc.data)) {
                        return `<summary file_name="${PDFCast(doc.data).url.pathname}" applicable_tools=["rag"]>${doc.summary}</summary>`;
                    } else if (CsvCast(doc.data)) {
                        return `<summary file_name="${CsvCast(doc.data).url.pathname}" applicable_tools=["dataAnalysis"]>${doc.summary}</summary>`;
                    } else {
                        return `${index + 1}) ${doc.summary}`;
                    }
                })
                .join('\n') + '\n'
        );
    }

    /**
     * Getter that retrieves all linked CSV files for analysis.
     */
    @computed get linkedCSVs(): { filename: string; id: string; text: string }[] {
        return this._linked_csv_files;
    }

    /**
     * Getter that formats the entire chat history as a string for the agent's system message.
     */
    @computed get formattedHistory(): string {
        let history = '<chat_history>\n';
        for (const message of this._history) {
            history += `<${message.role}>${message.content.map(content => content.text).join(' ')}`;
            if (message.loop_summary) {
                history += `<loop_summary>${message.loop_summary}</loop_summary>`;
            }
            history += `</${message.role}>\n`;
        }
        history += '</chat_history>';
        return history;
    }

    // Other helper methods for retrieving document data and processing

    retrieveSummaries = () => {
        return this.summaries;
    };

    retrieveCSVData = () => {
        return this.linkedCSVs;
    };

    retrieveFormattedHistory = () => {
        return this.formattedHistory;
    };

    retrieveDocIds = () => {
        return this.docIds;
    };

    /**
     * Handles follow-up questions when the user clicks on them.
     * Automatically sets the input value to the clicked follow-up question.
     * @param question The follow-up question clicked by the user.
     */
    @action
    handleFollowUpClick = (question: string) => {
        this._inputValue = question;
    };

    _dictation: DictationButton | null = null;
    /**
     * Renders the chat interface, including the message list, input field, and other UI elements.
     */
    render() {
        return (
            <div className="chat-box">
                {this._isUploadingDocs && (
                    <div className="uploading-overlay">
                        <div className="progress-container">
                            <ProgressBar />
                            <div className="step-name">{this._currentStep}</div>
                        </div>
                    </div>
                )}
                <div className="chat-header">
                    <h2>{this.userName()}&apos;s AI Assistant</h2>
                </div>
                <div className="chat-messages" ref={this.messagesRef}>
                    {this._history.map((message, index) => (
                        <MessageComponentBox key={index} message={message} onFollowUpClick={this.handleFollowUpClick} onCitationClick={this.handleCitationClick} updateMessageCitations={this.updateMessageCitations} />
                    ))}
                    {this._current_message && (
                        <MessageComponentBox key={this._history.length} message={this._current_message} onFollowUpClick={this.handleFollowUpClick} onCitationClick={this.handleCitationClick} updateMessageCitations={this.updateMessageCitations} />
                    )}
                </div>

                <form onSubmit={this.askGPT} className="chat-input">
                    <input
                        ref={r => {
                            this._textInputRef = r;
                        }}
                        type="text"
                        name="messageInput"
                        autoComplete="off"
                        placeholder="Type your message here..."
                        value={this._inputValue}
                        onChange={action(e => (this._inputValue = e.target.value))}
                        disabled={this._isLoading}
                    />
                    <button className="submit-button" onClick={() => this._dictation?.stopDictation()} type="submit" disabled={this._isLoading || !this._inputValue.trim()}>
                        {this._isLoading ? (
                            <div className="spinner"></div>
                        ) : (
                            <svg viewBox="0 0 24 24" width="24" height="24" stroke="currentColor" strokeWidth="2" fill="none" strokeLinecap="round" strokeLinejoin="round">
                                <line x1="22" y1="2" x2="11" y2="13"></line>
                                <polygon points="22 2 15 22 11 13 2 9 22 2"></polygon>
                            </svg>
                        )}
                    </button>
                    <DictationButton
                        ref={r => {
                            this._dictation = r;
                        }}
                        setInput={this.setChatInput}
                        inputRef={this._textInputRef}
                    />
                </form>
                {/* Popup for citation */}
                {this._citationPopup.visible && (
                    <div className="citation-popup">
                        <p>
                            <strong>Text from your document: </strong> {this._citationPopup.text}
                        </p>
                    </div>
                )}
            </div>
        );
    }
}

/**
 * Register the ChatBox component as the template for CHAT document types.
 */
Docs.Prototypes.TemplateMap.set(DocumentType.CHAT, {
    layout: { view: ChatBox, dataField: 'data' },
    options: { acl: '', _layout_fitWidth: true, chat: '', chat_history: '', chat_thread_id: '', chat_assistant_id: '', chat_vector_store_id: '' },
});