aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/ChatBox/ChatBox.tsx
diff options
context:
space:
mode:
authorA.J. Shulman <Shulman.aj@gmail.com>2024-07-11 10:21:56 -0400
committerA.J. Shulman <Shulman.aj@gmail.com>2024-07-11 10:21:56 -0400
commite0e4871224e626240dc899c653cd0eb9f54c3693 (patch)
tree36067e21ee81d47977e971e8f9ebced9c7ebeef2 /src/client/views/nodes/ChatBox/ChatBox.tsx
parentf82343158f423fdca136a639ec61c1e8d93b5467 (diff)
working with new documents
Diffstat (limited to 'src/client/views/nodes/ChatBox/ChatBox.tsx')
-rw-r--r--src/client/views/nodes/ChatBox/ChatBox.tsx68
1 files changed, 37 insertions, 31 deletions
diff --git a/src/client/views/nodes/ChatBox/ChatBox.tsx b/src/client/views/nodes/ChatBox/ChatBox.tsx
index 2ce1ebdd2..64ab2888b 100644
--- a/src/client/views/nodes/ChatBox/ChatBox.tsx
+++ b/src/client/views/nodes/ChatBox/ChatBox.tsx
@@ -1,4 +1,4 @@
-import { action, makeObservable, observable, observe, reaction, runInAction } from 'mobx';
+import { action, computed, makeObservable, observable, observe, reaction, runInAction } from 'mobx';
import { observer } from 'mobx-react';
import OpenAI, { ClientOptions } from 'openai';
import * as React from 'react';
@@ -18,6 +18,7 @@ import { CollectionFreeFormDocumentView } from '../CollectionFreeFormDocumentVie
import { CollectionFreeFormView } from '../../collections/collectionFreeForm';
import { Agent } from './Agent';
import dotenv from 'dotenv';
+import { DocData } from '../../../../fields/DocSymbols';
dotenv.config();
@observer
@@ -30,7 +31,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
@observable expandedScratchpadIndex: number | null = null;
@observable linked_docs_to_add: Doc[] = [];
@observable inputValue: string = '';
+ @observable private _visibleDocs: Doc[] = [];
private openai: OpenAI;
+ private vectorstore_id: string;
private documents: AI_Document[] = [];
private _oldWheel: any;
private vectorstore: Vectorstore;
@@ -46,7 +49,6 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
this.openai = this.initializeOpenAI();
this.history = [{ role: ASSISTANT_ROLE.ASSISTANT, text: 'Welcome to the Document Analyser Assistant! Link a document or ask questions to get started.' }];
this.openai = this.initializeOpenAI();
- this.getOtherDocs();
this.vectorstore = new Vectorstore();
this.agent = new Agent(this.vectorstore); // Initialize the Agent
@@ -59,22 +61,20 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
}
@action
- getOtherDocs = async () => {
- 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);
-
+ addAIDocs = async (visible_docs: Doc[]) => {
console.log('All Docs:', visible_docs);
-
visible_docs?.forEach(async doc => {
- const local_file_path: string = CsvCast(doc.data, PDFCast(doc.data)).url?.pathname;
- if (local_file_path) {
- const { document_json } = await Networking.PostToServer('/createDocument', { file_path: local_file_path });
- const ai_document: AI_Document = convertToAIDocument(document_json);
- this.documents.push(ai_document);
- await this.vectorstore.addDocument(ai_document);
- doc['ai_document'] = document_json;
+ if (doc[DocData].ai_document) {
+ this.documents.push(convertToAIDocument(JSON.parse(StrCast(doc[DocData].ai_document))));
+ } else {
+ const local_file_path: string = CsvCast(doc.data, PDFCast(doc.data))?.url?.pathname;
+ if (local_file_path) {
+ const { document_json } = await Networking.PostToServer('/createDocument', { file_path: local_file_path });
+ const ai_document: AI_Document = convertToAIDocument(document_json);
+ this.documents.push(ai_document);
+ await this.vectorstore.addDocument(ai_document);
+ doc[DocData].ai_document = JSON.stringify(document_json);
+ }
}
});
this.isInitializing = false;
@@ -120,8 +120,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
textInput.value = '';
runInAction(() => {
this.history.push({ role: ASSISTANT_ROLE.USER, text: trimmedText });
+ this.isLoading = true;
});
- this.isLoading = true;
+
const response = await this.agent.askAgent(trimmedText); // Use the chatbot to get the response
runInAction(() => {
this.history.push(this.parseAssistantResponse(response));
@@ -133,7 +134,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
this.history.push({ role: ASSISTANT_ROLE.ASSISTANT, text: 'Sorry, I encountered an error while processing your request.' });
});
} finally {
- this.isLoading = false;
+ runInAction(() => {
+ this.isLoading = false;
+ });
}
}
};
@@ -236,20 +239,15 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
}
}
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 => this.linked_docs_to_add.push(...linked.filter(linkedDoc => !this.linked_docs_to_add.includes(linkedDoc)))
+ () => this.visibleDocs,
+ visibleDocs => {
+ this._visibleDocs = visibleDocs;
+ }
);
-
observe(
// right now this skips during initialization which is necessary because it would be blank
// However, it will upload the same link twice when it is
- this.linked_docs_to_add,
+ this._visibleDocs,
change => {
// observe pushes/splices on a user link DB 'data' field (should only happen for local changes)
switch (change.type as any) {
@@ -257,9 +255,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
if ((change as any).addedCount > 0) {
// maybe check here if its already in the urls datadoc array so doesn't add twice
console.log((change as any).added as Doc[]);
- ((change as any).added as Doc[]).forEach(doc => {
- this.uploadNewDocument(doc);
- });
+ this.addAIDocs((change as any).added as Doc[]);
}
// (change as any).removed.forEach((link: any) => remLinkFromDoc(toRealField(link)));
break;
@@ -269,6 +265,16 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
},
true
);
+ runInAction(() => {
+ if (!this._visibleDocs.length) {
+ this.isInitializing = false;
+ }
+ });
+ }
+
+ @computed
+ get visibleDocs() {
+ return (CollectionFreeFormDocumentView.from(this._props.DocumentView?.())?._props.parent as CollectionFreeFormView)?.childDocs.filter(doc => doc != this.Document) ?? [];
}
@action