UNPKG

agentis

Version:

A TypeScript framework for building sophisticated multi-agent systems

71 lines (70 loc) 2.63 kB
"use strict"; // src/tools/WebSearchTool.ts var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.WebSearchTool = void 0; const axios_1 = __importDefault(require("axios")); class WebSearchTool { constructor() { this.name = 'WebSearchTool'; this.description = 'Searches the web for real-time information using Tavily API'; const apiKey = process.env.TAVILY_API_KEY; if (!apiKey) { throw new Error('TAVILY_API_KEY is not set in environment variables'); } this.apiKey = apiKey; } async execute(input) { try { // Extract just the search query, removing any analysis instructions const searchQuery = this.extractSearchQuery(input); const response = await axios_1.default.post('https://api.tavily.com/search', { query: searchQuery, search_depth: "basic", max_results: 5, include_answer: true, include_images: false, include_raw_content: false }, { headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${this.apiKey}` } }); return { result: response.data.answer || response.data.results?.map((r) => r.title + ': ' + r.content).join('\n\n'), raw: response.data }; } catch (error) { console.error('WebSearchTool error:', error); if (axios_1.default.isAxiosError(error)) { console.error('API Response:', error.response?.data); } return { result: null, error: error instanceof Error ? error.message : 'An error occurred during web search' }; } } extractSearchQuery(input) { // Remove any analysis instructions and keep only the core search query const cleanInput = input .replace(/Analyze.*?:/g, '') .replace(/Based on.*?:/g, '') .replace(/Please.*?:/g, '') .trim(); // Limit query length to prevent API errors return cleanInput.slice(0, 300); } // Helper method to clean and format content cleanContent(content) { return content .replace(/\s+/g, ' ') .replace(/\n+/g, '\n') .trim(); } } exports.WebSearchTool = WebSearchTool;