Repository navigation
Expand file tree
/
Copy pathpgvector-adapter.ts
More file actions
846 lines (798 loc) · 37 KB
/
Copy pathpgvector-adapter.ts
File metadata and controls
846 lines (798 loc) · 37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
import { createHash } from 'node:crypto';
import type {
AdapterContext,
AdapterEmbeddingDescriptor,
AdapterIndexStatus,
AdapterListDocumentsOptions,
AdapterListDocumentsPage,
AdapterSearchOptions,
IEmbedder,
IKnowledgeAdapter,
} from '@objectstack/spec/contracts';
import type { KnowledgeDocument, KnowledgeHit } from '@objectstack/spec/ai';
import {
DEFAULT_KNOWLEDGE_SCHEMA,
GENERATION_TABLE,
SAFE_IDENTIFIER,
qualified,
schemaDdl,
vectorLiteral,
vectorTable,
vectorTableDdl,
} from './sql.js';
/** One answer of a query: its rows. */
export interface PgQueryResult<R = Record<string, unknown>> {
rows: R[];
}
/** What the adapter needs of a checked-out connection: a query, and its release. */
export interface PgClientLike {
query<R = Record<string, unknown>>(text: string, params?: unknown[]): Promise<PgQueryResult<R>>;
release(err?: Error | boolean): void;
}
/**
* What the adapter needs of a connection pool (`pg.Pool` has it — see
* `poolFromPg`): a one-shot query, which takes a connection and gives it back,
* and a checkout for a transaction.
*/
export interface PgPoolLike {
query<R = Record<string, unknown>>(text: string, params?: unknown[]): Promise<PgQueryResult<R>>;
connect(): Promise<PgClientLike>;
}
/** Options of {@link PgvectorKnowledgeAdapter}. */
export interface PgvectorKnowledgeAdapterOptions {
/** Adapter id, the value of `KnowledgeSource.adapter`. @default 'pgvector' */
id?: string;
/**
* The pool the store queries through. Absent: the store has no database,
* and every call answers `SERVICE_UNAVAILABLE` naming `unavailable`.
*/
pool?: PgPoolLike;
/** Why there is no pool, said by every call when `pool` is absent. */
unavailable?: string;
/**
* The embedder the store calls, or a getter read at each call (the kernel's
* `embedder` service may register after this adapter). Its `dimensions`
* choose the passage table; its `model` and `dimensions` are what the
* adapter reports to the knowledge service's index fingerprint.
*/
embedder: IEmbedder | (() => IEmbedder | undefined);
/**
* The schema the store's tables live in.
* @default 'os_knowledge'
*/
schema?: string;
/**
* Text put before every QUERY before it is embedded, for an asymmetric
* model that embeds questions and passages differently (BGE's
* `'Represent this sentence for searching relevant passages: '`). Passages
* are embedded as they are. It changes no stored vector, so it is not part
* of the index fingerprint (ADR-0161 K8, item 15).
*/
queryPrefix?: string;
}
/** The ADR-0112 code of a write or a search refused under another fingerprint (ADR-0161 K7, rules 4 and 5). */
export const KNOWLEDGE_REBUILD_REQUIRED = 'KNOWLEDGE_REBUILD_REQUIRED';
/** How many times {@link PgvectorKnowledgeAdapter.upsert} plans a batch again when a concurrent write removed a passage it meant to keep. */
const UPSERT_ATTEMPTS = 3;
/** A generation row: one complete index of one source under one fingerprint. */
interface GenerationRow {
generation: number;
fingerprint: string | null;
recipe_hash: string | null;
dim: number;
}
/** What the store learned when it became ready. */
interface Readiness {
/** The schema the `vector` extension is installed in, quoted for SQL. */
extensionSchema: string;
/** The extension's version. */
version: string;
}
/**
* `PgvectorKnowledgeAdapter` — an `IKnowledgeAdapter` on PostgreSQL with the
* pgvector extension: the open vector store for self-hosted deployments
* (cloud ADR-0119 D5; ADR-0161 Execution, row 8a).
*
* ## Storage
*
* In its own schema ({@link DEFAULT_KNOWLEDGE_SCHEMA}, outside the
* connection's `search_path`, so no table can collide with an object table or
* be read as schema drift): a generation table, `knowledge_generation` (source,
* generation, fingerprint, recipe hash, vector width, model, state), and one
* passage table per vector width, `knowledge_vec_d<width>`, created the first
* time a width is written. A passage row holds its source, generation, chunk
* and document ids, record id, organization, text, title, heading path,
* document hash, chunk hash, metadata and vector. The text is stored: it is
* as sensitive as the record it came from, and it is in every backup of the database.
*
* ## Search
*
* An exact scan by cosine distance — no approximate index, so a 3072-wide
* model works (pgvector's HNSW index stops at 2000 dimensions; adding one is an
* operator's step, documented with the package). The organization filter
* (ADR-0161 K8, item 9) and the metadata-equality `filter` run in the same
* `WHERE`, before ranking. A passage whose cosine similarity is not positive is
* not a hit.
*
* ## The index fingerprint (ADR-0161 K7; K8, item 8)
*
* The generation row holds the fingerprint the source was built under. A write
* or a search carrying another is refused `KNOWLEDGE_REBUILD_REQUIRED` and is
* never served from the stored generation. A source that holds nothing takes
* the first fingerprint it is handed, and passage rows of the source left
* without a generation row are deleted as that generation opens: it never
* adopts vectors of another fingerprint. `beginBuild` under the stored
* fingerprint rebuilds inside the generation; under another it is refused,
* its `remedy` carrying the exact statements that clear the source (one
* DELETE per passage table, then the generation row) — this store does not
* yet build a second generation beside the first. `completeBuild` records
* the recipe hash; `indexStatus` answers both.
*
* ## Writes
*
* The adapter takes the service's passages (`capabilities.chunks`) and hashes
* the text of each (`chunk_hash`). A passage whose hash is already stored is
* not embedded again: its row's metadata is rewritten; when that row is gone
* by the time the write runs (a concurrent write removed it), the batch is
* planned again, so the passage is embedded rather than lost. Every embed call
* runs before a connection is taken for the write, so no pooled connection
* waits on the embedder.
*
* ## When the database is not ready
*
* Without the `vector` extension (and the right to create it), or without the
* right to create the schema, the adapter creates nothing; every call answers
* `SERVICE_UNAVAILABLE`, `healthCheck` answers `ok: false` with the SQL to run
* by hand, and the next call tries again. A database it cannot reach is
* reported as unreachable, with no SQL.
*
* ## What a caller reads
*
* A refusal's message is one sentence: no driver text, no SQL, no schema
* name. Operator text travels in the error's `remedy`, which `healthCheck`,
* the plugin's boot warning and the knowledge service's reindex result and
* log show.
*/
export class PgvectorKnowledgeAdapter implements IKnowledgeAdapter {
readonly id: string;
readonly capabilities = { chunks: true } as const;
private readonly pool?: PgPoolLike;
private readonly unavailable?: string;
private readonly resolveEmbedder: () => IEmbedder | undefined;
private readonly schema: string;
private readonly queryPrefix: string;
private readiness?: Readiness;
/** Widths whose passage table this adapter has created or found. */
private readonly tables = new Set<number>();
constructor(opts: PgvectorKnowledgeAdapterOptions) {
this.id = opts.id ?? 'pgvector';
this.pool = opts.pool;
this.unavailable = opts.unavailable;
const embedder = opts.embedder;
this.resolveEmbedder = typeof embedder === 'function' ? embedder : () => embedder;
this.schema = opts.schema ?? DEFAULT_KNOWLEDGE_SCHEMA;
if (!SAFE_IDENTIFIER.test(this.schema)) {
throw new Error(
`knowledge-pgvector: schema '${this.schema}' is not a lower-case identifier (letters, digits, underscores).`,
);
}
this.queryPrefix = opts.queryPrefix ?? '';
}
/** What the adapter reports about its embedder (ADR-0161 K8, item 6), read now. */
get embedding(): AdapterEmbeddingDescriptor | undefined {
const embedder = this.resolveEmbedder();
if (!embedder) return undefined;
return {
dimensions: embedder.dimensions,
...(embedder.model === undefined ? {} : { model: embedder.model }),
...(embedder.maxInputTokens === undefined ? {} : { maxInputTokens: embedder.maxInputTokens }),
};
}
async upsert(docs: KnowledgeDocument[], ctx: AdapterContext): Promise<void> {
if (docs.length === 0) return;
this.requirePool();
const readiness = await this.ready();
const embedder = this.requireEmbedder();
const generation = (await this.writeTarget(ctx, embedder, true))!;
await this.ensureVectorTable(generation.dim, readiness);
// An unchanged passage is rewritten in place, keeping its vector. When its
// row is gone by the time the write runs (a concurrent rewrite of the same
// document removed it), the write is rolled back and planned again, so the
// passage is embedded and inserted rather than lost.
for (let attempt = 1; ; attempt++) {
const missing = await this.writePassages(docs, ctx.source.id, generation, readiness, embedder);
if (missing === undefined) return;
if (attempt >= UPSERT_ATTEMPTS) {
throw new Error(
`knowledge-pgvector: passage '${missing}' of source '${ctx.source.id}' was removed by a concurrent write ` +
`${UPSERT_ATTEMPTS} times while this write ran; nothing of this batch was written. Retry the write.`,
);
}
}
}
/**
* One attempt of {@link upsert}: read the stored chunk hashes, embed what
* changed, write in one transaction. Answers `undefined` when written, or the
* id of an unchanged passage whose row was gone (the transaction is rolled
* back).
*/
private async writePassages(
docs: KnowledgeDocument[],
sourceId: string,
generation: GenerationRow,
readiness: Readiness,
embedder: IEmbedder,
): Promise<string | undefined> {
const pool = this.requirePool();
const table = qualified(this.schema, vectorTable(generation.dim));
const planned = docs.map((doc) => ({
doc,
passages: passagesOf(doc).map((p) => ({ ...p, hash: chunkHash(p.content) })),
}));
const stored = new Map<string, string>();
const existing = await pool.query<{ chunk_id: string; chunk_hash: string }>(
`SELECT chunk_id, chunk_hash FROM ${table} WHERE source_id = $1 AND generation = $2 AND document_id = ANY($3::text[])`,
[sourceId, generation.generation, docs.map((d) => d.id)],
);
for (const row of existing.rows) stored.set(row.chunk_id, row.chunk_hash);
// Embed what changed, before any connection is taken for the write.
const toEmbed = planned.flatMap((p) => p.passages).filter((p) => stored.get(p.id) !== p.hash);
const vectors = toEmbed.length === 0 ? [] : await embedder.embed(toEmbed.map((p) => p.content));
if (vectors.length !== toEmbed.length) {
throw new Error(
`knowledge-pgvector: the embedder answered ${vectors.length} vector(s) for ${toEmbed.length} passage(s); nothing was written.`,
);
}
const vectorOf = new Map<string, number[]>();
toEmbed.forEach((p, i) => {
const v = vectors[i];
if (!Array.isArray(v) || v.length !== generation.dim) {
throw new Error(
`knowledge-pgvector: the embedder answered a ${Array.isArray(v) ? v.length : 'missing'}-wide vector for ` +
`passage '${p.id}', and this source's index holds ${generation.dim}-wide vectors; nothing was written.`,
);
}
vectorOf.set(p.id, v);
});
let missing: string | undefined;
const written = await this.transaction(async (client) => {
for (const { doc, passages } of planned) {
await client.query(
`DELETE FROM ${table} WHERE source_id = $1 AND generation = $2 AND document_id = $3 AND NOT (chunk_id = ANY($4::text[]))`,
[sourceId, generation.generation, doc.id, passages.map((p) => p.id)],
);
for (const p of passages) {
const common = [
sourceId,
generation.generation,
p.id,
doc.id,
doc.sourceRecordId ?? null,
doc.organizationId ?? null,
p.content,
doc.title ?? null,
doc.docHash ?? null,
p.hash,
JSON.stringify(doc.metadata ?? {}),
p.headingPath === undefined ? null : JSON.stringify(p.headingPath),
];
const vector = vectorOf.get(p.id);
if (vector === undefined) {
// Unchanged text: the stored vector stands; the rest of the row is rewritten.
const updated = await client.query(
`UPDATE ${table} SET document_id = $4, source_record_id = $5, organization_id = $6, content = $7, title = $8,
doc_hash = $9, chunk_hash = $10, metadata = $11::jsonb, heading_path = $12::jsonb
WHERE source_id = $1 AND generation = $2 AND chunk_id = $3 AND chunk_hash = $10
RETURNING chunk_id`,
common,
);
if (updated.rows.length === 0) {
missing = p.id;
return false;
}
} else {
await client.query(
`INSERT INTO ${table} (source_id, generation, chunk_id, document_id, source_record_id, organization_id, content,
title, doc_hash, chunk_hash, metadata, heading_path, embedding)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11::jsonb, $12::jsonb, $13::${readiness.extensionSchema}.vector)
ON CONFLICT (source_id, generation, chunk_id) DO UPDATE SET document_id = EXCLUDED.document_id,
source_record_id = EXCLUDED.source_record_id, organization_id = EXCLUDED.organization_id,
content = EXCLUDED.content, title = EXCLUDED.title, doc_hash = EXCLUDED.doc_hash,
chunk_hash = EXCLUDED.chunk_hash, metadata = EXCLUDED.metadata, heading_path = EXCLUDED.heading_path,
embedding = EXCLUDED.embedding`,
[...common, vectorLiteral(vector)],
);
}
}
}
return true;
});
return written ? undefined : missing;
}
async delete(documentIds: string[], ctx: AdapterContext): Promise<void> {
if (documentIds.length === 0) return;
const pool = this.requirePool();
await this.ready();
// From every generation of the source: a removed document leaves them all.
const widths = await pool.query<{ dim: number }>(
`SELECT DISTINCT dim FROM ${qualified(this.schema, GENERATION_TABLE)} WHERE source_id = $1`,
[ctx.source.id],
);
for (const { dim } of widths.rows) {
if (!(await this.tableExists(Number(dim)))) continue;
await pool.query(
`DELETE FROM ${qualified(this.schema, vectorTable(Number(dim)))} WHERE source_id = $1 AND document_id = ANY($2::text[])`,
[ctx.source.id, documentIds],
);
}
}
async listDocuments(ctx: AdapterContext, opts: AdapterListDocumentsOptions): Promise<AdapterListDocumentsPage> {
if (!Number.isInteger(opts.limit) || opts.limit < 1) {
throw new RangeError(`listDocuments: limit must be a positive integer, got ${String(opts.limit)}`);
}
const pool = this.requirePool();
await this.ready();
// The embedder first, as on every other call: without one the service's
// fingerprint carries no model or width, and comparing it would refuse a
// healthy index as one to rebuild. No embedder is SERVICE_UNAVAILABLE.
const embedder = this.requireEmbedder();
// The generation the writes go to (ADR-0161 K8, item 1).
const generation = await this.writeTarget(ctx, embedder, false);
if (!generation || !(await this.tableExists(generation.dim))) return { documents: [] };
// In byte order ("C"), both the order and the cursor's comparison, so a
// cursor stays valid whatever the database's collation.
const params: unknown[] = [ctx.source.id, generation.generation, opts.limit + 1];
const after = opts.cursor === undefined ? '' : `AND document_id COLLATE "C" > $4`;
if (opts.cursor !== undefined) params.push(opts.cursor);
const page = await pool.query<{ document_id: string; doc_hash: string | null }>(
`SELECT document_id, max(doc_hash) AS doc_hash FROM ${qualified(this.schema, vectorTable(generation.dim))}
WHERE source_id = $1 AND generation = $2 ${after}
GROUP BY document_id ORDER BY document_id COLLATE "C" LIMIT $3`,
params,
);
const rows = page.rows.slice(0, opts.limit);
return {
documents: rows.map((r) => (r.doc_hash === null ? { documentId: r.document_id } : { documentId: r.document_id, docHash: r.doc_hash })),
...(page.rows.length > opts.limit ? { nextCursor: rows[rows.length - 1].document_id } : {}),
};
}
async search(query: string, opts: AdapterSearchOptions): Promise<KnowledgeHit[]> {
// An empty organization set ranks nothing (ADR-0161 K8, item 9).
if (opts.organizationIds !== undefined && opts.organizationIds.length === 0) return [];
// A record scope with no term that has ids ranks nothing (ADR-0161 K8, item 17).
if (opts.scope !== undefined && !opts.scope.some((term) => term.ids.length > 0)) return [];
const pool = this.requirePool();
const readiness = await this.ready();
// The embedder first: without one the service's fingerprint carries no
// model or width, and comparing it would refuse a healthy index as one to
// rebuild. No embedder is SERVICE_UNAVAILABLE, as on every other call.
const embedder = this.requireEmbedder();
const generation = await this.activeGeneration(opts.source.id);
if (!generation) return [];
// Vectors of two fingerprints are never ranked against each other (K7, rule 5).
if (opts.index !== undefined && generation.fingerprint !== opts.index.fingerprint) {
throw rebuildRequired(opts.source.id, await this.clearSql(opts.source.id, generation.dim));
}
if (embedder.dimensions !== generation.dim) throw rebuildRequired(opts.source.id, await this.clearSql(opts.source.id, generation.dim));
if (!(await this.tableExists(generation.dim))) return [];
const topK = Math.max(0, Math.floor(opts.topK));
if (topK === 0) return [];
// The query is embedded before a connection is taken.
const [vector] = await embedder.embed([`${this.queryPrefix}${query}`]);
if (!Array.isArray(vector) || vector.length !== generation.dim) {
throw new Error(
`knowledge-pgvector: the embedder answered a ${Array.isArray(vector) ? vector.length : 'missing'}-wide query ` +
`vector, and this source's index holds ${generation.dim}-wide vectors.`,
);
}
if (vector.every((x) => x === 0)) return [];
const ext = readiness.extensionSchema;
const params: unknown[] = [opts.source.id, generation.generation, vectorLiteral(vector), topK];
const where = ['source_id = $1', 'generation = $2'];
if (opts.organizationIds !== undefined) {
params.push(opts.organizationIds);
where.push(`organization_id = ANY($${params.length}::text[])`);
}
if (opts.scope !== undefined) {
// The record scope (ADR-0161 K8, item 17), a union of terms, beside the
// organization filter so `topK` is filled from in-scope documents. Ids
// and field names go as bound parameters. A `metadata` term holds for a
// stored string or number equal to an id as text, or a list with such an
// element; a missing value or any other kind holds for nothing.
const terms: string[] = [];
for (const term of opts.scope) {
if (term.ids.length === 0) continue;
params.push(term.ids);
const ids = `$${params.length}::text[]`;
if (term.kind === 'record') {
terms.push(`source_record_id = ANY(${ids})`);
continue;
}
params.push(term.field);
const value = `(metadata -> $${params.length}::text)`;
terms.push(
`EXISTS (SELECT 1 FROM jsonb_array_elements(CASE jsonb_typeof(${value}) WHEN 'array' THEN ${value} ` +
`ELSE jsonb_build_array(${value}) END) AS e(v) ` +
`WHERE jsonb_typeof(e.v) IN ('string', 'number') AND e.v #>> '{}' = ANY(${ids}))`,
);
}
where.push(`(${terms.join(' OR ')})`);
}
for (const [key, value] of Object.entries(opts.filter ?? {})) {
params.push(key, JSON.stringify(value === undefined ? null : value));
where.push(`(metadata -> $${params.length - 1}) = $${params.length}::jsonb`);
}
const result = await pool.query<{
chunk_id: string;
document_id: string;
source_record_id: string | null;
content: string;
title: string | null;
metadata: Record<string, unknown> | null;
doc_hash: string | null;
heading_path: string[] | null;
distance: number;
}>(
`SELECT * FROM (
SELECT chunk_id, document_id, source_record_id, content, title, metadata, doc_hash, heading_path,
embedding OPERATOR(${ext}.<=>) $3::${ext}.vector AS distance
FROM ${qualified(this.schema, vectorTable(generation.dim))}
WHERE ${where.join(' AND ')}
) ranked
WHERE distance < 1
ORDER BY distance, chunk_id
LIMIT $4`,
params,
);
return result.rows.map((r) => ({
chunkId: r.chunk_id,
documentId: r.document_id,
sourceId: opts.source.id,
...(r.source_record_id === null ? {} : { sourceRecordId: r.source_record_id }),
score: 1 - Number(r.distance),
snippet: r.content,
...(r.title === null ? {} : { title: r.title }),
metadata: r.metadata ?? {},
// The stored hash, as given (ADR-0161 K8, item 3): the service's
// record-level freshness check compares it with the record as it reads now.
...(r.doc_hash === null ? {} : { docHash: r.doc_hash }),
// The passage's heading path, when the splitter gave it one (item 14).
...(Array.isArray(r.heading_path) ? { headingPath: r.heading_path } : {}),
}));
}
async indexStatus(ctx: AdapterContext): Promise<AdapterIndexStatus> {
this.requirePool();
await this.ready();
const generation = await this.activeGeneration(ctx.source.id);
const status: AdapterIndexStatus = {};
if (generation?.fingerprint != null) status.fingerprint = generation.fingerprint;
if (generation?.recipe_hash != null) status.recipeHash = generation.recipe_hash;
return status;
}
async beginBuild(ctx: AdapterContext): Promise<void> {
const index = requireIndex(ctx, 'beginBuild');
this.requirePool();
await this.ready();
const embedder = this.requireEmbedder();
const generation = (await this.activeGeneration(ctx.source.id))
?? (await this.openFirstGeneration(ctx.source.id, index.fingerprint, embedder));
if (generation.fingerprint === index.fingerprint && generation.dim === embedder.dimensions) return;
const sql = await this.clearSql(ctx.source.id, generation.dim);
throw Object.assign(
new Error(
`Knowledge source '${ctx.source.id}' was indexed under another embedding model, split or content recipe, ` +
'and this store does not yet build a second index beside the first.',
),
{
code: KNOWLEDGE_REBUILD_REQUIRED,
status: 409,
remedy:
'Either restore the earlier embedding and split configuration, or clear the source\'s index and run a ' +
`complete reindex. Run as a database owner:\n${sql.join('\n')}`,
},
);
}
async completeBuild(ctx: AdapterContext): Promise<void> {
const index = requireIndex(ctx, 'completeBuild');
const pool = this.requirePool();
await this.ready();
const updated = await pool.query(
`UPDATE ${qualified(this.schema, GENERATION_TABLE)} SET recipe_hash = $3
WHERE source_id = $1 AND state = 'active' AND fingerprint = $2 RETURNING generation`,
[ctx.source.id, index.fingerprint, index.recipeHash],
);
if (updated.rows.length === 0) {
throw new Error(
`completeBuild: source '${ctx.source.id}' holds no active generation under this fingerprint; call beginBuild first.`,
);
}
}
async healthCheck(): Promise<{ ok: boolean; message?: string }> {
try {
this.requirePool();
const r = await this.ready();
return { ok: true, message: `pgvector ${r.version}, tables in schema '${this.schema}'` };
} catch (err) {
const remedy = remedyOf(err);
return { ok: false, message: `${errorText(err)}${remedy === undefined ? '' : ` ${remedy}`}` };
}
}
// ── Internals ─────────────────────────────────────────────────────
private requirePool(): PgPoolLike {
if (!this.pool) {
throw notReady('it has no PostgreSQL database', this.unavailable ?? 'It was given no PostgreSQL connection.', []);
}
return this.pool;
}
private requireEmbedder(): IEmbedder {
const embedder = this.resolveEmbedder();
if (!embedder) {
throw Object.assign(
new Error(
'knowledge-pgvector has no embedder: register an embedder service (Settings → AI & Embedder) or pass ' +
'`embedder` to the plugin. Passages are sent to that embedder\'s provider.',
),
{ code: 'SERVICE_UNAVAILABLE', status: 503 },
);
}
return embedder;
}
/**
* Make the store ready once: the `vector` extension (created when it is
* missing and the connection may), then the schema and the generation
* table. Until that succeeds nothing is created past the step that failed,
* and the refusal carries the SQL to run by hand; the next call tries again.
*/
private async ready(): Promise<Readiness> {
if (this.readiness) return this.readiness;
const pool = this.requirePool();
const ddl = schemaDdl(this.schema);
let ext: { schema: string; version: string } | undefined;
try {
ext = await this.findExtension(pool);
} catch (err) {
// The first query failed: the database is not reachable (or refuses this
// role). Not a missing extension, and no SQL would fix it.
throw notReady(
'it cannot reach its PostgreSQL database',
`The connection failed: ${errorText(err)}. Check the store's URL, the network and the role's access.`,
[],
);
}
if (!ext) {
let refusal: unknown;
try {
await pool.query('CREATE EXTENSION IF NOT EXISTS vector');
ext = await this.findExtension(pool);
} catch (err) {
refusal = err;
}
if (!ext) {
throw notReady(
"the PostgreSQL 'vector' extension (pgvector) is not installed in its database",
`This connection could not install it${refusal === undefined ? '' : ` (${errorText(refusal)})`}.`,
['CREATE EXTENSION IF NOT EXISTS vector;', ...ddl.map((statement) => `${statement};`)],
);
}
}
try {
for (const statement of ddl) await pool.query(statement);
} catch (err) {
throw notReady(
'its schema and generation table could not be created',
`Creating schema '${this.schema}' failed (${errorText(err)}).`,
ddl.map((statement) => `${statement};`),
);
}
this.readiness = { extensionSchema: quoteIdentifier(ext.schema), version: ext.version };
return this.readiness;
}
private async findExtension(pool: PgPoolLike): Promise<{ schema: string; version: string } | undefined> {
const found = await pool.query<{ schema: string; version: string }>(
`SELECT n.nspname AS schema, e.extversion AS version
FROM pg_extension e JOIN pg_namespace n ON n.oid = e.extnamespace
WHERE e.extname = 'vector'`,
);
return found.rows[0];
}
private async ensureVectorTable(dimensions: number, readiness: Readiness): Promise<void> {
if (this.tables.has(dimensions)) return;
const pool = this.requirePool();
const ddl = vectorTableDdl(this.schema, dimensions, `${readiness.extensionSchema}.vector`);
try {
for (const statement of ddl) await pool.query(statement);
} catch (err) {
throw notReady(
`its ${dimensions}-wide passage table could not be created`,
`Creating ${qualified(this.schema, vectorTable(dimensions))} failed (${errorText(err)}).`,
ddl.map((statement) => `${statement};`),
);
}
this.tables.add(dimensions);
}
private async tableExists(dimensions: number): Promise<boolean> {
if (this.tables.has(dimensions)) return true;
const found = await this.requirePool().query<{ present: string | null }>(
'SELECT to_regclass($1) AS present',
[qualified(this.schema, vectorTable(dimensions))],
);
const present = found.rows[0]?.present != null;
if (present) this.tables.add(dimensions);
return present;
}
private async activeGeneration(sourceId: string): Promise<GenerationRow | undefined> {
const found = await this.requirePool().query<GenerationRow>(
`SELECT generation, fingerprint, recipe_hash, dim FROM ${qualified(this.schema, GENERATION_TABLE)}
WHERE source_id = $1 AND state = 'active' ORDER BY generation DESC LIMIT 1`,
[sourceId],
);
const row = found.rows[0];
return row === undefined ? undefined : { ...row, generation: Number(row.generation), dim: Number(row.dim) };
}
/**
* A source that holds nothing takes the first fingerprint it is handed
* (there is nothing to mix with). "Holds nothing" is made true: passage rows
* of the source left in any passage table without a generation row (a
* generation row deleted by hand, its passages not) are deleted in the same
* transaction that opens the generation, so a first generation never adopts
* vectors embedded under another fingerprint. Only the call whose insert
* opened the generation clears; a concurrent one waits on the key and finds
* it open.
*/
private async openFirstGeneration(sourceId: string, fingerprint: string | undefined, embedder: IEmbedder): Promise<GenerationRow> {
await this.transaction(async (client) => {
const opened = await client.query(
`INSERT INTO ${qualified(this.schema, GENERATION_TABLE)} (source_id, generation, fingerprint, dim, model, state)
VALUES ($1, 1, $2, $3, $4, 'active') ON CONFLICT (source_id, generation) DO NOTHING RETURNING generation`,
[sourceId, fingerprint ?? null, embedder.dimensions, embedder.model ?? null],
);
if (opened.rows.length === 0) return true;
for (const width of await this.passageWidths(client)) {
await client.query(`DELETE FROM ${qualified(this.schema, vectorTable(width))} WHERE source_id = $1`, [sourceId]);
}
return true;
});
return (await this.activeGeneration(sourceId))!;
}
/** The widths of the passage tables that exist in the store's schema. */
private async passageWidths(client: Pick<PgClientLike, 'query'> = this.requirePool()): Promise<number[]> {
const found = await client.query<{ table_name: string }>(
`SELECT table_name FROM information_schema.tables
WHERE table_schema = $1 AND table_name ~ '^knowledge_vec_d[0-9]+$' ORDER BY table_name`,
[this.schema],
);
return found.rows.map((r) => Number(r.table_name.slice('knowledge_vec_d'.length)));
}
/**
* The generation a write carrying `ctx.index` goes to: the active one, when
* its fingerprint is the one carried (or none is carried); refused when it is
* another, or when the embedder's width is not the generation's. A source
* that holds nothing gets its first generation (`create`), or none.
*/
private async writeTarget(
ctx: AdapterContext,
embedder: IEmbedder | undefined,
create: boolean,
): Promise<GenerationRow | undefined> {
const active = await this.activeGeneration(ctx.source.id);
if (!active) {
if (!create || !embedder) return undefined;
const opened = await this.openFirstGeneration(ctx.source.id, ctx.index?.fingerprint, embedder);
return this.checkTarget(ctx, opened, embedder);
}
return this.checkTarget(ctx, active, embedder);
}
private async checkTarget(
ctx: AdapterContext,
generation: GenerationRow,
embedder: IEmbedder | undefined,
): Promise<GenerationRow> {
const fingerprint = ctx.index?.fingerprint;
if (fingerprint !== undefined && generation.fingerprint !== fingerprint) {
throw rebuildRequired(ctx.source.id, await this.clearSql(ctx.source.id, generation.dim));
}
if (embedder !== undefined && embedder.dimensions !== generation.dim) {
throw rebuildRequired(ctx.source.id, await this.clearSql(ctx.source.id, generation.dim));
}
return generation;
}
/**
* The statements that clear everything this store holds for a source, so it
* can be built afresh: one DELETE per passage table that exists, then the
* generation rows — passages first, so no generation row is ever missing
* while its passages remain.
*/
private async clearSql(sourceId: string, knownWidth: number): Promise<string[]> {
const literal = `'${sourceId.replace(/'/g, "''")}'`;
// The tables found in the schema, and always the width the source's
// generation records: should the catalogue read fail, the statements still
// clear that table before the generation row, never the row alone.
const found = await this.passageWidths().catch(() => [] as number[]);
const widths = [...new Set([...found, knownWidth])].sort((a, b) => a - b);
return [
...widths.map((width) => `DELETE FROM ${qualified(this.schema, vectorTable(width))} WHERE source_id = ${literal};`),
`DELETE FROM ${qualified(this.schema, GENERATION_TABLE)} WHERE source_id = ${literal};`,
];
}
/** Run `work` in one transaction: committed when it answers `true`, rolled back when it answers `false` or throws. */
private async transaction(work: (client: PgClientLike) => Promise<boolean>): Promise<boolean> {
const client = await this.requirePool().connect();
try {
await client.query('BEGIN');
const commit = await work(client);
await client.query(commit ? 'COMMIT' : 'ROLLBACK');
client.release();
return commit;
} catch (err) {
await client.query('ROLLBACK').catch(() => undefined);
client.release(err instanceof Error ? err : true);
throw err;
}
}
}
/** The passages of a document: the service's `chunks`, or the whole text as one. Empty passages are not stored. */
function passagesOf(doc: KnowledgeDocument): Array<{ id: string; content: string; headingPath?: string[] }> {
const passages: Array<{ id: string; content: string; headingPath?: string[] }> =
doc.chunks ?? [{ id: `${doc.id}#0`, content: doc.content }];
return passages
.filter((p) => p.content.trim() !== '')
.map((p) => ({ id: p.id, content: p.content, ...(p.headingPath === undefined ? {} : { headingPath: p.headingPath }) }));
}
/** The hash of the text the adapter embeds for a passage. */
function chunkHash(text: string): string {
return `c1:${createHash('sha256').update(text).digest('hex')}`;
}
function requireIndex(ctx: AdapterContext, member: string): NonNullable<AdapterContext['index']> {
if (ctx.index === undefined) {
throw new Error(`${member}: the call carries no index fingerprint (ctx.index); it is made by the knowledge service.`);
}
return ctx.index;
}
/**
* A refusal under another fingerprint (ADR-0161 K7, rules 4 and 5). Its
* message is one sentence a search caller may read; the statements that clear
* the source are operator text, carried in `remedy` — the knowledge service
* shows it in a reindex result and its log, never to a search caller.
*/
function rebuildRequired(sourceId: string, clearSql: string[]): Error {
return Object.assign(
new Error(
`Knowledge source '${sourceId}' needs a rebuild: its index was built under another embedding model, split or ` +
'content recipe, and it is not searched until a complete reindex has rebuilt it.',
),
{
code: KNOWLEDGE_REBUILD_REQUIRED,
status: 409,
remedy:
'Run a complete reindex of the source; if the store refuses to begin it, clear the source\'s index first. ' +
`Run as a database owner:\n${clearSql.join('\n')}`,
},
);
}
/**
* The store is not ready. The message is one sentence with no database
* detail, as a search caller may read it; what went wrong and the SQL to run
* are operator text in `remedy`, shown by `healthCheck`, the plugin's boot
* warning and the knowledge service's log.
*/
function notReady(diagnosis: string, detail: string, sql: string[]): Error {
const remedy = `${detail}${sql.length === 0 ? '' : ` Run as a database owner, then retry (no restart needed):\n${sql.join('\n')}`}`;
return Object.assign(new Error(`The knowledge store (pgvector) is not ready: ${diagnosis}.`), {
code: 'SERVICE_UNAVAILABLE',
status: 503,
remedy,
});
}
/** The operator text an adapter error carries, if any. */
function remedyOf(err: unknown): string | undefined {
const remedy = err !== null && typeof err === 'object' ? (err as { remedy?: unknown }).remedy : undefined;
return typeof remedy === 'string' && remedy !== '' ? remedy : undefined;
}
/** A PostgreSQL identifier, double-quoted, an embedded quote doubled. */
function quoteIdentifier(name: string): string {
return `"${name.replace(/"/g, '""')}"`;
}
function errorText(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}