diff options
Diffstat (limited to 'src')
3 files changed, 158 insertions, 11 deletions
diff --git a/src/client/views/nodes/chatbot/agentsystem/Agent.ts b/src/client/views/nodes/chatbot/agentsystem/Agent.ts index 3c76cf348..bab2081b3 100644 --- a/src/client/views/nodes/chatbot/agentsystem/Agent.ts +++ b/src/client/views/nodes/chatbot/agentsystem/Agent.ts @@ -87,7 +87,7 @@ export class Agent { noTool: new NoTool(), imageCreationTool: new ImageCreationTool(createImage), // createTextDoc: new CreateTextDocTool(addLinkedDoc), - createDoc: new CreateDocTool(addLinkedDoc), + // createDoc: new CreateDocTool(addLinkedDoc), // createAnyDocument: new CreateAnyDocumentTool(addLinkedDoc), // dictionary: new DictionaryTool(), documentMetadata: new DocumentMetadataTool(this), diff --git a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts index 284879a4a..b3bf6dfb2 100644 --- a/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts +++ b/src/client/views/nodes/chatbot/tools/CreateDocumentTool.ts @@ -9,7 +9,7 @@ import { CollectionViewType } from '../../../../documents/DocumentTypes'; */ export enum supportedDocTypes { flashcard = 'flashcard', - text = 'text', + note = 'note', html = 'html', equation = 'equation', functionplot = 'functionplot', diff --git a/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts b/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts index 093ab248d..c74e502e7 100644 --- a/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts +++ b/src/client/views/nodes/chatbot/tools/DocumentMetadataTool.ts @@ -8,6 +8,8 @@ import { CollectionFreeFormDocumentView } from '../../../nodes/CollectionFreeFor import { v4 as uuidv4 } from 'uuid'; import { LinkManager } from '../../../../util/LinkManager'; import { DocCast, StrCast } from '../../../../../fields/Types'; +import { supportedDocTypes } from './CreateDocumentTool'; +import { parsedDoc } from '../chatboxcomponents/ChatBox'; // Define the parameters for the DocumentMetadataTool const parameterDefinitions: ReadonlyArray<Parameter> = [ @@ -15,13 +17,13 @@ const parameterDefinitions: ReadonlyArray<Parameter> = [ name: 'action', type: 'string', required: true, - description: 'The action to perform: "get" to retrieve metadata, "edit" to modify metadata, "list" to enumerate documents, or "getFieldOptions" to retrieve all available field options', + description: 'The action to perform: "get" to retrieve metadata, "edit" to modify metadata, "list" to enumerate documents, "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 "list" and "getFieldOptions"', + description: 'The ID of the document to get or edit metadata for. Required for "edit", optional for "get", ignored for "list", "getFieldOptions", and "create"', }, { name: 'fieldName', @@ -40,14 +42,32 @@ const parameterDefinitions: ReadonlyArray<Parameter> = [ type: 'string', required: false, description: 'JSON array of field edits for editing multiple fields at once. Each item should have fieldName and fieldValue. 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. -This tool helps you work with document properties, understand available fields, and edit document metadata. +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 @@ -63,6 +83,13 @@ This tool provides the following capabilities: - 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 @@ -99,6 +126,14 @@ To GET ALL FIELD OPTIONS (call this first): - 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: { 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 documentId, and either: 1. fieldName + fieldValue for single field edits, OR @@ -120,6 +155,10 @@ RECOMMENDED WORKFLOW: 2. Then 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 +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 @@ -154,6 +193,8 @@ Examples: - To edit a width property: { action: "edit", documentId: "doc123", fieldName: "width", fieldValue: 300 } - To edit text content: { action: "edit", documentId: "doc123", fieldName: "text", fieldValue: "Simple plain text goes here" } - To disable auto-height: { action: "edit", documentId: "doc123", 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 (recommended): { action: "edit", documentId: "doc123", fieldEdits: [ { fieldName: "layout_autoHeight", fieldValue: false }, @@ -936,10 +977,10 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp // Ensure the action is valid and convert to string const action = String(args.action); - if (!['get', 'edit', 'list', 'getFieldOptions'].includes(action)) { + if (!['get', 'edit', 'list', 'getFieldOptions', 'create'].includes(action)) { return [{ type: 'text', - text: 'Error: Invalid action. Valid actions are "get", "edit", "list", or "getFieldOptions".' + text: 'Error: Invalid action. Valid actions are "get", "edit", "list", "getFieldOptions", or "create".' }]; } @@ -1149,10 +1190,95 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp }]; } + 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); + + // Validate doc_type + if (!this.isValidDocType(docType)) { + return [{ + type: 'text', + text: `Error: Invalid doc_type. Valid options are: ${Object.keys(supportedDocTypes).join(',')}` + }]; + } + + try { + // Create simple document with just title and data + const simpleDoc: parsedDoc = { + doc_type: docType, + title: title, + data: data, + x: 0, + y: 0, + _width: 300, + _height: 300, + _layout_fitWidth: false, + _layout_autoHeight: true + }; + + // Use the chatBox's createDocInDash method to create and link the document + if (!this.chatBox || !this.chatBox.createDocInDash) { + return [{ + type: 'text', + text: 'Error: Could not access document creation functionality.' + }]; + } + + const createdDoc = this.chatBox.createDocInDash(simpleDoc); + + if (!createdDoc) { + return [{ + type: 'text', + text: 'Error: Failed to create document.' + }]; + } + + // Update our local document maps with the new document + this.processDocument(createdDoc); + + // Get the created document's metadata + const createdMetadata = this.getDocumentMetadata(createdDoc.id); + + return [{ + type: 'text', + text: `Document created successfully. +Document ID: ${createdDoc.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: "${createdDoc.id}", 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: "${createdDoc.id}", fieldName: "text", fieldValue: "New content" } + +Full metadata for the created document: +${JSON.stringify(createdMetadata, null, 2)}` + }]; + } catch (error) { + return [{ + type: 'text', + text: `Error creating document: ${error instanceof Error ? error.message : String(error)}` + }]; + } + } + default: return [{ type: 'text', - text: 'Error: Unknown action. Valid actions are "get", "edit", "list", or "getFieldOptions".' + text: 'Error: Unknown action. Valid actions are "get", "edit", "list", "getFieldOptions", or "create".' }]; } } catch (error) { @@ -1178,6 +1304,11 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp return false; } + // For create action, validate required parameters + if (params.action === 'create') { + return !!(params.title && params.data && params.doc_type); + } + // For edit action, validate either single field edit or multiple field edits if (params.action === 'edit') { // If fieldEdits is provided, it must be valid and we'll ignore fieldName/fieldValue @@ -1228,6 +1359,11 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp return true; } + // list action doesn't require any additional parameters + if (params.action === 'list') { + return true; + } + // Allow for numeric or boolean fieldValue even though the type is defined as string if (params.fieldValue !== undefined) { if (typeof params.fieldValue === 'number') { @@ -1253,7 +1389,7 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp */ private getParameterRequirementsByAction(action?: string): string { if (!action) { - return 'Please specify an action: "get", "edit", "list", or "getFieldOptions".'; + return 'Please specify an action: "get", "edit", "list", "getFieldOptions", or "create".'; } switch (action.toLowerCase()) { @@ -1265,8 +1401,10 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp return 'The "list" action does not require any additional parameters.'; 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", "list", or "getFieldOptions".`; + return `Unknown action "${action}". Valid actions are "get", "edit", "list", "getFieldOptions", or "create".`; } } @@ -1293,4 +1431,13 @@ export class DocumentMetadataTool extends BaseTool<DocumentMetadataToolParamsTyp }; } } + + /** + * Helper method to validate a document type and ensure it's a valid supportedDocType + * @param docType The document type to validate + * @returns True if the document type is valid, false otherwise + */ + private isValidDocType(docType: string): boolean { + return Object.values(supportedDocTypes).includes(docType as supportedDocTypes); + } }
\ No newline at end of file |