src / providers / SerperProvider.ts
src / providers / SerperProvider.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 SERPER_SEARCH_URL = "https://google.serper.dev/search";
export interface SerperProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
cooldownHours?: number;
}
/**
* Serper (Google Search API wrapper) search provider implementation.
* Mapped from Python `providers/serper.py` SerperProvider.
*/
export class SerperProvider extends BaseProvider {
public static readonly PROVIDER_NAME = "serper";
public static readonly DISPLAY_NAME = "Serper";
private httpClient: HTTPClient;
constructor(options: SerperProviderOptions = {}) {
super({
providerName: SerperProvider.PROVIDER_NAME,
displayName: SerperProvider.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 web search using Serper 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> = {
"X-API-KEY": apiKey.trim(),
"Content-Type": "application/json",
};
const payload: JsonObject = {
q: query.trim(),
num: numResults,
...extraKwargs,
};
let response;
try {
response = await this.httpClient.post(SERPER_SEARCH_URL, {
json: payload,
headers,
});
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`Serper search HTTP request failed: ${err.message}`);
}
const data = this.parseJsonResponse<Record<string, unknown>>(response);
const rawResults = data.organic;
if (rawResults === undefined || rawResults === null) {
return [];
}
if (!Array.isArray(rawResults)) {
throw new ProviderError(
"Invalid response payload from Serper API: 'organic' 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 url = (rawItem.link as string) || (rawItem.url as string) || "";
const snippetCandidate = (rawItem.snippet as string) || "";
return {
title: String(rawItem.title || ""),
url: String(url),
snippetCandidate,
publishedDate:
(rawItem.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 SERPER_SEARCH_URL = "https://google.serper.dev/search";
export interface SerperProviderOptions {
keys?: string[];
keyManager?: KeyManager;
httpClient?: HTTPClient;
cooldownHours?: number;
}
/**
* Serper (Google Search API wrapper) search provider implementation.
* Mapped from Python `providers/serper.py` SerperProvider.
*/
export class SerperProvider extends BaseProvider {
public static readonly PROVIDER_NAME = "serper";
public static readonly DISPLAY_NAME = "Serper";
private httpClient: HTTPClient;
constructor(options: SerperProviderOptions = {}) {
super({
providerName: SerperProvider.PROVIDER_NAME,
displayName: SerperProvider.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 web search using Serper 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> = {
"X-API-KEY": apiKey.trim(),
"Content-Type": "application/json",
};
const payload: JsonObject = {
q: query.trim(),
num: numResults,
...extraKwargs,
};
let response;
try {
response = await this.httpClient.post(SERPER_SEARCH_URL, {
json: payload,
headers,
});
} catch (exc: unknown) {
const err = exc instanceof Error ? exc : new Error(String(exc));
throw new ProviderError(`Serper search HTTP request failed: ${err.message}`);
}
const data = this.parseJsonResponse<Record<string, unknown>>(response);
const rawResults = data.organic;
if (rawResults === undefined || rawResults === null) {
return [];
}
if (!Array.isArray(rawResults)) {
throw new ProviderError(
"Invalid response payload from Serper API: 'organic' 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 url = (rawItem.link as string) || (rawItem.url as string) || "";
const snippetCandidate = (rawItem.snippet as string) || "";
return {
title: String(rawItem.title || ""),
url: String(url),
snippetCandidate,
publishedDate:
(rawItem.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" },
};
}
}