aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts
blob: 829ab97a400c3002a4eeeee491c871b56ae8a805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
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: 'equation',
        title: 'quadratic',
        data: 'x^2 + y^2 = 3',
        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,
        type_collection: 'tree',
    },
    {
        doc_type: 'image',
        title: 'experiment',
        data: 'https://plus.unsplash.com/premium_photo-1694819488591-a43907d1c5cc?q=80&w=2628&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D',
        width: 300,
        height: 300,
    },
    {
        doc_type: 'deck',
        title: 'Chemistry',
        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: '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,
            },
        ],
        backgroundColor: '#00ff00',
        width: 600,
        height: 600,
    },
    {
        doc_type: 'web',
        title: 'Brown University Wikipedia',
        data: 'https://en.wikipedia.org/wiki/Brown_University',
        width: 300,
        height: 300,
    },
    {
        doc_type: 'simulation',
        title: 'Physics simulation',
        data: '',
        width: 300,
        height: 300,
    },
    {
        doc_type: 'comparison',
        title: 'WWI vs. WWII',
        data: [
            {
                doc_type: 'text',
                title: 'WWI',
                data: 'From 1914 to 1918, fighting took place across several continents, at sea and, for the first time, in the air.',
                width: 300,
                height: 300,
            },
            {
                doc_type: 'text',
                title: 'WWII',
                data: 'A devastating global conflict spanning from 1939 to 1945, saw the Allied powers fight against the Axis powers.',
                width: 300,
                height: 300,
            },
        ],
        width: 300,
        height: 300,
    },
    {
        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: 'web',
                title: 'Brown University Wikipedia',
                data: 'https://en.wikipedia.org/wiki/Brown_University',
                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,
                type_collection: 'freeform',
            },
        ],
        width: 600,
        height: 600,
        type_collection: 'freeform',
    },
];

// 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", "deck", "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.',
    deck: 'A decks data is an array of flashcards.',
    image: 'A URL to an image for data. Example: "https://example.com/image.jpg"',
    web: 'A URL to a webpage. Example: https://en.wikipedia.org/wiki/Brown_University',
    equation: 'Create an equation document, not a text document. Data is math equation.',
    noteboard: 'Create a noteboard document',
    comparison: 'Create a comparison document - in type_collection specify the type of collection: masonry, freeform, tree, carousel.',
    simulation: 'Create a simulation document',
    audio: 'A url to an audio recording. Example: ',
} 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,
    },
    {
        name: 'type_collection',
        type: 'string',
        description: 'Either freeform, card, carousel, 3d-carousel, multicolumn, multirow, linear, map, notetaking, schema, stacking, grid, tree, or masonry.',
        required: false,
    },
] 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 the user’s request. If the user requests a "dashboard," first call search tool and then generate a variety of document types individually, each with different content and color schemes. For example, create multiple individual documents like "text," "deck," "web," "image," "equation," and "comparison." Use decks instead of flashcards for dashboards. ' +
                'Do not nest all documents within a single collection unless explicitly requested by the user. Instead, create a set of independent documents with diverse document types. Each type should appear separately unless specified otherwise.',
            createListDocToolParams,
            'Use the "data" parameter for document content and include title, color, and document dimensions. Ensure web documents use URLs from the search tool if relevant. Each document in a dashboard should be unique and well-differentiated in type and content, without repetition of similar types in any single collection.',
            'When creating a dashboard, ensure that it consists of a broad range of document types. Include a variety of documents, such as text, web, deck, comparison, image, simulation, and equation documents, each with distinct titles and colors, following the user’s preferences. ' +
                'Do not overuse collections or nest all document types within a single collection; instead, represent document types individually. Use this example for reference: ' +
                finalJsonString +
                '. Which documents are created should be random with different numbers of each document type and different for each dashboard. Must use search tool before creating a dashboard.'
        );

        // super(
        //     'createDoc',
        //     'Creates one or more documents that best fit users request with input following the example below. Or creates a dashboard of many documents/collections with this as an example: ' + finalJsonString,
        //     createListDocToolParams,
        //     'Modify the data parameter and include title (and optionally color) for the document. Web doc data type must be url from search tool.',
        //     '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 the search tool to save information.' +
        //         'When user asks for dashboard, create many documents/collections with different colors and texts while listening to their preferences, after using search tool to create a dashboard.'
        // );
        // this._addLinkedDoc = addLinkedDoc;

        // super(
        //     'createDoc',
        //     'Creates one or more documents based on the user’s request. If the user asks for a "dashboard," generate multiple documents of different types, such as "text," "flashcard," "image," "web," "comparison," and "collection," and include no more than three web documents unless specified otherwise. For a single document request (like "a text document"), create only the requested document type, without extra templates. Follow the examples below to understand the structure of various document types and their required properties.',
        //     createListDocToolParams,
        //     'Use the "data" parameter to specify document content and include the title (and optionally colors) for each document. For web documents, use a URL from a search tool if applicable.' +
        //         'For dashboards, create a diverse array of documents including text, flashcard decks, images, comparisons, and collections, with a mix of colors and unique titles, adhering to the user’s preferences. Example structures are provided for both dashboards and individual document types.' +
        //         'Examples:' +
        //         finalJsonString,
        //     ''
        // );

        // super(
        //     'createDoc',
        //     'Creates one or more documents that best fit users request with input following the example below. Or creates a dashboard for the user; there must be different kinds of documents. If the user asks for "a" document, only make one since it is singular - not an additional template one or more documents.',
        //     createListDocToolParams,
        //     'Modify the data parameter and include title (and optionally color) for the document. ' +
        //         'Web doc data type must be url from search tool. ' +
        //         'Creates one or more documents represented by an array of strings with the provided content based on the instructions ' +
        //         docInstructions +
        //         //'. If user asks for dashboard, create many documents/collections, including text, web, image, flashcard, deck, comparison, and collection, after calling the search tool following this example but only have three websites max: ' +
        //         //'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 the search tool to save information. If use after the search tool, do not use than more than 3 websites unless specified if dashboard; have text documents, flashcards, and more documents.' +
        //         // 'When user asks for dashboard, create many documents/collections of different types, such as text, web, flashcard, with different colors and texts while listening to their preferences, after using search tool to create a dashboard. Documents, such as text and web, should not all be in a collection unless specified. Do not have too many websites, and all websites do not need to be in one collection. An example of a dashboard is ' +
        //         // finalJsonString
        //         '. If user asks for dashboard, first use search tool. Then create text, web, flashcard, collections, and comparison documents. There absolutely must be different kinds of documents.' +
        //         'Do not only have web documents for dashboard; have other kinds of documents. Follow this example: ' +
        //         finalJsonString
        // );
        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'],
                        type_collection: doc['type_collection'],
                        _layout_fitWidth: false,
                        _layout_autoHeight: true,
                    },
                    uuidv4()
                );
            });
            return [{ type: 'text', text: 'Created document.' }];
        } catch (error) {
            return [{ type: 'text', text: 'Error creating text document, ' + error }];
        }
    }
}