From 4791cd23af08da70895204a3a7fbaf889d9af2d5 Mon Sep 17 00:00:00 2001
From: "A.J. Shulman" <Shulman.aj@gmail.com>
Date: Sat, 7 Sep 2024 12:43:05 -0400
Subject: completely restructured, added comments, and significantly reduced
 the length of the prompt (~72% shorter and cheaper)

---
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    | 745 +++++++++++++++++++++
 1 file changed, 745 insertions(+)
 create mode 100644 src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
new file mode 100644
index 000000000..6dc691798
--- /dev/null
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -0,0 +1,745 @@
+import { action, computed, makeObservable, observable, observe, reaction, runInAction, ObservableSet } from 'mobx';
+import { observer } from 'mobx-react';
+import OpenAI, { ClientOptions } from 'openai';
+import * as React from 'react';
+import { Doc, DocListCast } from '../../../../../fields/Doc';
+import { CsvCast, DocCast, PDFCast, RTFCast, StrCast } from '../../../../../fields/Types';
+import { DocumentType } from '../../../../documents/DocumentTypes';
+import { Docs } from '../../../../documents/Documents';
+import { LinkManager } from '../../../../util/LinkManager';
+import { ViewBoxAnnotatableComponent } from '../../../DocComponent';
+import { FieldView, FieldViewProps } from '../../FieldView';
+import './ChatBox.scss';
+import MessageComponentBox from './MessageComponent';
+import { ASSISTANT_ROLE, AssistantMessage, Citation, CHUNK_TYPE, TEXT_TYPE, SimplifiedChunk, ProcessingInfo } from '../types/types';
+import { Vectorstore } from '../vectorstore/Vectorstore';
+import { Agent } from '../agentsystem/Agent';
+import dotenv from 'dotenv';
+import { DocData, DocViews } from '../../../../../fields/DocSymbols';
+import { DocumentManager } from '../../../../util/DocumentManager';
+import { v4 as uuidv4 } from 'uuid';
+import { DocUtils } from '../../../../documents/DocUtils';
+import { ClientUtils } from '../../../../../ClientUtils';
+import { ProgressBar } from './ProgressBar';
+import { DocumentView } from '../../DocumentView';
+import { Networking } from '../../../../Network';
+
+dotenv.config();
+
+/**
+ * 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 history: AssistantMessage[] = [];
+    @observable.deep current_message: AssistantMessage | undefined = undefined;
+    @observable isLoading: boolean = false;
+    @observable uploadProgress: number = 0;
+    @observable currentStep: string = '';
+    @observable expandedScratchpadIndex: number | null = null;
+    @observable 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 _oldWheel: HTMLDivElement | null = null;
+    private messagesRef: React.RefObject;
+
+    /**
+     * 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);
+    }
+
+    /**
+     * 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.createCSVInDash);
+        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 {
+            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 => {
+        event.preventDefault();
+        this.inputValue = '';
+
+        // Extract the user's message
+        const textInput = event.currentTarget.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
+                this.history.push({
+                    role: ASSISTANT_ROLE.ASSISTANT,
+                    content: [{ index: 0, type: TEXT_TYPE.ERROR, text: 'Sorry, I encountered an error while processing your request.', citation_ids: null }],
+                    processing_info: [],
+                });
+            } finally {
+                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);
+        let canDisplay;
+
+        try {
+            // Fetch the URL content through the proxy
+            const { data } = await Networking.PostToServer('/proxyFetch', { url });
+
+            // Simulating header behavior since we can't fetch headers via proxy
+            const xFrameOptions = data.headers?.['x-frame-options'];
+
+            if (xFrameOptions && xFrameOptions.toUpperCase() === 'SAMEORIGIN') {
+                canDisplay = false;
+            } else {
+                canDisplay = true;
+            }
+        } catch (error) {
+            console.error('Error fetching the URL from the server:', error);
+        }
+
+        const chunkToAdd = {
+            chunkId: id,
+            chunkType: CHUNK_TYPE.URL,
+            url: url,
+            canDisplay: canDisplay,
+        };
+
+        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 = async (url: string, title: string, id: string, data: string) => {
+        const doc = DocCast(await DocUtils.DocumentFromType('csv', url, { title: title, text: RTFCast(data) }));
+
+        const linkDoc = Docs.Create.LinkDocument(this.Document, doc);
+        LinkManager.Instance.addLink(linkDoc);
+
+        doc && this._props.addDocument?.(doc);
+        await DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
+
+        this.addCSVForAnalysis(doc, id);
+    };
+
+    /**
+     * Event handler to manage citations click in the message components.
+     * @param citation The citation object clicked by the user.
+     */
+    @action
+    handleCitationClick = (citation: Citation) => {
+        const currentLinkedDocs: Doc[] = this.linkedDocs;
+
+        const chunkId = citation.chunk_id;
+
+        // Loop through the linked documents to find the matching chunk and handle its display
+        for (let 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 different types of chunks (image, text, table, etc.)
+                    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;
+                            }
+
+                            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); // Hide after 3 seconds
+
+                            DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {
+                                const firstView = Array.from(doc[DocViews])[0] as DocumentView;
+                                firstView.ComponentView?.search?.(citation.direct_text ?? '');
+                            });
+                            break;
+                        case CHUNK_TYPE.URL:
+                            if (!foundChunk.canDisplay) {
+                                window.open(StrCast(doc.displayUrl), '_blank');
+                            } else if (foundChunk.canDisplay) {
+                                DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
+                            }
+                            break;
+                        case CHUNK_TYPE.CSV:
+                            DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
+                            break;
+                        default:
+                            console.error('Chunk type not recognized:', 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 (PDFCast(change.newValue.data)) {
+                    this.addDocToVectorstore(change.newValue);
+                } else if (CsvCast(change.newValue.data)) {
+                    this.addCSVForAnalysis(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 => 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;
+    };
+
+    /**
+     * 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()}'s AI Assistant</h2>
+                </div>
+                <div className="chat-messages" ref={this.messagesRef}>
+                    {this.history.map((message, index) => (
+                        <MessageComponentBox key={index} message={message} index={index} onFollowUpClick={this.handleFollowUpClick} onCitationClick={this.handleCitationClick} updateMessageCitations={this.updateMessageCitations} />
+                    ))}
+                    {this.current_message && (
+                        <MessageComponentBox
+                            key={this.history.length}
+                            message={this.current_message}
+                            index={this.history.length}
+                            onFollowUpClick={this.handleFollowUpClick}
+                            onCitationClick={this.handleCitationClick}
+                            updateMessageCitations={this.updateMessageCitations}
+                        />
+                    )}
+                </div>
+                <form onSubmit={this.askGPT} className="chat-input">
+                    <input type="text" name="messageInput" autoComplete="off" placeholder="Type your message here..." value={this.inputValue} onChange={e => (this.inputValue = e.target.value)} />
+                    <button className="submit-button" type="submit" disabled={this.isLoading}>
+                        {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>
+                </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: '', chat: '', chat_history: '', chat_thread_id: '', chat_assistant_id: '', chat_vector_store_id: '' },
+});
-- 
cgit v1.2.3-70-g09d2


From ba0520baaa1f84d9fb08d3b2880c68302d28350a Mon Sep 17 00:00:00 2001
From: "A.J. Shulman" <Shulman.aj@gmail.com>
Date: Sat, 7 Sep 2024 13:07:33 -0400
Subject: added clarifying structural info to ReAct prompt (still ~69% shorter
 than previous prompt) and shortened the RAG prompt

---
 .../views/nodes/chatbot/agentsystem/prompts.ts     |  28 +++++-
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    |   7 +-
 src/client/views/nodes/chatbot/tools/RAGTool.ts    | 104 +++++----------------
 3 files changed, 52 insertions(+), 87 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/agentsystem/prompts.ts b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
index 9daabc35f..7000d8634 100644
--- a/src/client/views/nodes/chatbot/agentsystem/prompts.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
@@ -26,12 +26,33 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
         <point>Ensure that **ALL answers follow the answer structure**: grounded text wrapped in <grounded_text> tags with corresponding citations, normal text in <normal_text> tags, and three follow-up questions at the end.</point>
     </critical_points>
 
+    <thought_structure>
+        <thought>
+            <description>
+                Always provide a thought before each action to explain why you are choosing the next step or tool. This helps clarify your reasoning for the action you will take.
+            </description>
+        </thought>
+    </thought_structure>
+
+    <action_input_structure>
+        <action_input>
+            <action_input_description>
+                Always describe what the action will do in the <action_input_description> tag. Be clear about how the tool will process the input and why it is appropriate for this stage.
+            </action_input_description>
+            <inputs>
+                <description>
+                    Provide the actual inputs for the action in the <inputs> tag. Ensure that each input is specific to the tool being used. Inputs should match the expected parameters for the tool (e.g., a search term for the website scraper, document references for RAG).
+                </description>
+            </inputs>
+        </action_input>
+    </action_input_structure>
+
     <answer_structure>
         <answer>
             <grounded_text> - All information derived from tools or user documents must be wrapped in these tags with proper citation.</grounded_text>
-            <normal_text> - Use this tag for text not derived from tools or user documents.</normal_text>
+            <normal_text> - Use this tag for text not derived from tools or user documents. It should only be for narrative-like text or extremely common knowledge information.</normal_text>
             <citations>
-                <citation> - Provide proper citations for each <grounded_text>, referencing the tool or document chunk used.</citation>
+                <citation> - Provide proper citations for each <grounded_text>, referencing the tool or document chunk used. ENSURE THAT THERE IS A CITATION WHOSE INDEX MATCHES FOR EVERY GROUNDED TEXT CITATION INDEX. </citation>
             </citations>
             <follow_up_questions> - Provide exactly three user-perspective follow-up questions.</follow_up_questions>
             <loop_summary> - Summarize the actions and tools used in the conversation.</loop_summary>
@@ -41,7 +62,8 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
     <grounded_text_guidelines>
         <step>**Wrap ALL tool-based information** in <grounded_text> tags and provide citations.</step>
         <step>Use separate <grounded_text> tags for distinct information or when switching to a different tool or document.</step>
-        <step>Ensure that **EVERY** <grounded_text> tag includes a citation index referencing the source of the information.</step>
+        <step>Ensure that **EVERY** <grounded_text> tag includes a citation index aligned with a citation that you provide that references the source of the information.</step>
+        <step>There should be a one-to-one relationship between <grounded_text> tags and citations.</step>
         <step>Over-citing is discouraged—only cite the information that is directly relevant to the user's query.</step>
     </grounded_text_guidelines>
 
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 6dc691798..28bfbeae3 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -51,8 +51,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
     private vectorstore_id: string;
     private vectorstore: Vectorstore;
     private agent: Agent;
-    private _oldWheel: HTMLDivElement | null = null;
-    private messagesRef: React.RefObject;
+    private messagesRef: React.RefObject<HTMLDivElement>;
 
     /**
      * Static method that returns the layout string for the field.
@@ -246,12 +245,12 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
      * @param event The form submission event.
      */
     @action
-    askGPT = async (event: React.FormEvent): Promise => {
+    askGPT = async (event: React.FormEvent): Promise<any> => {
         event.preventDefault();
         this.inputValue = '';
 
         // Extract the user's message
-        const textInput = event.currentTarget.elements.namedItem('messageInput') as HTMLInputElement;
+        const textInput = (event.currentTarget as HTMLFormElement).elements.namedItem('messageInput') as HTMLInputElement;
         const trimmedText = textInput.value.trim();
 
         if (trimmedText) {
diff --git a/src/client/views/nodes/chatbot/tools/RAGTool.ts b/src/client/views/nodes/chatbot/tools/RAGTool.ts
index c24306dcd..f4b7b42ea 100644
--- a/src/client/views/nodes/chatbot/tools/RAGTool.ts
+++ b/src/client/views/nodes/chatbot/tools/RAGTool.ts
@@ -14,114 +14,58 @@ export class RAGTool extends BaseTool {
             {
                 hypothetical_document_chunk: {
                     type: 'string',
-                    description:
-                        "Detailed version of the prompt that is effectively a hypothetical document chunk that would be ideal to embed and compare to the vectors of real document chunks to fetch the most relevant document chunks to answer the user's query",
+                    description: "A detailed prompt representing an ideal chunk to embed and compare against document vectors to retrieve the most relevant content for answering the user's query.",
                     required: 'true',
                 },
             },
             `
-            Your task is to provide a comprehensive response to the user's prompt based on the given chunks and chat history. Follow these structural guidelines meticulously:
+            When using the RAG tool, the structure must adhere to the format described in the ReAct prompt. Below are additional guidelines specifically for RAG-based responses:
 
-            1. Overall Structure:
-            <answer>
-                [Main content with grounded_text tags interspersed with normal plain text (information that is not derived from chunks' information)]
-                <citations>
-                [Individual citation tags]
-                </citations>
-                <follow_up_questions>
-                [Three question tags]
-                </follow_up_questions>
-            </answer>
-
-            2. Grounded Text Tag Structure:
-            - Basic format: 
-                <grounded_text citation_index="[citation index number(s)]">
-                [Your generated text based on information from a subset of a chunk (a citation's direct text)]
-                </grounded_text>
+            1. **Grounded Text Guidelines**:
+                - Each <grounded_text> tag must correspond to exactly one citation, ensuring a one-to-one relationship.
+                - Always cite a **subset** of the chunk, never the full text. The citation should be as short as possible while providing the relevant information (typically one to two sentences).
+                - Do not paraphrase the chunk text in the citation; use the original subset directly from the chunk.
+                - If multiple citations are needed for different sections of the response, create new <grounded_text> tags for each.
 
-            3. Citation Tag Structure:
-            <citation index="[unique number]" chunk_id="[UUID v4]" type="[text/image/table]">
-                [For text: relevant subset of original chunk]
-                [For image/table: leave empty]
-            </citation>
+            2. **Citation Guidelines**:
+                - The citation must include only the relevant excerpt from the chunk being referenced.
+                - Use unique citation indices and reference the chunk_id for the source of the information.
+                - For text chunks, the citation content must reflect the **exact subset** of the original chunk that is relevant to the grounded_text tag.
 
-            4. Detailed Grounded Text Guidelines:
-                a. Wrap all information derived from chunks in grounded_text tags.
-                b. DO NOT PUT ANYTHING THAT IS NOT DIRECTLY DERIVED FROM INFORMATION FROM CHUNKS (EITHER IMAGE, TABLE, OR TEXT) IN GROUNDED_TEXT TAGS.
-                c. Use a single grounded_text tag for suquential and closely related information that references the same citation. If other citations' information are used sequentially, create new grounded_text tags.
-                d. Ensure every grounded_text tag has up to a few corresponding citations (should not be more than 3 and only 1 is fine). Multiple citation indices should be separated by commas.
-                e. Grounded text can be as short as a few words or as long as several sentences.
-                f. Avoid overlapping or nesting grounded_text tags; instead, use sequential tags.
-
-            5. Detailed Citation Guidelines:
-                a. Create a unique citation for each distinct piece of information from the chunks that is used to support grounded_text.
-                b. ALL TEXT CITATIONS must have direct text in its element content (e.g. <citation ...>DIRECT TEXT HERE</citation>) that is a relevant SUBSET of the original text chunk that is being cited specifically.
-                c. DO NOT paraphrase or summarize the text; use the original text as much as possible.
-                d. DO NOT USE THE FULL TEXT CHUNK as the citation content; only use the relevant subset of the text that the grounded_text is base. AS SHORT AS POSSIBLE WHILE PROVIDING INFORMATION (ONE TO TWO SENTENCES USUALLY)!
-                e. Ensure each citation has a unique index number.
-                f. Specify the correct type: "text", "image", or "table".
-                g. For text chunks, the content of the citation should ALWAYS have the relevant subset of the original text that the grounded_text is based on.
-                h. For image/table chunks, leave the citation content empty.
-                i. One citation can be used for multiple grounded_text tags if they are based on the same chunk information.
-                j. !!!DO NOT OVERCITE - only include citations for information that is directly relevant to the grounded_text.
-
-            6. Structural Integrity Checks:
-                a. Ensure all opening tags have corresponding closing tags.
-                b. Verify that all grounded_text tags have valid citation_index attributes (they should be equal to the associated citation(s) index field—not their chunk_id field).
-                c. Check that all cited indices in grounded_text tags have corresponding citations.
-
-            Example of grounded_text usage:
+            **Example**:
 
             <answer>
-                <grounded_text citation_index="1,2">
-                Artificial Intelligence (AI) is revolutionizing various sectors, with healthcare experiencing significant transformations in areas such as diagnosis and treatment planning.
-                </grounded_text>
-                <grounded_text citation_index="2,3,4">
-                In the field of medical diagnosis, AI has shown remarkable capabilities, particularly in radiology. For instance, AI systems have drastically improved mammogram analysis, achieving 99% accuracy at a rate 30 times faster than human radiologists.
+                <grounded_text citation_index="1">
+                    Artificial Intelligence is revolutionizing various sectors, with healthcare seeing transformations in diagnosis and treatment planning.
                 </grounded_text>
-                <grounded_text citation_index="4">
-                This advancement not only enhances the efficiency of healthcare systems but also significantly reduces the occurrence of false positives, leading to fewer unnecessary biopsies and reduced patient stress.
+                <grounded_text citation_index="2">
+                    Based on recent data, AI has drastically improved mammogram analysis, achieving 99% accuracy at a rate 30 times faster than human radiologists.
                 </grounded_text>
-            
-                <grounded_text citation_index="5,6">
-                Beyond diagnosis, AI is playing a crucial role in drug discovery and development. By analyzing vast amounts of genetic and molecular data, AI algorithms can identify potential drug candidates much faster than traditional methods.
-                </grounded_text>
-                <grounded_text citation_index="6">
-                    This could potentially reduce the time and cost of bringing new medications to market, especially for rare diseases that have historically received less attention due to limited market potential.
-                </grounded_text>
-
-                [... rest of the content ...]
 
                 <citations>
-                    <citation index="1" chunk_id="123e4567-e89b-12d3-a456-426614174000" type="text">Artificial Intelligence is revolutionizing various industries, with healthcare being one of the most profoundly affected sectors.</citation>
-                    <citation index="2" chunk_id="123e4567-e89b-12d3-a456-426614174001" type="text">AI has shown particular promise in the field of radiology, enhancing the accuracy and speed of image analysis.</citation>
-                    <citation index="3" chunk_id="123e4567-e89b-12d3-a456-426614174002" type="text">According to recent studies, AI systems have achieved 99% accuracy in mammogram analysis, performing the task 30 times faster than human radiologists.</citation>
-                    <citation index="4" chunk_id="123e4567-e89b-12d3-a456-426614174003" type="text">The improvement in mammogram accuracy has led to a significant reduction in false positives, decreasing the need for unnecessary biopsies and reducing patient anxiety.</citation>
-                    <citation index="5" chunk_id="123e4567-e89b-12d3-a456-426614174004" type="text">AI is accelerating the drug discovery process by analyzing complex molecular and genetic data to identify potential drug candidates.</citation>
-                    <citation index="6" chunk_id="123e4567-e89b-12d3-a456-426614174005" type="text">The use of AI in drug discovery could significantly reduce the time and cost associated with bringing new medications to market, particularly for rare diseases.</citation>
+                    <citation index="1" chunk_id="abc123" type="text">Artificial Intelligence is revolutionizing various industries, especially in healthcare.</citation>
+                    <citation index="2" chunk_id="abc124" type="table"></citation>
                 </citations>
 
                 <follow_up_questions>
-                    <question>How might AI-driven personalized medicine impact the cost and accessibility of healthcare in the future?</question>
-                    <question>What measures can be taken to ensure that AI systems in healthcare are free from biases and equally effective for diverse populations?</question>
-                    <question>How could the role of healthcare professionals evolve as AI becomes more integrated into medical practices?</question>
+                    <question>How can AI enhance patient outcomes in fields outside radiology?</question>
+                    <question>What are the challenges in implementing AI systems across different hospitals?</question>
+                    <question>How might AI-driven advancements impact healthcare costs?</question>
                 </follow_up_questions>
             </answer>
             `,
 
-            `Performs a RAG (Retrieval-Augmented Generation) search on user documents and returns a 
-            set of document chunks (either images or text) that can be used to provide a grounded response based on 
-            user documents`
+            `Performs a RAG (Retrieval-Augmented Generation) search on user documents and returns a set of document chunks (text or images) to provide a grounded response based on user documents.`
         );
     }
 
-    async execute(args: { hypothetical_document_chunk: string }): Promise {
+    async execute(args: { hypothetical_document_chunk: string }): Promise<any> {
         const relevantChunks = await this.vectorstore.retrieve(args.hypothetical_document_chunk);
         const formatted_chunks = await this.getFormattedChunks(relevantChunks);
         return formatted_chunks;
     }
 
-    async getFormattedChunks(relevantChunks: RAGChunk[]): Promise {
+    async getFormattedChunks(relevantChunks: RAGChunk[]): Promise<any> {
         try {
             const { formattedChunks } = await Networking.PostToServer('/formatChunks', { relevantChunks });
 
-- 
cgit v1.2.3-70-g09d2


From 00b12111c4007ef6ecff645327007a67f6655d8b Mon Sep 17 00:00:00 2001
From: "A.J. Shulman" <Shulman.aj@gmail.com>
Date: Thu, 19 Sep 2024 09:53:19 -0400
Subject: added comments and fixed some styling and error handling

---
 .../views/nodes/chatbot/agentsystem/prompts.ts     |  12 +-
 .../nodes/chatbot/chatboxcomponents/ChatBox.scss   |   6 +-
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    |  11 +-
 src/server/ApiManagers/AssistantManager.ts         | 224 +++++++++++++++------
 4 files changed, 179 insertions(+), 74 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/agentsystem/prompts.ts b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
index 7000d8634..01c30d444 100644
--- a/src/client/views/nodes/chatbot/agentsystem/prompts.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
@@ -22,8 +22,9 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
         <point>**STRUCTURE**: Always use the correct stage tags (e.g., <stage number="2" role="assistant">) for every response. Use only even-numbered stages for your responses.</point>
         <point>**STOP after every stage and wait for input. Do not combine multiple stages in one response.**</point>
         <point>If a tool is needed, select the most appropriate tool based on the query.</point>
-        <point>**If one tool does not yield satisfactory results or fails twice, try another tool that might work better for the query.**</point>
+        <point>**If one tool does not yield satisfactory results or fails twice, try another tool that might work better for the query.** This often happens with the rag tool, which may not yeild great results. If this happens, try the search tool.</point>
         <point>Ensure that **ALL answers follow the answer structure**: grounded text wrapped in <grounded_text> tags with corresponding citations, normal text in <normal_text> tags, and three follow-up questions at the end.</point>
+        <point>If you use a tool that will do something (i.e. creating a CSV), and want to also use a tool that will provide you with information (i.e. RAG), use the tool that will provide you with information first. Then proceed with the tool that will do something.</point>
     </critical_points>
 
     <thought_structure>
@@ -48,8 +49,9 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
     </action_input_structure>
 
     <answer_structure>
+        ALL answers must follow this structure and everything must be witin the <answer> tag:
         <answer>
-            <grounded_text> - All information derived from tools or user documents must be wrapped in these tags with proper citation.</grounded_text>
+            <grounded_text> - All information derived from tools or user documents must be wrapped in these tags with proper citation. This should not be word for word, but paraphrased from the text.</grounded_text>
             <normal_text> - Use this tag for text not derived from tools or user documents. It should only be for narrative-like text or extremely common knowledge information.</normal_text>
             <citations>
                 <citation> - Provide proper citations for each <grounded_text>, referencing the tool or document chunk used. ENSURE THAT THERE IS A CITATION WHOSE INDEX MATCHES FOR EVERY GROUNDED TEXT CITATION INDEX. </citation>
@@ -65,6 +67,10 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
         <step>Ensure that **EVERY** <grounded_text> tag includes a citation index aligned with a citation that you provide that references the source of the information.</step>
         <step>There should be a one-to-one relationship between <grounded_text> tags and citations.</step>
         <step>Over-citing is discouraged—only cite the information that is directly relevant to the user's query.</step>
+        <step>Paraphrase the information in the <grounded_text> tags, but ensure that the meaning is preserved.</step>
+        <step>Do not include the full text of the chunk in the citation—only the relevant excerpt.</step>
+        <step>For text chunks, the citation content must reflect the exact subset of the original chunk that is relevant to the grounded_text tag.</step>
+        <step>Do not use citations from previous interactions. Only use citations from the current action loop.</step>
     </grounded_text_guidelines>
 
     <normal_text_guidelines>
@@ -130,7 +136,7 @@ export function getReactPrompt(tools: Tool[], summaries: () => string, chatHisto
 
             <stage number="6" role="assistant">
                 <thought>
-                    With key moments from the World Cup retrieved, I will now use the website scraper tool to gather data on Qatar’s tourism impact during the World Cup.
+                    With key moments from the World Cup retrieved, I will now use the website scraper tool to gather data on Qatar's tourism impact during the World Cup.
                 </thought>
                 <action>websiteInfoScraper</action>
             </stage>
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.scss b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.scss
index 42f6a0d61..50111f678 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.scss
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.scss
@@ -93,6 +93,7 @@ $transition: all 0.3s ease;
             display: flex;
             align-items: center;
             justify-content: center;
+            position: relative;
 
             &:hover {
                 background-color: darken($primary-color, 10%);
@@ -109,10 +110,7 @@ $transition: all 0.3s ease;
                 border: 3px solid rgba(255, 255, 255, 0.3);
                 border-top: 3px solid #fff;
                 border-radius: 50%;
-                animation: spin 1s linear infinite;
-                display: flex;
-                align-items: center;
-                justify-content: center;
+                animation: spin 2s linear infinite;
             }
         }
     }
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 28bfbeae3..d4deff78b 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -23,6 +23,8 @@ import { ClientUtils } from '../../../../../ClientUtils';
 import { ProgressBar } from './ProgressBar';
 import { DocumentView } from '../../DocumentView';
 import { Networking } from '../../../../Network';
+import { PDFViewer } from '../../../pdf/PDFViewer';
+import { PDFBox } from '../../PDFBox';
 
 dotenv.config();
 
@@ -225,9 +227,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
      * 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;
-        }
+        // if (this.messagesRef.current) {
+        //     this.messagesRef.current.scrollTop = this.messagesRef.current.scrollHeight;
+        // }
     };
 
     /**
@@ -445,7 +447,8 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
 
                             DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {
                                 const firstView = Array.from(doc[DocViews])[0] as DocumentView;
-                                firstView.ComponentView?.search?.(citation.direct_text ?? '');
+                                (firstView.ComponentView as PDFBox)?.gotoPage?.(foundChunk.startPage);
+                                (firstView.ComponentView as PDFBox)?.search?.(citation.direct_text ?? '');
                             });
                             break;
                         case CHUNK_TYPE.URL:
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts
index 88a81c5de..063ba193a 100644
--- a/src/server/ApiManagers/AssistantManager.ts
+++ b/src/server/ApiManagers/AssistantManager.ts
@@ -19,6 +19,7 @@ import * as puppeteer from 'puppeteer';
 import { JSDOM } from 'jsdom';
 import { Readability } from '@mozilla/readability';
 
+// Enumeration of directories where different file types are stored
 export enum Directory {
     parsed_files = 'parsed_files',
     images = 'images',
@@ -32,41 +33,76 @@ export enum Directory {
     scrape_images = 'scrape_images',
 }
 
+/**
+ * Constructs a normalized path to a file in the server's file system.
+ * @param directory The directory where the file is stored.
+ * @param filename The name of the file.
+ * @returns The full normalized path to the file.
+ */
 export function serverPathToFile(directory: Directory, filename: string) {
     return path.normalize(`${filesDirectory}/${directory}/${filename}`);
 }
 
+/**
+ * Constructs a normalized path to a directory in the server's file system.
+ * @param directory The directory to access.
+ * @returns The full normalized path to the directory.
+ */
 export function pathToDirectory(directory: Directory) {
     return path.normalize(`${filesDirectory}/${directory}`);
 }
 
+/**
+ * Constructs the client-accessible URL for a file.
+ * @param directory The directory where the file is stored.
+ * @param filename The name of the file.
+ * @returns The URL path to the file.
+ */
 export function clientPathToFile(directory: Directory, filename: string) {
     return `/files/${directory}/${filename}`;
 }
 
+// Promisified versions of filesystem functions
 const writeFileAsync = promisify(writeFile);
 const readFileAsync = promisify(fs.readFile);
 
+/**
+ * Class responsible for handling various API routes related to the Assistant functionality.
+ * This class extends `ApiManager` and handles registration of routes and secure request handlers.
+ */
 export default class AssistantManager extends ApiManager {
+    /**
+     * Registers all API routes and initializes necessary services like OpenAI, Scrapfly, and UnstructuredClient.
+     * @param register The registration method to register routes and handlers.
+     */
     protected initialize(register: Registration): void {
+        // Initialize OpenAI API with client key
         const openai = new OpenAI({
-            apiKey: process.env._CLIENT_OPENAI_KEY, // Use client key so don't have to set key seperately for client and server.
+            apiKey: process.env._CLIENT_OPENAI_KEY,
             dangerouslyAllowBrowser: true,
         });
+
+        // Initialize UnstructuredClient for document processing
         const unstructuredClient = new UnstructuredClient({
             security: {
                 apiKeyAuth: process.env._CLIENT_UNSTRUCTURED_API_KEY!,
             },
         });
+
+        // Initialize ScrapflyClient for scraping purposes
         const scrapflyClient = new ScrapflyClient({ key: process.env._CLIENT_SCRAPFLY_API_KEY! });
+
+        // Initialize Google Custom Search API
         const customsearch = google.customsearch('v1');
 
+        // Register Wikipedia summary API route
         register({
             method: Method.POST,
             subscription: '/getWikipediaSummary',
             secureHandler: async ({ req, res }) => {
                 const { title } = req.body;
                 try {
+                    // Fetch summary from Wikipedia using axios
                     const response = await axios.get('https://en.wikipedia.org/w/api.php', {
                         params: {
                             action: 'query',
@@ -75,25 +111,26 @@ export default class AssistantManager extends ApiManager {
                             format: 'json',
                         },
                     });
-                    const summary = response.data.query.search[0].snippet;
-                    if (!summary || summary.length === 0 || summary === '' || summary === ' ') {
-                        res.send({ text: 'No article found with that title.' });
-                    } else {
-                        res.send({ text: summary });
-                    }
+                    const summary = response.data.query.search[0]?.snippet || 'No article found with that title.';
+                    res.send({ text: summary });
                 } catch (error: any) {
-                    console.error('Error retrieving article summary from Wikipedia:', error);
-                    res.status(500).send({ error: 'Error retrieving article summary from Wikipedia.', details: error.message });
+                    console.error('Error retrieving Wikipedia summary:', error);
+                    res.status(500).send({
+                        error: 'Error retrieving article summary from Wikipedia.',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Register Google Web Search Results API route
         register({
             method: Method.POST,
             subscription: '/getWebSearchResults',
             secureHandler: async ({ req, res }) => {
                 const { query, max_results } = req.body;
                 try {
+                    // Fetch search results using Google Custom Search API
                     const response = await customsearch.cse.list({
                         q: query,
                         cx: process.env._CLIENT_GOOGLE_SEARCH_ENGINE_ID,
@@ -111,25 +148,40 @@ export default class AssistantManager extends ApiManager {
                     res.send({ results });
                 } catch (error: any) {
                     console.error('Error performing web search:', error);
-                    res.status(500).send({ error: 'Failed to perform web search', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to perform web search',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Axios instance with custom headers for scraping
         const axiosInstance = axios.create({
             headers: {
                 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
             },
         });
 
+        /**
+         * Utility function to introduce delay (used for retries).
+         * @param ms Delay in milliseconds.
+         */
         const delay = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
 
+        /**
+         * Function to fetch a URL with retry logic, handling rate limits.
+         * Retries a request if it fails due to rate limits (HTTP status 429).
+         * @param url The URL to fetch.
+         * @param retries The number of retry attempts.
+         * @param backoff Initial backoff time in milliseconds.
+         */
         const fetchWithRetry = async (url: string, retries = 3, backoff = 300) => {
             try {
                 const response = await axiosInstance.get(url);
                 return response.data;
             } catch (error: any) {
-                if (retries > 0 && error.response && error.response.status === 429) {
+                if (retries > 0 && error.response?.status === 429) {
                     console.log(`Rate limited. Retrying in ${backoff}ms...`);
                     await delay(backoff);
                     return fetchWithRetry(url, retries - 1, backoff * 2);
@@ -138,6 +190,7 @@ export default class AssistantManager extends ApiManager {
             }
         };
 
+        // Register a proxy fetch API route
         register({
             method: Method.POST,
             subscription: '/proxyFetch',
@@ -154,18 +207,22 @@ export default class AssistantManager extends ApiManager {
                     res.send({ data });
                 } catch (error: any) {
                     console.error('Error fetching the URL:', error);
-                    res.status(500).send({ error: 'Failed to fetch the URL', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to fetch the URL',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Register an API route to scrape website content using Puppeteer and JSDOM
         register({
             method: Method.POST,
             subscription: '/scrapeWebsite',
             secureHandler: async ({ req, res }) => {
                 const { url } = req.body;
                 try {
-                    // Launch Puppeteer to navigate to the webpage
+                    // Launch Puppeteer browser to navigate to the webpage
                     const browser = await puppeteer.launch({
                         args: ['--no-sandbox', '--disable-setuid-sandbox'],
                     });
@@ -173,45 +230,47 @@ export default class AssistantManager extends ApiManager {
                     await page.setUserAgent('Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36');
                     await page.goto(url, { waitUntil: 'networkidle2' });
 
-                    // Get the HTML content of the page
+                    // Extract HTML content
                     const htmlContent = await page.content();
                     await browser.close();
 
-                    // Use JSDOM to parse the HTML content
+                    // Parse HTML content using JSDOM
                     const dom = new JSDOM(htmlContent, { url });
 
-                    // Use Readability to extract the readable content
+                    // Extract readable content using Mozilla's Readability API
                     const reader = new Readability(dom.window.document);
                     const article = reader.parse();
 
                     if (article) {
-                        // Extract the plain text from the article content
                         const plainText = article.textContent;
-
-                        // Return the plain text content
                         res.send({ website_plain_text: plainText });
                     } else {
                         res.status(500).send({ error: 'Failed to extract readable content' });
                     }
                 } catch (error: any) {
                     console.error('Error scraping website:', error);
-                    res.status(500).send({ error: 'Failed to scrape website', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to scrape website',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Register an API route to create documents by sending files to a chatbot
         register({
             method: Method.POST,
             subscription: '/createDocument',
             secureHandler: async ({ req, res }) => {
                 const { file_path } = req.body;
-                const public_path = path.join(publicDirectory, file_path);
-                const file_name = path.basename(file_path);
+                const public_path = path.join(publicDirectory, file_path); // Resolve the file path in the public directory
+                const file_name = path.basename(file_path); // Extract the file name from the path
 
                 try {
-                    // Read file data and convert to base64
+                    // Read the file data and encode it as base64
                     const file_data: string = fs.readFileSync(public_path, { encoding: 'base64' });
 
+                    // Send the file data to a local chatbot API for document creation
                     const response = await axios.post(
                         'http://localhost:8080/createDocument',
                         {
@@ -225,65 +284,79 @@ export default class AssistantManager extends ApiManager {
                         }
                     );
 
+                    // Retrieve the job ID from the response
                     const jobId = response.data['job_id'];
                     console.log('Job ID:', jobId);
 
+                    // Send the job ID back to the client
                     res.send({ jobId });
                 } catch (error: any) {
                     console.error('Error communicating with chatbot:', error);
-                    res.status(500).send({ error: 'Failed to communicate with the chatbot', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to communicate with the chatbot',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Register an API route to check the progress of a document creation job
         register({
             method: Method.GET,
             subscription: '/getProgress/:jobId',
             secureHandler: async ({ req, res }) => {
-                const { jobId } = req.params;
+                const { jobId } = req.params; // Get the job ID from the URL parameters
                 try {
+                    // Query the local API to get the progress of the job
                     const progressResponse = await axios.get(`http://localhost:8080/getProgress/${jobId}`);
                     console.log(`Current step: ${progressResponse.data.step}, Progress within step: ${progressResponse.data.progress}%`);
-                    res.json(progressResponse.data);
+                    res.json(progressResponse.data); // Send the progress data back to the client
                 } catch (error) {
                     console.error('Error getting progress:', error);
-                    res.status(500).send({ error: 'Failed to get progress', details: JSON.parse(error as string).message });
+                    res.status(500).send({
+                        error: 'Failed to get progress',
+                        details: error,
+                    });
                 }
             },
         });
 
+        // Register an API route to get the final result of a document creation job
         register({
             method: Method.GET,
             subscription: '/getResult/:jobId',
             secureHandler: async ({ req, res }) => {
-                const { jobId } = req.params;
+                const { jobId } = req.params; // Get the job ID from the URL parameters
                 try {
+                    // Query the local API to get the final result of the job
                     const finalResponse = await axios.get(`http://localhost:8080/getResult/${jobId}`);
                     console.log('Result:', finalResponse.data);
                     const result = finalResponse.data;
 
+                    // If the result contains image or table chunks, save the base64 data as image files
                     if (result.chunks && Array.isArray(result.chunks)) {
                         for (const chunk of result.chunks) {
                             if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) {
                                 let files_directory = '/files/chunk_images/';
                                 const directory = path.join(publicDirectory, files_directory);
 
+                                // Ensure the directory exists or create it
                                 if (!fs.existsSync(directory)) {
                                     fs.mkdirSync(directory);
                                 }
 
-                                const fileName = path.basename(chunk.metadata.file_path);
-                                const filePath = path.join(directory, fileName);
+                                const fileName = path.basename(chunk.metadata.file_path); // Get the file name from the path
+                                const filePath = path.join(directory, fileName); // Create the full file path
 
-                                // Check if base64_data exists
+                                // Check if the chunk contains base64 encoded data
                                 if (chunk.metadata.base64_data) {
-                                    // Decode Base64 and save as file
+                                    // Decode the base64 data and write it to a file
                                     const buffer = Buffer.from(chunk.metadata.base64_data, 'base64');
                                     await fs.promises.writeFile(filePath, buffer);
 
-                                    // Update the file path in the chunk
+                                    // Update the file path in the chunk's metadata
                                     chunk.metadata.file_path = path.join(files_directory, fileName);
-                                    chunk.metadata.base64_data = undefined;
+                                    chunk.metadata.base64_data = undefined; // Remove the base64 data from the metadata
                                 } else {
                                     console.warn(`No base64_data found for chunk: ${fileName}`);
                                 }
@@ -294,32 +367,42 @@ export default class AssistantManager extends ApiManager {
                         console.warn('Not ready');
                         result.status = 'pending';
                     }
-                    res.json(result);
+                    res.json(result); // Send the result back to the client
                 } catch (error) {
-                    console.error('Error getting progress:', error);
-                    res.status(500).send({ error: 'Failed to get progress', details: error });
+                    console.error('Error getting result:', error);
+                    res.status(500).send({
+                        error: 'Failed to get result',
+                        details: error,
+                    });
                 }
             },
         });
 
+        // Register an API route to format chunks (e.g., text or image chunks) for display
         register({
             method: Method.POST,
             subscription: '/formatChunks',
             secureHandler: async ({ req, res }) => {
-                const { relevantChunks } = req.body;
+                const { relevantChunks } = req.body; // Get the relevant chunks from the request body
+
+                // Initialize an array to hold the formatted content
                 const content: { type: string; text?: string; image_url?: { url: string } }[] = [{ type: 'text', text: '<chunks>' }];
 
                 for (const chunk of relevantChunks) {
+                    // Format each chunk by adding its metadata and content
                     content.push({
                         type: 'text',
-                        text: `<chunk chunk_id=${chunk.id} chunk_type=${chunk.metadata.type}>`,
+                        text: `<chunk chunk_id=${chunk.id} chunk_type="${chunk.metadata.type}">`,
                     });
 
+                    // If the chunk is an image or table, read the corresponding file and encode it as base64
                     if (chunk.metadata.type === 'image' || chunk.metadata.type === 'table') {
                         try {
-                            const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path);
-                            const imageBuffer = await readFileAsync(filePath);
-                            const base64Image = imageBuffer.toString('base64');
+                            const filePath = serverPathToFile(Directory.chunk_images, chunk.metadata.file_path); // Get the file path
+                            const imageBuffer = await readFileAsync(filePath); // Read the image file
+                            const base64Image = imageBuffer.toString('base64'); // Convert the image to base64
+
+                            // Add the base64-encoded image to the content array
                             if (base64Image) {
                                 content.push({
                                     type: 'image_url',
@@ -334,33 +417,35 @@ export default class AssistantManager extends ApiManager {
                             console.error(`Error reading image file for chunk ${chunk.id}:`, error);
                         }
                     }
+
+                    // Add the chunk's text content to the formatted content
                     content.push({ type: 'text', text: `${chunk.metadata.text}\n</chunk>\n` });
                 }
 
                 content.push({ type: 'text', text: '</chunks>' });
 
+                // Send the formatted content back to the client
                 res.send({ formattedChunks: content });
             },
         });
 
+        // Register an API route to create and save a CSV file on the server
         register({
             method: Method.POST,
             subscription: '/createCSV',
             secureHandler: async ({ req, res }) => {
                 const { filename, data } = req.body;
 
-                // Validate input
+                // Validate that both the filename and data are provided
                 if (!filename || !data) {
                     res.status(400).send({ error: 'Filename and data fields are required.' });
                     return;
                 }
 
                 try {
-                    // Generate a UUID for the file
+                    // Generate a UUID for the file to ensure unique naming
                     const uuidv4 = uuid.v4();
-
-                    // Construct the full filename with the UUID prefix
-                    const fullFilename = `${uuidv4}-${filename}`;
+                    const fullFilename = `${uuidv4}-${filename}`; // Prefix the file name with the UUID
 
                     // Get the full server path where the file will be saved
                     const serverFilePath = serverPathToFile(Directory.csv, fullFilename);
@@ -368,60 +453,73 @@ export default class AssistantManager extends ApiManager {
                     // Write the CSV data (which is a raw string) to the file
                     await writeFileAsync(serverFilePath, data, 'utf8');
 
-                    // Construct the full client URL for accessing the file
+                    // Construct the client-accessible URL for the file
                     const fileUrl = clientPathToFile(Directory.csv, fullFilename);
 
-                    // Return the file URL and UUID to the client
+                    // Send the file URL and UUID back to the client
                     res.send({ fileUrl, id: uuidv4 });
                 } catch (error: any) {
                     console.error('Error creating CSV file:', error);
-                    res.status(500).send({ error: 'Failed to create CSV file.', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to create CSV file.',
+                        details: error.message,
+                    });
                 }
             },
         });
 
+        // Register an API route to chunk a document using the UnstructuredClient
         register({
             method: Method.POST,
             subscription: '/chunkDocument',
             secureHandler: async ({ req, res }) => {
-                const { file_path } = req.body;
-                const public_path = path.join(publicDirectory, file_path);
-                const file_name = path.basename(file_path);
+                const { file_path } = req.body; // Get the file path from the request body
+                const public_path = path.join(publicDirectory, file_path); // Resolve the full path in the public directory
+                const file_name = path.basename(file_path); // Extract the file name from the path
 
                 try {
-                    // Read file data and convert to base64
+                    // Read the file content as a Buffer
                     const file_data = await fs.promises.readFile(public_path);
 
                     try {
+                        // Use UnstructuredClient to partition the document into chunks
                         const result = await unstructuredClient.general.partition({
                             partitionParameters: {
                                 files: {
                                     content: file_data,
                                     fileName: file_name,
                                 },
-                                strategy: Strategy.Auto,
-                                chunkingStrategy: ChunkingStrategy.ByTitle,
-                                extractImageBlockTypes: ['Image', 'Table'],
+                                strategy: Strategy.Auto, // Automatically determine the chunking strategy
+                                chunkingStrategy: ChunkingStrategy.ByTitle, // Chunk by title
+                                extractImageBlockTypes: ['Image', 'Table'], // Extract images and tables
                             },
                         });
 
                         if (result.statusCode === 200) {
                             console.log(result.elements);
                             const jsonElements = JSON.stringify(result.elements, null, 2);
-                            // Print the processed data.
-                            console.log(jsonElements);
-                            res.send({ document_json: jsonElements });
+                            console.log(jsonElements); // Log the JSON result of the partitioned elements
+                            res.send({ document_json: jsonElements }); // Send the partitioned data as a JSON response
                         } else {
                             console.error(`Unexpected status code: ${result.statusCode}`);
-                            res.status(result.statusCode).send({ error: 'Failed to process the document', details: result });
+                            res.status(result.statusCode).send({
+                                error: 'Failed to process the document',
+                                details: result,
+                            });
                         }
                     } catch (e: any) {
                         console.error('Error during partitioning:', e);
-                        res.status(500).send({ error: 'Failed to partition the document', details: e.message });
+                        res.status(500).send({
+                            error: 'Failed to partition the document',
+                            details: e.message,
+                        });
                     }
                 } catch (error: any) {
                     console.error('Error reading file:', error);
-                    res.status(500).send({ error: 'Failed to read the file', details: error.message });
+                    res.status(500).send({
+                        error: 'Failed to read the file',
+                        details: error.message,
+                    });
                 }
             },
         });
-- 
cgit v1.2.3-70-g09d2


From 139a3cb0b3b081c270187e9b4ca281d04ca923bf Mon Sep 17 00:00:00 2001
From: bobzel <zzzman@gmail.com>
Date: Mon, 30 Sep 2024 12:19:22 -0400
Subject: upate AJ assistant from master and fix some lint errors

---
 eslint.config.mjs                                  |   2 +-
 src/client/documents/Documents.ts                  |   9 --
 src/client/util/CurrentUserUtils.ts                |   8 +-
 src/client/util/Scripting.ts                       |  13 +-
 src/client/views/MainView.tsx                      |   7 +-
 .../collections/CollectionNoteTakingViewColumn.tsx |   5 +-
 src/client/views/nodes/PDFBox.tsx                  |  21 ++-
 .../views/nodes/RecordingBox/ProgressBar.tsx       |   2 -
 .../views/nodes/chatbot/agentsystem/Agent.ts       |  33 ++--
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    |  61 +++----
 .../nodes/chatbot/response_parsers/AnswerParser.ts |   9 +-
 .../response_parsers/StreamedAnswerParser.ts       |   2 -
 src/client/views/nodes/chatbot/tools/BaseTool.ts   |   8 +-
 .../views/nodes/chatbot/tools/CalculateTool.ts     |   2 +-
 .../views/nodes/chatbot/tools/CreateCSVTool.ts     |   2 +-
 .../nodes/chatbot/tools/CreateCollectionTool.ts    |   2 +-
 .../views/nodes/chatbot/tools/DataAnalysisTool.ts  |   2 +-
 .../views/nodes/chatbot/tools/GetDocsTool.ts       |   2 +-
 src/client/views/nodes/chatbot/tools/NoTool.ts     |   5 +-
 src/client/views/nodes/chatbot/tools/RAGTool.ts    |  13 +-
 src/client/views/nodes/chatbot/tools/SearchTool.ts |   5 +-
 .../nodes/chatbot/tools/WebsiteInfoScraperTool.ts  |   5 +-
 .../views/nodes/chatbot/tools/WikipediaTool.ts     |   7 +-
 src/client/views/nodes/chatbot/types/types.ts      |  25 ++-
 .../views/nodes/chatbot/vectorstore/Vectorstore.ts |  13 +-
 src/client/views/pdf/PDFViewer.tsx                 |  46 ++----
 src/server/ApiManagers/AssistantManager.ts         | 178 ++++++++++-----------
 27 files changed, 215 insertions(+), 272 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/eslint.config.mjs b/eslint.config.mjs
index aebdc20d0..f7063caa5 100644
--- a/eslint.config.mjs
+++ b/eslint.config.mjs
@@ -48,7 +48,7 @@ export default [
             'no-return-assign': 'error',
             'no-await-in-loop': 'error',
             'no-loop-func': 'error',
-            '@typescript-eslint/no-cond-assign': 'error',
+            'no-cond-assign': 'error',
             'no-use-before-define': 'error',
             '@typescript-eslint/no-explicit-any': 'error',
             'no-restricted-globals': ['error', 'event'],
diff --git a/src/client/documents/Documents.ts b/src/client/documents/Documents.ts
index e0a9918f4..d77f76b81 100644
--- a/src/client/documents/Documents.ts
+++ b/src/client/documents/Documents.ts
@@ -1,5 +1,3 @@
-/* eslint-disable prefer-destructuring */
-/* eslint-disable default-param-last */
 /* eslint-disable no-use-before-define */
 import { reaction } from 'mobx';
 import { basename } from 'path';
@@ -671,7 +669,6 @@ export namespace Docs {
          * only when creating a DockDocument from the current user's already existing
          * main document.
          */
-        // eslint-disable-next-line default-param-last
         function InstanceFromProto(proto: Doc, data: FieldType | undefined, options: DocumentOptions, delegId?: string, fieldKey: string = 'data', protoId?: string, placeholderDocIn?: Doc, noView?: boolean) {
             const placeholderDoc = placeholderDocIn;
             const viewKeys = ['x', 'y', 'isSystem']; // keys that should be addded to the view document even though they don't begin with an "_"
@@ -732,7 +729,6 @@ export namespace Docs {
             return dataDoc;
         }
 
-        // eslint-disable-next-line default-param-last
         export function ImageDocument(url: string | ImageField, options: DocumentOptions = {}, overwriteDoc?: Doc) {
             const imgField = url instanceof ImageField ? url : url ? new ImageField(url) : undefined;
             return InstanceFromProto(Prototypes.get(DocumentType.IMG), imgField, { title: basename(imgField?.url.href ?? '-no image-'), ...options }, undefined, undefined, undefined, overwriteDoc);
@@ -751,7 +747,6 @@ export namespace Docs {
          * @param fieldKey the field that the compiled script is written into.
          * @returns the Scripting Doc
          */
-        // eslint-disable-next-line default-param-last
         export function ScriptingDocument(script: Opt<ScriptField> | null, options: DocumentOptions = {}, fieldKey?: string) {
             return InstanceFromProto(Prototypes.get(DocumentType.SCRIPTING), script || undefined, { ...options, layout: fieldKey ? `<ScriptingBox {...props} fieldKey={'${fieldKey}'}/>` /* ScriptingBox.LayoutString(fieldKey) */ : undefined });
         }
@@ -759,7 +754,6 @@ export namespace Docs {
         export function ChatDocument(options?: DocumentOptions) {
             return InstanceFromProto(Prototypes.get(DocumentType.CHAT), undefined, { ...(options || {}) });
         }
-        // eslint-disable-next-line default-param-last
         export function VideoDocument(url: string, options: DocumentOptions = {}, overwriteDoc?: Doc) {
             return InstanceFromProto(Prototypes.get(DocumentType.VID), new VideoField(url), options, undefined, undefined, undefined, overwriteDoc);
         }
@@ -779,7 +773,6 @@ export namespace Docs {
             return InstanceFromProto(Prototypes.get(DocumentType.DIAGRAM), undefined, options);
         }
 
-        // eslint-disable-next-line default-param-last
         export function AudioDocument(url: string, options: DocumentOptions = {}, overwriteDoc?: Doc) {
             return InstanceFromProto(Prototypes.get(DocumentType.AUDIO), new AudioField(url), options, undefined, undefined, undefined, overwriteDoc);
         }
@@ -839,7 +832,6 @@ export namespace Docs {
             return InstanceFromProto(Prototypes.get(DocumentType.RTF), field, options, undefined, fieldKey);
         }
 
-        // eslint-disable-next-line default-param-last
         export function LinkDocument(source: Doc, target: Doc, options: DocumentOptions = {}, id?: string) {
             const linkDoc = InstanceFromProto(
                 Prototypes.get(DocumentType.LINK),
@@ -883,7 +875,6 @@ export namespace Docs {
             return ink;
         }
 
-        // eslint-disable-next-line default-param-last
         export function PdfDocument(url: string, options: DocumentOptions = {}, overwriteDoc?: Doc) {
             const width = options._width || undefined;
             const height = options._height || undefined;
diff --git a/src/client/util/CurrentUserUtils.ts b/src/client/util/CurrentUserUtils.ts
index 96b30c429..09adf70f5 100644
--- a/src/client/util/CurrentUserUtils.ts
+++ b/src/client/util/CurrentUserUtils.ts
@@ -2,7 +2,7 @@
 import { reaction, runInAction } from "mobx";
 import * as rp from 'request-promise';
 import { ClientUtils, OmitKeys } from "../../ClientUtils";
-import { Doc, DocListCast, DocListCastAsync, FieldType, Opt, StrListCast } from "../../fields/Doc";
+import { Doc, DocListCast, DocListCastAsync, FieldType, Opt } from "../../fields/Doc";
 import { DocData } from "../../fields/DocSymbols";
 import { InkTool } from "../../fields/InkField";
 import { List } from "../../fields/List";
@@ -366,11 +366,11 @@ pie title Minerals in my tap water
             {key: "Button",     creator: Docs.Create.ButtonDocument,                        opts: { _width: 150, _height: 50, _xPadding: 10, _yPadding: 10, title_custom: true,  waitForDoubleClickToClick: 'never'}, scripts: {onClick: FollowLinkScript()?.script.originalScript ?? ""}},
             {key: "Script",     creator: opts => Docs.Create.ScriptingDocument(null, opts), opts: { _width: 200, _height: 250, }},
             {key: "DataViz",    creator: opts => Docs.Create.DataVizDocument("/users/rz/Downloads/addresses.csv", opts),   opts: { _width: 300, _height: 300 }},
-            {key: "Chat",       creator: Docs.Create.ChatDocument,          opts: { _width: 500, _height: 500, }},
+            {key: "Chat",       creator: Docs.Create.ChatDocument,                          opts: { _width: 500, _height: 500, }},
             {key: "Header",     creator: headerTemplate,                                    opts: { _width: 300, _height: 120, _header_pointerEvents: "all", _header_height: 50, _header_fontSize: 9,_layout_autoHeightMargins: 50, _layout_autoHeight: true, treeView_HideUnrendered: true}},
             {key: "ViewSlide",  creator: slideView,                                         opts: { _width: 400, _height: 300, _xMargin: 3, _yMargin: 3,}},
             {key: "Trail",      creator: Docs.Create.PresDocument,                          opts: { _width: 400, _height: 30, _type_collection: CollectionViewType.Stacking, _layout_dontCenter:'xy', dropAction: dropActionType.embed, treeView_HideTitle: true, _layout_fitWidth:true, layout_boxShadow: "0 0" }},
-            {key: "Tab",        creator: opts => Docs.Create.FreeformDocument([], opts),    opts: { _width: 500, _height: 800, _layout_fitWidth: true, _freeform_backgroundGrid: true, }},
+        {key: "Tab",        creator: opts => Docs.Create.FreeformDocument([], opts),    opts: { _width: 500, _height: 800, _layout_fitWidth: true, _freeform_backgroundGrid: true, }},
             {key: "Slide",      creator: opts => Docs.Create.TreeDocument([], opts),        opts: { _width: 300, _height: 200, _type_collection: CollectionViewType.Tree, 
                                                                                                 treeView_HasOverlay: true, _text_fontSize: "20px", _layout_autoHeight: true, 
                                                                                                 dropAction:dropActionType.move, treeView_Type: TreeViewType.outline, 
@@ -801,7 +801,7 @@ pie title Minerals in my tap water
             { title: "Num",     icon:"", toolTip: "Frame # (click to toggle edit mode)",btnType: ButtonType.TextButton,          expertMode: true,  toolType:CollectionViewType.Freeform, funcs: {hidden: '!SelectedDocType(this.toolType, this.expertMode)', buttonText: 'selectedDocs()?.lastElement()?.currentFrame?.toString()'}, width: 20, scripts: { onClick: '{ return curKeyFrame(_readOnly_);}'}},
             { title: "Fwd",     icon: "chevron-right", toolTip: "Next Animation Frame", btnType: ButtonType.ClickButton,         expertMode: true,  toolType:CollectionViewType.Freeform, funcs: {hidden: '!SelectedDocType(this.toolType, this.expertMode)'}, width: 30,  scripts: { onClick: 'nextKeyFrame(_readOnly_)'}},
             
-            { title: "Filter",  icon: "=",      toolTip: "Filter cards by tags", subMenu: CurrentUserUtils.tagGroupTools(),ignoreClick:true, toolType:DocumentType.COL,            funcs: {hidden: '!SelectedDocType(this.toolType, this.expertMode)'}, btnType: ButtonType.MultiToggleButton, width: 30, backgroundColor: doc.userVariantColor},
+            { title: "Filter",  icon: "=",      toolTip: "Filter cards by tags", subMenu: CurrentUserUtils.tagGroupTools(),ignoreClick:true, toolType:DocumentType.COL,            funcs: {hidden: '!SelectedDocType(this.toolType, this.expertMode)'}, btnType: ButtonType.MultiToggleButton, width: 30, backgroundColor: doc.userVariantColor as string},
             { title: "Text",    icon: "Text",   toolTip: "Text functions",       subMenu: CurrentUserUtils.textTools(),   expertMode: false, toolType:DocumentType.RTF,            funcs: {                                                                             linearView_IsOpen: `SelectedDocType(this.toolType, this.expertMode)`} }, // Always available
             { title: "Ink",     icon: "Ink",    toolTip: "Ink functions",        subMenu: CurrentUserUtils.inkTools(),    expertMode: false, toolType:DocumentType.INK,            funcs: {hidden: `IsExploreMode()`,                                                   linearView_IsOpen: `SelectedDocType(this.toolType, this.expertMode)`}, scripts: { onClick: 'setInkToolDefaults()'} }, // Always available
             { title: "Doc",     icon: "Doc",    toolTip: "Freeform Doc tools",   subMenu: CurrentUserUtils.freeTools(),   expertMode: false, toolType:CollectionViewType.Freeform, funcs: {hidden: `!SelectedDocType(this.toolType, this.expertMode, true)`, linearView_IsOpen: `SelectedDocType(this.toolType, this.expertMode)`} }, // Always available
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts
index 3e7a2df02..c7b86815a 100644
--- a/src/client/util/Scripting.ts
+++ b/src/client/util/Scripting.ts
@@ -1,7 +1,7 @@
 // export const ts = (window as any).ts;
 // import * as typescriptlib from '!!raw-loader!../../../node_modules/typescript/lib/lib.d.ts'
 // import * as typescriptes5 from '!!raw-loader!../../../node_modules/typescript/lib/lib.es5.d.ts'
-// import typescriptlib from 'type_decls.d';
+import typescriptlib from 'type_decls.d';
 import * as ts from 'typescript';
 import { Doc, FieldType } from '../../fields/Doc';
 import { RefField } from '../../fields/RefField';
@@ -29,7 +29,7 @@ export interface CompiledScript {
     readonly compiled: true;
     readonly originalScript: string;
     // eslint-disable-next-line no-use-before-define
-    readonly options: Readonly;
+    readonly options: Readonly<ScriptOptions>;
     run(args?: { [name: string]: unknown }, onError?: (res: string) => void, errorVal?: unknown): ScriptResult;
 }
 
@@ -60,7 +60,6 @@ function Run(script: string | undefined, customParams: string[], diagnostics: ts
     // let params: any[] = [Docs, ...fieldTypes];
     const compiledFunction = (() => {
         try {
-            // eslint-disable-next-line no-new-func
             return new Function(...paramNames, `return ${script}`);
         } catch (e) {
             console.log(e);
@@ -69,10 +68,8 @@ function Run(script: string | undefined, customParams: string[], diagnostics: ts
     })();
     if (!compiledFunction) return { compiled: false, errors };
     const { capturedVariables = {} } = options;
-    // eslint-disable-next-line default-param-last
     const run = (args: { [name: string]: unknown } = {}, onError?: (e: string) => void, errorVal?: ts.Diagnostic): ScriptResult => {
         const argsArray: unknown[] = [];
-        // eslint-disable-next-line no-restricted-syntax
         for (const name of customParams) {
             if (name !== 'this') {
                 argsArray.push(name in args ? args[name] : capturedVariables[name]);
@@ -154,7 +151,7 @@ class ScriptingCompilerHost {
 export type Traverser = (node: ts.Node, indentation: string) => boolean | void;
 export type TraverserParam = Traverser | { onEnter: Traverser; onLeave: Traverser };
 export type Transformer = {
-    transformer: ts.TransformerFactory;
+    transformer: ts.TransformerFactory<ts.Node>;
     getVars?: () => { [name: string]: FieldType };
 };
 export interface ScriptOptions {
@@ -224,7 +221,6 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
     if ('this' in params || 'this' in capturedVariables) {
         paramNames.push('this');
     }
-    // eslint-disable-next-line no-restricted-syntax
     for (const key in params) {
         if (key !== 'this') {
             paramNames.push(key);
@@ -234,7 +230,6 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
         const val = params[key];
         return `${key}: ${val}`;
     });
-    // eslint-disable-next-line no-restricted-syntax
     for (const key in capturedVariables) {
         if (key !== 'this') {
             const val = capturedVariables[key];
@@ -248,7 +243,7 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
     const funcScript = `(function(${paramString})${reqTypes} { ${body} })`;
     host.writeFile('file.ts', funcScript);
 
-    // if (typecheck) host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib);
+    if (typecheck) host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib);
     const program = ts.createProgram(['file.ts'], {}, host);
     const testResult = program.emit();
     const outputText = host.readFile('file.js');
diff --git a/src/client/views/MainView.tsx b/src/client/views/MainView.tsx
index 8b8f85dfb..abe154de4 100644
--- a/src/client/views/MainView.tsx
+++ b/src/client/views/MainView.tsx
@@ -75,7 +75,7 @@ import { AnchorMenu } from './pdf/AnchorMenu';
 import { GPTPopup } from './pdf/GPTPopup/GPTPopup';
 import { TopBar } from './topbar/TopBar';
 
-// eslint-disable-next-line @typescript-eslint/no-var-requires, @typescript-eslint/no-require-imports
+// eslint-disable-next-line @typescript-eslint/no-require-imports
 const { LEFT_MENU_WIDTH, TOPBAR_HEIGHT } = require('./global/globalCssVariables.module.scss'); // prettier-ignore
 
 @observer
@@ -1059,10 +1059,7 @@ export class MainView extends ObservableReactComponent<object> {
                         docView={DocButtonState.Instance.LinkEditorDocView}
                     />
                 ) : null}
-                {LinkInfo.Instance?.LinkInfo ? (
-                    // eslint-disable-next-line react/jsx-props-no-spreading
-                    <LinkDocPreview {...LinkInfo.Instance.LinkInfo} />
-                ) : null}
+                {LinkInfo.Instance?.LinkInfo ? <LinkDocPreview {...LinkInfo.Instance.LinkInfo} /> : null}
                 {((page: string) => {
                     // prettier-ignore
                     switch (page) {
diff --git a/src/client/views/collections/CollectionNoteTakingViewColumn.tsx b/src/client/views/collections/CollectionNoteTakingViewColumn.tsx
index fc5f5cb71..226d06f37 100644
--- a/src/client/views/collections/CollectionNoteTakingViewColumn.tsx
+++ b/src/client/views/collections/CollectionNoteTakingViewColumn.tsx
@@ -252,10 +252,7 @@ export class CollectionNoteTakingViewColumn extends ObservableReactComponent<CSV
                                     <EditableView GetValue={returnEmptyString} SetValue={this.addNewTextDoc} textCallback={this.addTextNote} placeholder={"Type ':' for commands"} contents="+ Node" menuCallback={this.menuCallback} />
                                 </div>
                                 <div className="collectionNoteTakingView-addDocumentButton" style={{ color: lightOrDark(this._props.backgroundColor?.()) }}>
-                                    {
-                                        // eslint-disable-next-line react/jsx-props-no-spreading
-                                        <EditableView {...this._props.editableViewProps()} />
-                                    }
+                                    <EditableView {...this._props.editableViewProps()} />
                                 </div>
                             </div>
                         ) : null}
diff --git a/src/client/views/nodes/PDFBox.tsx b/src/client/views/nodes/PDFBox.tsx
index 4616ec057..596975062 100644
--- a/src/client/views/nodes/PDFBox.tsx
+++ b/src/client/views/nodes/PDFBox.tsx
@@ -10,7 +10,7 @@ import { DocData } from '../../../fields/DocSymbols';
 import { Id } from '../../../fields/FieldSymbols';
 import { InkTool } from '../../../fields/InkField';
 import { ComputedField } from '../../../fields/ScriptField';
-import { Cast, FieldValue, ImageCast, NumCast, StrCast, toList } from '../../../fields/Types';
+import { Cast, FieldValue, NumCast, StrCast, toList } from '../../../fields/Types';
 import { ImageField, PdfField } from '../../../fields/URLField';
 import { TraceMobx } from '../../../fields/util';
 import { emptyFunction } from '../../../Utils';
@@ -43,14 +43,14 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
     public static openSidebarWidth = 250;
     public static sidebarResizerWidth = 5;
     private _searchString: string = '';
-    private _initialScrollTarget: Opt;
+    private _initialScrollTarget: Opt<Doc>;
     private _pdfViewer: PDFViewer | undefined;
     private _searchRef = React.createRef<HTMLInputElement>();
     private _disposers: { [name: string]: IReactionDisposer } = {};
     private _sidebarRef = React.createRef<SidebarAnnos>();
 
     @observable private _searching: boolean = false;
-    @observable private _pdf: Opt = undefined;
+    @observable private _pdf: Opt<Pdfjs.PDFDocumentProxy> = undefined;
     @observable private _pageControls = false;
 
     @computed get pdfUrl() {
@@ -242,13 +242,13 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             options.didMove = true;
             this.toggleSidebar(false);
         }
-        return new Promise<Opt>(res => {
+        return new Promise<Opt<DocumentView>>(res => {
             DocumentView.addViewRenderedCb(doc, dv => res(dv));
         });
     };
 
     getAnchor = (addAsAnnotation: boolean, pinProps?: PinProps) => {
-        let ele: Opt;
+        let ele: Opt<HTMLDivElement>;
         if (this._pdfViewer?.selectionContent()) {
             ele = document.createElement('div');
             ele.append(this._pdfViewer.selectionContent()!);
@@ -327,7 +327,7 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             this._initialScrollTarget = undefined;
         }
     };
-    searchStringChanged = (e: React.ChangeEvent) => {
+    searchStringChanged = (e: React.ChangeEvent<HTMLInputElement>) => {
         this._searchString = e.currentTarget.value;
     };
 
@@ -365,8 +365,8 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             }
         );
     };
-    @observable _previewNativeWidth: Opt = undefined;
-    @observable _previewWidth: Opt = undefined;
+    @observable _previewNativeWidth: Opt<number> = undefined;
+    @observable _previewWidth: Opt<number> = undefined;
     toggleSidebar = action((preview: boolean = false) => {
         const nativeWidth = NumCast(this.layoutDoc[this.fieldKey + '_nativeWidth']);
         const sideratio = ((!this.layoutDoc.nativeWidth || this.layoutDoc.nativeWidth === nativeWidth ? PDFBox.openSidebarWidth : 0) + nativeWidth) / nativeWidth;
@@ -540,7 +540,6 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             return ComponentTag === CollectionStackingView ? (
                 <SidebarAnnos
                     ref={this._sidebarRef}
-                    // eslint-disable-next-line react/jsx-props-no-spreading
                     {...this._props}
                     Document={this.Document}
                     layoutDoc={this.layoutDoc}
@@ -556,7 +555,6 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
             ) : (
                 <div onPointerDown={e => setupMoveUpEvents(this, e, returnFalse, emptyFunction, () => this._props.select(false), true)}>
                     <ComponentTag
-                        // eslint-disable-next-line react/jsx-props-no-spreading
                         {...this._props}
                         setContentViewBox={emptyFunction} // override setContentView to do nothing
                         NativeWidth={this.sidebarNativeWidthFunc}
@@ -610,7 +608,6 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                         top: 0,
                     }}>
                     <PDFViewer
-                        // eslint-disable-next-line react/jsx-props-no-spreading
                         {...this._props}
                         pdfBox={this}
                         sidebarAddDoc={this.sidebarAddDocument}
@@ -637,7 +634,7 @@ export class PDFBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
     }
 
     static pdfcache = new Map<string, Pdfjs.PDFDocumentProxy>();
-    static pdfpromise = new Map<string, Promise>();
+    static pdfpromise = new Map<string, Promise<Pdfjs.PDFDocumentProxy>>();
     render() {
         TraceMobx();
         const pdfView = !this._pdf ? null : this.renderPdfView;
diff --git a/src/client/views/nodes/RecordingBox/ProgressBar.tsx b/src/client/views/nodes/RecordingBox/ProgressBar.tsx
index 62798bc2f..7e91df7ab 100644
--- a/src/client/views/nodes/RecordingBox/ProgressBar.tsx
+++ b/src/client/views/nodes/RecordingBox/ProgressBar.tsx
@@ -1,5 +1,3 @@
-/* eslint-disable react/no-array-index-key */
-/* eslint-disable react/require-default-props */
 import * as React from 'react';
 import { useEffect, useState, useRef } from 'react';
 import './ProgressBar.scss';
diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
index 180d05cf3..ccf9caf15 100644
--- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
@@ -1,20 +1,19 @@
+import dotenv from 'dotenv';
+import { XMLBuilder, XMLParser } from 'fast-xml-parser';
 import OpenAI from 'openai';
-import { Tool, AgentMessage, AssistantMessage, TEXT_TYPE, CHUNK_TYPE, ASSISTANT_ROLE, ProcessingInfo, PROCESSING_TYPE } from '../types/types';
-import { getReactPrompt } from './prompts';
-import { XMLParser, XMLBuilder } from 'fast-xml-parser';
-import { Vectorstore } from '../vectorstore/Vectorstore';
 import { ChatCompletionMessageParam } from 'openai/resources';
-import dotenv from 'dotenv';
-import { CalculateTool } from '../tools/CalculateTool';
-import { RAGTool } from '../tools/RAGTool';
-import { DataAnalysisTool } from '../tools/DataAnalysisTool';
-import { WebsiteInfoScraperTool } from '../tools/WebsiteInfoScraperTool';
-import { SearchTool } from '../tools/SearchTool';
-import { NoTool } from '../tools/NoTool';
-import { v4 as uuidv4 } from 'uuid';
 import { AnswerParser } from '../response_parsers/AnswerParser';
 import { StreamedAnswerParser } from '../response_parsers/StreamedAnswerParser';
+import { CalculateTool } from '../tools/CalculateTool';
 import { CreateCSVTool } from '../tools/CreateCSVTool';
+import { DataAnalysisTool } from '../tools/DataAnalysisTool';
+import { NoTool } from '../tools/NoTool';
+import { RAGTool } from '../tools/RAGTool';
+import { SearchTool } from '../tools/SearchTool';
+import { WebsiteInfoScraperTool } from '../tools/WebsiteInfoScraperTool';
+import { AgentMessage, AssistantMessage, PROCESSING_TYPE, ProcessingInfo, Tool } from '../types/types';
+import { Vectorstore } from '../vectorstore/Vectorstore';
+import { getReactPrompt } from './prompts';
 
 dotenv.config();
 
@@ -25,7 +24,7 @@ dotenv.config();
 export class Agent {
     // Private properties
     private client: OpenAI;
-    private tools: Record<string, Tool<any>>;
+    private tools: Record<string, Tool<any>>; // bcz: need a real type here
     private messages: AgentMessage[] = [];
     private interMessages: AgentMessage[] = [];
     private vectorstore: Vectorstore;
@@ -102,7 +101,7 @@ export class Agent {
             ignoreAttributes: false,
             attributeNamePrefix: '@_',
             textNodeName: '_text',
-            isArray: (name, jpath, isLeafNode, isAttribute) => ['query', 'url'].indexOf(name) !== -1,
+            isArray: (name /* , jpath, isLeafNode, isAttribute */) => ['query', 'url'].indexOf(name) !== -1,
         });
         const builder = new XMLBuilder({ ignoreAttributes: false, attributeNamePrefix: '@_' });
 
@@ -167,7 +166,7 @@ export class Agent {
                     if (currentAction) {
                         try {
                             // Process the action with its input
-                            const observation = await this.processAction(currentAction, actionInput.inputs);
+                            const observation = (await this.processAction(currentAction, actionInput.inputs)) as any; // bcz: really need a type here
                             const nextPrompt = [{ type: 'text', text: `<stage number="${i + 1}" role="user"> <observation>` }, ...observation, { type: 'text', text: '</observation></stage>' }];
                             console.log(observation);
                             this.interMessages.push({ role: 'user', content: nextPrompt });
@@ -214,7 +213,7 @@ export class Agent {
 
         // Process each chunk of the streamed response
         for await (const chunk of stream) {
-            let content = chunk.choices[0]?.delta?.content || '';
+            const content = chunk.choices[0]?.delta?.content || '';
             fullResponse += content;
 
             // Parse the streamed content character by character
@@ -267,7 +266,7 @@ export class Agent {
      * @param actionInput The inputs for the action.
      * @returns The result of the action.
      */
-    private async processAction(action: string, actionInput: any): Promise<any> {
+    private async processAction(action: string, actionInput: unknown): Promise<unknown> {
         if (!(action in this.tools)) {
             throw new Error(`Unknown action: ${action}`);
         }
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index d4deff78b..613cb7078 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -1,30 +1,29 @@
-import { action, computed, makeObservable, observable, observe, reaction, runInAction, ObservableSet } from 'mobx';
+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 } from '../../../../../ClientUtils';
 import { Doc, DocListCast } from '../../../../../fields/Doc';
+import { DocData, DocViews } from '../../../../../fields/DocSymbols';
 import { CsvCast, DocCast, PDFCast, RTFCast, StrCast } from '../../../../../fields/Types';
+import { Networking } from '../../../../Network';
+import { DocUtils } from '../../../../documents/DocUtils';
 import { DocumentType } from '../../../../documents/DocumentTypes';
 import { Docs } from '../../../../documents/Documents';
+import { DocumentManager } from '../../../../util/DocumentManager';
 import { LinkManager } from '../../../../util/LinkManager';
 import { ViewBoxAnnotatableComponent } from '../../../DocComponent';
+import { DocumentView } from '../../DocumentView';
 import { FieldView, FieldViewProps } from '../../FieldView';
+import { PDFBox } from '../../PDFBox';
+import { Agent } from '../agentsystem/Agent';
+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 { ASSISTANT_ROLE, AssistantMessage, Citation, CHUNK_TYPE, TEXT_TYPE, SimplifiedChunk, ProcessingInfo } from '../types/types';
-import { Vectorstore } from '../vectorstore/Vectorstore';
-import { Agent } from '../agentsystem/Agent';
-import dotenv from 'dotenv';
-import { DocData, DocViews } from '../../../../../fields/DocSymbols';
-import { DocumentManager } from '../../../../util/DocumentManager';
-import { v4 as uuidv4 } from 'uuid';
-import { DocUtils } from '../../../../documents/DocUtils';
-import { ClientUtils } from '../../../../../ClientUtils';
 import { ProgressBar } from './ProgressBar';
-import { DocumentView } from '../../DocumentView';
-import { Networking } from '../../../../Network';
-import { PDFViewer } from '../../../pdf/PDFViewer';
-import { PDFBox } from '../../PDFBox';
 
 dotenv.config();
 
@@ -247,7 +246,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
      * @param event The form submission event.
      */
     @action
-    askGPT = async (event: React.FormEvent): Promise<any> => {
+    askGPT = async (event: React.FormEvent): Promise<void> => {
         event.preventDefault();
         this.inputValue = '';
 
@@ -413,7 +412,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
         const chunkId = citation.chunk_id;
 
         // Loop through the linked documents to find the matching chunk and handle its display
-        for (let doc of currentLinkedDocs) {
+        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);
@@ -422,24 +421,26 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                     switch (foundChunk.chunkType) {
                         case CHUNK_TYPE.IMAGE:
                         case CHUNK_TYPE.TABLE:
-                            const values = foundChunk.location?.replace(/[\[\]]/g, '').split(',');
+                            {
+                                const values = foundChunk.location?.replace(/[[\]]/g, '').split(',');
 
-                            if (values?.length !== 4) {
-                                console.error('Location string must contain exactly 4 numbers');
-                                return;
-                            }
+                                if (values?.length !== 4) {
+                                    console.error('Location string must contain exactly 4 numbers');
+                                    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 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 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);
+                                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 }, () => {});
+                                DocumentManager.Instance.showDocument(highlightDoc, { willZoomCentered: true }, () => {});
+                            }
                             break;
                         case CHUNK_TYPE.TEXT:
                             this.citationPopup = { text: citation.direct_text ?? 'No text available', visible: true };
@@ -695,7 +696,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                     </div>
                 )}
                 <div className="chat-header">
-                    <h2>{this.userName()}'s AI Assistant</h2>
+                    <h2>{this.userName()}&apos;s AI Assistant</h2>
                 </div>
                 <div className="chat-messages" ref={this.messagesRef}>
                     {this.history.map((message, index) => (
diff --git a/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts b/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
index 3b4fdb6f5..1ac753790 100644
--- a/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
+++ b/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
@@ -1,5 +1,5 @@
-import { ASSISTANT_ROLE, AssistantMessage, Citation, CHUNK_TYPE, TEXT_TYPE, getChunkType, ProcessingInfo } from '../types/types';
 import { v4 as uuid } from 'uuid';
+import { ASSISTANT_ROLE, AssistantMessage, Citation, ProcessingInfo, TEXT_TYPE, getChunkType } from '../types/types';
 
 export class AnswerParser {
     static parse(xml: string, processingInfo: ProcessingInfo[]): AssistantMessage {
@@ -22,8 +22,8 @@ export class AnswerParser {
         }
 
         let rawTextContent = answerMatch[1].trim();
-        let content: AssistantMessage['content'] = [];
-        let citations: Citation[] = [];
+        const content: AssistantMessage['content'] = [];
+        const citations: Citation[] = [];
         let contentIndex = 0;
 
         // Remove citations and follow-up questions from rawTextContent
@@ -43,6 +43,7 @@ export class AnswerParser {
         if (citationsMatch) {
             const citationsContent = citationsMatch[1];
             while ((citationMatch = citationRegex.exec(citationsContent)) !== null) {
+                // eslint-disable-next-line @typescript-eslint/no-unused-vars
                 const [_, index, chunk_id, type, direct_text] = citationMatch;
                 const citation_id = uuid();
                 citationMap.set(index, citation_id);
@@ -102,7 +103,7 @@ export class AnswerParser {
             }
         }
 
-        let followUpQuestions: string[] = [];
+        const followUpQuestions: string[] = [];
         if (followUpQuestionsMatch) {
             const questionsText = followUpQuestionsMatch[1];
             let questionMatch;
diff --git a/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
index 3585cab4a..4149f3da9 100644
--- a/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
+++ b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
@@ -1,5 +1,3 @@
-import { threadId } from 'worker_threads';
-
 enum ParserState {
     Outside,
     InGroundedText,
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts
index b57f1c8e4..10780617b 100644
--- a/src/client/views/nodes/chatbot/tools/BaseTool.ts
+++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts
@@ -1,17 +1,17 @@
 import { Tool } from '../types/types';
 
-export abstract class BaseTool<T extends Record<string, any> = Record<string, any>> implements Tool<T> {
+export abstract class BaseTool<T extends Record<string, unknown> = Record<string, unknown>> implements Tool<T> {
     constructor(
         public name: string,
         public description: string,
-        public parameters: Record<string, any>,
+        public parameters: Record<string, unknown>,
         public citationRules: string,
         public briefSummary: string
     ) {}
 
-    abstract execute(args: T): Promise<any>;
+    abstract execute(args: T): Promise<unknown>;
 
-    getActionRule(): Record<string, any> {
+    getActionRule(): Record<string, unknown> {
         return {
             [this.name]: {
                 name: this.name,
diff --git a/src/client/views/nodes/chatbot/tools/CalculateTool.ts b/src/client/views/nodes/chatbot/tools/CalculateTool.ts
index 74b7ca27b..77ab1b39b 100644
--- a/src/client/views/nodes/chatbot/tools/CalculateTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CalculateTool.ts
@@ -18,7 +18,7 @@ export class CalculateTool extends BaseTool<{ expression: string }> {
         );
     }
 
-    async execute(args: { expression: string }): Promise<any> {
+    async execute(args: { expression: string }): Promise<unknown> {
         // Note: Using eval() can be dangerous. Consider using a safer alternative.
         const result = eval(args.expression);
         return [{ type: 'text', text: result.toString() }];
diff --git a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts
index 55015846b..d3ded0de0 100644
--- a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts
@@ -28,7 +28,7 @@ export class CreateCSVTool extends BaseTool<{ csvData: string; filename: string
         this._handleCSVResult = handleCSVResult;
     }
 
-    async execute(args: { csvData: string; filename: string }): Promise<any> {
+    async execute(args: { csvData: string; filename: string }): Promise<unknown> {
         try {
             console.log('Creating CSV file:', args.filename, ' with data:', args.csvData);
             // Post the raw CSV data to the createCSV endpoint on the server
diff --git a/src/client/views/nodes/chatbot/tools/CreateCollectionTool.ts b/src/client/views/nodes/chatbot/tools/CreateCollectionTool.ts
index 573428179..1e479a62c 100644
--- a/src/client/views/nodes/chatbot/tools/CreateCollectionTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CreateCollectionTool.ts
@@ -25,7 +25,7 @@ export class GetDocsContentTool extends BaseTool<{ title: string; document_ids:
         this._docView = docView;
     }
 
-    async execute(args: { title: string; document_ids: string[] }): Promise<any> {
+    async execute(args: { title: string; document_ids: string[] }): Promise<unknown> {
         // Note: Using eval() can be dangerous. Consider using a safer alternative.
         const docs = args.document_ids.map(doc_id => DocCast(DocServer.GetCachedRefField(doc_id)));
         const collection = Docs.Create.FreeformDocument(docs, { title: args.title });
diff --git a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts
index a12ee46e5..2e663fed1 100644
--- a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts
+++ b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts
@@ -33,7 +33,7 @@ export class DataAnalysisTool extends BaseTool<{ csv_file_name: string | string[
         return file?.id;
     }
 
-    async execute(args: { csv_file_name: string | string[] }): Promise<any> {
+    async execute(args: { csv_file_name: string | string[] }): Promise<unknown> {
         const filenames = Array.isArray(args.csv_file_name) ? args.csv_file_name : [args.csv_file_name];
         const results = [];
 
diff --git a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts
index f970ca8ee..903f3f69c 100644
--- a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts
+++ b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts
@@ -18,7 +18,7 @@ export class GetDocsTool extends BaseTool<{ title: string; document_ids: string[
         this._docView = docView;
     }
 
-    async execute(args: { title: string; document_ids: string[] }): Promise<any> {
+    async execute(args: { title: string; document_ids: string[] }): Promise<unknown> {
         // Note: Using eval() can be dangerous. Consider using a safer alternative.
         const docs = args.document_ids.map(doc_id => DocCast(DocServer.GetCachedRefField(doc_id)));
         const collection = Docs.Create.FreeformDocument(docs, { title: args.title });
diff --git a/src/client/views/nodes/chatbot/tools/NoTool.ts b/src/client/views/nodes/chatbot/tools/NoTool.ts
index 1f0830a77..edd3160ec 100644
--- a/src/client/views/nodes/chatbot/tools/NoTool.ts
+++ b/src/client/views/nodes/chatbot/tools/NoTool.ts
@@ -1,7 +1,7 @@
 // tools/NoTool.ts
 import { BaseTool } from './BaseTool';
 
-export class NoTool extends BaseTool<{}> {
+export class NoTool extends BaseTool<Record<string, unknown>> {
     constructor() {
         super(
             'no_tool',
@@ -12,7 +12,8 @@ export class NoTool extends BaseTool<{}> {
         );
     }
 
-    async execute(args: {}): Promise<any> {
+    // eslint-disable-next-line @typescript-eslint/no-unused-vars
+    async execute(args: object): Promise<unknown> {
         return [{ type: 'text', text: 'No tool used. Proceed with answering the question.' }];
     }
 }
diff --git a/src/client/views/nodes/chatbot/tools/RAGTool.ts b/src/client/views/nodes/chatbot/tools/RAGTool.ts
index f4b7b42ea..4cc2f26ff 100644
--- a/src/client/views/nodes/chatbot/tools/RAGTool.ts
+++ b/src/client/views/nodes/chatbot/tools/RAGTool.ts
@@ -1,10 +1,7 @@
-import { BaseTool } from './BaseTool';
-import { Vectorstore } from '../vectorstore/Vectorstore';
-import { RAGChunk } from '../types/types';
-import * as fs from 'fs';
 import { Networking } from '../../../../Network';
-import { file } from 'jszip';
-import { ChatCompletion, ChatCompletionContentPart, ChatCompletionMessageParam } from 'openai/resources';
+import { RAGChunk } from '../types/types';
+import { Vectorstore } from '../vectorstore/Vectorstore';
+import { BaseTool } from './BaseTool';
 
 export class RAGTool extends BaseTool {
     constructor(private vectorstore: Vectorstore) {
@@ -59,13 +56,13 @@ export class RAGTool extends BaseTool {
         );
     }
 
-    async execute(args: { hypothetical_document_chunk: string }): Promise<any> {
+    async execute(args: { hypothetical_document_chunk: string }): Promise<unknown> {
         const relevantChunks = await this.vectorstore.retrieve(args.hypothetical_document_chunk);
         const formatted_chunks = await this.getFormattedChunks(relevantChunks);
         return formatted_chunks;
     }
 
-    async getFormattedChunks(relevantChunks: RAGChunk[]): Promise<any> {
+    async getFormattedChunks(relevantChunks: RAGChunk[]): Promise<unknown> {
         try {
             const { formattedChunks } = await Networking.PostToServer('/formatChunks', { relevantChunks });
 
diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts
index b926cbadc..3a4668422 100644
--- a/src/client/views/nodes/chatbot/tools/SearchTool.ts
+++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts
@@ -1,7 +1,6 @@
-import { max } from 'lodash';
+import { v4 as uuidv4 } from 'uuid';
 import { Networking } from '../../../../Network';
 import { BaseTool } from './BaseTool';
-import { v4 as uuidv4 } from 'uuid';
 
 export class SearchTool extends BaseTool<{ query: string | string[] }> {
     private _addLinkedUrlDoc: (url: string, id: string) => void;
@@ -25,7 +24,7 @@ export class SearchTool extends BaseTool<{ query: string | string[] }> {
         this._max_results = max_results;
     }
 
-    async execute(args: { query: string | string[] }): Promise<any> {
+    async execute(args: { query: string | string[] }): Promise<unknown> {
         const queries = Array.isArray(args.query) ? args.query : [args.query];
         const allResults = [];
 
diff --git a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts
index 2118218f6..1efb389b8 100644
--- a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts
+++ b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts
@@ -1,6 +1,6 @@
+import { v4 as uuidv4 } from 'uuid';
 import { Networking } from '../../../../Network';
 import { BaseTool } from './BaseTool';
-import { v4 as uuidv4 } from 'uuid';
 
 export class WebsiteInfoScraperTool extends BaseTool<{ url: string | string[] }> {
     private _addLinkedUrlDoc: (url: string, id: string) => void;
@@ -63,7 +63,7 @@ export class WebsiteInfoScraperTool extends BaseTool<{ url: string | string[] }>
         this._addLinkedUrlDoc = addLinkedUrlDoc;
     }
 
-    async execute(args: { url: string | string[] }): Promise<any> {
+    async execute(args: { url: string | string[] }): Promise<unknown> {
         const urls = Array.isArray(args.url) ? args.url : [args.url];
         const results = [];
 
@@ -74,6 +74,7 @@ export class WebsiteInfoScraperTool extends BaseTool<{ url: string | string[] }>
                 this._addLinkedUrlDoc(url, id);
                 results.push({ type: 'text', text: `<chunk chunk_id=${id} chunk_type=url>\n${website_plain_text}\n</chunk>\n` });
             } catch (error) {
+                console.log(error);
                 results.push({ type: 'text', text: `An error occurred while scraping the website: ${url}` });
             }
         }
diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts
index 143d91d80..692dff749 100644
--- a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts
+++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts
@@ -1,8 +1,6 @@
-import { title } from 'process';
+import { v4 as uuidv4 } from 'uuid';
 import { Networking } from '../../../../Network';
 import { BaseTool } from './BaseTool';
-import axios from 'axios';
-import { v4 as uuidv4 } from 'uuid';
 
 export class WikipediaTool extends BaseTool<{ title: string }> {
     private _addLinkedUrlDoc: (url: string, id: string) => void;
@@ -23,7 +21,7 @@ export class WikipediaTool extends BaseTool<{ title: string }> {
         this._addLinkedUrlDoc = addLinkedUrlDoc;
     }
 
-    async execute(args: { title: string }): Promise<any> {
+    async execute(args: { title: string }): Promise<unknown> {
         try {
             const { text } = await Networking.PostToServer('/getWikipediaSummary', { title: args.title });
             const id = uuidv4();
@@ -31,6 +29,7 @@ export class WikipediaTool extends BaseTool<{ title: string }> {
             this._addLinkedUrlDoc(url, id);
             return [{ type: 'text', text: `<chunk chunk_id=${id} chunk_type=csv}> ${text} </chunk>` }];
         } catch (error) {
+            console.log(error);
             return [{ type: 'text', text: 'An error occurred while fetching the article.' }];
         }
     }
diff --git a/src/client/views/nodes/chatbot/types/types.ts b/src/client/views/nodes/chatbot/types/types.ts
index f5d14ad6a..2bc7f4952 100644
--- a/src/client/views/nodes/chatbot/types/types.ts
+++ b/src/client/views/nodes/chatbot/types/types.ts
@@ -52,15 +52,6 @@ export interface ProcessingInfo {
     content: string;
 }
 
-export interface AssistantMessage {
-    role: ASSISTANT_ROLE;
-    content: MessageContent[];
-    follow_up_questions?: string[];
-    citations?: Citation[];
-    processing_info: ProcessingInfo[];
-    loop_summary?: string;
-}
-
 export interface MessageContent {
     index: number;
     type: TEXT_TYPE;
@@ -75,6 +66,14 @@ export interface Citation {
     citation_id: string;
     url?: string;
 }
+export interface AssistantMessage {
+    role: ASSISTANT_ROLE;
+    content: MessageContent[];
+    follow_up_questions?: string[];
+    citations?: Citation[];
+    processing_info: ProcessingInfo[];
+    loop_summary?: string;
+}
 
 export interface RAGChunk {
     id: string;
@@ -113,14 +112,14 @@ export interface AI_Document {
     type: string;
 }
 
-export interface Tool<T extends Record<string, any> = Record<string, any>> {
+export interface Tool<T extends Record<string, unknown> = Record<string, unknown>> {
     name: string;
     description: string;
-    parameters: Record<string, any>;
+    parameters: Record<string, unknown>;
     citationRules: string;
     briefSummary: string;
-    execute: (args: T) => Promise<any>;
-    getActionRule: () => Record<string, any>;
+    execute: (args: T) => Promise<unknown>;
+    getActionRule: () => Record<string, unknown>;
 }
 
 export interface AgentMessage {
diff --git a/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts b/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
index 07a2b73bc..9575277f7 100644
--- a/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
+++ b/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
@@ -1,11 +1,11 @@
-import { Pinecone, Index, IndexList, PineconeRecord, RecordMetadata, QueryResponse } from '@pinecone-database/pinecone';
+import { Index, IndexList, Pinecone, PineconeRecord, QueryResponse, RecordMetadata } from '@pinecone-database/pinecone';
 import { CohereClient } from 'cohere-ai';
 import { EmbedResponse } from 'cohere-ai/api';
 import dotenv from 'dotenv';
-import { RAGChunk, AI_Document, CHUNK_TYPE } from '../types/types';
 import { Doc } from '../../../../../fields/Doc';
 import { CsvCast, PDFCast, StrCast } from '../../../../../fields/Types';
 import { Networking } from '../../../../Network';
+import { AI_Document, CHUNK_TYPE, RAGChunk } from '../types/types';
 
 dotenv.config();
 
@@ -103,8 +103,8 @@ export class Vectorstore {
                 const { jobId } = await Networking.PostToServer('/createDocument', { file_path: local_file_path });
 
                 // Poll the server for progress updates.
-                let inProgress: boolean = true;
-                let result: any = null;
+                const inProgress = true;
+                let result: (AI_Document & { doc_id: string }) | null = null; // bcz: is this the correct type??
                 while (inProgress) {
                     // Polling interval for status updates.
                     await new Promise(resolve => setTimeout(resolve, 2000));
@@ -127,6 +127,9 @@ export class Vectorstore {
                         progressCallback(progress, step);
                     }
                 }
+                if (!result) {
+                    throw new Error('no result received...'); // bcz: is this an Error?
+                }
 
                 // Once completed, process the document and add it to the vectorstore.
                 console.log('Document JSON:', result);
@@ -175,7 +178,7 @@ export class Vectorstore {
      * Indexes the processed document by uploading the document's vector chunks to the Pinecone index.
      * @param document The processed document containing its chunks and metadata.
      */
-    private async indexDocument(document: any) {
+    private async indexDocument(document: AI_Document) {
         console.log('Uploading vectors to content namespace...');
 
         // Prepare Pinecone records for each chunk in the document.
diff --git a/src/client/views/pdf/PDFViewer.tsx b/src/client/views/pdf/PDFViewer.tsx
index 7543b3fb1..b5c69bff0 100644
--- a/src/client/views/pdf/PDFViewer.tsx
+++ b/src/client/views/pdf/PDFViewer.tsx
@@ -51,7 +51,7 @@ interface IViewerProps extends FieldViewProps {
  * Handles rendering and virtualization of the pdf
  */
 @observer
-export class PDFViewer extends ObservableReactComponent {
+export class PDFViewer extends ObservableReactComponent<IViewerProps> {
     static _annotationStyle = addStyleSheet();
 
     constructor(props: IViewerProps) {
@@ -68,12 +68,12 @@ export class PDFViewer extends ObservableReactComponent {
     private _pdfViewer!: PDFJSViewer.PDFViewer;
     private _styleRule: number | undefined; // stylesheet rule for making hyperlinks clickable
     private _retries = 0; // number of times tried to create the PDF viewer
-    private _setPreviewCursor: undefined | ((x: number, y: number, drag: boolean, hide: boolean, doc: Opt) => void);
+    private _setPreviewCursor: undefined | ((x: number, y: number, drag: boolean, hide: boolean, doc: Opt<Doc>) => void);
     private _marqueeref = React.createRef<MarqueeAnnotator>();
-    private _annotationLayer: React.RefObject = React.createRef();
+    private _annotationLayer: React.RefObject<HTMLDivElement> = React.createRef();
     private _disposers: { [name: string]: IReactionDisposer } = {};
-    private _viewer: React.RefObject = React.createRef();
-    _mainCont: React.RefObject = React.createRef();
+    private _viewer: React.RefObject<HTMLDivElement> = React.createRef();
+    _mainCont: React.RefObject<HTMLDivElement> = React.createRef();
     private _selectionText: string = '';
     private _selectionContent: DocumentFragment | undefined;
     private _downX: number = 0;
@@ -81,9 +81,9 @@ export class PDFViewer extends ObservableReactComponent {
     private _lastSearch = false;
     private _viewerIsSetup = false;
     private _ignoreScroll = false;
-    private _initialScroll: { loc: Opt; easeFunc: 'linear' | 'ease' | undefined } | undefined;
+    private _initialScroll: { loc: Opt<number>; easeFunc: 'linear' | 'ease' | undefined } | undefined;
     private _forcedScroll = true;
-    _getAnchor: (savedAnnotations: Opt, addAsAnnotation: boolean) => Opt = () => undefined;
+    _getAnchor: (savedAnnotations: Opt<ObservableMap<number, HTMLDivElement[]>>, addAsAnnotation: boolean) => Opt<Doc> = () => undefined;
 
     selectionText = () => this._selectionText;
     selectionContent = () => this._selectionContent;
@@ -179,7 +179,7 @@ export class PDFViewer extends ObservableReactComponent {
     // otherwise it will scroll smoothly.
     scrollFocus = (doc: Doc, scrollTop: number, options: FocusViewOptions) => {
         const mainCont = this._mainCont.current;
-        let focusSpeed: Opt;
+        let focusSpeed: Opt<number>;
         if (doc !== this._props.Document && mainCont) {
             const windowHeight = this._props.PanelHeight() / (this._props.NativeDimScaling?.() || 1);
             const scrollTo = ClientUtils.scrollIntoView(scrollTop, doc[Height](), NumCast(this._props.layoutDoc._layout_scrollTop), windowHeight, windowHeight * 0.1, this._scrollHeight);
@@ -394,30 +394,6 @@ export class PDFViewer extends ObservableReactComponent {
         }
     };
 
-    // @action
-    // createMarquee = (coords: [x1: number, x2: number, y1: number, y2: number]): void => {
-    //     // const hit = document.elementFromPoint(e.clientX, e.clientY);
-    //     // bcz: Change. drag selecting requires that preventDefault is NOT called.  This used to happen in DocumentView,
-    //     //      but that's changed, so this shouldn't be needed.
-    //     // if (hit && hit.localName === "span" && this.annotationsActive(true)) {  // drag selecting text stops propagation
-    //     //     e.button === 0 && e.stopPropagation();
-    //     // }
-    //     // if alt+left click, drag and annotate
-    //     this._downX = coords[0];
-    //     this._downY = coords[2];
-
-    //     if ((this._props.Document._freeform_scale || 1) !== 1) return;
-    //     this._props.select(false);
-    //     MarqueeAnnotator.clearAnnotations(this._savedAnnotations);
-    //     this.isAnnotating = true;
-    //     this._textSelecting = false;
-    //     // if textLayer is hit, then we select text instead of using a marquee so clear out the marquee.
-    //     this._styleRule = addStyleSheetRule(PDFViewer._annotationStyle, 'htmlAnnotation', { 'pointer-events': 'none' });
-
-    //     this._marqueeref.current?.onInitiateSelection([coords[0], coords[2]]);
-    //     this._marqueeref.current?.onTerminateSelection();
-    // };
-
     @action
     finishMarquee = (/* x?: number, y?: number */) => {
         this._getAnchor = AnchorMenu.Instance?.GetAnchor;
@@ -495,7 +471,7 @@ export class PDFViewer extends ObservableReactComponent {
         // e.stopPropagation();  // bcz: not sure why this was here.  We need to allow the DocumentView to get clicks to process doubleClicks
     };
 
-    setPreviewCursor = (func?: (x: number, y: number, drag: boolean, hide: boolean, doc: Opt) => void) => {
+    setPreviewCursor = (func?: (x: number, y: number, drag: boolean, hide: boolean, doc: Opt<Doc>) => void) => {
         this._setPreviewCursor = func;
     };
 
@@ -520,7 +496,6 @@ export class PDFViewer extends ObservableReactComponent {
         return (
             <div className="pdfViewerDash-annotationLayer" style={{ height: Doc.NativeHeight(this._props.Document), transform: `scale(${NumCast(this._props.layoutDoc._freeform_scale, 1)})` }} ref={this._annotationLayer}>
                 {inlineAnnos.map(anno => (
-                    // eslint-disable-next-line react/jsx-props-no-spreading
                     <Annotation {...this._props} fieldKey={this._props.fieldKey + '_annotations'} pointerEvents={this.pointerEvents} containerDataDoc={this._props.dataDoc} annoDoc={anno} key={`${anno[Id]}-annotation`} />
                 ))}
             </div>
@@ -534,7 +509,7 @@ export class PDFViewer extends ObservableReactComponent {
     panelHeight = () => this._props.PanelHeight() / (this._props.NativeDimScaling?.() || 1);
     transparentFilter = () => [...this._props.childFilters(), ClientUtils.TransparentBackgroundFilter];
     opaqueFilter = () => [...this._props.childFilters(), ClientUtils.noDragDocsFilter, ...(SnappingManager.CanEmbed && this._props.isContentActive() ? [] : [ClientUtils.OpaqueBackgroundFilter])];
-    childStyleProvider = (doc: Doc | undefined, props: Opt, property: string) => {
+    childStyleProvider = (doc: Doc | undefined, props: Opt<FieldViewProps>, property: string) => {
         if (doc instanceof Doc && property === StyleProp.PointerEvents) {
             if (this.inlineTextAnnotations.includes(doc) || this._props.isContentActive() === false) return 'none';
             const isInk = doc.layout_isSvg && !props?.LayoutTemplateString;
@@ -554,7 +529,6 @@ export class PDFViewer extends ObservableReactComponent {
                 pointerEvents: Doc.ActiveTool !== InkTool.None ? 'all' : undefined,
             }}>
             <CollectionFreeFormView
-                // eslint-disable-next-line react/jsx-props-no-spreading
                 {...this._props}
                 NativeWidth={returnZero}
                 NativeHeight={returnZero}
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts
index e940fb121..b4ebb1eae 100644
--- a/src/server/ApiManagers/AssistantManager.ts
+++ b/src/server/ApiManagers/AssistantManager.ts
@@ -1,23 +1,19 @@
+import { Readability } from '@mozilla/readability';
+import axios from 'axios';
+import { spawn } from 'child_process';
 import * as fs from 'fs';
-import { createReadStream, writeFile } from 'fs';
+import { writeFile } from 'fs';
+import { google } from 'googleapis';
+import { JSDOM } from 'jsdom';
 import OpenAI from 'openai';
 import * as path from 'path';
+import * as puppeteer from 'puppeteer';
 import { promisify } from 'util';
 import * as uuid from 'uuid';
-import { filesDirectory, publicDirectory } from '../SocketData';
+import { AI_Document } from '../../client/views/nodes/chatbot/types/types';
 import { Method } from '../RouteManager';
+import { filesDirectory, publicDirectory } from '../SocketData';
 import ApiManager, { Registration } from './ApiManager';
-import axios from 'axios';
-import { RAGChunk } from '../../client/views/nodes/chatbot/types/types';
-import { UnstructuredClient } from 'unstructured-client';
-import { PartitionResponse } from 'unstructured-client/sdk/models/operations';
-import { ChunkingStrategy, Strategy } from 'unstructured-client/sdk/models/shared';
-import * as cheerio from 'cheerio';
-import { google } from 'googleapis';
-import * as puppeteer from 'puppeteer';
-import { JSDOM } from 'jsdom';
-import { Readability } from '@mozilla/readability';
-import { spawn } from 'child_process';
 
 // Enumeration of directories where different file types are stored
 export enum Directory {
@@ -34,8 +30,8 @@ export enum Directory {
 }
 
 // In-memory job tracking
-let jobResults: { [key: string]: any } = {};
-let jobProgress: { [key: string]: any } = {};
+const jobResults: { [key: string]: unknown } = {};
+const jobProgress: { [key: string]: unknown } = {};
 
 /**
  * Constructs a normalized path to a file in the server's file system.
@@ -81,10 +77,10 @@ export default class AssistantManager extends ApiManager {
      */
     protected initialize(register: Registration): void {
         // Initialize OpenAI API with client key
-        const openai = new OpenAI({
+        const openai = new OpenAI({ // bcz: is this needed?  variable is never used...
             apiKey: process.env._CLIENT_OPENAI_KEY,
             dangerouslyAllowBrowser: true,
-        });
+        }); // prettier-ignore
 
         // Initialize Google Custom Search API
         const customsearch = google.customsearch('v1');
@@ -107,11 +103,11 @@ export default class AssistantManager extends ApiManager {
                     });
                     const summary = response.data.query.search[0]?.snippet || 'No article found with that title.';
                     res.send({ text: summary });
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error retrieving Wikipedia summary:', error);
                     res.status(500).send({
                         error: 'Error retrieving article summary from Wikipedia.',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know what the error type contains...
                     });
                 }
             },
@@ -134,17 +130,17 @@ export default class AssistantManager extends ApiManager {
                     });
 
                     const results =
-                        response.data.items?.map((item: any) => ({
+                        response.data.items?.map(item => ({
                             url: item.link,
                             snippet: item.snippet,
                         })) || [];
 
                     res.send({ results });
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error performing web search:', error);
                     res.status(500).send({
                         error: 'Failed to perform web search',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -170,16 +166,16 @@ export default class AssistantManager extends ApiManager {
          * @param retries The number of retry attempts.
          * @param backoff Initial backoff time in milliseconds.
          */
-        const fetchWithRetry = async (url: string, retries = 3, backoff = 300) => {
+        const fetchWithRetry = async (url: string, retries = 3, backoff = 300): Promise<unknown> => {
             try {
                 const response = await axiosInstance.get(url);
                 return response.data;
-            } catch (error: any) {
-                if (retries > 0 && error.response?.status === 429) {
+            } catch (error) {
+                if (retries > 0 && (error as { response: { status: number } }).response?.status === 429) {   // bcz: don't know the error type
                     console.log(`Rate limited. Retrying in ${backoff}ms...`);
                     await delay(backoff);
                     return fetchWithRetry(url, retries - 1, backoff * 2);
-                }
+                } // prettier-ignore
                 throw error;
             }
         };
@@ -199,11 +195,11 @@ export default class AssistantManager extends ApiManager {
                 try {
                     const data = await fetchWithRetry(url);
                     res.send({ data });
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error fetching the URL:', error);
                     res.status(500).send({
                         error: 'Failed to fetch the URL',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -241,11 +237,11 @@ export default class AssistantManager extends ApiManager {
                     } else {
                         res.status(500).send({ error: 'Failed to extract readable content' });
                     }
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error scraping website:', error);
                     res.status(500).send({
                         error: 'Failed to scrape website',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -267,15 +263,16 @@ export default class AssistantManager extends ApiManager {
                     const jobId = uuid.v4();
 
                     // Spawn the Python process and track its progress/output
+                    // eslint-disable-next-line no-use-before-define
                     spawnPythonProcess(jobId, file_name, file_data);
 
                     // Send the job ID back to the client for tracking
                     res.send({ jobId });
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error initiating document creation:', error);
                     res.status(500).send({
                         error: 'Failed to initiate document creation',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -307,13 +304,13 @@ export default class AssistantManager extends ApiManager {
                 const { jobId } = req.params; // Get the job ID from the URL parameters
                 // Check if the job result is available
                 if (jobResults[jobId]) {
-                    const result = jobResults[jobId];
+                    const result = jobResults[jobId] as AI_Document & { status: string };
 
                     // If the result contains image or table chunks, save the base64 data as image files
                     if (result.chunks && Array.isArray(result.chunks)) {
                         for (const chunk of result.chunks) {
                             if (chunk.metadata && (chunk.metadata.type === 'image' || chunk.metadata.type === 'table')) {
-                                let files_directory = '/files/chunk_images/';
+                                const files_directory = '/files/chunk_images/';
                                 const directory = path.join(publicDirectory, files_directory);
 
                                 // Ensure the directory exists or create it
@@ -338,7 +335,7 @@ export default class AssistantManager extends ApiManager {
                                 }
                             }
                         }
-                        result['status'] = 'completed';
+                        result.status = 'completed';
                     } else {
                         result.status = 'pending';
                     }
@@ -429,11 +426,11 @@ export default class AssistantManager extends ApiManager {
 
                     // Send the file URL and UUID back to the client
                     res.send({ fileUrl, id: uuidv4 });
-                } catch (error: any) {
+                } catch (error) {
                     console.error('Error creating CSV file:', error);
                     res.status(500).send({
                         error: 'Failed to create CSV file.',
-                        details: error.message,
+                        details: (error as { message: string }).message ?? error, // bcz: don't know what the error type contains...
                     });
                 }
             },
@@ -446,59 +443,6 @@ function spawnPythonProcess(jobId: string, file_name: string, file_data: string)
     const requirementsPath = path.join(__dirname, '../chunker/requirements.txt');
     const pythonScriptPath = path.join(__dirname, '../chunker/pdf_chunker.py');
 
-    // Check if venv exists
-    if (!fs.existsSync(venvPath)) {
-        console.log('Virtual environment not found. Creating and setting up...');
-
-        // Create venv
-        const createVenvProcess = spawn('python', ['-m', 'venv', venvPath]);
-
-        createVenvProcess.on('close', code => {
-            if (code !== 0) {
-                console.error(`Failed to create virtual environment. Exit code: ${code}`);
-                return;
-            }
-
-            console.log('Virtual environment created. Installing requirements...');
-
-            // Determine the pip path based on the OS
-            const pipPath = process.platform === 'win32' ? path.join(venvPath, 'Scripts', 'pip.exe') : path.join(venvPath, 'bin', 'pip3'); // Try 'pip3' for Unix-like systems
-
-            if (!fs.existsSync(pipPath)) {
-                console.error(`pip executable not found at ${pipPath}`);
-                return;
-            }
-
-            // Install requirements
-            const installRequirementsProcess = spawn(pipPath, ['install', '-r', requirementsPath]);
-
-            installRequirementsProcess.stdout.on('data', data => {
-                console.log(`pip stdout: ${data}`);
-            });
-
-            installRequirementsProcess.stderr.on('data', data => {
-                console.error(`pip stderr: ${data}`);
-            });
-
-            installRequirementsProcess.on('error', error => {
-                console.error(`Error starting pip process: ${error}`);
-            });
-
-            installRequirementsProcess.on('close', code => {
-                if (code !== 0) {
-                    console.error(`Failed to install requirements. Exit code: ${code}`);
-                    return;
-                }
-
-                console.log('Requirements installed. Running Python script...');
-                runPythonScript();
-            });
-        });
-    } else {
-        console.log('Virtual environment found. Running Python script...');
-        runPythonScript();
-    }
-
     function runPythonScript() {
         const pythonPath = process.platform === 'win32' ? path.join(venvPath, 'Scripts', 'python') : path.join(venvPath, 'bin', 'python3');
 
@@ -530,7 +474,7 @@ function spawnPythonProcess(jobId: string, file_name: string, file_data: string)
                             };
                         }
                     } catch (err) {
-                        console.error('Progress log from Python:', line);
+                        console.error('Progress log from Python:', line, err);
                     }
                 }
             });
@@ -551,4 +495,56 @@ function spawnPythonProcess(jobId: string, file_name: string, file_data: string)
             }
         });
     }
+    // Check if venv exists
+    if (!fs.existsSync(venvPath)) {
+        console.log('Virtual environment not found. Creating and setting up...');
+
+        // Create venv
+        const createVenvProcess = spawn('python', ['-m', 'venv', venvPath]);
+
+        createVenvProcess.on('close', code => {
+            if (code !== 0) {
+                console.error(`Failed to create virtual environment. Exit code: ${code}`);
+                return;
+            }
+
+            console.log('Virtual environment created. Installing requirements...');
+
+            // Determine the pip path based on the OS
+            const pipPath = process.platform === 'win32' ? path.join(venvPath, 'Scripts', 'pip.exe') : path.join(venvPath, 'bin', 'pip3'); // Try 'pip3' for Unix-like systems
+
+            if (!fs.existsSync(pipPath)) {
+                console.error(`pip executable not found at ${pipPath}`);
+                return;
+            }
+
+            // Install requirements
+            const installRequirementsProcess = spawn(pipPath, ['install', '-r', requirementsPath]);
+
+            installRequirementsProcess.stdout.on('data', data => {
+                console.log(`pip stdout: ${data}`);
+            });
+
+            installRequirementsProcess.stderr.on('data', data => {
+                console.error(`pip stderr: ${data}`);
+            });
+
+            installRequirementsProcess.on('error', error => {
+                console.error(`Error starting pip process: ${error}`);
+            });
+
+            installRequirementsProcess.on('close', closecode => {
+                if (closecode !== 0) {
+                    console.error(`Failed to install requirements. Exit code: ${closecode}`);
+                    return;
+                }
+
+                console.log('Requirements installed. Running Python script...');
+                runPythonScript();
+            });
+        });
+    } else {
+        console.log('Virtual environment found. Running Python script...');
+        runPythonScript();
+    }
 }
-- 
cgit v1.2.3-70-g09d2


From d347fc59feefd91a796012892da57511787bb6d0 Mon Sep 17 00:00:00 2001
From: "A.J. Shulman" <Shulman.aj@gmail.com>
Date: Thu, 10 Oct 2024 11:39:19 -0400
Subject: added new file header comments and fixed some error handling

---
 .../views/nodes/chatbot/agentsystem/prompts.ts     |  9 ++++++++-
 .../nodes/chatbot/chatboxcomponents/ChatBox.tsx    |  9 +++++++++
 .../chatbot/chatboxcomponents/MessageComponent.tsx |  9 +++++++++
 .../chatbot/chatboxcomponents/ProgressBar.tsx      |  8 ++++++++
 .../nodes/chatbot/response_parsers/AnswerParser.ts |  8 ++++++++
 .../response_parsers/StreamedAnswerParser.ts       |  8 ++++++++
 src/client/views/nodes/chatbot/tools/BaseTool.ts   |  8 ++++++++
 .../views/nodes/chatbot/vectorstore/Vectorstore.ts | 10 +++++++++-
 src/server/ApiManagers/AssistantManager.ts         | 22 ++++++++++------------
 9 files changed, 77 insertions(+), 14 deletions(-)

(limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')

diff --git a/src/client/views/nodes/chatbot/agentsystem/prompts.ts b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
index 01c30d444..f5aec3130 100644
--- a/src/client/views/nodes/chatbot/agentsystem/prompts.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/prompts.ts
@@ -1,4 +1,11 @@
-// prompts.ts
+/**
+ * @file prompts.ts
+ * @description This file contains functions that generate prompts for various AI tasks, including
+ * generating system messages for structured AI assistant interactions and summarizing document chunks.
+ * It defines prompt structures to ensure the AI follows specific guidelines for response formatting,
+ * tool usage, and citation rules, with a rigid structure in mind for tasks such as answering user queries
+ * and summarizing content from provided text chunks.
+ */
 
 import { Tool } from '../types/types';
 
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 613cb7078..44c231c87 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -1,3 +1,12 @@
+/**
+ * @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';
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/MessageComponent.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/MessageComponent.tsx
index 801becb64..d48f46963 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/MessageComponent.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/MessageComponent.tsx
@@ -1,3 +1,12 @@
+/**
+ * @file MessageComponentBox.tsx
+ * @description This file defines the MessageComponentBox component, which renders the content
+ * of an AssistantMessage. It supports rendering various message types such as grounded text,
+ * normal text, and follow-up questions. The component uses React and MobX for state management
+ * and includes functionality for handling citation and follow-up actions, as well as displaying
+ * agent processing information.
+ */
+
 import React, { useState } from 'react';
 import { observer } from 'mobx-react';
 import { AssistantMessage, Citation, MessageContent, PROCESSING_TYPE, ProcessingInfo, TEXT_TYPE } from '../types/types';
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ProgressBar.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ProgressBar.tsx
index b9fd08742..240862f8b 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ProgressBar.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ProgressBar.tsx
@@ -1,3 +1,11 @@
+/**
+ * @file ProgressBar.tsx
+ * @description This file defines the ProgressBar component, which displays a loading spinner
+ * to indicate progress during ongoing tasks or processing. The animation consists of two
+ * bouncing elements that create a pulsating effect, providing a visual cue for active progress.
+ * The component is styled using the accompanying `ProgressBar.scss` for smooth animation.
+ */
+
 import React from 'react';
 import './ProgressBar.scss';
 
diff --git a/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts b/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
index 1ac753790..ed78cc7cb 100644
--- a/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
+++ b/src/client/views/nodes/chatbot/response_parsers/AnswerParser.ts
@@ -1,3 +1,11 @@
+/**
+ * @file AnswerParser.ts
+ * @description This file defines the AnswerParser class, which processes structured XML-like responses
+ * from the AI system, parsing grounded text, normal text, citations, follow-up questions, and loop summaries.
+ * The parser converts the XML response into an AssistantMessage format, extracting key information like
+ * citations and processing steps for further use in the assistant's workflow.
+ */
+
 import { v4 as uuid } from 'uuid';
 import { ASSISTANT_ROLE, AssistantMessage, Citation, ProcessingInfo, TEXT_TYPE, getChunkType } from '../types/types';
 
diff --git a/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
index 4149f3da9..dbd568faa 100644
--- a/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
+++ b/src/client/views/nodes/chatbot/response_parsers/StreamedAnswerParser.ts
@@ -1,3 +1,11 @@
+/**
+ * @file StreamedAnswerParser.ts
+ * @description This file defines the StreamedAnswerParser class, which parses incoming character streams
+ * to extract grounded or normal text based on the tags found in the input stream. It maintains state
+ * between grounded text and normal text sections, handling buffered input and ensuring proper text formatting
+ * for AI assistant responses.
+ */
+
 enum ParserState {
     Outside,
     InGroundedText,
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts
index 10780617b..a77f567a5 100644
--- a/src/client/views/nodes/chatbot/tools/BaseTool.ts
+++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts
@@ -1,3 +1,11 @@
+/**
+ * @file BaseTool.ts
+ * @description This file defines the abstract BaseTool class, which serves as a blueprint
+ * for tool implementations in the AI assistant system. Each tool has a name, description,
+ * parameters, and citation rules. The BaseTool class provides a structure for executing actions
+ * and retrieving action rules for use within the assistant's workflow.
+ */
+
 import { Tool } from '../types/types';
 
 export abstract class BaseTool<T extends Record<string, unknown> = Record<string, unknown>> implements Tool<T> {
diff --git a/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts b/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
index 9575277f7..f96f55997 100644
--- a/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
+++ b/src/client/views/nodes/chatbot/vectorstore/Vectorstore.ts
@@ -1,3 +1,10 @@
+/**
+ * @file Vectorstore.ts
+ * @description This file defines the Vectorstore class, which integrates with Pinecone for vector-based document indexing and Cohere for text embeddings.
+ * It handles tasks such as AI document management, document chunking, and retrieval of relevant document sections based on user queries.
+ * The class supports adding documents to the vectorstore, managing document status, and querying Pinecone for document chunks matching a query.
+ */
+
 import { Index, IndexList, Pinecone, PineconeRecord, QueryResponse, RecordMetadata } from '@pinecone-database/pinecone';
 import { CohereClient } from 'cohere-ai';
 import { EmbedResponse } from 'cohere-ai/api';
@@ -128,7 +135,8 @@ export class Vectorstore {
                     }
                 }
                 if (!result) {
-                    throw new Error('no result received...'); // bcz: is this an Error?
+                    console.error('Error processing document.');
+                    return;
                 }
 
                 // Once completed, process the document and add it to the vectorstore.
diff --git a/src/server/ApiManagers/AssistantManager.ts b/src/server/ApiManagers/AssistantManager.ts
index b4ebb1eae..b7d4191ca 100644
--- a/src/server/ApiManagers/AssistantManager.ts
+++ b/src/server/ApiManagers/AssistantManager.ts
@@ -1,3 +1,13 @@
+/**
+ * @file AssistantManager.ts
+ * @description This file defines the AssistantManager class, responsible for managing various
+ * API routes related to the Assistant functionality. It provides features such as file handling,
+ * web scraping, and integration with third-party APIs like OpenAI and Google Custom Search.
+ * It also handles job tracking and progress reporting for tasks like document creation and web scraping.
+ * Utility functions for path manipulation and file operations are included, along with
+ * a mechanism for handling retry logic during API calls.
+ */
+
 import { Readability } from '@mozilla/readability';
 import axios from 'axios';
 import { spawn } from 'child_process';
@@ -76,12 +86,6 @@ export default class AssistantManager extends ApiManager {
      * @param register The registration method to register routes and handlers.
      */
     protected initialize(register: Registration): void {
-        // Initialize OpenAI API with client key
-        const openai = new OpenAI({ // bcz: is this needed?  variable is never used...
-            apiKey: process.env._CLIENT_OPENAI_KEY,
-            dangerouslyAllowBrowser: true,
-        }); // prettier-ignore
-
         // Initialize Google Custom Search API
         const customsearch = google.customsearch('v1');
 
@@ -107,7 +111,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error retrieving Wikipedia summary:', error);
                     res.status(500).send({
                         error: 'Error retrieving article summary from Wikipedia.',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know what the error type contains...
                     });
                 }
             },
@@ -140,7 +143,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error performing web search:', error);
                     res.status(500).send({
                         error: 'Failed to perform web search',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -199,7 +201,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error fetching the URL:', error);
                     res.status(500).send({
                         error: 'Failed to fetch the URL',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -241,7 +242,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error scraping website:', error);
                     res.status(500).send({
                         error: 'Failed to scrape website',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -272,7 +272,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error initiating document creation:', error);
                     res.status(500).send({
                         error: 'Failed to initiate document creation',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know wha tthe error type contains...
                     });
                 }
             },
@@ -430,7 +429,6 @@ export default class AssistantManager extends ApiManager {
                     console.error('Error creating CSV file:', error);
                     res.status(500).send({
                         error: 'Failed to create CSV file.',
-                        details: (error as { message: string }).message ?? error, // bcz: don't know what the error type contains...
                     });
                 }
             },
-- 
cgit v1.2.3-70-g09d2

