aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts')
-rw-r--r--src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts235
1 files changed, 235 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..0b83ff24f
--- /dev/null
+++ b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
@@ -0,0 +1,235 @@
+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: { 'What is photosynthesis?': 'The process by which plants make food.' },
+ 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: 'flashcard',
+ title: 'Respiration',
+ data: { 'What is respiration?': 'Conversion of oxygen and glucose to energy.' },
+ backgroundColor: '#00ff00',
+ width: 300,
+ height: 300,
+ },
+ {
+ doc_type: 'text',
+ title: 'Cell Structure',
+ data: 'Cells are the basic building blocks of all living organisms.',
+ width: 300,
+ height: 300,
+ },
+ ],
+ 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,
+ // example: [
+ // {
+ // doc_type: 'collection',
+ // title: 'Science Collection',
+ // data: [
+ // {
+ // doc_type: 'flashcard',
+ // title: 'Photosynthesis',
+ // data: { 'What is photosynthesis?': 'The process by which plants make food.' },
+ // width: 300,
+ // height: 300,
+ // backgroundColor: '#0000FF',
+ // },
+ // {
+ // 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: 'flashcard',
+ // title: 'Respiration',
+ // data: { 'What is respiration?': 'Conversion of oxygen and glucose to energy.' },
+ // width: 300,
+ // height: 300,
+ // },
+ // {
+ // doc_type: 'text',
+ // title: 'Cell Structure',
+ // data: 'Cells are the basic building blocks of all living organisms.',
+ // width: 300,
+ // height: 300,
+ // },
+ // ],
+ // width: 600,
+ // height: 600,
+ // },
+ // ],
+ // width: 600,
+ // height: 600,
+ // },
+ // ],
+ },
+ text: 'Provide text content as a plain string. Example: "This is a standalone text document."',
+ flashcard: 'A dictionary mapping the front to the back of the flashcard. Example: {"Question":"Answer"}',
+ flashcardDeck: 'A collection of flashcards under a common theme.',
+ image: 'A URL to an image. 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,
+ //["{"doc_type":"collection","title":"Science Topics","data":"[\\"{\\\\"doc_type\\\\":\\\\"text\\\\",\\\\"title\\\\":\\\\"Photosynthesis\\\\",\\\\"background_color\\\\":\\\\""#0000FF"\\\\",\\\\"data\\\\":\\\\"Photosynthesis is the process by which plants make food.\\\\",\\\\"width\\\\":300,\\\\"height\\\\":300}\\",\\"{\\\\"doc_type\\\\":\\\\"collection\\\\",\\\\"title\\\\":\\\\"Advanced Biology\\\\",\\\\"data\\\\":\\\\"[\\\\"{\\\\"doc_type\\\\":\\\\"flashcard\\\\",\\\\"title\\\\":\\\\"Respiration\\\\",\\\\"data\\\\":{\\\\"What is respiration?\\\\":\\\\"Conversion of oxygen and glucose to energy.\\\\"},\\\\"width\\\\":300,\\\\"height\\\\":300}\\",\\\\"{\\\\"doc_type\\\\":\\\\"text\\\\",\\\\"title\\\\":\\\\"Cell Structure\\\\",\\\\"data\\\\":\\\\"Cells are the basic building blocks of all living organisms.\\\\",\\\\"width\\\\":300,\\\\"height\\\\":300}\\"]\\\\",\\\\"width\\\\":600,\\\\"height\\\\":600}\\"]","width":600,"height":600}"]',
+
+ //["{"doc_type":"collection","title":"Science Topics","data":["{\\"doc_type\\":\\"text\\",\\"title\\":\\"Photosynthesis\\",\\"data\\":\\"Photosynthesis is the process by which plants make food.\\",\\"width\\":300,\\"height\\":300}","{\\"doc_type\\":\\"collection\\",\\"title\\":\\"Advanced Biology\\",\\"data\\":["{\\"doc_type\\":\\"flashcard\\",\\"title\\":\\"Respiration\\",\\"data\\":{\\"What is respiration?\\":\\"Conversion of oxygen and glucose to energy.\\"},\\"width\\":300,\\"height\\":300}","{\\"doc_type\\":\\"text\\",\\"title\\":\\"Cell Structure\\",\\"data\\":\\"Cells are the basic building blocks of all living organisms.\\",\\"width\\":300,\\"height\\":300}"],\\"width\\":600,\\"height\\":600}"],"width":600,"height":600}"]',
+ required: true,
+ },
+] as const;
+
+type CreateListDocToolParamsType = typeof createListDocToolParams;
+// type CreateDocToolParamsType = typeof createDocToolParams;
+
+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[]> {
+ /**
+ * loop through each collection calling the
+ */
+
+ try {
+ console.log('EXE' + args.docs);
+ const parsedDoc = JSON.parse(args.docs);
+ console.log('parsed' + parsedDoc);
+ parsedDoc.forEach(doc => {
+ // console.log('THIS DOC' + firstDoc);
+ // console.log(typeof firstDoc);
+ // const doc = JSON.parse(firstDoc);
+ // console.log('NEW DOC' + doc);
+ // console.log('TYPE' + doc['doc_type']);
+ // console.log('WIDTH' + doc['width']);
+ // console.log('HEIGHT' + doc['height']);
+
+ 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()
+ );
+ });
+ // this._addLinkedDoc(args.doc_type, args.data, { title: args.title, backgroundColor: args.background_color, text_fontColor: args.font_color }, uuidv4());
+ return [{ type: 'text', text: 'Created document.' }];
+ } catch (error) {
+ return [{ type: 'text', text: 'Error creating text document, ' + error }];
+ }
+ }
+}