aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/ChatBox.tsx
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/views/nodes/ChatBox/ChatBox.tsx')
-rw-r--r--src/client/views/nodes/ChatBox/ChatBox.tsx96
1 files changed, 61 insertions, 35 deletions
diff --git a/src/client/views/nodes/ChatBox/ChatBox.tsx b/src/client/views/nodes/ChatBox/ChatBox.tsx
index 2283aad56..73f35f501 100644
--- a/src/client/views/nodes/ChatBox/ChatBox.tsx
+++ b/src/client/views/nodes/ChatBox/ChatBox.tsx
@@ -27,6 +27,7 @@ import { Vectorstore } from './vectorstore/VectorstoreUpload';
import { DocumentView } from '../DocumentView';
import { CollectionFreeFormDocumentView } from '../CollectionFreeFormDocumentView';
import { CollectionFreeFormView } from '../../collections/collectionFreeForm';
+import { ChatBot } from './ChatBot';
@observer
export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
@@ -42,6 +43,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
private documents: AI_Document[] = [];
private _oldWheel: any;
private vectorstore: Vectorstore;
+ private chatbot: ChatBot; // Add the ChatBot instance
public static LayoutString(fieldKey: string) {
return FieldView.LayoutString(ChatBox, fieldKey);
@@ -55,6 +57,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
this.openai = this.initializeOpenAI();
this.getOtherDocs();
this.vectorstore = new Vectorstore();
+ this.chatbot = new ChatBot(this.vectorstore); // Initialize the ChatBot
reaction(
() => this.history.map((msg: AssistantMessage) => ({ role: msg.role, text: msg.text, follow_up_questions: msg.follow_up_questions, citations: msg.citations })),
@@ -65,7 +68,11 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
}
getOtherDocs = async () => {
- const visible_docs = (CollectionFreeFormDocumentView.from(this._props.DocumentView?.())?._props.parent as CollectionFreeFormView)?.childDocs.filter(doc => doc != this.Document);
+ const visible_docs = (CollectionFreeFormDocumentView.from(this._props.DocumentView?.())?._props.parent as CollectionFreeFormView)?.childDocs
+ .filter(doc => doc != this.Document)
+ .map(d => DocCast(d?.annotationOn, d))
+ .filter(d => d);
+
console.log('All Docs:', visible_docs);
visible_docs?.forEach(async doc => {
@@ -121,17 +128,39 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
runInAction(() => {
this.history.push({ role: ASSISTANT_ROLE.USER, text: trimmedText });
});
- const { response } = await Networking.PostToServer('/askAgent', { input: trimmedText });
+ this.isLoading = true;
+ const response = await this.chatbot.ask(trimmedText); // Use the chatbot to get the response
runInAction(() => {
- this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, text: response });
+ this.history.push(this.parseAssistantResponse(response));
});
this.dataDoc.data = JSON.stringify(this.history);
} catch (err) {
console.error('Error:', err);
+ runInAction(() => {
+ this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, text: 'Sorry, I encountered an error while processing your request.' });
+ });
+ } finally {
+ this.isLoading = false;
}
}
};
+ parseAssistantResponse(response: string): AssistantMessage {
+ const parser = new DOMParser();
+ const xmlDoc = parser.parseFromString(response, 'text/xml');
+ const answerElement = xmlDoc.querySelector('answer');
+ const followUpQuestionsElement = xmlDoc.querySelector('follow_up_questions');
+
+ const text = answerElement ? answerElement.textContent || '' : '';
+ const followUpQuestions = followUpQuestionsElement ? Array.from(followUpQuestionsElement.querySelectorAll('question')).map(q => q.textContent || '') : [];
+
+ return {
+ role: ASSISTANT_ROLE.ASSISTANT,
+ text,
+ follow_up_questions: followUpQuestions,
+ };
+ }
+
// @action
// uploadLinks = async (linkedDocs: Doc[]) => {
// if (this.isInitializing) {
@@ -241,42 +270,39 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
r?.addEventListener('wheel', this.onPassiveWheel, { passive: false });
}}>
<div className="messages">
- {
- //this.history.map((message, index) => (
- // <MessageComponent
- // key={index}
- // message={message}
- // toggleToolLogs={this.toggleToolLogs}
- // expandedLogIndex={this.expandedLogIndex}
- // index={index}
- // showModal={this.showModal}
- // goToLinkedDoc={() => {}}
- // setCurrentFile={this.setCurrentFile}
- // onFollowUpClick={this.handleFollowUpClick}
- // />
- //)
- //)
- }
- {
- //!this.current_message ? null : (
- // <MessageComponent
- // key={this.history.length}
- // message={this.current_message}
- // toggleToolLogs={this.toggleToolLogs}
- // expandedLogIndex={this.expandedLogIndex}
- // index={this.history.length}
- // showModal={this.showModal}
- // goToLinkedDoc={() => {}}
- // setCurrentFile={this.setCurrentFile}
- // onFollowUpClick={this.handleFollowUpClick}
- // />
- //)
- }
+ {this.history.map((message, index) => (
+ <MessageComponent
+ key={index}
+ message={message}
+ toggleToolLogs={this.toggleToolLogs}
+ expandedLogIndex={this.expandedScratchpadIndex}
+ index={index}
+ showModal={() => {}} // Implement this method if needed
+ goToLinkedDoc={() => {}} // Implement this method if needed
+ setCurrentFile={() => {}} // Implement this method if needed
+ onFollowUpClick={this.handleFollowUpClick}
+ />
+ ))}
+ {this.current_message && (
+ <MessageComponent
+ key={this.history.length}
+ message={this.current_message}
+ toggleToolLogs={this.toggleToolLogs}
+ expandedLogIndex={this.expandedScratchpadIndex}
+ index={this.history.length}
+ showModal={() => {}} // Implement this method if needed
+ goToLinkedDoc={() => {}} // Implement this method if needed
+ setCurrentFile={() => {}} // Implement this method if needed
+ onFollowUpClick={this.handleFollowUpClick}
+ />
+ )}
</div>
</div>
<form onSubmit={this.askGPT} className="chat-form">
<input type="text" name="messageInput" autoComplete="off" placeholder="Type a message..." value={this.inputValue} onChange={e => (this.inputValue = e.target.value)} />
- <button type="submit">Send</button>
+ <button type="submit" disabled={this.isLoading}>
+ {this.isLoading ? 'Thinking...' : 'Send'}
+ </button>
</form>
</div>
/** </MathJaxContext> **/