/** * `PublicSearchProvider`: a `WebSearchProvider` that fans one query out to all * the credential-free public engines concurrently — Startpage → DuckDuckGo → * Ecosia → Google → Mojeek — and consolidates their answers. No API key or * credential is required. The fan-out races three exits and returns at the * earliest: every engine settled; the soft deadline elapsed with at least one * success in hand; the hard deadline elapsed regardless. Sources are * deduplicated across engines and ranked by cross-engine consensus (how many * engines returned a URL — one vote per engine per canonical URL), then by * best per-engine rank. Individual engine failures are tolerated; the call * fails only when no engine accepted the search. * * Engine outcomes are structured ({@link EngineSearchFailure} kinds), not * message-regexed: a challenge wall trips that engine's circuit breaker * immediately, HTTP 4xx is a hard block, HTTP 5xx/timeouts/network failures * are retryable, and an accepted empty page is a *success* — `[]` is reserved * for an accepted empty search, never for "everything failed". The hard * deadline is one overall budget across every retry attempt, and the provider * returns the full merged list (the web seam caps it to `request.maxResults` * and flags truncation there). Unloading drains: `dispose()` aborts in-flight * searches and waits for them to settle. * @module @hy-sde-org/dsh-web-search-public/provider */ import type { WebSearchProvider, WebSearchRequest, WebSearchResult, WebSearchSource } from '@deepseek-ai/dsh-web'; import type { PublicEngine } from './types.ts'; /** Stable id this provider registers under in `ctx.web`. */ export declare const PUBLIC_PROVIDER_ID = "public"; /** * Soft aggregate deadline (ms): past this point the fan-out returns as soon as * it has at least one engine's sources. Fast HTML engines answer well under * this; the deadline is the latency floor for stragglers the aggregate waits * on to enrich consensus. */ export declare const SOFT_DEADLINE_MS = 5000; /** * Hard aggregate deadline (ms): the OVERALL budget for one `search` call — * shared across every retry attempt and its backoff — after which the fan-out * returns whatever it has, even nothing, so one pathologically slow engine can * never pin the call to the search-tool budget. */ export declare const HARD_DEADLINE_MS = 30000; /** * Retries for the all-engines-failed aggregate when at least one engine died * from a *retryable* transport failure (HTTP 5xx, a timeout, or a network-level * fetch failure). A retry re-runs the whole fan-out after a backoff, so a short * engine throttle that strips the first attempt still yields results. Hard * blocks (HTTP 4xx — the anti-bot rate-limit signature) and challenge walls are * never retried: they open that engine's circuit breaker instead. 0 disables * retrying. */ export declare const DEFAULT_MAX_RETRIES = 1; /** Base delay before retry #1 (ms); doubles on each subsequent attempt. */ export declare const DEFAULT_RETRY_DELAY_MS = 2000; /** * Fast-fail window (ms) after an all-engines-down failure (retry exhausted, or * every engine hard-blocked/tripped): searches in this window return a clear * "retry in about Ns" error instead of re-blasting engines that just throttled * us, which would deepen the block. 0 disables the window. Default: 5 minutes. */ export declare const DEFAULT_FAILURE_COOLDOWN_MS = 300000; /** * Base per-engine circuit-breaker window (ms): an engine that hard-blocks * (HTTP 4xx), answers with a challenge wall, or hangs past the aggregate * deadline is skipped for this long instead of being re-blasted on every * search. The window doubles per consecutive trip up to * {@link DEFAULT_MAX_ENGINE_BACKOFF_MS} and resets after a successful probe. * 0 disables the per-engine breaker. Default: 5 minutes. */ export declare const DEFAULT_ENGINE_BACKOFF_MS = 300000; /** Cap for the doubled per-engine circuit-breaker window. Default: 1 hour. */ export declare const DEFAULT_MAX_ENGINE_BACKOFF_MS = 3600000; /** Resolved provider options (the plugin's `apply` supplies config defaults). */ export interface PublicSearchProviderOptions { /** * Engines the fan-out races concurrently. Order is the tiebreak for merged * ranking (earlier engines win equal consensus/rank). */ readonly engines: readonly PublicEngine[]; /** Per-engine transport timeout (ms); an engine stalls if it ignores abort. */ readonly timeoutMs: number; /** Soft aggregate deadline (ms). Default: {@link SOFT_DEADLINE_MS}. */ readonly softDeadlineMs?: number; /** Hard aggregate deadline (ms) — the overall budget across retries. Default: {@link HARD_DEADLINE_MS}. */ readonly hardDeadlineMs?: number; /** * Retries for the all-failed aggregate on the retryable transport signature. * Default: {@link DEFAULT_MAX_RETRIES}. 0 disables. */ readonly maxRetries?: number; /** Base retry delay (ms), doubled per attempt. Default: {@link DEFAULT_RETRY_DELAY_MS}. */ readonly retryDelayMs?: number; /** * Fast-fail window (ms) after an all-engines-down failure. * Default: {@link DEFAULT_FAILURE_COOLDOWN_MS}. 0 disables. */ readonly failureCooldownMs?: number; /** * Base per-engine circuit-breaker window (ms); doubles per consecutive trip * up to `maxEngineBackoffMs`. Default: {@link DEFAULT_ENGINE_BACKOFF_MS}. * 0 disables the per-engine breaker. */ readonly engineBackoffMs?: number; /** * Cap for the doubled per-engine circuit-breaker window. * Default: {@link DEFAULT_MAX_ENGINE_BACKOFF_MS}. */ readonly maxEngineBackoffMs?: number; } /** Accumulator for one deduplicated URL across engines. */ export interface MergedSource { source: WebSearchSource; /** Number of engines that returned this URL — the primary ranking signal. */ engines: number; /** Best (lowest) per-engine rank observed. */ bestRank: number; /** First-seen insertion index; final tiebreak keeps ordering deterministic. */ order: number; } /** * Canonical dedup key for a result URL: case-normalized host without a leading * `www.`, path without a trailing slash, query preserved, fragment dropped. * Engines disagree on exactly these variations for the same page. */ export declare function dedupKey(rawUrl: string): string; /** * Merge one engine's ranked sources into the accumulator map. One engine * answer casts ONE vote per canonical URL: several URL variants of the same * page within a single answer (www./trailing slash/…) count once, at the best * rank — cross-engine consensus stays meaningful. Exported for tests. */ export declare function mergeSources(merged: Map, sources: readonly WebSearchSource[]): void; /** The credential-free public web search provider. */ export declare class PublicSearchProvider implements WebSearchProvider { private readonly options; readonly id = "public"; /** Resolved retry count (see {@link PublicSearchProviderOptions.maxRetries}). */ private readonly maxRetries; /** Resolved base retry delay (see {@link PublicSearchProviderOptions.retryDelayMs}). */ private readonly retryDelayMs; /** Resolved fast-fail window (see {@link PublicSearchProviderOptions.failureCooldownMs}). */ private readonly failureCooldownMs; /** Resolved per-engine breaker base window (see {@link PublicSearchProviderOptions.engineBackoffMs}). */ private readonly engineBackoffMs; /** Resolved per-engine breaker cap (see {@link PublicSearchProviderOptions.maxEngineBackoffMs}). */ private readonly maxEngineBackoffMs; /** Epoch ms of the last all-engines-down failure; 0 = none yet. */ private lastAllFailureAt; /** Aggregate message of that last failure, surfaced by fast-fails. */ private lastAllFailureMessage; /** Per-engine circuit-breaker state, keyed by engine id. */ private readonly health; /** Aborted when the provider is disposed; composed into every search signal. */ private readonly lifetime; /** In-flight searches, tracked so {@link dispose} can drain them. */ private readonly active; /** * Engine-level work promises: fan-out aggregates AND the raw engine * operations themselves. An early deadline return abandons the aggregate * while engines are still settling, and an aggregate settles when each * attempt's timeout expires even though an aborted engine may still be * running async cleanup — so the raw operations are tracked through FINAL * settlement. Dispose drains both, or unload leaves engine work (browser * navigations, transport timers) running. */ private readonly inflight; private disposed; constructor(options: PublicSearchProviderOptions); /** Credential-free: usable whenever at least one engine is configured. */ available(): boolean; /** * Abort every in-flight search and wait for it to settle. The plugin * registers this as its unload disposer, so unloading drains in-flight work * (bounded by each engine's transport timeout) instead of abandoning it. * Idempotent; later searches throw `WEB_PROVIDER_UNAVAILABLE`. */ dispose(): Promise; /** * Track a raw engine operation through its FINAL settlement, independently * of the attempt facade's timeout result: `Effect.timeout` settles the * attempt while the underlying promise may still be tearing down. */ private trackEngineOp; search(request: WebSearchRequest, signal?: AbortSignal): Promise; /** * The retry loop over fan-out passes. The hard deadline is ONE overall * budget: `deadlineAt` is fixed once, every pass clamps into what remains, * and the backoff sleep is bounded by it — a slow first attempt leaves the * retry only what is left, and an exhausted budget skips the retry entirely. */ private searchAll; /** * One fan-out pass over every engine (the pre-retry aggregate): races the * soft/hard deadlines (clamped into the overall budget), merges the * consensus result, and throws {@link AllFailedError} when NO engine * accepted the search — engines still pending at the hard deadline count as * failures ("did not settle"). When at least one engine accepted the search * (results or an accepted empty page) the pass succeeds. */ private fanOut; /** Whether an engine's circuit breaker is open right now. */ private isTripped; /** Open an engine's breaker and count the trip for the aggregate classifier. */ private recordTrip; /** Open an engine's circuit breaker; returns whether the breaker is armed. */ private trip; /** An accepted search (results or an accepted empty page) resets the backoff window. */ private recordSuccess; private healthOf; /** * Run one engine with a per-engine transport deadline. * * The engine's own promise is never abandoned by management: the deadline * and the caller's aggregate signal both abort the engine's controller, and * on timeout the `Effect.timeout` interrupt still runs the Scope finalizer * (which signals the engine), so a signal-ignoring engine becomes a zombie * exactly like the old `Promise.race` — never a hang. * * `Effect.timeout` fails with `Cause.TimeoutError` on expiry; the facade * maps that back to the `timedOut` attempt kind the aggregate understands. * Deliberately not `Effect.timeoutOption`: the aggregate needs the timeout * distinguishable from a value-less success. * * {@link EngineSearchFailure} rejections map onto their structured attempt * kinds; plain `Error`s keep the legacy `failed` kind and its message * signatures. */ private attempt; } //# sourceMappingURL=provider.d.ts.map