claude-db 0.1.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/LICENSE +201 -0
- package/README.md +306 -0
- package/dist/capture/command.d.ts +20 -0
- package/dist/capture/command.js +81 -0
- package/dist/capture/flush.d.ts +20 -0
- package/dist/capture/flush.js +102 -0
- package/dist/capture/identity.d.ts +13 -0
- package/dist/capture/identity.js +26 -0
- package/dist/capture/index.d.ts +7 -0
- package/dist/capture/index.js +5 -0
- package/dist/capture/transcript.d.ts +41 -0
- package/dist/capture/transcript.js +160 -0
- package/dist/capture/turn-extractor.d.ts +13 -0
- package/dist/capture/turn-extractor.js +135 -0
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +349 -0
- package/dist/cli/install.d.ts +40 -0
- package/dist/cli/install.js +156 -0
- package/dist/config/index.d.ts +3 -0
- package/dist/config/index.js +2 -0
- package/dist/config/load.d.ts +11 -0
- package/dist/config/load.js +31 -0
- package/dist/config/schema.d.ts +28 -0
- package/dist/config/schema.js +66 -0
- package/dist/context.d.ts +22 -0
- package/dist/context.js +25 -0
- package/dist/embed/builtin.d.ts +20 -0
- package/dist/embed/builtin.js +66 -0
- package/dist/embed/embedder.d.ts +27 -0
- package/dist/embed/embedder.js +18 -0
- package/dist/embed/index.d.ts +15 -0
- package/dist/embed/index.js +39 -0
- package/dist/embed/local.d.ts +17 -0
- package/dist/embed/local.js +41 -0
- package/dist/hooks/payload.d.ts +21 -0
- package/dist/hooks/payload.js +33 -0
- package/dist/hooks/relevance.d.ts +20 -0
- package/dist/hooks/relevance.js +84 -0
- package/dist/hooks/session-end.d.ts +2 -0
- package/dist/hooks/session-end.js +35 -0
- package/dist/hooks/session-start.d.ts +2 -0
- package/dist/hooks/session-start.js +46 -0
- package/dist/hooks/user-prompt.d.ts +2 -0
- package/dist/hooks/user-prompt.js +56 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +5 -0
- package/dist/mcp/server.d.ts +8 -0
- package/dist/mcp/server.js +87 -0
- package/dist/search/index.d.ts +2 -0
- package/dist/search/index.js +2 -0
- package/dist/search/rank.d.ts +17 -0
- package/dist/search/rank.js +43 -0
- package/dist/search/service.d.ts +44 -0
- package/dist/search/service.js +78 -0
- package/dist/store/adapter.d.ts +47 -0
- package/dist/store/adapter.js +1 -0
- package/dist/store/index.d.ts +14 -0
- package/dist/store/index.js +39 -0
- package/dist/store/mongo/driver.d.ts +44 -0
- package/dist/store/mongo/driver.js +28 -0
- package/dist/store/mongo/index.d.ts +36 -0
- package/dist/store/mongo/index.js +275 -0
- package/dist/store/postgres/index.d.ts +30 -0
- package/dist/store/postgres/index.js +289 -0
- package/dist/store/sqlite/index.d.ts +30 -0
- package/dist/store/sqlite/index.js +309 -0
- package/dist/store/sqlite/schema.sql +61 -0
- package/dist/types.d.ts +55 -0
- package/dist/types.js +5 -0
- package/dist/util/project.d.ts +14 -0
- package/dist/util/project.js +24 -0
- package/dist/util/scope.d.ts +10 -0
- package/dist/util/scope.js +17 -0
- package/dist/util/shortid.d.ts +11 -0
- package/dist/util/shortid.js +36 -0
- package/dist/util/vector.d.ts +5 -0
- package/dist/util/vector.js +17 -0
- package/dist/util/warnings.d.ts +12 -0
- package/dist/util/warnings.js +27 -0
- package/package.json +96 -0
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { importMongo } from './driver.js';
|
|
2
|
+
import { cosine } from '../../util/vector.js';
|
|
3
|
+
import { partitionIds } from '../../util/shortid.js';
|
|
4
|
+
/**
|
|
5
|
+
* Works against any MongoDB: Atlas, self-hosted, or a local container.
|
|
6
|
+
*
|
|
7
|
+
* Keyword search uses a compound text index, available in every deployment.
|
|
8
|
+
* Vector search prefers an Atlas `$vectorSearch` index when one exists and
|
|
9
|
+
* otherwise falls back to scoring candidates in process, so a plain
|
|
10
|
+
* `mongodb://localhost` connection string still gets semantic recall.
|
|
11
|
+
*/
|
|
12
|
+
export class MongoStore {
|
|
13
|
+
client;
|
|
14
|
+
db;
|
|
15
|
+
sessions;
|
|
16
|
+
observations;
|
|
17
|
+
kind = 'mongodb';
|
|
18
|
+
/** Resolved on first vector query, then cached for the process lifetime. */
|
|
19
|
+
atlasVectorIndex = null;
|
|
20
|
+
constructor(client, db, sessions, observations) {
|
|
21
|
+
this.client = client;
|
|
22
|
+
this.db = db;
|
|
23
|
+
this.sessions = sessions;
|
|
24
|
+
this.observations = observations;
|
|
25
|
+
}
|
|
26
|
+
static async create(uri) {
|
|
27
|
+
const mongo = await importMongo();
|
|
28
|
+
const client = new mongo.MongoClient(uri);
|
|
29
|
+
await client.connect();
|
|
30
|
+
// Honour a database name in the URI, fall back to a sensible default.
|
|
31
|
+
const db = client.db(databaseNameFrom(uri) ?? 'claude_memory_db');
|
|
32
|
+
return new MongoStore(client, db, db.collection('sessions'), db.collection('observations'));
|
|
33
|
+
}
|
|
34
|
+
async init() {
|
|
35
|
+
await this.sessions.createIndex({ project: 1, startedAt: -1 });
|
|
36
|
+
await this.observations.createIndex({ project: 1, createdAt: -1 });
|
|
37
|
+
await this.observations.createIndex({ project: 1, kind: 1, createdAt: -1 });
|
|
38
|
+
await this.observations.createIndex({ title: 'text', body: 'text', tags: 'text' }, { name: 'memory_text', weights: { title: 10, tags: 5, body: 1 } });
|
|
39
|
+
}
|
|
40
|
+
async close() {
|
|
41
|
+
await this.client.close();
|
|
42
|
+
}
|
|
43
|
+
async ping() {
|
|
44
|
+
const res = await this.db.command({ ping: 1 });
|
|
45
|
+
return res['ok'] === 1;
|
|
46
|
+
}
|
|
47
|
+
async upsertSession(session) {
|
|
48
|
+
const set = {
|
|
49
|
+
project: session.project,
|
|
50
|
+
startedAt: session.startedAt,
|
|
51
|
+
};
|
|
52
|
+
if (session.endedAt !== undefined)
|
|
53
|
+
set.endedAt = session.endedAt;
|
|
54
|
+
if (session.summary !== undefined)
|
|
55
|
+
set.summary = session.summary;
|
|
56
|
+
await this.sessions.updateOne({ _id: session.id }, { $set: set }, { upsert: true });
|
|
57
|
+
}
|
|
58
|
+
async getSession(id) {
|
|
59
|
+
const doc = await this.sessions.findOne({ _id: id });
|
|
60
|
+
return doc ? toSession(doc) : null;
|
|
61
|
+
}
|
|
62
|
+
async recentSessions(project, limit) {
|
|
63
|
+
const docs = await this.sessions
|
|
64
|
+
.find({ project, summary: { $type: 'string' } })
|
|
65
|
+
.sort({ startedAt: -1 })
|
|
66
|
+
.limit(limit)
|
|
67
|
+
.toArray();
|
|
68
|
+
return docs.map(toSession);
|
|
69
|
+
}
|
|
70
|
+
async insertObservations(observations) {
|
|
71
|
+
if (observations.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
await this.observations.bulkWrite(observations.map((obs) => ({
|
|
74
|
+
replaceOne: {
|
|
75
|
+
filter: { _id: obs.id },
|
|
76
|
+
replacement: toDoc(obs),
|
|
77
|
+
upsert: true,
|
|
78
|
+
},
|
|
79
|
+
})), { ordered: false });
|
|
80
|
+
}
|
|
81
|
+
async getObservations(ids) {
|
|
82
|
+
if (ids.length === 0)
|
|
83
|
+
return [];
|
|
84
|
+
const { exact, prefixes } = partitionIds(ids);
|
|
85
|
+
const or = [];
|
|
86
|
+
if (exact.length > 0)
|
|
87
|
+
or.push({ _id: { $in: exact } });
|
|
88
|
+
for (const prefix of prefixes) {
|
|
89
|
+
// Anchored regex on _id is index-eligible in MongoDB.
|
|
90
|
+
or.push({ _id: { $regex: `^${escapeRegex(prefix)}` } });
|
|
91
|
+
}
|
|
92
|
+
const docs = await this.observations
|
|
93
|
+
.find({ $or: or })
|
|
94
|
+
.toArray();
|
|
95
|
+
return docs.map(toObservation);
|
|
96
|
+
}
|
|
97
|
+
async clear(project) {
|
|
98
|
+
const filter = project ? { project } : {};
|
|
99
|
+
const docs = await this.observations.find(filter, { projection: { _id: 1 } }).toArray();
|
|
100
|
+
await this.observations.deleteMany(filter);
|
|
101
|
+
await this.sessions.deleteMany(filter);
|
|
102
|
+
return docs.length;
|
|
103
|
+
}
|
|
104
|
+
async listProjects() {
|
|
105
|
+
const rows = await this.observations
|
|
106
|
+
.aggregate([
|
|
107
|
+
{ $group: { _id: '$project', n: { $sum: 1 }, last: { $max: '$createdAt' } } },
|
|
108
|
+
{ $sort: { last: -1 } },
|
|
109
|
+
])
|
|
110
|
+
.toArray();
|
|
111
|
+
return rows.map((row) => ({
|
|
112
|
+
project: row._id,
|
|
113
|
+
observations: row.n,
|
|
114
|
+
lastActive: row.last,
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
async searchKeyword(query) {
|
|
118
|
+
const filter = {
|
|
119
|
+
$text: { $search: query.text },
|
|
120
|
+
...this.scopeFilter(query),
|
|
121
|
+
};
|
|
122
|
+
const docs = await this.observations
|
|
123
|
+
.find(filter, { projection: { score: { $meta: 'textScore' }, body: 0, embedding: 0 } })
|
|
124
|
+
.sort({ score: { $meta: 'textScore' } })
|
|
125
|
+
.limit(query.limit)
|
|
126
|
+
.toArray();
|
|
127
|
+
return docs.map((doc) => toIndexEntry(doc, doc['score'] ?? 0));
|
|
128
|
+
}
|
|
129
|
+
async searchVector(vector, query) {
|
|
130
|
+
if (this.atlasVectorIndex === null) {
|
|
131
|
+
this.atlasVectorIndex = await this.hasAtlasVectorIndex();
|
|
132
|
+
}
|
|
133
|
+
if (this.atlasVectorIndex) {
|
|
134
|
+
const docs = await this.observations
|
|
135
|
+
.aggregate([
|
|
136
|
+
{
|
|
137
|
+
$vectorSearch: {
|
|
138
|
+
index: 'memory_vector',
|
|
139
|
+
path: 'embedding',
|
|
140
|
+
queryVector: vector,
|
|
141
|
+
numCandidates: Math.max(query.limit * 10, 100),
|
|
142
|
+
limit: query.limit,
|
|
143
|
+
filter: this.scopeFilter(query),
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
$project: {
|
|
148
|
+
kind: 1, title: 1, project: 1, createdAt: 1,
|
|
149
|
+
score: { $meta: 'vectorSearchScore' },
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
])
|
|
153
|
+
.toArray();
|
|
154
|
+
return docs.map((doc) => toIndexEntry(doc, doc.score));
|
|
155
|
+
}
|
|
156
|
+
// Portable path: score candidates in process. Bounded by the scope filter,
|
|
157
|
+
// which in practice narrows to a single project.
|
|
158
|
+
const docs = await this.observations
|
|
159
|
+
.find({ embedding: { $exists: true }, ...this.scopeFilter(query) }, { projection: { kind: 1, title: 1, project: 1, createdAt: 1, embedding: 1 } })
|
|
160
|
+
.sort({ createdAt: -1 })
|
|
161
|
+
.limit(query.maxScanCandidates ?? 25000)
|
|
162
|
+
.toArray();
|
|
163
|
+
return docs
|
|
164
|
+
.map((doc) => toIndexEntry(doc, doc.embedding ? cosine(vector, doc.embedding) : 0))
|
|
165
|
+
.sort((a, b) => b.score - a.score)
|
|
166
|
+
.slice(0, query.limit);
|
|
167
|
+
}
|
|
168
|
+
async timeline(query) {
|
|
169
|
+
const anchor = await this.observations.findOne({ _id: query.observationId }, { projection: { project: 1, createdAt: 1 } });
|
|
170
|
+
if (!anchor)
|
|
171
|
+
return [];
|
|
172
|
+
const projection = { kind: 1, title: 1, project: 1, createdAt: 1 };
|
|
173
|
+
const [before, after] = await Promise.all([
|
|
174
|
+
this.observations
|
|
175
|
+
.find({ project: anchor.project, createdAt: { $lte: anchor.createdAt } }, { projection })
|
|
176
|
+
.sort({ createdAt: -1 })
|
|
177
|
+
.limit(query.before + 1)
|
|
178
|
+
.toArray(),
|
|
179
|
+
this.observations
|
|
180
|
+
.find({ project: anchor.project, createdAt: { $gt: anchor.createdAt } }, { projection })
|
|
181
|
+
.sort({ createdAt: 1 })
|
|
182
|
+
.limit(query.after)
|
|
183
|
+
.toArray(),
|
|
184
|
+
]);
|
|
185
|
+
return [...before.reverse(), ...after].map((doc) => toIndexEntry(doc, 0));
|
|
186
|
+
}
|
|
187
|
+
scopeFilter(query) {
|
|
188
|
+
const filter = {};
|
|
189
|
+
if (query.project)
|
|
190
|
+
filter['project'] = query.project;
|
|
191
|
+
if (query.kind)
|
|
192
|
+
filter['kind'] = query.kind;
|
|
193
|
+
if (query.since !== undefined || query.until !== undefined) {
|
|
194
|
+
const range = {};
|
|
195
|
+
if (query.since !== undefined)
|
|
196
|
+
range['$gte'] = query.since;
|
|
197
|
+
if (query.until !== undefined)
|
|
198
|
+
range['$lte'] = query.until;
|
|
199
|
+
filter['createdAt'] = range;
|
|
200
|
+
}
|
|
201
|
+
return filter;
|
|
202
|
+
}
|
|
203
|
+
async hasAtlasVectorIndex() {
|
|
204
|
+
try {
|
|
205
|
+
const indexes = await this.observations.listSearchIndexes().toArray();
|
|
206
|
+
return indexes.some((index) => index['name'] === 'memory_vector');
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
// listSearchIndexes is Atlas-only; anything else means no vector index.
|
|
210
|
+
return false;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
function escapeRegex(text) {
|
|
215
|
+
return text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
216
|
+
}
|
|
217
|
+
function databaseNameFrom(uri) {
|
|
218
|
+
const match = /\/\/[^/]+\/([^?]+)/.exec(uri);
|
|
219
|
+
const name = match?.[1]?.trim();
|
|
220
|
+
return name && name.length > 0 ? name : null;
|
|
221
|
+
}
|
|
222
|
+
function toSession(doc) {
|
|
223
|
+
const session = {
|
|
224
|
+
id: doc._id,
|
|
225
|
+
project: doc.project,
|
|
226
|
+
startedAt: doc.startedAt,
|
|
227
|
+
};
|
|
228
|
+
if (doc.endedAt !== undefined)
|
|
229
|
+
session.endedAt = doc.endedAt;
|
|
230
|
+
if (doc.summary !== undefined)
|
|
231
|
+
session.summary = doc.summary;
|
|
232
|
+
return session;
|
|
233
|
+
}
|
|
234
|
+
function toDoc(obs) {
|
|
235
|
+
const doc = {
|
|
236
|
+
_id: obs.id,
|
|
237
|
+
sessionId: obs.sessionId,
|
|
238
|
+
project: obs.project,
|
|
239
|
+
kind: obs.kind,
|
|
240
|
+
title: obs.title,
|
|
241
|
+
body: obs.body,
|
|
242
|
+
files: obs.files,
|
|
243
|
+
tags: obs.tags,
|
|
244
|
+
createdAt: obs.createdAt,
|
|
245
|
+
};
|
|
246
|
+
if (obs.embedding)
|
|
247
|
+
doc.embedding = obs.embedding;
|
|
248
|
+
return doc;
|
|
249
|
+
}
|
|
250
|
+
function toObservation(doc) {
|
|
251
|
+
const obs = {
|
|
252
|
+
id: doc._id,
|
|
253
|
+
sessionId: doc.sessionId,
|
|
254
|
+
project: doc.project,
|
|
255
|
+
kind: doc.kind,
|
|
256
|
+
title: doc.title,
|
|
257
|
+
body: doc.body,
|
|
258
|
+
files: doc.files ?? [],
|
|
259
|
+
tags: doc.tags ?? [],
|
|
260
|
+
createdAt: doc.createdAt,
|
|
261
|
+
};
|
|
262
|
+
if (doc.embedding)
|
|
263
|
+
obs.embedding = doc.embedding;
|
|
264
|
+
return obs;
|
|
265
|
+
}
|
|
266
|
+
function toIndexEntry(doc, score) {
|
|
267
|
+
return {
|
|
268
|
+
id: doc._id,
|
|
269
|
+
kind: doc.kind,
|
|
270
|
+
title: doc.title,
|
|
271
|
+
project: doc.project,
|
|
272
|
+
createdAt: doc.createdAt,
|
|
273
|
+
score,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MemoryStore, ProjectSummary } from '../adapter.js';
|
|
2
|
+
import type { Observation, ObservationIndexEntry, SearchQuery, Session, TimelineQuery } from '../../types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Postgres backend. Uses tsvector for keyword search, and pgvector for
|
|
5
|
+
* embeddings when the extension is available. If `CREATE EXTENSION vector`
|
|
6
|
+
* is not permitted (common on locked-down managed instances) the store still
|
|
7
|
+
* works, minus semantic recall, which the search service handles gracefully.
|
|
8
|
+
*/
|
|
9
|
+
export declare class PostgresStore implements MemoryStore {
|
|
10
|
+
private readonly pool;
|
|
11
|
+
readonly kind = "postgres";
|
|
12
|
+
private vectorEnabled;
|
|
13
|
+
private constructor();
|
|
14
|
+
static create(uri: string): Promise<PostgresStore>;
|
|
15
|
+
init(): Promise<void>;
|
|
16
|
+
close(): Promise<void>;
|
|
17
|
+
ping(): Promise<boolean>;
|
|
18
|
+
upsertSession(session: Session): Promise<void>;
|
|
19
|
+
getSession(id: string): Promise<Session | null>;
|
|
20
|
+
recentSessions(project: string, limit: number): Promise<Session[]>;
|
|
21
|
+
insertObservations(observations: Observation[]): Promise<void>;
|
|
22
|
+
getObservations(ids: string[]): Promise<Observation[]>;
|
|
23
|
+
clear(project?: string): Promise<number>;
|
|
24
|
+
listProjects(): Promise<ProjectSummary[]>;
|
|
25
|
+
searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
26
|
+
searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
27
|
+
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
28
|
+
private appendScope;
|
|
29
|
+
private tryEnableVector;
|
|
30
|
+
}
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
import { partitionIds } from '../../util/shortid.js';
|
|
2
|
+
/**
|
|
3
|
+
* Postgres backend. Uses tsvector for keyword search, and pgvector for
|
|
4
|
+
* embeddings when the extension is available. If `CREATE EXTENSION vector`
|
|
5
|
+
* is not permitted (common on locked-down managed instances) the store still
|
|
6
|
+
* works, minus semantic recall, which the search service handles gracefully.
|
|
7
|
+
*/
|
|
8
|
+
export class PostgresStore {
|
|
9
|
+
pool;
|
|
10
|
+
kind = 'postgres';
|
|
11
|
+
vectorEnabled = false;
|
|
12
|
+
constructor(pool) {
|
|
13
|
+
this.pool = pool;
|
|
14
|
+
}
|
|
15
|
+
static async create(uri) {
|
|
16
|
+
const pg = await importPg();
|
|
17
|
+
const pool = new pg.Pool({ connectionString: uri });
|
|
18
|
+
return new PostgresStore(pool);
|
|
19
|
+
}
|
|
20
|
+
async init() {
|
|
21
|
+
this.vectorEnabled = await this.tryEnableVector();
|
|
22
|
+
await this.pool.query(`
|
|
23
|
+
CREATE TABLE IF NOT EXISTS sessions (
|
|
24
|
+
id TEXT PRIMARY KEY,
|
|
25
|
+
project TEXT NOT NULL,
|
|
26
|
+
started_at BIGINT NOT NULL,
|
|
27
|
+
ended_at BIGINT,
|
|
28
|
+
summary TEXT
|
|
29
|
+
);
|
|
30
|
+
CREATE INDEX IF NOT EXISTS idx_sessions_project
|
|
31
|
+
ON sessions(project, started_at DESC);
|
|
32
|
+
|
|
33
|
+
CREATE TABLE IF NOT EXISTS observations (
|
|
34
|
+
id TEXT PRIMARY KEY,
|
|
35
|
+
session_id TEXT NOT NULL,
|
|
36
|
+
project TEXT NOT NULL,
|
|
37
|
+
kind TEXT NOT NULL,
|
|
38
|
+
title TEXT NOT NULL,
|
|
39
|
+
body TEXT NOT NULL,
|
|
40
|
+
files JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
41
|
+
tags JSONB NOT NULL DEFAULT '[]'::jsonb,
|
|
42
|
+
created_at BIGINT NOT NULL,
|
|
43
|
+
tsv TSVECTOR GENERATED ALWAYS AS (
|
|
44
|
+
setweight(to_tsvector('english', coalesce(title, '')), 'A') ||
|
|
45
|
+
setweight(to_tsvector('english', coalesce(body, '')), 'B')
|
|
46
|
+
) STORED
|
|
47
|
+
);
|
|
48
|
+
CREATE INDEX IF NOT EXISTS idx_obs_tsv ON observations USING GIN(tsv);
|
|
49
|
+
CREATE INDEX IF NOT EXISTS idx_obs_project_time
|
|
50
|
+
ON observations(project, created_at DESC);
|
|
51
|
+
`);
|
|
52
|
+
if (this.vectorEnabled) {
|
|
53
|
+
await this.pool.query(`ALTER TABLE observations ADD COLUMN IF NOT EXISTS embedding vector(384)`);
|
|
54
|
+
await this.pool.query(`CREATE INDEX IF NOT EXISTS idx_obs_embedding
|
|
55
|
+
ON observations USING hnsw (embedding vector_cosine_ops)`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
async close() {
|
|
59
|
+
await this.pool.end();
|
|
60
|
+
}
|
|
61
|
+
async ping() {
|
|
62
|
+
const res = await this.pool.query('SELECT 1 AS ok');
|
|
63
|
+
return res.rowCount === 1;
|
|
64
|
+
}
|
|
65
|
+
async upsertSession(session) {
|
|
66
|
+
await this.pool.query(`INSERT INTO sessions (id, project, started_at, ended_at, summary)
|
|
67
|
+
VALUES ($1, $2, $3, $4, $5)
|
|
68
|
+
ON CONFLICT (id) DO UPDATE SET
|
|
69
|
+
ended_at = COALESCE(EXCLUDED.ended_at, sessions.ended_at),
|
|
70
|
+
summary = COALESCE(EXCLUDED.summary, sessions.summary)`, [
|
|
71
|
+
session.id,
|
|
72
|
+
session.project,
|
|
73
|
+
session.startedAt,
|
|
74
|
+
session.endedAt ?? null,
|
|
75
|
+
session.summary ?? null,
|
|
76
|
+
]);
|
|
77
|
+
}
|
|
78
|
+
async getSession(id) {
|
|
79
|
+
const res = await this.pool.query('SELECT * FROM sessions WHERE id = $1', [id]);
|
|
80
|
+
const row = res.rows[0];
|
|
81
|
+
return row ? toSession(row) : null;
|
|
82
|
+
}
|
|
83
|
+
async recentSessions(project, limit) {
|
|
84
|
+
const res = await this.pool.query(`SELECT * FROM sessions
|
|
85
|
+
WHERE project = $1 AND summary IS NOT NULL
|
|
86
|
+
ORDER BY started_at DESC LIMIT $2`, [project, limit]);
|
|
87
|
+
return res.rows.map(toSession);
|
|
88
|
+
}
|
|
89
|
+
async insertObservations(observations) {
|
|
90
|
+
if (observations.length === 0)
|
|
91
|
+
return;
|
|
92
|
+
const client = await this.pool.connect();
|
|
93
|
+
try {
|
|
94
|
+
await client.query('BEGIN');
|
|
95
|
+
for (const obs of observations) {
|
|
96
|
+
const columns = `id, session_id, project, kind, title, body, files, tags, created_at` +
|
|
97
|
+
(this.vectorEnabled && obs.embedding ? ', embedding' : '');
|
|
98
|
+
const values = [
|
|
99
|
+
obs.id, obs.sessionId, obs.project, obs.kind, obs.title, obs.body,
|
|
100
|
+
JSON.stringify(obs.files), JSON.stringify(obs.tags), obs.createdAt,
|
|
101
|
+
];
|
|
102
|
+
if (this.vectorEnabled && obs.embedding) {
|
|
103
|
+
values.push(`[${obs.embedding.join(',')}]`);
|
|
104
|
+
}
|
|
105
|
+
const holes = values.map((_, i) => `$${i + 1}`).join(', ');
|
|
106
|
+
await client.query(`INSERT INTO observations (${columns}) VALUES (${holes})
|
|
107
|
+
ON CONFLICT (id) DO NOTHING`, values);
|
|
108
|
+
}
|
|
109
|
+
await client.query('COMMIT');
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
await client.query('ROLLBACK');
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
finally {
|
|
116
|
+
client.release();
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
async getObservations(ids) {
|
|
120
|
+
if (ids.length === 0)
|
|
121
|
+
return [];
|
|
122
|
+
const { exact, prefixes } = partitionIds(ids);
|
|
123
|
+
const clauses = [];
|
|
124
|
+
const values = [];
|
|
125
|
+
if (exact.length > 0) {
|
|
126
|
+
values.push(exact);
|
|
127
|
+
clauses.push(`id = ANY($${values.length}::text[])`);
|
|
128
|
+
}
|
|
129
|
+
for (const prefix of prefixes) {
|
|
130
|
+
// Left-anchored LIKE uses the btree index on the text primary key.
|
|
131
|
+
values.push(`${prefix.replace(/[%_]/g, '')}%`);
|
|
132
|
+
clauses.push(`id LIKE $${values.length}`);
|
|
133
|
+
}
|
|
134
|
+
const res = await this.pool.query(`SELECT * FROM observations WHERE ${clauses.join(' OR ')}`, values);
|
|
135
|
+
return res.rows.map(toObservation);
|
|
136
|
+
}
|
|
137
|
+
async clear(project) {
|
|
138
|
+
const where = project ? 'WHERE project = $1' : '';
|
|
139
|
+
const values = project ? [project] : [];
|
|
140
|
+
const client = await this.pool.connect();
|
|
141
|
+
try {
|
|
142
|
+
await client.query('BEGIN');
|
|
143
|
+
const counted = await client.query(`SELECT COUNT(*)::int AS n FROM observations ${where}`, values);
|
|
144
|
+
await client.query(`DELETE FROM observations ${where}`, values);
|
|
145
|
+
await client.query(`DELETE FROM sessions ${where}`, values);
|
|
146
|
+
await client.query('COMMIT');
|
|
147
|
+
return Number(counted.rows[0]?.['n'] ?? 0);
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
await client.query('ROLLBACK');
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
finally {
|
|
154
|
+
client.release();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async listProjects() {
|
|
158
|
+
const res = await this.pool.query(`SELECT project, COUNT(*)::int AS n, MAX(created_at) AS last
|
|
159
|
+
FROM observations GROUP BY project ORDER BY last DESC`);
|
|
160
|
+
return res.rows.map((row) => ({
|
|
161
|
+
project: row['project'],
|
|
162
|
+
observations: Number(row['n']),
|
|
163
|
+
lastActive: Number(row['last']),
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
async searchKeyword(query) {
|
|
167
|
+
const conditions = ['tsv @@ plainto_tsquery($1)'];
|
|
168
|
+
const values = [query.text];
|
|
169
|
+
this.appendScope(query, conditions, values);
|
|
170
|
+
values.push(query.limit);
|
|
171
|
+
const res = await this.pool.query(`SELECT id, kind, title, project, created_at,
|
|
172
|
+
ts_rank(tsv, plainto_tsquery($1)) AS score
|
|
173
|
+
FROM observations
|
|
174
|
+
WHERE ${conditions.join(' AND ')}
|
|
175
|
+
ORDER BY score DESC
|
|
176
|
+
LIMIT $${values.length}`, values);
|
|
177
|
+
return res.rows.map(toIndexEntry);
|
|
178
|
+
}
|
|
179
|
+
async searchVector(vector, query) {
|
|
180
|
+
if (!this.vectorEnabled)
|
|
181
|
+
return [];
|
|
182
|
+
const conditions = ['embedding IS NOT NULL'];
|
|
183
|
+
const values = [`[${vector.join(',')}]`];
|
|
184
|
+
this.appendScope(query, conditions, values);
|
|
185
|
+
values.push(query.limit);
|
|
186
|
+
const res = await this.pool.query(`SELECT id, kind, title, project, created_at,
|
|
187
|
+
1 - (embedding <=> $1::vector) AS score
|
|
188
|
+
FROM observations
|
|
189
|
+
WHERE ${conditions.join(' AND ')}
|
|
190
|
+
ORDER BY embedding <=> $1::vector
|
|
191
|
+
LIMIT $${values.length}`, values);
|
|
192
|
+
return res.rows.map(toIndexEntry);
|
|
193
|
+
}
|
|
194
|
+
async timeline(query) {
|
|
195
|
+
const anchorRes = await this.pool.query('SELECT project, created_at FROM observations WHERE id = $1', [query.observationId]);
|
|
196
|
+
const anchor = anchorRes.rows[0];
|
|
197
|
+
if (!anchor)
|
|
198
|
+
return [];
|
|
199
|
+
const res = await this.pool.query(`(SELECT id, kind, title, project, created_at, 0 AS score
|
|
200
|
+
FROM observations
|
|
201
|
+
WHERE project = $1 AND created_at <= $2
|
|
202
|
+
ORDER BY created_at DESC LIMIT $3)
|
|
203
|
+
UNION
|
|
204
|
+
(SELECT id, kind, title, project, created_at, 0 AS score
|
|
205
|
+
FROM observations
|
|
206
|
+
WHERE project = $1 AND created_at > $2
|
|
207
|
+
ORDER BY created_at ASC LIMIT $4)
|
|
208
|
+
ORDER BY created_at ASC`, [anchor.project, anchor.created_at, query.before + 1, query.after]);
|
|
209
|
+
return res.rows.map(toIndexEntry);
|
|
210
|
+
}
|
|
211
|
+
appendScope(query, conditions, values) {
|
|
212
|
+
if (query.project) {
|
|
213
|
+
values.push(query.project);
|
|
214
|
+
conditions.push(`project = $${values.length}`);
|
|
215
|
+
}
|
|
216
|
+
if (query.kind) {
|
|
217
|
+
values.push(query.kind);
|
|
218
|
+
conditions.push(`kind = $${values.length}`);
|
|
219
|
+
}
|
|
220
|
+
if (query.since !== undefined) {
|
|
221
|
+
values.push(query.since);
|
|
222
|
+
conditions.push(`created_at >= $${values.length}`);
|
|
223
|
+
}
|
|
224
|
+
if (query.until !== undefined) {
|
|
225
|
+
values.push(query.until);
|
|
226
|
+
conditions.push(`created_at <= $${values.length}`);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
async tryEnableVector() {
|
|
230
|
+
try {
|
|
231
|
+
await this.pool.query('CREATE EXTENSION IF NOT EXISTS vector');
|
|
232
|
+
return true;
|
|
233
|
+
}
|
|
234
|
+
catch {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
async function importPg() {
|
|
240
|
+
try {
|
|
241
|
+
// Bypasses TypeScript's static resolution: `pg` is an optional peer and
|
|
242
|
+
// may legitimately be absent at build time.
|
|
243
|
+
const mod = (await import(/* @vite-ignore */ 'pg'));
|
|
244
|
+
// pg is CommonJS; under NodeNext it may or may not arrive default-wrapped.
|
|
245
|
+
if (mod.default?.Pool)
|
|
246
|
+
return mod.default;
|
|
247
|
+
if (mod.Pool)
|
|
248
|
+
return { Pool: mod.Pool };
|
|
249
|
+
throw new Error('unexpected pg module shape');
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
throw new Error('Postgres driver not installed. Run: npm install pg');
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
function toSession(row) {
|
|
256
|
+
const session = {
|
|
257
|
+
id: row['id'],
|
|
258
|
+
project: row['project'],
|
|
259
|
+
startedAt: Number(row['started_at']),
|
|
260
|
+
};
|
|
261
|
+
if (row['ended_at'] != null)
|
|
262
|
+
session.endedAt = Number(row['ended_at']);
|
|
263
|
+
if (row['summary'] != null)
|
|
264
|
+
session.summary = row['summary'];
|
|
265
|
+
return session;
|
|
266
|
+
}
|
|
267
|
+
function toObservation(row) {
|
|
268
|
+
return {
|
|
269
|
+
id: row['id'],
|
|
270
|
+
sessionId: row['session_id'],
|
|
271
|
+
project: row['project'],
|
|
272
|
+
kind: row['kind'],
|
|
273
|
+
title: row['title'],
|
|
274
|
+
body: row['body'],
|
|
275
|
+
files: row['files'] ?? [],
|
|
276
|
+
tags: row['tags'] ?? [],
|
|
277
|
+
createdAt: Number(row['created_at']),
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function toIndexEntry(row) {
|
|
281
|
+
return {
|
|
282
|
+
id: row['id'],
|
|
283
|
+
kind: row['kind'],
|
|
284
|
+
title: row['title'],
|
|
285
|
+
project: row['project'],
|
|
286
|
+
createdAt: Number(row['created_at']),
|
|
287
|
+
score: Number(row['score'] ?? 0),
|
|
288
|
+
};
|
|
289
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { MemoryStore, ProjectSummary } from '../adapter.js';
|
|
2
|
+
import type { Observation, ObservationIndexEntry, SearchQuery, Session, TimelineQuery } from '../../types.js';
|
|
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
|
+
*/
|
|
12
|
+
export declare class SqliteStore implements MemoryStore {
|
|
13
|
+
private readonly db;
|
|
14
|
+
readonly kind = "sqlite";
|
|
15
|
+
private constructor();
|
|
16
|
+
static create(uri: string): Promise<SqliteStore>;
|
|
17
|
+
init(): Promise<void>;
|
|
18
|
+
close(): Promise<void>;
|
|
19
|
+
ping(): Promise<boolean>;
|
|
20
|
+
upsertSession(session: Session): Promise<void>;
|
|
21
|
+
getSession(id: string): Promise<Session | null>;
|
|
22
|
+
recentSessions(project: string, limit: number): Promise<Session[]>;
|
|
23
|
+
insertObservations(observations: Observation[]): Promise<void>;
|
|
24
|
+
getObservations(ids: string[]): Promise<Observation[]>;
|
|
25
|
+
clear(project?: string): Promise<number>;
|
|
26
|
+
listProjects(): Promise<ProjectSummary[]>;
|
|
27
|
+
searchKeyword(query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
28
|
+
searchVector(vector: number[], query: SearchQuery): Promise<ObservationIndexEntry[]>;
|
|
29
|
+
timeline(query: TimelineQuery): Promise<ObservationIndexEntry[]>;
|
|
30
|
+
}
|