diff options
author | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2025-03-11 17:43:05 +0100 |
---|---|---|
committer | Nathan-SR <144961007+Nathan-SR@users.noreply.github.com> | 2025-03-11 17:43:05 +0100 |
commit | fa937182bc93aa2c6faadda80ea998cdfd479b4e (patch) | |
tree | cba8e16edcccc6fd2932173484ac444cb79abea2 /src/client/views/nodes/chatbot/tools/CreateCSVTool.ts | |
parent | cf91c46cfec6e3e36b9184764016f9c1b5c997d4 (diff) | |
parent | 04669ffeb163688c7aefd7b5face7998252abdca (diff) |
Merge branch 'master' of https://github.com/brown-dash/Dash-Web into DocCreatorMenu-work
Diffstat (limited to 'src/client/views/nodes/chatbot/tools/CreateCSVTool.ts')
-rw-r--r-- | src/client/views/nodes/chatbot/tools/CreateCSVTool.ts | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts new file mode 100644 index 000000000..290c48d6c --- /dev/null +++ b/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts @@ -0,0 +1,59 @@ +import { BaseTool } from './BaseTool'; +import { Networking } from '../../../../Network'; +import { Observation } from '../types/types'; +import { ParametersType, ToolInfo } from '../types/tool_types'; + +const createCSVToolParams = [ + { + name: 'csvData', + type: 'string', + description: 'A string of comma-separated values representing the CSV data.', + required: true, + }, + { + name: 'filename', + type: 'string', + description: 'The base name of the CSV file to be created. Should end in ".csv".', + required: true, + }, +] as const; + +type CreateCSVToolParamsType = typeof createCSVToolParams; + +const createCSVToolInfo: ToolInfo<CreateCSVToolParamsType> = { + name: 'createCSV', + description: 'Creates a CSV file from the provided CSV string and saves it to the server with a unique identifier, returning the file URL and UUID.', + citationRules: 'No citation needed.', + parameterRules: createCSVToolParams, +}; + +export class CreateCSVTool extends BaseTool<CreateCSVToolParamsType> { + private _handleCSVResult: (url: string, filename: string, id: string, data: string) => void; + + constructor(handleCSVResult: (url: string, title: string, id: string, data: string) => void) { + super(createCSVToolInfo); + this._handleCSVResult = handleCSVResult; + } + + async execute(args: ParametersType<CreateCSVToolParamsType>): Promise<Observation[]> { + try { + console.log('Creating CSV file:', args.filename, ' with data:', args.csvData); + const { fileUrl, id } = (await Networking.PostToServer('/createCSV', { + filename: args.filename, + data: args.csvData, + })) as { fileUrl: string; id: string }; + + this._handleCSVResult(fileUrl, args.filename, id, args.csvData); + + return [ + { + type: 'text', + text: `File successfully created: ${fileUrl}. \nNow a CSV file with this data and the name ${args.filename} is available as a user doc.`, + }, + ]; + } catch (error) { + console.error('Error creating CSV file:', error); + throw new Error('Failed to create CSV file.'); + } + } +} |