aboutsummaryrefslogtreecommitdiff
path: root/src
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
parent9cee38a2bceb2871cf3fda32e0df18d14f8c8c8e (diff)
more cleanup of createDoc and chatbox
Diffstat (limited to 'src')
-rw-r--r--src/client/views/nodes/chatbot/agentsystem/Agent.ts9
-rw-r--r--src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx30
-rw-r--r--src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts11
-rw-r--r--src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts18
4 files changed, 31 insertions, 37 deletions
diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
index 13fdac7f5..215e450b5 100644
--- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts
+++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts
@@ -2,13 +2,11 @@ import dotenv from 'dotenv';
import { XMLBuilder, XMLParser } from 'fast-xml-parser';
import { escape } from 'lodash'; // Imported escape from lodash
import OpenAI from 'openai';
-import { ChatCompletionMessageParam } from 'openai/resources';
-import { DocumentOptions } from '../../../../documents/Documents';
import { AnswerParser } from '../response_parsers/AnswerParser';
import { StreamedAnswerParser } from '../response_parsers/StreamedAnswerParser';
import { BaseTool } from '../tools/BaseTool';
import { CalculateTool } from '../tools/CalculateTool';
-import { CreateDocTool, supportedDocumentTypes } from '../tools/CreateDocumentTool';
+import { CreateDocTool } from '../tools/CreateDocumentTool';
import { DataAnalysisTool } from '../tools/DataAnalysisTool';
import { NoTool } from '../tools/NoTool';
import { SearchTool } from '../tools/SearchTool';
@@ -16,6 +14,9 @@ import { Parameter, ParametersType, TypeMap } from '../types/tool_types';
import { AgentMessage, ASSISTANT_ROLE, AssistantMessage, Observation, PROCESSING_TYPE, ProcessingInfo, TEXT_TYPE } from '../types/types';
import { Vectorstore } from '../vectorstore/Vectorstore';
import { getReactPrompt } from './prompts';
+import { Doc } from '../../../../../fields/Doc';
+import { parsedDoc } from '../chatboxcomponents/ChatBox';
+import { ChatCompletionMessageParam } from 'openai/resources';
dotenv.config();
@@ -54,7 +55,7 @@ export class Agent {
history: () => string,
csvData: () => { filename: string; id: string; text: string }[],
addLinkedUrlDoc: (url: string, id: string) => void,
- addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions, id?: string) => void,
+ addLinkedDoc: (doc: parsedDoc) => Doc | undefined,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
createCSVInDash: (url: string, title: string, id: string, data: string) => void
) {
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,
diff --git a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts
index efc66880f..7ec39704a 100644
--- a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts
@@ -1,10 +1,10 @@
import { toLower } from 'lodash';
-import { v4 as uuidv4 } from 'uuid';
import { DocumentOptions } from '../../../../documents/Documents';
import { ParametersType } from '../types/tool_types';
import { Observation } from '../types/types';
import { BaseTool } from './BaseTool';
import { supportedDocumentTypes } from './CreateDocumentTool';
+import { Id } from '../../../../../fields/FieldSymbols';
const standardOptions = ['title', 'backgroundColor'];
/**
@@ -76,9 +76,9 @@ const createAnyDocumentToolParams = [
type CreateAnyDocumentToolParamsType = typeof createAnyDocumentToolParams;
export class CreateAnyDocumentTool extends BaseTool<CreateAnyDocumentToolParamsType> {
- private _addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions, id: string) => void;
+ private _addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions) => void;
- constructor(addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions, id: string) => void) {
+ constructor(addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions) => void) {
// prettier-ignore
super(
'createAnyDocument',
@@ -113,13 +113,12 @@ export class CreateAnyDocumentTool extends BaseTool<CreateAnyDocumentToolParamsT
throw new Error(`Data is required for ${documentType} documents. ${info.dataDescription}`);
}
- const id = uuidv4();
const options: DocumentOptions = !args.options ? {} : JSON.parse(args.options);
// Call the function to add the linked document (add default title that can be overriden if set in options)
- this._addLinkedDoc(documentType, args.data, { title: `New ${documentType.charAt(0).toUpperCase() + documentType.slice(1)} Document`, ...options }, id);
+ const doc = this._addLinkedDoc(documentType, args.data, { title: `New ${documentType.charAt(0).toUpperCase() + documentType.slice(1)} Document`, ...options });
- return [{ type: 'text', text: `Created ${documentType} document with ID ${id}.` }];
+ return [{ type: 'text', text: `Created ${documentType} document with ID ${doc?.[Id]}.` }];
} catch (error) {
return [{ type: 'text', text: 'Error creating document: ' + (error as Error).message }];
}
diff --git a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
index 07b515fea..28c0eb32e 100644
--- a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
+++ b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
@@ -1,8 +1,7 @@
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType } from '../types/tool_types';
-import { DocumentOptions } from '../../../../documents/Documents';
-import { OmitKeys } from '../../../../../ClientUtils';
+import { parsedDoc } from '../chatboxcomponents/ChatBox';
export enum supportedDocumentTypes {
flashcard = 'flashcard',
@@ -360,9 +359,9 @@ type CreateListDocToolParamsType = typeof createListDocToolParams;
// Tool class for creating documents
export class CreateDocTool extends BaseTool<CreateListDocToolParamsType> {
- private _addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions) => void;
+ private _addLinkedDoc: (doc: parsedDoc) => void;
- constructor(addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions) => void) {
+ constructor(addLinkedDoc: (doc: parsedDoc) => void) {
super(
'createDoc',
`Creates one or more documents that best fit the user’s request.
@@ -394,15 +393,8 @@ export class CreateDocTool extends BaseTool<CreateListDocToolParamsType> {
// Executes the tool logic for creating documents
async execute(args: ParametersType<CreateListDocToolParamsType>): Promise<Observation[]> {
try {
- const parsedDoc = JSON.parse(args.docs) as ({ doc_type: supportedDocumentTypes; data: unknown } & DocumentOptions)[];
- parsedDoc.forEach(
- doc =>
- this._addLinkedDoc(
- doc.doc_type,
- doc.data,
- {...OmitKeys(doc, ["data", "doc_type"]).omit, _layout_fitWidth: false, _layout_autoHeight: true}
- ) // prettier-ignore
- );
+ const parsedDocs = JSON.parse(args.docs) as parsedDoc[];
+ parsedDocs.forEach(doc => this._addLinkedDoc({ ...doc, _layout_fitWidth: false, _layout_autoHeight: true }));
return [{ type: 'text', text: 'Created document.' }];
} catch (error) {
return [{ type: 'text', text: 'Error creating text document, ' + error }];