diff options
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts')
-rw-r--r-- | src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts | 179 |
1 files changed, 179 insertions, 0 deletions
diff --git a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts new file mode 100644 index 000000000..b14a57779 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts @@ -0,0 +1,179 @@ +import { v4 as uuidv4 } from 'uuid'; +import { BaseTool } from './BaseTool'; +import { Observation } from '../types/types'; +import { ParametersType } from '../types/tool_types'; +import { DocumentOptions } from '../../../../documents/Documents'; + +const example = [ + { + doc_type: 'collection', + title: 'Science Collection', + data: [ + { + doc_type: 'flashcard', + title: 'Photosynthesis', + data: [ + { + doc_type: 'text', + title: 'Front Photosynthesis', + data: 'What is photosynthesis?', + width: 300, + height: 300, + }, + { + doc_type: 'text', + title: 'back_photosynthesis', + data: 'The process by which plants make food.', + width: 300, + height: 300, + }, + ], + backgroundColor: '#00ff00', + width: 300, + height: 300, + }, + { + doc_type: 'text', + title: 'Water Cycle', + data: 'The continuous movement of water on, above, and below the Earth’s surface.', + width: 300, + height: 300, + }, + { + doc_type: 'collection', + title: 'Advanced Biology', + data: [ + { + doc_type: 'text', + title: 'Cell Structure', + data: 'Cells are the basic building blocks of all living organisms.', + width: 300, + height: 300, + }, + ], + backgroundColor: '#00ff00', + width: 600, + height: 600, + }, + ], + width: 600, + height: 600, + }, +]; + +// Stringify the entire structure for transmission if needed +const finalJsonString = JSON.stringify(example); + +const docInstructions = { + collection: { + description: + 'A recursive collection of documents as a stringified array. Each document can be a "text", "flashcard", "image", "web", "image", "comparison", "equation", "noteboard", "simulation", "diagram", "map", "screengrab", "webcam", "button", or another "collection".', + example: finalJsonString, + }, + text: 'Provide text content as a plain string. Example: "This is a standalone text document."', + flashcard: 'Two text documents with content for the front and back.', + flashcardDeck: 'A collection of flashcards under a common theme.', + image: 'A URL to an image for data. Example: "https://example.com/image.jpg"', + web: 'A URL to a webpage. Example: "https://example.com"', + equation: 'Create a equation document.', + noteboard: 'Create a noteboard document', + comparison: 'Create a comparison document', + simulation: 'Create a simulation document', +} as const; + +const createDocToolParams = [ + { + name: 'data', + type: 'string', // Accepts either string or array, supporting individual and nested data + description: docInstructions, + required: true, + }, + { + name: 'doc_type', + type: 'string', + description: 'The type of the document. Options: "collection", "text", "flashcard", "image", "web".', + 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, + }, + { + name: 'width', + type: 'number', + description: 'The width of the document in pixels.', + required: true, + }, + { + name: 'height', + type: 'number', + description: 'The height of the document in pixels.', + required: true, + }, +] as const; + +const createListDocToolParams = [ + { + name: 'docs', + type: 'string', // Array of stringified JSON objects + description: + 'Array of documents in stringified JSON format. Each item in the array should be an individual stringified JSON object. Each document can be of type "text", "flashcard", "image", "web", or "collection" (for nested documents). ' + + 'Use this structure for nesting collections within collections. Each document should follow the structure in ' + + createDocToolParams + + '. Example: ' + + finalJsonString, + required: true, + }, +] as const; + +type CreateListDocToolParamsType = typeof createListDocToolParams; + +export class CreateDocTool extends BaseTool<CreateListDocToolParamsType> { + private _addLinkedDoc: (doc_type: string, data: string, options: DocumentOptions, id: string) => void; + + constructor(addLinkedDoc: (doc_type: string, data: string, options: DocumentOptions, id: string) => void) { + super( + 'createDoc', + 'Creates one or more documents that best fit users request', + createListDocToolParams, + 'Modify the data parameter and include title (and optionally color) for the document.', + 'Creates one or more documents represented by an array of strings with the provided content based on the instructions ' + + docInstructions + + 'Use if the user wants to create something that aligns with a document type in dash like a flashcard, flashcard deck/stack, or textbox or text document of some sort. Can use after a search or other tool to save information.' + ); + this._addLinkedDoc = addLinkedDoc; + } + + async execute(args: ParametersType<CreateListDocToolParamsType>): Promise<Observation[]> { + try { + console.log('EXE' + args.docs); + const parsedDoc = JSON.parse(args.docs); + console.log('parsed' + parsedDoc); + parsedDoc.forEach(doc => { + this._addLinkedDoc( + doc['doc_type'], + doc['data'], + { title: doc['title'], backgroundColor: doc['backgroundColor'], text_fontColor: doc['font_color'], _width: doc['width'], _height: doc['height'], _layout_fitWidth: false, _layout_autoHeight: true }, + uuidv4() + ); + }); + return [{ type: 'text', text: 'Created document.' }]; + } catch (error) { + return [{ type: 'text', text: 'Error creating text document, ' + error }]; + } + } +} |