/**
 * Test-connection probe for the Tavily Search API.
 *
 * Makes a small real search call against
 *   POST https://api.tavily.com/search
 * with the configured key and a fixed probe query. Returns `ok: true`
 * on HTTP 200, `ok: false` on auth / network / other errors. This is the
 * same endpoint + request shape the runtime search tool uses
 * (see agent/tools/web-tools.ts), so a green probe means search will work.
 *
 * The probe never echoes the upstream error body — only a generic canned
 * phrase and the HTTP status code (no headers, no payload).
 *
 * `fetchImpl` is injectable so unit tests can exercise the status mapping
 * without contacting the real upstream (mirrors the Anthropic probe).
 */
const TAVILY_URL = 'https://api.tavily.com/search';
const PROBE_QUERY = 'studiograph probe';
export async function probeTavily(apiKey, _ctx, fetchImpl = fetch) {
    const started = Date.now();
    if (!apiKey || typeof apiKey !== 'string' || apiKey.length === 0) {
        return { ok: false, message: 'No API key provided', durationMs: Date.now() - started };
    }
    try {
        const res = await fetchImpl(TAVILY_URL, {
            method: 'POST',
            headers: { 'Content-Type': 'application/json' },
            body: JSON.stringify({
                api_key: apiKey,
                query: PROBE_QUERY,
                max_results: 1,
                search_depth: 'basic',
            }),
        });
        const durationMs = Date.now() - started;
        if (res.ok) {
            return { ok: true, durationMs };
        }
        if (res.status === 401 || res.status === 403) {
            return { ok: false, message: 'API key rejected', durationMs };
        }
        if (res.status === 429) {
            return { ok: false, message: 'Rate limited (try again shortly)', durationMs };
        }
        if (res.status >= 500) {
            return { ok: false, message: `Upstream ${res.status}`, durationMs };
        }
        return { ok: false, message: `HTTP ${res.status}`, durationMs };
    }
    catch (err) {
        return {
            ok: false,
            message: err instanceof Error && err.message ? err.message.slice(0, 100) : 'Network error',
            durationMs: Date.now() - started,
        };
    }
}
//# sourceMappingURL=tavily.js.map