From daa72b906e3364c2b6a836533fc1980bb63ba303 Mon Sep 17 00:00:00 2001
From: "A.J. Shulman" <Shulman.aj@gmail.com>
Date: Fri, 16 Aug 2024 15:45:23 -0400
Subject: now shows thoughts in real time
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

next steps:
integrate everything with the AnswerParser
make sure citations work perfectly (right now clicking citations isn't perfect for urls and multiple citations for the same url source are generated—check examples for mistakes)
---
 src/client/views/nodes/ChatBox/ChatBox.tsx | 62 +++++++++++++++++++++++-------
 1 file changed, 48 insertions(+), 14 deletions(-)

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

diff --git a/src/client/views/nodes/ChatBox/ChatBox.tsx b/src/client/views/nodes/ChatBox/ChatBox.tsx
index 8d09cde1e..d38c71810 100644
--- a/src/client/views/nodes/ChatBox/ChatBox.tsx
+++ b/src/client/views/nodes/ChatBox/ChatBox.tsx
@@ -149,26 +149,50 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
 
         if (trimmedText) {
             try {
+                //Make everything go through the answer parser
+                //Pass in all the updates to the AnswrParser and it will create the assistant messasge that will be the current message including adding in the thoughts and also waiting for the asnwer and also showing tool progress
                 textInput.value = '';
-                runInAction(() => {
-                    this.history.push({ role: ASSISTANT_ROLE.USER, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: trimmedText, citation_ids: null }] });
-                    this.isLoading = true;
-                });
+                this.history.push({ role: ASSISTANT_ROLE.USER, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: trimmedText, citation_ids: null }] });
+                this.isLoading = true;
+                this.current_message = { role: ASSISTANT_ROLE.ASSISTANT, content: [], thoughts: [] };
+
+                let currentThought = '';
+
+                this.current_message?.thoughts?.push(currentThought);
+
+                const onUpdate = (update: string) => {
+                    const thoughtNumber = Number(update.match(/^THOUGHT(\d+):/)?.[1] ?? 0);
+                    const regex = /<thought>\s*([\s\S]*?)(?:<\/thought>|$)/;
+                    const match = update.match(regex);
+                    const currentThought = match ? match[1].trim() : '';
+                    //const numericPrefix = Number(update.match(/^\d+/)?.[0]);
+                    if (update.startsWith('THOUGHT')) {
+                        console.log('Thought:', currentThought, thoughtNumber);
+                        if (this.current_message?.thoughts) {
+                            if (this.current_message.thoughts.length <= thoughtNumber) {
+                                this.current_message.thoughts.push(currentThought);
+                            } else {
+                                this.current_message.thoughts[thoughtNumber] = currentThought;
+                            }
+                        }
+                        console.log('Thoughts:', this.current_message?.thoughts);
+                    }
+                };
 
-                const response = await this.agent.askAgent(trimmedText); // Use the chatbot to get the response
-                runInAction(() => {
-                    this.history.push(AnswerParser.parse(response));
-                });
+                const response = await this.agent.askAgent(trimmedText, 20, onUpdate);
+                const parsedAnswer = AnswerParser.parse(response);
+                parsedAnswer.thoughts = this.current_message?.thoughts;
+
+                if (this.current_message) {
+                    this.history.push(parsedAnswer);
+                    this.current_message = undefined;
+                }
                 this.dataDoc.data = JSON.stringify(this.history);
             } catch (err) {
                 console.error('Error:', err);
-                runInAction(() => {
-                    this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: 'Sorry, I encountered an error while processing your request.', citation_ids: null }] });
-                });
+                this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, content: [{ index: 0, type: TEXT_TYPE.NORMAL, text: 'Sorry, I encountered an error while processing your request.', citation_ids: null }] });
             } finally {
-                runInAction(() => {
-                    this.isLoading = false;
-                });
+                this.isLoading = false;
             }
         }
     };
@@ -416,6 +440,16 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
                         {this.history.map((message, index) => (
                             <MessageComponentBox key={index} message={message} index={index} onFollowUpClick={this.handleFollowUpClick} onCitationClick={this.handleCitationClick} updateMessageCitations={this.updateMessageCitations} />
                         ))}
+                        {!this.current_message ? null : (
+                            <MessageComponentBox
+                                key={this.history.length}
+                                message={this.current_message}
+                                index={this.history.length}
+                                onFollowUpClick={this.handleFollowUpClick}
+                                onCitationClick={this.handleCitationClick}
+                                updateMessageCitations={this.updateMessageCitations}
+                            />
+                        )}
                     </div>
                 </div>
                 <form onSubmit={this.askGPT} className="chat-form">
-- 
cgit v1.2.3-70-g09d2

