aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/FileContentTool.ts
blob: f994aab671679e0542ea3e5bdc029cacd4e93d3d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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;
    }
}