aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CreateCSVTool.ts
blob: b321d98ba433b1351893fa94b3faa095d2000fed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import { BaseTool } from './BaseTool';
import { Networking } from '../../../../Network';
import { Observation } from '../types/types';
import { ParametersType } from './ToolTypes';

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;

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(
            'createCSV',
            'Creates a CSV file from raw CSV data and saves it to the server',
            createCSVToolParams,
            'Provide a CSV string and a filename to create a CSV file.',
            '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.'
        );
        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,
            });

            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.');
        }
    }
}