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'; const standardOptions = ['title', 'backgroundColor']; /** * Description of document options and data field for each type. */ const documentTypesInfo: { [key in supportedDocumentTypes]: { options: string[]; dataDescription: string } } = { [supportedDocumentTypes.flashcard]: { options: [...standardOptions, 'fontColor', 'text_align'], dataDescription: 'an array of two strings. the first string contains a question, and the second string contains an answer', }, [supportedDocumentTypes.text]: { options: [...standardOptions, 'fontColor', 'text_align'], dataDescription: 'The text content of the document.', }, [supportedDocumentTypes.html]: { options: [], dataDescription: 'The HTML-formatted text content of the document.', }, [supportedDocumentTypes.equation]: { options: [...standardOptions, 'fontColor'], dataDescription: 'The equation content as a string.', }, [supportedDocumentTypes.functionplot]: { options: [...standardOptions, 'function_definition'], dataDescription: 'The function definition(s) for plotting. Provide as a string or array of function definitions.', }, [supportedDocumentTypes.dataviz]: { options: [...standardOptions, 'chartType'], dataDescription: 'A string of comma-separated values representing the CSV data.', }, [supportedDocumentTypes.notetaking]: { options: standardOptions, dataDescription: 'The initial content or structure for note-taking.', }, [supportedDocumentTypes.rtf]: { options: standardOptions, dataDescription: 'The rich text content in RTF format.', }, [supportedDocumentTypes.message]: { options: standardOptions, dataDescription: 'The message content of the document.', }, }; const createAnyDocumentToolParams = [ { name: 'document_type', type: 'string', description: `The type of the document to create. Supported types are: ${Object.values(supportedDocumentTypes).join(', ')}`, required: true, }, { name: 'data', type: 'string', description: 'The content or data of the document. The exact format depends on the document type.', required: true, }, { name: 'options', type: 'string', required: false, description: `A JSON string representing the document options. Available options depend on the document type. For example: ${Object.entries(documentTypesInfo).map( ([doc_type, info]) => ` - For '${doc_type}' documents, options include: ${info.options.join(', ')}`) .join('\n')}`, // prettier-ignore }, ] as const; type CreateAnyDocumentToolParamsType = typeof createAnyDocumentToolParams; export class CreateAnyDocumentTool extends BaseTool { private _addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions, id: string) => void; constructor(addLinkedDoc: (doc_type: supportedDocumentTypes, data: unknown, options: DocumentOptions, id: string) => void) { // prettier-ignore super( 'createAnyDocument', `Creates any type of document with the provided options and data. Supported document types are: ${Object.values(supportedDocumentTypes).join(', ')}. dataviz is a csv table tool, so for CSVs, use dataviz. Here are the options for each type: ${Object.entries(documentTypesInfo).map(([doc_type, info]) => ` ${info.dataDescription} ${info.options.map(option => ``).join('\n')} `).join('\n')} `, 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. Supported document types are: ${Object.values(supportedDocumentTypes).join(', ')}.` ); this._addLinkedDoc = addLinkedDoc; } async execute(args: ParametersType): Promise { try { const documentType = toLower(args.document_type) as unknown as supportedDocumentTypes; const info = documentTypesInfo[documentType]; if (info === undefined) { throw new Error(`Unsupported document type: ${documentType}. Supported types are: ${Object.values(supportedDocumentTypes).join(', ')}.`); } if (!args.data) { 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); return [{ type: 'text', text: `Created ${documentType} document with ID ${id}.` }]; } catch (error) { return [{ type: 'text', text: 'Error creating document: ' + (error as Error).message }]; } } }