| 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
 | import { v4 as uuidv4 } from 'uuid';
import { Networking } from '../../../../Network';
import { BaseTool } from './BaseTool';
import { Observation } from '../types/types';
import { ParametersType, ToolInfo } from '../types/tool_types';
const searchToolParams = [
    {
        name: 'queries',
        type: 'string[]',
        description:
            'The search query or queries to use for finding websites. Provide up to 3 search queries to find a broad range of websites. Should be in the form of a TypeScript array of strings (e.g. <queries>["search term 1", "search term 2", "search term 3"]</queries>).',
        required: true,
        max_inputs: 3,
    },
] as const;
type SearchToolParamsType = typeof searchToolParams;
const searchToolInfo: ToolInfo<SearchToolParamsType> = {
    name: 'searchTool',
    citationRules: 'No citation needed. Cannot cite search results for a response. Use web scraping tools to cite specific information.',
    parameterRules: searchToolParams,
    description: 'Search the web to find a wide range of websites related to a query or multiple queries. Returns a list of websites and their overviews based on the search queries.',
};
export class SearchTool extends BaseTool<SearchToolParamsType> {
    private _addLinkedUrlDoc: (url: string, id: string) => void;
    private _max_results: number;
    constructor(addLinkedUrlDoc: (url: string, id: string) => void, max_results: number = 4) {
        super(searchToolInfo);
        this._addLinkedUrlDoc = addLinkedUrlDoc;
        this._max_results = max_results;
    }
    async execute(args: ParametersType<SearchToolParamsType>): Promise<Observation[]> {
        const queries = args.queries;
        console.log(`Searching the web for queries: ${queries[0]}`);
        // Create an array of promises, each one handling a search for a query
        const searchPromises = queries.map(async query => {
            try {
                const { results } = (await Networking.PostToServer('/getWebSearchResults', {
                    query,
                    max_results: this._max_results,
                })) as { results: { url: string; snippet: string }[] };
                const data = results.map((result: { url: string; snippet: string }) => {
                    const id = uuidv4();
                    this._addLinkedUrlDoc(result.url, id);
                    return {
                        type: 'text' as const,
                        text: `<chunk chunk_id="${id}" chunk_type="url"><url>${result.url}</url><overview>${result.snippet}</overview></chunk>`,
                    };
                });
                return data;
            } catch (error) {
                console.log(error);
                return [
                    {
                        type: 'text' as const,
                        text: `An error occurred while performing the web search for query: ${query}`,
                    },
                ];
            }
        });
        const allResultsArrays = await Promise.all(searchPromises);
        return allResultsArrays.flat();
    }
}
 |