/** * Web fetcher + indexer for `fetch_and_index`. * * Flow (single URL): * 1. SSRF guard: assertUrlSafe resolves + classifies the hostname's IPs. * 2. TTL cache check: if a fresh entry exists for this (source, url) key, * return a cache-hit result WITHOUT fetching or indexing. * 3. fetchWithSsrfGuard: manual redirect walk — each hop re-resolved + classified. * 4. Content-Length + read byte cap (MAX_FETCH_BYTES) — reject oversized before * slurping the body into the long-running process heap. * 5. Convert: HTML → markdown (turndown+gfm), JSON → key-path chunks, * everything else → plain text. * 6. Index SERIALY via the injected `indexChunk` seam (single-writer safety — * the underlying PostgreSQL FTS5 / pgvector stores are not safe under * concurrent writers, and even pgvector embeddings serialize better one * doc at a time). The PARALLEL phase is the fetch (run-pool in the * controller); the SERIAL phase is always this drain. * * The indexing layer is injected (`WebIndexDeps`) so unit tests can capture * indexed chunks against an in-memory map instead of the real vector store. */ import { type FetchGuardOptions } from "./ssrf.js"; /** Hard cap on fetched body size (50 MB). Checked against Content-Length AND * the actual bytes read (defense-in-depth: a lying Content-Length or chunked * transfer can still slip past the header check). */ export declare const MAX_FETCH_BYTES: number; /** Default TTL for the fetch cache (24h). */ export declare const DEFAULT_FETCH_TTL_MS: number; export type FetchContentType = "html" | "json" | "text"; /** A single indexed chunk produced from a fetch. */ export interface IndexedChunk { /** Stable id: projectId:url-hash:idx — unique per (url, position). */ id: string; content: string; metadata: Record; } /** Per-fetch result returned to the controller. */ export type FetchOneResult = { kind: "cached"; url: string; source: string; chunkCount: number; ageMs: number; } | { kind: "fetched"; url: string; source: string; contentType: FetchContentType; chunks: IndexedChunk[]; bytes: number; } | { kind: "error"; url: string; error: string; }; /** * Indexing seams injected by the controller. Keeping these as an interface (not * a hard dependency on the vector/keyword stores) lets tests assert indexed * chunks without a DB, and lets the controller compose vector+keyword writes. */ export interface WebIndexDeps { /** * Index one chunk into BOTH stores. Called SERIALY by the fetcher (caller * awaits each call). Implementations typically do: * await Promise.all([ * vectorStore.addDocuments([doc]), * keywordSearch.index(doc.id, doc.content, doc.metadata), * ]); */ indexChunk(chunk: IndexedChunk): Promise; /** * Return the timestamp (ms since epoch) this (source,url) was last indexed, * or `null` if unknown / stale-beyond-repair. Used for TTL cache hits. */ getLastIndexedAt?(cacheKey: string): number | null; /** Record that indexing just finished for this cacheKey (post-write). */ markIndexed?(cacheKey: string, ts: number): void; } /** Hash the (source, url) tuple into a stable cache/storage key. */ export declare function composeFetchCacheKey(source: string | undefined, url: string): string; export interface FetchOneOptions extends FetchGuardOptions { source?: string; /** Skip cache and re-fetch. */ force?: boolean; /** Override cache window for this call (ms). `0` bypasses cache like force. */ ttl?: number; /** projectId scoping the indexed chunks. Defaults to a synthetic web pid. */ projectId?: string; } /** * Fetch + convert + index a single URL. Safe to run in PARALLEL on the fetch * phase; the indexing drain inside is SERIAL (awaits indexChunk per chunk). * * `projectId` defaults to `"web"` so fetched content lives in its own scope * unless the caller namespaces it. Tests should pass a throwaway pid to avoid * polluting any shared index. */ export declare function fetchAndConvertOne(url: string, deps: WebIndexDeps, opts?: FetchOneOptions): Promise; //# sourceMappingURL=fetcher.d.ts.map