@mcp-monorepo/notion-query 1.2.0 → 1.3.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.
- package/dist/index.js +53 -6
- package/dist/index.js.map +1 -1
- package/dist/lib/config.d.ts +44 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/config.js +49 -0
- package/dist/lib/config.js.map +1 -0
- package/dist/lib/notion-syncer.d.ts +27 -0
- package/dist/lib/notion-syncer.d.ts.map +1 -0
- package/dist/lib/notion-syncer.js +212 -0
- package/dist/lib/notion-syncer.js.map +1 -0
- package/dist/lib/property-parser.d.ts +8 -10
- package/dist/lib/property-parser.d.ts.map +1 -1
- package/dist/lib/property-parser.js +102 -15
- package/dist/lib/property-parser.js.map +1 -1
- package/dist/lib/sync-state-manager.d.ts +29 -0
- package/dist/lib/sync-state-manager.d.ts.map +1 -0
- package/dist/lib/sync-state-manager.js +45 -0
- package/dist/lib/sync-state-manager.js.map +1 -0
- package/dist/local-rag/DEMO.d.ts +22 -0
- package/dist/local-rag/DEMO.d.ts.map +1 -0
- package/dist/local-rag/DEMO.js +142 -0
- package/dist/local-rag/DEMO.js.map +1 -0
- package/dist/local-rag/chunker.d.ts +24 -0
- package/dist/local-rag/chunker.d.ts.map +1 -0
- package/dist/local-rag/chunker.js +58 -0
- package/dist/local-rag/chunker.js.map +1 -0
- package/dist/local-rag/embedder.d.ts +43 -0
- package/dist/local-rag/embedder.d.ts.map +1 -0
- package/dist/local-rag/embedder.js +74 -0
- package/dist/local-rag/embedder.js.map +1 -0
- package/dist/local-rag/embedder.service.d.ts +15 -0
- package/dist/local-rag/embedder.service.d.ts.map +1 -0
- package/dist/local-rag/embedder.service.js +84 -0
- package/dist/local-rag/embedder.service.js.map +1 -0
- package/dist/local-rag/embedder.worker.d.ts +2 -0
- package/dist/local-rag/embedder.worker.d.ts.map +1 -0
- package/dist/local-rag/embedder.worker.js +34 -0
- package/dist/local-rag/embedder.worker.js.map +1 -0
- package/dist/local-rag/errors.d.ts +31 -0
- package/dist/local-rag/errors.d.ts.map +1 -0
- package/dist/local-rag/errors.js +47 -0
- package/dist/local-rag/errors.js.map +1 -0
- package/dist/local-rag/html-parser.d.ts +2 -0
- package/dist/local-rag/html-parser.d.ts.map +1 -0
- package/dist/local-rag/html-parser.js +32 -0
- package/dist/local-rag/html-parser.js.map +1 -0
- package/dist/local-rag/index.d.ts +67 -0
- package/dist/local-rag/index.d.ts.map +1 -0
- package/dist/local-rag/index.js +410 -0
- package/dist/local-rag/index.js.map +1 -0
- package/dist/local-rag/parser.d.ts +59 -0
- package/dist/local-rag/parser.d.ts.map +1 -0
- package/dist/local-rag/parser.js +206 -0
- package/dist/local-rag/parser.js.map +1 -0
- package/dist/local-rag/types.d.ts +209 -0
- package/dist/local-rag/types.d.ts.map +1 -0
- package/dist/local-rag/types.js +5 -0
- package/dist/local-rag/types.js.map +1 -0
- package/dist/local-rag/utils/pool.d.ts +60 -0
- package/dist/local-rag/utils/pool.d.ts.map +1 -0
- package/dist/local-rag/utils/pool.js +140 -0
- package/dist/local-rag/utils/pool.js.map +1 -0
- package/dist/local-rag/utils/typed-emitter.d.ts +28 -0
- package/dist/local-rag/utils/typed-emitter.d.ts.map +1 -0
- package/dist/local-rag/utils/typed-emitter.js +44 -0
- package/dist/local-rag/utils/typed-emitter.js.map +1 -0
- package/dist/local-rag/vectordb/index.d.ts +91 -0
- package/dist/local-rag/vectordb/index.d.ts.map +1 -0
- package/dist/local-rag/vectordb/index.js +278 -0
- package/dist/local-rag/vectordb/index.js.map +1 -0
- package/dist/local-rag/vectordb/manager.d.ts +28 -0
- package/dist/local-rag/vectordb/manager.d.ts.map +1 -0
- package/dist/local-rag/vectordb/manager.js +91 -0
- package/dist/local-rag/vectordb/manager.js.map +1 -0
- package/dist/local-rag/vectordb/migration.d.ts +27 -0
- package/dist/local-rag/vectordb/migration.d.ts.map +1 -0
- package/dist/local-rag/vectordb/migration.js +121 -0
- package/dist/local-rag/vectordb/migration.js.map +1 -0
- package/dist/local-rag/vectordb/retriever.d.ts +51 -0
- package/dist/local-rag/vectordb/retriever.d.ts.map +1 -0
- package/dist/local-rag/vectordb/retriever.js +157 -0
- package/dist/local-rag/vectordb/retriever.js.map +1 -0
- package/dist/local-rag/vectordb/schema.d.ts +33 -0
- package/dist/local-rag/vectordb/schema.d.ts.map +1 -0
- package/dist/local-rag/vectordb/schema.js +102 -0
- package/dist/local-rag/vectordb/schema.js.map +1 -0
- package/dist/local-rag/watcher.d.ts +48 -0
- package/dist/local-rag/watcher.d.ts.map +1 -0
- package/dist/local-rag/watcher.js +102 -0
- package/dist/local-rag/watcher.js.map +1 -0
- package/dist/tools/create-pages.d.ts +2 -1
- package/dist/tools/create-pages.d.ts.map +1 -1
- package/dist/tools/create-pages.js +3 -2
- package/dist/tools/create-pages.js.map +1 -1
- package/dist/tools/fetch.d.ts +2 -1
- package/dist/tools/fetch.d.ts.map +1 -1
- package/dist/tools/fetch.js +2 -1
- package/dist/tools/fetch.js.map +1 -1
- package/dist/tools/query-datasource.d.ts +2 -1
- package/dist/tools/query-datasource.d.ts.map +1 -1
- package/dist/tools/query-datasource.js +3 -3
- package/dist/tools/query-datasource.js.map +1 -1
- package/dist/tools/search.d.ts +12 -0
- package/dist/tools/search.d.ts.map +1 -0
- package/dist/tools/search.js +75 -0
- package/dist/tools/search.js.map +1 -0
- package/dist/tools/update-page.d.ts +4 -0
- package/dist/tools/update-page.d.ts.map +1 -0
- package/dist/tools/update-page.js +135 -0
- package/dist/tools/update-page.js.map +1 -0
- package/package.json +15 -1
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { cpus } from 'node:os';
|
|
2
|
+
import { Worker } from 'node:worker_threads';
|
|
3
|
+
import { logger } from '@mcp-monorepo/shared';
|
|
4
|
+
import { EmbeddingError } from '../errors.js';
|
|
5
|
+
export class WorkerPool {
|
|
6
|
+
workerPath;
|
|
7
|
+
workerData;
|
|
8
|
+
config;
|
|
9
|
+
workers = [];
|
|
10
|
+
taskQueue = [];
|
|
11
|
+
taskIdCounter = 0;
|
|
12
|
+
workerIdCounter = 0;
|
|
13
|
+
activeTasks = new Map();
|
|
14
|
+
idleCheckInterval;
|
|
15
|
+
constructor(workerPath, workerData, config = {}) {
|
|
16
|
+
this.workerPath = workerPath;
|
|
17
|
+
this.workerData = workerData;
|
|
18
|
+
this.config = {
|
|
19
|
+
maxWorkers: config.maxWorkers ?? Math.max(1, cpus().length - 1),
|
|
20
|
+
minWorkers: config.minWorkers ?? 0,
|
|
21
|
+
idleTimeoutMs: config.idleTimeoutMs ?? 1_800_000,
|
|
22
|
+
};
|
|
23
|
+
if (this.config.idleTimeoutMs > 0) {
|
|
24
|
+
this.idleCheckInterval = setInterval(() => this._reapIdleWorkers(), this.config.idleTimeoutMs / 2);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
runTask(task) {
|
|
28
|
+
const taskId = this.taskIdCounter++;
|
|
29
|
+
return new Promise((resolve, reject) => {
|
|
30
|
+
this.activeTasks.set(taskId, { resolve, reject });
|
|
31
|
+
this.taskQueue.push({ taskId, task, resolve, reject });
|
|
32
|
+
this._dispatch();
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
_dispatch() {
|
|
36
|
+
if (this.taskQueue.length === 0) {
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
// 1. Prioritize using an existing idle worker
|
|
40
|
+
const availableWorker = this.workers.find((w) => w.currentTaskId === undefined);
|
|
41
|
+
if (availableWorker) {
|
|
42
|
+
const taskInfo = this.taskQueue.shift();
|
|
43
|
+
if (taskInfo) {
|
|
44
|
+
this._assignTask(availableWorker, taskInfo);
|
|
45
|
+
}
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
// 2. If no idle worker, create a new one if we're not at the max limit
|
|
49
|
+
if (this.workers.length < this.config.maxWorkers) {
|
|
50
|
+
const taskInfo = this.taskQueue.shift();
|
|
51
|
+
if (taskInfo) {
|
|
52
|
+
logger.info(`Scaling up workers to ${this.workers.length + 1} to handle load...`);
|
|
53
|
+
const newWorker = this._createWorker();
|
|
54
|
+
this.workers.push(newWorker);
|
|
55
|
+
this._assignTask(newWorker, taskInfo);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
// 3. If we are at the max limit and all workers are busy, the task remains queued.
|
|
59
|
+
// It will be picked up when a worker finishes its current task.
|
|
60
|
+
}
|
|
61
|
+
_createWorker() {
|
|
62
|
+
const worker = new Worker(this.workerPath, { workerData: this.workerData });
|
|
63
|
+
const trackedWorker = {
|
|
64
|
+
id: ++this.workerIdCounter,
|
|
65
|
+
worker,
|
|
66
|
+
currentTaskId: undefined,
|
|
67
|
+
lastUsed: Date.now(),
|
|
68
|
+
};
|
|
69
|
+
worker.on('message', (message) => {
|
|
70
|
+
this._handleTaskCompletion(trackedWorker, message);
|
|
71
|
+
});
|
|
72
|
+
worker.on('error', (err) => {
|
|
73
|
+
this._handleWorkerCrash(trackedWorker, err);
|
|
74
|
+
});
|
|
75
|
+
return trackedWorker;
|
|
76
|
+
}
|
|
77
|
+
_assignTask(worker, taskInfo) {
|
|
78
|
+
worker.currentTaskId = taskInfo.taskId;
|
|
79
|
+
worker.worker.postMessage({ task: taskInfo.task, taskId: taskInfo.taskId });
|
|
80
|
+
}
|
|
81
|
+
_handleTaskCompletion(worker, message) {
|
|
82
|
+
if (worker.currentTaskId === undefined)
|
|
83
|
+
return; // Should not happen
|
|
84
|
+
const taskCallbacks = this.activeTasks.get(worker.currentTaskId);
|
|
85
|
+
if (taskCallbacks) {
|
|
86
|
+
if (message.status === 'success') {
|
|
87
|
+
taskCallbacks.resolve(message.result);
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
taskCallbacks.reject(new EmbeddingError(message.error.message, message.error));
|
|
91
|
+
}
|
|
92
|
+
this.activeTasks.delete(worker.currentTaskId);
|
|
93
|
+
}
|
|
94
|
+
worker.currentTaskId = undefined;
|
|
95
|
+
worker.lastUsed = Date.now();
|
|
96
|
+
// After finishing, check if there are more tasks to run.
|
|
97
|
+
this._dispatch();
|
|
98
|
+
}
|
|
99
|
+
_handleWorkerCrash(crashedWorker, err) {
|
|
100
|
+
logger.error(`Worker #${crashedWorker.id} crashed.`, err);
|
|
101
|
+
// Reject the task it was working on
|
|
102
|
+
if (crashedWorker.currentTaskId !== undefined) {
|
|
103
|
+
const taskCallbacks = this.activeTasks.get(crashedWorker.currentTaskId);
|
|
104
|
+
taskCallbacks?.reject(new EmbeddingError(`Worker #${crashedWorker.id} crashed while processing task.`, err));
|
|
105
|
+
this.activeTasks.delete(crashedWorker.currentTaskId);
|
|
106
|
+
}
|
|
107
|
+
// Remove the dead worker from the pool
|
|
108
|
+
const workerIndex = this.workers.findIndex((w) => w.id === crashedWorker.id);
|
|
109
|
+
if (workerIndex !== -1) {
|
|
110
|
+
this.workers.splice(workerIndex, 1);
|
|
111
|
+
}
|
|
112
|
+
// Immediately try to dispatch another task, which may create a new worker if needed
|
|
113
|
+
this._dispatch();
|
|
114
|
+
}
|
|
115
|
+
_reapIdleWorkers() {
|
|
116
|
+
const now = Date.now();
|
|
117
|
+
const idleWorkers = this.workers.filter((w) => w.currentTaskId === undefined);
|
|
118
|
+
for (const worker of idleWorkers) {
|
|
119
|
+
if (this.workers.length <= this.config.minWorkers) {
|
|
120
|
+
break; // Stop reaping if we're at the minimum
|
|
121
|
+
}
|
|
122
|
+
if (now - worker.lastUsed > this.config.idleTimeoutMs) {
|
|
123
|
+
logger.info(`Scaling down: Terminating idle worker #${worker.id}.`);
|
|
124
|
+
worker.worker.terminate();
|
|
125
|
+
const workerIndex = this.workers.findIndex((w) => w.id === worker.id);
|
|
126
|
+
if (workerIndex !== -1) {
|
|
127
|
+
this.workers.splice(workerIndex, 1);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async destroy() {
|
|
133
|
+
if (this.idleCheckInterval) {
|
|
134
|
+
clearInterval(this.idleCheckInterval);
|
|
135
|
+
}
|
|
136
|
+
await Promise.all(this.workers.map((w) => w.worker.terminate()));
|
|
137
|
+
this.workers = [];
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
//# sourceMappingURL=pool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pool.js","sourceRoot":"","sources":["../../../src/local-rag/utils/pool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,SAAS,CAAA;AAC9B,OAAO,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAA;AAE5C,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C,OAAO,EAAE,cAAc,EAAE,MAAM,cAAc,CAAA;AAkC7C,MAAM,OAAO,UAAU;IACJ,UAAU,CAAK;IACf,UAAU,CAAQ;IAClB,MAAM,CAAsB;IAErC,OAAO,GAAoB,EAAE,CAAA;IACpB,SAAS,GAAe,EAAE,CAAA;IACnC,aAAa,GAAG,CAAC,CAAA;IACjB,eAAe,GAAG,CAAC,CAAA;IACV,WAAW,GAAG,IAAI,GAAG,EAAgD,CAAA;IAC9E,iBAAiB,CAA4B;IAErD,YAAY,UAAe,EAAE,UAAkB,EAAE,SAAqB,EAAE;QACtE,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAC5B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAA;QAE5B,IAAI,CAAC,MAAM,GAAG;YACZ,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,EAAE,CAAC,MAAM,GAAG,CAAC,CAAC;YAC/D,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,CAAC;YAClC,aAAa,EAAE,MAAM,CAAC,aAAa,IAAI,SAAS;SACjD,CAAA;QAED,IAAI,IAAI,CAAC,MAAM,CAAC,aAAa,GAAG,CAAC,EAAE,CAAC;YAClC,IAAI,CAAC,iBAAiB,GAAG,WAAW,CAAC,GAAG,EAAE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,CAAC;IAEM,KAAK,CAAC,OAAO;QAClB,IAAI,IAAI,CAAC,iBAAiB,EAAE,CAAC;YAC3B,aAAa,CAAC,IAAI,CAAC,iBAAiB,CAAC,CAAA;QACvC,CAAC;QACD,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,CAAA;QAChE,IAAI,CAAC,OAAO,GAAG,EAAE,CAAA;IACnB,CAAC;CACF"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
type Listener<T extends unknown[]> = (...args: T) => void;
|
|
2
|
+
/**
|
|
3
|
+
* A generic, type-safe event emitter.
|
|
4
|
+
* Ensures that event names and their payload types are strictly checked at compile time.
|
|
5
|
+
* @template T - A map of event names to their listener argument types.
|
|
6
|
+
*/
|
|
7
|
+
export declare class TypedEventEmitter<T extends Record<string, unknown[]>> {
|
|
8
|
+
private readonly listeners;
|
|
9
|
+
/**
|
|
10
|
+
* Registers an event listener for a given event.
|
|
11
|
+
* @param event - The name of the event to listen for.
|
|
12
|
+
* @param listener - The callback function to execute when the event is emitted.
|
|
13
|
+
*/
|
|
14
|
+
on<E extends keyof T>(event: E, listener: Listener<T[E]>): void;
|
|
15
|
+
/**
|
|
16
|
+
* Emits an event, calling all registered listeners with the provided arguments.
|
|
17
|
+
* @param event - The name of the event to emit.
|
|
18
|
+
* @param args - The arguments to pass to the event listeners.
|
|
19
|
+
*/
|
|
20
|
+
emit<E extends keyof T>(event: E, ...args: T[E]): void;
|
|
21
|
+
/**
|
|
22
|
+
* Removes all listeners, or all listeners for a specific event.
|
|
23
|
+
* @param event - Optional. The name of the event to remove listeners for.
|
|
24
|
+
*/
|
|
25
|
+
off<E extends keyof T>(event?: E): void;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=typed-emitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-emitter.d.ts","sourceRoot":"","sources":["../../../src/local-rag/utils/typed-emitter.ts"],"names":[],"mappings":"AAAA,KAAK,QAAQ,CAAC,CAAC,SAAS,OAAO,EAAE,IAAI,CAAC,GAAG,IAAI,EAAE,CAAC,KAAK,IAAI,CAAA;AAEzD;;;;GAIG;AACH,qBAAa,iBAAiB,CAAC,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,EAAE,CAAC;IAChE,OAAO,CAAC,QAAQ,CAAC,SAAS,CAA6C;IAEvE;;;;OAIG;IACI,EAAE,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;IAMtE;;;;OAIG;IACI,IAAI,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;IAS7D;;;OAGG;IACI,GAAG,CAAC,CAAC,SAAS,MAAM,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,GAAG,IAAI;CAO/C"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A generic, type-safe event emitter.
|
|
3
|
+
* Ensures that event names and their payload types are strictly checked at compile time.
|
|
4
|
+
* @template T - A map of event names to their listener argument types.
|
|
5
|
+
*/
|
|
6
|
+
export class TypedEventEmitter {
|
|
7
|
+
listeners = new Map();
|
|
8
|
+
/**
|
|
9
|
+
* Registers an event listener for a given event.
|
|
10
|
+
* @param event - The name of the event to listen for.
|
|
11
|
+
* @param listener - The callback function to execute when the event is emitted.
|
|
12
|
+
*/
|
|
13
|
+
on(event, listener) {
|
|
14
|
+
const existing = this.listeners.get(event) ?? [];
|
|
15
|
+
// @ts-expect-error Undefined subtype
|
|
16
|
+
this.listeners.set(event, [...existing, listener]);
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Emits an event, calling all registered listeners with the provided arguments.
|
|
20
|
+
* @param event - The name of the event to emit.
|
|
21
|
+
* @param args - The arguments to pass to the event listeners.
|
|
22
|
+
*/
|
|
23
|
+
emit(event, ...args) {
|
|
24
|
+
const eventListeners = this.listeners.get(event);
|
|
25
|
+
if (eventListeners) {
|
|
26
|
+
for (const listener of eventListeners) {
|
|
27
|
+
listener(...args);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Removes all listeners, or all listeners for a specific event.
|
|
33
|
+
* @param event - Optional. The name of the event to remove listeners for.
|
|
34
|
+
*/
|
|
35
|
+
off(event) {
|
|
36
|
+
if (event) {
|
|
37
|
+
this.listeners.delete(event);
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
this.listeners.clear();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=typed-emitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typed-emitter.js","sourceRoot":"","sources":["../../../src/local-rag/utils/typed-emitter.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,OAAO,iBAAiB;IACX,SAAS,GAAG,IAAI,GAAG,EAAmC,CAAA;IAEvE;;;;OAIG;IACI,EAAE,CAAoB,KAAQ,EAAE,QAAwB;QAC7D,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,EAAE,CAAA;QAChD,qCAAqC;QACrC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,QAAQ,EAAE,QAAQ,CAAC,CAAC,CAAA;IACpD,CAAC;IAED;;;;OAIG;IACI,IAAI,CAAoB,KAAQ,EAAE,GAAG,IAAU;QACpD,MAAM,cAAc,GAAG,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;QAChD,IAAI,cAAc,EAAE,CAAC;YACnB,KAAK,MAAM,QAAQ,IAAI,cAAc,EAAE,CAAC;gBACtC,QAAQ,CAAC,GAAG,IAAI,CAAC,CAAA;YACnB,CAAC;QACH,CAAC;IACH,CAAC;IAED;;;OAGG;IACI,GAAG,CAAoB,KAAS;QACrC,IAAI,KAAK,EAAE,CAAC;YACV,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,SAAS,CAAC,KAAK,EAAE,CAAA;QACxB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { type Table } from '@lancedb/lancedb';
|
|
2
|
+
import { StoreManager } from './manager.js';
|
|
3
|
+
import { Retriever } from './retriever.js';
|
|
4
|
+
import type { DocumentMetadata, ListItem, ListOptions, QueryFilters, QueryResult, StatusReport, VectorChunk, WatchedPath } from '../types.js';
|
|
5
|
+
export interface VectorStoreConfig {
|
|
6
|
+
dbPath: string;
|
|
7
|
+
hybridWeight?: number;
|
|
8
|
+
maxDistance?: number;
|
|
9
|
+
grouping?: 'similar' | 'related';
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Manages all interactions with the LanceDB vector database.
|
|
13
|
+
* This class serves as a facade, coordinating migration, retrieval, and management components.
|
|
14
|
+
* An instance must be initialized via the `initialize()` method before use.
|
|
15
|
+
*/
|
|
16
|
+
export declare class VectorStore {
|
|
17
|
+
private readonly config;
|
|
18
|
+
private db;
|
|
19
|
+
table: Table;
|
|
20
|
+
retriever: Retriever;
|
|
21
|
+
manager: StoreManager;
|
|
22
|
+
private ftsEnabled;
|
|
23
|
+
private constructor();
|
|
24
|
+
static create(config: VectorStoreConfig): Promise<VectorStore>;
|
|
25
|
+
close(): void;
|
|
26
|
+
/**
|
|
27
|
+
* Connects to the database, ensures all necessary tables exist and are migrated,
|
|
28
|
+
* and initializes all components. This must be called before any other method.
|
|
29
|
+
*/
|
|
30
|
+
private initialize;
|
|
31
|
+
/**
|
|
32
|
+
* Inserts an array of vector chunks into the database.
|
|
33
|
+
* @param chunks - An array of vector chunks to insert.
|
|
34
|
+
*/
|
|
35
|
+
insertChunks(chunks: VectorChunk[]): Promise<void>;
|
|
36
|
+
/**
|
|
37
|
+
* Deletes all chunks associated with a given file path from the documents table.
|
|
38
|
+
* @param filePath - The absolute or synthetic path of the document to delete.
|
|
39
|
+
*/
|
|
40
|
+
deleteChunks(filePath: string): Promise<void>;
|
|
41
|
+
/**
|
|
42
|
+
* Executes a search query against the vector store.
|
|
43
|
+
*/
|
|
44
|
+
search(queryVector: number[], queryText: string, limit?: number, filters?: QueryFilters): Promise<QueryResult[]>;
|
|
45
|
+
/**
|
|
46
|
+
* Lists all items in the vector store, with optional filtering and pagination.
|
|
47
|
+
*/
|
|
48
|
+
listFiles(options?: ListOptions): Promise<ListItem[]>;
|
|
49
|
+
/**
|
|
50
|
+
* Retrieves the current status of the RAG system.
|
|
51
|
+
*/
|
|
52
|
+
getStatus(): Promise<StatusReport>;
|
|
53
|
+
/**
|
|
54
|
+
* Removes all expired text snippets from the database.
|
|
55
|
+
*/
|
|
56
|
+
cleanupExpired(): Promise<number>;
|
|
57
|
+
/**
|
|
58
|
+
* Optimizes the database table, which compacts segments and rebuilds indexes.
|
|
59
|
+
* This can improve query performance after many writes.
|
|
60
|
+
*/
|
|
61
|
+
optimize(): Promise<void>;
|
|
62
|
+
/**
|
|
63
|
+
* Retrieves the most recently updated metadata for a specific file path.
|
|
64
|
+
* This is a read-only operation and does not use the write queue.
|
|
65
|
+
* @param filePath - The full path of the document.
|
|
66
|
+
* @returns A promise that resolves to the document's metadata or undefined if not found.
|
|
67
|
+
*/
|
|
68
|
+
getLatestMetadata(filePath: string): Promise<DocumentMetadata | undefined>;
|
|
69
|
+
/**
|
|
70
|
+
* Retrieves all chunks for a specific document path.
|
|
71
|
+
*/
|
|
72
|
+
getChunksByPath(filePath: string): Promise<VectorChunk[]>;
|
|
73
|
+
/**
|
|
74
|
+
* Retrieves all persisted watched paths from the database.
|
|
75
|
+
* @returns A promise that resolves to an array of watched path configurations.
|
|
76
|
+
*/
|
|
77
|
+
getWatchedPaths(): Promise<WatchedPath[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Adds or updates a path to be watched in the persistence layer.
|
|
80
|
+
* This operation is idempotent.
|
|
81
|
+
*/
|
|
82
|
+
addWatchedPath(path: string, type: 'file' | 'folder', recursive: boolean): Promise<void>;
|
|
83
|
+
/**
|
|
84
|
+
* Removes a path from the persistence layer.
|
|
85
|
+
*/
|
|
86
|
+
removeWatchedPath(path: string): Promise<void>;
|
|
87
|
+
private _ensureWatchedPathsTable;
|
|
88
|
+
private _setupComponents;
|
|
89
|
+
private _ensureFtsIndex;
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/local-rag/vectordb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAA0B,KAAK,KAAK,EAAW,MAAM,kBAAkB,CAAA;AAI9E,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAE3C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAG1C,OAAO,KAAK,EACV,gBAAgB,EAChB,QAAQ,EACR,WAAW,EACX,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,WAAW,EACX,WAAW,EACZ,MAAM,aAAa,CAAA;AAMpB,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,MAAM,CAAA;IACd,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,SAAS,GAAG,SAAS,CAAA;CACjC;AAED;;;;GAIG;AACH,qBAAa,WAAW;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmB;IAC1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AAC;IAQrG;;OAEG;IACU,iBAAiB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;YAK7C,wBAAwB;IAYtC,OAAO,CAAC,gBAAgB;YASV,eAAe;CAqB9B"}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { Index, connect } from '@lancedb/lancedb';
|
|
2
|
+
import { logger } from '@mcp-monorepo/shared';
|
|
3
|
+
import { DatabaseError } from '../errors.js';
|
|
4
|
+
import { StoreManager } from './manager.js';
|
|
5
|
+
import { SchemaMigrator } from './migration.js';
|
|
6
|
+
import { Retriever } from './retriever.js';
|
|
7
|
+
import { DataMapper, getDocumentsSchema } from './schema.js';
|
|
8
|
+
// Table names are now internal constants, not part of the public config.
|
|
9
|
+
const DOCUMENTS_TABLE_NAME = 'documents';
|
|
10
|
+
const WATCHED_PATHS_TABLE_NAME = 'watched_paths';
|
|
11
|
+
/**
|
|
12
|
+
* Manages all interactions with the LanceDB vector database.
|
|
13
|
+
* This class serves as a facade, coordinating migration, retrieval, and management components.
|
|
14
|
+
* An instance must be initialized via the `initialize()` method before use.
|
|
15
|
+
*/
|
|
16
|
+
export class VectorStore {
|
|
17
|
+
config;
|
|
18
|
+
db;
|
|
19
|
+
table;
|
|
20
|
+
retriever;
|
|
21
|
+
manager;
|
|
22
|
+
ftsEnabled = false;
|
|
23
|
+
constructor(config, db) {
|
|
24
|
+
this.config = config;
|
|
25
|
+
this.db = db;
|
|
26
|
+
}
|
|
27
|
+
static async create(config) {
|
|
28
|
+
const db = await connect(config.dbPath);
|
|
29
|
+
const vectorStore = new VectorStore(config, db);
|
|
30
|
+
await vectorStore.initialize();
|
|
31
|
+
return vectorStore;
|
|
32
|
+
}
|
|
33
|
+
close() {
|
|
34
|
+
logger.info('VectorStore: Closing database connection.');
|
|
35
|
+
this.db.close();
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Connects to the database, ensures all necessary tables exist and are migrated,
|
|
39
|
+
* and initializes all components. This must be called before any other method.
|
|
40
|
+
*/
|
|
41
|
+
async initialize() {
|
|
42
|
+
try {
|
|
43
|
+
const migrator = new SchemaMigrator(this.db, DOCUMENTS_TABLE_NAME);
|
|
44
|
+
let documentsTable = await migrator.run();
|
|
45
|
+
// If the table doesn't exist after migration (or at all), create it with the correct schema.
|
|
46
|
+
if (!documentsTable) {
|
|
47
|
+
logger.info(`VectorStore: Creating new table "${DOCUMENTS_TABLE_NAME}" with predefined schema.`);
|
|
48
|
+
// Create the table with an empty array and the explicit schema.
|
|
49
|
+
// This forces LanceDB to use our schema instead of inferring it.
|
|
50
|
+
documentsTable = await this.db.createTable(DOCUMENTS_TABLE_NAME, [], { schema: getDocumentsSchema() });
|
|
51
|
+
}
|
|
52
|
+
this._setupComponents(documentsTable);
|
|
53
|
+
await this._ensureFtsIndex();
|
|
54
|
+
await this._ensureWatchedPathsTable();
|
|
55
|
+
logger.info(`VectorStore initialized at: ${this.config.dbPath}`);
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof Error && error.message.includes('Migration resulted in an empty table')) {
|
|
59
|
+
logger.warn('A migration resulted in an empty table, which was recreated.');
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
throw new DatabaseError('Failed to initialize VectorStore', error);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Inserts an array of vector chunks into the database.
|
|
67
|
+
* @param chunks - An array of vector chunks to insert.
|
|
68
|
+
*/
|
|
69
|
+
async insertChunks(chunks) {
|
|
70
|
+
if (chunks.length === 0)
|
|
71
|
+
return;
|
|
72
|
+
try {
|
|
73
|
+
await this.table.add(chunks);
|
|
74
|
+
}
|
|
75
|
+
catch (error) {
|
|
76
|
+
throw new DatabaseError('Failed to insert chunks into the database.', error);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Deletes all chunks associated with a given file path from the documents table.
|
|
81
|
+
* @param filePath - The absolute or synthetic path of the document to delete.
|
|
82
|
+
*/
|
|
83
|
+
async deleteChunks(filePath) {
|
|
84
|
+
try {
|
|
85
|
+
const escapedFilePath = filePath.replace(/'/g, "''");
|
|
86
|
+
await this.table.delete(`\`filePath\` = '${escapedFilePath}'`);
|
|
87
|
+
}
|
|
88
|
+
catch (error) {
|
|
89
|
+
throw new DatabaseError(`Failed to delete chunks for file: ${filePath}`, error);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Executes a search query against the vector store.
|
|
94
|
+
*/
|
|
95
|
+
async search(queryVector, queryText, limit = 10, filters) {
|
|
96
|
+
if (!this.retriever) {
|
|
97
|
+
return []; // If no table/retriever, search returns no results.
|
|
98
|
+
}
|
|
99
|
+
return this.retriever.search(queryVector, queryText, limit, filters);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Lists all items in the vector store, with optional filtering and pagination.
|
|
103
|
+
*/
|
|
104
|
+
async listFiles(options) {
|
|
105
|
+
if (!this.manager) {
|
|
106
|
+
return []; // If no table/manager, list returns no results.
|
|
107
|
+
}
|
|
108
|
+
return this.manager.listFiles(options);
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Retrieves the current status of the RAG system.
|
|
112
|
+
*/
|
|
113
|
+
async getStatus() {
|
|
114
|
+
if (!this.table) {
|
|
115
|
+
return {
|
|
116
|
+
documentCount: 0,
|
|
117
|
+
chunkCount: 0,
|
|
118
|
+
memoryUsageMb: process.memoryUsage().heapUsed / 1024 / 1024,
|
|
119
|
+
uptimeSeconds: process.uptime(),
|
|
120
|
+
ftsIndexEnabled: false,
|
|
121
|
+
searchMode: 'vector-only',
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
const chunkCount = await this.table.countRows();
|
|
125
|
+
const list = await this.listFiles({ limit: 1_000_000 }); // A pragmatic way to count unique files
|
|
126
|
+
const searchMode = this.ftsEnabled && (this.config.hybridWeight ?? 0.6) > 0 ? 'hybrid' : 'vector-only';
|
|
127
|
+
return {
|
|
128
|
+
documentCount: list.length,
|
|
129
|
+
chunkCount,
|
|
130
|
+
memoryUsageMb: process.memoryUsage().heapUsed / 1024 / 1024,
|
|
131
|
+
uptimeSeconds: process.uptime(),
|
|
132
|
+
ftsIndexEnabled: this.ftsEnabled,
|
|
133
|
+
searchMode,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Removes all expired text snippets from the database.
|
|
138
|
+
*/
|
|
139
|
+
async cleanupExpired() {
|
|
140
|
+
if (!this.manager) {
|
|
141
|
+
return 0;
|
|
142
|
+
}
|
|
143
|
+
const deletedCount = await this.manager.cleanupExpired();
|
|
144
|
+
if (deletedCount > 0) {
|
|
145
|
+
logger.info(`VectorStore: Triggering optimization after deleting ${deletedCount} documents.`);
|
|
146
|
+
await this.optimize();
|
|
147
|
+
}
|
|
148
|
+
return deletedCount;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Optimizes the database table, which compacts segments and rebuilds indexes.
|
|
152
|
+
* This can improve query performance after many writes.
|
|
153
|
+
*/
|
|
154
|
+
async optimize() {
|
|
155
|
+
if (!this.table) {
|
|
156
|
+
logger.info('VectorStore: Skipping optimization as table does not exist.');
|
|
157
|
+
return;
|
|
158
|
+
}
|
|
159
|
+
try {
|
|
160
|
+
logger.info('VectorStore: Starting database optimization...');
|
|
161
|
+
await this.table.optimize();
|
|
162
|
+
logger.info('VectorStore: Database optimization complete.');
|
|
163
|
+
}
|
|
164
|
+
catch (error) {
|
|
165
|
+
logger.error('VectorStore: Database optimization failed.', error);
|
|
166
|
+
throw new DatabaseError('Failed to optimize the database.', error);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Retrieves the most recently updated metadata for a specific file path.
|
|
171
|
+
* This is a read-only operation and does not use the write queue.
|
|
172
|
+
* @param filePath - The full path of the document.
|
|
173
|
+
* @returns A promise that resolves to the document's metadata or undefined if not found.
|
|
174
|
+
*/
|
|
175
|
+
async getLatestMetadata(filePath) {
|
|
176
|
+
if (!this.table) {
|
|
177
|
+
return undefined;
|
|
178
|
+
}
|
|
179
|
+
const records = await this.table
|
|
180
|
+
.query()
|
|
181
|
+
.select(['metadata'])
|
|
182
|
+
.where(`\`filePath\` = '${filePath.replace(/'/g, "''")}'`)
|
|
183
|
+
.toArray();
|
|
184
|
+
if (records.length === 0) {
|
|
185
|
+
return undefined;
|
|
186
|
+
}
|
|
187
|
+
records.sort((a, b) => {
|
|
188
|
+
const dateA = a.metadata?.updatedAt ?? '';
|
|
189
|
+
const dateB = b.metadata?.updatedAt ?? '';
|
|
190
|
+
return dateB.localeCompare(dateA);
|
|
191
|
+
});
|
|
192
|
+
return DataMapper.toDocumentMetadata(records[0].metadata);
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Retrieves all chunks for a specific document path.
|
|
196
|
+
*/
|
|
197
|
+
async getChunksByPath(filePath) {
|
|
198
|
+
if (!this.manager) {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
return this.manager.getChunksByPath(filePath);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Retrieves all persisted watched paths from the database.
|
|
205
|
+
* @returns A promise that resolves to an array of watched path configurations.
|
|
206
|
+
*/
|
|
207
|
+
async getWatchedPaths() {
|
|
208
|
+
const table = await this.db.openTable(WATCHED_PATHS_TABLE_NAME);
|
|
209
|
+
const records = await table.query().toArray();
|
|
210
|
+
return records.map((r) => ({
|
|
211
|
+
path: r.path,
|
|
212
|
+
type: r.type,
|
|
213
|
+
recursive: r.recursive,
|
|
214
|
+
addedAt: r.addedAt,
|
|
215
|
+
}));
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Adds or updates a path to be watched in the persistence layer.
|
|
219
|
+
* This operation is idempotent.
|
|
220
|
+
*/
|
|
221
|
+
async addWatchedPath(path, type, recursive) {
|
|
222
|
+
const table = await this.db.openTable(WATCHED_PATHS_TABLE_NAME);
|
|
223
|
+
await table.delete(`path = '${path.replace(/'/g, "''")}'`).catch(() => {
|
|
224
|
+
/* ignore if it doesn't exist */
|
|
225
|
+
});
|
|
226
|
+
await table.add([{ path, type, recursive, addedAt: new Date().toISOString() }]);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Removes a path from the persistence layer.
|
|
230
|
+
*/
|
|
231
|
+
async removeWatchedPath(path) {
|
|
232
|
+
const table = await this.db.openTable(WATCHED_PATHS_TABLE_NAME);
|
|
233
|
+
await table.delete(`path = '${path.replace(/'/g, "''")}'`);
|
|
234
|
+
}
|
|
235
|
+
async _ensureWatchedPathsTable() {
|
|
236
|
+
const tableNames = await this.db.tableNames();
|
|
237
|
+
if (!tableNames.includes(WATCHED_PATHS_TABLE_NAME)) {
|
|
238
|
+
logger.info(`Creating new '${WATCHED_PATHS_TABLE_NAME}' table for persistence.`);
|
|
239
|
+
// Create table with a dummy record to define the schema, then delete it.
|
|
240
|
+
const table = await this.db.createTable(WATCHED_PATHS_TABLE_NAME, [
|
|
241
|
+
{ path: '__dummy__', type: 'file', recursive: false, addedAt: '' },
|
|
242
|
+
]);
|
|
243
|
+
await table.delete("path = '__dummy__'");
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
_setupComponents(table) {
|
|
247
|
+
this.table = table;
|
|
248
|
+
this.retriever = new Retriever(this.table, {
|
|
249
|
+
...this.config,
|
|
250
|
+
ftsEnabled: this.ftsEnabled,
|
|
251
|
+
});
|
|
252
|
+
this.manager = new StoreManager(this.table);
|
|
253
|
+
}
|
|
254
|
+
async _ensureFtsIndex() {
|
|
255
|
+
if (!this.table)
|
|
256
|
+
return;
|
|
257
|
+
try {
|
|
258
|
+
await this.table.createIndex('text', { config: Index.fts() });
|
|
259
|
+
this.ftsEnabled = true;
|
|
260
|
+
logger.info('VectorStore: FTS index is active.');
|
|
261
|
+
}
|
|
262
|
+
catch (error) {
|
|
263
|
+
const msg = error.message;
|
|
264
|
+
if (msg.includes('already exists')) {
|
|
265
|
+
this.ftsEnabled = true;
|
|
266
|
+
logger.info('VectorStore: FTS index already exists and is active.');
|
|
267
|
+
}
|
|
268
|
+
else {
|
|
269
|
+
logger.error('VectorStore: FTS index creation failed. Hybrid search will be disabled.', error);
|
|
270
|
+
this.ftsEnabled = false;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// Update the retriever with the correct FTS status
|
|
274
|
+
if (this.retriever)
|
|
275
|
+
this.retriever.setFtsEnabled(this.ftsEnabled);
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/local-rag/vectordb/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,KAAK,EAAc,OAAO,EAAE,MAAM,kBAAkB,CAAA;AAC9E,OAAO,EAAE,MAAM,EAAE,MAAM,sBAAsB,CAAA;AAE7C,OAAO,EAAE,aAAa,EAAE,MAAM,cAAc,CAAA;AAC5C,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AAC3C,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAA;AAC/C,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAA;AAC1C,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAa5D,yEAAyE;AACzE,MAAM,oBAAoB,GAAG,WAAW,CAAA;AACxC,MAAM,wBAAwB,GAAG,eAAe,CAAA;AAShD;;;;GAIG;AACH,MAAM,OAAO,WAAW;IACL,MAAM,CAAmB;IAClC,EAAE,CAAY;IACf,KAAK,CAAQ;IACb,SAAS,CAAY;IACrB,OAAO,CAAe;IACrB,UAAU,GAAG,KAAK,CAAA;IAE1B,YAAoB,MAAyB,EAAE,EAAc;QAC3D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAA;QACpB,IAAI,CAAC,EAAE,GAAG,EAAE,CA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
|
|
4
|
+
* Handles administrative and management tasks for the vector store, like listing and cleaning documents.
|
|
5
|
+
*/
|
|
6
|
+
export declare class StoreManager {
|
|
7
|
+
private readonly table;
|
|
8
|
+
constructor(table: Table);
|
|
9
|
+
/**
|
|
10
|
+
* Lists all unique documents in the store, with optional filtering and pagination.
|
|
11
|
+
* This method first filters in the DB, then aggregates unique files in memory.
|
|
12
|
+
* @param options - Optional filters and pagination settings.
|
|
13
|
+
* @returns A promise that resolves to an array of list items.
|
|
14
|
+
*/
|
|
15
|
+
listFiles(options?: ListOptions): Promise<ListItem[]>;
|
|
16
|
+
/**
|
|
17
|
+
* Removes all documents (and their chunks) that have expired.
|
|
18
|
+
* @returns A promise that resolves to the number of documents deleted.
|
|
19
|
+
*/
|
|
20
|
+
cleanupExpired(): Promise<number>;
|
|
21
|
+
/**
|
|
22
|
+
* Retrieves all chunks associated with a specific file path.
|
|
23
|
+
* @param filePath - The full path of the document (e.g., '/path/to/file.pdf' or 'memory://label').
|
|
24
|
+
* @returns A promise that resolves to an array of vector chunks.
|
|
25
|
+
*/
|
|
26
|
+
getChunksByPath(filePath: string): Promise<VectorChunk[]>;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=manager.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"manager.d.ts","sourceRoot":"","sources":["../../../src/local-rag/vectordb/manager.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,aAAa,CAAA;AACrE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAE7C;;GAEG;AACH,qBAAa,YAAY;IACvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAO;gBAEjB,KAAK,EAAE,KAAK;IAIxB;;;;;OAKG;IACU,SAAS,CAAC,OAAO,GAAE,WAAgB,GAAG,OAAO,CAAC,QAAQ,EAAE,CAAC;IAkDtE;;;OAGG;IACU,cAAc,IAAI,OAAO,CAAC,MAAM,CAAC;IAyB9C;;;;OAIG;IACU,eAAe,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;CAOvE"}
|