aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
diff options
context:
space:
mode:
authorbobzel <zzzman@gmail.com>2025-02-10 10:54:48 -0500
committerbobzel <zzzman@gmail.com>2025-02-10 10:54:48 -0500
commit1a6a53eeca4eea46af2dbd3e0778a18497d7b3a8 (patch)
tree369e3a3f5027752065e81993822e88471669b338 /src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
parent9cee38a2bceb2871cf3fda32e0df18d14f8c8c8e (diff)
more cleanup of createDoc and chatbox
Diffstat (limited to 'src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx')
-rw-r--r--src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
index 47d4fb8a7..810e1793b 100644
--- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
+++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx
@@ -13,7 +13,7 @@ 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 { ClientUtils, OmitKeys } from '../../../../../ClientUtils';
import { Doc, DocListCast, Opt } from '../../../../../fields/Doc';
import { DocData, DocViews } from '../../../../../fields/DocSymbols';
import { CsvCast, DocCast, NumCast, PDFCast, RTFCast, StrCast } from '../../../../../fields/Types';
@@ -36,7 +36,8 @@ import { supportedDocumentTypes } from '../tools/CreateDocumentTool';
dotenv.config();
-type parsedDoc = { doc_type: string; id: string; data: unknown; title: string; width: number; height: number; backgroundColor: string };
+export type parsedDocData = { doc_type: string; data: unknown };
+export type parsedDoc = DocumentOptions & parsedDocData;
/**
* ChatBox is the main class responsible for managing the interaction between the user and the assistant,
* handling documents, and integrating with OpenAI for tasks such as document analysis, chat functionality,
@@ -400,13 +401,14 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
* @param id The unique ID for the document.
*/
@action
- private createCollectionWithChildren = (data: parsedDoc[], insideCol: boolean): Opt<Doc>[] =>
- data.map(doc => this.whichDoc(doc.doc_type, doc.data, { backgroundColor: doc.backgroundColor, _width: doc.width, _height: doc.height }, doc.id, insideCol));
+ private createCollectionWithChildren = (data: parsedDoc[], insideCol: boolean): Opt<Doc>[] => data.map(doc => this.whichDoc(doc, insideCol));
@action
- whichDoc = (doc_type: string, data: unknown, options: DocumentOptions, id: string, insideCol: boolean): Opt<Doc> => {
+ whichDoc = (doc: parsedDoc, insideCol: boolean): Opt<Doc> => {
+ const options = OmitKeys(doc, ['doct_type', 'data']).omit as DocumentOptions;
+ const data = (doc as parsedDocData).data;
const ndoc = (() => {
- switch (doc_type) {
+ switch (doc.doc_type) {
case supportedDocumentTypes.text: return Docs.Create.TextDocument(data as string, options);
case supportedDocumentTypes.comparison: return this.createComparison(data as parsedDoc[], options);
case supportedDocumentTypes.flashcard: return this.createFlashcard(data as parsedDoc[], options);
@@ -456,11 +458,10 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
* @param {string} doc_type - The type of document to create.
* @param {string} data - The data used to generate the document.
* @param {DocumentOptions} options - Configuration options for the document.
- * @param {string} id - Unique identifier for the document.
* @returns {Promise<void>} A promise that resolves once the document is created and displayed.
*/
@action
- createDocInDash = (doc_type: string, data: unknown, options: DocumentOptions /*, id: string */) => {
+ createDocInDash = (pdoc: parsedDoc) => {
const linkAndShowDoc = (doc: Opt<Doc>) => {
if (doc) {
LinkManager.Instance.addLink(Docs.Create.LinkDocument(this.Document, doc));
@@ -468,8 +469,9 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {});
}
};
- const doc = this.whichDoc(doc_type, data, options, uuidv4(), false);
+ const doc = this.whichDoc(pdoc, false);
if (doc) linkAndShowDoc(doc);
+ return doc;
};
/**
@@ -487,7 +489,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
this.createFlashcard(data, options);
} else {
data.forEach(doc => {
- const flashcardDoc = this.createFlashcard(doc.data as parsedDoc[] | string[], options);
+ const flashcardDoc = this.createFlashcard((doc as parsedDocData).data as parsedDoc[] | string[], options);
if (flashcardDoc) flashcardDeck.push(flashcardDoc);
});
}
@@ -520,8 +522,8 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
};
// Create front and back text documents
- const side1 = typeof front === 'string' ? Docs.Create.CenteredTextCreator('question', front as string, sideOptions) : this.whichDoc(front.doc_type, front.data, options, front.id, false);
- const side2 = typeof back === 'string' ? Docs.Create.CenteredTextCreator('answer', back as string, sideOptions) : this.whichDoc(back.doc_type, back.data, options, back.id, false);
+ const side1 = typeof front === 'string' ? Docs.Create.CenteredTextCreator('question', front as string, sideOptions) : this.whichDoc(front, false);
+ const side2 = typeof back === 'string' ? Docs.Create.CenteredTextCreator('answer', back as string, sideOptions) : this.whichDoc(back, false);
// Create the flashcard document with both sides
return Docs.Create.FlashcardDocument('flashcard', side1, side2, sideOptions);
@@ -537,8 +539,8 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() {
@action
createComparison = (doc: parsedDoc[], options: DocumentOptions) =>
Docs.Create.ComparisonDocument(options.title as string, {
- data_back: Docs.Create.TextDocument(doc[0].data as string, { backgroundColor: doc[0].backgroundColor, _width: doc[0].width, _height: doc[0].height }),
- data_front: Docs.Create.TextDocument(doc[1].data as string, { backgroundColor: doc[1].backgroundColor, _width: doc[1].width, _height: doc[1].height }),
+ data_back: this.whichDoc(doc[0], false),
+ data_front: this.whichDoc(doc[1], false),
_width: options._width,
_height: options._height || 300,
backgroundColor: options.backgroundColor,