import { Json, ModuleFilePath, SerializedSchema, SourcePath } from "@valbuild/core"; /** * The search index and the labels to render for its hits. * * Kept as a plain module rather than living inside a worker so it can be tested: * a worker entry runs `self.onmessage` on import, which no test environment here * provides. * * In `@valbuild/shared` rather than the Studio because there are now two * realms searching the same content: the Studio's `SearchStore`, which keeps an * index alive in a worker and re-indexes a module at a time, and the MCP * `search_content` tool, which builds one per call and throws it away. What * counts as a document, and what a hit is labelled, must not differ between * them — an agent and an editor searching the same project should find the same * things. */ /** * The parts of FlexSearch's `Index` this module uses. * * Structural, rather than importing flexsearch's own `Index`, because * {@link SearchIndex} is EXPORTED — and a `.d.ts` that names `flexsearch` pulls * that package's `declare module "flexsearch"` into every consumer's TypeScript * program. Ambient module declarations are global to a program, so a consumer * with its own flexsearch at another version then checks its OWN calls against * the copy we dragged in, whichever of the two wins. * * That is not hypothetical: `@valbuild/shared` gained a flexsearch dependency * in 0.123.0 for `search_content`, and valbuild/web — which pins flexsearch * 0.7 and calls `new flexsearch.Document({ language: "en", … })` — stopped * building on 0.123.2, because 0.8's `DocumentOptions` has no `language`. Its * own resolved copy was still 0.7; only the types had been replaced. * * Three methods is all this module calls, and none of their signatures here is * a guess: `add` and `remove` are used in `indexModule`/`removeModule`, and * `search`'s result is only ever `.length`-ed, `.slice`-d and mapped over as * ids. `FlexSearch.Index` is still constructed with the real library in * {@link createSearchIndex} — a value import, which does not reach the `.d.ts`. */ type SearchableIndex = { add(id: string, content: string): void; remove(id: string): void; search(query: string, options: { limit: number; }): (string | number)[]; }; export type SearchIndex = { index: SearchableIndex; pathToLabel: Map; /** * Which document ids belong to which module, so one module can be re-indexed * without touching the others. * * The ids are the `SourcePath`s themselves — `index.add(path, …)` below — so * this could be recovered by scanning `pathToLabel` for a module-file-path * prefix. It is kept explicitly anyway: the scan would be O(every document in * the project) per module re-indexed, which is the whole-project cost that * re-indexing one module exists to avoid. */ docsByModule: Map>; /** * For a module whose root is a record: each entry's documents, and the * value they were made from, by key -- so indexing it again walks only the * entries whose value is a different object (see {@link indexModule}). */ /** * What each module was last indexed from, whole: the same source object * under a schema that says the same is not walked again. Removing a * document from a big index is slow (FlexSearch looks for it everywhere), * so indexing an unchanged module again was the cost of a pass. */ walkedByModule: Map; entriesByModule: Map; }>; }; /** An index with nothing in it. Fill it with {@link indexModule}. */ export declare function createSearchIndex(): SearchIndex; /** * Index one module, replacing whatever was previously indexed for it. * * Remove-then-add rather than `update`, because the set of paths in a module is * not stable: deleting an array item or a record entry means documents that must * disappear, and `update` only revises ids it is given. * * `.jsonValues()` entries are indexed only once their content is loaded — an * un-loaded entry is an opaque marker that {@link traverseSchemaSource} skips, so * the index is naturally PARTIAL and grows as batches land. That is why the search * UI loads the entries on the first query and re-indexes as they arrive, rather * than pretending an empty result set is an answer -- and why a module whose * root is a record is re-indexed by entry ({@link indexRecordModule}): a pass * per batch walks the batch, not the record. */ export declare function indexModule(searchIndex: SearchIndex, moduleFilePath: ModuleFilePath, source: Json, schema: SerializedSchema, /** * Stop by this time (`performance.now()`), for a caller that must not hold * the thread: answers `false` when it stopped with entries of a record * module still to walk, and the same call again goes on from there. A * module that is not a record is walked whole, whatever the time. */ until?: number): boolean; /** * Drop everything indexed for one module. * * Also used for a module that has gone away entirely — otherwise its documents * stay searchable and a hit navigates to a path that no longer exists. */ export declare function removeModule(searchIndex: SearchIndex, moduleFilePath: ModuleFilePath): void; /** * Index every module, from scratch. * * Now a loop over {@link indexModule} rather than its own walk, so the full * build and the incremental one cannot drift in what they consider a document. */ export declare function buildSearchIndex(modules: Record): SearchIndex; export type SearchResults = { results: Array<{ path: SourcePath; label: string; }>; /** Matches for the query, not just on this page. */ total: number; /** `total` hit {@link MAX_COUNTED_RESULTS}, so it means "at least this many". */ totalIsLowerBound: boolean; }; export declare function performSearch(searchIndex: SearchIndex | null, query: string, limit?: number, offset?: number): SearchResults; export {};