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 wikipediaToolParams = [ { name: 'title', type: 'string', description: 'The title of the Wikipedia article to search', required: true, }, ] as const; type WikipediaToolParamsType = typeof wikipediaToolParams; const wikipediaToolInfo: ToolInfo = { name: 'wikipedia', citationRules: 'No citation needed.', parameterRules: wikipediaToolParams, description: 'Returns a summary from searching an article title on Wikipedia.', }; export class WikipediaTool extends BaseTool { private _addLinkedUrlDoc: (url: string, id: string) => void; constructor(addLinkedUrlDoc: (url: string, id: string) => void) { super(wikipediaToolInfo); this._addLinkedUrlDoc = addLinkedUrlDoc; } async execute(args: ParametersType): Promise { try { const { text } = (await Networking.PostToServer('/getWikipediaSummary', { title: args.title })) as { text: string }; const id = uuidv4(); const url = `https://en.wikipedia.org/wiki/${args.title.replace(/ /g, '_')}`; this._addLinkedUrlDoc(url, id); return [ { type: 'text', text: ` ${text} `, }, ]; } catch (error) { console.log(error); return [{ type: 'text', text: 'An error occurred while fetching the article.' }]; } } }