index.d.ts 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /**
  2. * QMD SDK - Library mode for programmatic access to QMD search and indexing.
  3. *
  4. * Usage:
  5. * import { createStore } from '@tobilu/qmd'
  6. *
  7. * const store = await createStore({
  8. * dbPath: './my-index.sqlite',
  9. * config: {
  10. * collections: {
  11. * docs: { path: '/path/to/docs', pattern: '**\/*.md' }
  12. * }
  13. * }
  14. * })
  15. *
  16. * const results = await store.search({ query: "how does auth work?" })
  17. * await store.close()
  18. */
  19. import { extractSnippet, addLineNumbers, DEFAULT_MULTI_GET_MAX_BYTES, type Store as InternalStore, type DocumentResult, type DocumentNotFound, type SearchResult, type HybridQueryResult, type HybridQueryOptions, type HybridQueryExplain, type ExpandedQuery, type StructuredSearchOptions, type MultiGetResult, type IndexStatus, type IndexHealthInfo, type SearchHooks, type ReindexProgress, type ReindexResult, type EmbedProgress, type EmbedResult, type ChunkStrategy } from "./store.js";
  20. import { type Collection, type CollectionConfig, type NamedCollection, type ContextMap } from "./collections.js";
  21. export type { DocumentResult, DocumentNotFound, SearchResult, HybridQueryResult, HybridQueryOptions, HybridQueryExplain, ExpandedQuery, StructuredSearchOptions, MultiGetResult, IndexStatus, IndexHealthInfo, SearchHooks, ReindexProgress, ReindexResult, EmbedProgress, EmbedResult, Collection, CollectionConfig, NamedCollection, ContextMap, };
  22. export type { InternalStore };
  23. export { extractSnippet, addLineNumbers, DEFAULT_MULTI_GET_MAX_BYTES };
  24. export type { ChunkStrategy } from "./store.js";
  25. export { getDefaultDbPath } from "./store.js";
  26. export { Maintenance } from "./maintenance.js";
  27. /**
  28. * Progress info emitted during update() for each file processed.
  29. */
  30. export type UpdateProgress = {
  31. collection: string;
  32. file: string;
  33. current: number;
  34. total: number;
  35. };
  36. /**
  37. * Aggregated result from update() across all collections.
  38. */
  39. export type UpdateResult = {
  40. collections: number;
  41. indexed: number;
  42. updated: number;
  43. unchanged: number;
  44. removed: number;
  45. needsEmbedding: number;
  46. };
  47. /**
  48. * Options for the unified search() method.
  49. */
  50. export interface SearchOptions {
  51. /** Simple query string — will be auto-expanded via LLM */
  52. query?: string;
  53. /** Pre-expanded queries (from expandQuery) — skips auto-expansion */
  54. queries?: ExpandedQuery[];
  55. /** Domain intent hint — steers expansion and reranking */
  56. intent?: string;
  57. /** Rerank results using LLM (default: true) */
  58. rerank?: boolean;
  59. /** Filter to a specific collection */
  60. collection?: string;
  61. /** Filter to specific collections */
  62. collections?: string[];
  63. /** Max results (default: 10) */
  64. limit?: number;
  65. /** Minimum score threshold */
  66. minScore?: number;
  67. /** Include explain traces */
  68. explain?: boolean;
  69. /** Chunk strategy: "auto" (default, uses AST for code files) or "regex" (legacy) */
  70. chunkStrategy?: ChunkStrategy;
  71. }
  72. /**
  73. * Options for searchLex() — BM25 keyword search.
  74. */
  75. export interface LexSearchOptions {
  76. limit?: number;
  77. collection?: string;
  78. }
  79. /**
  80. * Options for searchVector() — vector similarity search.
  81. */
  82. export interface VectorSearchOptions {
  83. limit?: number;
  84. collection?: string;
  85. }
  86. /**
  87. * Options for expandQuery() — manual query expansion.
  88. */
  89. export interface ExpandQueryOptions {
  90. intent?: string;
  91. }
  92. /**
  93. * Options for creating a QMD store.
  94. *
  95. * Provide `dbPath` and optionally `configPath` (YAML file) or `config` (inline).
  96. * If neither configPath nor config is provided, the store reads from existing
  97. * DB state (useful for reopening a previously-configured store).
  98. */
  99. export interface StoreOptions {
  100. /** Path to the SQLite database file */
  101. dbPath: string;
  102. /** Path to a YAML config file (mutually exclusive with `config`) */
  103. configPath?: string;
  104. /** Inline collection config (mutually exclusive with `configPath`) */
  105. config?: CollectionConfig;
  106. }
  107. /**
  108. * The QMD SDK store — provides search, retrieval, collection management,
  109. * context management, and indexing operations.
  110. *
  111. * All methods are async. The store manages its own LlamaCpp instance
  112. * (lazy-loaded, auto-unloaded after inactivity) — no global singletons.
  113. */
  114. export interface QMDStore {
  115. /** The underlying internal store (for advanced use) */
  116. readonly internal: InternalStore;
  117. /** Path to the SQLite database */
  118. readonly dbPath: string;
  119. /** Full search: query expansion + multi-signal retrieval + LLM reranking */
  120. search(options: SearchOptions): Promise<HybridQueryResult[]>;
  121. /** BM25 keyword search (fast, no LLM) */
  122. searchLex(query: string, options?: LexSearchOptions): Promise<SearchResult[]>;
  123. /** Vector similarity search (embedding model, no reranking) */
  124. searchVector(query: string, options?: VectorSearchOptions): Promise<SearchResult[]>;
  125. /** Expand a query into typed sub-searches (lex/vec/hyde) for manual control */
  126. expandQuery(query: string, options?: ExpandQueryOptions): Promise<ExpandedQuery[]>;
  127. /** Get a single document by path or docid */
  128. get(pathOrDocid: string, options?: {
  129. includeBody?: boolean;
  130. }): Promise<DocumentResult | DocumentNotFound>;
  131. /** Get the body content of a document, optionally sliced by line range */
  132. getDocumentBody(pathOrDocid: string, opts?: {
  133. fromLine?: number;
  134. maxLines?: number;
  135. }): Promise<string | null>;
  136. /** Get multiple documents by glob pattern or comma-separated list */
  137. multiGet(pattern: string, options?: {
  138. includeBody?: boolean;
  139. maxBytes?: number;
  140. }): Promise<{
  141. docs: MultiGetResult[];
  142. errors: string[];
  143. }>;
  144. /** Add or update a collection */
  145. addCollection(name: string, opts: {
  146. path: string;
  147. pattern?: string;
  148. ignore?: string[];
  149. }): Promise<void>;
  150. /** Remove a collection */
  151. removeCollection(name: string): Promise<boolean>;
  152. /** Rename a collection */
  153. renameCollection(oldName: string, newName: string): Promise<boolean>;
  154. /** List all collections with document stats */
  155. listCollections(): Promise<{
  156. name: string;
  157. pwd: string;
  158. glob_pattern: string;
  159. doc_count: number;
  160. active_count: number;
  161. last_modified: string | null;
  162. includeByDefault: boolean;
  163. }[]>;
  164. /** Get names of collections included by default in queries */
  165. getDefaultCollectionNames(): Promise<string[]>;
  166. /** Add context for a path within a collection */
  167. addContext(collectionName: string, pathPrefix: string, contextText: string): Promise<boolean>;
  168. /** Remove context from a collection path */
  169. removeContext(collectionName: string, pathPrefix: string): Promise<boolean>;
  170. /** Set global context (applies to all collections) */
  171. setGlobalContext(context: string | undefined): Promise<void>;
  172. /** Get global context */
  173. getGlobalContext(): Promise<string | undefined>;
  174. /** List all contexts across all collections */
  175. listContexts(): Promise<Array<{
  176. collection: string;
  177. path: string;
  178. context: string;
  179. }>>;
  180. /** Re-index collections by scanning the filesystem */
  181. update(options?: {
  182. collections?: string[];
  183. onProgress?: (info: UpdateProgress) => void;
  184. }): Promise<UpdateResult>;
  185. /** Generate vector embeddings for documents that need them */
  186. embed(options?: {
  187. force?: boolean;
  188. model?: string;
  189. maxDocsPerBatch?: number;
  190. maxBatchBytes?: number;
  191. chunkStrategy?: ChunkStrategy;
  192. onProgress?: (info: EmbedProgress) => void;
  193. }): Promise<EmbedResult>;
  194. /** Get index status (document counts, collections, embedding state) */
  195. getStatus(): Promise<IndexStatus>;
  196. /** Get index health info (stale embeddings, etc.) */
  197. getIndexHealth(): Promise<IndexHealthInfo>;
  198. /** Close the store and release all resources (LLM models, DB connection) */
  199. close(): Promise<void>;
  200. }
  201. /**
  202. * Create a QMD store for programmatic access to search and indexing.
  203. *
  204. * @example
  205. * ```typescript
  206. * // With a YAML config file
  207. * const store = await createStore({
  208. * dbPath: './index.sqlite',
  209. * configPath: './qmd.yml',
  210. * })
  211. *
  212. * // With inline config (no files needed besides the DB)
  213. * const store = await createStore({
  214. * dbPath: './index.sqlite',
  215. * config: {
  216. * collections: {
  217. * docs: { path: '/path/to/docs', pattern: '**\/*.md' }
  218. * }
  219. * }
  220. * })
  221. *
  222. * const results = await store.search({ query: "authentication flow" })
  223. * await store.close()
  224. * ```
  225. */
  226. export declare function createStore(options: StoreOptions): Promise<QMDStore>;