/** * Query Understanding (Phase 2, plan item 4). * * Expands the user's query before the vector/keyword fan-out: * 1. `rewriteQuery` — LLM produces structured `{ expansions, keywords }` * via `llmObject` + a zod schema (rejects malformed/empty output). * 2. `hyde` — LLM produces a hypothetical implementation paragraph, which * is then **embedded with the existing EmbeddingService** (no new * provider spawned) and returned as an extra vector-query stream. * * Contract (cross-cutting §1 + spec NF1): * - Default-off via `config.search.queryUnderstanding.enabled`. * - Silent degradation: on any LLM throw / timeout / disabled, `understand()` * returns `null` → caller falls through to the original 2-stream search. * NEVER blocks search, NEVER throws to the caller. * - HyDE's embed call runs ONLY after the LLM step succeeds (no wasted work). * * Pure over its inputs + the injected `llm` handle + the injected `embedFn`, * so tests inject fakes without touching config or network. */ import { z } from "zod"; /** Non-empty arrays, bounded length. `llmObject` returns `{ok:false}` on zod failure. */ export declare const QueryRewriteSchema: z.ZodObject<{ expansions: z.ZodArray; keywords: z.ZodArray; }, z.core.$strip>; export type QueryRewrite = z.infer; /** Injectable LLM surface (matches the `llm` export from llm-client.ts). */ export interface QueryLlmSurface { complete(prompt: string, opts?: { system?: string; timeoutMs?: number; modelRole?: "instruct" | "code"; }): Promise<{ ok: boolean; value?: string; error?: string; }>; object(prompt: string, schema: z.ZodSchema, opts?: { system?: string; timeoutMs?: number; modelRole?: "instruct" | "code"; }): Promise<{ ok: boolean; value?: T; error?: string; }>; isEnabled(): boolean; } /** Inject an embed function so tests don't touch the EmbeddingService singleton. */ export type EmbedFn = (text: string) => Promise; export interface QueryUnderstandingResult { expansions: string[]; keywords: string[]; /** Null when HyDE is disabled, the LLM step failed, or the embed call threw. */ hydeVector: number[] | null; } /** * Rewrite the query into structured expansions + keywords. Returns `null` * when the LLM is disabled, times out, throws, or returns zod-invalid output * (P2-REWRITE-01/02/03). Never throws. */ export declare function rewriteQuery(query: string, surface: QueryLlmSurface, opts?: { timeoutMs?: number; }): Promise; /** * Generate a hypothetical implementation paragraph and embed it. The embed * call runs ONLY if the LLM step succeeded (P2-HYDE-02). Uses the **existing** * EmbeddingService via the injected `embedFn` (P2-HYDE-03). Returns `null` on * any failure; never throws. */ export declare function hyde(query: string, surface: QueryLlmSurface, embedFn: EmbedFn, opts?: { timeoutMs?: number; }): Promise; /** * Orchestrates rewrite + HyDE behind a per-`(projectId, query)` cache. * `understand()` returns `null` on any failure path → caller falls through. */ export declare class QueryUnderstandingService { private readonly llmSurface; private readonly embedFn; private readonly cache; /** Capture the last-call telemetry for tests/observability. */ lastHydeUsed: boolean; constructor(opts?: { llmSurface?: QueryLlmSurface; embedFn?: EmbedFn; cacheTtlMs?: number; cacheMaxSize?: number; }); /** * Run rewrite + HyDE for `(query, projectId)`. Cached per the configured TTL. * Returns `null` on any failure / disabled → caller's original search path. * Never throws. */ understand(query: string, projectId: string): Promise; /** @internal — clear cache between tests. */ clearCache(): void; } /** * Build a quoted FTS5 OR query from the original query + keywords. Each term * is FTS5-quoted (internal `"` doubled) and joined with `OR` so the keyword * search treats them as a recall-broadening disjunction. Mirrors the per-term * quoting that `sanitizeFTS5Query` applies, but operates on already-distinct * terms (calling sanitizeFTS5Query on the composed string would re-split the * quoted phrases). Returns a sanitized form of the original query when no * keywords survive cleaning. */ export declare function buildRewrittenFTSQuery(query: string, keywords: string[]): string; //# sourceMappingURL=query-understanding.d.ts.map