diff options
Diffstat (limited to 'src/client/views/nodes/chatbot/tools')
14 files changed, 1885 insertions, 748 deletions
diff --git a/src/client/views/nodes/chatbot/tools/CodebaseSummarySearchTool.ts b/src/client/views/nodes/chatbot/tools/CodebaseSummarySearchTool.ts new file mode 100644 index 000000000..5fdc52375 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CodebaseSummarySearchTool.ts @@ -0,0 +1,75 @@ +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; +import { Vectorstore } from '../vectorstore/Vectorstore'; +import { BaseTool } from './BaseTool'; + +const codebaseSummarySearchToolParams = [ + { + name: 'query', + type: 'string[]', + description: 'HIGHLY detailed (MANY SENTENCES) descriptions of the code files you want to find in the codebase.', + required: true, + }, + { + name: 'top_k', + type: 'number', + description: 'Number of top matching files to return. Default is 5.', + required: false, + }, +] as const; + +type CodebaseSummarySearchToolParamsType = typeof codebaseSummarySearchToolParams; + +const codebaseSummarySearchToolInfo: ToolInfo<CodebaseSummarySearchToolParamsType> = { + name: 'codebaseSummarySearch', + description: 'Searches the Dash codebase for files that match a semantic query. Returns a list of the most relevant files with their summaries to help understand the codebase structure.', + citationRules: `When using the CodebaseSummarySearchTool: +1. Present results clearly, showing filepaths and their summaries +2. Use the file summaries to provide context about the codebase organization +3. The results can be used to identify relevant files for deeper inspection`, + parameterRules: codebaseSummarySearchToolParams, +}; + +export class CodebaseSummarySearchTool extends BaseTool<CodebaseSummarySearchToolParamsType> { + constructor(private vectorstore: Vectorstore) { + super(codebaseSummarySearchToolInfo); + } + + async execute(args: ParametersType<CodebaseSummarySearchToolParamsType>): Promise<Observation[]> { + console.log(`Executing codebase summary search with query: "${args.query}"`); + + // Use the vectorstore's searchFileSummaries method + const topK = args.top_k || 5; + const results: { filepath: string; summary: string; score?: number | undefined }[] = []; + for (const query of args.query) { + const result = await this.vectorstore.searchFileSummaries(query, topK); + results.push(...result); + } + + if (results.length === 0) { + return [ + { + type: 'text', + text: `No files matching the query "${args.query}" were found in the codebase.`, + }, + ]; + } + + // Format results as observations + const formattedResults: Observation[] = [ + { + type: 'text', + text: `Found ${results.length} file(s) matching the query "${args.query}":\n\n`, + }, + ]; + + results.forEach((result, index) => { + formattedResults.push({ + type: 'text', + text: `File #${index + 1}: ${result.filepath}\n` + `Relevance Score: ${result.score?.toFixed(4) || 'N/A'}\n` + `Summary: ${result.summary}\n\n`, + }); + }); + + return formattedResults; + } +} diff --git a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts b/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts deleted file mode 100644 index 754d230c8..000000000 --- a/src/client/views/nodes/chatbot/tools/CreateAnyDocTool.ts +++ /dev/null @@ -1,158 +0,0 @@ -import { toLower } from 'lodash'; -import { Doc } from '../../../../../fields/Doc'; -import { Id } from '../../../../../fields/FieldSymbols'; -import { DocumentOptions } from '../../../../documents/Documents'; -import { parsedDoc } from '../chatboxcomponents/ChatBox'; -import { ParametersType, ToolInfo } from '../types/tool_types'; -import { Observation } from '../types/types'; -import { BaseTool } from './BaseTool'; -import { supportedDocTypes } from './CreateDocumentTool'; - -const standardOptions = ['title', 'backgroundColor']; -/** - * Description of document options and data field for each type. - */ -const documentTypesInfo: { [key in supportedDocTypes]: { options: string[]; dataDescription: string } } = { - [supportedDocTypes.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', - }, - [supportedDocTypes.text]: { - options: [...standardOptions, 'fontColor', 'text_align'], - dataDescription: 'The text content of the document.', - }, - [supportedDocTypes.html]: { - options: [], - dataDescription: 'The HTML-formatted text content of the document.', - }, - [supportedDocTypes.equation]: { - options: [...standardOptions, 'fontColor'], - dataDescription: 'The equation content as a string.', - }, - [supportedDocTypes.functionplot]: { - options: [...standardOptions, 'function_definition'], - dataDescription: 'The function definition(s) for plotting. Provide as a string or array of function definitions.', - }, - [supportedDocTypes.dataviz]: { - options: [...standardOptions, 'chartType'], - dataDescription: 'A string of comma-separated values representing the CSV data.', - }, - [supportedDocTypes.notetaking]: { - options: standardOptions, - dataDescription: 'The initial content or structure for note-taking.', - }, - [supportedDocTypes.rtf]: { - options: standardOptions, - dataDescription: 'The rich text content in RTF format.', - }, - [supportedDocTypes.image]: { - options: standardOptions, - dataDescription: 'The image content as an image file URL.', - }, - [supportedDocTypes.pdf]: { - options: standardOptions, - dataDescription: 'the pdf content as a PDF file url.', - }, - [supportedDocTypes.audio]: { - options: standardOptions, - dataDescription: 'The audio content as a file url.', - }, - [supportedDocTypes.video]: { - options: standardOptions, - dataDescription: 'The video content as a file url.', - }, - [supportedDocTypes.message]: { - options: standardOptions, - dataDescription: 'The message content of the document.', - }, - [supportedDocTypes.diagram]: { - options: ['title', 'backgroundColor'], - dataDescription: 'diagram content as a text string in Mermaid format.', - }, - [supportedDocTypes.script]: { - options: ['title', 'backgroundColor'], - dataDescription: 'The compilable JavaScript code. Use this for creating scripts.', - }, -}; - -const createAnyDocumentToolParams = [ - { - name: 'document_type', - type: 'string', - description: `The type of the document to create. Supported types are: ${Object.values(supportedDocTypes).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; - -const createAnyDocToolInfo: ToolInfo<CreateAnyDocumentToolParamsType> = { - name: 'createAnyDocument', - description: - `Creates any type of document with the provided options and data. - Supported document types are: ${Object.values(supportedDocTypes).join(', ')}. - dataviz is a csv table tool, so for CSVs, use dataviz. Here are the options for each type: - <supported_document_types>` + - Object.entries(documentTypesInfo) - .map( - ([doc_type, info]) => - `<document_type name="${doc_type}"> - <data_description>${info.dataDescription}</data_description> - <options>` + - info.options.map(option => `<option>${option}</option>`).join('\n') + - `</options> - </document_type>` - ) - .join('\n') + - `</supported_document_types>`, - parameterRules: createAnyDocumentToolParams, - citationRules: 'No citation needed.', -}; - -export class CreateAnyDocumentTool extends BaseTool<CreateAnyDocumentToolParamsType> { - private _addLinkedDoc: (doc: parsedDoc) => Doc | undefined; - - constructor(addLinkedDoc: (doc: parsedDoc) => Doc | undefined) { - super(createAnyDocToolInfo); - this._addLinkedDoc = addLinkedDoc; - } - - async execute(args: ParametersType<CreateAnyDocumentToolParamsType>): Promise<Observation[]> { - try { - const documentType = toLower(args.document_type) as unknown as supportedDocTypes; - const info = documentTypesInfo[documentType]; - - if (info === undefined) { - throw new Error(`Unsupported document type: ${documentType}. Supported types are: ${Object.values(supportedDocTypes).join(', ')}.`); - } - - if (!args.data) { - throw new Error(`Data is required for ${documentType} documents. ${info.dataDescription}`); - } - - 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) - const doc = this._addLinkedDoc({ doc_type: documentType, data: args.data, title: `New ${documentType.charAt(0).toUpperCase() + documentType.slice(1)} Document`, ...options }); - - return [{ type: 'text', text: `Created ${documentType} document with ID ${doc?.[Id]}.` }]; - } catch (error) { - return [{ type: 'text', text: 'Error creating document: ' + (error as Error).message }]; - } - } -} diff --git a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts deleted file mode 100644 index 284879a4a..000000000 --- a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts +++ /dev/null @@ -1,497 +0,0 @@ -import { BaseTool } from './BaseTool'; -import { Observation } from '../types/types'; -import { Parameter, ParametersType, ToolInfo } from '../types/tool_types'; -import { parsedDoc } from '../chatboxcomponents/ChatBox'; -import { CollectionViewType } from '../../../../documents/DocumentTypes'; - -/** - * List of supported document types that can be created via text LLM. - */ -export enum supportedDocTypes { - flashcard = 'flashcard', - text = 'text', - html = 'html', - equation = 'equation', - functionplot = 'functionplot', - dataviz = 'dataviz', - notetaking = 'notetaking', - audio = 'audio', - video = 'video', - pdf = 'pdf', - rtf = 'rtf', - message = 'message', - collection = 'collection', - image = 'image', - deck = 'deck', - web = 'web', - comparison = 'comparison', - diagram = 'diagram', - script = 'script', -} -/** - * Tthe CreateDocTool class is responsible for creating - * documents of various types (e.g., text, flashcards, collections) and organizing them in a - * structured manner. The tool supports creating dashboards with diverse document types and - * ensures proper placement of documents without overlap. - */ - -// Example document structure for various document types -const example = [ - { - doc_type: supportedDocTypes.equation, - title: 'quadratic', - data: 'x^2 + y^2 = 3', - _width: 300, - _height: 300, - x: 0, - y: 0, - }, - { - doc_type: supportedDocTypes.collection, - title: 'Advanced Biology', - data: [ - { - doc_type: supportedDocTypes.text, - title: 'Cell Structure', - data: 'Cells are the basic building blocks of all living organisms.', - _width: 300, - _height: 300, - x: 500, - y: 0, - }, - ], - backgroundColor: '#00ff00', - _width: 600, - _height: 600, - x: 600, - y: 0, - type_collection: 'tree', - }, - { - doc_type: supportedDocTypes.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, - x: 600, - y: 300, - }, - { - doc_type: supportedDocTypes.deck, - title: 'Chemistry', - data: [ - { - doc_type: supportedDocTypes.flashcard, - title: 'Photosynthesis', - data: [ - { - doc_type: supportedDocTypes.text, - title: 'front_Photosynthesis', - data: 'What is photosynthesis?', - _width: 300, - _height: 300, - x: 100, - y: 600, - }, - { - doc_type: supportedDocTypes.text, - title: 'back_photosynthesis', - data: 'The process by which plants make food.', - _width: 300, - _height: 300, - x: 100, - y: 700, - }, - ], - backgroundColor: '#00ff00', - _width: 300, - _height: 300, - x: 300, - y: 1000, - }, - { - doc_type: supportedDocTypes.flashcard, - title: 'Photosynthesis', - data: [ - { - doc_type: supportedDocTypes.text, - title: 'front_Photosynthesis', - data: 'What is photosynthesis?', - _width: 300, - _height: 300, - x: 200, - y: 800, - }, - { - doc_type: supportedDocTypes.text, - title: 'back_photosynthesis', - data: 'The process by which plants make food.', - _width: 300, - _height: 300, - x: 100, - y: -100, - }, - ], - backgroundColor: '#00ff00', - _width: 300, - _height: 300, - x: 10, - y: 70, - }, - ], - backgroundColor: '#00ff00', - _width: 600, - _height: 600, - x: 200, - y: 800, - }, - { - doc_type: supportedDocTypes.web, - title: 'Brown University Wikipedia', - data: 'https://en.wikipedia.org/wiki/Brown_University', - _width: 300, - _height: 300, - x: 1000, - y: 2000, - }, - { - doc_type: supportedDocTypes.comparison, - title: 'WWI vs. WWII', - data: [ - { - doc_type: supportedDocTypes.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, - x: 100, - y: 100, - }, - { - doc_type: supportedDocTypes.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, - x: 100, - y: 100, - }, - ], - _width: 300, - _height: 300, - x: 100, - y: 100, - }, - { - doc_type: supportedDocTypes.collection, - title: 'Science Collection', - data: [ - { - doc_type: supportedDocTypes.flashcard, - title: 'Photosynthesis', - data: [ - { - doc_type: supportedDocTypes.text, - title: 'front_Photosynthesis', - data: 'What is photosynthesis?', - _width: 300, - _height: 300, - }, - { - doc_type: supportedDocTypes.text, - title: 'back_photosynthesis', - data: 'The process by which plants make food.', - _width: 300, - _height: 300, - }, - ], - backgroundColor: '#00ff00', - _width: 300, - _height: 300, - }, - { - doc_type: supportedDocTypes.web, - title: 'Brown University Wikipedia', - data: 'https://en.wikipedia.org/wiki/Brown_University', - _width: 300, - _height: 300, - x: 1100, - y: 1100, - }, - { - doc_type: supportedDocTypes.text, - title: 'Water Cycle', - data: 'The continuous movement of water on, above, and below the Earth’s surface.', - _width: 300, - _height: 300, - x: 1500, - y: 500, - }, - { - doc_type: supportedDocTypes.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, - x: 1100, - y: 500, - type_collection: 'stacking', - }, - ], - _width: 600, - _height: 600, - x: 500, - y: 500, - type_collection: 'carousel', - }, -]; - -// Stringify the entire structure for transmission if needed -const finalJsonString = JSON.stringify(example); - -const standardOptions = ['title', 'backgroundColor']; -/** - * Description of document options and data field for each type. - */ -const documentTypesInfo: { [key in supportedDocTypes]: { options: string[]; dataDescription: string } } = { - comparison: { - options: [...standardOptions, 'fontColor', 'text_align'], - dataDescription: 'an array of two documents of any kind that can be compared.', - }, - deck: { - options: [...standardOptions, 'fontColor', 'text_align'], - dataDescription: 'an array of flashcard docs', - }, - 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', - }, - text: { - options: [...standardOptions, 'fontColor', 'text_align'], - dataDescription: 'The text content of the document.', - }, - web: { - options: [], - dataDescription: 'A URL to a webpage. Example: https://en.wikipedia.org/wiki/Brown_University', - }, - html: { - options: [], - dataDescription: 'The HTML-formatted text content of the document.', - }, - equation: { - options: [...standardOptions, 'fontColor'], - dataDescription: 'The equation content represented as a MathML string.', - }, - functionplot: { - options: [...standardOptions, 'function_definition'], - dataDescription: 'The function definition(s) for plotting. Provide as a string or array of function definitions.', - }, - dataviz: { - options: [...standardOptions, 'chartType'], - dataDescription: 'A string of comma-separated values representing the CSV data.', - }, - notetaking: { - options: standardOptions, - dataDescription: 'An array of related text documents with small amounts of text.', - }, - rtf: { - options: standardOptions, - dataDescription: 'The rich text content in RTF format.', - }, - image: { - options: standardOptions, - dataDescription: `A url string that must end with '.png', '.jpeg', '.gif', or '.jpg'`, - }, - pdf: { - options: standardOptions, - dataDescription: 'the pdf content as a PDF file url.', - }, - audio: { - options: standardOptions, - dataDescription: 'The audio content as a file url.', - }, - video: { - options: standardOptions, - dataDescription: 'The video content as a file url.', - }, - message: { - options: standardOptions, - dataDescription: 'The message content of the document.', - }, - diagram: { - options: standardOptions, - dataDescription: 'diagram content as a text string in Mermaid format.', - }, - script: { - options: standardOptions, - dataDescription: 'The compilable JavaScript code. Use this for creating scripts.', - }, - collection: { - options: [...standardOptions, 'type_collection'], - dataDescription: 'A collection of Docs represented as an array.', - }, -}; - -// Parameters for creating individual documents -const createDocToolParams: { name: string; type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; description: string; required: boolean }[] = [ - { - name: 'data', - type: 'string', // Accepts either string or array, supporting individual and nested data - description: - 'the data that describes the Document contents. For collections this is an' + - `Array of documents in stringified JSON format. Each item in the array should be an individual stringified JSON object. ` + - `Creates any type of document with the provided options and data. Supported document types are: ${Object.keys(documentTypesInfo).join(', ')}. - dataviz is a csv table tool, so for CSVs, use dataviz. Here are the options for each type: - <supported_document_types>` + - Object.entries(documentTypesInfo) - .map( - ([doc_type, info]) => - `<document_type name="${doc_type}"> - <data_description>${info.dataDescription}</data_description> - <options>` + - info.options.map(option => `<option>${option}</option>`).join('\n') + - ` - </options> - </document_type>` - ) - .join('\n') + - `</supported_document_types> An example of the structure of a collection is:` + - finalJsonString, // prettier-ignore, - required: true, - }, - { - name: 'doc_type', - type: 'string', - description: `The type of the document. Options: ${Object.keys(documentTypesInfo).join(',')}.`, - required: true, - }, - { - name: 'title', - type: 'string', - description: 'The title of the document.', - required: true, - }, - { - name: 'x', - type: 'number', - description: 'The x location of the document; 0 <= x.', - required: true, - }, - { - name: 'y', - type: 'number', - description: 'The y location of the document; 0 <= y.', - required: true, - }, - { - name: 'backgroundColor', - type: 'string', - description: 'The background color of the document as a hex string.', - required: false, - }, - { - name: 'fontColor', - 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: `the visual style for a collection doc. Options include: ${Object.values(CollectionViewType).join(',')}.`, - required: false, - }, -] as const; - -type CreateDocToolParamsType = typeof createDocToolParams; - -const createDocToolInfo: ToolInfo<CreateDocToolParamsType> = { - name: 'createDoc', - description: `Creates one or more documents that best fit the user’s request. - If the user requests a "dashboard," first call the search tool and then generate a variety of document types individually, with absolutely a minimum of 20 documents - with two stacks of flashcards that are small and it should have a couple nested freeform collections of things, each with different content and color schemes. - For example, create multiple individual documents, including ${Object.keys(documentTypesInfo) - .map(t => '"' + t + '"') - .join(',')} - If the "doc_type" parameter is missing, set it to an empty string (""). - Use Decks instead of Flashcards for dashboards. Decks should have at least three flashcards. - Really think about what documents are useful to the user. If they ask for a dashboard about the skeletal system, include flashcards, as they would be helpful. - Arrange the documents in a grid layout, ensuring that the x and y coordinates are calculated so no documents overlap but they should be directly next to each other with 20 padding in between. - Take into account the width and height of each document, spacing them appropriately to prevent collisions. - Use a systematic approach, such as placing each document in a grid cell based on its order, where cell dimensions match the document dimensions plus a fixed margin for spacing. - 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. - 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, 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.`, - parameterRules: createDocToolParams, - citationRules: 'No citation needed.', -}; - -// Tool class for creating documents -export class CreateDocTool extends BaseTool< - { - name: string; - type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; - description: string; - required: boolean; - }[] -> { - private _addLinkedDoc: (doc: parsedDoc) => void; - - constructor(addLinkedDoc: (doc: parsedDoc) => void) { - super(createDocToolInfo); - this._addLinkedDoc = addLinkedDoc; - } - - override inputValidator(inputParam: ParametersType<readonly Parameter[]>) { - return !!inputParam.data; - } - // Executes the tool logic for creating documents - async execute( - args: ParametersType< - { - name: 'string'; - type: 'string' | 'number' | 'boolean' | 'string[]' | 'number[]'; - description: 'string'; - required: boolean; - }[] - > - ): Promise<Observation[]> { - try { - const parsedDocs = args instanceof Array ? args : Object.keys(args).length === 1 && 'data' in args ? JSON.parse(args.data as string) : [args]; - parsedDocs.forEach((pdoc: parsedDoc) => this._addLinkedDoc({ ...pdoc, _layout_fitWidth: false, _layout_autoHeight: true })); - return [{ type: 'text', text: 'Created document.' }]; - } catch (error) { - return [{ type: 'text', text: 'Error creating text document, ' + error }]; - } - } -} diff --git a/src/client/views/nodes/chatbot/tools/CreateLinksTool.ts b/src/client/views/nodes/chatbot/tools/CreateLinksTool.ts new file mode 100644 index 000000000..c2850a8ce --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateLinksTool.ts @@ -0,0 +1,68 @@ +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; +import { BaseTool } from './BaseTool'; +import { AgentDocumentManager } from '../utils/AgentDocumentManager'; + +const createLinksToolParams = [ + { + name: 'document_ids', + type: 'string[]', + description: 'List of document IDs to create links between. All documents will be linked to each other.', + required: true, + }, +] as const; + +type CreateLinksToolParamsType = typeof createLinksToolParams; + +const createLinksToolInfo: ToolInfo<CreateLinksToolParamsType> = { + name: 'createLinks', + description: 'Creates visual links between multiple documents in the dashboard. This allows related documents to be connected visually with lines that users can see.', + citationRules: 'No citation needed.', + parameterRules: createLinksToolParams, +}; + +export class CreateLinksTool extends BaseTool<CreateLinksToolParamsType> { + private _documentManager: AgentDocumentManager; + + constructor(documentManager: AgentDocumentManager) { + super(createLinksToolInfo); + this._documentManager = documentManager; + } + + async execute(args: ParametersType<CreateLinksToolParamsType>): Promise<Observation[]> { + try { + // Validate that we have at least 2 documents to link + if (args.document_ids.length < 2) { + return [{ type: 'text', text: 'Error: At least 2 document IDs are required to create links.' }]; + } + + // Validate that all documents exist + const missingDocIds = args.document_ids.filter(id => !this._documentManager.has(id)); + if (missingDocIds.length > 0) { + return [ + { + type: 'text', + text: `Error: The following document IDs were not found: ${missingDocIds.join(', ')}`, + }, + ]; + } + + // Create links between all documents with the specified relationship + const createdLinks = this._documentManager.addLinks(args.document_ids); + + return [ + { + type: 'text', + text: `Successfully created ${createdLinks.length} visual links between ${args.document_ids.length}.`, + }, + ]; + } catch (error) { + return [ + { + type: 'text', + text: `Error creating links: ${error instanceof Error ? error.message : String(error)}`, + }, + ]; + } + } +} diff --git a/src/client/views/nodes/chatbot/tools/CreateNewTool.ts b/src/client/views/nodes/chatbot/tools/CreateNewTool.ts new file mode 100644 index 000000000..1cc50a803 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateNewTool.ts @@ -0,0 +1,599 @@ +import { Observation } from '../types/types'; +import { Parameter, ParametersType, ToolInfo } from '../types/tool_types'; +import { BaseTool } from './BaseTool'; +import * as ts from 'typescript'; +import { v4 as uuidv4 } from 'uuid'; +import { Networking } from '../../../../Network'; + +// Forward declaration to avoid circular import +interface AgentLike { + registerDynamicTool(toolName: string, toolInstance: BaseTool<ReadonlyArray<Parameter>>): void; + notifyToolCreated(toolName: string, completeToolCode: string): void; +} + +const createNewToolParams = [ + { + name: 'toolName', + type: 'string', + description: 'The name of the new tool class (PascalCase) and filename. This will also be converted to lowercase for the action name.', + required: true, + }, + { + name: 'toolCode', + type: 'string', + description: + 'The complete TypeScript code for the new tool class. IMPORTANT: Provide this as a single string without any XML formatting. Do not break it into multiple lines or add any XML tags. The tool must extend BaseTool, implement an async execute method, and have proper parameter definitions. Use CDATA format if needed: <![CDATA[your code here]]>', + required: true, + }, + { + name: 'description', + type: 'string', + description: 'A brief description of what the tool does.', + required: true, + }, +] as const; + +type CreateNewToolParamsType = typeof createNewToolParams; + +const createNewToolInfo: ToolInfo<CreateNewToolParamsType> = { + name: 'createNewTool', + description: `Creates a new tool for the agent to use based on research done with the codebase search, file content, and filenames tools. The new tool will be instantly available for use in the current session and saved as a proper TypeScript file. + +IMPORTANT TOOL CREATION RULES: +1. Your tool will be created with proper imports adjusted for the dynamic subfolder location +2. Your tool MUST extend BaseTool with proper parameter type definition +3. Your tool MUST implement an async execute method that returns Promise<Observation[]> +4. Your tool MUST call super() with the proper tool info configuration object +5. CRITICAL: The toolInfo.name property MUST be lowercase and should match the action name you want to use +6. Follow this EXACT pattern (imports will be added automatically): + +\`\`\`typescript +const yourToolParams = [ + { + name: 'inputParam', + type: 'string', + description: 'Your parameter description', + required: true + } +] as const; + +type YourToolParamsType = typeof yourToolParams; + +const yourToolInfo: ToolInfo<YourToolParamsType> = { + name: 'yourtoolname', + description: 'Your tool description', + citationRules: 'No citation needed.', + parameterRules: yourToolParams +}; + +export class YourToolName extends BaseTool<YourToolParamsType> { + constructor() { + super(yourToolInfo); + } + + async execute(args: ParametersType<YourToolParamsType>): Promise<Observation[]> { + const { inputParam } = args; + // Your implementation here + return [{ type: 'text', text: 'Your result' }]; + } +} +\`\`\` + +EXAMPLE - Character Count Tool: + +\`\`\`typescript +const characterCountParams = [ + { + name: 'text', + type: 'string', + description: 'The text to count characters in', + required: true + } +] as const; + +type CharacterCountParamsType = typeof characterCountParams; + +const characterCountInfo: ToolInfo<CharacterCountParamsType> = { + name: 'charactercount', + description: 'Counts characters in text, excluding spaces', + citationRules: 'No citation needed.', + parameterRules: characterCountParams +}; + +export class CharacterCountTool extends BaseTool<CharacterCountParamsType> { + constructor() { + super(characterCountInfo); + } + + async execute(args: ParametersType<CharacterCountParamsType>): Promise<Observation[]> { + const { text } = args; + const count = text ? text.replace(/\\s/g, '').length : 0; + return [{ type: 'text', text: \`Character count (excluding spaces): \${count}\` }]; + } +} +\`\`\``, + citationRules: `No citation needed.`, + parameterRules: createNewToolParams, +}; + +/** + * This tool allows the agent to create new custom tools after researching the codebase. + * It validates the provided code, dynamically compiles it, and registers it with the + * Agent for immediate use. + */ +export class CreateNewTool extends BaseTool<CreateNewToolParamsType> { + // Reference to the dynamic tool registry in the Agent class + private dynamicToolRegistry: Map<string, BaseTool<ReadonlyArray<Parameter>>>; + private existingTools: Record<string, BaseTool<ReadonlyArray<Parameter>>>; + private agent?: AgentLike; + + constructor(toolRegistry: Map<string, BaseTool<ReadonlyArray<Parameter>>>, existingTools: Record<string, BaseTool<ReadonlyArray<Parameter>>> = {}, agent?: AgentLike) { + super(createNewToolInfo); + this.dynamicToolRegistry = toolRegistry; + this.existingTools = existingTools; + this.agent = agent; + } + + /** + * Validates TypeScript code for basic safety and correctness + * @param code The TypeScript code to validate + * @returns An object with validation result and any error messages + */ + private validateToolCode(code: string, toolName: string): { valid: boolean; errors: string[] } { + const errors: string[] = []; + + // Check for fundamental structure + if (!code.includes('extends BaseTool')) { + errors.push('Tool must extend BaseTool class'); + } + + if (!code.includes(`class ${toolName} extends`)) { + errors.push(`Tool class name must match the provided toolName: ${toolName}`); + } + + if (!code.includes('async execute(')) { + errors.push('Tool must implement an async execute method'); + } + + if (!code.includes('super(')) { + errors.push('Tool must call super() in constructor'); + } + + // Check if the tool exports the class correctly (should use export class) + if (!code.includes(`export class ${toolName}`)) { + errors.push(`Tools must export the class using: export class ${toolName}`); + } + + // Check if tool info has name property in lowercase + const nameMatch = code.match(/name\s*:\s*['"]([^'"]+)['"]/); + if (nameMatch && nameMatch[1]) { + const toolInfoName = nameMatch[1]; + if (toolInfoName !== toolInfoName.toLowerCase()) { + errors.push(`Tool info name property must be lowercase. Found: "${toolInfoName}", should be "${toolInfoName.toLowerCase()}"`); + } + } else { + errors.push('Tool info must have a name property'); + } + + // Check for type definition - make this more flexible + const hasTypeDefinition = code.includes(`type ${toolName}ParamsType`) || code.includes(`type ${toolName.toLowerCase()}ParamsType`) || code.includes('ParamsType = typeof'); + if (!hasTypeDefinition) { + errors.push(`Tool must define a type for parameters like: type ${toolName}ParamsType = typeof ${toolName.toLowerCase()}Params`); + } + + // Check for ToolInfo type annotation - make this more flexible + const hasToolInfoType = code.includes(`ToolInfo<${toolName}ParamsType>`) || code.includes(`ToolInfo<${toolName.toLowerCase()}ParamsType>`) || code.includes('ToolInfo<'); + if (!hasToolInfoType) { + errors.push(`Tool info must be typed as ToolInfo<YourParamsType>`); + } + + // Check for proper execute method typing - make this more flexible + if (!code.includes(`ParametersType<${toolName}ParamsType>`) && !code.includes('args: ParametersType<')) { + errors.push(`Execute method must have typed parameters: args: ParametersType<${toolName}ParamsType>`); + } + + // Check for unsafe code patterns + const unsafePatterns = [ + { pattern: /eval\s*\(/, message: 'eval() is not allowed' }, + { pattern: /Function\s*\(/, message: 'Function constructor is not allowed' }, + { pattern: /require\s*\(\s*['"]child_process['"]/, message: 'child_process module is not allowed' }, + { pattern: /require\s*\(\s*['"]fs['"]/, message: 'Direct fs module import is not allowed' }, + { pattern: /require\s*\(\s*['"]path['"]/, message: 'Direct path module import is not allowed' }, + { pattern: /process\.env/, message: 'Accessing process.env is not allowed' }, + { pattern: /import\s+.*['"]child_process['"]/, message: 'child_process module is not allowed' }, + { pattern: /import\s+.*['"]fs['"]/, message: 'Direct fs module import is not allowed' }, + { pattern: /import\s+.*['"]path['"]/, message: 'Direct path module import is not allowed' }, + { pattern: /\bnew\s+Function\b/, message: 'Function constructor is not allowed' }, + { pattern: /\bwindow\b/, message: 'Direct window access is not allowed' }, + { pattern: /\bdocument\b/, message: 'Direct document access is not allowed' }, + { pattern: /\blocation\b/, message: 'Direct location access is not allowed' }, + { pattern: /\bsessionStorage\b/, message: 'Direct sessionStorage access is not allowed' }, + { pattern: /\blocalStorage\b/, message: 'Direct localStorage access is not allowed' }, + { pattern: /fetch\s*\(/, message: 'Direct fetch calls are not allowed' }, + { pattern: /XMLHttpRequest/, message: 'Direct XMLHttpRequest use is not allowed' }, + ]; + + for (const { pattern, message } of unsafePatterns) { + if (pattern.test(code)) { + errors.push(message); + } + } + + // Check if the tool name is already used by an existing tool + const toolNameLower = toolName.toLowerCase(); + if (Object.keys(this.existingTools).some(key => key.toLowerCase() === toolNameLower) || Array.from(this.dynamicToolRegistry.keys()).some(key => key.toLowerCase() === toolNameLower)) { + errors.push(`A tool with the name "${toolNameLower}" already exists. Please choose a different name.`); + } + + // Use TypeScript compiler API to check for syntax errors + try { + const sourceFile = ts.createSourceFile(`${toolName}.ts`, code, ts.ScriptTarget.Latest, true); + + // Create a TypeScript program to check for type errors + const options: ts.CompilerOptions = { + target: ts.ScriptTarget.ES2020, + module: ts.ModuleKind.ESNext, + strict: true, + esModuleInterop: true, + skipLibCheck: true, + forceConsistentCasingInFileNames: true, + }; + + // Perform additional static analysis on the AST + const visitor = (node: ts.Node) => { + // Check for potentially unsafe constructs + if (ts.isCallExpression(node)) { + const expression = node.expression; + if (ts.isIdentifier(expression)) { + const name = expression.text; + if (name === 'eval' || name === 'Function') { + errors.push(`Use of ${name} is not allowed`); + } + } + } + + // Recursively visit all child nodes + ts.forEachChild(node, visitor); + }; + + visitor(sourceFile); + } catch (error) { + errors.push(`TypeScript syntax error: ${error}`); + } + + return { + valid: errors.length === 0, + errors, + }; + } + + /** + * Extracts tool info name from the tool code + * @param code The tool TypeScript code + * @returns The tool info name or null if not found + */ + private extractToolInfoName(code: string): string | null { + const nameMatch = code.match(/name\s*:\s*['"]([^'"]+)['"]/); + return nameMatch && nameMatch[1] ? nameMatch[1] : null; + } + + /** + * Extracts and parses parameter info from the tool code + * @param code The tool TypeScript code + * @returns An array of parameter objects + */ + private extractToolParameters(code: string): Array<{ name: string; type: string; description: string; required: boolean }> { + // Basic regex-based extraction - in a production environment, this should use the TypeScript AST + const paramsMatch = code.match(/const\s+\w+Params\s*=\s*\[([\s\S]*?)\]\s*as\s*const/); + if (!paramsMatch || !paramsMatch[1]) { + return []; + } + + const paramsText = paramsMatch[1]; + + // Parse individual parameters + const paramRegex = /{\s*name\s*:\s*['"]([^'"]+)['"]\s*,\s*type\s*:\s*['"]([^'"]+)['"]\s*,\s*description\s*:\s*['"]([^'"]+)['"]\s*,\s*required\s*:\s*(true|false)/g; + const params = []; + let match; + + while ((match = paramRegex.exec(paramsText)) !== null) { + params.push({ + name: match[1], + type: match[2], + description: match[3], + required: match[4] === 'true', + }); + } + + return params; + } + + /** + * Generates the complete tool file content with proper imports for the dynamic subfolder + * @param toolCode The user-provided tool code + * @param toolName The name of the tool class + * @returns The complete TypeScript file content + */ + private generateCompleteToolFile(toolCode: string, toolName: string): string { + // Add proper imports for the dynamic subfolder (one level deeper than regular tools) + const imports = `import { Observation } from '../../types/types'; +import { ParametersType, ToolInfo } from '../../types/tool_types'; +import { BaseTool } from '../BaseTool'; + +`; + + // Clean the user code - remove any existing imports they might have added + const cleanedCode = toolCode + .replace(/import\s+[^;]+;?\s*/g, '') // Remove any import statements + .trim(); + + return imports + cleanedCode; + } + + /** + * Transpiles TypeScript code to JavaScript + * @param code The TypeScript code to compile + * @param filename The name of the file (for error reporting) + * @returns The compiled JavaScript code + */ + private transpileTypeScript(code: string, filename: string): { jsCode: string; errors: string[] } { + try { + const transpileOptions: ts.TranspileOptions = { + compilerOptions: { + module: ts.ModuleKind.CommonJS, // Use CommonJS for dynamic imports + target: ts.ScriptTarget.ES2020, + moduleResolution: ts.ModuleResolutionKind.NodeJs, + esModuleInterop: true, + sourceMap: false, + strict: false, // Relax strict mode for dynamic compilation + noImplicitAny: false, // Allow implicit any types + }, + reportDiagnostics: true, + fileName: `${filename}.ts`, + }; + + const output = ts.transpileModule(code, transpileOptions); + + // Check for compilation errors + const errors: string[] = []; + if (output.diagnostics && output.diagnostics.length > 0) { + for (const diagnostic of output.diagnostics) { + if (diagnostic.file && diagnostic.start !== undefined) { + const { line, character } = diagnostic.file.getLineAndCharacterOfPosition(diagnostic.start); + const message = ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'); + errors.push(`Line ${line + 1}, Column ${character + 1}: ${message}`); + } else { + errors.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n')); + } + } + + if (errors.length > 0) { + return { jsCode: '', errors }; + } + } + + return { jsCode: output.outputText, errors: [] }; + } catch (error) { + return { + jsCode: '', + errors: [`Transpilation failed: ${error instanceof Error ? error.message : String(error)}`], + }; + } + } + + /** + * Dynamically evaluates and instantiates a tool from JavaScript code + * @param jsCode The JavaScript code + * @param toolName The name of the tool class + * @returns An instance of the tool or null if instantiation failed + */ + private async createDynamicTool(jsCode: string, toolName: string): Promise<BaseTool<ReadonlyArray<Parameter>> | null> { + try { + // Create a safe evaluation context with necessary globals + const globalContext = { + BaseTool: BaseTool, // Actual class reference + exports: {}, + module: { exports: {} }, + require: (id: string) => { + // Mock require for the imports we know about + if (id.includes('types/types')) { + return { Observation: null }; + } + if (id.includes('tool_types')) { + return { ParametersType: null, ToolInfo: null }; + } + if (id.includes('BaseTool')) { + return { BaseTool: BaseTool }; + } + return {}; + }, + console: console, + // Add any other commonly needed globals + JSON: JSON, + Array: Array, + Object: Object, + String: String, + Number: Number, + Boolean: Boolean, + Math: Math, + Date: Date, + }; + + // Create function to evaluate in the proper context + const evaluationFunction = new Function( + ...Object.keys(globalContext), + `"use strict"; + try { + ${jsCode} + // Get the exported class from the module + const ToolClass = exports.${toolName} || module.exports.${toolName} || module.exports; + if (ToolClass && typeof ToolClass === 'function') { + return new ToolClass(); + } else { + console.error('Tool class not found in exports:', Object.keys(exports), Object.keys(module.exports)); + return null; + } + } catch (error) { + console.error('Error during tool evaluation:', error); + return null; + }` + ); + + // Execute with our controlled globals + const toolInstance = evaluationFunction(...Object.values(globalContext)); + + if (!toolInstance) { + console.error(`Failed to instantiate ${toolName} - no instance returned`); + return null; + } + + // Verify it's a proper BaseTool instance + if (!(toolInstance instanceof BaseTool)) { + console.error(`${toolName} is not a proper instance of BaseTool`); + return null; + } + + console.log(`Successfully created dynamic tool instance: ${toolName}`); + return toolInstance; + } catch (error) { + console.error('Error creating dynamic tool:', error); + return null; + } + } + + /** + * Save the tool code to the server so it's available for future sessions + * @param toolName The name of the tool + * @param completeToolCode The complete TypeScript code for the tool with imports + */ + private async saveToolToServer(toolName: string, completeToolCode: string): Promise<boolean> { + try { + // Create a server endpoint to save the tool + const response = await Networking.PostToServer('/saveDynamicTool', { + toolName: toolName, + toolCode: completeToolCode, + }); + + // Type check the response to avoid property access errors + return typeof response === 'object' && response !== null && 'success' in response && (response as { success: boolean }).success === true; + } catch (error) { + console.error('Failed to save tool to server:', error); + return false; + } + } + + async execute(args: ParametersType<CreateNewToolParamsType>): Promise<Observation[]> { + const { toolName, toolCode, description } = args; + + console.log(`Creating new tool: ${toolName}`); + + // Remove any markdown backticks that might be in the code + const cleanedCode = (toolCode as string).replace(/```typescript|```/g, '').trim(); + + if (!cleanedCode) { + return [ + { + type: 'text', + text: 'Failed to extract tool code from the provided input. Please ensure the tool code is provided as valid TypeScript code.', + }, + ]; + } + + // Validate the provided code + const validation = this.validateToolCode(cleanedCode, toolName); + if (!validation.valid) { + return [ + { + type: 'text', + text: `Failed to create tool: Code validation failed with the following errors:\n- ${validation.errors.join('\n- ')}`, + }, + ]; + } + + try { + // Generate the complete tool file with proper imports + const completeToolCode = this.generateCompleteToolFile(cleanedCode, toolName); + + // Extract tool info name from the code + const toolInfoName = this.extractToolInfoName(cleanedCode); + if (!toolInfoName) { + return [ + { + type: 'text', + text: 'Failed to extract tool info name from the code. Make sure the tool has a name property.', + }, + ]; + } + + // Extract parameters from the tool code + const parameters = this.extractToolParameters(cleanedCode); + + // Transpile the TypeScript to JavaScript + const { jsCode, errors } = this.transpileTypeScript(completeToolCode, toolName); + if (errors.length > 0) { + return [ + { + type: 'text', + text: `Failed to transpile tool code with the following errors:\n- ${errors.join('\n- ')}`, + }, + ]; + } + + // Create a dynamic tool instance + const toolInstance = await this.createDynamicTool(jsCode, toolName); + if (!toolInstance) { + return [ + { + type: 'text', + text: 'Failed to instantiate the tool. Make sure it follows all the required patterns and properly extends BaseTool.', + }, + ]; + } + + // Register the tool in the dynamic registry + // Use the name property from the tool info as the registry key + this.dynamicToolRegistry.set(toolInfoName, toolInstance); + + // If we have a reference to the agent, tell it to register dynamic tool + // This ensures the tool is properly loaded from the filesystem for the prompt system + if (this.agent) { + this.agent.registerDynamicTool(toolInfoName, toolInstance); + } + + // Create the success message + const successMessage = `Successfully created and registered new tool: ${toolName}\n\nThe tool is now available for use in the current session. You can call it using the action "${toolInfoName}".\n\nDescription: ${description}\n\nParameters: ${ + parameters.length > 0 ? parameters.map(p => `\n- ${p.name} (${p.type}${p.required ? ', required' : ''}): ${p.description}`).join('') : '\nNo parameters' + }\n\nThe tool will be saved permanently after you confirm the page reload.`; + + // Notify the agent that a tool was created with the complete code for deferred saving + // This will trigger the modal but NOT save to disk yet + if (this.agent) { + this.agent.notifyToolCreated(toolName, completeToolCode); + } + + return [ + { + type: 'text', + text: successMessage, + }, + ]; + } catch (error) { + console.error(`Error creating new tool:`, error); + return [ + { + type: 'text', + text: `Failed to create tool: ${(error as Error).message || 'Unknown error'}`, + }, + ]; + } + } + + /** + * Public method to save tool to server (called by agent after user confirmation) + * @param toolName The name of the tool + * @param completeToolCode The complete TypeScript code for the tool with imports + */ + public async saveToolToServerDeferred(toolName: string, completeToolCode: string): Promise<boolean> { + return this.saveToolToServer(toolName, completeToolCode); + } +} diff --git a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts deleted file mode 100644 index 16dc938bb..000000000 --- a/src/client/views/nodes/chatbot/tools/CreateTextDocumentTool.ts +++ /dev/null @@ -1,57 +0,0 @@ -import { parsedDoc } from '../chatboxcomponents/ChatBox'; -import { ParametersType, ToolInfo } from '../types/tool_types'; -import { Observation } from '../types/types'; -import { BaseTool } from './BaseTool'; -const createTextDocToolParams = [ - { - name: 'text_content', - type: 'string', - description: 'The text content that the document will display', - 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, - // }, -] as const; - -type CreateTextDocToolParamsType = typeof createTextDocToolParams; - -const createTextDocToolInfo: ToolInfo<CreateTextDocToolParamsType> = { - name: 'createTextDoc', - description: 'Creates a text document with the provided content and title. Use if the user wants to create a textbox or text document of some sort. Can use after a search or other tool to save information.', - citationRules: 'No citation needed.', - parameterRules: createTextDocToolParams, -}; - -export class CreateTextDocTool extends BaseTool<CreateTextDocToolParamsType> { - private _addLinkedDoc: (doc: parsedDoc) => void; - - constructor(addLinkedDoc: (doc: parsedDoc) => void) { - super(createTextDocToolInfo); - this._addLinkedDoc = addLinkedDoc; - } - - async execute(args: ParametersType<CreateTextDocToolParamsType>): Promise<Observation[]> { - try { - this._addLinkedDoc({ doc_type: 'text', data: args.text_content, title: args.title }); - return [{ type: 'text', text: 'Created text document.' }]; - } catch (error) { - return [{ type: 'text', text: 'Error creating text document, ' + error }]; - } - } -} diff --git a/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts b/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts new file mode 100644 index 000000000..a55f901e1 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts @@ -0,0 +1,853 @@ +import { Doc, FieldType } from '../../../../../fields/Doc'; +import { DocData } from '../../../../../fields/DocSymbols'; +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo, Parameter } from '../types/tool_types'; +import { BaseTool } from './BaseTool'; +import { DocumentOptions } from '../../../../documents/Documents'; +import { CollectionFreeFormDocumentView } from '../../../nodes/CollectionFreeFormDocumentView'; +import { v4 as uuidv4 } from 'uuid'; +import { LinkManager } from '../../../../util/LinkManager'; +import { DocCast, StrCast } from '../../../../../fields/Types'; +import { supportedDocTypes } from '../types/tool_types'; +import { parsedDoc } from '../chatboxcomponents/ChatBox'; +import { AgentDocumentManager } from '../utils/AgentDocumentManager'; + +// Define the parameters for the DocumentMetadataTool +const parameterDefinitions: ReadonlyArray<Parameter> = [ + { + name: 'action', + type: 'string', + required: true, + description: 'The action to perform: "get" to retrieve metadata, "edit" to modify metadata, "getFieldOptions" to retrieve all available field options, or "create" to create a new document', + }, + { + name: 'documentId', + type: 'string', + required: false, + description: 'The ID of the document to get or edit metadata for. Required for "edit", optional for "get", ignored for "getFieldOptions", and "create"', + }, + { + name: 'fieldEdits', + type: 'string', + required: false, + description: + 'JSON array of field edits for editing fields. Each item should have fieldName and fieldValue. For single field edits, use an array with one item. Example: [{"fieldName":"layout_autoHeight","fieldValue":false},{"fieldName":"height","fieldValue":300}]', + }, + { + name: 'title', + type: 'string', + required: false, + description: 'The title of the document to create. Required for "create" action', + }, + { + name: 'data', + type: 'string', + required: false, + description: 'The data content for the document to create. Required for "create" action', + }, + { + name: 'doc_type', + type: 'string', + required: false, + description: `The type of document to create. Required for "create" action. Options: ${Object.keys(supportedDocTypes).join(',')}`, + }, +] as const; + +type DocumentMetadataToolParamsType = typeof parameterDefinitions; + +// Detailed description with usage guidelines for the DocumentMetadataTool +const toolDescription = `Extracts and modifies metadata from documents in the same Freeform view as the ChatBox, and can create new documents. +This tool helps you work with document properties, understand available fields, edit document metadata, and create new documents. + +The Dash document system organizes fields in two locations: +1. Layout documents: contain visual properties like position, dimensions, and appearance +2. Data documents: contain the actual content and document-specific data + +This tool provides the following capabilities: +- Get metadata from all documents in the current Freeform view +- Get metadata from a specific document +- Edit metadata fields on documents (in either layout or data documents) +- Edit multiple fields at once (useful for updating dependent fields together) +- Retrieve all available field options with metadata (IMPORTANT: always call this before editing) +- Understand which fields are stored where (layout vs data document) +- Get detailed information about all available document fields +- Support for all value types: strings, numbers, and booleans +- Create new documents with basic properties + +DOCUMENT CREATION: +- Use action="create" to create new documents with a simplified approach +- Required parameters: title, data, and doc_type +- The tool will create the document with sensible defaults and link it to the current view +- After creation, you can use the edit action to update its properties + +IMPORTANT: Before editing any document metadata, first call 'getFieldOptions' to understand: +- Which fields are available +- The data type of each field +- Special dependencies between fields (like layout_autoHeight and height) +- Proper naming conventions (with or without underscores) + +IMPORTANT: Some fields have dependencies that must be handled for edits to work correctly: +- When editing "height", first set "layout_autoHeight" to false (as a boolean value, not a string) +- When editing "width", first set "layout_autoWidth" to false (as a boolean value, not a string) +- Check document metadata to identify other similar dependencies +- All edits are done using the fieldEdits parameter which accepts an array of fields to modify + +Example: To change document height, disable auto-height and set height in a single operation: +{... inputs: { action: "edit", documentId: "doc123", fieldEdits: [ + { fieldName: "layout_autoHeight", fieldValue: false }, + { fieldName: "height", fieldValue: 300 } +]}} + + +Here is a detailed example: + +<interaction description="Using DocumentMetadataTool to create and align a diagram with its notes"> + <stage number="1" role="user"> + <query>Create a flow-chart of neurotransmitter release at a synapse, and place a notes box directly underneath that explains each step.</query> + </stage> + + <!-- CREATE THE DIAGRAM --> + <stage number="2" role="assistant"> + <thought> + I will create a DiagramBox containing a Mermaid flow-chart. After that, I'll make a notes document, read the diagram’s metadata, and then move the notes so it lines up beneath the chart. + </thought> + <action>documentMetadata</action> + </stage> + + <stage number="3" role="user"> + <action_rules>***Action rules omitted***</action_rules> + </stage> + + <stage number="4" role="assistant"> + <action_input> + <action_input_description>Creating a DiagramBox with a Mermaid chart of neurotransmitter release.</action_input_description> + <inputs> + <action>create</action> + <title>Synaptic Release Flowchart</title> + <data>graph TD; A(Vesicle docks)-->B(Ca²⁺ influx); B-->C(Vesicle fuses); C-->D(Neurotransmitter released)</data> + <doc_type>diagram</doc_type> + </inputs> + </action_input> + </stage> + + <stage number="5" role="user"> + <observation> + <chunk chunk_id="9f2c43c8-0a16-4c12-987b-b813d2f7b1ee" chunk_type="text"> + Document created successfully. + Document ID: 9f2c43c8-0a16-4c12-987b-b813d2f7b1ee + Type: diagram + Title: "Synaptic Release Flowchart" + + The document has been created with default dimensions and positioning. + … + Full metadata for the created document: + { /* huge JSON omitted for brevity */ } + </chunk> + </observation> + </stage> + + <!-- CREATE THE NOTES DOC --> + <stage number="6" role="assistant"> + <thought> + I will create a plain-text notes document that summarises the diagram’s steps. + </thought> + <action>documentMetadata</action> + </stage> + + <stage number="7" role="user"> + <action_rules>***Action rules omitted***</action_rules> + </stage> + + <stage number="8" role="assistant"> + <action_input> + <action_input_description>Creating a text document for explanatory notes.</action_input_description> + <inputs> + <action>create</action> + <title>Synaptic Release – Notes</title> + <data>This diagram shows vesicle docking, calcium-triggered fusion, and neurotransmitter release into the synaptic cleft.</data> + <doc_type>note</doc_type> + </inputs> + </action_input> + </stage> + + <stage number="9" role="user"> + <observation> + <chunk chunk_id="c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f" chunk_type="text"> + Document created successfully. + Document ID: c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f + Type: note + Title: "Synaptic Release – Notes" + … + Full metadata for the created document: + { /* huge JSON omitted for brevity */ } + </chunk> + </observation> + </stage> + + <!-- READ DIAGRAM METADATA --> + <stage number="10" role="assistant"> + <thought> + I'll fetch the diagram’s width, height, and position so I can align the notes box underneath it. + </thought> + <action>documentMetadata</action> + </stage> + + <stage number="11" role="user"> + <action_rules>***Action rules omitted***</action_rules> + </stage> + + <stage number="12" role="assistant"> + <action_input> + <action_input_description>Getting metadata for the diagram document.</action_input_description> + <inputs> + <action>get</action> + <documentId>9f2c43c8-0a16-4c12-987b-b813d2f7b1ee</documentId> + </inputs> + </action_input> + </stage> + + <stage number="13" role="user"> + <observation> + <chunk chunk_id="9f2c43c8-0a16-4c12-987b-b813d2f7b1ee" chunk_type="text"> + { + "x": 100, + "y": 100, + "width": 600, + "height": 300, + "layout_autoWidth": false, + "layout_autoHeight": false + } + </chunk> + </observation> + </stage> + + <!-- MOVE & SIZE THE NOTES DOC --> + <stage number="14" role="assistant"> + <thought> + The diagram ends at y = 100 + 300 = 400. + I’ll move the notes to x = 100, y = 420 (a 20-px gap) and expand its width to 600 px. + </thought> + <action>documentMetadata</action> + </stage> + + <stage number="15" role="user"> + <action_rules>***Action rules omitted***</action_rules> + </stage> + + <stage number="16" role="assistant"> + <action_input> + <action_input_description>Editing the notes document’s position and size.</action_input_description> + <inputs> + <action>edit</action> + <documentId>c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f</documentId> + <fieldEdits>[ + {"fieldName":"layout_autoWidth","fieldValue":false}, + {"fieldName":"width","fieldValue":600}, + {"fieldName":"x","fieldValue":100}, + {"fieldName":"y","fieldValue":420} + ]</fieldEdits> + </inputs> + </action_input> + </stage> + + <stage number="17" role="user"> + <observation> + <chunk chunk_id="c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f" chunk_type="text"> + Successfully edited 4 fields on document c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f. + New position: (x:100, y:420) Width:600 + </chunk> + </observation> + </stage> + + <!-- FINAL ANSWER --> + <stage number="18" role="assistant"> + <thought> + Both documents are now created and perfectly aligned; I can summarise the result. + </thought> + <answer> + <grounded_text citation_index="1"> + A **DiagramBox** depicting neurotransmitter release was created at **(100 px, 100 px)**, sized **600 × 300 px**. + </grounded_text> + <grounded_text citation_index="2"> + A **notes document** was then resized to **600 px** wide and moved to **(100 px, 420 px)**—20 px below the diagram—to keep the explanation directly beneath the visualization. + </grounded_text> + <normal_text> + This layout ensures viewers can read the synopsis while referring to the flow-chart above. + </normal_text> + <citations> + <citation index="1" chunk_id="9f2c43c8-0a16-4c12-987b-b813d2f7b1ee" type="text"></citation> + <citation index="2" chunk_id="c8e3f4b9-7d2e-462a-a444-61e8e9ff0c7f" type="text"></citation> + </citations> + <follow_up_questions> + <question>Would you like to tweak the diagram’s styling (e.g., colours or fonts)?</question> + <question>Should I link external references or papers in the notes?</question> + <question>Do you want similar diagrams for other neural processes?</question> + </follow_up_questions> + <loop_summary> + The assistant used **DocumentMetadataTool** four times: + 1) **create** DiagramBox → 2) **create** notes document → 3) **get** diagram metadata → 4) **edit** notes position/size. + This demonstrates creating, inspecting, and aligning documents within a Freeform view. + </loop_summary> + </answer> + </stage> +</interaction> + +<MermaidMindmapGuide> + <Overview> + <Description> + Mermaid mindmaps are hierarchical diagrams used to visually organize ideas. Nodes are created using indentation to show parent-child relationships. + </Description> + <Note>This is an experimental feature in Mermaid and may change in future versions.</Note> + </Overview> + + <BasicSyntax> + <CodeExample language="mermaid"> + <![CDATA[ + mindmap + Root + Branch A + Leaf A1 + Leaf A2 + Branch B + Leaf B1 + ]]> + </CodeExample> + <Explanation> + <Point><code>mindmap</code> declares the diagram.</Point> + <Point>Indentation determines the hierarchy.</Point> + <Point>Each level must be indented more than its parent.</Point> + </Explanation> + </BasicSyntax> + + <NodeShapes> + <Description>Nodes can be styled with various shapes similar to flowchart syntax.</Description> + <Shapes> + <Shape name="Square"><Code>id[Square Text]</Code></Shape> + <Shape name="Rounded Square"><Code>id(Rounded Square)</Code></Shape> + <Shape name="Circle"><Code>id((Circle))</Code></Shape> + <Shape name="Bang"><Code>id))Bang((</Code></Shape> + <Shape name="Cloud"><Code>id)Cloud(</Code></Shape> + <Shape name="Hexagon"><Code>id{{Hexagon}}</Code></Shape> + <Shape name="Default"><Code>Default shape without any brackets</Code></Shape> + </Shapes> + </NodeShapes> + + <Icons> + <Description>Nodes can include icons using the <code>::icon(class)</code> syntax.</Description> + <CodeExample> + <![CDATA[ + mindmap + Root + Node A + ::icon(fa fa-book) + Node B + ::icon(mdi mdi-lightbulb) + ]]> + </CodeExample> + <Note>Icon fonts must be included by the site administrator for proper rendering.</Note> + </Icons> + + <CSSClasses> + <Description>Add custom styling classes using <code>:::</code>.</Description> + <CodeExample> + <![CDATA[ + mindmap + Root + Important Node + :::urgent large + Regular Node + ]]> + </CodeExample> + <Note>Classes must be defined in your website or application CSS.</Note> + </CSSClasses> + + <MarkdownSupport> + <Description>Supports markdown-style strings for rich text, line breaks, and auto-wrapping.</Description> + <CodeExample> + <![CDATA[ + mindmap + id1["**Bold Root** with new line"] + id2["*Italicized* and long text that wraps"] + id3[Plain label] + ]]> + </CodeExample> + </MarkdownSupport> + + <RenderingNote> + <Note>Indentation is relative, not absolute — Mermaid will infer hierarchy based on surrounding context even with inconsistent spacing.</Note> + </RenderingNote> + + <Integration> + <Description> + From Mermaid v11, mindmaps are included natively. For older versions, use external imports with lazy loading. + </Description> + <CodeExample> + <![CDATA[ + <script type="module"> + import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid@11/dist/mermaid.esm.min.mjs'; + </script> + ]]> + </CodeExample> + </Integration> +</MermaidMindmapGuide> + +`; + +// Extensive usage guidelines for the tool +const citationRules = `USAGE GUIDELINES: +To GET document metadata: +- Use action="get" with optional documentId to return metadata for one or all documents +- Returns field values, field definitions, and location information (layout vs data document) + +To GET ALL FIELD OPTIONS (call this first): +- Use action="getFieldOptions" to retrieve metadata about all available document fields +- No additional parameters are required +- Returns structured metadata with field names, types, descriptions, and dependencies +- ALWAYS call this before attempting to edit document metadata +- Use this information to understand which fields need special handling + +To CREATE a new document: +- Use action="create" with the following required parameters: + - title: The title of the document to create + - data: The content data for the document (text content, URL, etc.) + - doc_type: The type of document to create (text, web, image, etc.) +- Example: {...inputs: { action: "create", title: "My Notes", data: "This is the content", doc_type: "text" }} +- After creation, you can edit the document with more specific properties + +To EDIT document metadata: +- Use action="edit" with required parameters: + - documentId: The ID of the document to edit + - fieldEdits: JSON array of fields to edit, each with fieldName and fieldValue +- The tool will determine the correct document location automatically +- Field names can be provided with or without leading underscores (e.g., both "width" and "_width" work) +- Common fields like "width" and "height" are automatically mapped to "_width" and "_height" +- All value types are supported: strings, numbers, and booleans +- The tool will apply the edit to the correct document (layout or data) based on existing fields + +SPECIAL FIELD HANDLING: +- Text fields: When editing the 'text' field, provide simple plain text + Example: {...inputs: { action: "edit", documentId: "doc123", fieldEdits: [{ fieldName: "text", fieldValue: "Hello world" }] }} + The tool will automatically convert your text to the proper RichTextField format +- Width/Height: Set layout_autoHeight/layout_autoWidth to false before editing + +RECOMMENDED WORKFLOW: +0. Understand the currently available documents that were provided as <available_documents> in the prompt +1. Call action="getFieldOptions" to understand available fields +3. Get document metadata with action="get" to see current values +4. Edit fields with action="edit" using proper dependencies +OR +0. Understand the state of the currently available documents and their metadata using action="get" (this includes spacial positioning). +1. Create a new document with action="create" +2. Get its ID from the response +3. Edit the document's properties with action="edit" + +HANDLING DEPENDENT FIELDS: +- When editing some fields, you may need to update related dependent fields +- For example, when changing "height", you should also set "layout_autoHeight" to false +- Use the fieldEdits parameter to update dependent fields in a single operation: + {...inputs: { action: "edit", documentId: "doc123", fieldEdits: [ + { fieldName: "layout_autoHeight", fieldValue: false }, + { fieldName: "height", fieldValue: 300 } +]}} +- Always check for dependent fields that might affect your edits, such as: + - height → layout_autoHeight (set to false to allow manual height) + - width → layout_autoWidth (set to false to allow manual width) + - Other auto-sizing related properties + +Editing fields follows these rules: +1. First checks if the field exists on the layout document using Doc.Get +2. If it exists on the layout document, it's updated there +3. If it has an underscore prefix (_), it's created/updated on the layout document +4. Otherwise, the field is created/updated on the data document +5. Fields with leading underscores are automatically handled correctly + +Examples: +- To get field options: { action: "getFieldOptions" } +- To get all document metadata: { action: "get" } +- To get metadata for a specific document: { action: "get", documentId: "doc123" } +- To edit a single field: { action: "edit", documentId: "doc123", fieldEdits: [{ fieldName: "backgroundColor", fieldValue: "#ff0000" }] } +- To edit a width property: { action: "edit", documentId: "doc123", fieldEdits: [{ fieldName: "width", fieldValue: 300 }] } +- To edit text content: { action: "edit", documentId: "doc123", fieldEdits: [{ fieldName: "text", fieldValue: "Simple plain text goes here" }] } +- To disable auto-height: { action: "edit", documentId: "doc123", fieldEdits: [{ fieldName: "layout_autoHeight", fieldValue: false }] } +- To create a text document: { action: "create", title: "My Notes", data: "This is my note content", doc_type: "text" } +- To create a web document: { action: "create", title: "Google", data: "https://www.google.com", doc_type: "web" } +- To edit height with its dependent field together: + { action: "edit", documentId: "doc123", fieldEdits: [ + { fieldName: "layout_autoHeight", fieldValue: false }, + { fieldName: "height", fieldValue: 200 } + ]} +- IMPORTANT: MULTI STEP WORKFLOWS ARE NOT ONLY ALLOWED BUT ENCOURAGED. TAKE THINGS 1 STEP AT A TIME. +- IMPORTANT: WHEN CITING A DOCUMENT, MAKE THE CHUNK ID THE DOCUMENT ID. WHENEVER YOU CITE A DOCUMENT, ALWAYS MAKE THE CITATION TYPE "text", THE "direct_text" FIELD BLANK, AND THE "chunk_id" FIELD THE DOCUMENT ID.`; +const documentMetadataToolInfo: ToolInfo<DocumentMetadataToolParamsType> = { + name: 'documentMetadata', + description: toolDescription, + parameterRules: parameterDefinitions, + citationRules: citationRules, +}; + +/** + * A tool for extracting and modifying metadata from documents in a Freeform view. + * This tool collects metadata from both layout and data documents in a Freeform view + * and allows for editing document fields in the correct location. + */ +export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsType> { + private _docManager: AgentDocumentManager; + + constructor(docManager: AgentDocumentManager) { + super(documentMetadataToolInfo); + this._docManager = docManager; + this._docManager.initializeFindDocsFreeform(); + } + + /** + * Executes the document metadata tool + * @param args The arguments for the tool + * @returns An observation with the results of the tool execution + */ + async execute(args: ParametersType<DocumentMetadataToolParamsType>): Promise<Observation[]> { + console.log('DocumentMetadataTool: Executing with args:', args); + + // Find all documents in the Freeform view + this._docManager.initializeFindDocsFreeform(); + + try { + // Validate required input parameters based on action + if (!this.inputValidator(args)) { + return [ + { + type: 'text', + text: `Error: Invalid or missing parameters for action "${args.action}". ${this.getParameterRequirementsByAction(String(args.action))}`, + }, + ]; + } + + // Ensure the action is valid and convert to string + const action = String(args.action); + if (!['get', 'edit', 'getFieldOptions', 'create'].includes(action)) { + return [ + { + type: 'text', + text: 'Error: Invalid action. Valid actions are "get", "edit", "getFieldOptions", or "create".', + }, + ]; + } + + // Safely convert documentId to string or undefined + const documentId = args.documentId ? String(args.documentId) : undefined; + + // Perform the specified action + switch (action) { + case 'get': { + // Get metadata for a specific document or all documents + const result = this._docManager.getDocumentMetadata(documentId); + console.log('DocumentMetadataTool: Get metadata result:', result); + return [ + { + type: 'text', + text: `Document metadata ${documentId ? 'for document ' + documentId : ''} retrieved successfully:\n${JSON.stringify(result, null, 2)}`, + }, + ]; + } + + case 'edit': { + // Edit a specific field on a document + if (!documentId) { + return [ + { + type: 'text', + text: 'Error: Document ID is required for edit actions.', + }, + ]; + } + + // Ensure document exists + if (!this._docManager.has(documentId)) { + return [ + { + type: 'text', + text: `Error: Document with ID ${documentId} not found.`, + }, + ]; + } + + // Check for fieldEdits parameter + if (!args.fieldEdits) { + return [ + { + type: 'text', + text: 'Error: fieldEdits is required for edit actions. Please provide a JSON array of field edits.', + }, + ]; + } + + try { + // Parse fieldEdits array + const edits = JSON.parse(String(args.fieldEdits)); + if (!Array.isArray(edits) || edits.length === 0) { + return [ + { + type: 'text', + text: 'Error: fieldEdits must be a non-empty array of field edits.', + }, + ]; + } + + // Track results for all edits + const results: { + success: boolean; + message: string; + fieldName?: string; + originalFieldName?: string; + newValue?: any; + warning?: string; + }[] = []; + + let allSuccessful = true; + + // Process each edit + for (const edit of edits) { + // Get fieldValue in its original form + let fieldValue = edit.fieldValue; + + // Only convert to string if it's neither boolean nor number + if (typeof fieldValue !== 'boolean' && typeof fieldValue !== 'number') { + fieldValue = String(fieldValue); + } + + const fieldName = String(edit.fieldName); + + // Edit the field + const result = this._docManager.editDocumentField(documentId, fieldName, fieldValue); + + console.log(`DocumentMetadataTool: Edit field result for ${fieldName}:`, result); + + // Add to results + results.push(result); + + // Update success status + if (!result.success) { + allSuccessful = false; + } + } + + // Format response based on results + let responseText = ''; + if (allSuccessful) { + responseText = `Successfully edited ${results.length} fields on document ${documentId}:\n`; + results.forEach(result => { + responseText += `- Field '${result.originalFieldName}': updated to ${JSON.stringify(result.newValue)}\n`; + + // Add any warnings + if (result.warning) { + responseText += ` Warning: ${result.warning}\n`; + } + }); + } else { + responseText = `Errors occurred while editing fields on document ${documentId}:\n`; + results.forEach(result => { + if (result.success) { + responseText += `- Field '${result.originalFieldName}': updated to ${JSON.stringify(result.newValue)}\n`; + + // Add any warnings + if (result.warning) { + responseText += ` Warning: ${result.warning}\n`; + } + } else { + responseText += `- Error editing '${result.originalFieldName}': ${result.message}\n`; + } + }); + } + + // Get the updated metadata to return + const updatedMetadata = this._docManager.getDocumentMetadata(documentId); + + return [ + { + type: 'text', + text: `${responseText}\nUpdated metadata:\n${JSON.stringify(updatedMetadata, null, 2)}`, + }, + ]; + } catch (error) { + return [ + { + type: 'text', + text: `Error processing fieldEdits: ${error instanceof Error ? error.message : String(error)}`, + }, + ]; + } + } + + case 'getFieldOptions': { + // Get all available field options with metadata + const fieldOptions = this._docManager.getAllFieldMetadata(); + + return [ + { + type: 'text', + text: `Document field options retrieved successfully.\nThis information should be consulted before editing document fields to understand available options and dependencies:\n${JSON.stringify(fieldOptions, null, 2)}`, + }, + ]; + } + + case 'create': { + // Create a new document + if (!args.title || !args.data || !args.doc_type) { + return [ + { + type: 'text', + text: 'Error: Title, data, and doc_type are required for create action.', + }, + ]; + } + + const docType = String(args.doc_type); + const title = String(args.title); + const data = String(args.data); + + const id = await this._docManager.createDocInDash(docType, data, { title: title }); + + if (!id) { + return [ + { + type: 'text', + text: 'Error: Failed to create document.', + }, + ]; + } + // Get the created document's metadata + const createdMetadata = this._docManager.extractDocumentMetadata(id); + + return [ + { + type: 'text', + text: `Document created successfully. +Document ID: ${id} +Type: ${docType} +Title: "${title}" + +The document has been created with default dimensions and positioning. +You can now use the "edit" action to modify additional properties of this document. + +Next steps: +1. Use the "getFieldOptions" action to understand available editable/addable fields/properties and their dependencies. +2. To modify this document, use: { action: "edit", documentId: "${id}", fieldEdits: [{"fieldName":"property","fieldValue":"value"}] } +3. To add styling, consider setting backgroundColor, fontColor, or other properties +4. For text documents, you can edit the content with: { action: "edit", documentId: "${id}", fieldEdits: [{"fieldName":"text","fieldValue":"New content"}] } + +Full metadata for the created document: +${JSON.stringify(createdMetadata, null, 2)}`, + }, + ]; + } + + default: + return [ + { + type: 'text', + text: 'Error: Unknown action. Valid actions are "get", "edit", "getFieldOptions", or "create".', + }, + ]; + } + } catch (error) { + console.error('DocumentMetadataTool execution error:', error); + return [ + { + type: 'text', + text: `Error executing DocumentMetadataTool: ${error instanceof Error ? error.message : String(error)}`, + }, + ]; + } + } + + /** + * Validates the input parameters for the DocumentMetadataTool + * This custom validator allows numbers and booleans to be passed for fieldValue + * while maintaining compatibility with the standard validation + * + * @param params The parameters to validate + * @returns True if the parameters are valid, false otherwise + */ + inputValidator(params: ParametersType<DocumentMetadataToolParamsType>): boolean { + // Default validation for required fields + if (params.action === undefined) { + return false; + } + + // For create action, validate required parameters + if (params.action === 'create') { + return !!(params.title && params.data && params.doc_type); + } + + // For edit action, validate fieldEdits is provided + if (params.action === 'edit') { + if (!params.documentId || !params.fieldEdits) { + return false; + } + + try { + // Parse fieldEdits and validate its structure + const edits = JSON.parse(String(params.fieldEdits)); + + // Ensure it's an array + if (!Array.isArray(edits)) { + console.log('fieldEdits is not an array'); + return false; + } + + // Ensure each item has fieldName and fieldValue + for (const edit of edits) { + if (!edit.fieldName) { + console.log('An edit is missing fieldName'); + return false; + } + if (edit.fieldValue === undefined) { + console.log('An edit is missing fieldValue'); + return false; + } + } + + // Everything looks good with fieldEdits + return true; + } catch (error) { + console.log('Error parsing fieldEdits:', error); + return false; + } + } + + // For get action with documentId, documentId is required + if (params.action === 'get' && params.documentId === '') { + return false; + } + + // getFieldOptions action doesn't require any additional parameters + if (params.action === 'getFieldOptions') { + return true; + } + + return true; + } + + /** + * Returns the parameter requirements for a specific action + * @param action The action to get requirements for + * @returns A string describing the required parameters + */ + private getParameterRequirementsByAction(action?: string): string { + if (!action) { + return 'Please specify an action: "get", "edit", "getFieldOptions", or "create".'; + } + + switch (action.toLowerCase()) { + case 'get': + return 'The "get" action accepts an optional documentId parameter.'; + case 'edit': + return 'The "edit" action requires documentId and fieldEdits parameters. fieldEdits must be a JSON array of field edits.'; + case 'getFieldOptions': + return 'The "getFieldOptions" action does not require any additional parameters. It returns metadata about all available document fields.'; + case 'create': + return 'The "create" action requires title, data, and doc_type parameters.'; + default: + return `Unknown action "${action}". Valid actions are "get", "edit", "getFieldOptions", or "create".`; + } + } +} diff --git a/src/client/views/nodes/chatbot/tools/FileContentTool.ts b/src/client/views/nodes/chatbot/tools/FileContentTool.ts new file mode 100644 index 000000000..f994aab67 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/FileContentTool.ts @@ -0,0 +1,78 @@ +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; +import { Vectorstore } from '../vectorstore/Vectorstore'; +import { BaseTool } from './BaseTool'; + +const fileContentToolParams = [ + { + name: 'filepaths', + type: 'string[]', + description: 'Array of file paths to retrieve content for. Limited to a maximum of 3 files.', + required: true, + }, +] as const; + +type FileContentToolParamsType = typeof fileContentToolParams; + +const fileContentToolInfo: ToolInfo<FileContentToolParamsType> = { + name: 'fileContent', + description: 'Retrieves the complete content of up to 3 specified files from the Dash codebase to help understand implementation details.', + citationRules: `When using the FileContentTool: +1. Present file content clearly with proper code formatting +2. Include the file path at the beginning of each file's content +3. Use this tool after identifying relevant files with the CodebaseSummarySearchTool +4. Maximum of 3 files can be retrieved at once`, + parameterRules: fileContentToolParams, +}; + +export class FileContentTool extends BaseTool<FileContentToolParamsType> { + constructor(private vectorstore: Vectorstore) { + super(fileContentToolInfo); + } + + async execute(args: ParametersType<FileContentToolParamsType>): Promise<Observation[]> { + console.log(`Executing file content retrieval for: ${args.filepaths.join(', ')}`); + + // Enforce the limit of 3 files + const filepaths = args.filepaths.slice(0, 3); + if (args.filepaths.length > 3) { + console.warn(`FileContentTool: Request for ${args.filepaths.length} files was limited to 3`); + } + + const observations: Observation[] = []; + + if (filepaths.length === 0) { + return [ + { + type: 'text', + text: 'No filepaths provided. Please specify at least one file path to retrieve content.', + }, + ]; + } + + // Add initial message + observations.push({ + type: 'text', + text: `Retrieving content for ${filepaths.length} file(s):\n\n`, + }); + + // Fetch content for each file + for (const filepath of filepaths) { + const content = await this.vectorstore.getFileContent(filepath); + + if (content) { + observations.push({ + type: 'text', + text: `File: ${filepath}\n\n\`\`\`\n${content}\n\`\`\`\n\n`, + }); + } else { + observations.push({ + type: 'text', + text: `Error: Could not retrieve content for file "${filepath}"\n\n`, + }); + } + } + + return observations; + } +} diff --git a/src/client/views/nodes/chatbot/tools/FileNamesTool.ts b/src/client/views/nodes/chatbot/tools/FileNamesTool.ts new file mode 100644 index 000000000..b69874afa --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/FileNamesTool.ts @@ -0,0 +1,34 @@ +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; +import { Vectorstore } from '../vectorstore/Vectorstore'; +import { BaseTool } from './BaseTool'; + +const fileNamesToolParams = [] as const; + +type FileNamesToolParamsType = typeof fileNamesToolParams; + +const fileNamesToolInfo: ToolInfo<FileNamesToolParamsType> = { + name: 'fileNames', + description: 'Retrieves the names of all files in the Dash codebase to help understand the codebase structure.', + citationRules: `No citation needed.`, + parameterRules: fileNamesToolParams, +}; + +export class FileNamesTool extends BaseTool<FileNamesToolParamsType> { + constructor(private vectorstore: Vectorstore) { + super(fileNamesToolInfo); + } + + async execute(args: ParametersType<FileNamesToolParamsType>): Promise<Observation[]> { + console.log(`Executing file names retrieval`); + + const filepaths = await this.vectorstore.getFileNames(); + + return [ + { + type: 'text', + text: `The file names in the codebase are: ${filepaths}`, + }, + ]; + } +} diff --git a/src/client/views/nodes/chatbot/tools/RAGTool.ts b/src/client/views/nodes/chatbot/tools/RAGTool.ts index ef374ed22..af44de520 100644 --- a/src/client/views/nodes/chatbot/tools/RAGTool.ts +++ b/src/client/views/nodes/chatbot/tools/RAGTool.ts @@ -3,6 +3,7 @@ import { Observation, RAGChunk } from '../types/types'; import { ParametersType, ToolInfo } from '../types/tool_types'; import { Vectorstore } from '../vectorstore/Vectorstore'; import { BaseTool } from './BaseTool'; +import { DocumentMetadataTool } from './DocumentMetadataTool'; const ragToolParams = [ { @@ -11,13 +12,19 @@ const ragToolParams = [ description: "A detailed prompt representing an ideal chunk to embed and compare against document vectors to retrieve the most relevant content for answering the user's query.", required: true, }, + { + name: 'doc_ids', + type: 'string[]', + description: 'An optional array of document IDs to retrieve chunks from. If you want to retrieve chunks from all documents, leave this as an empty array: [] (DO NOT LEAVE THIS EMPTY).', + required: false, + }, ] as const; type RAGToolParamsType = typeof ragToolParams; const ragToolInfo: ToolInfo<RAGToolParamsType> = { name: 'rag', - description: 'Performs a RAG (Retrieval-Augmented Generation) search on user documents and returns a set of document chunks (text or images) to provide a grounded response based on user documents.', + description: `Performs a RAG (Retrieval-Augmented Generation) search on user documents (only PDF, audio, and video are supported—for information about other document types, use the ${DocumentMetadataTool.name} tool) and returns a set of document chunks (text or images) to provide a grounded response based on user documents.`, citationRules: `When using the RAG tool, the structure must adhere to the format described in the ReAct prompt. Below are additional guidelines specifically for RAG-based responses: 1. **Grounded Text Guidelines**: @@ -68,14 +75,14 @@ export class RAGTool extends BaseTool<RAGToolParamsType> { } async execute(args: ParametersType<RAGToolParamsType>): Promise<Observation[]> { - const relevantChunks = await this.vectorstore.retrieve(args.hypothetical_document_chunk); + const relevantChunks = await this.vectorstore.retrieve(args.hypothetical_document_chunk, undefined, args.doc_ids ?? undefined); const formattedChunks = await this.getFormattedChunks(relevantChunks); return formattedChunks; } async getFormattedChunks(relevantChunks: RAGChunk[]): Promise<Observation[]> { try { - const { formattedChunks } = await Networking.PostToServer('/formatChunks', { relevantChunks }) as { formattedChunks: Observation[]} + const { formattedChunks } = (await Networking.PostToServer('/formatChunks', { relevantChunks })) as { formattedChunks: Observation[] }; if (!formattedChunks) { throw new Error('Failed to format chunks'); diff --git a/src/client/views/nodes/chatbot/tools/SearchTool.ts b/src/client/views/nodes/chatbot/tools/SearchTool.ts index 6a11407a5..8e6edce8c 100644 --- a/src/client/views/nodes/chatbot/tools/SearchTool.ts +++ b/src/client/views/nodes/chatbot/tools/SearchTool.ts @@ -3,6 +3,9 @@ import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; import { ParametersType, ToolInfo } from '../types/tool_types'; +import { Agent } from 'http'; +import { AgentDocumentManager } from '../utils/AgentDocumentManager'; +import { StrCast } from '../../../../../fields/Types'; const searchToolParams = [ { @@ -19,18 +22,18 @@ type SearchToolParamsType = typeof searchToolParams; const searchToolInfo: ToolInfo<SearchToolParamsType> = { name: 'searchTool', - citationRules: 'No citation needed. Cannot cite search results for a response. Use web scraping tools to cite specific information.', + citationRules: 'Always cite the search results for a response, if the search results are relevant to the response. Use the chunk_id to cite the search results. If the search results are not relevant to the response, do not cite them. ', parameterRules: searchToolParams, description: 'Search the web to find a wide range of websites related to a query or multiple queries. Returns a list of websites and their overviews based on the search queries.', }; export class SearchTool extends BaseTool<SearchToolParamsType> { - private _addLinkedUrlDoc: (url: string, id: string) => void; + private _docManager: AgentDocumentManager; private _max_results: number; - constructor(addLinkedUrlDoc: (url: string, id: string) => void, max_results: number = 4) { + constructor(docManager: AgentDocumentManager, max_results: number = 3) { super(searchToolInfo); - this._addLinkedUrlDoc = addLinkedUrlDoc; + this._docManager = docManager; this._max_results = max_results; } @@ -45,14 +48,21 @@ export class SearchTool extends BaseTool<SearchToolParamsType> { query, max_results: this._max_results, })) as { results: { url: string; snippet: string }[] }; - const data = results.map((result: { url: string; snippet: string }) => { - const id = uuidv4(); - this._addLinkedUrlDoc(result.url, id); - return { - type: 'text' as const, - text: `<chunk chunk_id="${id}" chunk_type="url"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`, - }; - }); + const data = await Promise.all( + results.map(async (result: { url: string; snippet: string }) => { + // Create a web document with the URL + const id = await this._docManager.createDocInDash('web', result.url, { + title: `Search Result: ${result.url}`, + text_html: result.snippet, + data_useCors: true, + }); + + return { + type: 'text' as const, + text: `<chunk chunk_id="${id}" chunk_type="url"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`, + }; + }) + ); return data; } catch (error) { console.log(error); diff --git a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts index 19ccd0b36..727d35e2c 100644 --- a/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts +++ b/src/client/views/nodes/chatbot/tools/WebsiteInfoScraperTool.ts @@ -3,12 +3,14 @@ import { Networking } from '../../../../Network'; import { BaseTool } from './BaseTool'; import { Observation } from '../types/types'; import { ParametersType, ToolInfo } from '../types/tool_types'; - +import { AgentDocumentManager } from '../utils/AgentDocumentManager'; +import { Doc } from '../../../../../fields/Doc'; +import { StrCast, WebCast } from '../../../../../fields/Types'; const websiteInfoScraperToolParams = [ { - name: 'urls', + name: 'chunk_ids', type: 'string[]', - description: 'The URLs of the websites to scrape', + description: 'The chunk_ids of the urls to scrape from the SearchTool.', required: true, max_inputs: 3, }, @@ -20,6 +22,7 @@ const websiteInfoScraperToolInfo: ToolInfo<WebsiteInfoScraperToolParamsType> = { name: 'websiteInfoScraper', description: 'Scrape detailed information from specific websites relevant to the user query. Returns the text content of the webpages for further analysis and grounding.', citationRules: ` + !IMPORTANT! THESE CHUNKS REPLACE THE CHUNKS THAT ARE RETURNED FROM THE SEARCHTOOL. Your task is to provide a comprehensive response to the user's prompt using the content scraped from relevant websites. Ensure you follow these guidelines for structuring your response: 1. Grounded Text Tag Structure: @@ -66,38 +69,121 @@ const websiteInfoScraperToolInfo: ToolInfo<WebsiteInfoScraperToolParamsType> = { }; export class WebsiteInfoScraperTool extends BaseTool<WebsiteInfoScraperToolParamsType> { - private _addLinkedUrlDoc: (url: string, id: string) => void; + private _docManager: AgentDocumentManager; - constructor(addLinkedUrlDoc: (url: string, id: string) => void) { + constructor(docManager: AgentDocumentManager) { super(websiteInfoScraperToolInfo); - this._addLinkedUrlDoc = addLinkedUrlDoc; + this._docManager = docManager; } - async execute(args: ParametersType<WebsiteInfoScraperToolParamsType>): Promise<Observation[]> { - const urls = args.urls; - - // Create an array of promises, each one handling a website scrape for a URL - const scrapingPromises = urls.map(async url => { + /** + * Attempts to scrape a website with retry logic + * @param url URL to scrape + * @param maxRetries Maximum number of retry attempts + * @returns The scraped content or error message + */ + private async scrapeWithRetry(chunkDoc: Doc, maxRetries = 2): Promise<Observation> { + let lastError = ''; + let retryCount = 0; + const url = WebCast(chunkDoc.data!)!.url.href; + console.log(url); + console.log(chunkDoc); + console.log(chunkDoc.data); + const id = chunkDoc.id; + // Validate URL format + try { + new URL(url); // This will throw if URL is invalid + } catch (e) { + return { + type: 'text', + text: `Invalid URL format: ${url}. Please provide a valid URL including http:// or https://`, + } as Observation; + } + + while (retryCount <= maxRetries) { try { - const { website_plain_text } = await Networking.PostToServer('/scrapeWebsite', { url }); - const id = uuidv4(); - this._addLinkedUrlDoc(url, id); + // Add a slight delay between retries + if (retryCount > 0) { + console.log(`Retry attempt ${retryCount} for ${url}`); + await new Promise(resolve => setTimeout(resolve, retryCount * 2000)); // Increasing delay for each retry + } + + const response = await Networking.PostToServer('/scrapeWebsite', { url }); + + if (!response || typeof response !== 'object') { + lastError = 'Empty or invalid response from server'; + retryCount++; + continue; + } + + const { website_plain_text } = response as { website_plain_text: string }; + + // Validate content quality + if (!website_plain_text) { + lastError = 'Retrieved content was empty'; + retryCount++; + continue; + } + + if (website_plain_text.length < 100) { + console.warn(`Warning: Content from ${url} is very short (${website_plain_text.length} chars)`); + + // Still return it if this is our last try + if (retryCount === maxRetries) { + return { + type: 'text', + text: `<chunk chunk_id="${id}" chunk_type="url">\n${website_plain_text}\nNote: Limited content was retrieved from this URL.\n</chunk>`, + } as Observation; + } + + lastError = 'Retrieved content was too short, trying again'; + retryCount++; + continue; + } + + // Process and return content if it looks good return { type: 'text', text: `<chunk chunk_id="${id}" chunk_type="url">\n${website_plain_text}\n</chunk>`, } as Observation; } catch (error) { - console.log(error); - return { - type: 'text', - text: `An error occurred while scraping the website: ${url}`, - } as Observation; + lastError = error instanceof Error ? error.message : 'Unknown error'; + console.log(`Error scraping ${url} (attempt ${retryCount + 1}):`, error); } - }); + + retryCount++; + } + + // All attempts failed + return { + type: 'text', + text: `Unable to scrape website: ${url}. Error: ${lastError}`, + } as Observation; + } + + async execute(args: ParametersType<WebsiteInfoScraperToolParamsType>): Promise<Observation[]> { + const chunk_ids = args.chunk_ids; + + // Create an array of promises, each one handling a website scrape for a URL + const scrapingPromises = chunk_ids.map(chunk_id => this.scrapeWithRetry(this._docManager.getDocument(chunk_id)!)); // Wait for all scraping promises to resolve const results = await Promise.all(scrapingPromises); + // Check if we got any successful results + const successfulResults = results.filter(result => { + if (result.type !== 'text') return false; + return (result as { type: 'text'; text: string }).text.includes('chunk_id') && !(result as { type: 'text'; text: string }).text.includes('Unable to scrape'); + }); + + // If all scrapes failed, provide a more helpful error message + if (successfulResults.length === 0 && results.length > 0) { + results.push({ + type: 'text', + text: `Note: All website scraping attempts failed. Please try with different URLs or try again later.`, + } as Observation); + } + return results; } } diff --git a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts index ee815532a..ec5d83e52 100644 --- a/src/client/views/nodes/chatbot/tools/WikipediaTool.ts +++ b/src/client/views/nodes/chatbot/tools/WikipediaTool.ts @@ -32,7 +32,7 @@ export class WikipediaTool extends BaseTool<WikipediaToolParamsType> { async execute(args: ParametersType<WikipediaToolParamsType>): Promise<Observation[]> { try { - const { text } = await Networking.PostToServer('/getWikipediaSummary', { title: args.title }); + const { text } = (await Networking.PostToServer('/getWikipediaSummary', { title: args.title })) as { text: string }; const id = uuidv4(); const url = `https://en.wikipedia.org/wiki/${args.title.replace(/ /g, '_')}`; this._addLinkedUrlDoc(url, id); diff --git a/src/client/views/nodes/chatbot/tools/dynamic/InspirationalQuotesTool.ts b/src/client/views/nodes/chatbot/tools/dynamic/InspirationalQuotesTool.ts new file mode 100644 index 000000000..23bbe1d76 --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/dynamic/InspirationalQuotesTool.ts @@ -0,0 +1,39 @@ +import { Observation } from '../../types/types'; +import { ParametersType, ToolInfo } from '../../types/tool_types'; +import { BaseTool } from '../BaseTool'; + +const inspirationalQuotesParams = [ + { + name: 'category', + type: 'string', + description: 'The category of inspirational quotes to retrieve', + required: false + } + ] as const; + + type InspirationalQuotesParamsType = typeof inspirationalQuotesParams; + + const inspirationalQuotesInfo: ToolInfo<InspirationalQuotesParamsType> = { + name: 'inspirationalquotestool', + description: 'Provides a random inspirational quote from a predefined list.', + citationRules: 'No citation needed.', + parameterRules: inspirationalQuotesParams + }; + + export class InspirationalQuotesTool extends BaseTool<InspirationalQuotesParamsType> { + constructor() { + super(inspirationalQuotesInfo); + } + + async execute(args: ParametersType<InspirationalQuotesParamsType>): Promise<Observation[]> { + const quotes = [ + "The only way to do great work is to love what you do. - Steve Jobs", + "The best time to plant a tree was 20 years ago. The second best time is now. - Chinese Proverb", + "Your time is limited, so don’t waste it living someone else’s life. - Steve Jobs", + "Not everything that is faced can be changed, but nothing can be changed until it is faced. - James Baldwin", + "The purpose of our lives is to be happy. - Dalai Lama" + ]; + const randomQuote = quotes[Math.floor(Math.random() * quotes.length)]; + return [{ type: 'text', text: randomQuote }]; + } + }
\ No newline at end of file |
