@kedem/okdb 2.3.5 → 2.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (59) hide show
  1. package/CHANGELOG.md +349 -0
  2. package/algorithm-worker-thread.js +1 -1
  3. package/bin/okdb.js +1 -1
  4. package/docs/auth-and-sync.md +28 -4
  5. package/docs/auth-permissions.md +19 -6
  6. package/docs/change-log.md +79 -59
  7. package/docs/data-model.md +20 -18
  8. package/docs/deployment.md +25 -0
  9. package/docs/diagnostics.md +59 -17
  10. package/docs/embeddings.md +57 -6
  11. package/docs/fts.md +50 -5
  12. package/docs/functions.md +21 -8
  13. package/docs/http-api.md +139 -98
  14. package/docs/http-cluster.md +30 -8
  15. package/docs/indexes.md +5 -4
  16. package/docs/logging.md +18 -0
  17. package/docs/migration.md +94 -2
  18. package/docs/pipelines.md +6 -1
  19. package/docs/plugins.md +12 -18
  20. package/docs/process-registry.md +50 -39
  21. package/docs/processors.md +40 -22
  22. package/docs/querying.md +50 -36
  23. package/docs/queue.md +110 -14
  24. package/docs/subscriptions.md +35 -24
  25. package/docs/sync.md +96 -37
  26. package/docs/time-machine.md +23 -17
  27. package/docs/transactions.md +11 -19
  28. package/docs/ttl.md +118 -67
  29. package/docs/upgrade-2.0.md +2 -2
  30. package/docs/views.md +71 -52
  31. package/okdb-functions-sandbox-worker.js +1 -1
  32. package/okdb-http-worker-child.js +1 -1
  33. package/okdb-views-bootstrap-worker.js +1 -1
  34. package/okdb.js +1 -1
  35. package/package.json +1 -1
  36. package/public/sections/db/parts/type-viewer.ok.js +1 -1
  37. package/public/sections/embeddings/parts/engines-overview.ok.js +1 -1
  38. package/public/sections/queue/parts/queue-jobs.ok.js +1 -1
  39. package/public/sections/system/modals/data-ops-modal.ok.js +1 -1
  40. package/types/environment.d.ts +320 -52
  41. package/types/features/admin.d.ts +8 -2
  42. package/types/features/api.d.ts +10 -16
  43. package/types/features/auth.d.ts +135 -16
  44. package/types/features/embeddings.d.ts +639 -31
  45. package/types/features/engines.d.ts +368 -14
  46. package/types/features/files.d.ts +0 -3
  47. package/types/features/fts.d.ts +98 -26
  48. package/types/features/functions.d.ts +175 -15
  49. package/types/features/mcp.d.ts +23 -10
  50. package/types/features/pipelines.d.ts +268 -9
  51. package/types/features/queue.d.ts +230 -34
  52. package/types/features/sync.d.ts +177 -14
  53. package/types/features/time-machine.d.ts +117 -16
  54. package/types/features/views.d.ts +230 -39
  55. package/types/index.d.ts +357 -254
  56. package/types/okdb.d.ts +377 -0
  57. package/types/options.d.ts +309 -48
  58. package/public/test-cron-input.html +0 -60
  59. package/public/test-json-input.html +0 -280
@@ -1,48 +1,656 @@
1
- export interface OKDBEmbeddingsPipeline {
2
- name: string;
3
- sourceType: string;
4
- sourceField: string;
5
- model: string;
6
- algorithm: string;
1
+ // Embeddings feature — `db.embeddings` (src/features/embeddings/okdb-embeddings.js).
2
+ //
3
+ // A pipeline is four engines (embedder → indexer → [embed-worker] → vector-search) plus an
4
+ // env-local generic `~pipelines` record. Engine names are scoped `<env>:<name>`:
5
+ // - indexer / vector-search: `<source_env>:<storage_key>`
6
+ // - embed-worker: `<source_env>:<storage_key>-worker` (default)
7
+ // - embedder: `<source_env>:<embedder name>` (env-local; legacy unscoped
8
+ // `~system` names still resolve)
9
+
10
+ // ── Shared ──────────────────────────────────────────────────────────────────
11
+
12
+ /** A query: text (embedded by the pipeline's embedder) or a raw vector. */
13
+ export type OKDBEmbeddingsQuery = string | Float32Array | number[];
14
+
15
+ /** Per-document indexing status (`doc_status:<storage_key>` rows). */
16
+ export type OKDBEmbeddingsDocStatus = 'pending' | 'done' | 'failed' | 'deleted';
17
+
18
+ /** Built-in preparers are `'none' | 'html' | 'markdown'`; custom ones via `registerPreparer`. */
19
+ export type OKDBEmbeddingsPreparer = 'none' | 'html' | 'markdown' | (string & {});
20
+
21
+ export interface OKDBEmbeddingsChunkConfig {
22
+ /** `'fixed'` (default) | `'sentence'` | `'paragraph'` | `'off'` | a registered custom strategy. */
23
+ strategy: 'fixed' | 'sentence' | 'paragraph' | 'off' | (string & {});
24
+ /** Target chunk size in characters. Default 512. */
25
+ size?: number;
26
+ /** Overlap in characters (`fixed` only). Default 64. */
27
+ overlap?: number;
28
+ }
29
+
30
+ /** One chunk produced by a chunk strategy — offsets into the prepared text. */
31
+ export interface OKDBEmbeddingsChunk {
32
+ text: string;
33
+ start: number;
34
+ end: number;
35
+ }
36
+
37
+ // ── Pipelines ───────────────────────────────────────────────────────────────
38
+
39
+ export interface OKDBEmbeddingsEmbedderConfig {
40
+ /**
41
+ * Reuse an existing embedder by name (scoped `<env>:<name>` or a legacy unscoped one).
42
+ * Default: the pipeline name, scoped to `source_env`.
43
+ */
44
+ name?: string;
45
+ /** Provider type: `'ollama' | 'openai' | 'fake'` or a registered factory name. */
46
+ type?: string;
47
+ model?: string;
48
+ dims?: number;
49
+ /** Ollama base URL. */
50
+ url?: string;
51
+ /** OpenAI(-compatible) key. */
52
+ api_key?: string;
53
+ /** OpenAI(-compatible) base URL. */
54
+ base_url?: string;
55
+ /** OpenAI request timeout (ms). */
56
+ timeout?: number;
7
57
  [key: string]: unknown;
8
58
  }
9
59
 
10
- export interface OKDBEmbeddingsSearchResult {
11
- key: string | number;
12
- score: number;
13
- value?: unknown;
60
+ export interface OKDBEmbeddingsWorkerConfig {
61
+ /** Worker engine name. Default `<source_env>:<storage_key>-worker`. */
62
+ name?: string;
63
+ /** Default 1. */
64
+ concurrency?: number;
65
+ /** ms between empty-queue polls. Default 1000. */
66
+ pollInterval?: number;
67
+ /** Job claim TTL in ms. Default 30 000. */
68
+ ttl?: number;
69
+ }
70
+
71
+ export interface OKDBEmbeddingsPipelineConfig {
72
+ /** The OKDB type to watch. Required. */
73
+ source_type: string;
74
+ /** Source environment. Default `'default'`. */
75
+ source_env?: string;
76
+ /** Logical vector-store name and generic pipeline record name. Default: the pipeline name. */
77
+ storage_key?: string;
78
+ /** Field to embed; `null`/omitted = the whole record. */
79
+ field?: string | null;
80
+ /** Vector dimensions; default resolved from the embedder. */
81
+ dims?: number;
82
+ /** `'inline'` (default) embeds in the change-log drain; `'queue'` hands jobs to an embed-worker. */
83
+ mode?: 'inline' | 'queue';
84
+ embedder?: OKDBEmbeddingsEmbedderConfig;
85
+ /** Queue mode only. */
86
+ worker?: OKDBEmbeddingsWorkerConfig;
87
+ prepare?: OKDBEmbeddingsPreparer;
88
+ chunk?: OKDBEmbeddingsChunkConfig;
89
+ /** Texts per embedder request. Default 32. */
90
+ batch?: number;
91
+ /** Hash → vector LRU entries (0 disables). Default 1024. */
92
+ cache?: number;
93
+ /** Queue mode: ms a job waits before it can be claimed (coalesces edit bursts). Default 0. */
94
+ debounce?: number;
95
+ /** Changes per indexer live-drain quantum (smaller = shorter hold of the instance's drain gate). Default 32. */
96
+ flushQuantum?: number;
97
+ /** Deadline in ms for one indexer drain quantum before it is failed and retried. Default 600 000. */
98
+ drainDeadlineMs?: number;
99
+ /** Search algorithm: `'flat'` (default) | `'hnsw'` | `'usearch'` | a registered factory. */
100
+ algorithm?: string;
101
+ /** Forwarded to the algorithm factory (e.g. `metric`, `M`, `efSearch`, `idleEvictMs`). */
102
+ algorithm_config?: Record<string, unknown>;
103
+ }
104
+
105
+ /** An engine instance as returned by the engines subsystem (may be in an error state). */
106
+ export interface OKDBEmbeddingsEngine<TApi = unknown> {
107
+ /** `<type>@<name>`. */
108
+ readonly key: string;
109
+ readonly type: string;
110
+ readonly name: string;
111
+ readonly isRunning: boolean;
112
+ readonly status: string;
113
+ /** The driver's runtime API (null when not running). */
114
+ readonly api: TApi | null;
115
+ start(): Promise<unknown>;
116
+ stop(options?: { reason?: string }): Promise<unknown>;
117
+ [key: string]: unknown;
118
+ }
119
+
120
+ /** `createPipeline(...).api` — convenience surface bound to one pipeline. */
121
+ export interface OKDBEmbeddingsPipelineApi {
122
+ /** Chunk-level search (see `OKDBEmbeddings.search`). */
123
+ search(query: OKDBEmbeddingsQuery, options?: OKDBEmbeddingsSearchOptions): Promise<OKDBEmbeddingsSearchResult[]>;
124
+ /** Document-level search (see `OKDBEmbeddings.searchDocs`). */
125
+ searchDocs(
126
+ query: OKDBEmbeddingsQuery,
127
+ options?: OKDBEmbeddingsSearchOptions,
128
+ ): Promise<OKDBEmbeddingsDocSearchResult[]>;
129
+ /**
130
+ * Wait until the indexer's cursor reaches the source head clock (default bound 30 s). Rejects with
131
+ * `INDEXER_FLUSH_TIMEOUT` (`details: { cursor, head, timeoutMs, waiting }`) if it doesn't get there in
132
+ * time. Throws if the indexer isn't running here.
133
+ */
134
+ flush(options?: { timeoutMs?: number }): Promise<void>;
135
+ /** Each member's health/stats; `undefined` for members not running in this process. */
136
+ stats(): Promise<{
137
+ embedder: OKDBEmbedderHealth | undefined;
138
+ indexer: OKDBEmbeddingsIndexerStats | undefined;
139
+ worker: OKDBEmbeddingsWorkerStats | undefined;
140
+ search: OKDBEmbeddingsSearchStats | undefined;
141
+ }>;
142
+ }
143
+
144
+ /** Result of `createPipeline`: the member engines plus a bound API. */
145
+ export interface OKDBEmbeddingsPipelineResult {
146
+ embedder: OKDBEmbeddingsEngine<OKDBEmbedderApi>;
147
+ indexer: OKDBEmbeddingsEngine<OKDBEmbeddingsIndexerApi>;
148
+ /** `null` unless `mode: 'queue'`. */
149
+ worker: OKDBEmbeddingsEngine<OKDBEmbeddingsWorkerApi> | null;
150
+ search: OKDBEmbeddingsEngine<OKDBEmbeddingsSearchApi>;
151
+ api: OKDBEmbeddingsPipelineApi;
152
+ }
153
+
154
+ /**
155
+ * @deprecated Pipelines are no longer a record type of this class — `createPipeline` returns
156
+ * {@link OKDBEmbeddingsPipelineResult}; the persisted topology lives in the env's generic
157
+ * `~pipelines` registry (`env.pipelines`). Alias kept for existing imports.
158
+ */
159
+ export type OKDBEmbeddingsPipeline = OKDBEmbeddingsPipelineResult;
160
+
161
+ // ── Engine runtime APIs ─────────────────────────────────────────────────────
162
+
163
+ export interface OKDBEmbedderHealth {
164
+ ok: boolean;
165
+ type: string;
166
+ model: string | null;
167
+ dims: number | null;
168
+ [key: string]: unknown;
169
+ }
170
+
171
+ /** `embeddings.embedder(name)` — the running embedder engine's API. */
172
+ export interface OKDBEmbedderApi {
173
+ /** Resolved output dimensions (null if unknown at start). */
174
+ readonly dims: number | null;
175
+ embed(input: string): Promise<Float32Array>;
176
+ embed(input: string[]): Promise<Float32Array[]>;
177
+ health(): Promise<OKDBEmbedderHealth>;
178
+ }
179
+
180
+ export interface OKDBEmbeddingsDocCounts {
181
+ pending: number;
182
+ done: number;
183
+ failed: number;
184
+ deleted: number;
185
+ total: number;
186
+ }
187
+
188
+ /** Identifies an embedding vector space: provider, model id, explicit output dims. */
189
+ export interface OKDBEmbeddingsFingerprint {
190
+ type: string;
191
+ model: string | null;
192
+ dims: number | null;
193
+ }
194
+
195
+ /**
196
+ * Vector-space status of a pipeline. `stale`: the stored vectors were embedded by a different
197
+ * embedder (provider/model/dims) than the one configured now — search still serves them (with a
198
+ * warning), new embeds are held, and a rebuild ("Re-embed all") re-embeds and clears it.
199
+ */
200
+ export interface OKDBEmbeddingsVectorSpace {
201
+ /** Fingerprint recorded with the stored vectors (null until the first embed). */
202
+ fingerprint: OKDBEmbeddingsFingerprint | null;
203
+ /** Fingerprint of the embedder configured now. */
204
+ current: OKDBEmbeddingsFingerprint | null;
205
+ stale: boolean;
206
+ /** Human-readable explanation, only when stale. */
207
+ reason?: string;
208
+ }
209
+
210
+ export interface OKDBEmbeddingsIndexerStats {
211
+ engine_key: string;
212
+ mode: 'inline' | 'queue';
213
+ source_type: string;
214
+ storage_key: string;
215
+ job_type: string | null;
216
+ last_seen_clock: number;
217
+ head_clock: number;
218
+ processor_cursor: number;
219
+ vector_count: number | null;
220
+ doc_counts: OKDBEmbeddingsDocCounts;
221
+ /** Live indexer only. */
222
+ embedder?: string;
223
+ /**
224
+ * Live indexer only — set while work is held:
225
+ * `{ reason: 'FIELD_RESOLVER_MISSING' | 'REGISTRATION_MISSING' | 'EMBEDDER_NOT_RUNNING' | 'EMBEDDER_CHANGED', error, since, … }`.
226
+ */
227
+ waiting?: Record<string, unknown> | null;
228
+ /** Which embedder the stored vectors came from, and whether that is still the configured one. */
229
+ vector_space?: OKDBEmbeddingsVectorSpace;
230
+ batch?: number;
231
+ prepare?: string;
232
+ chunk?: OKDBEmbeddingsChunkConfig;
233
+ /** Durable (no live indexer here) reads only. */
234
+ remote?: true;
235
+ enabled?: boolean;
236
+ [key: string]: unknown;
237
+ }
238
+
239
+ /** A `doc_status` row, keyed by source doc. */
240
+ export interface OKDBEmbeddingsDocStatusEntry {
241
+ key: string;
242
+ source_key?: string;
243
+ status: OKDBEmbeddingsDocStatus;
244
+ version?: number | null;
245
+ clock?: number | null;
246
+ updated?: number;
247
+ error?: string;
248
+ empty?: boolean;
249
+ /** Unchunked: content hash of the embedded text. */
250
+ hash?: string;
251
+ /** Chunked: manifest of embedded chunks. */
252
+ chunks?: Array<{ hash: string; start: number; end: number }>;
253
+ /** Chunked: `chunks.length`. */
254
+ chunk_done?: number;
255
+ [key: string]: unknown;
256
+ }
257
+
258
+ export interface OKDBEmbeddingsRetryFailedOptions {
259
+ /** Max docs to retry (1–10 000). Default 1000. */
260
+ limit?: number;
261
+ /** Also retry PENDING docs whose job was lost. Default false. */
262
+ includeStuckPending?: boolean;
263
+ }
264
+
265
+ export interface OKDBEmbeddingsRetryFailedResult {
266
+ retried: number;
267
+ deleted: number;
268
+ /** Inline mode only. */
269
+ succeeded?: number;
270
+ }
271
+
272
+ /** `embeddings.indexer(name)` — the running indexer engine's API. */
273
+ export interface OKDBEmbeddingsIndexerApi {
274
+ /**
275
+ * Wait for the change feed to catch up with the source type (default bound 30 s). Rejects with
276
+ * `INDEXER_FLUSH_TIMEOUT` if it doesn't get there in time.
277
+ */
278
+ flush(options?: { timeoutMs?: number }): Promise<void>;
279
+ stats(): Promise<OKDBEmbeddingsIndexerStats>;
280
+ getDocStatus(sourceKey: string): Promise<Record<string, unknown> | null>;
281
+ listDocStatus(options?: {
282
+ status?: OKDBEmbeddingsDocStatus | null;
283
+ /** 1–1000. Default 100. */
284
+ limit?: number;
285
+ }): Promise<OKDBEmbeddingsDocStatusEntry[]>;
286
+ /** Bring one doc's vectors in line with its content; throws `EMBED_FAILED` if the doc ends failed. */
287
+ reconcile(sourceKey: string, options?: { heartbeat?: () => unknown }): Promise<OKDBEmbeddingsDocStatus | undefined>;
288
+ /** External write-back (custom workers). `vector` must be a Float32Array. */
289
+ markDone(
290
+ sourceKey: string,
291
+ options?: { vector?: Float32Array; clock?: number | null; empty?: boolean },
292
+ ): Promise<unknown>;
293
+ markFail(sourceKey: string, error: unknown, options?: { clock?: number | null }): Promise<unknown>;
294
+ markDelete(sourceKey: string): Promise<null>;
295
+ /** Queue mode re-enqueues; inline mode reconciles on the spot (and reports `status`). */
296
+ retryDoc(
297
+ sourceKey: string,
298
+ ): Promise<{ retried: number; source_key: string; status?: OKDBEmbeddingsDocStatus | null }>;
299
+ retryFailed(options?: OKDBEmbeddingsRetryFailedOptions): Promise<OKDBEmbeddingsRetryFailedResult>;
300
+ /** Generic engine retry action — alias of `retryFailed`. */
301
+ retry(options?: OKDBEmbeddingsRetryFailedOptions): Promise<OKDBEmbeddingsRetryFailedResult>;
302
+ /** Full re-embed: drops vectors + doc_status, resets the cursor to 0. Throws `ENGINE_NOT_RUNNING` if stopped. */
303
+ rebuild(): Promise<{ rebuilt: true; vectorsDropped: number; statusesDropped: number }>;
304
+ }
305
+
306
+ export interface OKDBEmbeddingsWorkerStats {
307
+ engine_key: string;
308
+ job_type: string | null;
309
+ indexer: string | null;
310
+ embedder: string | null;
311
+ concurrency: number | null;
312
+ /** Per-process counters; `null` on durable (remote) reads. */
313
+ processed: number | null;
314
+ failed: number | null;
315
+ in_flight: number | null;
316
+ /** Durable (remote) reads only. */
317
+ queue_depth?: unknown;
318
+ remote?: true;
319
+ enabled?: boolean;
320
+ }
321
+
322
+ /** `embeddings.worker(name)` — the running embed-worker engine's API. */
323
+ export interface OKDBEmbeddingsWorkerApi {
324
+ stats(): Promise<OKDBEmbeddingsWorkerStats>;
14
325
  }
15
326
 
16
327
  export interface OKDBEmbeddingsSearchOptions {
328
+ /** Max results. Default 10. For `searchDocs` it counts documents; otherwise chunks. */
17
329
  limit?: number;
18
- filter?: Record<string, unknown>;
19
- includeValues?: boolean;
330
+ /** Minimum similarity score (0–1). Default 0. */
331
+ threshold?: number;
332
+ /** Other options are forwarded to the search algorithm. */
333
+ [key: string]: unknown;
20
334
  }
21
335
 
22
- export interface OKDBEmbeddingsAddPipelineOptions {
23
- sourceField: string;
24
- model: string;
336
+ /**
337
+ * A chunk-level hit. For chunked pipelines `key` is a vec key (`docKey\tchunkHash`,
338
+ * decode with `parseVecKey`); otherwise the source doc key. Score ∈ [0, 1], higher = closer.
339
+ */
340
+ export interface OKDBEmbeddingsSearchResult {
341
+ key: string;
342
+ score: number;
343
+ }
344
+
345
+ /** A document-level hit from `searchDocs`: best chunk per source doc. */
346
+ export interface OKDBEmbeddingsDocSearchResult {
347
+ key: string;
348
+ score: number;
349
+ /** The winning chunk's hash — chunked pipelines only. */
350
+ chunkHash?: string;
351
+ }
352
+
353
+ export interface OKDBEmbeddingsSearchStats {
354
+ engine_key: string;
355
+ loaded: boolean;
356
+ count: number;
357
+ storage_key?: string;
358
+ dims?: number;
25
359
  algorithm?: string;
360
+ idleEvictMs?: number;
361
+ vector_count?: number | null;
362
+ [key: string]: unknown;
363
+ }
364
+
365
+ /**
366
+ * `embeddings.search(name)` — a per-process local search view. `search`/`stats` are always
367
+ * present; the rest exist only when the vector-search engine is running in this process.
368
+ */
369
+ export interface OKDBEmbeddingsSearchApi {
370
+ /** Chunk-level nearest neighbours, best-first. */
371
+ search(query: OKDBEmbeddingsQuery, options?: OKDBEmbeddingsSearchOptions): Promise<OKDBEmbeddingsSearchResult[]>;
372
+ stats(): Promise<OKDBEmbeddingsSearchStats>;
373
+ /** Persist a vector (Float32Array of the pipeline's dims). */
374
+ add?(key: string, vector: Float32Array): Promise<void>;
375
+ remove?(key: string): Promise<void>;
376
+ /** Vectors loaded in this process's view (0 if not materialized). */
377
+ count?(): number;
378
+ snapshot?(): Promise<unknown>;
379
+ rebuild?(): Promise<{ count: number }>;
380
+ }
381
+
382
+ // ── Registries / catalog ────────────────────────────────────────────────────
383
+
384
+ /** Object an embedder factory returns. */
385
+ export interface OKDBEmbedderImpl {
386
+ /** Embed one input; return one vector. */
387
+ embed(input: string): Promise<Float32Array | number[]>;
388
+ /** Optional native batch (one provider request for many inputs). */
389
+ embedBatch?(texts: string[]): Promise<Array<Float32Array | number[]>>;
390
+ health?(): Promise<Record<string, unknown>>;
391
+ start?(): Promise<void> | void;
392
+ stop?(): Promise<void> | void;
26
393
  dims?: number;
27
394
  [key: string]: unknown;
28
395
  }
29
396
 
397
+ export type OKDBEmbedderFactory = (config: Record<string, unknown>, okdb: unknown) => OKDBEmbedderImpl;
398
+
399
+ /** A UI form field descriptor for provider/algorithm schemas. */
400
+ export interface OKDBEmbeddingsSchemaField {
401
+ key: string;
402
+ type?: string;
403
+ label?: string;
404
+ [key: string]: unknown;
405
+ }
406
+
407
+ export interface OKDBEmbedderSchema {
408
+ label: string;
409
+ fields: OKDBEmbeddingsSchemaField[];
410
+ note: string | null;
411
+ }
412
+
413
+ export interface OKDBEmbeddingsProvider extends OKDBEmbedderSchema {
414
+ key: string;
415
+ }
416
+
417
+ /**
418
+ * Returns a search algorithm instance (flat/hnsw/usearch or custom). okdb drives it through
419
+ * `ready`, `load(getAll)`, `add`, `remove`, `search(vec, { limit, threshold, … })`,
420
+ * `snapshot`, `rebuild(getAll)`, `count`, `info`, `stop`.
421
+ */
422
+ export type OKDBAlgorithmFactory = (
423
+ algorithmConfig: Record<string, unknown>,
424
+ options: { dims: number; dir: string },
425
+ ) => unknown;
426
+
427
+ export interface OKDBAlgorithmSchema {
428
+ label?: string;
429
+ description?: string | null;
430
+ note?: string | null;
431
+ fields?: OKDBEmbeddingsSchemaField[];
432
+ /** JSON Schema for `algorithm_config`. */
433
+ configSchema?: Record<string, unknown> | null;
434
+ }
435
+
436
+ export interface OKDBEmbeddingsAlgorithmInfo {
437
+ key: string;
438
+ label: string;
439
+ description: string | null;
440
+ note: string | null;
441
+ fields: OKDBEmbeddingsSchemaField[];
442
+ configSchema: Record<string, unknown> | null;
443
+ }
444
+
445
+ export interface OKDBEmbeddingsModel {
446
+ provider: string;
447
+ model: string;
448
+ dims: number;
449
+ description?: string;
450
+ builtin?: boolean;
451
+ [key: string]: unknown;
452
+ }
453
+
454
+ /** Custom vector storage adapter returned by a `setStorageResolver` function. */
455
+ export interface OKDBVectorStorageAdapter {
456
+ put(key: string, vector: Float32Array): Promise<unknown> | unknown;
457
+ remove(key: string): Promise<unknown> | unknown;
458
+ get?(key: string): Float32Array | null;
459
+ getAll?(): Promise<unknown[]>;
460
+ write?(puts: unknown[], removes: string[]): Promise<unknown>;
461
+ keys?(range?: unknown): string[];
462
+ count?(): number;
463
+ dropAll?(): Promise<unknown>;
464
+ stats?(): Record<string, unknown>;
465
+ [key: string]: unknown;
466
+ }
467
+
468
+ export type OKDBVectorStorageResolver = (storageKey: string, okdb: unknown) => OKDBVectorStorageAdapter;
469
+
470
+ export interface OKDBVectorStoreStats {
471
+ storage_key: string;
472
+ /** `vec:<storage_key>`. */
473
+ type: string;
474
+ count: number;
475
+ }
476
+
477
+ export interface OKDBEmbeddingsDurableRebuildResult {
478
+ rebuilt: true;
479
+ remote: true;
480
+ /** Processor reset command epoch, or null if the desired-state store was unavailable. */
481
+ resetEpoch: number | null;
482
+ vectorsDropped: number;
483
+ statusesDropped: number;
484
+ enqueued: number;
485
+ /** True when nothing could be enqueued here (inline mode) — a live indexer must re-embed. */
486
+ requiresLiveEngine: boolean;
487
+ }
488
+
489
+ // ── Feature object ──────────────────────────────────────────────────────────
490
+
491
+ /**
492
+ * `db.embeddings` — vector embedding pipelines and semantic search.
493
+ *
494
+ * Engine/pipeline names are env-scoped `<env>:<name>` (see file header). List/stop/remove
495
+ * pipelines through the env's generic pipelines registry (`env.pipelines`).
496
+ * Removed: `addPipeline`/`removePipeline`/`getPipeline`/`listPipelines`/`embed` (never existed
497
+ * in 2.x — use `createPipeline`, `env.pipelines`, and `embedder(name).embed`).
498
+ */
30
499
  export declare class OKDBEmbeddings {
31
- addPipeline(
32
- name: string,
33
- sourceType: string,
34
- options: OKDBEmbeddingsAddPipelineOptions,
35
- ): Promise<OKDBEmbeddingsPipeline>;
36
- removePipeline(name: string, sourceType: string): Promise<void>;
37
- getPipeline(name: string, sourceType: string): OKDBEmbeddingsPipeline | null;
38
- listPipelines(sourceType?: string): OKDBEmbeddingsPipeline[];
39
- search(
40
- name: string,
41
- sourceType: string,
42
- query: string | number[],
500
+ // ── Configuration / registries — register before `db.open()` so restored pipelines find them.
501
+
502
+ /** Register an embedder provider type (also used to wrap a built-in, see `getEmbedderFactory`). */
503
+ registerEmbedderFactory(name: string, factory: OKDBEmbedderFactory, schema?: Partial<OKDBEmbedderSchema>): void;
504
+ /** The factory for an embedder type (built-in `'ollama' | 'openai' | 'fake'` or registered), or null. */
505
+ getEmbedderFactory(type: string): OKDBEmbedderFactory | null;
506
+ /** The `{ label, fields, note }` schema registered with an embedder type, or null. */
507
+ getEmbedderSchema(type: string): OKDBEmbedderSchema | null;
508
+ /** Register a search algorithm (built-ins: `'flat'`, `'hnsw'`, `'usearch'`). */
509
+ registerAlgorithmFactory(name: string, factory: OKDBAlgorithmFactory, schema?: OKDBAlgorithmSchema): void;
510
+ listAlgorithms(): OKDBEmbeddingsAlgorithmInfo[];
511
+ /** Custom text preparer: raw field value → string. Use as `{ prepare: name }`. */
512
+ registerPreparer(name: string, fn: (value: unknown) => string): void;
513
+ /** Custom chunk strategy. Use as `{ chunk: { strategy: name } }`. */
514
+ registerChunkStrategy(
515
+ name: string,
516
+ fn: (text: string, options: { size: number; overlap: number }) => OKDBEmbeddingsChunk[],
517
+ ): void;
518
+ /** Replace the built-in LMDB vector storage (`vec:<storage_key>`); `null` restores it. */
519
+ setStorageResolver(fn: OKDBVectorStorageResolver | null): void;
520
+
521
+ // ── Pipelines & engines
522
+
523
+ /**
524
+ * Create (or repair) a full pipeline: embedder, indexer, optional queue worker, vector-search,
525
+ * plus an env-local `~pipelines` record named `storage_key`. Rejects if a complete pipeline
526
+ * with that `storage_key` already exists. License-gated (`embeddings`).
527
+ */
528
+ createPipeline(
529
+ name: string,
530
+ config: OKDBEmbeddingsPipelineConfig,
531
+ meta?: Record<string, unknown>,
532
+ ): Promise<OKDBEmbeddingsPipelineResult>;
533
+ /**
534
+ * Create and start an env-local embedder engine. The env comes from `envName`, `config.env`,
535
+ * or the `<env>:` prefix of `name`; default `'default'`.
536
+ */
537
+ createEmbedder(
538
+ name: string,
539
+ config?: OKDBEmbeddingsEmbedderConfig & { env?: string },
540
+ meta?: Record<string, unknown>,
541
+ envName?: string | null,
542
+ ): Promise<OKDBEmbeddingsEngine<OKDBEmbedderApi>>;
543
+ /** Create and start an indexer engine in `config.source_env` (default `'default'`). */
544
+ createIndexer(
545
+ name: string,
546
+ config: Record<string, unknown> & {
547
+ source_type: string;
548
+ storage_key: string;
549
+ embedder: string;
550
+ source_env?: string;
551
+ },
552
+ meta?: Record<string, unknown>,
553
+ ): Promise<OKDBEmbeddingsEngine<OKDBEmbeddingsIndexerApi>>;
554
+ /** Create and start a vector-search engine in `config.source_env` (default `'default'`). */
555
+ createSearch(
556
+ name: string,
557
+ config: Record<string, unknown> & { storage_key: string; source_env?: string },
558
+ meta?: Record<string, unknown>,
559
+ ): Promise<OKDBEmbeddingsEngine<OKDBEmbeddingsSearchApi>>;
560
+ /** Create and start an embed-worker engine in `config.source_env` (default `'default'`). */
561
+ createWorker(
562
+ name: string,
563
+ config: Record<string, unknown> & { job_type: string; indexer: string; embedder: string; source_env?: string },
564
+ meta?: Record<string, unknown>,
565
+ ): Promise<OKDBEmbeddingsEngine<OKDBEmbeddingsWorkerApi>>;
566
+
567
+ /** Running embedder's API in this process, or null. */
568
+ embedder(name: string): OKDBEmbedderApi | null;
569
+ /** Running indexer's API in this process (e.g. `'default:articles'`), or null. */
570
+ indexer(name: string): OKDBEmbeddingsIndexerApi | null;
571
+ /** Running embed-worker's API in this process, or null. */
572
+ worker(name: string): OKDBEmbeddingsWorkerApi | null;
573
+
574
+ // ── Search
575
+
576
+ /**
577
+ * Search API for a scoped name (`'<env>:<storage_key>'`). Works on any process: the first
578
+ * search lazily materializes a local in-memory view over the durable vectors (its `search()`
579
+ * rejects with `ENGINE_NOT_FOUND` if the name isn't configured). Chunk-level.
580
+ */
581
+ search(name: string): OKDBEmbeddingsSearchApi;
582
+ /**
583
+ * Document-level search: collapses chunk hits to one row per source doc (best chunk wins,
584
+ * its `chunkHash` rides along) and over-fetches until `limit` distinct docs are found.
585
+ * Unchunked pipelines pass straight through. Default `limit` 10. Best-first.
586
+ */
587
+ searchDocs(
588
+ name: string,
589
+ query: OKDBEmbeddingsQuery,
43
590
  options?: OKDBEmbeddingsSearchOptions,
44
- ): Promise<OKDBEmbeddingsSearchResult[]>;
45
- embed(model: string, text: string | string[]): Promise<number[] | number[][]>;
46
- start(): Promise<void>;
47
- stop(): Promise<void>;
591
+ ): Promise<OKDBEmbeddingsDocSearchResult[]>;
592
+ /**
593
+ * Vector-space status of a pipeline (scoped name): which embedder the stored vectors came from
594
+ * and whether it is still the configured one (`stale`). Null without a durable indexer record.
595
+ */
596
+ vectorSpace(name: string): OKDBEmbeddingsVectorSpace | null;
597
+ /**
598
+ * Describe one chunk of a search hit: offsets from the doc_status manifest, plus `text`
599
+ * re-derived from `sourceValue` when given (only while the doc still contains that chunk).
600
+ */
601
+ describeChunk(
602
+ name: string,
603
+ sourceKey: string,
604
+ chunkHash: string,
605
+ sourceValue?: unknown,
606
+ ): Promise<{ start: number; end: number; text?: string } | null>;
607
+
608
+ // ── Durable (engine-less) operations — usable from a process with no live indexer (e.g.
609
+ // `engines:false`). Each resolves `null` when the indexer has no durable record.
610
+
611
+ durableMarkDone(
612
+ name: string,
613
+ sourceKey: string,
614
+ options?: { vector?: Float32Array; clock?: number | null },
615
+ ): Promise<unknown>;
616
+ durableMarkFail(
617
+ name: string,
618
+ sourceKey: string,
619
+ error: unknown,
620
+ options?: { clock?: number | null },
621
+ ): Promise<unknown>;
622
+ durableMarkDelete(name: string, sourceKey: string): Promise<{ source_key: string; dropped: true } | null>;
623
+ /** Queue-mode pipelines only (null otherwise). A missing source doc is deleted instead. */
624
+ durableRetryDoc(
625
+ name: string,
626
+ sourceKey: string,
627
+ ): Promise<{ retried: number; source_key: string } | { source_key: string; dropped: true } | null>;
628
+ /** Queue-mode pipelines only (null otherwise). */
629
+ durableRetryFailed(
630
+ name: string,
631
+ options?: OKDBEmbeddingsRetryFailedOptions,
632
+ ): Promise<{ retried: number; deleted: number } | null>;
633
+ /**
634
+ * "Re-embed all" without a live indexer: drops vectors + doc_status and resets the durable
635
+ * cursor (a live owner elsewhere re-bootstraps); queue mode also enqueues every doc.
636
+ */
637
+ durableRebuild(name: string): Promise<OKDBEmbeddingsDurableRebuildResult | null>;
638
+
639
+ // ── Vector stores
640
+
641
+ /** Stats of a `vec:<storage_key>` store; null when a custom storage resolver is set. */
642
+ vectorStoreStats(storageKey: string): OKDBVectorStoreStats | null;
643
+ /** Vector stores opened in this process; `env` is the `~<env>:emb:<type>` env name. */
644
+ listVectorStores(): Array<OKDBVectorStoreStats & { env: string }>;
645
+ /** One stored vector; null if absent or a custom storage resolver is set. */
646
+ getVector(storageKey: string, docKey: string): Float32Array | null;
647
+
648
+ // ── Model catalog (`~system` `~emb:models`)
649
+
650
+ /** Catalog dims for a provider/model (falls back to the base name without `:tag`), or null. */
651
+ resolveModelDims(provider: string, model: string): number | null;
652
+ registerModel(provider: string, model: string, dims: number, description?: string): Promise<void>;
653
+ listModels(provider?: string | null): OKDBEmbeddingsModel[];
654
+ /** Registered embedder provider types with their UI schemas. */
655
+ listProviders(): OKDBEmbeddingsProvider[];
48
656
  }