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
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Pool } from './driver.js';
|
|
2
|
+
import type { ListFilter, Observation, ObservationIndexEntry, RemoveFilter, TimelineQuery } from '../../types.js';
|
|
3
|
+
import type { ProjectSummary } from '../adapter.js';
|
|
4
|
+
export declare function getObservations(pool: Pool, ids: string[]): Promise<Observation[]>;
|
|
5
|
+
export declare function remove(pool: Pool, filter: RemoveFilter): Promise<number>;
|
|
6
|
+
export declare function list(pool: Pool, filter: ListFilter): Promise<Observation[]>;
|
|
7
|
+
export declare function listProjects(pool: Pool): Promise<ProjectSummary[]>;
|
|
8
|
+
export declare function inventory(pool: Pool): Promise<string[]>;
|
|
9
|
+
export declare function timeline(pool: Pool, query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
10
|
+
export declare function closeObservations(pool: Pool, ids: string[]): Promise<number>;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { foreignNames, isWholeScope } from '../adapter.js';
|
|
2
|
+
import { partitionIds } from '../../util/shortid.js';
|
|
3
|
+
import { toIndexEntry, toObservation } from './rows.js';
|
|
4
|
+
export async function getObservations(pool, ids) {
|
|
5
|
+
if (ids.length === 0)
|
|
6
|
+
return [];
|
|
7
|
+
const { exact, prefixes } = partitionIds(ids);
|
|
8
|
+
const clauses = [];
|
|
9
|
+
const values = [];
|
|
10
|
+
if (exact.length > 0) {
|
|
11
|
+
values.push(exact);
|
|
12
|
+
clauses.push(`id = ANY($${values.length}::text[])`);
|
|
13
|
+
}
|
|
14
|
+
for (const prefix of prefixes) {
|
|
15
|
+
values.push(`${prefix.replace(/[%_]/g, '')}%`);
|
|
16
|
+
clauses.push(`id LIKE $${values.length}`);
|
|
17
|
+
}
|
|
18
|
+
const res = await pool.query(`SELECT * FROM observations WHERE ${clauses.join(' OR ')}`, values);
|
|
19
|
+
return res.rows.map(toObservation);
|
|
20
|
+
}
|
|
21
|
+
export async function remove(pool, filter) {
|
|
22
|
+
if (filter.ids?.length === 0)
|
|
23
|
+
return 0;
|
|
24
|
+
const conditions = [];
|
|
25
|
+
const values = [];
|
|
26
|
+
if (filter.ids) {
|
|
27
|
+
const { exact, prefixes } = partitionIds(filter.ids);
|
|
28
|
+
const alternatives = [];
|
|
29
|
+
if (exact.length > 0) {
|
|
30
|
+
values.push(exact);
|
|
31
|
+
alternatives.push(`id = ANY($${values.length}::text[])`);
|
|
32
|
+
}
|
|
33
|
+
for (const prefix of prefixes) {
|
|
34
|
+
values.push(`${prefix.replace(/[%_]/g, '')}%`);
|
|
35
|
+
alternatives.push(`id LIKE $${values.length}`);
|
|
36
|
+
}
|
|
37
|
+
conditions.push(`(${alternatives.join(' OR ')})`);
|
|
38
|
+
}
|
|
39
|
+
if (filter.project) {
|
|
40
|
+
values.push(filter.project);
|
|
41
|
+
conditions.push(`project = $${values.length}`);
|
|
42
|
+
}
|
|
43
|
+
if (filter.kind) {
|
|
44
|
+
values.push(filter.kind);
|
|
45
|
+
conditions.push(`kind = $${values.length}`);
|
|
46
|
+
}
|
|
47
|
+
if (filter.before !== undefined) {
|
|
48
|
+
values.push(filter.before);
|
|
49
|
+
conditions.push(`created_at < $${values.length}`);
|
|
50
|
+
}
|
|
51
|
+
const where = conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '';
|
|
52
|
+
const client = await pool.connect();
|
|
53
|
+
try {
|
|
54
|
+
await client.query('BEGIN');
|
|
55
|
+
const counted = await client.query(`SELECT COUNT(*)::int AS n FROM observations ${where}`, values);
|
|
56
|
+
await client.query(`DELETE FROM observations ${where}`, values);
|
|
57
|
+
if (isWholeScope(filter)) {
|
|
58
|
+
const scope = filter.project ? 'WHERE project = $1' : '';
|
|
59
|
+
const scopeValues = filter.project ? [filter.project] : [];
|
|
60
|
+
for (const table of ['sessions', 'symbols', 'symbol_edges', 'scanned_files']) {
|
|
61
|
+
await client.query(`DELETE FROM ${table} ${scope}`, scopeValues);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
await client.query('COMMIT');
|
|
65
|
+
return Number(counted.rows[0]?.['n'] ?? 0);
|
|
66
|
+
}
|
|
67
|
+
catch (error) {
|
|
68
|
+
await client.query('ROLLBACK');
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
finally {
|
|
72
|
+
client.release();
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export async function list(pool, filter) {
|
|
76
|
+
const conditions = [];
|
|
77
|
+
const values = [];
|
|
78
|
+
if (filter.project) {
|
|
79
|
+
values.push(filter.project);
|
|
80
|
+
conditions.push(`project = $${values.length}`);
|
|
81
|
+
}
|
|
82
|
+
if (filter.kind) {
|
|
83
|
+
values.push(filter.kind);
|
|
84
|
+
conditions.push(`kind = $${values.length}`);
|
|
85
|
+
}
|
|
86
|
+
if (filter.status) {
|
|
87
|
+
values.push(filter.status);
|
|
88
|
+
conditions.push(`status = $${values.length}`);
|
|
89
|
+
}
|
|
90
|
+
if (filter.after !== undefined) {
|
|
91
|
+
values.push(filter.after);
|
|
92
|
+
conditions.push(`created_at > $${values.length}`);
|
|
93
|
+
}
|
|
94
|
+
values.push(filter.limit ?? 1000);
|
|
95
|
+
const res = await pool.query(`SELECT * FROM observations
|
|
96
|
+
${conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : ''}
|
|
97
|
+
ORDER BY created_at ASC LIMIT $${values.length}`, values);
|
|
98
|
+
return res.rows.map(toObservation);
|
|
99
|
+
}
|
|
100
|
+
export async function listProjects(pool) {
|
|
101
|
+
const res = await pool.query(`SELECT project, COUNT(*)::int AS n, MAX(created_at) AS last
|
|
102
|
+
FROM observations GROUP BY project ORDER BY last DESC`);
|
|
103
|
+
return res.rows.map((row) => ({
|
|
104
|
+
project: row['project'],
|
|
105
|
+
observations: Number(row['n']),
|
|
106
|
+
lastActive: Number(row['last']),
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
export async function inventory(pool) {
|
|
110
|
+
const res = await pool.query(`SELECT tablename AS name FROM pg_tables
|
|
111
|
+
WHERE schemaname = ANY(current_schemas(false))`);
|
|
112
|
+
return foreignNames(res.rows.map((row) => String(row['name'] ?? '')));
|
|
113
|
+
}
|
|
114
|
+
export async function timeline(pool, query) {
|
|
115
|
+
const anchorRes = await pool.query('SELECT project, created_at FROM observations WHERE id = $1', [
|
|
116
|
+
query.observationId,
|
|
117
|
+
]);
|
|
118
|
+
const anchor = anchorRes.rows[0];
|
|
119
|
+
if (!anchor)
|
|
120
|
+
return [];
|
|
121
|
+
const res = await pool.query(`(SELECT id, kind, title, project, created_at, 0 AS score
|
|
122
|
+
FROM observations
|
|
123
|
+
WHERE project = $1 AND created_at <= $2
|
|
124
|
+
ORDER BY created_at DESC LIMIT $3)
|
|
125
|
+
UNION
|
|
126
|
+
(SELECT id, kind, title, project, created_at, 0 AS score
|
|
127
|
+
FROM observations
|
|
128
|
+
WHERE project = $1 AND created_at > $2
|
|
129
|
+
ORDER BY created_at ASC LIMIT $4)
|
|
130
|
+
ORDER BY created_at ASC`, [anchor.project, anchor.created_at, query.before + 1, query.after]);
|
|
131
|
+
return res.rows.map(toIndexEntry);
|
|
132
|
+
}
|
|
133
|
+
export async function closeObservations(pool, ids) {
|
|
134
|
+
if (ids.length === 0)
|
|
135
|
+
return 0;
|
|
136
|
+
const res = await pool.query(`UPDATE observations SET status = 'done'
|
|
137
|
+
WHERE status = 'open' AND id = ANY($1::text[])`, [ids]);
|
|
138
|
+
return res.rowCount ?? 0;
|
|
139
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CodeEdge, CodeSymbol, Observation, ObservationIndexEntry, Session } from '../../types.js';
|
|
2
|
+
export declare function toSession(row: Record<string, unknown>): Session;
|
|
3
|
+
export declare function toObservation(row: Record<string, unknown>): Observation;
|
|
4
|
+
export declare function toIndexEntry(row: Record<string, unknown>): ObservationIndexEntry;
|
|
5
|
+
export declare function toSymbol(row: Record<string, unknown>): CodeSymbol;
|
|
6
|
+
export declare function toEdge(row: Record<string, unknown>): CodeEdge;
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { toSnippet } from '../../util/snippet.js';
|
|
2
|
+
export function toSession(row) {
|
|
3
|
+
const session = {
|
|
4
|
+
id: row['id'],
|
|
5
|
+
project: row['project'],
|
|
6
|
+
startedAt: Number(row['started_at']),
|
|
7
|
+
};
|
|
8
|
+
if (row['ended_at'] != null)
|
|
9
|
+
session.endedAt = Number(row['ended_at']);
|
|
10
|
+
if (row['summary'] != null)
|
|
11
|
+
session.summary = row['summary'];
|
|
12
|
+
return session;
|
|
13
|
+
}
|
|
14
|
+
export function toObservation(row) {
|
|
15
|
+
const obs = {
|
|
16
|
+
id: row['id'],
|
|
17
|
+
sessionId: row['session_id'],
|
|
18
|
+
project: row['project'],
|
|
19
|
+
kind: row['kind'],
|
|
20
|
+
title: row['title'],
|
|
21
|
+
body: row['body'],
|
|
22
|
+
files: row['files'] ?? [],
|
|
23
|
+
tags: row['tags'] ?? [],
|
|
24
|
+
createdAt: Number(row['created_at']),
|
|
25
|
+
};
|
|
26
|
+
if (row['embedder'] != null)
|
|
27
|
+
obs.embedder = row['embedder'];
|
|
28
|
+
if (row['author'] != null)
|
|
29
|
+
obs.author = row['author'];
|
|
30
|
+
if (row['status'] != null)
|
|
31
|
+
obs.status = row['status'];
|
|
32
|
+
return obs;
|
|
33
|
+
}
|
|
34
|
+
export function toIndexEntry(row) {
|
|
35
|
+
const entry = {
|
|
36
|
+
id: row['id'],
|
|
37
|
+
kind: row['kind'],
|
|
38
|
+
title: row['title'],
|
|
39
|
+
project: row['project'],
|
|
40
|
+
createdAt: Number(row['created_at']),
|
|
41
|
+
score: Number(row['score'] ?? 0),
|
|
42
|
+
};
|
|
43
|
+
const snippet = toSnippet(row['snippet']);
|
|
44
|
+
if (snippet)
|
|
45
|
+
entry.snippet = snippet;
|
|
46
|
+
return entry;
|
|
47
|
+
}
|
|
48
|
+
export function toSymbol(row) {
|
|
49
|
+
return {
|
|
50
|
+
id: row['id'],
|
|
51
|
+
project: row['project'],
|
|
52
|
+
name: row['name'],
|
|
53
|
+
kind: row['kind'],
|
|
54
|
+
file: row['file'],
|
|
55
|
+
line: Number(row['line']),
|
|
56
|
+
lang: row['lang'],
|
|
57
|
+
signature: row['signature'] ?? '',
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
export function toEdge(row) {
|
|
61
|
+
return {
|
|
62
|
+
id: row['id'],
|
|
63
|
+
project: row['project'],
|
|
64
|
+
srcId: row['src_id'],
|
|
65
|
+
srcName: row['src_name'],
|
|
66
|
+
dstId: row['dst_id'] ?? '',
|
|
67
|
+
dstName: row['dst_name'],
|
|
68
|
+
relation: row['relation'],
|
|
69
|
+
confidence: row['confidence'],
|
|
70
|
+
score: Number(row['score'] ?? 1),
|
|
71
|
+
file: row['file'],
|
|
72
|
+
line: Number(row['line']),
|
|
73
|
+
};
|
|
74
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const TSV_EXPRESSION = "\n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(body, '')), 'C')";
|
|
2
|
+
export declare const SCHEMA_VERSION = 3;
|
|
3
|
+
export declare const DDL = "\n CREATE TABLE IF NOT EXISTS claude_db_meta (\n id INT PRIMARY KEY,\n version INT NOT NULL,\n vector_enabled BOOLEAN NOT NULL DEFAULT FALSE,\n vector_dims INT\n );\n\n CREATE TABLE IF NOT EXISTS sessions (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n started_at BIGINT NOT NULL,\n ended_at BIGINT,\n summary TEXT\n );\n CREATE INDEX IF NOT EXISTS idx_sessions_project\n ON sessions(project, started_at DESC);\n\n CREATE TABLE IF NOT EXISTS observations (\n id TEXT PRIMARY KEY,\n session_id TEXT NOT NULL,\n project TEXT NOT NULL,\n kind TEXT NOT NULL,\n title TEXT NOT NULL,\n body TEXT NOT NULL,\n files JSONB NOT NULL DEFAULT '[]'::jsonb,\n tags JSONB NOT NULL DEFAULT '[]'::jsonb,\n created_at BIGINT NOT NULL,\n embedder TEXT,\n author TEXT,\n tsv TSVECTOR GENERATED ALWAYS AS (\n setweight(to_tsvector('english', coalesce(title, '')), 'A') ||\n setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||\n setweight(to_tsvector('english', coalesce(body, '')), 'C')) STORED\n );\n CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv);\n CREATE INDEX IF NOT EXISTS idx_obs_project_time\n ON observations(project, created_at DESC);\n CREATE INDEX IF NOT EXISTS idx_obs_tags ON observations USING GIN(tags);\n\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS embedder TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS author TEXT;\n ALTER TABLE observations ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'done';\n\n CREATE TABLE IF NOT EXISTS symbols (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n name TEXT NOT NULL,\n kind TEXT NOT NULL,\n file TEXT NOT NULL,\n line INT NOT NULL,\n lang TEXT NOT NULL,\n signature TEXT NOT NULL DEFAULT ''\n );\n CREATE INDEX IF NOT EXISTS idx_symbols_lookup ON symbols(project, name);\n CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(project, file);\n\n CREATE TABLE IF NOT EXISTS symbol_edges (\n id TEXT PRIMARY KEY,\n project TEXT NOT NULL,\n src_id TEXT NOT NULL,\n src_name TEXT NOT NULL,\n dst_id TEXT NOT NULL DEFAULT '',\n dst_name TEXT NOT NULL,\n relation TEXT NOT NULL,\n confidence TEXT NOT NULL,\n score REAL NOT NULL DEFAULT 1.0,\n file TEXT NOT NULL,\n line INT NOT NULL\n );\n CREATE INDEX IF NOT EXISTS idx_edges_src ON symbol_edges(project, src_id);\n CREATE INDEX IF NOT EXISTS idx_edges_dst ON symbol_edges(project, dst_id);\n CREATE INDEX IF NOT EXISTS idx_edges_file ON symbol_edges(project, file);\n\n CREATE TABLE IF NOT EXISTS scanned_files (\n project TEXT NOT NULL,\n path TEXT NOT NULL,\n hash TEXT NOT NULL,\n scanned_at BIGINT NOT NULL,\n PRIMARY KEY (project, path)\n );\n";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
export const TSV_EXPRESSION = `
|
|
2
|
+
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
|
|
3
|
+
setweight(to_tsvector('english', coalesce(tags::text, '')), 'B') ||
|
|
4
|
+
setweight(to_tsvector('english', coalesce(body, '')), 'C')`;
|
|
5
|
+
export const SCHEMA_VERSION = 3;
|
|
6
|
+
export const DDL = `
|
|
7
|
+
CREATE TABLE IF NOT EXISTS claude_db_meta (
|
|
8
|
+
id INT PRIMARY KEY,
|
|
9
|
+
version INT NOT NULL,
|
|
10
|
+
vector_enabled BOOLEAN NOT NULL DEFAULT FALSE,
|
|
11
|
+
vector_dims INT
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
15
|
+
id TEXT PRIMARY KEY,
|
|
16
|
+
project TEXT NOT NULL,
|
|
17
|
+
started_at BIGINT NOT NULL,
|
|
18
|
+
ended_at BIGINT,
|
|
19
|
+
summary TEXT
|
|
20
|
+
);
|
|
21
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_project
|
|
22
|
+
ON sessions(project, started_at DESC);
|
|
23
|
+
|
|
24
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
25
|
+
id TEXT PRIMARY KEY,
|
|
26
|
+
session_id TEXT NOT NULL,
|
|
27
|
+
project TEXT NOT NULL,
|
|
28
|
+
kind TEXT NOT NULL,
|
|
29
|
+
title TEXT NOT NULL,
|
|
30
|
+
body TEXT NOT NULL,
|
|
31
|
+
files JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
32
|
+
tags JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
33
|
+
created_at BIGINT NOT NULL,
|
|
34
|
+
embedder TEXT,
|
|
35
|
+
author TEXT,
|
|
36
|
+
tsv TSVECTOR GENERATED ALWAYS AS (${TSV_EXPRESSION}) STORED
|
|
37
|
+
);
|
|
38
|
+
CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv);
|
|
39
|
+
CREATE INDEX IF NOT EXISTS idx_obs_project_time
|
|
40
|
+
ON observations(project, created_at DESC);
|
|
41
|
+
CREATE INDEX IF NOT EXISTS idx_obs_tags ON observations USING GIN(tags);
|
|
42
|
+
|
|
43
|
+
ALTER TABLE observations ADD COLUMN IF NOT EXISTS embedder TEXT;
|
|
44
|
+
ALTER TABLE observations ADD COLUMN IF NOT EXISTS author TEXT;
|
|
45
|
+
ALTER TABLE observations ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'done';
|
|
46
|
+
|
|
47
|
+
CREATE TABLE IF NOT EXISTS symbols (
|
|
48
|
+
id TEXT PRIMARY KEY,
|
|
49
|
+
project TEXT NOT NULL,
|
|
50
|
+
name TEXT NOT NULL,
|
|
51
|
+
kind TEXT NOT NULL,
|
|
52
|
+
file TEXT NOT NULL,
|
|
53
|
+
line INT NOT NULL,
|
|
54
|
+
lang TEXT NOT NULL,
|
|
55
|
+
signature TEXT NOT NULL DEFAULT ''
|
|
56
|
+
);
|
|
57
|
+
CREATE INDEX IF NOT EXISTS idx_symbols_lookup ON symbols(project, name);
|
|
58
|
+
CREATE INDEX IF NOT EXISTS idx_symbols_file ON symbols(project, file);
|
|
59
|
+
|
|
60
|
+
CREATE TABLE IF NOT EXISTS symbol_edges (
|
|
61
|
+
id TEXT PRIMARY KEY,
|
|
62
|
+
project TEXT NOT NULL,
|
|
63
|
+
src_id TEXT NOT NULL,
|
|
64
|
+
src_name TEXT NOT NULL,
|
|
65
|
+
dst_id TEXT NOT NULL DEFAULT '',
|
|
66
|
+
dst_name TEXT NOT NULL,
|
|
67
|
+
relation TEXT NOT NULL,
|
|
68
|
+
confidence TEXT NOT NULL,
|
|
69
|
+
score REAL NOT NULL DEFAULT 1.0,
|
|
70
|
+
file TEXT NOT NULL,
|
|
71
|
+
line INT NOT NULL
|
|
72
|
+
);
|
|
73
|
+
CREATE INDEX IF NOT EXISTS idx_edges_src ON symbol_edges(project, src_id);
|
|
74
|
+
CREATE INDEX IF NOT EXISTS idx_edges_dst ON symbol_edges(project, dst_id);
|
|
75
|
+
CREATE INDEX IF NOT EXISTS idx_edges_file ON symbol_edges(project, file);
|
|
76
|
+
|
|
77
|
+
CREATE TABLE IF NOT EXISTS scanned_files (
|
|
78
|
+
project TEXT NOT NULL,
|
|
79
|
+
path TEXT NOT NULL,
|
|
80
|
+
hash TEXT NOT NULL,
|
|
81
|
+
scanned_at BIGINT NOT NULL,
|
|
82
|
+
PRIMARY KEY (project, path)
|
|
83
|
+
);
|
|
84
|
+
`;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { appendScope } from './filters.js';
|
|
2
|
+
import { toIndexEntry } from './rows.js';
|
|
3
|
+
export async function searchKeyword(pool, query) {
|
|
4
|
+
const conditions = ['tsv @@ q.tsq'];
|
|
5
|
+
const values = [query.text];
|
|
6
|
+
appendScope(query, conditions, values);
|
|
7
|
+
values.push(query.limit);
|
|
8
|
+
const res = await pool.query(`WITH q AS (
|
|
9
|
+
SELECT replace(plainto_tsquery('english', $1)::text, '&', '|')::tsquery AS tsq
|
|
10
|
+
)
|
|
11
|
+
SELECT id, kind, title, project, created_at,
|
|
12
|
+
ts_headline('english', body, q.tsq,
|
|
13
|
+
'MaxFragments=1, MaxWords=18, MinWords=5') AS snippet,
|
|
14
|
+
ts_rank(tsv, q.tsq) AS score
|
|
15
|
+
FROM observations, q
|
|
16
|
+
WHERE ${conditions.join(' AND ')}
|
|
17
|
+
ORDER BY score DESC
|
|
18
|
+
LIMIT $${values.length}`, values);
|
|
19
|
+
return res.rows.map(toIndexEntry);
|
|
20
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Pool } from './driver.js';
|
|
2
|
+
import type { Session } from '../../types.js';
|
|
3
|
+
export declare function upsertSession(pool: Pool, session: Session): Promise<void>;
|
|
4
|
+
export declare function getSession(pool: Pool, id: string): Promise<Session | null>;
|
|
5
|
+
export declare function recentSessions(pool: Pool, project: string, limit: number): Promise<Session[]>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { toSession } from './rows.js';
|
|
2
|
+
export async function upsertSession(pool, session) {
|
|
3
|
+
await pool.query(`INSERT INTO sessions (id, project, started_at, ended_at, summary)
|
|
4
|
+
VALUES ($1, $2, $3, $4, $5)
|
|
5
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
6
|
+
project = EXCLUDED.project,
|
|
7
|
+
ended_at = COALESCE(EXCLUDED.ended_at, sessions.ended_at),
|
|
8
|
+
summary = COALESCE(EXCLUDED.summary, sessions.summary)`, [
|
|
9
|
+
session.id,
|
|
10
|
+
session.project,
|
|
11
|
+
session.startedAt,
|
|
12
|
+
session.endedAt ?? null,
|
|
13
|
+
session.summary ?? null,
|
|
14
|
+
]);
|
|
15
|
+
}
|
|
16
|
+
export async function getSession(pool, id) {
|
|
17
|
+
const res = await pool.query('SELECT * FROM sessions WHERE id = $1', [id]);
|
|
18
|
+
const row = res.rows[0];
|
|
19
|
+
return row ? toSession(row) : null;
|
|
20
|
+
}
|
|
21
|
+
export async function recentSessions(pool, project, limit) {
|
|
22
|
+
const res = await pool.query(`SELECT * FROM sessions
|
|
23
|
+
WHERE project = $1 AND summary IS NOT NULL
|
|
24
|
+
ORDER BY started_at DESC LIMIT $2`, [project, limit]);
|
|
25
|
+
return res.rows.map(toSession);
|
|
26
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Pool } from './driver.js';
|
|
2
|
+
import type { ObservationIndexEntry, SearchQuery } from '../../types.js';
|
|
3
|
+
export declare function searchVector(pool: Pool, vectorEnabled: boolean, vectorDims: number | null, vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { appendScope } from './filters.js';
|
|
2
|
+
import { toIndexEntry } from './rows.js';
|
|
3
|
+
export async function searchVector(pool, vectorEnabled, vectorDims, vector, query) {
|
|
4
|
+
if (!vectorEnabled || vectorDims !== vector.length)
|
|
5
|
+
return [];
|
|
6
|
+
const conditions = ['embedding IS NOT NULL'];
|
|
7
|
+
const values = [`[${vector.join(',')}]`];
|
|
8
|
+
appendScope(query, conditions, values);
|
|
9
|
+
if (query.embedder) {
|
|
10
|
+
values.push(query.embedder);
|
|
11
|
+
conditions.push(`(embedder = $${values.length} OR embedder IS NULL)`);
|
|
12
|
+
}
|
|
13
|
+
values.push(query.limit);
|
|
14
|
+
const res = await pool.query(`SELECT id, kind, title, project, created_at,
|
|
15
|
+
1 - (embedding <=> $1::vector) AS score
|
|
16
|
+
FROM observations
|
|
17
|
+
WHERE ${conditions.join(' AND ')}
|
|
18
|
+
ORDER BY embedding <=> $1::vector
|
|
19
|
+
LIMIT $${values.length}`, values);
|
|
20
|
+
return res.rows.map(toIndexEntry);
|
|
21
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { RemoveFilter, SearchQuery } from '../../types.js';
|
|
2
|
+
export declare function removeWhere(filter: RemoveFilter): {
|
|
3
|
+
where: string;
|
|
4
|
+
params: unknown[];
|
|
5
|
+
};
|
|
6
|
+
export declare const TAG_PREDICATE: (prefix: string) => string;
|
|
7
|
+
export declare function appendScope(query: SearchQuery, conditions: string[], params: unknown[], prefix: string): void;
|
|
8
|
+
export declare function toMatchExpression(text: string, project?: string): string | null;
|
|
9
|
+
export declare function toFilePath(uri: string): string;
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { partitionIds } from '../../util/shortid.js';
|
|
2
|
+
import { resolve } from 'node:path';
|
|
3
|
+
import { scopeToken } from '../../util/scope.js';
|
|
4
|
+
export function removeWhere(filter) {
|
|
5
|
+
const conditions = [];
|
|
6
|
+
const params = [];
|
|
7
|
+
if (filter.ids) {
|
|
8
|
+
const { exact, prefixes } = partitionIds(filter.ids);
|
|
9
|
+
const alternatives = [];
|
|
10
|
+
if (exact.length > 0) {
|
|
11
|
+
alternatives.push(`id IN (${exact.map(() => '?').join(',')})`);
|
|
12
|
+
params.push(...exact);
|
|
13
|
+
}
|
|
14
|
+
for (const prefix of prefixes) {
|
|
15
|
+
alternatives.push('id GLOB ?');
|
|
16
|
+
params.push(`${prefix}*`);
|
|
17
|
+
}
|
|
18
|
+
conditions.push(`(${alternatives.join(' OR ')})`);
|
|
19
|
+
}
|
|
20
|
+
if (filter.project) {
|
|
21
|
+
conditions.push('project = ?');
|
|
22
|
+
params.push(filter.project);
|
|
23
|
+
}
|
|
24
|
+
if (filter.kind) {
|
|
25
|
+
conditions.push('kind = ?');
|
|
26
|
+
params.push(filter.kind);
|
|
27
|
+
}
|
|
28
|
+
if (filter.before !== undefined) {
|
|
29
|
+
conditions.push('created_at < ?');
|
|
30
|
+
params.push(filter.before);
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
where: conditions.length > 0 ? `WHERE ${conditions.join(' AND ')}` : '',
|
|
34
|
+
params,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
export const TAG_PREDICATE = (prefix) => `EXISTS (SELECT 1 FROM json_each(${prefix}tags) WHERE value = ?)`;
|
|
38
|
+
export function appendScope(query, conditions, params, prefix) {
|
|
39
|
+
if (query.project) {
|
|
40
|
+
conditions.push(`${prefix}project = ?`);
|
|
41
|
+
params.push(query.project);
|
|
42
|
+
}
|
|
43
|
+
if (query.kind) {
|
|
44
|
+
conditions.push(`${prefix}kind = ?`);
|
|
45
|
+
params.push(query.kind);
|
|
46
|
+
}
|
|
47
|
+
if (query.tag) {
|
|
48
|
+
conditions.push(TAG_PREDICATE(prefix));
|
|
49
|
+
params.push(query.tag);
|
|
50
|
+
}
|
|
51
|
+
if (query.since !== undefined) {
|
|
52
|
+
conditions.push(`${prefix}created_at >= ?`);
|
|
53
|
+
params.push(query.since);
|
|
54
|
+
}
|
|
55
|
+
if (query.until !== undefined) {
|
|
56
|
+
conditions.push(`${prefix}created_at <= ?`);
|
|
57
|
+
params.push(query.until);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
export function toMatchExpression(text, project) {
|
|
61
|
+
const tokens = text
|
|
62
|
+
.toLowerCase()
|
|
63
|
+
.split(/[^\p{L}\p{N}_]+/u)
|
|
64
|
+
.filter((token) => token.length > 1);
|
|
65
|
+
const scope = project ? `scope:${scopeToken(project)}` : null;
|
|
66
|
+
if (tokens.length === 0)
|
|
67
|
+
return scope;
|
|
68
|
+
const terms = tokens.map((token) => `"${token}"`).join(' OR ');
|
|
69
|
+
return scope ? `${scope} AND (${terms})` : terms;
|
|
70
|
+
}
|
|
71
|
+
export function toFilePath(uri) {
|
|
72
|
+
if (!uri || uri.trim() === '') {
|
|
73
|
+
return resolve(process.env['HOME'] ?? '.', '.claude-memory/memory.db');
|
|
74
|
+
}
|
|
75
|
+
const stripped = uri.replace(/^(sqlite|file):\/\//, '');
|
|
76
|
+
return resolve(stripped === '' ? './memory.db' : stripped);
|
|
77
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import type { CodeEdge, CodeSymbol, EdgeFilter, ScannedFile, SymbolFilter } from '../../types.js';
|
|
3
|
+
export declare function upsertGraph(db: DatabaseSync, scan: {
|
|
4
|
+
symbols: CodeSymbol[];
|
|
5
|
+
edges: CodeEdge[];
|
|
6
|
+
files: ScannedFile[];
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
export declare function findSymbols(db: DatabaseSync, filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
9
|
+
export declare function findEdges(db: DatabaseSync, filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
10
|
+
export declare function scannedFiles(db: DatabaseSync, project: string): Promise<ScannedFile[]>;
|
|
11
|
+
export declare function removeGraph(db: DatabaseSync, project: string, files?: string[]): Promise<number>;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { toEdge, toSymbol } from './rows.js';
|
|
2
|
+
export async function upsertGraph(db, scan) {
|
|
3
|
+
const symbol = db.prepare(`INSERT OR REPLACE INTO symbols
|
|
4
|
+
(id, project, name, kind, file, line, lang, signature)
|
|
5
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
6
|
+
const edge = db.prepare(`INSERT OR REPLACE INTO symbol_edges
|
|
7
|
+
(id, project, src_id, src_name, dst_id, dst_name,
|
|
8
|
+
relation, confidence, score, file, line)
|
|
9
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
|
|
10
|
+
const file = db.prepare(`INSERT OR REPLACE INTO scanned_files (project, path, hash, scanned_at)
|
|
11
|
+
VALUES (?, ?, ?, ?)`);
|
|
12
|
+
db.exec('BEGIN');
|
|
13
|
+
try {
|
|
14
|
+
for (const s of scan.symbols) {
|
|
15
|
+
symbol.run(s.id, s.project, s.name, s.kind, s.file, s.line, s.lang, s.signature);
|
|
16
|
+
}
|
|
17
|
+
for (const e of scan.edges) {
|
|
18
|
+
edge.run(e.id, e.project, e.srcId, e.srcName, e.dstId, e.dstName, e.relation, e.confidence, e.score, e.file, e.line);
|
|
19
|
+
}
|
|
20
|
+
for (const f of scan.files) {
|
|
21
|
+
file.run(f.project, f.path, f.hash, f.scannedAt);
|
|
22
|
+
}
|
|
23
|
+
db.exec('COMMIT');
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
db.exec('ROLLBACK');
|
|
27
|
+
throw error;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
export async function findSymbols(db, filter) {
|
|
31
|
+
const conditions = ['project = ?'];
|
|
32
|
+
const params = [filter.project];
|
|
33
|
+
if (filter.name) {
|
|
34
|
+
conditions.push('name = ?');
|
|
35
|
+
params.push(filter.name);
|
|
36
|
+
}
|
|
37
|
+
if (filter.file) {
|
|
38
|
+
conditions.push('file = ?');
|
|
39
|
+
params.push(filter.file);
|
|
40
|
+
}
|
|
41
|
+
params.push(filter.limit ?? 500);
|
|
42
|
+
const rows = db
|
|
43
|
+
.prepare(`SELECT * FROM symbols WHERE ${conditions.join(' AND ')}
|
|
44
|
+
ORDER BY file, line LIMIT ?`)
|
|
45
|
+
.all(...params);
|
|
46
|
+
return rows.map(toSymbol);
|
|
47
|
+
}
|
|
48
|
+
export async function findEdges(db, filter) {
|
|
49
|
+
const params = [filter.project];
|
|
50
|
+
const sides = [];
|
|
51
|
+
for (const [column, ids] of [
|
|
52
|
+
['src_id', filter.srcIds],
|
|
53
|
+
['dst_id', filter.dstIds],
|
|
54
|
+
]) {
|
|
55
|
+
if (!ids || ids.length === 0)
|
|
56
|
+
continue;
|
|
57
|
+
sides.push(`${column} IN (${ids.map(() => '?').join(',')})`);
|
|
58
|
+
params.push(...ids);
|
|
59
|
+
}
|
|
60
|
+
const where = sides.length > 0 ? ` AND (${sides.join(' OR ')})` : '';
|
|
61
|
+
params.push(filter.limit ?? 2000);
|
|
62
|
+
const rows = db
|
|
63
|
+
.prepare(`SELECT * FROM symbol_edges WHERE project = ?${where} LIMIT ?`)
|
|
64
|
+
.all(...params);
|
|
65
|
+
return rows.map(toEdge);
|
|
66
|
+
}
|
|
67
|
+
export async function scannedFiles(db, project) {
|
|
68
|
+
const rows = db.prepare('SELECT * FROM scanned_files WHERE project = ?').all(project);
|
|
69
|
+
return rows.map((row) => ({
|
|
70
|
+
project: row['project'],
|
|
71
|
+
path: row['path'],
|
|
72
|
+
hash: row['hash'],
|
|
73
|
+
scannedAt: row['scanned_at'],
|
|
74
|
+
}));
|
|
75
|
+
}
|
|
76
|
+
export async function removeGraph(db, project, files) {
|
|
77
|
+
const scoped = files && files.length > 0;
|
|
78
|
+
const where = scoped
|
|
79
|
+
? `project = ? AND file IN (${files.map(() => '?').join(',')})`
|
|
80
|
+
: 'project = ?';
|
|
81
|
+
const params = scoped ? [project, ...files] : [project];
|
|
82
|
+
const count = db
|
|
83
|
+
.prepare(`SELECT COUNT(*) AS n FROM symbols WHERE ${where}`)
|
|
84
|
+
.get(...params);
|
|
85
|
+
db.exec('BEGIN');
|
|
86
|
+
try {
|
|
87
|
+
db.prepare(`DELETE FROM symbols WHERE ${where}`).run(...params);
|
|
88
|
+
db.prepare(`DELETE FROM symbol_edges WHERE ${where}`).run(...params);
|
|
89
|
+
const cacheWhere = scoped
|
|
90
|
+
? `project = ? AND path IN (${files.map(() => '?').join(',')})`
|
|
91
|
+
: 'project = ?';
|
|
92
|
+
db.prepare(`DELETE FROM scanned_files WHERE ${cacheWhere}`).run(...params);
|
|
93
|
+
db.exec('COMMIT');
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
db.exec('ROLLBACK');
|
|
97
|
+
throw error;
|
|
98
|
+
}
|
|
99
|
+
return count?.n ?? 0;
|
|
100
|
+
}
|
|
@@ -1,14 +1,6 @@
|
|
|
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
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Zero-config default backend, built on Node's own `node:sqlite`.
|
|
5
|
-
*
|
|
6
|
-
* Using the builtin rather than better-sqlite3 means no node-gyp, no prebuilt
|
|
7
|
-
* binary to mismatch, and no install step that can fail on a user's machine.
|
|
8
|
-
* Keyword search is FTS5; vectors are scored by exact brute-force cosine,
|
|
9
|
-
* which stays comfortably fast past the ~100k observations one developer
|
|
10
|
-
* accumulates and avoids an approximate index that would need tuning.
|
|
11
|
-
*/
|
|
3
|
+
export declare const HERE: string;
|
|
12
4
|
export declare class SqliteStore implements MemoryStore {
|
|
13
5
|
private readonly db;
|
|
14
6
|
readonly kind = "sqlite";
|
|
@@ -28,5 +20,15 @@ export declare class SqliteStore implements MemoryStore {
|
|
|
28
20
|
inventory(): Promise<string[]>;
|
|
29
21
|
searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
30
22
|
searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
23
|
+
closeObservations(ids: string[]): Promise<number>;
|
|
31
24
|
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
25
|
+
upsertGraph(scan: {
|
|
26
|
+
symbols: CodeSymbol[];
|
|
27
|
+
edges: CodeEdge[];
|
|
28
|
+
files: ScannedFile[];
|
|
29
|
+
}): Promise<void>;
|
|
30
|
+
findSymbols(filter: SymbolFilter): Promise<CodeSymbol[]>;
|
|
31
|
+
findEdges(filter: EdgeFilter): Promise<CodeEdge[]>;
|
|
32
|
+
scannedFiles(project: string): Promise<ScannedFile[]>;
|
|
33
|
+
removeGraph(project: string, files?: string[]): Promise<number>;
|
|
32
34
|
}
|