aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/chatbot/tools/GetDocsTool.ts
blob: 42a7747d3946d7f66cc51e3b89f187c7b1922611 (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
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
import { BaseTool } from './BaseTool';
import { DocServer } from '../../../../DocServer';
import { Docs } from '../../../../documents/Documents';
import { DocumentView } from '../../DocumentView';
import { OpenWhere } from '../../OpenWhere';
import { DocCast } from '../../../../../fields/Types';

const getDocsToolParams = [
    {
        name: 'title',
        type: 'string',
        description: 'Title of the collection being created from retrieved documents',
        required: true,
    },
    {
        name: 'document_ids',
        type: 'string[]',
        description: 'List of document IDs to retrieve',
        required: true,
    },
] as const;

type GetDocsToolParamsType = typeof getDocsToolParams;

const getDocsToolInfo: ToolInfo<GetDocsToolParamsType> = {
    name: 'retrieveDocs',
    description: 'Retrieves the contents of all Documents that the user is interacting with in Dash.',
    citationRules: 'No citation needed.',
    parameterRules: getDocsToolParams,
};

export class GetDocsTool extends BaseTool<GetDocsToolParamsType> {
    private _docView: DocumentView;

    constructor(docView: DocumentView) {
        super(getDocsToolInfo);
        this._docView = docView;
    }

    async execute(args: ParametersType<GetDocsToolParamsType>): Promise<Observation[]> {
        const docs = args.document_ids
            .map(doc_id => DocCast(DocServer.GetCachedRefField(doc_id)))
            .filter(d => d)
            .map(d => d!);
        const collection = Docs.Create.FreeformDocument(docs, { title: args.title });
        this._docView._props.addDocTab(collection, OpenWhere.addRight);
        return [{ type: 'text', text: `Collection created in Dash called ${args.title}` }];
    }
}