@retrivora-ai/rag-engine 2.6.0 → 2.6.3

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 (58) hide show
  1. package/README.md +8 -6
  2. package/dist/ChromaDBProvider-NDA2IBOH.mjs +8 -0
  3. package/dist/{ILLMProvider-DUCL1r75.d.mts → ILLMProvider-CzXcTA6d.d.mts} +21 -3
  4. package/dist/{ILLMProvider-DUCL1r75.d.ts → ILLMProvider-CzXcTA6d.d.ts} +21 -3
  5. package/dist/MilvusProvider-H6GGL5FQ.mjs +8 -0
  6. package/dist/MongoDBProvider-KC3GF3FH.mjs +8 -0
  7. package/dist/MultiTablePostgresProvider-6LJBFZL2.mjs +9 -0
  8. package/dist/PineconeProvider-22RF2QPF.mjs +10 -0
  9. package/dist/PineconeProvider-HBBIXLT3.mjs +10 -0
  10. package/dist/PostgreSQLProvider-VWFL4OIP.mjs +8 -0
  11. package/dist/QdrantProvider-UU7RGXBI.mjs +8 -0
  12. package/dist/RedisProvider-MIIPSAAE.mjs +8 -0
  13. package/dist/SimpleGraphProvider-HJIS4S7Y.mjs +70 -0
  14. package/dist/UniversalVectorProvider-NO3L255G.mjs +9 -0
  15. package/dist/WeaviateProvider-YRZDZFAC.mjs +8 -0
  16. package/dist/chunk-23PFNEBF.mjs +107 -0
  17. package/dist/chunk-3E4GZGDX.mjs +10851 -0
  18. package/dist/chunk-3SQ4DODU.mjs +238 -0
  19. package/dist/chunk-AHMMUZ4W.mjs +152 -0
  20. package/dist/chunk-ASBNNEVI.mjs +81 -0
  21. package/dist/chunk-AT4TFUQN.mjs +219 -0
  22. package/dist/chunk-BEPLYQBS.mjs +89 -0
  23. package/dist/chunk-G7MJRFCO.mjs +340 -0
  24. package/dist/chunk-IHWKVAUR.mjs +112 -0
  25. package/dist/chunk-IMP6FUCY.mjs +30 -0
  26. package/dist/chunk-LFNV6DEL.mjs +281 -0
  27. package/dist/chunk-MAXMVO7B.mjs +211 -0
  28. package/dist/chunk-MGJ6V2IT.mjs +203 -0
  29. package/dist/chunk-N3TI27LG.mjs +334 -0
  30. package/dist/chunk-NWYE4Q7B.mjs +334 -0
  31. package/dist/chunk-OJMAT4EG.mjs +219 -0
  32. package/dist/chunk-QKZM3RSJ.mjs +10851 -0
  33. package/dist/chunk-QUY3E6HJ.mjs +168 -0
  34. package/dist/chunk-SG6XYLQE.mjs +53 -0
  35. package/dist/chunk-UT6SZZEC.mjs +69 -0
  36. package/dist/chunk-VW3U4MMX.mjs +157 -0
  37. package/dist/chunk-WOJCOTXI.mjs +281 -0
  38. package/dist/chunk-XP6NJMX4.mjs +168 -0
  39. package/dist/chunk-XYBKDJDW.mjs +696 -0
  40. package/dist/handlers/index.d.mts +2 -2
  41. package/dist/handlers/index.d.ts +2 -2
  42. package/dist/handlers/index.js +9736 -9354
  43. package/dist/handlers/index.mjs +24 -13686
  44. package/dist/{index-CsBcqng1.d.ts → index-BBbt5A2y.d.mts} +61 -47
  45. package/dist/{index-BVSHblli.d.mts → index-BST9Gzsv.d.mts} +8 -7
  46. package/dist/{index-D6Oq2zyc.d.mts → index-BZ_rR6V8.d.ts} +61 -47
  47. package/dist/{index-DGW0NNmR.d.ts → index-qLvtMUr-.d.ts} +8 -7
  48. package/dist/index.css +23 -0
  49. package/dist/index.d.mts +4 -4
  50. package/dist/index.d.ts +4 -4
  51. package/dist/index.js +278 -190
  52. package/dist/index.min.css +1 -1
  53. package/dist/index.mjs +218 -1558
  54. package/dist/server.d.mts +58 -12
  55. package/dist/server.d.ts +58 -12
  56. package/dist/server.js +968 -577
  57. package/dist/server.mjs +395 -14244
  58. package/package.json +1 -1
@@ -0,0 +1,281 @@
1
+ import {
2
+ ConfigFetcher
3
+ } from "./chunk-N3TI27LG.mjs";
4
+ import {
5
+ BaseVectorProvider
6
+ } from "./chunk-IMP6FUCY.mjs";
7
+ import {
8
+ __spreadValues
9
+ } from "./chunk-UT6SZZEC.mjs";
10
+
11
+ // src/providers/vectordb/PineconeProvider.ts
12
+ import { Pinecone } from "@pinecone-database/pinecone";
13
+ var PineconeProvider = class extends BaseVectorProvider {
14
+ constructor(config) {
15
+ super(config);
16
+ const opts = config.options || {};
17
+ this.apiKey = opts.apiKey || process.env.PINECONE_API_KEY || "";
18
+ }
19
+ getControlPlaneUrl() {
20
+ const urls = [
21
+ process.env.RETRIVORA_CONTROL_PLANE_URL,
22
+ process.env.NEXT_PUBLIC_RETRIVORA_CONTROL_PLANE_URL,
23
+ "https://www.retrivora.com"
24
+ ].filter((url) => Boolean(url));
25
+ return urls[0].replace(/\/$/, "");
26
+ }
27
+ static getValidator() {
28
+ return {
29
+ validate(config) {
30
+ const errors = [];
31
+ const opts = config.options || {};
32
+ const hasLicenseKey = Boolean(
33
+ config.licenseKey || process.env.RETRIVORA_LICENSE_KEY
34
+ );
35
+ if (!opts.apiKey && !hasLicenseKey) {
36
+ errors.push({
37
+ field: "vectorDb.options.apiKey",
38
+ message: "Pinecone API key is required when RETRIVORA_LICENSE_KEY is not set",
39
+ suggestion: "Set PINECONE_API_KEY environment variable or supply RETRIVORA_LICENSE_KEY for keyless vector storage",
40
+ severity: "error"
41
+ });
42
+ }
43
+ return errors;
44
+ }
45
+ };
46
+ }
47
+ static getHealthChecker() {
48
+ return {
49
+ async check(config) {
50
+ var _a, _b;
51
+ const opts = config.options || {};
52
+ const indexName = config.indexName;
53
+ const timestamp = Date.now();
54
+ try {
55
+ const { Pinecone: Pinecone2 } = await import("@pinecone-database/pinecone");
56
+ const client = new Pinecone2({ apiKey: opts.apiKey });
57
+ const indexes = await client.listIndexes();
58
+ const indexNames = (_b = (_a = indexes.indexes) == null ? void 0 : _a.map((i) => i.name)) != null ? _b : [];
59
+ if (!indexNames.includes(indexName)) {
60
+ return {
61
+ healthy: false,
62
+ provider: "pinecone",
63
+ error: `Index "${indexName}" not found. Available: ${indexNames.join(", ")}`,
64
+ timestamp
65
+ };
66
+ }
67
+ return {
68
+ healthy: true,
69
+ provider: "pinecone",
70
+ capabilities: { indexes: indexNames.length, targetIndex: indexName },
71
+ timestamp
72
+ };
73
+ } catch (error) {
74
+ return {
75
+ healthy: false,
76
+ provider: "pinecone",
77
+ error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
78
+ timestamp
79
+ };
80
+ }
81
+ }
82
+ };
83
+ }
84
+ async initialize() {
85
+ var _a, _b;
86
+ if (this.client) return;
87
+ let key = this.apiKey || process.env.PINECONE_API_KEY || "";
88
+ if (!key) {
89
+ const licenseKey = process.env.RETRIVORA_LICENSE_KEY;
90
+ this.licenseKey = licenseKey;
91
+ const projectId = process.env.RAG_PROJECT_ID || process.env.NEXT_PUBLIC_RAG_PROJECT_ID || this.projectId || "default";
92
+ this.projectId = projectId;
93
+ const remoteConfig = await ConfigFetcher.fetchRemoteVectorConfig(projectId, licenseKey);
94
+ if (remoteConfig) {
95
+ if (remoteConfig.apiKey) {
96
+ key = remoteConfig.apiKey;
97
+ this.apiKey = key;
98
+ }
99
+ if (remoteConfig.indexName) {
100
+ this.indexName = remoteConfig.indexName;
101
+ }
102
+ if (remoteConfig.projectId) {
103
+ this.projectId = remoteConfig.projectId;
104
+ }
105
+ if (remoteConfig.namespace) {
106
+ this.namespace = remoteConfig.namespace;
107
+ }
108
+ }
109
+ }
110
+ if (!key) {
111
+ console.warn(
112
+ "[PineconeProvider] Warning: No Pinecone API key available. Set PINECONE_API_KEY in your environment, or ensure the Retrivora Control Plane (https://www.retrivora.com) is reachable for proxy-based vector operations."
113
+ );
114
+ return;
115
+ }
116
+ this.client = new Pinecone({ apiKey: key });
117
+ try {
118
+ const indexes = await this.client.listIndexes();
119
+ const names = (_b = (_a = indexes.indexes) == null ? void 0 : _a.map((i) => i.name)) != null ? _b : [];
120
+ if (!names.includes(this.indexName)) {
121
+ console.warn(`[PineconeProvider] Target index "${this.indexName}" not listed. Proceeding with configured index name.`);
122
+ }
123
+ } catch (err) {
124
+ console.warn("[PineconeProvider] List index verification warning:", err instanceof Error ? err.message : String(err));
125
+ }
126
+ }
127
+ async getActiveIndex(namespace) {
128
+ if (!this.client) {
129
+ await this.initialize();
130
+ }
131
+ if (!this.client) {
132
+ throw new Error("[PineconeProvider] Cannot execute vector operation: Pinecone client failed to initialize.");
133
+ }
134
+ const idx = this.client.index(this.indexName);
135
+ return namespace ? idx.namespace(namespace) : idx.namespace("");
136
+ }
137
+ async upsert(doc, namespace) {
138
+ var _a, _b;
139
+ if (!this.client) {
140
+ if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
141
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
142
+ method: "POST",
143
+ headers: __spreadValues({
144
+ "Content-Type": "application/json",
145
+ "x-license-key": this.licenseKey
146
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
147
+ body: JSON.stringify({
148
+ projectId: this.projectId,
149
+ indexName: this.indexName,
150
+ namespace: namespace || this.projectId,
151
+ records: [{ id: String(doc.id), values: doc.vector, metadata: __spreadValues({ content: doc.content }, (_a = doc.metadata) != null ? _a : {}) }]
152
+ })
153
+ });
154
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy upsert failed: ${await res.text()}`);
155
+ return;
156
+ }
157
+ const targetIdx = await this.getActiveIndex(namespace);
158
+ await targetIdx.upsert({
159
+ records: [{
160
+ id: String(doc.id),
161
+ values: doc.vector,
162
+ metadata: __spreadValues({ content: doc.content }, (_b = doc.metadata) != null ? _b : {})
163
+ }]
164
+ });
165
+ }
166
+ async batchUpsert(docs, namespace) {
167
+ const BATCH = 100;
168
+ if (!this.client) {
169
+ if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
170
+ for (let i = 0; i < docs.length; i += BATCH) {
171
+ const records = docs.slice(i, i + BATCH).map((d) => {
172
+ var _a;
173
+ return {
174
+ id: String(d.id),
175
+ values: d.vector,
176
+ metadata: __spreadValues({ content: d.content }, (_a = d.metadata) != null ? _a : {})
177
+ };
178
+ });
179
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/upsert`, {
180
+ method: "POST",
181
+ headers: __spreadValues({
182
+ "Content-Type": "application/json",
183
+ "x-license-key": this.licenseKey
184
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
185
+ body: JSON.stringify({
186
+ projectId: this.projectId,
187
+ indexName: this.indexName,
188
+ namespace: namespace || this.projectId,
189
+ records
190
+ })
191
+ });
192
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy batch upsert failed: ${await res.text()}`);
193
+ }
194
+ return;
195
+ }
196
+ const targetIdx = await this.getActiveIndex(namespace);
197
+ for (let i = 0; i < docs.length; i += BATCH) {
198
+ const records = docs.slice(i, i + BATCH).map((d) => {
199
+ var _a;
200
+ return {
201
+ id: String(d.id),
202
+ values: d.vector,
203
+ metadata: __spreadValues({ content: d.content }, (_a = d.metadata) != null ? _a : {})
204
+ };
205
+ });
206
+ await targetIdx.upsert({ records });
207
+ }
208
+ }
209
+ async query(vector, topK, namespace, filter) {
210
+ var _a, _b;
211
+ const pineconeFilter = this.sanitizeFilter(filter);
212
+ if (!this.client) {
213
+ if (!this.licenseKey) throw new Error("[PineconeProvider] Cannot execute vector operation: Missing API key and License key.");
214
+ const res = await fetch(`${this.getControlPlaneUrl()}/api/v1/vectors/query`, {
215
+ method: "POST",
216
+ headers: __spreadValues({
217
+ "Content-Type": "application/json",
218
+ "x-license-key": this.licenseKey
219
+ }, this.projectId ? { "x-project-id": this.projectId } : {}),
220
+ body: JSON.stringify({
221
+ projectId: this.projectId,
222
+ indexName: this.indexName,
223
+ namespace: namespace || this.projectId,
224
+ vector,
225
+ topK,
226
+ filter: pineconeFilter
227
+ })
228
+ });
229
+ if (!res.ok) throw new Error(`[PineconeProvider] Proxy query failed: ${await res.text()}`);
230
+ const data = await res.json();
231
+ return ((_a = data.matches) != null ? _a : []).map((m) => {
232
+ var _a2, _b2;
233
+ return {
234
+ id: m.id,
235
+ score: (_a2 = m.score) != null ? _a2 : 0,
236
+ content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
237
+ metadata: m.metadata
238
+ };
239
+ });
240
+ }
241
+ const targetIdx = await this.getActiveIndex(namespace);
242
+ const result = await targetIdx.query(__spreadValues({
243
+ vector,
244
+ topK,
245
+ includeMetadata: true
246
+ }, Object.keys(pineconeFilter).length > 0 ? { filter: pineconeFilter } : {}));
247
+ return ((_b = result.matches) != null ? _b : []).map((m) => {
248
+ var _a2, _b2;
249
+ return {
250
+ id: m.id,
251
+ score: (_a2 = m.score) != null ? _a2 : 0,
252
+ content: ((_b2 = m.metadata) == null ? void 0 : _b2.content) || "",
253
+ metadata: m.metadata
254
+ };
255
+ });
256
+ }
257
+ async delete(id, namespace) {
258
+ const targetIdx = await this.getActiveIndex(namespace);
259
+ await targetIdx.deleteOne({ id: String(id) });
260
+ }
261
+ async deleteNamespace(namespace) {
262
+ const targetIdx = await this.getActiveIndex(namespace);
263
+ await targetIdx.deleteAll();
264
+ }
265
+ async ping() {
266
+ try {
267
+ if (!this.client) await this.initialize();
268
+ if (!this.client) return false;
269
+ await this.client.listIndexes();
270
+ return true;
271
+ } catch (e) {
272
+ return false;
273
+ }
274
+ }
275
+ async disconnect() {
276
+ }
277
+ };
278
+
279
+ export {
280
+ PineconeProvider
281
+ };
@@ -0,0 +1,211 @@
1
+ import {
2
+ BaseVectorProvider
3
+ } from "./chunk-IMP6FUCY.mjs";
4
+ import {
5
+ __spreadValues
6
+ } from "./chunk-UT6SZZEC.mjs";
7
+
8
+ // src/providers/vectordb/MongoDBProvider.ts
9
+ import { MongoClient } from "mongodb";
10
+ var MongoDBProvider = class extends BaseVectorProvider {
11
+ constructor(config) {
12
+ super(config);
13
+ this.initialized = false;
14
+ const opts = config.options;
15
+ if (!opts.uri || !opts.database || !opts.collection) {
16
+ throw new Error("[MongoDBProvider] options uri, database, and collection are required.");
17
+ }
18
+ this.client = new MongoClient(opts.uri, { serverSelectionTimeoutMS: 5e3 });
19
+ this.dbName = opts.database;
20
+ this.collectionName = opts.collection;
21
+ this.embeddingKey = opts.embeddingKey || "embedding";
22
+ this.contentKey = opts.contentKey || "content";
23
+ this.metadataKey = opts.metadataKey || "metadata";
24
+ }
25
+ static getValidator() {
26
+ return {
27
+ validate(config) {
28
+ const errors = [];
29
+ const opts = config.options || {};
30
+ if (!opts.uri) {
31
+ errors.push({
32
+ field: "vectorDb.options.uri",
33
+ message: "MongoDB connection URI is required",
34
+ suggestion: "Set MONGODB_URI environment variable",
35
+ severity: "error"
36
+ });
37
+ }
38
+ if (!opts.database) {
39
+ errors.push({
40
+ field: "vectorDb.options.database",
41
+ message: "MongoDB database name is required",
42
+ severity: "error"
43
+ });
44
+ }
45
+ if (!opts.collection) {
46
+ errors.push({
47
+ field: "vectorDb.options.collection",
48
+ message: "MongoDB collection name is required",
49
+ severity: "error"
50
+ });
51
+ }
52
+ return errors;
53
+ }
54
+ };
55
+ }
56
+ static getHealthChecker() {
57
+ return {
58
+ async check(config) {
59
+ const opts = config.options || {};
60
+ const timestamp = Date.now();
61
+ try {
62
+ const { MongoClient: MongoClient2 } = await import("mongodb");
63
+ const client = new MongoClient2(opts.uri);
64
+ await client.connect();
65
+ const db = client.db(opts.database);
66
+ await db.command({ ping: 1 });
67
+ const collections = await db.listCollections({ name: opts.collection }).toArray();
68
+ await client.close();
69
+ return {
70
+ healthy: true,
71
+ provider: "mongodb",
72
+ capabilities: {
73
+ database: opts.database,
74
+ collection: opts.collection,
75
+ exists: collections.length > 0
76
+ },
77
+ timestamp
78
+ };
79
+ } catch (error) {
80
+ return {
81
+ healthy: false,
82
+ provider: "mongodb",
83
+ error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
84
+ timestamp
85
+ };
86
+ }
87
+ }
88
+ };
89
+ }
90
+ async initialize() {
91
+ if (this.initialized) return;
92
+ await this.client.connect();
93
+ this.db = this.client.db(this.dbName);
94
+ this.collection = this.db.collection(this.collectionName);
95
+ this.initialized = true;
96
+ }
97
+ async upsert(doc, namespace) {
98
+ const document = __spreadValues({
99
+ _id: doc.id,
100
+ [this.embeddingKey]: doc.vector,
101
+ [this.contentKey]: doc.content,
102
+ [this.metadataKey]: doc.metadata || {}
103
+ }, namespace ? { namespace } : {});
104
+ await this.collection.updateOne({ _id: doc.id }, { $set: document }, { upsert: true });
105
+ }
106
+ async batchUpsert(docs, namespace) {
107
+ const operations = docs.map((doc) => ({
108
+ updateOne: {
109
+ filter: { _id: doc.id },
110
+ update: {
111
+ $set: __spreadValues({
112
+ [this.embeddingKey]: doc.vector,
113
+ [this.contentKey]: doc.content,
114
+ [this.metadataKey]: doc.metadata || {}
115
+ }, namespace ? { namespace } : {})
116
+ },
117
+ upsert: true
118
+ }
119
+ }));
120
+ await this.collection.bulkWrite(operations);
121
+ }
122
+ async query(vector, topK, namespace, filter) {
123
+ const publicFilter = this.sanitizeFilter(filter);
124
+ const vectorSearchFilter = {};
125
+ const matchFilter = {};
126
+ if (namespace) {
127
+ vectorSearchFilter.namespace = namespace;
128
+ }
129
+ for (const [key, value] of Object.entries(publicFilter)) {
130
+ if (key === "namespace") {
131
+ vectorSearchFilter.namespace = value;
132
+ } else {
133
+ if (typeof value === "string") {
134
+ matchFilter[key] = { $regex: new RegExp(`^${value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}$`, "i") };
135
+ } else {
136
+ matchFilter[key] = value;
137
+ }
138
+ }
139
+ }
140
+ const pipeline = [
141
+ {
142
+ $vectorSearch: __spreadValues({
143
+ index: this.config.indexName || "vector_index",
144
+ path: this.embeddingKey,
145
+ queryVector: vector,
146
+ numCandidates: this.config.options.numCandidates || Math.max(topK * 20, 200),
147
+ limit: topK * 2
148
+ }, Object.keys(vectorSearchFilter).length > 0 ? { filter: vectorSearchFilter } : {})
149
+ }
150
+ ];
151
+ if (Object.keys(matchFilter).length > 0) {
152
+ pipeline.push({ $match: matchFilter });
153
+ }
154
+ pipeline.push(
155
+ { $limit: topK },
156
+ {
157
+ $project: {
158
+ _id: 1,
159
+ [this.contentKey]: 1,
160
+ [this.metadataKey]: 1,
161
+ namespace: 1,
162
+ score: { $meta: "vectorSearchScore" }
163
+ }
164
+ }
165
+ );
166
+ const results = await this.collection.aggregate(pipeline).toArray();
167
+ return results.map((res) => {
168
+ const normalizedScore = res.score * 2 - 1;
169
+ return {
170
+ id: res._id,
171
+ content: res[this.contentKey],
172
+ metadata: res[this.metadataKey] || {},
173
+ score: normalizedScore
174
+ };
175
+ });
176
+ }
177
+ async delete(id, namespace) {
178
+ await this.collection.deleteOne(__spreadValues({ _id: id }, namespace ? { namespace } : {}));
179
+ }
180
+ async deleteNamespace(namespace) {
181
+ await this.collection.deleteMany({ namespace });
182
+ }
183
+ sanitizeFilter(filter) {
184
+ const sanitized = super.sanitizeFilter(filter);
185
+ const mongoFilter = {};
186
+ for (const [key, value] of Object.entries(sanitized)) {
187
+ if (key === "namespace") {
188
+ mongoFilter[key] = value;
189
+ } else {
190
+ mongoFilter[`${this.metadataKey}.${key}`] = value;
191
+ }
192
+ }
193
+ return mongoFilter;
194
+ }
195
+ async ping() {
196
+ try {
197
+ await this.db.command({ ping: 1 });
198
+ return true;
199
+ } catch (e) {
200
+ return false;
201
+ }
202
+ }
203
+ async disconnect() {
204
+ await this.client.close();
205
+ this.initialized = false;
206
+ }
207
+ };
208
+
209
+ export {
210
+ MongoDBProvider
211
+ };
@@ -0,0 +1,203 @@
1
+ import {
2
+ BaseVectorProvider
3
+ } from "./chunk-IMP6FUCY.mjs";
4
+
5
+ // src/providers/vectordb/PostgreSQLProvider.ts
6
+ import { Pool } from "pg";
7
+ var PostgreSQLProvider = class extends BaseVectorProvider {
8
+ constructor(config) {
9
+ var _a;
10
+ super(config);
11
+ this.tableName = this.indexName.replace(/[^a-z0-9_]/gi, "_");
12
+ const opts = config.options;
13
+ if (!opts.connectionString) throw new Error("[PostgreSQLProvider] options.connectionString is required");
14
+ this.connectionString = opts.connectionString;
15
+ this.dimensions = (_a = opts.dimensions) != null ? _a : 1536;
16
+ }
17
+ static getValidator() {
18
+ return {
19
+ validate(config) {
20
+ const errors = [];
21
+ const opts = config.options || {};
22
+ if (!opts.connectionString) {
23
+ errors.push({
24
+ field: "vectorDb.options.connectionString",
25
+ message: "PostgreSQL connection string is required",
26
+ suggestion: "Set PGVECTOR_CONNECTION_STRING environment variable",
27
+ severity: "error"
28
+ });
29
+ }
30
+ if (opts.tables && typeof opts.tables !== "string" && !Array.isArray(opts.tables)) {
31
+ errors.push({
32
+ field: "vectorDb.options.tables",
33
+ message: "PostgreSQL tables must be a string or a string array",
34
+ severity: "error"
35
+ });
36
+ }
37
+ return errors;
38
+ }
39
+ };
40
+ }
41
+ static getHealthChecker() {
42
+ return {
43
+ async check(config) {
44
+ const opts = config.options || {};
45
+ const timestamp = Date.now();
46
+ try {
47
+ const { Client } = await import("pg");
48
+ const client = new Client({ connectionString: opts.connectionString });
49
+ await client.connect();
50
+ const result = await client.query(`
51
+ SELECT EXISTS(SELECT 1 FROM pg_extension WHERE extname = 'vector');
52
+ `);
53
+ const hasVector = result.rows[0].exists;
54
+ await client.end();
55
+ return {
56
+ healthy: true,
57
+ provider: "postgresql",
58
+ capabilities: { pgvectorInstalled: hasVector },
59
+ timestamp
60
+ };
61
+ } catch (error) {
62
+ return {
63
+ healthy: false,
64
+ provider: "postgresql",
65
+ error: `Connection failed: ${error instanceof Error ? error.message : String(error)}`,
66
+ timestamp
67
+ };
68
+ }
69
+ }
70
+ };
71
+ }
72
+ async initialize() {
73
+ this.pool = new Pool({ connectionString: this.connectionString });
74
+ const client = await this.pool.connect();
75
+ try {
76
+ await client.query("CREATE EXTENSION IF NOT EXISTS vector");
77
+ await client.query(`
78
+ CREATE TABLE IF NOT EXISTS ${this.tableName} (
79
+ id TEXT PRIMARY KEY,
80
+ namespace TEXT NOT NULL DEFAULT '',
81
+ content TEXT NOT NULL,
82
+ metadata JSONB,
83
+ embedding VECTOR(${this.dimensions})
84
+ )
85
+ `);
86
+ await client.query(`
87
+ CREATE INDEX IF NOT EXISTS ${this.tableName}_embedding_idx
88
+ ON ${this.tableName}
89
+ USING hnsw (embedding vector_cosine_ops)
90
+ `);
91
+ } finally {
92
+ client.release();
93
+ }
94
+ }
95
+ async upsert(doc, namespace = "") {
96
+ var _a;
97
+ const vectorLiteral = `[${doc.vector.join(",")}]`;
98
+ await this.pool.query(
99
+ `INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
100
+ VALUES ($1, $2, $3, $4, $5::vector)
101
+ ON CONFLICT (id) DO UPDATE
102
+ SET namespace = EXCLUDED.namespace,
103
+ content = EXCLUDED.content,
104
+ metadata = EXCLUDED.metadata,
105
+ embedding = EXCLUDED.embedding`,
106
+ [doc.id, namespace, doc.content, JSON.stringify((_a = doc.metadata) != null ? _a : {}), vectorLiteral]
107
+ );
108
+ }
109
+ async batchUpsert(docs, namespace = "") {
110
+ if (docs.length === 0) return;
111
+ const client = await this.pool.connect();
112
+ try {
113
+ await client.query("BEGIN");
114
+ const BATCH_SIZE = 50;
115
+ for (let i = 0; i < docs.length; i += BATCH_SIZE) {
116
+ const batch = docs.slice(i, i + BATCH_SIZE);
117
+ const values = [];
118
+ const valuePlaceholders = batch.map((doc, idx) => {
119
+ var _a;
120
+ const offset = idx * 5;
121
+ values.push(doc.id, namespace, doc.content, JSON.stringify((_a = doc.metadata) != null ? _a : {}), `[${doc.vector.join(",")}]`);
122
+ return `($${offset + 1}, $${offset + 2}, $${offset + 3}, $${offset + 4}, $${offset + 5}::vector)`;
123
+ }).join(", ");
124
+ const query = `
125
+ INSERT INTO ${this.tableName} (id, namespace, content, metadata, embedding)
126
+ VALUES ${valuePlaceholders}
127
+ ON CONFLICT (id) DO UPDATE
128
+ SET namespace = EXCLUDED.namespace,
129
+ content = EXCLUDED.content,
130
+ metadata = EXCLUDED.metadata,
131
+ embedding = EXCLUDED.embedding
132
+ `;
133
+ await client.query(query, values);
134
+ }
135
+ await client.query("COMMIT");
136
+ } catch (error) {
137
+ await client.query("ROLLBACK");
138
+ throw error;
139
+ } finally {
140
+ client.release();
141
+ }
142
+ }
143
+ async query(vector, topK, namespace, filter) {
144
+ const vectorLiteral = `[${vector.join(",")}]`;
145
+ let whereClause = namespace ? `WHERE namespace = $3` : "";
146
+ const params = [vectorLiteral, topK];
147
+ if (namespace) params.push(namespace);
148
+ const publicFilter = this.sanitizeFilter(filter);
149
+ if (Object.keys(publicFilter).length > 0) {
150
+ const filterConditions = Object.entries(publicFilter).map(([key, val]) => {
151
+ const paramIdx = params.length + 1;
152
+ params.push(String(val));
153
+ return `metadata->>'${key.replace(/[^a-zA-Z0-9_.-]/g, "")}' = $${paramIdx}`;
154
+ }).join(" AND ");
155
+ whereClause = whereClause ? `${whereClause} AND ${filterConditions}` : `WHERE ${filterConditions}`;
156
+ }
157
+ const client = await this.pool.connect();
158
+ try {
159
+ const rawEfSearch = Number(this.config.options.efSearch) || Math.max(topK * 10, 40);
160
+ const efSearch = Math.max(1, Math.min(1e3, Math.floor(Number.isFinite(rawEfSearch) ? rawEfSearch : 40)));
161
+ await client.query(`SET LOCAL hnsw.ef_search = ${efSearch}`);
162
+ const result = await client.query(
163
+ `SELECT id, content, metadata, 1 - (embedding <=> $1::vector) AS score
164
+ FROM ${this.tableName}
165
+ ${whereClause}
166
+ ORDER BY embedding <=> $1::vector
167
+ LIMIT $2`,
168
+ params
169
+ );
170
+ return result.rows.map((row) => ({
171
+ id: String(row["id"]),
172
+ score: parseFloat(String(row["score"])),
173
+ content: String(row["content"]),
174
+ metadata: row["metadata"]
175
+ }));
176
+ } finally {
177
+ client.release();
178
+ }
179
+ }
180
+ async delete(id, namespace) {
181
+ const where = namespace ? "WHERE id = $1 AND namespace = $2" : "WHERE id = $1";
182
+ const params = namespace ? [id, namespace] : [id];
183
+ await this.pool.query(`DELETE FROM ${this.tableName} ${where}`, params);
184
+ }
185
+ async deleteNamespace(namespace) {
186
+ await this.pool.query(`DELETE FROM ${this.tableName} WHERE namespace = $1`, [namespace]);
187
+ }
188
+ async ping() {
189
+ try {
190
+ await this.pool.query("SELECT 1");
191
+ return true;
192
+ } catch (e) {
193
+ return false;
194
+ }
195
+ }
196
+ async disconnect() {
197
+ await this.pool.end();
198
+ }
199
+ };
200
+
201
+ export {
202
+ PostgreSQLProvider
203
+ };