/**
 * Web Tool Adapters
 *
 * ITool-compliant adapters for WebSearchTool operations.
 * These adapters wrap the existing WebSearchTool methods to conform
 * to the formal ITool interface for use with the FormalToolRegistry.
 */
import { WebSearchTool } from '../index.js';
// ============================================================================
// Shared WebSearchTool Instance
// ============================================================================
let webSearchInstance = null;
function getWebSearch() {
    if (!webSearchInstance) {
        webSearchInstance = new WebSearchTool();
    }
    return webSearchInstance;
}
/**
 * Reset the shared WebSearchTool instance (for testing)
 */
export function resetWebSearchInstance() {
    webSearchInstance = null;
}
// ============================================================================
// WebSearchExecuteTool
// ============================================================================
/**
 * WebSearchExecuteTool - ITool adapter for web search
 */
export class WebSearchExecuteTool {
    name = 'web_search';
    description = 'Search the web using Brave Search, Perplexity, Serper (Google), or DuckDuckGo. Supports region, language, and freshness filters.';
    async execute(input) {
        const query = input.query;
        const options = {
            maxResults: input.max_results,
            safeSearch: input.safe_search,
            country: input.country,
            search_lang: input.search_lang,
            ui_lang: input.ui_lang,
            freshness: input.freshness,
            provider: input.provider,
        };
        return await getWebSearch().search(query, options);
    }
    getSchema() {
        return {
            name: this.name,
            description: this.description,
            parameters: {
                type: 'object',
                properties: {
                    query: {
                        type: 'string',
                        description: 'The search query',
                    },
                    max_results: {
                        type: 'number',
                        description: 'Number of results to return (1-10, default: 5)',
                        default: 5,
                    },
                    country: {
                        type: 'string',
                        description: '2-letter country code for region-specific results (e.g., "DE", "US", "FR")',
                    },
                    search_lang: {
                        type: 'string',
                        description: 'ISO language code for search results (e.g., "de", "en", "fr")',
                    },
                    freshness: {
                        type: 'string',
                        description: 'Filter by discovery time (Brave only): "pd" (24h), "pw" (week), "pm" (month), "py" (year), or "YYYY-MM-DDtoYYYY-MM-DD"',
                    },
                    provider: {
                        type: 'string',
                        description: 'Force a specific provider: "brave", "perplexity", "serper", "duckduckgo". Default: auto-fallback chain.',
                    },
                },
                required: ['query'],
            },
        };
    }
    validate(input) {
        if (typeof input !== 'object' || input === null) {
            return { valid: false, errors: ['Input must be an object'] };
        }
        const data = input;
        if (typeof data.query !== 'string' || data.query.trim() === '') {
            return { valid: false, errors: ['query must be a non-empty string'] };
        }
        return { valid: true };
    }
    getMetadata() {
        return {
            name: this.name,
            description: this.description,
            category: 'web',
            keywords: ['search', 'web', 'internet', 'brave', 'perplexity', 'google', 'duckduckgo'],
            priority: 7,
            modifiesFiles: false,
            makesNetworkRequests: true,
        };
    }
    isAvailable() {
        return true;
    }
}
// ============================================================================
// WebFetchTool
// ============================================================================
/**
 * WebFetchTool - ITool adapter for fetching web pages
 */
export class WebFetchTool {
    name = 'web_fetch';
    description = 'Fetch and extract text content from a web page';
    async execute(input) {
        const url = input.url;
        const prompt = input.prompt;
        if (typeof prompt === 'string' && prompt.length > 0) {
            return await getWebSearch().fetchPage(url, prompt);
        }
        return await getWebSearch().fetchPage(url);
    }
    getSchema() {
        return {
            name: this.name,
            description: this.description,
            parameters: {
                type: 'object',
                properties: {
                    url: {
                        type: 'string',
                        description: 'The URL to fetch',
                    },
                    prompt: {
                        type: 'string',
                        description: 'Optional prompt for content extraction',
                    },
                },
                required: ['url'],
            },
        };
    }
    validate(input) {
        if (typeof input !== 'object' || input === null) {
            return { valid: false, errors: ['Input must be an object'] };
        }
        const data = input;
        if (typeof data.url !== 'string' || data.url.trim() === '') {
            return { valid: false, errors: ['url must be a non-empty string'] };
        }
        // Basic URL validation
        try {
            new URL(data.url);
        }
        catch {
            return { valid: false, errors: ['url must be a valid URL'] };
        }
        return { valid: true };
    }
    getMetadata() {
        return {
            name: this.name,
            description: this.description,
            category: 'web',
            keywords: ['fetch', 'web', 'page', 'url', 'content', 'scrape'],
            priority: 6,
            modifiesFiles: false,
            makesNetworkRequests: true,
        };
    }
    isAvailable() {
        return true;
    }
}
// ============================================================================
// Factory Function
// ============================================================================
/**
 * Create all web tool instances
 */
export function createWebTools() {
    return [
        new WebSearchExecuteTool(),
        new WebFetchTool(),
    ];
}
//# sourceMappingURL=web-tools.js.map