From cd43a88affe04634045a1fcbce7123c10141ec8c Mon Sep 17 00:00:00 2001 From: "A.J. Shulman" Date: Sun, 20 Oct 2024 15:01:14 -0400 Subject: changed to generic addLinkedDoc --- .../views/nodes/chatbot/agentsystem/Agent.ts | 7 ++-- .../nodes/chatbot/chatboxcomponents/ChatBox.tsx | 14 +++++-- src/client/views/nodes/chatbot/tools/BaseTool.ts | 2 +- .../views/nodes/chatbot/tools/CalculateTool.ts | 2 +- .../views/nodes/chatbot/tools/CreateCSVTool.ts | 2 +- .../nodes/chatbot/tools/CreateTextDocumentTool.ts | 12 +++--- .../views/nodes/chatbot/tools/DataAnalysisTool.ts | 2 +- .../views/nodes/chatbot/tools/GetDocsTool.ts | 2 +- src/client/views/nodes/chatbot/tools/NoTool.ts | 2 +- src/client/views/nodes/chatbot/tools/RAGTool.ts | 2 +- src/client/views/nodes/chatbot/tools/SearchTool.ts | 2 +- src/client/views/nodes/chatbot/tools/ToolTypes.ts | 46 ---------------------- .../nodes/chatbot/tools/WebsiteInfoScraperTool.ts | 2 +- .../views/nodes/chatbot/tools/WikipediaTool.ts | 2 +- src/client/views/nodes/chatbot/types/tool_types.ts | 46 ++++++++++++++++++++++ 15 files changed, 78 insertions(+), 67 deletions(-) delete mode 100644 src/client/views/nodes/chatbot/tools/ToolTypes.ts create mode 100644 src/client/views/nodes/chatbot/types/tool_types.ts (limited to 'src') diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts index df307bc21..9253175d5 100644 --- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts +++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts @@ -15,8 +15,9 @@ import { AgentMessage, AssistantMessage, Observation, PROCESSING_TYPE, Processin import { Vectorstore } from '../vectorstore/Vectorstore'; import { getReactPrompt } from './prompts'; import { BaseTool } from '../tools/BaseTool'; -import { Parameter, ParametersType } from '../tools/ToolTypes'; +import { Parameter, ParametersType } from '../types/tool_types'; import { CreateTextDocTool } from '../tools/CreateTextDocumentTool'; +import { DocumentOptions } from '../../../../documents/Documents'; dotenv.config(); @@ -55,7 +56,7 @@ export class Agent { history: () => string, csvData: () => { filename: string; id: string; text: string }[], addLinkedUrlDoc: (url: string, id: string) => void, - addLinkedTextDoc: (text_content: string, options: {}, id: string) => void, + addLinkedDoc: (doc_type: string, data: string, options: DocumentOptions, id: string) => void, createCSVInDash: (url: string, title: string, id: string, data: string) => void ) { // Initialize OpenAI client with API key from environment @@ -74,7 +75,7 @@ export class Agent { searchTool: new SearchTool(addLinkedUrlDoc), createCSV: new CreateCSVTool(createCSVInDash), noTool: new NoTool(), - createTextDoc: new CreateTextDocTool(addLinkedTextDoc), + createTextDoc: new CreateTextDocTool(addLinkedDoc), }; } diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx index 118d20153..98f242ebf 100644 --- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx +++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx @@ -33,6 +33,7 @@ import { Vectorstore } from '../vectorstore/Vectorstore'; import './ChatBox.scss'; import MessageComponentBox from './MessageComponent'; import { ProgressBar } from './ProgressBar'; +import { RichTextField } from '../../../../../fields/RichTextField'; dotenv.config(); @@ -89,7 +90,7 @@ export class ChatBox extends ViewBoxAnnotatableComponent() { this.vectorstore_id = StrCast(this.dataDoc.vectorstore_id); } this.vectorstore = new Vectorstore(this.vectorstore_id, this.retrieveDocIds); - this.agent = new Agent(this.vectorstore, this.retrieveSummaries, this.retrieveFormattedHistory, this.retrieveCSVData, this.addLinkedUrlDoc, this.createTextDocInDash, this.createCSVInDash); + this.agent = new Agent(this.vectorstore, this.retrieveSummaries, this.retrieveFormattedHistory, this.retrieveCSVData, this.addLinkedUrlDoc, this.createDocInDash, this.createCSVInDash); this.messagesRef = React.createRef(); // Reaction to update dataDoc when chat history changes @@ -418,8 +419,15 @@ export class ChatBox extends ViewBoxAnnotatableComponent() { * @param id The unique ID for the document. */ @action - createTextDocInDash = async (text_content: string, options: DocumentOptions, id: string) => { - const doc = DocCast(Docs.Create.TextDocument(text_content, options)); + createDocInDash = async (doc_type: string, data: string, options: DocumentOptions, id: string) => { + let doc; + switch (doc_type) { + case 'text': + doc = DocCast(Docs.Create.TextDocument(data, options)); + default: + doc = DocCast(Docs.Create.TextDocument(data, options)); + } + const linkDoc = Docs.Create.LinkDocument(this.Document, doc); LinkManager.Instance.addLink(linkDoc); diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts index b6091af6c..05ca83b26 100644 --- a/src/client/views/nodes/chatbot/tools/BaseTool.ts +++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { Parameter, ParametersType } from './ToolTypes'; +import { Parameter, ParametersType } from '../types/tool_types'; /** * @file BaseTool.ts diff --git a/src/client/views/nodes/chatbot/tools/CalculateTool.ts b/src/client/views/nodes/chatbot/tools/CalculateTool.ts index e96c9a98a..139ede8f0 100644 --- a/src/client/views/nodes/chatbot/tools/CalculateTool.ts +++ b/src/client/views/nodes/chatbot/tools/CalculateTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; const calculateToolParams = [ diff --git a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts index b321d98ba..2cc513d6c 100644 --- a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts +++ b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts @@ -1,7 +1,7 @@ import { BaseTool } from './BaseTool'; import { Networking } from '../../../../Network'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const createCSVToolParams = [ { diff --git a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts index fa978bdc3..fae78aa49 100644 --- a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts +++ b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts @@ -2,8 +2,9 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { DocumentOptions } from '../../../../documents/Documents'; +import { RTFCast, StrCast } from '../../../../../fields/Types'; const createTextDocToolParams = [ { @@ -35,9 +36,9 @@ const createTextDocToolParams = [ type CreateTextDocToolParamsType = typeof createTextDocToolParams; export class CreateTextDocTool extends BaseTool { - private _addLinkedTextDoc: (text_content: string, options: DocumentOptions, id: string) => void; + private _addLinkedDoc: (doc_type: string, data: string, options: DocumentOptions, id: string) => void; - constructor(addLinkedTextDoc: (text_content: string, options: DocumentOptions, id: string) => void) { + constructor(addLinkedDoc: (text_content: string, data: string, options: DocumentOptions, id: string) => void) { super( 'createTextDoc', 'Creates a text document with the provided content and title (and of specified other options if wanted)', @@ -45,12 +46,13 @@ export class CreateTextDocTool extends BaseTool { 'Provide the text content and title (and optionally color) for the document.', 'Creates a text document with the provided content and title (and of specified other options if wanted). Use if the user wants to create a textbox or text document of some sort. Can use after a search or other tool to save information.' ); - this._addLinkedTextDoc = addLinkedTextDoc; + this._addLinkedDoc = addLinkedDoc; } async execute(args: ParametersType): Promise { try { - this._addLinkedTextDoc(args.text_content, { title: args.title, backgroundColor: args.background_color, text_fontColor: args.font_color }, uuidv4()); + console.log(RTFCast(args.text_content)); + this._addLinkedDoc('text', args.text_content, { title: args.title, backgroundColor: args.background_color, text_fontColor: args.font_color }, uuidv4()); return [{ type: 'text', text: 'Created text document.' }]; } catch (error) { return [{ type: 'text', text: 'Error creating text document, ' + error }]; diff --git a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts index d9b75219d..97b9ee023 100644 --- a/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts +++ b/src/client/views/nodes/chatbot/tools/DataAnalysisTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; const dataAnalysisToolParams = [ diff --git a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts index 26756522c..4286e7ffe 100644 --- a/src/client/views/nodes/chatbot/tools/GetDocsTool.ts +++ b/src/client/views/nodes/chatbot/tools/GetDocsTool.ts @@ -1,5 +1,5 @@ import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { BaseTool } from './BaseTool'; import { DocServer } from '../../../../DocServer'; import { Docs } from '../../../../documents/Documents'; diff --git a/src/client/views/nodes/chatbot/tools/NoTool.ts b/src/client/views/nodes/chatbot/tools/NoTool.ts index a92e3fa23..5d652fd8d 100644 --- a/src/client/views/nodes/chatbot/tools/NoTool.ts +++ b/src/client/views/nodes/chatbot/tools/NoTool.ts @@ -1,6 +1,6 @@ import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const noToolParams = [] as const; diff --git a/src/client/views/nodes/chatbot/tools/RAGTool.ts b/src/client/views/nodes/chatbot/tools/RAGTool.ts index 482069f36..fcd93a07a 100644 --- a/src/client/views/nodes/chatbot/tools/RAGTool.ts +++ b/src/client/views/nodes/chatbot/tools/RAGTool.ts @@ -1,6 +1,6 @@ import { Networking } from '../../../../Network'; import { Observation, RAGChunk } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; import { Vectorstore } from '../vectorstore/Vectorstore'; import { BaseTool } from './BaseTool'; diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index 267dab6ff..03340aae5 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const searchToolParams = [ { diff --git a/src/client/views/nodes/chatbot/tools/ToolTypes.ts b/src/client/views/nodes/chatbot/tools/ToolTypes.ts deleted file mode 100644 index cc29d70f1..000000000 --- a/src/client/views/nodes/chatbot/tools/ToolTypes.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { Observation } from '../types/types'; -/** - * The `Parameter` type defines the structure of a parameter configuration. - */ -export type Parameter = { - // The type of the parameter; constrained to the types 'string', 'number', 'boolean', 'string[]', 'number[]' - readonly type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; - // The name of the parameter - readonly name: string; - // A description of the parameter - readonly description: string; - // Indicates whether the parameter is required - readonly required: boolean; - // (Optional) The maximum number of inputs (useful for array types) - readonly max_inputs?: number; -}; - -/** - * A utility type that maps string representations of types to actual TypeScript types. - * This is used to convert the `type` field of a `Parameter` into a concrete TypeScript type. - */ -type TypeMap = { - string: string; - number: number; - boolean: boolean; - 'string[]': string[]; - 'number[]': number[]; -}; - -/** - * The `ParamType` type maps a `Parameter`'s `type` field to the corresponding TypeScript type. - * If the `type` field matches a key in `TypeMap`, it returns the associated type. - * Otherwise, it returns `unknown`. - * @template P - A `Parameter` object. - */ -export type ParamType

= P['type'] extends keyof TypeMap ? TypeMap[P['type']] : unknown; - -/** - * The `ParametersType` type transforms an array of `Parameter` objects into an object type - * where each key is the parameter's name, and the value is the corresponding TypeScript type. - * This is used to define the types of the arguments passed to the `execute` method of a tool. - * @template P - An array of `Parameter` objects. - */ -export type ParametersType

> = { - [K in P[number] as K['name']]: ParamType; -}; diff --git a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts index f2e3863a6..ce659e344 100644 --- a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts +++ b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const websiteInfoScraperToolParams = [ { diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts index 966ca7708..f2dbf3cfd 100644 --- a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts +++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts @@ -2,7 +2,7 @@ import { v4 as uuidv4 } from 'uuid'; import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; -import { ParametersType } from './ToolTypes'; +import { ParametersType } from '../types/tool_types'; const wikipediaToolParams = [ { diff --git a/src/client/views/nodes/chatbot/types/tool_types.ts b/src/client/views/nodes/chatbot/types/tool_types.ts new file mode 100644 index 000000000..c1150534d --- /dev/null +++ b/src/client/views/nodes/chatbot/types/tool_types.ts @@ -0,0 +1,46 @@ +import { Observation } from './types'; +/** + * The `Parameter` type defines the structure of a parameter configuration. + */ +export type Parameter = { + // The type of the parameter; constrained to the types 'string', 'number', 'boolean', 'string[]', 'number[]' + readonly type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; + // The name of the parameter + readonly name: string; + // A description of the parameter + readonly description: string; + // Indicates whether the parameter is required + readonly required: boolean; + // (Optional) The maximum number of inputs (useful for array types) + readonly max_inputs?: number; +}; + +/** + * A utility type that maps string representations of types to actual TypeScript types. + * This is used to convert the `type` field of a `Parameter` into a concrete TypeScript type. + */ +type TypeMap = { + string: string; + number: number; + boolean: boolean; + 'string[]': string[]; + 'number[]': number[]; +}; + +/** + * The `ParamType` type maps a `Parameter`'s `type` field to the corresponding TypeScript type. + * If the `type` field matches a key in `TypeMap`, it returns the associated type. + * Otherwise, it returns `unknown`. + * @template P - A `Parameter` object. + */ +export type ParamType

= P['type'] extends keyof TypeMap ? TypeMap[P['type']] : unknown; + +/** + * The `ParametersType` type transforms an array of `Parameter` objects into an object type + * where each key is the parameter's name, and the value is the corresponding TypeScript type. + * This is used to define the types of the arguments passed to the `execute` method of a tool. + * @template P - An array of `Parameter` objects. + */ +export type ParametersType

> = { + [K in P[number] as K['name']]: ParamType; +}; -- cgit v1.2.3-70-g09d2