claude-db 0.12.4 → 0.14.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 +93 -0
- package/dist/capture/cursors.d.ts +6 -0
- package/dist/capture/cursors.js +80 -0
- package/dist/capture/flush.d.ts +1 -3
- package/dist/capture/flush.js +35 -96
- package/dist/capture/index.d.ts +3 -3
- package/dist/capture/index.js +3 -3
- package/dist/capture/progress.js +4 -2
- package/dist/capture/reingest.d.ts +1 -0
- package/dist/capture/reingest.js +4 -1
- package/dist/capture/scrub.d.ts +1 -0
- package/dist/capture/scrub.js +4 -1
- package/dist/capture/transcript.d.ts +1 -1
- package/dist/capture/transcript.js +6 -6
- package/dist/capture/vectors.js +2 -1
- package/dist/catchup/gather.js +1 -1
- package/dist/cli/commands/catchup.js +2 -1
- package/dist/cli/commands/daemon.d.ts +1 -0
- package/dist/cli/commands/daemon.js +29 -0
- package/dist/cli/commands/distill.js +2 -1
- package/dist/cli/commands/graph.js +11 -11
- package/dist/cli/commands/insight.js +10 -1
- package/dist/cli/commands/maintain.js +3 -1
- package/dist/cli/commands/memory.js +2 -1
- package/dist/cli/commands/setup.js +1 -2
- package/dist/cli/commands/sync.js +4 -1
- package/dist/cli/index.js +4 -0
- package/dist/cli/install.js +1 -2
- package/dist/cli/instructions.d.ts +1 -3
- package/dist/cli/instructions.js +5 -40
- package/dist/cli/refresh.js +2 -6
- package/dist/cli/usage.js +1 -0
- package/dist/config/load.js +2 -1
- package/dist/config/schema.d.ts +4 -2
- package/dist/config/schema.js +6 -2
- package/dist/context.d.ts +1 -0
- package/dist/context.js +30 -6
- package/dist/daemon/client.d.ts +14 -0
- package/dist/daemon/client.js +106 -0
- package/dist/daemon/paths.d.ts +18 -0
- package/dist/daemon/paths.js +19 -0
- package/dist/daemon/pid.d.ts +3 -0
- package/dist/daemon/pid.js +20 -0
- package/dist/daemon/server.d.ts +2 -0
- package/dist/daemon/server.js +227 -0
- package/dist/embed/local.js +4 -1
- package/dist/facts/budget.d.ts +1 -0
- package/dist/facts/budget.js +4 -0
- package/dist/facts/claude-memory.js +3 -3
- package/dist/facts/distill.js +2 -2
- package/dist/facts/known.js +1 -1
- package/dist/facts/last-chat.d.ts +1 -1
- package/dist/facts/last-chat.js +5 -4
- package/dist/facts/model.d.ts +1 -1
- package/dist/facts/model.js +31 -16
- package/dist/facts/start-render.d.ts +13 -0
- package/dist/facts/start-render.js +43 -0
- package/dist/facts/start.d.ts +3 -6
- package/dist/facts/start.js +33 -47
- package/dist/graph/grammars.js +2 -1
- package/dist/graph/index.d.ts +1 -1
- package/dist/graph/index.js +1 -1
- package/dist/graph/query/index.d.ts +1 -0
- package/dist/graph/query/index.js +1 -0
- package/dist/graph/query/refresh.js +5 -1
- package/dist/graph/query/stale.js +19 -2
- package/dist/graph/query/usages.d.ts +12 -0
- package/dist/graph/query/usages.js +37 -0
- package/dist/graph/scan/cache.d.ts +1 -0
- package/dist/graph/scan/cache.js +3 -2
- package/dist/graph/scan/files.js +2 -2
- package/dist/graph/scan/persist.js +12 -16
- package/dist/hooks/compact.d.ts +3 -1
- package/dist/hooks/compact.js +4 -3
- package/dist/hooks/detached.js +2 -0
- package/dist/hooks/graph-presence.d.ts +3 -0
- package/dist/hooks/graph-presence.js +42 -0
- package/dist/hooks/grep-symbols.d.ts +3 -1
- package/dist/hooks/grep-symbols.js +41 -2
- package/dist/hooks/handlers/prefer-usages.d.ts +2 -0
- package/dist/hooks/handlers/prefer-usages.js +118 -0
- package/dist/hooks/handlers/session-end.d.ts +2 -0
- package/dist/hooks/handlers/session-end.js +60 -0
- package/dist/hooks/handlers/session-start.d.ts +2 -0
- package/dist/hooks/handlers/session-start.js +83 -0
- package/dist/hooks/handlers/turn-end.d.ts +2 -0
- package/dist/hooks/handlers/turn-end.js +31 -0
- package/dist/hooks/handlers/user-prompt.d.ts +2 -0
- package/dist/hooks/handlers/user-prompt.js +55 -0
- package/dist/hooks/local.d.ts +2 -0
- package/dist/hooks/local.js +22 -0
- package/dist/hooks/payload.d.ts +9 -4
- package/dist/hooks/payload.js +30 -20
- package/dist/hooks/pick-deliver.js +2 -1
- package/dist/hooks/prefer-usages.js +2 -118
- package/dist/hooks/prompt-recall.d.ts +4 -2
- package/dist/hooks/prompt-recall.js +10 -10
- package/dist/hooks/route.d.ts +2 -0
- package/dist/hooks/route.js +33 -0
- package/dist/hooks/run.d.ts +13 -0
- package/dist/hooks/run.js +12 -0
- package/dist/hooks/save-turn.d.ts +2 -0
- package/dist/hooks/save-turn.js +11 -0
- package/dist/hooks/session-end.js +2 -63
- package/dist/hooks/session-start.js +2 -90
- package/dist/hooks/turn-end.js +2 -25
- package/dist/hooks/turn-save.js +10 -15
- package/dist/hooks/user-prompt.js +2 -58
- package/dist/mcp/tools/ids.d.ts +3 -0
- package/dist/mcp/tools/ids.js +6 -0
- package/dist/mcp/tools/memory.js +2 -1
- package/dist/mcp/tools/observations.js +3 -2
- package/dist/mcp/tools/search.js +4 -9
- package/dist/mcp/tools/usages.js +18 -44
- package/dist/pick/worker.js +1 -2
- package/dist/search/service.d.ts +6 -0
- package/dist/search/service.js +27 -11
- package/dist/store/adapter.d.ts +7 -5
- package/dist/store/mongo/index.d.ts +3 -6
- package/dist/store/mongo/index.js +5 -0
- package/dist/store/mongo/observations.js +13 -1
- package/dist/store/postgres/driver.d.ts +10 -3
- package/dist/store/postgres/driver.js +12 -0
- package/dist/store/postgres/filters.d.ts +1 -0
- package/dist/store/postgres/filters.js +3 -0
- package/dist/store/postgres/graph.d.ts +3 -5
- package/dist/store/postgres/graph.js +59 -52
- package/dist/store/postgres/index.d.ts +3 -6
- package/dist/store/postgres/index.js +8 -5
- package/dist/store/postgres/insert.js +98 -50
- package/dist/store/postgres/meta.d.ts +1 -0
- package/dist/store/postgres/meta.js +4 -0
- package/dist/store/postgres/observations.js +3 -3
- package/dist/store/project-resolver.d.ts +4 -0
- package/dist/store/project-resolver.js +34 -23
- package/dist/store/scoped-store.js +4 -2
- package/dist/store/sqlite/filters.d.ts +1 -0
- package/dist/store/sqlite/filters.js +6 -2
- package/dist/store/sqlite/graph.d.ts +3 -5
- package/dist/store/sqlite/graph.js +45 -35
- package/dist/store/sqlite/index.d.ts +3 -6
- package/dist/store/sqlite/index.js +3 -0
- package/dist/store/sqlite/observations.js +12 -9
- package/dist/util/claude-binary.js +5 -4
- package/dist/util/claude-cli.js +2 -1
- package/dist/util/claude-dir.d.ts +2 -0
- package/dist/util/claude-dir.js +8 -0
- package/dist/util/env.d.ts +2 -0
- package/dist/util/env.js +6 -0
- package/dist/util/job-lock.d.ts +1 -0
- package/dist/util/job-lock.js +50 -10
- package/dist/util/shortid.d.ts +3 -0
- package/dist/util/shortid.js +11 -0
- package/docs/how-it-works.md +5 -10
- package/docs/setup-guide.md +6 -1
- package/package.json +7 -6
- package/dist/hooks/start-legacy.d.ts +0 -6
- package/dist/hooks/start-legacy.js +0 -47
|
@@ -25,40 +25,56 @@ async function upsertChunked(client, rows, width, sql, columns) {
|
|
|
25
25
|
await client.query(sql(placeholders(slice.length, width)), values);
|
|
26
26
|
}
|
|
27
27
|
}
|
|
28
|
-
|
|
28
|
+
async function writeGraphRows(client, scan) {
|
|
29
|
+
await upsertChunked(client, lastById(scan.symbols, (s) => s.id), 8, (values) => `INSERT INTO symbols (id, project, name, kind, file, line, lang, signature)
|
|
30
|
+
VALUES ${values}
|
|
31
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
32
|
+
name = EXCLUDED.name, kind = EXCLUDED.kind, file = EXCLUDED.file,
|
|
33
|
+
line = EXCLUDED.line, lang = EXCLUDED.lang,
|
|
34
|
+
signature = EXCLUDED.signature`, (s) => [s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature]);
|
|
35
|
+
await upsertChunked(client, lastById(scan.edges, (e) => e.id), 11, (values) => `INSERT INTO symbol_edges (id, project, src_id, src_name, dst_id, dst_name,
|
|
36
|
+
relation, confidence, score, file, line)
|
|
37
|
+
VALUES ${values}
|
|
38
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
39
|
+
dst_id = EXCLUDED.dst_id, dst_name = EXCLUDED.dst_name,
|
|
40
|
+
relation = EXCLUDED.relation, confidence = EXCLUDED.confidence,
|
|
41
|
+
score = EXCLUDED.score, file = EXCLUDED.file, line = EXCLUDED.line`, (e) => [
|
|
42
|
+
e.id,
|
|
43
|
+
e.project,
|
|
44
|
+
e.srcId,
|
|
45
|
+
e.srcName,
|
|
46
|
+
e.dstId,
|
|
47
|
+
e.dstName,
|
|
48
|
+
e.relation,
|
|
49
|
+
e.confidence,
|
|
50
|
+
e.score,
|
|
51
|
+
e.file,
|
|
52
|
+
e.line,
|
|
53
|
+
]);
|
|
54
|
+
await upsertChunked(client, lastById(scan.files, (f) => `${f.project}\u0000${f.path}`), 4, (values) => `INSERT INTO scanned_files (project, path, hash, scanned_at)
|
|
55
|
+
VALUES ${values}
|
|
56
|
+
ON CONFLICT (project, path) DO UPDATE SET
|
|
57
|
+
hash = EXCLUDED.hash, scanned_at = EXCLUDED.scanned_at`, (f) => [f.project, f.path, f.hash, f.scannedAt]);
|
|
58
|
+
}
|
|
59
|
+
async function deleteGraphRows(client, project, files) {
|
|
60
|
+
if (files !== null && files.length === 0)
|
|
61
|
+
return 0;
|
|
62
|
+
const values = files === null ? [project] : [project, files];
|
|
63
|
+
const where = files === null ? 'project = $1' : 'project = $1 AND file = ANY($2::text[])';
|
|
64
|
+
const cacheWhere = files === null ? 'project = $1' : 'project = $1 AND path = ANY($2::text[])';
|
|
65
|
+
const res = await client.query(`WITH gone AS (DELETE FROM symbols WHERE ${where} RETURNING 1),
|
|
66
|
+
edges AS (DELETE FROM symbol_edges WHERE ${where}),
|
|
67
|
+
cached AS (DELETE FROM scanned_files WHERE ${cacheWhere})
|
|
68
|
+
SELECT COUNT(*)::int AS n FROM gone`, values);
|
|
69
|
+
return Number(res.rows[0]?.['n'] ?? 0);
|
|
70
|
+
}
|
|
71
|
+
async function inTransaction(pool, work) {
|
|
29
72
|
const client = await pool.connect();
|
|
30
73
|
try {
|
|
31
74
|
await client.query('BEGIN');
|
|
32
|
-
|
|
33
|
-
VALUES ${values}
|
|
34
|
-
ON CONFLICT (id) DO UPDATE SET
|
|
35
|
-
name = EXCLUDED.name, kind = EXCLUDED.kind, file = EXCLUDED.file,
|
|
36
|
-
line = EXCLUDED.line, lang = EXCLUDED.lang,
|
|
37
|
-
signature = EXCLUDED.signature`, (s) => [s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature]);
|
|
38
|
-
await upsertChunked(client, lastById(scan.edges, (e) => e.id), 11, (values) => `INSERT INTO symbol_edges (id, project, src_id, src_name, dst_id, dst_name,
|
|
39
|
-
relation, confidence, score, file, line)
|
|
40
|
-
VALUES ${values}
|
|
41
|
-
ON CONFLICT (id) DO UPDATE SET
|
|
42
|
-
dst_id = EXCLUDED.dst_id, dst_name = EXCLUDED.dst_name,
|
|
43
|
-
relation = EXCLUDED.relation, confidence = EXCLUDED.confidence,
|
|
44
|
-
score = EXCLUDED.score, file = EXCLUDED.file, line = EXCLUDED.line`, (e) => [
|
|
45
|
-
e.id,
|
|
46
|
-
e.project,
|
|
47
|
-
e.srcId,
|
|
48
|
-
e.srcName,
|
|
49
|
-
e.dstId,
|
|
50
|
-
e.dstName,
|
|
51
|
-
e.relation,
|
|
52
|
-
e.confidence,
|
|
53
|
-
e.score,
|
|
54
|
-
e.file,
|
|
55
|
-
e.line,
|
|
56
|
-
]);
|
|
57
|
-
await upsertChunked(client, lastById(scan.files, (f) => `${f.project}\u0000${f.path}`), 4, (values) => `INSERT INTO scanned_files (project, path, hash, scanned_at)
|
|
58
|
-
VALUES ${values}
|
|
59
|
-
ON CONFLICT (project, path) DO UPDATE SET
|
|
60
|
-
hash = EXCLUDED.hash, scanned_at = EXCLUDED.scanned_at`, (f) => [f.project, f.path, f.hash, f.scannedAt]);
|
|
75
|
+
const result = await work(client);
|
|
61
76
|
await client.query('COMMIT');
|
|
77
|
+
return result;
|
|
62
78
|
}
|
|
63
79
|
catch (error) {
|
|
64
80
|
await client.query('ROLLBACK');
|
|
@@ -68,6 +84,15 @@ export async function upsertGraph(pool, scan) {
|
|
|
68
84
|
client.release();
|
|
69
85
|
}
|
|
70
86
|
}
|
|
87
|
+
export async function upsertGraph(pool, scan) {
|
|
88
|
+
await inTransaction(pool, (client) => writeGraphRows(client, scan));
|
|
89
|
+
}
|
|
90
|
+
export async function replaceGraph(pool, project, files, scan) {
|
|
91
|
+
await inTransaction(pool, async (client) => {
|
|
92
|
+
await deleteGraphRows(client, project, files);
|
|
93
|
+
await writeGraphRows(client, scan);
|
|
94
|
+
});
|
|
95
|
+
}
|
|
71
96
|
const SYMBOL_COLUMNS = 'id, name, kind, file, line, lang, signature';
|
|
72
97
|
const EDGE_COLUMNS = 'id, src_id, src_name, dst_id, dst_name, relation, confidence, score, file, line';
|
|
73
98
|
export async function findSymbols(pool, filter) {
|
|
@@ -113,8 +138,9 @@ export async function findEdges(pool, filter) {
|
|
|
113
138
|
}
|
|
114
139
|
let where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
|
|
115
140
|
if (filter.dstName !== undefined) {
|
|
116
|
-
values.push(filter.dstName,
|
|
117
|
-
|
|
141
|
+
values.push(filter.dstName, `.${filter.dstName}`);
|
|
142
|
+
const [exact, suffix] = [`$${values.length - 1}`, `$${values.length}`];
|
|
143
|
+
where += ` AND (dst_name = ${exact} OR right(dst_name, length(${suffix})) = ${suffix})`;
|
|
118
144
|
}
|
|
119
145
|
values.push(filter.limit ?? 2000);
|
|
120
146
|
const res = await pool.query(`SELECT ${EDGE_COLUMNS} FROM symbol_edges WHERE project = $1${where}
|
|
@@ -132,24 +158,5 @@ export async function scannedFiles(pool, project, limit) {
|
|
|
132
158
|
}
|
|
133
159
|
export async function removeGraph(pool, project, files) {
|
|
134
160
|
const scoped = files !== undefined && files.length > 0;
|
|
135
|
-
|
|
136
|
-
const where = scoped ? 'project = $1 AND file = ANY($2::text[])' : 'project = $1';
|
|
137
|
-
const cacheWhere = scoped ? 'project = $1 AND path = ANY($2::text[])' : 'project = $1';
|
|
138
|
-
const client = await pool.connect();
|
|
139
|
-
try {
|
|
140
|
-
await client.query('BEGIN');
|
|
141
|
-
const counted = await client.query(`SELECT COUNT(*)::int AS n FROM symbols WHERE ${where}`, values);
|
|
142
|
-
await client.query(`DELETE FROM symbols WHERE ${where}`, values);
|
|
143
|
-
await client.query(`DELETE FROM symbol_edges WHERE ${where}`, values);
|
|
144
|
-
await client.query(`DELETE FROM scanned_files WHERE ${cacheWhere}`, values);
|
|
145
|
-
await client.query('COMMIT');
|
|
146
|
-
return Number(counted.rows[0]?.['n'] ?? 0);
|
|
147
|
-
}
|
|
148
|
-
catch (error) {
|
|
149
|
-
await client.query('ROLLBACK');
|
|
150
|
-
throw error;
|
|
151
|
-
}
|
|
152
|
-
finally {
|
|
153
|
-
client.release();
|
|
154
|
-
}
|
|
161
|
+
return deleteGraphRows(pool, project, scoped ? files : null);
|
|
155
162
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { CodeEdge, CodeSymbol, CountFilter, EdgeFilter, ListFilter, Observation, ObservationIndexEntry, ProjectFilter, RemoveFilter, ScannedFile, SearchQuery, Session, SymbolFilter, TimelineQuery } from '../../types.js';
|
|
2
|
-
import type { MemoryStore, ProjectSummary } from '../adapter.js';
|
|
2
|
+
import type { GraphRows, MemoryStore, ProjectSummary } from '../adapter.js';
|
|
3
3
|
export declare class PostgresStore implements MemoryStore {
|
|
4
4
|
private readonly pool;
|
|
5
5
|
readonly kind = "postgres";
|
|
@@ -42,11 +42,8 @@ export declare class PostgresStore implements MemoryStore {
|
|
|
42
42
|
markReplaced(ids: string[]): Promise<number>;
|
|
43
43
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
44
44
|
tryEnableVector(): Promise<boolean>;
|
|
45
|
-
upsertGraph(scan:
|
|
46
|
-
|
|
47
|
-
edges: CodeEdge[];
|
|
48
|
-
files: ScannedFile[];
|
|
49
|
-
}): Promise<void>;
|
|
45
|
+
upsertGraph(scan: GraphRows): Promise<void>;
|
|
46
|
+
replaceGraph(project: string, files: string[] | null, scan: GraphRows): Promise<void>;
|
|
50
47
|
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
51
48
|
symbolNames(project: string, limit: number): Promise<string[]>;
|
|
52
49
|
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
@@ -6,7 +6,7 @@ import * as observationsOps from './observations.js';
|
|
|
6
6
|
import * as sessionsOps from './sessions.js';
|
|
7
7
|
import * as graphOps from './graph.js';
|
|
8
8
|
import * as projectLinksOps from './project-links.js';
|
|
9
|
-
import {
|
|
9
|
+
import { openPool } from './driver.js';
|
|
10
10
|
import { DDL, SCHEMA_VERSION } from './schema.js';
|
|
11
11
|
export class PostgresStore {
|
|
12
12
|
pool;
|
|
@@ -18,13 +18,13 @@ export class PostgresStore {
|
|
|
18
18
|
this.pool = pool;
|
|
19
19
|
}
|
|
20
20
|
static async create(uri) {
|
|
21
|
-
|
|
22
|
-
const pool = new pg.Pool({ connectionString: uri });
|
|
23
|
-
return new PostgresStore(pool);
|
|
21
|
+
return new PostgresStore(await openPool(uri));
|
|
24
22
|
}
|
|
25
23
|
async init() {
|
|
26
24
|
const meta = await this.readMeta();
|
|
27
|
-
|
|
25
|
+
const cachedOff = meta?.version === SCHEMA_VERSION && !meta.vectorEnabled;
|
|
26
|
+
const installed = cachedOff && (await metaOps.hasVectorExtension(this.pool));
|
|
27
|
+
if (meta?.version === SCHEMA_VERSION && !installed) {
|
|
28
28
|
this.vectorEnabled = meta.vectorEnabled;
|
|
29
29
|
this.vectorDims = meta.vectorDims;
|
|
30
30
|
return;
|
|
@@ -155,6 +155,9 @@ export class PostgresStore {
|
|
|
155
155
|
async upsertGraph(scan) {
|
|
156
156
|
return graphOps.upsertGraph(this.pool, scan);
|
|
157
157
|
}
|
|
158
|
+
async replaceGraph(project, files, scan) {
|
|
159
|
+
return graphOps.replaceGraph(this.pool, project, files, scan);
|
|
160
|
+
}
|
|
158
161
|
async findSymbols(filter) {
|
|
159
162
|
return graphOps.findSymbols(this.pool, filter);
|
|
160
163
|
}
|
|
@@ -1,56 +1,91 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
const COLUMNS = [
|
|
2
|
+
'id',
|
|
3
|
+
'session_id',
|
|
4
|
+
'project',
|
|
5
|
+
'kind',
|
|
6
|
+
'title',
|
|
7
|
+
'body',
|
|
8
|
+
'files',
|
|
9
|
+
'tags',
|
|
10
|
+
'created_at',
|
|
11
|
+
'embedder',
|
|
12
|
+
'author',
|
|
13
|
+
'status',
|
|
14
|
+
];
|
|
15
|
+
const ROWS_PER_STATEMENT = 400;
|
|
16
|
+
const KEEP_CLOSED = `status = CASE
|
|
17
|
+
WHEN observations.status = 'done' AND EXCLUDED.status = 'open'
|
|
18
|
+
AND observations.files = EXCLUDED.files THEN observations.status
|
|
19
|
+
ELSE EXCLUDED.status END`;
|
|
20
|
+
function rowValues(obs, shape) {
|
|
21
|
+
const values = [
|
|
22
|
+
obs.id,
|
|
23
|
+
obs.sessionId,
|
|
24
|
+
obs.project,
|
|
25
|
+
obs.kind,
|
|
26
|
+
obs.title,
|
|
27
|
+
obs.body,
|
|
28
|
+
JSON.stringify(obs.files),
|
|
29
|
+
JSON.stringify(obs.tags),
|
|
30
|
+
obs.createdAt,
|
|
31
|
+
shape.vectorStored ? (obs.embedder ?? null) : null,
|
|
32
|
+
obs.author ?? null,
|
|
33
|
+
obs.status ?? 'done',
|
|
34
|
+
];
|
|
35
|
+
if (shape.vectorStored && obs.embedding)
|
|
36
|
+
values.push(`[${obs.embedding.join(',')}]`);
|
|
37
|
+
return values;
|
|
38
|
+
}
|
|
39
|
+
function statement(rows, shape, clearVector) {
|
|
40
|
+
const columns = shape.vectorStored ? [...COLUMNS, 'embedding'] : COLUMNS;
|
|
41
|
+
const values = [];
|
|
42
|
+
const tuples = rows.map((obs) => {
|
|
43
|
+
const start = values.length;
|
|
44
|
+
values.push(...rowValues(obs, shape));
|
|
45
|
+
return `(${columns.map((_, i) => `$${start + i + 1}`).join(', ')})`;
|
|
46
|
+
});
|
|
47
|
+
const updates = columns
|
|
48
|
+
.slice(1)
|
|
49
|
+
.filter((column) => !(shape.vectorKept && column === 'embedder'))
|
|
50
|
+
.map((column) => (column === 'status' ? KEEP_CLOSED : `${column} = EXCLUDED.${column}`))
|
|
51
|
+
.concat(clearVector ? ['embedding = NULL'] : [])
|
|
52
|
+
.join(', ');
|
|
53
|
+
return {
|
|
54
|
+
sql: `INSERT INTO observations (${columns.join(', ')}) VALUES ${tuples.join(', ')}
|
|
55
|
+
ON CONFLICT (id) DO UPDATE SET ${updates}`,
|
|
56
|
+
values,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function lastPerId(observations) {
|
|
60
|
+
return [...new Map(observations.map((obs) => [obs.id, obs])).values()];
|
|
61
|
+
}
|
|
62
|
+
function insertStatements(observations, width, vectors, hasVectorColumn) {
|
|
63
|
+
const groups = new Map();
|
|
64
|
+
for (const obs of lastPerId(observations)) {
|
|
65
|
+
const shape = {
|
|
66
|
+
vectorStored: vectors && obs.embedding?.length === width,
|
|
67
|
+
vectorKept: !obs.embedding?.length && obs.embedder != null,
|
|
68
|
+
};
|
|
69
|
+
const id = `${shape.vectorStored}:${shape.vectorKept}`;
|
|
70
|
+
const group = groups.get(id) ?? { shape, rows: [] };
|
|
71
|
+
group.rows.push(obs);
|
|
72
|
+
groups.set(id, group);
|
|
73
|
+
}
|
|
74
|
+
const statements = [];
|
|
75
|
+
for (const { shape, rows } of groups.values()) {
|
|
76
|
+
const clearVector = !shape.vectorStored && !shape.vectorKept && (hasVectorColumn || vectors);
|
|
77
|
+
for (let i = 0; i < rows.length; i += ROWS_PER_STATEMENT) {
|
|
78
|
+
statements.push(statement(rows.slice(i, i + ROWS_PER_STATEMENT), shape, clearVector));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return statements;
|
|
82
|
+
}
|
|
83
|
+
async function inTransaction(pool, statements) {
|
|
6
84
|
const client = await pool.connect();
|
|
7
85
|
try {
|
|
8
86
|
await client.query('BEGIN');
|
|
9
|
-
for (const
|
|
10
|
-
|
|
11
|
-
const vectorKept = !obs.embedding?.length && obs.embedder != null;
|
|
12
|
-
const columns = [
|
|
13
|
-
'id',
|
|
14
|
-
'session_id',
|
|
15
|
-
'project',
|
|
16
|
-
'kind',
|
|
17
|
-
'title',
|
|
18
|
-
'body',
|
|
19
|
-
'files',
|
|
20
|
-
'tags',
|
|
21
|
-
'created_at',
|
|
22
|
-
'embedder',
|
|
23
|
-
'author',
|
|
24
|
-
'status',
|
|
25
|
-
];
|
|
26
|
-
const values = [
|
|
27
|
-
obs.id,
|
|
28
|
-
obs.sessionId,
|
|
29
|
-
obs.project,
|
|
30
|
-
obs.kind,
|
|
31
|
-
obs.title,
|
|
32
|
-
obs.body,
|
|
33
|
-
JSON.stringify(obs.files),
|
|
34
|
-
JSON.stringify(obs.tags),
|
|
35
|
-
obs.createdAt,
|
|
36
|
-
vectorStored ? (obs.embedder ?? null) : null,
|
|
37
|
-
obs.author ?? null,
|
|
38
|
-
obs.status ?? 'done',
|
|
39
|
-
];
|
|
40
|
-
if (vectorStored && obs.embedding) {
|
|
41
|
-
columns.push('embedding');
|
|
42
|
-
values.push(`[${obs.embedding.join(',')}]`);
|
|
43
|
-
}
|
|
44
|
-
const holes = values.map((_, i) => `$${i + 1}`).join(', ');
|
|
45
|
-
const updates = columns
|
|
46
|
-
.slice(1)
|
|
47
|
-
.filter((column) => !(vectorKept && column === 'embedder'))
|
|
48
|
-
.map((column) => `${column} = EXCLUDED.${column}`)
|
|
49
|
-
.concat(!vectorStored && !vectorKept && (hasVectorColumn || vectors) ? ['embedding = NULL'] : [])
|
|
50
|
-
.join(', ');
|
|
51
|
-
await client.query(`INSERT INTO observations (${columns.join(', ')}) VALUES (${holes})
|
|
52
|
-
ON CONFLICT (id) DO UPDATE SET ${updates}`, values);
|
|
53
|
-
}
|
|
87
|
+
for (const { sql, values } of statements)
|
|
88
|
+
await client.query(sql, values);
|
|
54
89
|
await client.query('COMMIT');
|
|
55
90
|
}
|
|
56
91
|
catch (error) {
|
|
@@ -61,3 +96,16 @@ export async function insertObservations(pool, ensureVectorColumn, observations,
|
|
|
61
96
|
client.release();
|
|
62
97
|
}
|
|
63
98
|
}
|
|
99
|
+
export async function insertObservations(pool, ensureVectorColumn, observations, hasVectorColumn = false) {
|
|
100
|
+
if (observations.length === 0)
|
|
101
|
+
return;
|
|
102
|
+
const width = observations.find((obs) => obs.embedding?.length)?.embedding?.length;
|
|
103
|
+
const vectors = width !== undefined && (await ensureVectorColumn(width));
|
|
104
|
+
const statements = insertStatements(observations, width, vectors, hasVectorColumn);
|
|
105
|
+
const [only] = statements;
|
|
106
|
+
if (statements.length === 1 && only) {
|
|
107
|
+
await pool.query(only.sql, only.values);
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
await inTransaction(pool, statements);
|
|
111
|
+
}
|
|
@@ -6,6 +6,7 @@ export declare function readMeta(pool: Pool): Promise<{
|
|
|
6
6
|
} | null>;
|
|
7
7
|
export declare function ensureTagsIndexed(pool: Pool): Promise<void>;
|
|
8
8
|
export declare function readVectorDims(pool: Pool): Promise<number | null>;
|
|
9
|
+
export declare function hasVectorExtension(pool: Pool): Promise<boolean>;
|
|
9
10
|
export declare function tryEnableVector(pool: Pool): Promise<boolean>;
|
|
10
11
|
export declare function writeMeta(pool: Pool, vectorEnabled: boolean, vectorDims: number | null): Promise<void>;
|
|
11
12
|
export declare function indexEmbeddings(pool: Pool): Promise<void>;
|
|
@@ -48,6 +48,10 @@ export async function readVectorDims(pool) {
|
|
|
48
48
|
const dims = Number(res.rows[0]?.['dims'] ?? -1);
|
|
49
49
|
return dims > 0 ? dims : null;
|
|
50
50
|
}
|
|
51
|
+
export async function hasVectorExtension(pool) {
|
|
52
|
+
const res = await pool.query("SELECT 1 AS found FROM pg_extension WHERE extname = 'vector'");
|
|
53
|
+
return res.rows.length > 0;
|
|
54
|
+
}
|
|
51
55
|
export async function tryEnableVector(pool) {
|
|
52
56
|
try {
|
|
53
57
|
await pool.query('CREATE EXTENSION IF NOT EXISTS vector');
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { foreignNames, isWholeScope } from '../adapter.js';
|
|
2
2
|
import { partitionIds } from '../../util/shortid.js';
|
|
3
|
-
import { excludeSessionsClause, projectClause } from './filters.js';
|
|
3
|
+
import { excludeSessionsClause, likePrefix, projectClause } from './filters.js';
|
|
4
4
|
import { toIndexEntry, toObservation } from './rows.js';
|
|
5
5
|
import { noProjects } from '../project-scope.js';
|
|
6
6
|
const COLUMNS = 'id, session_id, project, kind, title, body, files, tags, created_at, embedder, author, status';
|
|
@@ -21,7 +21,7 @@ export async function getObservations(pool, ids) {
|
|
|
21
21
|
clauses.push(`id = ANY($${values.length}::text[])`);
|
|
22
22
|
}
|
|
23
23
|
for (const prefix of prefixes) {
|
|
24
|
-
values.push(
|
|
24
|
+
values.push(likePrefix(prefix));
|
|
25
25
|
clauses.push(`id LIKE $${values.length}`);
|
|
26
26
|
}
|
|
27
27
|
const res = await pool.query(`SELECT ${COLUMNS} FROM observations WHERE ${clauses.join(' OR ')}`, values);
|
|
@@ -42,7 +42,7 @@ export async function remove(pool, filter) {
|
|
|
42
42
|
alternatives.push(`id = ANY($${values.length}::text[])`);
|
|
43
43
|
}
|
|
44
44
|
for (const prefix of prefixes) {
|
|
45
|
-
values.push(
|
|
45
|
+
values.push(likePrefix(prefix));
|
|
46
46
|
alternatives.push(`id LIKE $${values.length}`);
|
|
47
47
|
}
|
|
48
48
|
conditions.push(`(${alternatives.join(' OR ')})`);
|
|
@@ -4,13 +4,17 @@ export interface ScopeResolverOptions {
|
|
|
4
4
|
remote: string;
|
|
5
5
|
isFolder?: (value: string) => boolean;
|
|
6
6
|
keyOf?: (folder: string, remote: string) => string;
|
|
7
|
+
scopeTtlMs?: number;
|
|
8
|
+
now?: () => number;
|
|
7
9
|
}
|
|
8
10
|
export interface ProjectScope {
|
|
9
11
|
key: string;
|
|
10
12
|
names: string[];
|
|
11
13
|
}
|
|
12
14
|
export interface ScopeResolver {
|
|
15
|
+
key(value: string): Promise<string>;
|
|
13
16
|
resolve(value: string): Promise<ProjectScope>;
|
|
14
17
|
}
|
|
18
|
+
export declare const SCOPE_TTL_MS = 60000;
|
|
15
19
|
export declare function isExistingFolder(value: string): boolean;
|
|
16
20
|
export declare function createScopeResolver(options: ScopeResolverOptions): ScopeResolver;
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { statSync } from 'node:fs';
|
|
2
2
|
import { isAbsolute } from 'node:path';
|
|
3
3
|
import { projectKey } from '../util/project-key.js';
|
|
4
|
-
import { orderScope } from './project-scope.js';
|
|
4
|
+
import { isLinkable, orderScope } from './project-scope.js';
|
|
5
|
+
export const SCOPE_TTL_MS = 60_000;
|
|
5
6
|
export function isExistingFolder(value) {
|
|
6
7
|
if (!isAbsolute(value))
|
|
7
8
|
return false;
|
|
@@ -13,37 +14,47 @@ export function isExistingFolder(value) {
|
|
|
13
14
|
}
|
|
14
15
|
}
|
|
15
16
|
export function createScopeResolver(options) {
|
|
16
|
-
const { store, remote, isFolder = isExistingFolder, keyOf = (folder, name) => projectKey(folder, { remote: name }), } = options;
|
|
17
|
-
const
|
|
18
|
-
|
|
19
|
-
const
|
|
17
|
+
const { store, remote, isFolder = isExistingFolder, keyOf = (folder, name) => projectKey(folder, { remote: name }), scopeTtlMs = SCOPE_TTL_MS, now = Date.now, } = options;
|
|
18
|
+
const scopes = new Map();
|
|
19
|
+
function forgetOthers(folder) {
|
|
20
|
+
for (const cached of [...scopes.keys()])
|
|
21
|
+
if (cached !== folder)
|
|
22
|
+
scopes.delete(cached);
|
|
23
|
+
}
|
|
24
|
+
async function linked(folder) {
|
|
20
25
|
try {
|
|
21
|
-
await store.
|
|
26
|
+
return await store.projectScope(folder);
|
|
22
27
|
}
|
|
23
28
|
catch {
|
|
24
|
-
return
|
|
29
|
+
return null;
|
|
25
30
|
}
|
|
26
|
-
return key;
|
|
27
31
|
}
|
|
28
|
-
async function
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
async function lookUp(folder) {
|
|
33
|
+
const key = keyOf(folder, remote);
|
|
34
|
+
let related = await linked(folder);
|
|
35
|
+
if (isLinkable(folder, key) && !related?.includes(key)) {
|
|
36
|
+
const before = related;
|
|
37
|
+
related = await store.linkProject(folder, key).then(() => {
|
|
38
|
+
forgetOthers(folder);
|
|
39
|
+
return linked(folder);
|
|
40
|
+
}, () => before);
|
|
34
41
|
}
|
|
42
|
+
return { key, names: orderScope(folder, [...(related ?? []), key]) };
|
|
43
|
+
}
|
|
44
|
+
function scopeOf(folder) {
|
|
45
|
+
const cached = scopes.get(folder);
|
|
46
|
+
if (cached && now() - cached.at < scopeTtlMs)
|
|
47
|
+
return cached.scope;
|
|
48
|
+
const scope = lookUp(folder);
|
|
49
|
+
scopes.set(folder, { at: now(), scope });
|
|
50
|
+
return scope;
|
|
35
51
|
}
|
|
36
52
|
return {
|
|
53
|
+
async key(value) {
|
|
54
|
+
return isFolder(value) ? (await scopeOf(value)).key : value;
|
|
55
|
+
},
|
|
37
56
|
async resolve(value) {
|
|
38
|
-
|
|
39
|
-
return { key: value, names: [value] };
|
|
40
|
-
let pending = keys.get(value);
|
|
41
|
-
if (!pending) {
|
|
42
|
-
pending = linkedKey(value);
|
|
43
|
-
keys.set(value, pending);
|
|
44
|
-
}
|
|
45
|
-
const key = await pending;
|
|
46
|
-
return { key, names: await namesFor(value, key) };
|
|
57
|
+
return isFolder(value) ? scopeOf(value) : { key: value, names: [value] };
|
|
47
58
|
},
|
|
48
59
|
};
|
|
49
60
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { assertValidIds } from '../util/shortid.js';
|
|
1
2
|
export function withProjectScope(inner, resolver) {
|
|
2
|
-
const keyOf =
|
|
3
|
+
const keyOf = (project) => resolver.key(project);
|
|
3
4
|
async function expand(filter) {
|
|
4
5
|
if (filter === undefined)
|
|
5
6
|
return undefined;
|
|
@@ -11,7 +12,7 @@ export function withProjectScope(inner, resolver) {
|
|
|
11
12
|
const overrides = {
|
|
12
13
|
async insertObservations(observations) {
|
|
13
14
|
const keyed = await Promise.all(observations.map(async (obs) => ({ ...obs, project: await keyOf(obs.project) })));
|
|
14
|
-
return inner.insertObservations(keyed);
|
|
15
|
+
return inner.insertObservations([...new Map(keyed.map((obs) => [obs.id, obs])).values()]);
|
|
15
16
|
},
|
|
16
17
|
async upsertSession(session) {
|
|
17
18
|
return inner.upsertSession({ ...session, project: await keyOf(session.project) });
|
|
@@ -20,6 +21,7 @@ export function withProjectScope(inner, resolver) {
|
|
|
20
21
|
return inner.recentSessions((await expand(project)) ?? project, limit);
|
|
21
22
|
},
|
|
22
23
|
async remove(filter) {
|
|
24
|
+
assertValidIds(filter.ids ?? []);
|
|
23
25
|
const project = await expand(filter.project);
|
|
24
26
|
return inner.remove(project === undefined ? filter : { ...filter, project });
|
|
25
27
|
},
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { ProjectFilter, RemoveFilter, SearchQuery } from '../../types.js';
|
|
2
2
|
export declare function projectClause(column: string, filter: ProjectFilter | undefined, params: unknown[]): string | null;
|
|
3
3
|
export declare function excludeSessionsClause(column: string, sessions: string[] | undefined, params: unknown[]): string | null;
|
|
4
|
+
export declare function globPrefix(prefix: string): string;
|
|
4
5
|
export declare function removeWhere(filter: RemoveFilter): {
|
|
5
6
|
where: string;
|
|
6
7
|
params: unknown[];
|
|
@@ -3,6 +3,7 @@ import { partitionIds } from '../../util/shortid.js';
|
|
|
3
3
|
import { resolve } from 'node:path';
|
|
4
4
|
import { scopeToken } from '../../util/scope.js';
|
|
5
5
|
import { meaningfulTokens } from '../../search/stopwords.js';
|
|
6
|
+
import { hookEnv } from '../../util/env.js';
|
|
6
7
|
export function projectClause(column, filter, params) {
|
|
7
8
|
const projects = projectsOf(filter);
|
|
8
9
|
if (projects.length === 0)
|
|
@@ -18,6 +19,9 @@ export function excludeSessionsClause(column, sessions, params) {
|
|
|
18
19
|
params.push(...sessions);
|
|
19
20
|
return `${column} NOT IN (${sessions.map(() => '?').join(',')})`;
|
|
20
21
|
}
|
|
22
|
+
export function globPrefix(prefix) {
|
|
23
|
+
return `${prefix.replace(/[*?[]/g, '[$&]')}*`;
|
|
24
|
+
}
|
|
21
25
|
export function removeWhere(filter) {
|
|
22
26
|
const conditions = [];
|
|
23
27
|
const params = [];
|
|
@@ -30,7 +34,7 @@ export function removeWhere(filter) {
|
|
|
30
34
|
}
|
|
31
35
|
for (const prefix of prefixes) {
|
|
32
36
|
alternatives.push('id GLOB ?');
|
|
33
|
-
params.push(
|
|
37
|
+
params.push(globPrefix(prefix));
|
|
34
38
|
}
|
|
35
39
|
conditions.push(`(${alternatives.join(' OR ')})`);
|
|
36
40
|
}
|
|
@@ -92,7 +96,7 @@ export function toMatchExpression(text, project) {
|
|
|
92
96
|
}
|
|
93
97
|
export function toFilePath(uri) {
|
|
94
98
|
if (!uri || uri.trim() === '') {
|
|
95
|
-
return resolve(
|
|
99
|
+
return resolve(hookEnv()['HOME'] ?? '.', '.claude-memory/memory.db');
|
|
96
100
|
}
|
|
97
101
|
const stripped = uri.replace(/^(sqlite|file):\/\//, '');
|
|
98
102
|
return resolve(stripped === '' ? './memory.db' : stripped);
|
|
@@ -1,10 +1,8 @@
|
|
|
1
1
|
import type { DatabaseSync } from 'node:sqlite';
|
|
2
2
|
import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
files: ScannedFile[];
|
|
7
|
-
}): Promise<void>;
|
|
3
|
+
import type { GraphRows } from '../adapter.js';
|
|
4
|
+
export declare function upsertGraph(db: DatabaseSync, scan: GraphRows): Promise<void>;
|
|
5
|
+
export declare function replaceGraph(db: DatabaseSync, project: string, files: string[] | null, scan: GraphRows): Promise<void>;
|
|
8
6
|
export declare function findSymbols(db: DatabaseSync, filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
9
7
|
export declare function symbolNames(db: DatabaseSync, project: string, limit: number): Promise<string[]>;
|
|
10
8
|
export declare function findEdges(db: DatabaseSync, filter: EdgeFilter): Promise<CodeEdge[]>;
|