claude-db 0.4.1 → 0.5.1
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/CHANGELOG.md +411 -0
- package/README.md +62 -94
- package/dist/capture/command.d.ts +0 -14
- package/dist/capture/command.js +0 -23
- package/dist/capture/flush.d.ts +1 -50
- package/dist/capture/flush.js +4 -63
- package/dist/capture/git.js +0 -20
- package/dist/capture/identity.d.ts +0 -17
- package/dist/capture/identity.js +1 -21
- package/dist/capture/index.d.ts +1 -0
- package/dist/capture/index.js +1 -0
- package/dist/capture/manual.d.ts +0 -19
- package/dist/capture/manual.js +0 -0
- package/dist/capture/progress.d.ts +4 -0
- package/dist/capture/progress.js +33 -0
- package/dist/capture/transcript.d.ts +0 -45
- package/dist/capture/transcript.js +4 -70
- package/dist/capture/turn-extractor.d.ts +0 -15
- package/dist/capture/turn-extractor.js +1 -104
- package/dist/cli/args.d.ts +4 -0
- package/dist/cli/args.js +16 -0
- package/dist/cli/commands/doctor.d.ts +1 -0
- package/dist/cli/commands/doctor.js +76 -0
- package/dist/cli/commands/graph.d.ts +2 -0
- package/dist/cli/commands/graph.js +93 -0
- package/dist/cli/commands/insight.d.ts +3 -0
- package/dist/cli/commands/insight.js +129 -0
- package/dist/cli/commands/maintain.d.ts +3 -0
- package/dist/cli/commands/maintain.js +77 -0
- package/dist/cli/commands/memory.d.ts +3 -0
- package/dist/cli/commands/memory.js +77 -0
- package/dist/cli/commands/seed.d.ts +1 -0
- package/dist/cli/commands/seed.js +43 -0
- package/dist/cli/commands/setup.d.ts +4 -0
- package/dist/cli/commands/setup.js +156 -0
- package/dist/cli/commands/sync.d.ts +1 -0
- package/dist/cli/commands/sync.js +95 -0
- package/dist/cli/commands/transfer.d.ts +9 -0
- package/dist/cli/commands/transfer.js +129 -0
- package/dist/cli/commands/use.d.ts +1 -0
- package/dist/cli/commands/use.js +90 -0
- package/dist/cli/constants.d.ts +2 -0
- package/dist/cli/constants.js +4 -0
- package/dist/cli/files.d.ts +4 -0
- package/dist/cli/files.js +33 -0
- package/dist/cli/index.js +15 -1080
- package/dist/cli/install.d.ts +1 -59
- package/dist/cli/install.js +5 -194
- package/dist/cli/instructions.d.ts +5 -0
- package/dist/cli/instructions.js +55 -0
- package/dist/cli/paths.d.ts +5 -0
- package/dist/cli/paths.js +19 -0
- package/dist/cli/refresh.d.ts +1 -0
- package/dist/cli/refresh.js +25 -0
- package/dist/cli/usage.d.ts +1 -0
- package/dist/cli/usage.js +39 -0
- package/dist/config/load.d.ts +0 -5
- package/dist/config/load.js +1 -8
- package/dist/config/schema.js +2 -58
- package/dist/context.d.ts +0 -10
- package/dist/context.js +0 -16
- package/dist/embed/builtin.d.ts +0 -12
- package/dist/embed/builtin.js +0 -20
- package/dist/embed/embedder.d.ts +0 -14
- package/dist/embed/embedder.js +0 -1
- package/dist/embed/index.d.ts +0 -9
- package/dist/embed/index.js +0 -9
- package/dist/embed/local.d.ts +0 -8
- package/dist/embed/local.js +0 -12
- package/dist/graph/index.d.ts +7 -0
- package/dist/graph/index.js +4 -0
- package/dist/graph/languages/ecmascript.d.ts +4 -0
- package/dist/graph/languages/ecmascript.js +47 -0
- package/dist/graph/languages/go.d.ts +2 -0
- package/dist/graph/languages/go.js +15 -0
- package/dist/graph/languages/index.d.ts +6 -0
- package/dist/graph/languages/index.js +20 -0
- package/dist/graph/languages/python.d.ts +2 -0
- package/dist/graph/languages/python.js +15 -0
- package/dist/graph/languages/rules.d.ts +18 -0
- package/dist/graph/languages/rules.js +1 -0
- package/dist/graph/languages/rust.d.ts +2 -0
- package/dist/graph/languages/rust.js +16 -0
- package/dist/graph/parser.d.ts +31 -0
- package/dist/graph/parser.js +29 -0
- package/dist/graph/query/format.d.ts +2 -0
- package/dist/graph/query/format.js +41 -0
- package/dist/graph/query/index.d.ts +5 -0
- package/dist/graph/query/index.js +4 -0
- package/dist/graph/query/lookup.d.ts +21 -0
- package/dist/graph/query/lookup.js +35 -0
- package/dist/graph/query/path.d.ts +2 -0
- package/dist/graph/query/path.js +48 -0
- package/dist/graph/query/refresh.d.ts +2 -0
- package/dist/graph/query/refresh.js +21 -0
- package/dist/graph/scan/extract.d.ts +15 -0
- package/dist/graph/scan/extract.js +101 -0
- package/dist/graph/scan/files.d.ts +12 -0
- package/dist/graph/scan/files.js +51 -0
- package/dist/graph/scan/index.d.ts +18 -0
- package/dist/graph/scan/index.js +44 -0
- package/dist/graph/scan/resolve.d.ts +3 -0
- package/dist/graph/scan/resolve.js +48 -0
- package/dist/hooks/payload.d.ts +0 -8
- package/dist/hooks/payload.js +0 -6
- package/dist/hooks/relevance.d.ts +0 -17
- package/dist/hooks/relevance.js +55 -38
- package/dist/hooks/session-end.js +0 -11
- package/dist/hooks/session-start.js +33 -14
- package/dist/hooks/user-prompt.js +0 -18
- package/dist/mcp/render.d.ts +0 -20
- package/dist/mcp/render.js +0 -39
- package/dist/mcp/server.js +46 -25
- package/dist/search/rank.d.ts +0 -14
- package/dist/search/rank.js +0 -16
- package/dist/search/service.d.ts +1 -32
- package/dist/search/service.js +1 -36
- package/dist/store/adapter.d.ts +11 -44
- package/dist/store/adapter.js +1 -12
- package/dist/store/index.d.ts +0 -11
- package/dist/store/index.js +0 -11
- package/dist/store/mongo/docs.d.ts +61 -0
- package/dist/store/mongo/docs.js +106 -0
- package/dist/store/mongo/driver.d.ts +1 -15
- package/dist/store/mongo/driver.js +0 -15
- package/dist/store/mongo/filters.d.ts +6 -0
- package/dist/store/mongo/filters.js +18 -0
- package/dist/store/mongo/graph.d.ts +12 -0
- package/dist/store/mongo/graph.js +61 -0
- package/dist/store/mongo/helpers.d.ts +2 -0
- package/dist/store/mongo/helpers.js +8 -0
- package/dist/store/mongo/index.d.ts +16 -13
- package/dist/store/mongo/index.js +52 -295
- package/dist/store/mongo/observations.d.ts +10 -0
- package/dist/store/mongo/observations.js +97 -0
- package/dist/store/mongo/search.d.ts +6 -0
- package/dist/store/mongo/search.js +51 -0
- package/dist/store/mongo/sessions.d.ts +6 -0
- package/dist/store/mongo/sessions.js +24 -0
- package/dist/store/mongo/vectorSearch.d.ts +5 -0
- package/dist/store/mongo/vectorSearch.js +57 -0
- package/dist/store/postgres/driver.d.ts +19 -0
- package/dist/store/postgres/driver.js +13 -0
- package/dist/store/postgres/filters.d.ts +2 -0
- package/dist/store/postgres/filters.js +22 -0
- package/dist/store/postgres/graph.d.ts +11 -0
- package/dist/store/postgres/graph.js +115 -0
- package/dist/store/postgres/index.d.ts +20 -33
- package/dist/store/postgres/index.js +43 -428
- package/dist/store/postgres/insert.d.ts +3 -0
- package/dist/store/postgres/insert.js +59 -0
- package/dist/store/postgres/meta.d.ts +10 -0
- package/dist/store/postgres/meta.js +54 -0
- package/dist/store/postgres/observations.d.ts +10 -0
- package/dist/store/postgres/observations.js +139 -0
- package/dist/store/postgres/rows.d.ts +6 -0
- package/dist/store/postgres/rows.js +74 -0
- package/dist/store/postgres/schema.d.ts +3 -0
- package/dist/store/postgres/schema.js +84 -0
- package/dist/store/postgres/search.d.ts +3 -0
- package/dist/store/postgres/search.js +20 -0
- package/dist/store/postgres/sessions.d.ts +5 -0
- package/dist/store/postgres/sessions.js +26 -0
- package/dist/store/postgres/vectorSearch.d.ts +3 -0
- package/dist/store/postgres/vectorSearch.js +21 -0
- package/dist/store/sqlite/filters.d.ts +9 -0
- package/dist/store/sqlite/filters.js +77 -0
- package/dist/store/sqlite/graph.d.ts +11 -0
- package/dist/store/sqlite/graph.js +100 -0
- package/dist/store/sqlite/index.d.ts +12 -10
- package/dist/store/sqlite/index.js +41 -379
- package/dist/store/sqlite/observations.d.ts +10 -0
- package/dist/store/sqlite/observations.js +128 -0
- package/dist/store/sqlite/rows.d.ts +8 -0
- package/dist/store/sqlite/rows.js +84 -0
- package/dist/store/sqlite/schema.sql +42 -2
- package/dist/store/sqlite/search.d.ts +5 -0
- package/dist/store/sqlite/search.js +86 -0
- package/dist/store/sqlite/sessions.d.ts +5 -0
- package/dist/store/sqlite/sessions.js +21 -0
- package/dist/types.d.ts +47 -40
- package/dist/types.js +0 -4
- package/dist/update.d.ts +0 -20
- package/dist/update.js +4 -29
- package/dist/usages/find.d.ts +2 -8
- package/dist/usages/find.js +51 -54
- package/dist/usages/index.d.ts +1 -1
- package/dist/usages/index.js +1 -1
- package/dist/util/project.d.ts +0 -12
- package/dist/util/project.js +0 -24
- package/dist/util/scope.d.ts +0 -9
- package/dist/util/scope.js +0 -9
- package/dist/util/shortid.d.ts +0 -5
- package/dist/util/shortid.js +1 -19
- package/dist/util/snippet.d.ts +0 -26
- package/dist/util/snippet.js +0 -30
- package/dist/util/vector.d.ts +0 -12
- package/dist/util/vector.js +0 -12
- package/dist/util/warnings.d.ts +0 -11
- package/dist/util/warnings.js +0 -11
- package/docs/how-it-works.md +183 -0
- package/package.json +14 -3
- package/skills/cdb-scan/SKILL.md +31 -6
|
@@ -1,13 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structural types for the slice of the MongoDB driver this adapter uses.
|
|
3
|
-
*
|
|
4
|
-
* Declared locally rather than imported from `mongodb` so the project compiles
|
|
5
|
-
* with zero optional dependencies installed. A user who only ever uses SQLite
|
|
6
|
-
* should not need a Mongo driver on disk to run `npm run build`.
|
|
7
|
-
*
|
|
8
|
-
* These describe real driver behaviour, so if the shapes ever drift the
|
|
9
|
-
* adapter fails at runtime with a clear error rather than silently misbehaving.
|
|
10
|
-
*/
|
|
11
1
|
export type Doc = Record<string, unknown>;
|
|
12
2
|
export interface FindCursor<T> {
|
|
13
3
|
sort(spec: Doc): FindCursor<T>;
|
|
@@ -20,6 +10,7 @@ export interface Collection<T> {
|
|
|
20
10
|
findOne(filter: Doc, options?: Doc): Promise<T | null>;
|
|
21
11
|
find(filter: Doc, options?: Doc): FindCursor<T>;
|
|
22
12
|
updateOne(filter: Doc, update: Doc, options?: Doc): Promise<unknown>;
|
|
13
|
+
updateMany(filter: Doc, update: Doc, options?: Doc): Promise<unknown>;
|
|
23
14
|
deleteMany(filter: Doc): Promise<unknown>;
|
|
24
15
|
bulkWrite(operations: Doc[], options?: Doc): Promise<unknown>;
|
|
25
16
|
aggregate<R>(pipeline: Doc[]): FindCursor<R>;
|
|
@@ -37,9 +28,4 @@ export interface MongoClient {
|
|
|
37
28
|
export interface MongoModule {
|
|
38
29
|
MongoClient: new (uri: string) => MongoClient;
|
|
39
30
|
}
|
|
40
|
-
/**
|
|
41
|
-
* Loads the driver at runtime. The specifier is widened to `string` so
|
|
42
|
-
* TypeScript does not try to resolve `mongodb` at build time, which is the
|
|
43
|
-
* whole point of keeping it optional.
|
|
44
|
-
*/
|
|
45
31
|
export declare function importMongo(): Promise<MongoModule>;
|
|
@@ -1,18 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Structural types for the slice of the MongoDB driver this adapter uses.
|
|
3
|
-
*
|
|
4
|
-
* Declared locally rather than imported from `mongodb` so the project compiles
|
|
5
|
-
* with zero optional dependencies installed. A user who only ever uses SQLite
|
|
6
|
-
* should not need a Mongo driver on disk to run `npm run build`.
|
|
7
|
-
*
|
|
8
|
-
* These describe real driver behaviour, so if the shapes ever drift the
|
|
9
|
-
* adapter fails at runtime with a clear error rather than silently misbehaving.
|
|
10
|
-
*/
|
|
11
|
-
/**
|
|
12
|
-
* Loads the driver at runtime. The specifier is widened to `string` so
|
|
13
|
-
* TypeScript does not try to resolve `mongodb` at build time, which is the
|
|
14
|
-
* whole point of keeping it optional.
|
|
15
|
-
*/
|
|
16
1
|
export async function importMongo() {
|
|
17
2
|
try {
|
|
18
3
|
const mod = (await import('mongodb'));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function scopeFilter(query) {
|
|
2
|
+
const filter = {};
|
|
3
|
+
if (query.project)
|
|
4
|
+
filter['project'] = query.project;
|
|
5
|
+
if (query.kind)
|
|
6
|
+
filter['kind'] = query.kind;
|
|
7
|
+
if (query.tag)
|
|
8
|
+
filter['tags'] = query.tag;
|
|
9
|
+
if (query.since !== undefined || query.until !== undefined) {
|
|
10
|
+
const range = {};
|
|
11
|
+
if (query.since !== undefined)
|
|
12
|
+
range['$gte'] = query.since;
|
|
13
|
+
if (query.until !== undefined)
|
|
14
|
+
range['$lte'] = query.until;
|
|
15
|
+
filter['createdAt'] = range;
|
|
16
|
+
}
|
|
17
|
+
return filter;
|
|
18
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Collection } from './driver.js';
|
|
2
|
+
import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
|
|
3
|
+
import type { EdgeDoc, ScannedFileDoc, SymbolDoc } from './docs.js';
|
|
4
|
+
export declare function upsertGraph(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, scan: {
|
|
5
|
+
symbols: CodeSymbol[];
|
|
6
|
+
edges: CodeEdge[];
|
|
7
|
+
files: ScannedFile[];
|
|
8
|
+
}): Promise<void>;
|
|
9
|
+
export declare function findSymbols(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
10
|
+
export declare function findEdges(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
11
|
+
export declare function scannedFiles(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, project: string): Promise<ScannedFile[]>;
|
|
12
|
+
export declare function removeGraph(symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, project: string, files?: string[]): Promise<number>;
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { toEdge, toSymbol, upsertsOf } from './docs.js';
|
|
2
|
+
export async function upsertGraph(symbols, edges, scanned, scan) {
|
|
3
|
+
if (scan.symbols.length > 0) {
|
|
4
|
+
await symbols.bulkWrite(upsertsOf(scan.symbols, (s) => s.id), { ordered: false });
|
|
5
|
+
}
|
|
6
|
+
if (scan.edges.length > 0) {
|
|
7
|
+
await edges.bulkWrite(upsertsOf(scan.edges, (e) => e.id), { ordered: false });
|
|
8
|
+
}
|
|
9
|
+
if (scan.files.length > 0) {
|
|
10
|
+
await scanned.bulkWrite(upsertsOf(scan.files, (f) => `${f.project} ${f.path}`), { ordered: false });
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export async function findSymbols(symbols, edges, scanned, filter) {
|
|
14
|
+
const query = { project: filter.project };
|
|
15
|
+
if (filter.name)
|
|
16
|
+
query['name'] = filter.name;
|
|
17
|
+
if (filter.file)
|
|
18
|
+
query['file'] = filter.file;
|
|
19
|
+
const docs = await symbols
|
|
20
|
+
.find(query)
|
|
21
|
+
.sort({ file: 1, line: 1 })
|
|
22
|
+
.limit(filter.limit ?? 500)
|
|
23
|
+
.toArray();
|
|
24
|
+
return docs.map(toSymbol);
|
|
25
|
+
}
|
|
26
|
+
export async function findEdges(symbols, edges, scanned, filter) {
|
|
27
|
+
const query = { project: filter.project };
|
|
28
|
+
const alternatives = [];
|
|
29
|
+
if (filter.srcIds && filter.srcIds.length > 0) {
|
|
30
|
+
alternatives.push({ srcId: { $in: filter.srcIds } });
|
|
31
|
+
}
|
|
32
|
+
if (filter.dstIds && filter.dstIds.length > 0) {
|
|
33
|
+
alternatives.push({ dstId: { $in: filter.dstIds } });
|
|
34
|
+
}
|
|
35
|
+
if (alternatives.length > 0)
|
|
36
|
+
query['$or'] = alternatives;
|
|
37
|
+
const docs = await edges
|
|
38
|
+
.find(query)
|
|
39
|
+
.limit(filter.limit ?? 2000)
|
|
40
|
+
.toArray();
|
|
41
|
+
return docs.map(toEdge);
|
|
42
|
+
}
|
|
43
|
+
export async function scannedFiles(symbols, edges, scanned, project) {
|
|
44
|
+
const docs = await scanned.find({ project }).toArray();
|
|
45
|
+
return docs.map((doc) => ({
|
|
46
|
+
project: doc.project,
|
|
47
|
+
path: doc.path,
|
|
48
|
+
hash: doc.hash,
|
|
49
|
+
scannedAt: doc.scannedAt,
|
|
50
|
+
}));
|
|
51
|
+
}
|
|
52
|
+
export async function removeGraph(symbols, edges, scanned, project, files) {
|
|
53
|
+
const scoped = files !== undefined && files.length > 0;
|
|
54
|
+
const symbolQuery = scoped ? { project, file: { $in: files } } : { project };
|
|
55
|
+
const cacheQuery = scoped ? { project, path: { $in: files } } : { project };
|
|
56
|
+
const count = await symbols.countDocuments(symbolQuery);
|
|
57
|
+
await symbols.deleteMany(symbolQuery);
|
|
58
|
+
await edges.deleteMany(symbolQuery);
|
|
59
|
+
await scanned.deleteMany(cacheQuery);
|
|
60
|
+
return count;
|
|
61
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export function escapeRegex(text) {
|
|
2
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
3
|
+
}
|
|
4
|
+
export function databaseNameFrom(uri) {
|
|
5
|
+
const match = /\/\/[^/]+\/([^?]+)/.exec(uri);
|
|
6
|
+
const name = match?.[1]?.trim();
|
|
7
|
+
return name && name.length > 0 ? name : null;
|
|
8
|
+
}
|
|
@@ -1,21 +1,15 @@
|
|
|
1
|
+
import type { CodeEdge, CodeSymbol, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
|
|
1
2
|
import type { MemoryStore, ProjectSummary } from '../adapter.js';
|
|
2
|
-
import type { ListFilter, Observation, ObservationIndexEntry, RemoveFilter, SearchQuery, Session, TimelineQuery } from '../../types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Works against any MongoDB: Atlas, self-hosted, or a local container.
|
|
5
|
-
*
|
|
6
|
-
* Keyword search uses a compound text index, available in every deployment.
|
|
7
|
-
* Vector search prefers an Atlas `$vectorSearch` index when one exists and
|
|
8
|
-
* otherwise falls back to scoring candidates in process, so a plain
|
|
9
|
-
* `mongodb://localhost` connection string still gets semantic recall.
|
|
10
|
-
*/
|
|
11
3
|
export declare class MongoStore implements MemoryStore {
|
|
12
4
|
private readonly client;
|
|
13
5
|
private readonly db;
|
|
14
6
|
private readonly sessions;
|
|
15
7
|
private readonly observations;
|
|
8
|
+
private readonly symbols;
|
|
9
|
+
private readonly edges;
|
|
10
|
+
private readonly scanned;
|
|
16
11
|
readonly kind = "mongodb";
|
|
17
|
-
|
|
18
|
-
private atlasVectorIndex;
|
|
12
|
+
private readonly cache;
|
|
19
13
|
private constructor();
|
|
20
14
|
static create(uri: string): Promise<MongoStore>;
|
|
21
15
|
init(): Promise<void>;
|
|
@@ -32,7 +26,16 @@ export declare class MongoStore implements MemoryStore {
|
|
|
32
26
|
inventory(): Promise<string[]>;
|
|
33
27
|
searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
34
28
|
searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
29
|
+
closeObservations(ids: string[]): Promise<number>;
|
|
35
30
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
hasAtlasVectorIndex(): Promise<boolean>;
|
|
32
|
+
upsertGraph(scan: {
|
|
33
|
+
symbols: CodeSymbol[];
|
|
34
|
+
edges: CodeEdge[];
|
|
35
|
+
files: ScannedFile[];
|
|
36
|
+
}): Promise<void>;
|
|
37
|
+
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
38
|
+
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
39
|
+
scannedFiles(project: string): Promise<ScannedFile[]>;
|
|
40
|
+
removeGraph(project: string, files?: string[]): Promise<number>;
|
|
38
41
|
}
|
|
@@ -1,43 +1,48 @@
|
|
|
1
|
+
import * as vectorSearchOps from './vectorSearch.js';
|
|
2
|
+
import * as searchOps from './search.js';
|
|
3
|
+
import * as observationsOps from './observations.js';
|
|
4
|
+
import * as sessionsOps from './sessions.js';
|
|
5
|
+
import * as graphOps from './graph.js';
|
|
6
|
+
import { databaseNameFrom } from './helpers.js';
|
|
7
|
+
import { foreignNames } from '../adapter.js';
|
|
1
8
|
import { importMongo } from './driver.js';
|
|
2
|
-
import { foreignNames, isWholeScope } from '../adapter.js';
|
|
3
|
-
import { cosine } from '../../util/vector.js';
|
|
4
|
-
import { partitionIds } from '../../util/shortid.js';
|
|
5
|
-
import { toSnippet } from '../../util/snippet.js';
|
|
6
|
-
/**
|
|
7
|
-
* Works against any MongoDB: Atlas, self-hosted, or a local container.
|
|
8
|
-
*
|
|
9
|
-
* Keyword search uses a compound text index, available in every deployment.
|
|
10
|
-
* Vector search prefers an Atlas `$vectorSearch` index when one exists and
|
|
11
|
-
* otherwise falls back to scoring candidates in process, so a plain
|
|
12
|
-
* `mongodb://localhost` connection string still gets semantic recall.
|
|
13
|
-
*/
|
|
14
9
|
export class MongoStore {
|
|
15
10
|
client;
|
|
16
11
|
db;
|
|
17
12
|
sessions;
|
|
18
13
|
observations;
|
|
14
|
+
symbols;
|
|
15
|
+
edges;
|
|
16
|
+
scanned;
|
|
19
17
|
kind = 'mongodb';
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
constructor(client, db, sessions, observations) {
|
|
18
|
+
cache = { atlasVectorIndex: null };
|
|
19
|
+
constructor(client, db, sessions, observations, symbols, edges, scanned) {
|
|
23
20
|
this.client = client;
|
|
24
21
|
this.db = db;
|
|
25
22
|
this.sessions = sessions;
|
|
26
23
|
this.observations = observations;
|
|
24
|
+
this.symbols = symbols;
|
|
25
|
+
this.edges = edges;
|
|
26
|
+
this.scanned = scanned;
|
|
27
27
|
}
|
|
28
28
|
static async create(uri) {
|
|
29
29
|
const mongo = await importMongo();
|
|
30
30
|
const client = new mongo.MongoClient(uri);
|
|
31
31
|
await client.connect();
|
|
32
|
-
// Honour a database name in the URI, fall back to a sensible default.
|
|
33
32
|
const db = client.db(databaseNameFrom(uri) ?? 'claude_memory_db');
|
|
34
|
-
return new MongoStore(client, db, db.collection('sessions'), db.collection('observations'));
|
|
33
|
+
return new MongoStore(client, db, db.collection('sessions'), db.collection('observations'), db.collection('symbols'), db.collection('symbol_edges'), db.collection('scanned_files'));
|
|
35
34
|
}
|
|
36
35
|
async init() {
|
|
37
36
|
await this.sessions.createIndex({ project: 1, startedAt: -1 });
|
|
38
37
|
await this.observations.createIndex({ project: 1, createdAt: -1 });
|
|
39
38
|
await this.observations.createIndex({ project: 1, kind: 1, createdAt: -1 });
|
|
40
39
|
await this.observations.createIndex({ title: 'text', body: 'text', tags: 'text' }, { name: 'memory_text', weights: { title: 10, tags: 5, body: 1 } });
|
|
40
|
+
await this.symbols.createIndex({ project: 1, name: 1 });
|
|
41
|
+
await this.symbols.createIndex({ project: 1, file: 1 });
|
|
42
|
+
await this.edges.createIndex({ project: 1, srcId: 1 });
|
|
43
|
+
await this.edges.createIndex({ project: 1, dstId: 1 });
|
|
44
|
+
await this.edges.createIndex({ project: 1, file: 1 });
|
|
45
|
+
await this.scanned.createIndex({ project: 1, path: 1 }, { unique: true });
|
|
41
46
|
}
|
|
42
47
|
async close() {
|
|
43
48
|
await this.client.close();
|
|
@@ -47,310 +52,62 @@ export class MongoStore {
|
|
|
47
52
|
return res['ok'] === 1;
|
|
48
53
|
}
|
|
49
54
|
async upsertSession(session) {
|
|
50
|
-
|
|
51
|
-
project: session.project,
|
|
52
|
-
startedAt: session.startedAt,
|
|
53
|
-
};
|
|
54
|
-
if (session.endedAt !== undefined)
|
|
55
|
-
set.endedAt = session.endedAt;
|
|
56
|
-
if (session.summary !== undefined)
|
|
57
|
-
set.summary = session.summary;
|
|
58
|
-
await this.sessions.updateOne({ _id: session.id }, { $set: set }, { upsert: true });
|
|
55
|
+
return sessionsOps.upsertSession(this.sessions, session);
|
|
59
56
|
}
|
|
60
57
|
async getSession(id) {
|
|
61
|
-
|
|
62
|
-
return doc ? toSession(doc) : null;
|
|
58
|
+
return sessionsOps.getSession(this.sessions, id);
|
|
63
59
|
}
|
|
64
60
|
async recentSessions(project, limit) {
|
|
65
|
-
|
|
66
|
-
.find({ project, summary: { $type: 'string' } })
|
|
67
|
-
.sort({ startedAt: -1 })
|
|
68
|
-
.limit(limit)
|
|
69
|
-
.toArray();
|
|
70
|
-
return docs.map(toSession);
|
|
61
|
+
return sessionsOps.recentSessions(this.sessions, project, limit);
|
|
71
62
|
}
|
|
72
63
|
async insertObservations(observations) {
|
|
73
|
-
|
|
74
|
-
return;
|
|
75
|
-
await this.observations.bulkWrite(observations.map((obs) => ({
|
|
76
|
-
replaceOne: {
|
|
77
|
-
filter: { _id: obs.id },
|
|
78
|
-
replacement: toDoc(obs),
|
|
79
|
-
upsert: true,
|
|
80
|
-
},
|
|
81
|
-
})), { ordered: false });
|
|
64
|
+
return observationsOps.insertObservations(this.observations, this.sessions, this.symbols, this.edges, this.scanned, observations);
|
|
82
65
|
}
|
|
83
66
|
async getObservations(ids) {
|
|
84
|
-
|
|
85
|
-
return [];
|
|
86
|
-
const { exact, prefixes } = partitionIds(ids);
|
|
87
|
-
const or = [];
|
|
88
|
-
if (exact.length > 0)
|
|
89
|
-
or.push({ _id: { $in: exact } });
|
|
90
|
-
for (const prefix of prefixes) {
|
|
91
|
-
// Anchored regex on _id is index-eligible in MongoDB.
|
|
92
|
-
or.push({ _id: { $regex: `^${escapeRegex(prefix)}` } });
|
|
93
|
-
}
|
|
94
|
-
const docs = await this.observations
|
|
95
|
-
.find({ $or: or })
|
|
96
|
-
.toArray();
|
|
97
|
-
return docs.map(toObservation);
|
|
67
|
+
return observationsOps.getObservations(this.observations, this.sessions, this.symbols, this.edges, this.scanned, ids);
|
|
98
68
|
}
|
|
99
69
|
async remove(filter) {
|
|
100
|
-
|
|
101
|
-
return 0;
|
|
102
|
-
const query = {};
|
|
103
|
-
if (filter.project)
|
|
104
|
-
query['project'] = filter.project;
|
|
105
|
-
if (filter.kind)
|
|
106
|
-
query['kind'] = filter.kind;
|
|
107
|
-
if (filter.before !== undefined)
|
|
108
|
-
query['createdAt'] = { $lt: filter.before };
|
|
109
|
-
if (filter.ids) {
|
|
110
|
-
const { exact, prefixes } = partitionIds(filter.ids);
|
|
111
|
-
const alternatives = [];
|
|
112
|
-
if (exact.length > 0)
|
|
113
|
-
alternatives.push({ _id: { $in: exact } });
|
|
114
|
-
for (const prefix of prefixes) {
|
|
115
|
-
alternatives.push({ _id: { $regex: `^${escapeRegex(prefix)}` } });
|
|
116
|
-
}
|
|
117
|
-
query['$or'] = alternatives;
|
|
118
|
-
}
|
|
119
|
-
const count = await this.observations.countDocuments(query);
|
|
120
|
-
await this.observations.deleteMany(query);
|
|
121
|
-
if (isWholeScope(filter)) {
|
|
122
|
-
await this.sessions.deleteMany(filter.project ? { project: filter.project } : {});
|
|
123
|
-
}
|
|
124
|
-
return count;
|
|
70
|
+
return observationsOps.remove(this.observations, this.sessions, this.symbols, this.edges, this.scanned, filter);
|
|
125
71
|
}
|
|
126
72
|
async list(filter) {
|
|
127
|
-
|
|
128
|
-
if (filter.project)
|
|
129
|
-
query['project'] = filter.project;
|
|
130
|
-
if (filter.kind)
|
|
131
|
-
query['kind'] = filter.kind;
|
|
132
|
-
if (filter.after !== undefined)
|
|
133
|
-
query['createdAt'] = { $gt: filter.after };
|
|
134
|
-
const docs = await this.observations
|
|
135
|
-
.find(query)
|
|
136
|
-
.sort({ createdAt: 1 })
|
|
137
|
-
.limit(filter.limit ?? 1000)
|
|
138
|
-
.toArray();
|
|
139
|
-
return docs.map(toObservation);
|
|
73
|
+
return observationsOps.list(this.observations, this.sessions, this.symbols, this.edges, this.scanned, filter);
|
|
140
74
|
}
|
|
141
75
|
async listProjects() {
|
|
142
|
-
|
|
143
|
-
.aggregate([
|
|
144
|
-
{ $group: { _id: '$project', n: { $sum: 1 }, last: { $max: '$createdAt' } } },
|
|
145
|
-
{ $sort: { last: -1 } },
|
|
146
|
-
])
|
|
147
|
-
.toArray();
|
|
148
|
-
return rows.map((row) => ({
|
|
149
|
-
project: row._id,
|
|
150
|
-
observations: row.n,
|
|
151
|
-
lastActive: row.last,
|
|
152
|
-
}));
|
|
76
|
+
return observationsOps.listProjects(this.observations, this.sessions, this.symbols, this.edges, this.scanned);
|
|
153
77
|
}
|
|
154
78
|
async inventory() {
|
|
155
|
-
// Through `command` rather than `listCollections`, so the structural driver
|
|
156
|
-
// types this adapter compiles against stay as small as they are.
|
|
157
79
|
const res = await this.db.command({ listCollections: 1, nameOnly: true });
|
|
158
80
|
const batch = res['cursor']?.firstBatch ?? [];
|
|
159
81
|
return foreignNames(batch.map((doc) => String(doc['name'] ?? '')));
|
|
160
82
|
}
|
|
161
83
|
async searchKeyword(query) {
|
|
162
|
-
|
|
163
|
-
$text: { $search: query.text },
|
|
164
|
-
...this.scopeFilter(query),
|
|
165
|
-
};
|
|
166
|
-
// $text has no fragment operator, so unlike FTS5 and ts_headline this is
|
|
167
|
-
// the head of the body rather than the text around the match. Still enough
|
|
168
|
-
// to tell two same-titled rows apart, and it costs one projected field
|
|
169
|
-
// instead of shipping whole bodies to slice them here.
|
|
170
|
-
//
|
|
171
|
-
// Inclusion throughout: MongoDB rejects a projection that mixes included
|
|
172
|
-
// and excluded fields, so the previous `body: 0, embedding: 0` form cannot
|
|
173
|
-
// carry a computed one.
|
|
174
|
-
const docs = await this.observations
|
|
175
|
-
.find(filter, {
|
|
176
|
-
projection: {
|
|
177
|
-
kind: 1,
|
|
178
|
-
title: 1,
|
|
179
|
-
project: 1,
|
|
180
|
-
createdAt: 1,
|
|
181
|
-
snippet: { $substrCP: ['$body', 0, 240] },
|
|
182
|
-
score: { $meta: 'textScore' },
|
|
183
|
-
},
|
|
184
|
-
})
|
|
185
|
-
.sort({ score: { $meta: 'textScore' } })
|
|
186
|
-
.limit(query.limit)
|
|
187
|
-
.toArray();
|
|
188
|
-
return docs.map((doc) => toIndexEntry(doc, doc['score'] ?? 0));
|
|
84
|
+
return searchOps.searchKeyword(this.observations, query);
|
|
189
85
|
}
|
|
190
86
|
async searchVector(vector, query) {
|
|
191
|
-
|
|
192
|
-
this.atlasVectorIndex = await this.hasAtlasVectorIndex();
|
|
193
|
-
}
|
|
194
|
-
if (this.atlasVectorIndex) {
|
|
195
|
-
const docs = await this.observations
|
|
196
|
-
.aggregate([
|
|
197
|
-
{
|
|
198
|
-
$vectorSearch: {
|
|
199
|
-
index: 'memory_vector',
|
|
200
|
-
path: 'embedding',
|
|
201
|
-
queryVector: vector,
|
|
202
|
-
numCandidates: Math.max(query.limit * 10, 100),
|
|
203
|
-
limit: query.limit,
|
|
204
|
-
filter: this.scopeFilter(query),
|
|
205
|
-
},
|
|
206
|
-
},
|
|
207
|
-
{
|
|
208
|
-
$project: {
|
|
209
|
-
kind: 1, title: 1, project: 1, createdAt: 1,
|
|
210
|
-
score: { $meta: 'vectorSearchScore' },
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
])
|
|
214
|
-
.toArray();
|
|
215
|
-
return docs.map((doc) => toIndexEntry(doc, doc.score));
|
|
216
|
-
}
|
|
217
|
-
// Portable path: score candidates in process. Bounded by the scope filter,
|
|
218
|
-
// which in practice narrows to a single project.
|
|
219
|
-
const docs = await this.observations
|
|
220
|
-
.find({
|
|
221
|
-
embedding: { $exists: true },
|
|
222
|
-
...(query.embedder
|
|
223
|
-
? { $or: [{ embedder: query.embedder }, { embedder: { $exists: false } }] }
|
|
224
|
-
: {}),
|
|
225
|
-
...this.scopeFilter(query),
|
|
226
|
-
}, { projection: { kind: 1, title: 1, project: 1, createdAt: 1, embedding: 1 } })
|
|
227
|
-
.sort({ createdAt: -1 })
|
|
228
|
-
.limit(query.maxScanCandidates ?? 25000)
|
|
229
|
-
.toArray();
|
|
230
|
-
return docs
|
|
231
|
-
.map((doc) => toIndexEntry(doc, doc.embedding ? cosine(vector, doc.embedding) : 0))
|
|
232
|
-
.sort((a, b) => b.score - a.score)
|
|
233
|
-
.slice(0, query.limit);
|
|
87
|
+
return vectorSearchOps.searchVector(this.observations, this.cache, vector, query);
|
|
234
88
|
}
|
|
235
|
-
async
|
|
236
|
-
|
|
237
|
-
if (!anchor)
|
|
238
|
-
return [];
|
|
239
|
-
const projection = { kind: 1, title: 1, project: 1, createdAt: 1 };
|
|
240
|
-
const [before, after] = await Promise.all([
|
|
241
|
-
this.observations
|
|
242
|
-
.find({ project: anchor.project, createdAt: { $lte: anchor.createdAt } }, { projection })
|
|
243
|
-
.sort({ createdAt: -1 })
|
|
244
|
-
.limit(query.before + 1)
|
|
245
|
-
.toArray(),
|
|
246
|
-
this.observations
|
|
247
|
-
.find({ project: anchor.project, createdAt: { $gt: anchor.createdAt } }, { projection })
|
|
248
|
-
.sort({ createdAt: 1 })
|
|
249
|
-
.limit(query.after)
|
|
250
|
-
.toArray(),
|
|
251
|
-
]);
|
|
252
|
-
return [...before.reverse(), ...after].map((doc) => toIndexEntry(doc, 0));
|
|
89
|
+
async closeObservations(ids) {
|
|
90
|
+
return observationsOps.closeObservations(this.observations, ids);
|
|
253
91
|
}
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
if (query.project)
|
|
257
|
-
filter['project'] = query.project;
|
|
258
|
-
if (query.kind)
|
|
259
|
-
filter['kind'] = query.kind;
|
|
260
|
-
if (query.tag)
|
|
261
|
-
filter['tags'] = query.tag;
|
|
262
|
-
if (query.since !== undefined || query.until !== undefined) {
|
|
263
|
-
const range = {};
|
|
264
|
-
if (query.since !== undefined)
|
|
265
|
-
range['$gte'] = query.since;
|
|
266
|
-
if (query.until !== undefined)
|
|
267
|
-
range['$lte'] = query.until;
|
|
268
|
-
filter['createdAt'] = range;
|
|
269
|
-
}
|
|
270
|
-
return filter;
|
|
92
|
+
async timeline(query) {
|
|
93
|
+
return searchOps.timeline(this.observations, query);
|
|
271
94
|
}
|
|
272
95
|
async hasAtlasVectorIndex() {
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
96
|
+
return searchOps.hasAtlasVectorIndex(this.observations);
|
|
97
|
+
}
|
|
98
|
+
async upsertGraph(scan) {
|
|
99
|
+
return graphOps.upsertGraph(this.symbols, this.edges, this.scanned, scan);
|
|
100
|
+
}
|
|
101
|
+
async findSymbols(filter) {
|
|
102
|
+
return graphOps.findSymbols(this.symbols, this.edges, this.scanned, filter);
|
|
103
|
+
}
|
|
104
|
+
async findEdges(filter) {
|
|
105
|
+
return graphOps.findEdges(this.symbols, this.edges, this.scanned, filter);
|
|
106
|
+
}
|
|
107
|
+
async scannedFiles(project) {
|
|
108
|
+
return graphOps.scannedFiles(this.symbols, this.edges, this.scanned, project);
|
|
109
|
+
}
|
|
110
|
+
async removeGraph(project, files) {
|
|
111
|
+
return graphOps.removeGraph(this.symbols, this.edges, this.scanned, project, files);
|
|
281
112
|
}
|
|
282
|
-
}
|
|
283
|
-
function escapeRegex(text) {
|
|
284
|
-
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
285
|
-
}
|
|
286
|
-
function databaseNameFrom(uri) {
|
|
287
|
-
const match = /\/\/[^/]+\/([^?]+)/.exec(uri);
|
|
288
|
-
const name = match?.[1]?.trim();
|
|
289
|
-
return name && name.length > 0 ? name : null;
|
|
290
|
-
}
|
|
291
|
-
function toSession(doc) {
|
|
292
|
-
const session = {
|
|
293
|
-
id: doc._id,
|
|
294
|
-
project: doc.project,
|
|
295
|
-
startedAt: doc.startedAt,
|
|
296
|
-
};
|
|
297
|
-
if (doc.endedAt !== undefined)
|
|
298
|
-
session.endedAt = doc.endedAt;
|
|
299
|
-
if (doc.summary !== undefined)
|
|
300
|
-
session.summary = doc.summary;
|
|
301
|
-
return session;
|
|
302
|
-
}
|
|
303
|
-
function toDoc(obs) {
|
|
304
|
-
const doc = {
|
|
305
|
-
_id: obs.id,
|
|
306
|
-
sessionId: obs.sessionId,
|
|
307
|
-
project: obs.project,
|
|
308
|
-
kind: obs.kind,
|
|
309
|
-
title: obs.title,
|
|
310
|
-
body: obs.body,
|
|
311
|
-
files: obs.files,
|
|
312
|
-
tags: obs.tags,
|
|
313
|
-
createdAt: obs.createdAt,
|
|
314
|
-
};
|
|
315
|
-
if (obs.embedding)
|
|
316
|
-
doc.embedding = obs.embedding;
|
|
317
|
-
if (obs.embedder)
|
|
318
|
-
doc.embedder = obs.embedder;
|
|
319
|
-
if (obs.author)
|
|
320
|
-
doc.author = obs.author;
|
|
321
|
-
return doc;
|
|
322
|
-
}
|
|
323
|
-
function toObservation(doc) {
|
|
324
|
-
const obs = {
|
|
325
|
-
id: doc._id,
|
|
326
|
-
sessionId: doc.sessionId,
|
|
327
|
-
project: doc.project,
|
|
328
|
-
kind: doc.kind,
|
|
329
|
-
title: doc.title,
|
|
330
|
-
body: doc.body,
|
|
331
|
-
files: doc.files ?? [],
|
|
332
|
-
tags: doc.tags ?? [],
|
|
333
|
-
createdAt: doc.createdAt,
|
|
334
|
-
};
|
|
335
|
-
if (doc.embedding)
|
|
336
|
-
obs.embedding = doc.embedding;
|
|
337
|
-
if (doc.embedder)
|
|
338
|
-
obs.embedder = doc.embedder;
|
|
339
|
-
if (doc.author)
|
|
340
|
-
obs.author = doc.author;
|
|
341
|
-
return obs;
|
|
342
|
-
}
|
|
343
|
-
function toIndexEntry(doc, score) {
|
|
344
|
-
const entry = {
|
|
345
|
-
id: doc._id,
|
|
346
|
-
kind: doc.kind,
|
|
347
|
-
title: doc.title,
|
|
348
|
-
project: doc.project,
|
|
349
|
-
createdAt: doc.createdAt,
|
|
350
|
-
score,
|
|
351
|
-
};
|
|
352
|
-
const snippet = toSnippet(doc.snippet);
|
|
353
|
-
if (snippet)
|
|
354
|
-
entry.snippet = snippet;
|
|
355
|
-
return entry;
|
|
356
113
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Collection } from './driver.js';
|
|
2
|
+
import type { ListFilter, Observation, RemoveFilter } from '../../types.js';
|
|
3
|
+
import type { ProjectSummary } from '../adapter.js';
|
|
4
|
+
import type { EdgeDoc, ObservationDoc, ScannedFileDoc, SessionDoc, SymbolDoc } from './docs.js';
|
|
5
|
+
export declare function insertObservations(collection: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, rows: Observation[]): Promise<void>;
|
|
6
|
+
export declare function getObservations(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, ids: string[]): Promise<Observation[]>;
|
|
7
|
+
export declare function remove(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: RemoveFilter): Promise<number>;
|
|
8
|
+
export declare function list(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>, filter: ListFilter): Promise<Observation[]>;
|
|
9
|
+
export declare function listProjects(observations: Collection<ObservationDoc>, sessions: Collection<SessionDoc>, symbols: Collection<SymbolDoc>, edges: Collection<EdgeDoc>, scanned: Collection<ScannedFileDoc>): Promise<ProjectSummary[]>;
|
|
10
|
+
export declare function closeObservations(observations: Collection<ObservationDoc>, ids: string[]): Promise<number>;
|