aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/CreateLinksTool.ts
blob: c2850a8ce142ad2f304d55fbe7c413fc6e17706a (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
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)}`,
                },
            ];
        }
    }
}