import { Observation } from '../types/types'; import { ParametersType, ToolInfo } from '../types/tool_types'; import { BaseTool } from './BaseTool'; // Define the tool's parameters const dictionaryToolParams = [ { name: 'word', type: 'string', description: 'The word to look up in the dictionary.', required: true, }, ] as const; type DictionaryToolParamsType = typeof dictionaryToolParams; // Define the tool's metadata and rules const dictionaryToolInfo: ToolInfo = { name: 'dictionary', citationRules: 'No citation needed.', parameterRules: dictionaryToolParams, description: 'Fetches the definition of a given word using an open dictionary API.', }; export class DictionaryTool extends BaseTool { constructor() { super(dictionaryToolInfo); } async execute(args: ParametersType): Promise { const url = `https://api.dictionaryapi.dev/api/v2/entries/en/${args.word}`; try { const response = await fetch(url); const data = await response.json(); // Handle cases where the word is not found if (data.title === 'No Definitions Found') { return [ { type: 'text', text: `Sorry, I couldn't find a definition for the word "${args.word}".`, }, ]; } // Extract the first definition const definition = data[0]?.meanings[0]?.definitions[0]?.definition; return [ { type: 'text', text: `The definition of "${args.word}" is: ${definition}`, }, ]; } catch (error) { return [ { type: 'text', text: `An error occurred while fetching the definition: ${error}`, }, ]; } } }