claude-db 0.4.1 → 0.5.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/CHANGELOG.md +399 -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
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { isWholeScope } from '../adapter.js';
|
|
2
|
+
import { toDoc, toObservation } from './docs.js';
|
|
3
|
+
import { escapeRegex } from './helpers.js';
|
|
4
|
+
import { partitionIds } from '../../util/shortid.js';
|
|
5
|
+
export async function insertObservations(collection, sessions, symbols, edges, scanned, rows) {
|
|
6
|
+
if (rows.length === 0)
|
|
7
|
+
return;
|
|
8
|
+
await collection.bulkWrite(rows.map((obs) => ({
|
|
9
|
+
replaceOne: {
|
|
10
|
+
filter: { _id: obs.id },
|
|
11
|
+
replacement: toDoc(obs),
|
|
12
|
+
upsert: true,
|
|
13
|
+
},
|
|
14
|
+
})), { ordered: false });
|
|
15
|
+
}
|
|
16
|
+
export async function getObservations(observations, sessions, symbols, edges, scanned, ids) {
|
|
17
|
+
if (ids.length === 0)
|
|
18
|
+
return [];
|
|
19
|
+
const { exact, prefixes } = partitionIds(ids);
|
|
20
|
+
const or = [];
|
|
21
|
+
if (exact.length > 0)
|
|
22
|
+
or.push({ _id: { $in: exact } });
|
|
23
|
+
for (const prefix of prefixes) {
|
|
24
|
+
or.push({ _id: { $regex: `^${escapeRegex(prefix)}` } });
|
|
25
|
+
}
|
|
26
|
+
const docs = await observations.find({ $or: or }).toArray();
|
|
27
|
+
return docs.map(toObservation);
|
|
28
|
+
}
|
|
29
|
+
export async function remove(observations, sessions, symbols, edges, scanned, filter) {
|
|
30
|
+
if (filter.ids?.length === 0)
|
|
31
|
+
return 0;
|
|
32
|
+
const query = {};
|
|
33
|
+
if (filter.project)
|
|
34
|
+
query['project'] = filter.project;
|
|
35
|
+
if (filter.kind)
|
|
36
|
+
query['kind'] = filter.kind;
|
|
37
|
+
if (filter.before !== undefined)
|
|
38
|
+
query['createdAt'] = { $lt: filter.before };
|
|
39
|
+
if (filter.ids) {
|
|
40
|
+
const { exact, prefixes } = partitionIds(filter.ids);
|
|
41
|
+
const alternatives = [];
|
|
42
|
+
if (exact.length > 0)
|
|
43
|
+
alternatives.push({ _id: { $in: exact } });
|
|
44
|
+
for (const prefix of prefixes) {
|
|
45
|
+
alternatives.push({ _id: { $regex: `^${escapeRegex(prefix)}` } });
|
|
46
|
+
}
|
|
47
|
+
query['$or'] = alternatives;
|
|
48
|
+
}
|
|
49
|
+
const count = await observations.countDocuments(query);
|
|
50
|
+
await observations.deleteMany(query);
|
|
51
|
+
if (isWholeScope(filter)) {
|
|
52
|
+
const scope = filter.project ? { project: filter.project } : {};
|
|
53
|
+
await sessions.deleteMany(scope);
|
|
54
|
+
await symbols.deleteMany(scope);
|
|
55
|
+
await edges.deleteMany(scope);
|
|
56
|
+
await scanned.deleteMany(scope);
|
|
57
|
+
}
|
|
58
|
+
return count;
|
|
59
|
+
}
|
|
60
|
+
export async function list(observations, sessions, symbols, edges, scanned, filter) {
|
|
61
|
+
const query = {};
|
|
62
|
+
if (filter.project)
|
|
63
|
+
query['project'] = filter.project;
|
|
64
|
+
if (filter.kind)
|
|
65
|
+
query['kind'] = filter.kind;
|
|
66
|
+
if (filter.status)
|
|
67
|
+
query['status'] = filter.status;
|
|
68
|
+
if (filter.after !== undefined)
|
|
69
|
+
query['createdAt'] = { $gt: filter.after };
|
|
70
|
+
const docs = await observations
|
|
71
|
+
.find(query)
|
|
72
|
+
.sort({ createdAt: 1 })
|
|
73
|
+
.limit(filter.limit ?? 1000)
|
|
74
|
+
.toArray();
|
|
75
|
+
return docs.map(toObservation);
|
|
76
|
+
}
|
|
77
|
+
export async function listProjects(observations, sessions, symbols, edges, scanned) {
|
|
78
|
+
const rows = await observations
|
|
79
|
+
.aggregate([
|
|
80
|
+
{ $group: { _id: '$project', n: { $sum: 1 }, last: { $max: '$createdAt' } } },
|
|
81
|
+
{ $sort: { last: -1 } },
|
|
82
|
+
])
|
|
83
|
+
.toArray();
|
|
84
|
+
return rows.map((row) => ({
|
|
85
|
+
project: row._id,
|
|
86
|
+
observations: row.n,
|
|
87
|
+
lastActive: row.last,
|
|
88
|
+
}));
|
|
89
|
+
}
|
|
90
|
+
export async function closeObservations(observations, ids) {
|
|
91
|
+
if (ids.length === 0)
|
|
92
|
+
return 0;
|
|
93
|
+
const filter = { _id: { $in: ids }, status: 'open' };
|
|
94
|
+
const n = await observations.countDocuments(filter);
|
|
95
|
+
await observations.updateMany(filter, { $set: { status: 'done' } });
|
|
96
|
+
return n;
|
|
97
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Collection } from './driver.js';
|
|
2
|
+
import type { ObservationIndexEntry, SearchQuery, TimelineQuery } from '../../types.js';
|
|
3
|
+
import type { ObservationDoc } from './docs.js';
|
|
4
|
+
export declare function searchKeyword(observations: Collection<ObservationDoc>, query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
5
|
+
export declare function timeline(observations: Collection<ObservationDoc>, query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
6
|
+
export declare function hasAtlasVectorIndex(observations: Collection<ObservationDoc>): Promise<boolean>;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { toIndexEntry } from './docs.js';
|
|
2
|
+
import { scopeFilter } from './filters.js';
|
|
3
|
+
export async function searchKeyword(observations, query) {
|
|
4
|
+
const filter = {
|
|
5
|
+
$text: { $search: query.text },
|
|
6
|
+
...scopeFilter(query),
|
|
7
|
+
};
|
|
8
|
+
const docs = await observations
|
|
9
|
+
.find(filter, {
|
|
10
|
+
projection: {
|
|
11
|
+
kind: 1,
|
|
12
|
+
title: 1,
|
|
13
|
+
project: 1,
|
|
14
|
+
createdAt: 1,
|
|
15
|
+
snippet: { $substrCP: ['$body', 0, 240] },
|
|
16
|
+
score: { $meta: 'textScore' },
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
.sort({ score: { $meta: 'textScore' } })
|
|
20
|
+
.limit(query.limit)
|
|
21
|
+
.toArray();
|
|
22
|
+
return docs.map((doc) => toIndexEntry(doc, doc['score'] ?? 0));
|
|
23
|
+
}
|
|
24
|
+
export async function timeline(observations, query) {
|
|
25
|
+
const anchor = await observations.findOne({ _id: query.observationId }, { projection: { project: 1, createdAt: 1 } });
|
|
26
|
+
if (!anchor)
|
|
27
|
+
return [];
|
|
28
|
+
const projection = { kind: 1, title: 1, project: 1, createdAt: 1 };
|
|
29
|
+
const [before, after] = await Promise.all([
|
|
30
|
+
observations
|
|
31
|
+
.find({ project: anchor.project, createdAt: { $lte: anchor.createdAt } }, { projection })
|
|
32
|
+
.sort({ createdAt: -1 })
|
|
33
|
+
.limit(query.before + 1)
|
|
34
|
+
.toArray(),
|
|
35
|
+
observations
|
|
36
|
+
.find({ project: anchor.project, createdAt: { $gt: anchor.createdAt } }, { projection })
|
|
37
|
+
.sort({ createdAt: 1 })
|
|
38
|
+
.limit(query.after)
|
|
39
|
+
.toArray(),
|
|
40
|
+
]);
|
|
41
|
+
return [...before.reverse(), ...after].map((doc) => toIndexEntry(doc, 0));
|
|
42
|
+
}
|
|
43
|
+
export async function hasAtlasVectorIndex(observations) {
|
|
44
|
+
try {
|
|
45
|
+
const indexes = await observations.listSearchIndexes().toArray();
|
|
46
|
+
return indexes.some((index) => index['name'] === 'memory_vector');
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Collection } from './driver.js';
|
|
2
|
+
import type { Session } from '../../types.js';
|
|
3
|
+
import type { SessionDoc } from './docs.js';
|
|
4
|
+
export declare function upsertSession(sessions: Collection<SessionDoc>, session: Session): Promise<void>;
|
|
5
|
+
export declare function getSession(sessions: Collection<SessionDoc>, id: string): Promise<Session | null>;
|
|
6
|
+
export declare function recentSessions(sessions: Collection<SessionDoc>, project: string, limit: number): Promise<Session[]>;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { toSession } from './docs.js';
|
|
2
|
+
export async function upsertSession(sessions, session) {
|
|
3
|
+
const set = {
|
|
4
|
+
project: session.project,
|
|
5
|
+
startedAt: session.startedAt,
|
|
6
|
+
};
|
|
7
|
+
if (session.endedAt !== undefined)
|
|
8
|
+
set.endedAt = session.endedAt;
|
|
9
|
+
if (session.summary !== undefined)
|
|
10
|
+
set.summary = session.summary;
|
|
11
|
+
await sessions.updateOne({ _id: session.id }, { $set: set }, { upsert: true });
|
|
12
|
+
}
|
|
13
|
+
export async function getSession(sessions, id) {
|
|
14
|
+
const doc = await sessions.findOne({ _id: id });
|
|
15
|
+
return doc ? toSession(doc) : null;
|
|
16
|
+
}
|
|
17
|
+
export async function recentSessions(sessions, project, limit) {
|
|
18
|
+
const docs = await sessions
|
|
19
|
+
.find({ project, summary: { $type: 'string' } })
|
|
20
|
+
.sort({ startedAt: -1 })
|
|
21
|
+
.limit(limit)
|
|
22
|
+
.toArray();
|
|
23
|
+
return docs.map(toSession);
|
|
24
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Collection } from './driver.js';
|
|
2
|
+
import type { ObservationIndexEntry, SearchQuery } from '../../types.js';
|
|
3
|
+
import type { ObservationDoc } from './docs.js';
|
|
4
|
+
import type { VectorCache } from './filters.js';
|
|
5
|
+
export declare function searchVector(observations: Collection<ObservationDoc>, cache: VectorCache, vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { toIndexEntry } from './docs.js';
|
|
2
|
+
import { scopeFilter } from './filters.js';
|
|
3
|
+
import * as searchOps from './search.js';
|
|
4
|
+
import { cosine } from '../../util/vector.js';
|
|
5
|
+
export async function searchVector(observations, cache, vector, query) {
|
|
6
|
+
if (cache.atlasVectorIndex === null) {
|
|
7
|
+
cache.atlasVectorIndex = await searchOps.hasAtlasVectorIndex(observations);
|
|
8
|
+
}
|
|
9
|
+
if (cache.atlasVectorIndex) {
|
|
10
|
+
const docs = await observations
|
|
11
|
+
.aggregate([
|
|
12
|
+
{
|
|
13
|
+
$vectorSearch: {
|
|
14
|
+
index: 'memory_vector',
|
|
15
|
+
path: 'embedding',
|
|
16
|
+
queryVector: vector,
|
|
17
|
+
numCandidates: Math.max(query.limit * 10, 100),
|
|
18
|
+
limit: query.limit,
|
|
19
|
+
filter: scopeFilter(query),
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
...(query.embedder
|
|
23
|
+
? [
|
|
24
|
+
{
|
|
25
|
+
$match: { $or: [{ embedder: query.embedder }, { embedder: { $exists: false } }] },
|
|
26
|
+
},
|
|
27
|
+
]
|
|
28
|
+
: []),
|
|
29
|
+
{
|
|
30
|
+
$project: {
|
|
31
|
+
kind: 1,
|
|
32
|
+
title: 1,
|
|
33
|
+
project: 1,
|
|
34
|
+
createdAt: 1,
|
|
35
|
+
score: { $meta: 'vectorSearchScore' },
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
])
|
|
39
|
+
.toArray();
|
|
40
|
+
return docs.map((doc) => toIndexEntry(doc, doc.score));
|
|
41
|
+
}
|
|
42
|
+
const docs = await observations
|
|
43
|
+
.find({
|
|
44
|
+
embedding: { $exists: true },
|
|
45
|
+
...(query.embedder
|
|
46
|
+
? { $or: [{ embedder: query.embedder }, { embedder: { $exists: false } }] }
|
|
47
|
+
: {}),
|
|
48
|
+
...scopeFilter(query),
|
|
49
|
+
}, { projection: { kind: 1, title: 1, project: 1, createdAt: 1, embedding: 1 } })
|
|
50
|
+
.sort({ createdAt: -1 })
|
|
51
|
+
.limit(query.maxScanCandidates ?? 25000)
|
|
52
|
+
.toArray();
|
|
53
|
+
return docs
|
|
54
|
+
.map((doc) => toIndexEntry(doc, doc.embedding ? cosine(vector, doc.embedding) : 0))
|
|
55
|
+
.sort((a, b) => b.score - a.score)
|
|
56
|
+
.slice(0, query.limit);
|
|
57
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export interface QueryResult {
|
|
2
|
+
rows: Record<string, unknown>[];
|
|
3
|
+
rowCount: number | null;
|
|
4
|
+
}
|
|
5
|
+
export interface PoolClient {
|
|
6
|
+
query(sql: string, values?: unknown[]): Promise<QueryResult>;
|
|
7
|
+
release(): void;
|
|
8
|
+
}
|
|
9
|
+
export interface Pool {
|
|
10
|
+
query(sql: string, values?: unknown[]): Promise<QueryResult>;
|
|
11
|
+
connect(): Promise<PoolClient>;
|
|
12
|
+
end(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface PgModule {
|
|
15
|
+
Pool: new (config: {
|
|
16
|
+
connectionString: string;
|
|
17
|
+
}) => Pool;
|
|
18
|
+
}
|
|
19
|
+
export declare function importPg(): Promise<PgModule>;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export async function importPg() {
|
|
2
|
+
try {
|
|
3
|
+
const mod = (await import('pg'));
|
|
4
|
+
if (mod.default?.Pool)
|
|
5
|
+
return mod.default;
|
|
6
|
+
if (mod.Pool)
|
|
7
|
+
return { Pool: mod.Pool };
|
|
8
|
+
throw new Error('unexpected pg module shape');
|
|
9
|
+
}
|
|
10
|
+
catch {
|
|
11
|
+
throw new Error('Postgres driver not installed. Run: npm install pg');
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export function appendScope(query, conditions, values) {
|
|
2
|
+
if (query.project) {
|
|
3
|
+
values.push(query.project);
|
|
4
|
+
conditions.push(`project = $${values.length}`);
|
|
5
|
+
}
|
|
6
|
+
if (query.kind) {
|
|
7
|
+
values.push(query.kind);
|
|
8
|
+
conditions.push(`kind = $${values.length}`);
|
|
9
|
+
}
|
|
10
|
+
if (query.tag) {
|
|
11
|
+
values.push(JSON.stringify([query.tag]));
|
|
12
|
+
conditions.push(`tags @> $${values.length}::jsonb`);
|
|
13
|
+
}
|
|
14
|
+
if (query.since !== undefined) {
|
|
15
|
+
values.push(query.since);
|
|
16
|
+
conditions.push(`created_at >= $${values.length}`);
|
|
17
|
+
}
|
|
18
|
+
if (query.until !== undefined) {
|
|
19
|
+
values.push(query.until);
|
|
20
|
+
conditions.push(`created_at <= $${values.length}`);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Pool } from './driver.js';
|
|
2
|
+
import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
|
|
3
|
+
export declare function upsertGraph(pool: Pool, scan: {
|
|
4
|
+
symbols: CodeSymbol[];
|
|
5
|
+
edges: CodeEdge[];
|
|
6
|
+
files: ScannedFile[];
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
export declare function findSymbols(pool: Pool, filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
9
|
+
export declare function findEdges(pool: Pool, filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
10
|
+
export declare function scannedFiles(pool: Pool, project: string): Promise<ScannedFile[]>;
|
|
11
|
+
export declare function removeGraph(pool: Pool, project: string, files?: string[]): Promise<number>;
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { toEdge, toSymbol } from './rows.js';
|
|
2
|
+
export async function upsertGraph(pool, scan) {
|
|
3
|
+
const client = await pool.connect();
|
|
4
|
+
try {
|
|
5
|
+
await client.query('BEGIN');
|
|
6
|
+
for (const s of scan.symbols) {
|
|
7
|
+
await client.query(`INSERT INTO symbols (id, project, name, kind, file, line, lang, signature)
|
|
8
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
|
9
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
10
|
+
name = EXCLUDED.name, kind = EXCLUDED.kind, file = EXCLUDED.file,
|
|
11
|
+
line = EXCLUDED.line, lang = EXCLUDED.lang,
|
|
12
|
+
signature = EXCLUDED.signature`, [s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature]);
|
|
13
|
+
}
|
|
14
|
+
for (const e of scan.edges) {
|
|
15
|
+
await client.query(`INSERT INTO symbol_edges (id, project, src_id, src_name, dst_id, dst_name,
|
|
16
|
+
relation, confidence, score, file, line)
|
|
17
|
+
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
|
|
18
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
19
|
+
dst_id = EXCLUDED.dst_id, dst_name = EXCLUDED.dst_name,
|
|
20
|
+
relation = EXCLUDED.relation, confidence = EXCLUDED.confidence,
|
|
21
|
+
score = EXCLUDED.score, file = EXCLUDED.file, line = EXCLUDED.line`, [
|
|
22
|
+
e.id,
|
|
23
|
+
e.project,
|
|
24
|
+
e.srcId,
|
|
25
|
+
e.srcName,
|
|
26
|
+
e.dstId,
|
|
27
|
+
e.dstName,
|
|
28
|
+
e.relation,
|
|
29
|
+
e.confidence,
|
|
30
|
+
e.score,
|
|
31
|
+
e.file,
|
|
32
|
+
e.line,
|
|
33
|
+
]);
|
|
34
|
+
}
|
|
35
|
+
for (const f of scan.files) {
|
|
36
|
+
await client.query(`INSERT INTO scanned_files (project, path, hash, scanned_at)
|
|
37
|
+
VALUES ($1, $2, $3, $4)
|
|
38
|
+
ON CONFLICT (project, path) DO UPDATE SET
|
|
39
|
+
hash = EXCLUDED.hash, scanned_at = EXCLUDED.scanned_at`, [f.project, f.path, f.hash, f.scannedAt]);
|
|
40
|
+
}
|
|
41
|
+
await client.query('COMMIT');
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
await client.query('ROLLBACK');
|
|
45
|
+
throw error;
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
client.release();
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export async function findSymbols(pool, filter) {
|
|
52
|
+
const values = [filter.project];
|
|
53
|
+
const conditions = ['project = $1'];
|
|
54
|
+
if (filter.name) {
|
|
55
|
+
values.push(filter.name);
|
|
56
|
+
conditions.push(`name = $${values.length}`);
|
|
57
|
+
}
|
|
58
|
+
if (filter.file) {
|
|
59
|
+
values.push(filter.file);
|
|
60
|
+
conditions.push(`file = $${values.length}`);
|
|
61
|
+
}
|
|
62
|
+
values.push(filter.limit ?? 500);
|
|
63
|
+
const res = await pool.query(`SELECT * FROM symbols WHERE ${conditions.join(' AND ')}
|
|
64
|
+
ORDER BY file, line LIMIT $${values.length}`, values);
|
|
65
|
+
return res.rows.map(toSymbol);
|
|
66
|
+
}
|
|
67
|
+
export async function findEdges(pool, filter) {
|
|
68
|
+
const values = [filter.project];
|
|
69
|
+
const sides = [];
|
|
70
|
+
for (const [column, ids] of [
|
|
71
|
+
['src_id', filter.srcIds],
|
|
72
|
+
['dst_id', filter.dstIds],
|
|
73
|
+
]) {
|
|
74
|
+
if (!ids || ids.length === 0)
|
|
75
|
+
continue;
|
|
76
|
+
values.push(ids);
|
|
77
|
+
sides.push(`${column} = ANY($${values.length}::text[])`);
|
|
78
|
+
}
|
|
79
|
+
const where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
|
|
80
|
+
values.push(filter.limit ?? 2000);
|
|
81
|
+
const res = await pool.query(`SELECT * FROM symbol_edges WHERE project = $1${where} LIMIT $${values.length}`, values);
|
|
82
|
+
return res.rows.map(toEdge);
|
|
83
|
+
}
|
|
84
|
+
export async function scannedFiles(pool, project) {
|
|
85
|
+
const res = await pool.query('SELECT * FROM scanned_files WHERE project = $1', [project]);
|
|
86
|
+
return res.rows.map((row) => ({
|
|
87
|
+
project: row['project'],
|
|
88
|
+
path: row['path'],
|
|
89
|
+
hash: row['hash'],
|
|
90
|
+
scannedAt: Number(row['scanned_at']),
|
|
91
|
+
}));
|
|
92
|
+
}
|
|
93
|
+
export async function removeGraph(pool, project, files) {
|
|
94
|
+
const scoped = files !== undefined && files.length > 0;
|
|
95
|
+
const values = scoped ? [project, files] : [project];
|
|
96
|
+
const where = scoped ? 'project = $1 AND file = ANY($2::text[])' : 'project = $1';
|
|
97
|
+
const cacheWhere = scoped ? 'project = $1 AND path = ANY($2::text[])' : 'project = $1';
|
|
98
|
+
const client = await pool.connect();
|
|
99
|
+
try {
|
|
100
|
+
await client.query('BEGIN');
|
|
101
|
+
const counted = await client.query(`SELECT COUNT(*)::int AS n FROM symbols WHERE ${where}`, values);
|
|
102
|
+
await client.query(`DELETE FROM symbols WHERE ${where}`, values);
|
|
103
|
+
await client.query(`DELETE FROM symbol_edges WHERE ${where}`, values);
|
|
104
|
+
await client.query(`DELETE FROM scanned_files WHERE ${cacheWhere}`, values);
|
|
105
|
+
await client.query('COMMIT');
|
|
106
|
+
return Number(counted.rows[0]?.['n'] ?? 0);
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
await client.query('ROLLBACK');
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
finally {
|
|
113
|
+
client.release();
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
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
3
|
export declare class PostgresStore implements MemoryStore {
|
|
4
4
|
private readonly pool;
|
|
5
5
|
readonly kind = "postgres";
|
|
@@ -8,37 +8,15 @@ export declare class PostgresStore implements MemoryStore {
|
|
|
8
8
|
private warnedDims;
|
|
9
9
|
private constructor();
|
|
10
10
|
static create(uri: string): Promise<PostgresStore>;
|
|
11
|
-
/**
|
|
12
|
-
* Every hook is its own process, so nothing here is ever pooled or reused:
|
|
13
|
-
* this ran in full on every single prompt. Against a managed Postgres, where
|
|
14
|
-
* one round trip measured 49ms at best and 1045ms at the median, four of them
|
|
15
|
-
* before the first real query is most of why a remote backend feels broken.
|
|
16
|
-
*
|
|
17
|
-
* The version row collapses that to one read on the steady-state path. It
|
|
18
|
-
* carries the vector state too, because otherwise proving the extension is
|
|
19
|
-
* available costs back the round trip just saved. Anything unexpected — no
|
|
20
|
-
* table, an older version, an unreadable row — falls through to the full DDL,
|
|
21
|
-
* so the worst case is exactly the behaviour this replaces.
|
|
22
|
-
*/
|
|
23
11
|
init(): Promise<void>;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
private readVectorDims;
|
|
33
|
-
/**
|
|
34
|
-
* Creates the embedding column sized to the vectors actually being stored,
|
|
35
|
-
* since pgvector needs a fixed width but the width belongs to the embedder:
|
|
36
|
-
* 256 for the builtin, 384 for MiniLM. Hardcoding 384 meant every insert on
|
|
37
|
-
* a default install failed on a dimension mismatch and captured nothing.
|
|
38
|
-
* Returns false when this width cannot be stored, and the caller keeps the
|
|
39
|
-
* observation without a vector rather than losing it. `dims` is bounded
|
|
40
|
-
* above because DDL cannot be parameterised.
|
|
41
|
-
*/
|
|
12
|
+
readMeta(): Promise<{
|
|
13
|
+
version: number;
|
|
14
|
+
vectorEnabled: boolean;
|
|
15
|
+
vectorDims: number | null;
|
|
16
|
+
} | null>;
|
|
17
|
+
writeMeta(): Promise<void>;
|
|
18
|
+
ensureTagsIndexed(): Promise<void>;
|
|
19
|
+
readVectorDims(): Promise<number | null>;
|
|
42
20
|
private ensureVectorColumn;
|
|
43
21
|
close(): Promise<void>;
|
|
44
22
|
ping(): Promise<boolean>;
|
|
@@ -53,7 +31,16 @@ export declare class PostgresStore implements MemoryStore {
|
|
|
53
31
|
inventory(): Promise<string[]>;
|
|
54
32
|
searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
55
33
|
searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
34
|
+
closeObservations(ids: string[]): Promise<number>;
|
|
56
35
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
57
|
-
|
|
58
|
-
|
|
36
|
+
tryEnableVector(): Promise<boolean>;
|
|
37
|
+
upsertGraph(scan: {
|
|
38
|
+
symbols: CodeSymbol[];
|
|
39
|
+
edges: CodeEdge[];
|
|
40
|
+
files: ScannedFile[];
|
|
41
|
+
}): Promise<void>;
|
|
42
|
+
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
43
|
+
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
44
|
+
scannedFiles(project: string): Promise<ScannedFile[]>;
|
|
45
|
+
removeGraph(project: string, files?: string[]): Promise<number>;
|
|
59
46
|
}
|