diff options
-rw-r--r-- | src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx | 45 | ||||
-rw-r--r-- | src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts | 95 |
2 files changed, 115 insertions, 25 deletions
diff --git a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx index 14fdd9b8d..7b7431bbe 100644 --- a/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx +++ b/src/client/views/nodes/chatbot/chatboxcomponents/ChatBox.tsx @@ -427,10 +427,35 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { // this.createIndivDocInDash(parsedDoc.doc_type, parsedDoc.data, parsedDoc.options, ''); // }); // }; + @action + private createCollectionWithChildren = async (data: string[]): Promise<Doc[]> => { + console.log('Creating collection with nested documents'); + + // Create an array of promises for each document + const childDocPromises = data.map(async doc => { + const parsedDoc = JSON.parse(doc); + + if (parsedDoc.doc_type !== 'collection') { + // Handle non-collection documents + return await this.whichDoc(parsedDoc.doc_type, parsedDoc.data, parsedDoc.options, parsedDoc.id); + } else { + // Recursively process collections + const nestedDocs = await this.createCollectionWithChildren(parsedDoc.data); + return nestedDocs; // This will return an array of Docs + } + }); + + // Await all child document creations concurrently + const nestedResults = await Promise.all(childDocPromises); + + // Flatten any nested arrays from recursive collection calls + const childDocs = nestedResults.flat(); + + return childDocs; + }; @action - createDocInDash = async (doc_type: string, data: string, options: DocumentOptions, id: string) => { - console.log('INDIV DOC' + doc_type); + whichDoc = async (doc_type: string, data: string, options: DocumentOptions, id: string): Promise<Doc> => { let doc; switch (doc_type) { case 'text': @@ -451,9 +476,12 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { case 'simulation': doc = DocCast(Docs.Create.SimulationDocument(options)); break; - case 'collection': - doc = DocCast(Docs.Create.FreeformDocument([], options)); + case 'collection': { + const par = JSON.parse(data); + const arr = await this.createCollectionWithChildren(par); + doc = DocCast(Docs.Create.FreeformDocument(arr, options)); break; + } case 'web': doc = DocCast(Docs.Create.WebDocument(data, options)); break; @@ -499,6 +527,15 @@ export class ChatBox extends ViewBoxAnnotatableComponent<FieldViewProps>() { default: doc = DocCast(Docs.Create.TextDocument(data, options)); } + return doc; + }; + + @action + createDocInDash = async (doc_type: string, data: string, options: DocumentOptions, id: string) => { + console.log('INDIV DOC' + doc_type); + + const doc = await this.whichDoc(doc_type, data, options, id); + console.log('DOC' + doc_type); const linkDoc = Docs.Create.LinkDocument(this.Document, doc); LinkManager.Instance.addLink(linkDoc); diff --git a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts index 992935b77..4c5d4dc7d 100644 --- a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts +++ b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts @@ -3,60 +3,104 @@ import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; import { ParametersType } from '../types/tool_types'; import { DocumentOptions } from '../../../../documents/Documents'; -import { RTFCast, StrCast } from '../../../../../fields/Types'; - const docInstructions = { - text: 'Provide text content as string, not dictionary', - flashcard: 'A string dictionary mapping front to back of flashcard that the document will display. Follow this example: data: {"What is photosynthesis?":"The process by which g…t and absorb water and nutrients from the soil."}', - image: 'Provide a real image url', - web: 'Only provide real url to be displayed, not text content', + collection: { + description: 'A recursive collection of documents. Each document can be a "text", "flashcard", "image", "web", or another "collection".', + 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, + }, + { + 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"', } as const; -// have recursive structure -// get array of all documents that each have their options -// (if its a collection) - const createDocToolParams = [ { name: 'data', - type: 'string', + type: 'any', // 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', + description: 'The type of the document. Options: "collection", "text", "flashcard", "image", "web".', required: true, }, { name: 'title', type: 'string', - description: 'The title of the document', + description: 'The title of the document.', required: true, }, { name: 'background_color', type: 'string', - description: 'The background color of the document as a hex 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', + description: 'The font color of the document as a hex string.', required: false, }, { name: 'width', type: 'number', - description: 'The height of the document as a number', + description: 'The width of the document in pixels.', required: true, }, { name: 'height', type: 'number', - description: 'The height of the document as a number', + description: 'The height of the document in pixels.', required: true, }, ] as const; @@ -64,11 +108,12 @@ const createDocToolParams = [ const createListDocToolParams = [ { name: 'docs', - type: 'string', // array of stringified documents + type: 'string', // Array of stringified JSON objects description: - 'docs is an array that contains stringified JSON objects representing different document types. Each item in the array is a stringified version of this: ' + + '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 + - 'Each document should be individually serialized (using JSON.stringify or equivalent) so that it fits within string[]. An example is ["{"data":"Plants are living organisms that belong to the kingdom Plantae.","doc_type":"text","title":"Introduction to Plants","width":300,"height":300}", "{"data":"Photosynthesis is the process by which plants make food.","type":"text","title":"Photosynthesis","width":300,"height":300}"]', + '. Example: ["{"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; @@ -93,6 +138,12 @@ export class CreateDocTool extends BaseTool<CreateListDocToolParamsType> { } 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); @@ -103,8 +154,10 @@ export class CreateDocTool extends BaseTool<CreateListDocToolParamsType> { 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['background_color'], text_fontColor: doc['font_color'] }, uuidv4()); + this._addLinkedDoc(doc['doc_type'], doc['data'], { title: doc['title'], backgroundColor: doc['background_color'], text_fontColor: doc['font_color'], _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.' }]; |