diff options
author | A.J. Shulman <Shulman.aj@gmail.com> | 2024-11-07 11:02:09 -0500 |
---|---|---|
committer | A.J. Shulman <Shulman.aj@gmail.com> | 2024-11-07 11:02:09 -0500 |
commit | ab6672a702986d9b22de4f2df7955a0297308cab (patch) | |
tree | 8f2be09c55fd4594a22256780bd1b20f96143adb /src | |
parent | 5d4e19ad5961e42b90f7bfc920ea80da6edc5089 (diff) |
trying to add a new create any doc tool
Diffstat (limited to 'src')
3 files changed, 163 insertions, 5 deletions
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx index fcbaf2e27..57d02a408 100644 --- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx +++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx @@ -401,15 +401,47 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { * @param id The unique ID for the document. */ @action - createDocInDash = async (doc_type: string, data: string, options: DocumentOptions, id: string) => { + createDocInDash = async (doc_type: string, data: string | undefined, options: DocumentOptions, id: string) => { let doc; - switch (doc_type) { + + switch (doc_type.toLowerCase()) { case 'text': - doc = DocCast(Docs.Create.TextDocument(data, options)); + doc = Docs.Create.TextDocument(data || '', options); + break; + case 'image': + doc = Docs.Create.ImageDocument(data || '', options); + break; + case 'pdf': + doc = Docs.Create.PdfDocument(data || '', options); + break; + case 'video': + doc = Docs.Create.VideoDocument(data || '', options); + break; + case 'audio': + doc = Docs.Create.AudioDocument(data || '', options); + break; + case 'web': + doc = Docs.Create.WebDocument(data || '', options); + break; + case 'equation': + doc = Docs.Create.EquationDocument(data || '', options); + break; + case 'functionplot': + case 'function_plot': + doc = Docs.Create.FunctionPlotDocument([], options); + break; + case 'dataviz': + case 'data_viz': + doc = Docs.Create.DataVizDocument(data || '', options); + break; + case 'chat': + doc = Docs.Create.ChatDocument(options); + break; + // Add more cases for other document types default: - doc = DocCast(Docs.Create.TextDocument(data, options)); + console.error('Unknown or unsupported document type:', doc_type); + return; } - 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 05ca83b26..8efba2d28 100644 --- a/src/client/views/nodes/chatbot/tools/BaseTool.ts +++ b/src/client/views/nodes/chatbot/tools/BaseTool.ts @@ -59,6 +59,7 @@ export abstract class BaseTool<P extends ReadonlyArray<Parameter>> { return { tool: this.name, description: this.description, + citationRules: this.citationRules, parameters: this.parameterRules.reduce( (acc, param) => { // Build an object for each parameter without the 'name' property, since it's used as the key diff --git a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts new file mode 100644 index 000000000..af0dcc79c --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts @@ -0,0 +1,125 @@ +import { v4 as uuidv4 } from 'uuid'; +import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType, TypeMap, Parameter } from '../types/tool_types'; +import { DocumentOptions, Docs } from '../../../../documents/Documents'; + +/** + * List of supported document types. + */ +const supportedDocumentTypes = [ + 'text', + 'image', + 'pdf', + 'video', + 'audio', + 'web', + 'map', + 'equation', + 'functionPlot', + 'dataViz', + 'chat', + // Add more document types as needed +]; + +/** + * Description of document options for each type. + */ +const documentOptionsDescription = { + text: ['title', 'backgroundColor', 'fontColor', 'text_align', 'layout', 'text_content'], + image: ['title', 'backgroundColor', 'width', 'height', 'layout'], + pdf: ['title', 'backgroundColor', 'width', 'height', 'layout'], + video: ['title', 'backgroundColor', 'width', 'height', 'layout'], + audio: ['title', 'backgroundColor', 'layout'], + web: ['title', 'backgroundColor', 'width', 'height', 'layout', 'url'], + // Include descriptions for other document types +}; + +const createAnyDocumentToolParams = [ + { + name: 'document_type', + type: 'string', + description: `The type of the document to create. Supported types are: ${supportedDocumentTypes.join(', ')}`, + required: true, + }, + { + name: 'data', + type: 'string', + description: 'The content or data of the document (e.g., text content, URL, etc.).', + required: false, + }, + { + name: 'options', + type: 'string', + description: `A JSON string representing the document options. Available options depend on the document type. For example, for 'text' documents, options include: ${documentOptionsDescription['text'].join(', ')}.`, + required: false, + }, +] as const; + +type CreateAnyDocumentToolParamsType = typeof createAnyDocumentToolParams; + +export class CreateAnyDocumentTool extends BaseTool<CreateAnyDocumentToolParamsType> { + private _addLinkedDoc: (doc_type: string, data: string | undefined, options: DocumentOptions, id: string) => void; + + constructor(addLinkedDoc: (doc_type: string, data: string | undefined, options: DocumentOptions, id: string) => void) { + super( + 'createAnyDocument', + `Creates any type of document with the provided options and data. Supported document types are: ${supportedDocumentTypes.join(', ')}.`, + createAnyDocumentToolParams, + 'Provide the document type, data, and options for the document. Options should be a valid JSON string containing the document options specific to the document type.', + 'Creates any type of document with the provided options and data.' + ); + this._addLinkedDoc = addLinkedDoc; + } + + async execute(args: ParametersType<CreateAnyDocumentToolParamsType>): Promise<Observation[]> { + try { + const documentType = args.document_type.toLowerCase(); + let options: DocumentOptions = {}; + + if (!supportedDocumentTypes.includes(documentType)) { + throw new Error(`Unsupported document type: ${documentType}. Supported types are: ${supportedDocumentTypes.join(', ')}.`); + } + + if (args.options) { + try { + options = JSON.parse(args.options as string) as DocumentOptions; + } catch (e) { + throw new Error('Options must be a valid JSON string.'); + } + } + + const data = args.data as string | undefined; + const id = uuidv4(); + + // Validate and set default options based on document type + switch (documentType) { + case 'text': + if (!data) { + throw new Error('Data is required for text documents.'); + } + options.title = options.title || 'New Text Document'; + break; + case 'image': + case 'pdf': + case 'video': + case 'audio': + case 'web': + if (!data) { + throw new Error(`Data (e.g., URL) is required for ${documentType} documents.`); + } + options.title = options.title || `New ${documentType.charAt(0).toUpperCase() + documentType.slice(1)} Document`; + break; + // Add cases and default options for other document types as needed + default: + break; + } + + this._addLinkedDoc(documentType, data, options, id); + + return [{ type: 'text', text: `Created ${documentType} document with ID ${id}.` }]; + } catch (error) { + return [{ type: 'text', text: 'Error creating document: ' + (error as Error).message }]; + } + } +} |