import { type ServeOptions, type ServerHandle } from "../api/index.js"; import { type DocumentOptions, type ResolvedConfig } from "../config.js"; import { type AnswerResult, type PromptOptions } from "../llm/index.js"; import { type MetadataFilter } from "../retrieval/filter.js"; import { type RankedList } from "../retrieval/fusion.js"; import { type KeywordHit } from "../retrieval/keyword-index.js"; import { type RetrievalPlan } from "../retrieval/plan.js"; import { type RerankConfig, type Reranker } from "../retrieval/rerank.js"; import { type SearchToolOptions } from "../tools/search-tool.js"; import type { EmbeddingProviderConfig, HybridOptions, IndexMetadata, LLMProviderConfig, RetrievalMode, SearchResult } from "../types.js"; import { type VectorStore } from "../vectordb/index.js"; export interface IndexOptions { /** Defaults to the constructor option, then to the existing index's value, then to 500. */ chunkSize?: number; /** Defaults to the constructor option, then to the existing index's value, then to 50. */ overlap?: number; /** Defaults to the constructor option, then to the existing index's provider and model, then to local. */ embeddings?: EmbeddingProviderConfig; rebuild?: boolean; /** Called after each batch of chunks is embedded. */ onProgress?: (progress: BuildProgress) => void; /** Stops the build between embedding batches; the previous index is kept. */ signal?: AbortSignal; } export interface BuildProgress { /** The document being indexed (file name, URL or fromText id). */ source: string; /** Chunks embedded so far. */ embedded: number; total: number; } export interface SearchOptions { topK?: number; /** Minimum cosine similarity. In hybrid mode it filters the vector results before fusion. */ scoreThreshold?: number; /** Overrides the `retrieval.mode` default (which is "vector"). */ mode?: RetrievalMode; hybrid?: HybridOptions; /** * Only chunks whose metadata matches: `source`, `page`, `pageEnd`, * `section`, `chunk`, `totalChunks` and your own `metadata` fields. */ filter?: MetadataFilter; /** * Reorders the first `rerank.candidates` results with a reranking model * (or your function) before the topK cut. `false` turns off the default. */ rerank?: RerankConfig | Reranker | false; } /** @internal Candidate lists of one document, before fusion. */ export interface RetrievalCandidates { vector: SearchResult[]; /** null when the document has no keyword index (a warning has been logged). */ keyword: KeywordHit[] | null; } export interface AskOptions extends SearchOptions, PromptOptions { llm?: LLMProviderConfig; /** Cancels the LLM request. */ signal?: AbortSignal; } export interface IndexBuildResult { chunkCount: number; cached: boolean; embeddingProvider: string; embeddingModel: string; dimensions: number | null; } /** * Main entry point. * * ```ts * const doc = new Document("./policy.pdf", { * embeddings: { provider: "openai", apiKey: process.env.OPENAI_API_KEY }, * llm: { provider: "anthropic", apiKey: process.env.ANTHROPIC_API_KEY }, * }); * * await doc.build(); * const answer = await doc.ask("What is the refund policy?"); * ``` */ export declare class Document { readonly filePath: string; private readonly namespace; private readonly config; private readonly logger; private readonly store; /** Sidecar file holding the BM25 keyword index; see retrieval/keyword-index.ts. */ private readonly keywordPath; /** Chunking the caller set in the constructor; unset values follow the existing index. */ private readonly chunking; /** Embeddings the caller set in the constructor; if unset, an existing index keeps its own. */ private readonly explicitEmbeddings?; /** Set by fromText(): indexed instead of reading `filePath`. */ private readonly inlineText?; private embedder; private ready; private keywordIndex; private keywordUnavailableWarned; /** * Index text you already have (a DB row, an upload, CMS content) without a * file. `id` names the index: reuse the same id to reuse it, and changed * text is re-indexed on the next build(). `id` is also the chunks' `source`. */ static fromText(id: string, text: string, options?: DocumentOptions): Document; /** `inlineText` is internal; use Document.fromText(). */ constructor(filePath: string, options?: DocumentOptions, inlineText?: string); /** * Build (or reuse) the semantic index for this document. */ build(options?: IndexOptions): Promise; /** * Search the indexed document. `mode` picks vector (default), keyword or * hybrid retrieval. */ search(query: string, options?: SearchOptions): Promise; private retrieve; /** @internal Resolves search options against this document's defaults. */ retrievalPlan(options: SearchOptions): RetrievalPlan; /** * Checks the document-level part of `filter` (source and custom metadata) * and returns the per-chunk part for the store, or null when this * document cannot match. */ private chunkFilter; /** * @internal The vector and keyword candidate lists for a keyword or hybrid * search, before fusion. DocumentCollection fuses these across documents. * Without a keyword index the vector list is returned for every mode, so * the caller can fall back to vector search. */ retrieveCandidates(query: string, plan: RetrievalPlan): Promise; /** * RAG question answering. `options.llm` overrides the constructor default. */ ask(question: string, options?: AskOptions): Promise; /** * Streaming RAG answer. Yields text deltas as they arrive. */ askStream(question: string, options?: AskOptions): AsyncGenerator; /** The `source` of this document's chunks: the file name, URL or fromText id. */ get source(): string; /** Number of chunks currently indexed. */ get chunkCount(): number; /** Filesystem namespace under which this document's index is stored. */ get storeNamespace(): string; /** Underlying resolved configuration (readonly view). */ get resolvedConfig(): Readonly; /** Underlying vector store (advanced use). */ get vectorStore(): VectorStore; /** * An AI SDK tool that searches this document, for `generateText({ tools })` and agents. * * ```ts * await generateText({ model, tools: { searchDocs: doc.asTool() }, prompt }); * ``` */ asTool(options?: SearchToolOptions): ({ title?: string; providerOptions?: import("@ai-sdk/provider-utils").ProviderOptions; metadata?: import("@ai-sdk/provider").JSONObject; inputSchema: import("ai").FlexibleSchema<{ query: string; }>; contextSchema?: import("ai").FlexibleSchema | undefined; needsApproval?: boolean | import("@ai-sdk/provider-utils").ToolNeedsApprovalFunction<{ query: string; }, NoInfer> | undefined; onInputStart?: ((options: import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputDelta?: ((options: { inputTextDelta: string; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputAvailable?: ((options: { input: { query: string; }; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; toModelOutput?: ((options: { toolCallId: string; input: { query: string; }; output: NoInfer; }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike) | undefined; } & { outputSchema?: import("ai").FlexibleSchema | undefined; execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; } & { description?: string | ((options: { context: NoInfer; experimental_sandbox?: import("ai").Experimental_SandboxSession; }) => string) | undefined; strict?: boolean; inputExamples?: { input: NoInfer<{ query: string; }>; }[] | undefined; id?: never; isProviderExecuted?: never; args?: never; supportsDeferredResults?: never; } & { type?: undefined | "function"; } & { execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; }) | ({ title?: string; providerOptions?: import("@ai-sdk/provider-utils").ProviderOptions; metadata?: import("@ai-sdk/provider").JSONObject; inputSchema: import("ai").FlexibleSchema<{ query: string; }>; contextSchema?: import("ai").FlexibleSchema | undefined; needsApproval?: boolean | import("@ai-sdk/provider-utils").ToolNeedsApprovalFunction<{ query: string; }, NoInfer> | undefined; onInputStart?: ((options: import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputDelta?: ((options: { inputTextDelta: string; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputAvailable?: ((options: { input: { query: string; }; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; toModelOutput?: ((options: { toolCallId: string; input: { query: string; }; output: NoInfer; }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike) | undefined; } & { outputSchema?: import("ai").FlexibleSchema | undefined; execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; } & { description?: string | ((options: { context: NoInfer; experimental_sandbox?: import("ai").Experimental_SandboxSession; }) => string) | undefined; strict?: boolean; inputExamples?: { input: NoInfer<{ query: string; }>; }[] | undefined; id?: never; isProviderExecuted?: never; args?: never; supportsDeferredResults?: never; } & { type: "dynamic"; } & { execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; }) | ({ title?: string; providerOptions?: import("@ai-sdk/provider-utils").ProviderOptions; metadata?: import("@ai-sdk/provider").JSONObject; inputSchema: import("ai").FlexibleSchema<{ query: string; }>; contextSchema?: import("ai").FlexibleSchema | undefined; needsApproval?: boolean | import("@ai-sdk/provider-utils").ToolNeedsApprovalFunction<{ query: string; }, NoInfer> | undefined; onInputStart?: ((options: import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputDelta?: ((options: { inputTextDelta: string; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputAvailable?: ((options: { input: { query: string; }; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; toModelOutput?: ((options: { toolCallId: string; input: { query: string; }; output: NoInfer; }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike) | undefined; } & { outputSchema?: import("ai").FlexibleSchema | undefined; execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; } & { type: "provider"; id: `${string}.${string}`; args: Record; description?: never; strict?: never; inputExamples?: never; } & { isProviderExecuted: false; supportsDeferredResults?: never; } & { execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; }) | ({ title?: string; providerOptions?: import("@ai-sdk/provider-utils").ProviderOptions; metadata?: import("@ai-sdk/provider").JSONObject; inputSchema: import("ai").FlexibleSchema<{ query: string; }>; contextSchema?: import("ai").FlexibleSchema | undefined; needsApproval?: boolean | import("@ai-sdk/provider-utils").ToolNeedsApprovalFunction<{ query: string; }, NoInfer> | undefined; onInputStart?: ((options: import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputDelta?: ((options: { inputTextDelta: string; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; onInputAvailable?: ((options: { input: { query: string; }; } & import("ai").ToolExecutionOptions>) => void | PromiseLike) | undefined; toModelOutput?: ((options: { toolCallId: string; input: { query: string; }; output: NoInfer; }) => import("@ai-sdk/provider-utils").ToolResultOutput | PromiseLike) | undefined; } & { outputSchema?: import("ai").FlexibleSchema | undefined; execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; } & { type: "provider"; id: `${string}.${string}`; args: Record; description?: never; strict?: never; inputExamples?: never; } & { isProviderExecuted: true; supportsDeferredResults?: boolean; } & { execute: import("ai").ToolExecuteFunction<{ query: string; }, import("../tools/search-tool.js").Passage[], NoInfer>; }); /** * Launch a REST API server (Hono + Node) exposing this document. */ serve(options?: ServeOptions): Promise; private vectorSearch; /** BM25 (or the store's native) keyword search; null when no keyword index exists. */ private keywordSearch; /** * Drops hits a custom store returned despite the filter, and adds the * document's custom metadata to each hit. */ private finishHits; private loadKeywordIndex; private saveKeywordIndex; private ensureReady; } /** Indexes written before `formatVersion` existed are identified by package version. */ export declare function indexFormatVersion(metadata: IndexMetadata): number | null; /** The candidate lists to fuse for a keyword or hybrid plan. */ export declare function fusionLists(plan: RetrievalPlan, candidates: { vector: SearchResult[]; keyword: KeywordHit[]; }): RankedList[]; /** The retrieval-related subset of ask() options. */ export declare function searchOptionsOf(options: SearchOptions): SearchOptions;