src / providers / TavilyProvider.ts
src / providers / TavilyProvider.ts
import { HTTPClient, JsonObject } from "../http/HTTPClient";
import { ProviderError } from "../models/errors";
import { SearchOptions } from "../types/search";
import { KeyManager } from "../utils/KeyManager";
import { BaseProvider } from "./BaseProvider";
import { InternalProviderResult } from "./types/InternalProviderResult";
export const TAVILY_SEARCH_URL = "https://api.tavily.com/search";
export interface TavilyProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
cooldownHours?: number;
}
/**
* Tavily search engine provider implementation.
* Mapped from Python `providers/tavily.py` TavilyProvider.
*/
export class TavilyProvider extends BaseProvider {
public static readonly PROVIDER_NAME = "tavily";
public static readonly DISPLAY_NAME = "Tavily";
private httpClient: HTTPClient;
constructor(options: TavilyProviderOptions = {}) {
super({
providerName: TavilyProvider.PROVIDER_NAME,
displayName: TavilyProvider.DISPLAY_NAME,
capabilities: ["web"],
keys: options.keys,
keyManager: options.keyManager,
cooldownHours: options.cooldownHours,
});
this._rotationStatusCodes = [401, 403, 429];
this.httpClient = options.httpClient ?? new HTTPClient();
}
/**
* Execute search using Tavily search API endpoint with key rotation.
*/
public async search(query: string, options: SearchOptions = {}): Promise<InternalProviderResult[]> {
if (!query || !query.trim()) {
return [];
}
const numResults = options.numResults ?? 10;
const { apiKey: _k, numResults: _n, provider: _p, ...extraKwargs } = options;
const doSearch = async (apiKey: string): Promise<InternalProviderResult[]> => {
const headers: Record<string, string> = {
"Content-Type": "application/json",
accept: "application/json",
};
const payload: JsonObject = {
api_key: apiKey.trim(),
query: query.trim(),
max_results: numResults,
...extraKwargs,
};
let response;
try {
response = await this.httpClient.post(TAVILY_SEARCH_URL, {
json: payload,
headers,
});
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`Tavily search HTTP request failed: ${err.message}`);
}
const data = this.parseJsonResponse<Record<string, unknown>>(response);
if (!("results" in data)) {
throw new ProviderError("Invalid response payload from Tavily API: missing 'results' array");
}
const rawResults = data.results;
if (!Array.isArray(rawResults)) {
throw new ProviderError("Invalid response payload from Tavily API: 'results' field must be a list");
}
this._lastRawResultsCount = rawResults.length;
const candidates = rawResults.map((item: unknown) =>
this.extractCandidate((item as Record<string, unknown>) || {})
);
return this.enforceResultLimit(candidates, numResults);
};
return this.executeWithKeyRotation(doSearch);
}
private extractCandidate(rawItem: Record<string, unknown>): InternalProviderResult {
const snippetCandidate = (rawItem.content as string) || (rawItem.snippet as string) || "";
return {
title: String(rawItem.title || ""),
url: String(rawItem.url || ""),
snippetCandidate,
publishedDate: (rawItem.published_date as string) || (rawItem.publishedDate as string) || undefined,
source: this.providerName,
score: typeof rawItem.score === "number" ? rawItem.score : undefined,
raw: rawItem,
debug: { snippetSource: "native_snippet" },
};
}
}
import { HTTPClient, JsonObject } from "../http/HTTPClient";
import { ProviderError } from "../models/errors";
import { SearchOptions } from "../types/search";
import { KeyManager } from "../utils/KeyManager";
import { BaseProvider } from "./BaseProvider";
import { InternalProviderResult } from "./types/InternalProviderResult";
export const TAVILY_SEARCH_URL = "https://api.tavily.com/search";
export interface TavilyProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
cooldownHours?: number;
}
/**
* Tavily search engine provider implementation.
* Mapped from Python `providers/tavily.py` TavilyProvider.
*/
export class TavilyProvider extends BaseProvider {
public static readonly PROVIDER_NAME = "tavily";
public static readonly DISPLAY_NAME = "Tavily";
private httpClient: HTTPClient;
constructor(options: TavilyProviderOptions = {}) {
super({
providerName: TavilyProvider.PROVIDER_NAME,
displayName: TavilyProvider.DISPLAY_NAME,
capabilities: ["web"],
keys: options.keys,
keyManager: options.keyManager,
cooldownHours: options.cooldownHours,
});
this._rotationStatusCodes = [401, 403, 429];
this.httpClient = options.httpClient ?? new HTTPClient();
}
/**
* Execute search using Tavily search API endpoint with key rotation.
*/
public async search(query: string, options: SearchOptions = {}): Promise<InternalProviderResult[]> {
if (!query || !query.trim()) {
return [];
}
const numResults = options.numResults ?? 10;
const { apiKey: _k, numResults: _n, provider: _p, ...extraKwargs } = options;
const doSearch = async (apiKey: string): Promise<InternalProviderResult[]> => {
const headers: Record<string, string> = {
"Content-Type": "application/json",
accept: "application/json",
};
const payload: JsonObject = {
api_key: apiKey.trim(),
query: query.trim(),
max_results: numResults,
...extraKwargs,
};
let response;
try {
response = await this.httpClient.post(TAVILY_SEARCH_URL, {
json: payload,
headers,
});
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`Tavily search HTTP request failed: ${err.message}`);
}
const data = this.parseJsonResponse<Record<string, unknown>>(response);
if (!("results" in data)) {
throw new ProviderError("Invalid response payload from Tavily API: missing 'results' array");
}
const rawResults = data.results;
if (!Array.isArray(rawResults)) {
throw new ProviderError("Invalid response payload from Tavily API: 'results' field must be a list");
}
this._lastRawResultsCount = rawResults.length;
const candidates = rawResults.map((item: unknown) =>
this.extractCandidate((item as Record<string, unknown>) || {})
);
return this.enforceResultLimit(candidates, numResults);
};
return this.executeWithKeyRotation(doSearch);
}
private extractCandidate(rawItem: Record<string, unknown>): InternalProviderResult {
const snippetCandidate = (rawItem.content as string) || (rawItem.snippet as string) || "";
return {
title: String(rawItem.title || ""),
url: String(rawItem.url || ""),
snippetCandidate,
publishedDate: (rawItem.published_date as string) || (rawItem.publishedDate as string) || undefined,
source: this.providerName,
score: typeof rawItem.score === "number" ? rawItem.score : undefined,
raw: rawItem,
debug: { snippetSource: "native_snippet" },
};
}
}