const DEFAULT_BASE_URL = 'https://api.tavily.com';
const DEFAULT_TIMEOUT_MS = 30_000;
const TAVILY_MAX_RESULTS_LIMIT = 20;
export class TavilyError extends Error {
    status;
    constructor(message, status) {
        super(message);
        this.status = status;
        this.name = 'TavilyError';
    }
}
export async function tavilySearch(input) {
    if (!input.apiKey) {
        throw new TavilyError('Tavily API key is not configured');
    }
    const base = (input.baseUrl || DEFAULT_BASE_URL).replace(/\/+$/, '');
    const requestedMax = input.maxResults ?? 5;
    const maxResults = Math.max(0, Math.min(requestedMax, TAVILY_MAX_RESULTS_LIMIT));
    const body = {
        query: input.query,
        search_depth: input.searchDepth ?? 'basic',
        max_results: maxResults,
        include_answer: input.includeAnswer ?? true,
        include_raw_content: false,
    };
    const ctrl = new AbortController();
    const timer = setTimeout(() => ctrl.abort(), DEFAULT_TIMEOUT_MS);
    if (input.signal) {
        input.signal.addEventListener('abort', () => ctrl.abort(), { once: true });
    }
    let resp;
    try {
        resp = await fetch(`${base}/search`, {
            method: 'POST',
            headers: {
                'content-type': 'application/json',
                authorization: `Bearer ${input.apiKey}`,
            },
            body: JSON.stringify(body),
            signal: ctrl.signal,
        });
    }
    catch (err) {
        throw new TavilyError(`Tavily request failed: ${err.message || String(err)}`);
    }
    finally {
        clearTimeout(timer);
    }
    if (!resp.ok) {
        const text = await resp.text().catch(() => '');
        throw new TavilyError(`Tavily ${resp.status}: ${text.slice(0, 200) || 'no body'}`, resp.status);
    }
    const json = (await resp.json());
    const answer = typeof json.answer === 'string' ? json.answer : '';
    const rawResults = Array.isArray(json.results) ? json.results : [];
    const sources = [];
    for (const r of rawResults) {
        const url = typeof r.url === 'string' ? r.url : '';
        if (!url)
            continue;
        const publishedAt = typeof r.published_date === 'string' && r.published_date.trim()
            ? r.published_date.trim()
            : null;
        sources.push({
            title: typeof r.title === 'string' && r.title.trim()
                ? r.title.trim()
                : url,
            url,
            snippet: typeof r.content === 'string'
                ? r.content.trim().slice(0, 800)
                : '',
            provider: 'tavily',
            ...(publishedAt ? { publishedAt } : {}),
        });
    }
    return { answer, sources };
}
//# sourceMappingURL=tavily.js.map