From 98d0bba3e59ab7ec9dfbf5e6c9c58e6ac1d22ae3 Mon Sep 17 00:00:00 2001 From: "A.J. Shulman" Date: Thu, 17 Oct 2024 17:41:47 -0400 Subject: added create text doc tool with font color and background color and fixed no tool --- .../views/nodes/chatbot/agentsystem/Agent.ts | 7 ++- .../nodes/chatbot/chatboxcomponents/ChatBox.tsx | 32 ++++++++---- src/client/views/nodes/chatbot/tools/BaseTool.ts | 4 +- .../nodes/chatbot/tools/CreateTextDocumentTool.ts | 59 ++++++++++++++++++++++ src/client/views/nodes/chatbot/tools/SearchTool.ts | 3 +- src/client/views/nodes/chatbot/tools/ToolTypes.ts | 30 ----------- .../views/nodes/chatbot/tools/WikipediaTool.ts | 2 +- 7 files changed, 90 insertions(+), 47 deletions(-) create mode 100644 src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.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 34e7cf5ea..df307bc21 100644 --- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts +++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts @@ -15,7 +15,8 @@ import { AgentMessage, AssistantMessage, Observation, PROCESSING_TYPE, Processin import { Vectorstore } from '../vectorstore/Vectorstore'; import { getReactPrompt } from './prompts'; import { BaseTool } from '../tools/BaseTool'; -import { Parameter, ParametersType, Tool } from '../tools/ToolTypes'; +import { Parameter, ParametersType } from '../tools/ToolTypes'; +import { CreateTextDocTool } from '../tools/CreateTextDocumentTool'; dotenv.config(); @@ -54,6 +55,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, createCSVInDash: (url: string, title: string, id: string, data: string) => void ) { // Initialize OpenAI client with API key from environment @@ -71,7 +73,8 @@ export class Agent { websiteInfoScraper: new WebsiteInfoScraperTool(addLinkedUrlDoc), searchTool: new SearchTool(addLinkedUrlDoc), createCSV: new CreateCSVTool(createCSVInDash), - no_tool: new NoTool(), + noTool: new NoTool(), + createTextDoc: new CreateTextDocTool(addLinkedTextDoc), }; } diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx index 44c231c87..118d20153 100644 --- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx +++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx @@ -20,7 +20,7 @@ import { CsvCast, DocCast, PDFCast, RTFCast, StrCast } from '../../../../../fiel import { Networking } from '../../../../Network'; import { DocUtils } from '../../../../documents/DocUtils'; import { DocumentType } from '../../../../documents/DocumentTypes'; -import { Docs } from '../../../../documents/Documents'; +import { Docs, DocumentOptions } from '../../../../documents/Documents'; import { DocumentManager } from '../../../../util/DocumentManager'; import { LinkManager } from '../../../../util/LinkManager'; import { ViewBoxAnnotatableComponent } from '../../../DocComponent'; @@ -89,7 +89,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.createCSVInDash); + this.agent = new Agent(this.vectorstore, this.retrieveSummaries, this.retrieveFormattedHistory, this.retrieveCSVData, this.addLinkedUrlDoc, this.createTextDocInDash, this.createCSVInDash); this.messagesRef = React.createRef(); // Reaction to update dataDoc when chat history changes @@ -410,6 +410,23 @@ export class ChatBox extends ViewBoxAnnotatableComponent() { this.addCSVForAnalysis(doc, id); }; + /** + * Creates a text document in the dashboard and adds it for analysis. + * @param title The title of the doc. + * @param text_content The text of the document. + * @param options Other optional document options (e.g. color) + * @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)); + const linkDoc = Docs.Create.LinkDocument(this.Document, doc); + LinkManager.Instance.addLink(linkDoc); + + doc && this._props.addDocument?.(doc); + await DocumentManager.Instance.showDocument(doc, { willZoomCentered: true }, () => {}); + }; + /** * Event handler to manage citations click in the message components. * @param citation The citation object clicked by the user. @@ -709,17 +726,10 @@ export class ChatBox extends ViewBoxAnnotatableComponent() {
{this.history.map((message, index) => ( - + ))} {this.current_message && ( - + )}
diff --git a/src/client/views/nodes/chatbot/tools/BaseTool.ts b/src/client/views/nodes/chatbot/tools/BaseTool.ts index 58cd514d9..b6091af6c 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, Tool, ParametersType } from './ToolTypes'; +import { Parameter, ParametersType } from './ToolTypes'; /** * @file BaseTool.ts @@ -14,7 +14,7 @@ import { Parameter, Tool, ParametersType } from './ToolTypes'; * It is generic over a type parameter `P`, which extends `ReadonlyArray`. * This means `P` is a readonly array of `Parameter` objects that cannot be modified (immutable). */ -export abstract class BaseTool

> implements Tool

{ +export abstract class BaseTool

> { // The name of the tool (e.g., "calculate", "searchTool") name: string; // A description of the tool's functionality diff --git a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts new file mode 100644 index 000000000..fa978bdc3 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts @@ -0,0 +1,59 @@ +import { v4 as uuidv4 } from 'uuid'; +import { Networking } from '../../../../Network'; +import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType } from './ToolTypes'; +import { DocumentOptions } from '../../../../documents/Documents'; + +const createTextDocToolParams = [ + { + name: 'text_content', + type: 'string', + description: 'The text content that the document will display', + required: true, + }, + { + name: 'title', + type: 'string', + description: 'The title of the document', + required: true, + }, + { + name: 'background_color', + type: 'string', + description: 'The background color of the document as a hex string', + required: false, + }, + { + name: 'font_color', + type: 'string', + description: 'The font color of the document as a hex string', + required: false, + }, +] as const; + +type CreateTextDocToolParamsType = typeof createTextDocToolParams; + +export class CreateTextDocTool extends BaseTool { + private _addLinkedTextDoc: (text_content: string, options: DocumentOptions, id: string) => void; + + constructor(addLinkedTextDoc: (text_content: 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)', + createTextDocToolParams, + '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; + } + + async execute(args: ParametersType): Promise { + try { + this._addLinkedTextDoc(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/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index fd5144dd6..267dab6ff 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -44,9 +44,10 @@ export class SearchTool extends BaseTool { }); const data = results.map((result: { url: string; snippet: string }) => { const id = uuidv4(); + this._addLinkedUrlDoc(result.url, id); return { type: 'text', - text: `${result.url}${result.snippet}`, + text: `${result.url}${result.snippet}`, }; }); return data; diff --git a/src/client/views/nodes/chatbot/tools/ToolTypes.ts b/src/client/views/nodes/chatbot/tools/ToolTypes.ts index d47a38952..cc29d70f1 100644 --- a/src/client/views/nodes/chatbot/tools/ToolTypes.ts +++ b/src/client/views/nodes/chatbot/tools/ToolTypes.ts @@ -1,34 +1,4 @@ import { Observation } from '../types/types'; - -/** - * The `Tool` interface represents a generic tool in the system. - * It is generic over a type parameter `P`, which extends `ReadonlyArray`. - * @template P - An array of `Parameter` objects defining the tool's parameters. - */ -export interface Tool

> { - // The name of the tool (e.g., "calculate", "searchTool") - name: string; - // A description of the tool's functionality - description: string; - // An array of parameter definitions for the tool - parameterRules: P; - // Guidelines for how to handle citations when using the tool - citationRules: string; - // A brief summary of the tool's purpose - briefSummary: string; - /** - * Executes the tool's main functionality. - * @param args - The arguments for execution, with types inferred from `ParametersType

`. - * @returns A promise that resolves to an array of `Observation` objects. - */ - execute: (args: ParametersType

) => Promise; - /** - * Generates an action rule object that describes the tool's usage. - * @returns An object representing the tool's action rules. - */ - getActionRule: () => Record; -} - /** * The `Parameter` type defines the structure of a parameter configuration. */ diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts index 4fcffe2ed..966ca7708 100644 --- a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts +++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts @@ -38,7 +38,7 @@ export class WikipediaTool extends BaseTool { return [ { type: 'text', - text: ` ${text} `, + text: ` ${text} `, }, ]; } catch (error) { -- cgit v1.2.3-70-g09d2