cntx-ui 3.0.7 → 3.0.9
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/dist/bin/cntx-ui.js +70 -0
- package/dist/lib/agent-runtime.js +269 -0
- package/dist/lib/agent-tools.js +162 -0
- package/dist/lib/api-router.js +387 -0
- package/dist/lib/bundle-manager.js +236 -0
- package/dist/lib/configuration-manager.js +230 -0
- package/dist/lib/database-manager.js +277 -0
- package/dist/lib/file-system-manager.js +305 -0
- package/dist/lib/function-level-chunker.js +144 -0
- package/dist/lib/heuristics-manager.js +491 -0
- package/dist/lib/mcp-server.js +159 -0
- package/dist/lib/mcp-transport.js +10 -0
- package/dist/lib/semantic-splitter.js +335 -0
- package/dist/lib/simple-vector-store.js +98 -0
- package/dist/lib/treesitter-semantic-chunker.js +277 -0
- package/dist/lib/websocket-manager.js +268 -0
- package/dist/server.js +225 -0
- package/package.json +18 -8
- package/bin/cntx-ui-mcp.sh +0 -3
- package/bin/cntx-ui.js +0 -123
- package/lib/agent-runtime.js +0 -371
- package/lib/agent-tools.js +0 -370
- package/lib/api-router.js +0 -1026
- package/lib/bundle-manager.js +0 -326
- package/lib/configuration-manager.js +0 -760
- package/lib/database-manager.js +0 -397
- package/lib/file-system-manager.js +0 -489
- package/lib/function-level-chunker.js +0 -406
- package/lib/heuristics-manager.js +0 -529
- package/lib/mcp-server.js +0 -1380
- package/lib/mcp-transport.js +0 -97
- package/lib/semantic-splitter.js +0 -304
- package/lib/simple-vector-store.js +0 -108
- package/lib/treesitter-semantic-chunker.js +0 -1485
- package/lib/websocket-manager.js +0 -470
- package/server.js +0 -687
package/lib/database-manager.js
DELETED
|
@@ -1,397 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Database Manager for cntx-ui
|
|
3
|
-
* Handles SQLite operations for bundle and file data
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
import Database from 'better-sqlite3';
|
|
7
|
-
import { join } from 'path';
|
|
8
|
-
import { statSync } from 'fs';
|
|
9
|
-
|
|
10
|
-
export default class DatabaseManager {
|
|
11
|
-
constructor(cntxDir, options = {}) {
|
|
12
|
-
this.cntxDir = cntxDir;
|
|
13
|
-
this.verbose = options.verbose || false;
|
|
14
|
-
this.dbPath = join(cntxDir, 'bundles.db');
|
|
15
|
-
|
|
16
|
-
try {
|
|
17
|
-
this.db = new Database(this.dbPath);
|
|
18
|
-
this.initSchema();
|
|
19
|
-
if (this.verbose) {
|
|
20
|
-
console.log(`📊 SQLite database initialized: ${this.dbPath}`);
|
|
21
|
-
}
|
|
22
|
-
} catch (error) {
|
|
23
|
-
console.error('Failed to initialize SQLite database:', error.message);
|
|
24
|
-
throw error;
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
initSchema() {
|
|
29
|
-
this.db.exec(`
|
|
30
|
-
-- Existing bundles table
|
|
31
|
-
CREATE TABLE IF NOT EXISTS bundles (
|
|
32
|
-
name TEXT PRIMARY KEY,
|
|
33
|
-
patterns TEXT NOT NULL,
|
|
34
|
-
files TEXT NOT NULL,
|
|
35
|
-
size INTEGER DEFAULT 0,
|
|
36
|
-
file_count INTEGER DEFAULT 0,
|
|
37
|
-
generated_at TEXT,
|
|
38
|
-
changed BOOLEAN DEFAULT FALSE
|
|
39
|
-
);
|
|
40
|
-
|
|
41
|
-
-- Persistent Semantic Chunks
|
|
42
|
-
CREATE TABLE IF NOT EXISTS semantic_chunks (
|
|
43
|
-
id TEXT PRIMARY KEY,
|
|
44
|
-
name TEXT NOT NULL,
|
|
45
|
-
file_path TEXT NOT NULL,
|
|
46
|
-
type TEXT,
|
|
47
|
-
subtype TEXT,
|
|
48
|
-
content TEXT NOT NULL,
|
|
49
|
-
start_line INTEGER,
|
|
50
|
-
complexity_score INTEGER,
|
|
51
|
-
purpose TEXT,
|
|
52
|
-
metadata TEXT, -- JSON string for tags, imports, types, etc.
|
|
53
|
-
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
-- Vector Embeddings (Persistence for RAG)
|
|
57
|
-
CREATE TABLE IF NOT EXISTS vector_embeddings (
|
|
58
|
-
chunk_id TEXT PRIMARY KEY,
|
|
59
|
-
embedding BLOB NOT NULL, -- Stored as Float32Array blob
|
|
60
|
-
model_name TEXT NOT NULL,
|
|
61
|
-
FOREIGN KEY(chunk_id) REFERENCES semantic_chunks(id) ON DELETE CASCADE
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
-- Agent Working Memory & Sessions
|
|
65
|
-
CREATE TABLE IF NOT EXISTS agent_sessions (
|
|
66
|
-
id TEXT PRIMARY KEY,
|
|
67
|
-
title TEXT,
|
|
68
|
-
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
69
|
-
last_active_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
70
|
-
context_summary TEXT -- High-level summary of what was being worked on
|
|
71
|
-
);
|
|
72
|
-
|
|
73
|
-
CREATE TABLE IF NOT EXISTS agent_memory (
|
|
74
|
-
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
75
|
-
session_id TEXT NOT NULL,
|
|
76
|
-
role TEXT NOT NULL, -- 'user' or 'agent'
|
|
77
|
-
content TEXT NOT NULL,
|
|
78
|
-
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
79
|
-
metadata TEXT, -- JSON for tools used, files referenced, etc.
|
|
80
|
-
FOREIGN KEY(session_id) REFERENCES agent_sessions(id) ON DELETE CASCADE
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
CREATE INDEX IF NOT EXISTS idx_bundles_changed ON bundles(changed);
|
|
84
|
-
CREATE INDEX IF NOT EXISTS idx_chunks_file ON semantic_chunks(file_path);
|
|
85
|
-
CREATE INDEX IF NOT EXISTS idx_chunks_purpose ON semantic_chunks(purpose);
|
|
86
|
-
CREATE INDEX IF NOT EXISTS idx_memory_session ON agent_memory(session_id);
|
|
87
|
-
`);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
// Save all bundles from bundleStates Map
|
|
91
|
-
saveBundles(bundleStates) {
|
|
92
|
-
const stmt = this.db.prepare(`
|
|
93
|
-
INSERT OR REPLACE INTO bundles (name, patterns, files, size, file_count, generated_at, changed)
|
|
94
|
-
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
95
|
-
`);
|
|
96
|
-
|
|
97
|
-
const transaction = this.db.transaction(() => {
|
|
98
|
-
for (const [name, bundle] of bundleStates) {
|
|
99
|
-
stmt.run(
|
|
100
|
-
name,
|
|
101
|
-
JSON.stringify(bundle.patterns || []),
|
|
102
|
-
JSON.stringify(bundle.files || []),
|
|
103
|
-
bundle.size || 0,
|
|
104
|
-
bundle.files ? bundle.files.length : 0,
|
|
105
|
-
bundle.generated || null,
|
|
106
|
-
bundle.changed ? 1 : 0
|
|
107
|
-
);
|
|
108
|
-
}
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
try {
|
|
112
|
-
transaction();
|
|
113
|
-
if (this.verbose) {
|
|
114
|
-
console.log(`💾 Saved ${bundleStates.size} bundles to SQLite`);
|
|
115
|
-
}
|
|
116
|
-
} catch (error) {
|
|
117
|
-
console.error('Failed to save bundles to SQLite:', error.message);
|
|
118
|
-
throw error;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
// Load all bundles and return as Map
|
|
123
|
-
loadBundles() {
|
|
124
|
-
try {
|
|
125
|
-
const rows = this.db.prepare('SELECT * FROM bundles').all();
|
|
126
|
-
const bundleStates = new Map();
|
|
127
|
-
|
|
128
|
-
rows.forEach(row => {
|
|
129
|
-
bundleStates.set(row.name, {
|
|
130
|
-
patterns: JSON.parse(row.patterns),
|
|
131
|
-
files: JSON.parse(row.files),
|
|
132
|
-
size: row.size,
|
|
133
|
-
generated: row.generated_at,
|
|
134
|
-
changed: Boolean(row.changed)
|
|
135
|
-
});
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
if (this.verbose) {
|
|
139
|
-
console.log(`📊 Loaded ${bundleStates.size} bundles from SQLite`);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return bundleStates;
|
|
143
|
-
} catch (error) {
|
|
144
|
-
console.error('Failed to load bundles from SQLite:', error.message);
|
|
145
|
-
return new Map();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
// Get single bundle by name
|
|
150
|
-
getBundle(name) {
|
|
151
|
-
try {
|
|
152
|
-
const row = this.db.prepare('SELECT * FROM bundles WHERE name = ?').get(name);
|
|
153
|
-
if (!row) return null;
|
|
154
|
-
|
|
155
|
-
return {
|
|
156
|
-
patterns: JSON.parse(row.patterns),
|
|
157
|
-
files: JSON.parse(row.files),
|
|
158
|
-
size: row.size,
|
|
159
|
-
generated: row.generated_at,
|
|
160
|
-
changed: Boolean(row.changed)
|
|
161
|
-
};
|
|
162
|
-
} catch (error) {
|
|
163
|
-
console.error(`Failed to get bundle ${name}:`, error.message);
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
// Delete bundle
|
|
169
|
-
deleteBundle(name) {
|
|
170
|
-
try {
|
|
171
|
-
const stmt = this.db.prepare('DELETE FROM bundles WHERE name = ?');
|
|
172
|
-
const result = stmt.run(name);
|
|
173
|
-
|
|
174
|
-
if (this.verbose && result.changes > 0) {
|
|
175
|
-
console.log(`🗑️ Deleted bundle: ${name}`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return result.changes > 0;
|
|
179
|
-
} catch (error) {
|
|
180
|
-
console.error(`Failed to delete bundle ${name}:`, error.message);
|
|
181
|
-
return false;
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
// Get all bundle names
|
|
186
|
-
getBundleNames() {
|
|
187
|
-
try {
|
|
188
|
-
const rows = this.db.prepare('SELECT name FROM bundles ORDER BY name').all();
|
|
189
|
-
return rows.map(row => row.name);
|
|
190
|
-
} catch (error) {
|
|
191
|
-
console.error('Failed to get bundle names:', error.message);
|
|
192
|
-
return [];
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// Query interface for database tab
|
|
197
|
-
query(sql) {
|
|
198
|
-
try {
|
|
199
|
-
// Safety: only allow SELECT statements for now
|
|
200
|
-
if (!sql.trim().toLowerCase().startsWith('select')) {
|
|
201
|
-
throw new Error('Only SELECT queries are allowed');
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
const stmt = this.db.prepare(sql);
|
|
205
|
-
return stmt.all();
|
|
206
|
-
} catch (error) {
|
|
207
|
-
console.error('Query failed:', error.message);
|
|
208
|
-
throw error;
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
|
|
212
|
-
// Get database info for debugging
|
|
213
|
-
getInfo() {
|
|
214
|
-
try {
|
|
215
|
-
const bundleCount = this.db.prepare('SELECT COUNT(*) as count FROM bundles').get().count;
|
|
216
|
-
const chunkCount = this.db.prepare('SELECT COUNT(*) as count FROM semantic_chunks').get().count;
|
|
217
|
-
const embeddingCount = this.db.prepare('SELECT COUNT(*) as count FROM vector_embeddings').get().count;
|
|
218
|
-
const sessionCount = this.db.prepare('SELECT COUNT(*) as count FROM agent_sessions').get().count;
|
|
219
|
-
const dbSize = statSync(this.dbPath).size;
|
|
220
|
-
|
|
221
|
-
return {
|
|
222
|
-
path: this.dbPath,
|
|
223
|
-
bundleCount,
|
|
224
|
-
chunkCount,
|
|
225
|
-
embeddingCount,
|
|
226
|
-
sessionCount,
|
|
227
|
-
sizeBytes: dbSize,
|
|
228
|
-
sizeFormatted: (dbSize / 1024).toFixed(1) + ' KB',
|
|
229
|
-
tables: ['bundles', 'semantic_chunks', 'vector_embeddings', 'agent_sessions', 'agent_memory']
|
|
230
|
-
};
|
|
231
|
-
} catch (error) {
|
|
232
|
-
return { error: error.message };
|
|
233
|
-
}
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// Save semantic chunks
|
|
237
|
-
saveChunks(chunks) {
|
|
238
|
-
const stmt = this.db.prepare(`
|
|
239
|
-
INSERT OR REPLACE INTO semantic_chunks (id, name, file_path, type, subtype, content, start_line, complexity_score, purpose, metadata)
|
|
240
|
-
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
241
|
-
`);
|
|
242
|
-
|
|
243
|
-
const transaction = this.db.transaction(() => {
|
|
244
|
-
for (const chunk of chunks) {
|
|
245
|
-
// Generate a stable ID if not provided (org/repo:path:name)
|
|
246
|
-
const id = chunk.id || `${chunk.filePath}:${chunk.name}:${chunk.startLine}`;
|
|
247
|
-
stmt.run(
|
|
248
|
-
id,
|
|
249
|
-
chunk.name,
|
|
250
|
-
chunk.filePath,
|
|
251
|
-
chunk.type || 'unknown',
|
|
252
|
-
chunk.subtype || 'unknown',
|
|
253
|
-
chunk.code || chunk.content || '',
|
|
254
|
-
chunk.startLine || 0,
|
|
255
|
-
chunk.complexity?.score || 0,
|
|
256
|
-
chunk.purpose || 'Utility function',
|
|
257
|
-
JSON.stringify({
|
|
258
|
-
tags: chunk.tags || [],
|
|
259
|
-
businessDomain: chunk.businessDomain || [],
|
|
260
|
-
technicalPatterns: chunk.technicalPatterns || [],
|
|
261
|
-
imports: chunk.includes?.imports || [],
|
|
262
|
-
types: chunk.includes?.types || [],
|
|
263
|
-
bundles: chunk.bundles || []
|
|
264
|
-
})
|
|
265
|
-
);
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
try {
|
|
270
|
-
transaction();
|
|
271
|
-
return true;
|
|
272
|
-
} catch (error) {
|
|
273
|
-
console.error('Failed to save chunks to SQLite:', error.message);
|
|
274
|
-
return false;
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
// Get chunks for a specific file
|
|
279
|
-
getChunksByFile(filePath) {
|
|
280
|
-
try {
|
|
281
|
-
const rows = this.db.prepare('SELECT * FROM semantic_chunks WHERE file_path = ?').all(filePath);
|
|
282
|
-
return rows.map(row => this.mapChunkRow(row));
|
|
283
|
-
} catch (error) {
|
|
284
|
-
return [];
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
// Search chunks by name or purpose
|
|
289
|
-
searchChunks(query) {
|
|
290
|
-
try {
|
|
291
|
-
const rows = this.db.prepare(`
|
|
292
|
-
SELECT * FROM semantic_chunks
|
|
293
|
-
WHERE name LIKE ? OR purpose LIKE ?
|
|
294
|
-
LIMIT 50
|
|
295
|
-
`).all(`%${query}%`, `%${query}%`);
|
|
296
|
-
return rows.map(row => this.mapChunkRow(row));
|
|
297
|
-
} catch (error) {
|
|
298
|
-
return [];
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
mapChunkRow(row) {
|
|
303
|
-
const metadata = JSON.parse(row.metadata || '{}');
|
|
304
|
-
return {
|
|
305
|
-
id: row.id,
|
|
306
|
-
name: row.name,
|
|
307
|
-
filePath: row.file_path,
|
|
308
|
-
type: row.type,
|
|
309
|
-
subtype: row.subtype,
|
|
310
|
-
code: row.content,
|
|
311
|
-
startLine: row.start_line,
|
|
312
|
-
complexity: { score: row.complexity_score },
|
|
313
|
-
purpose: row.purpose,
|
|
314
|
-
tags: metadata.tags || [],
|
|
315
|
-
businessDomain: metadata.businessDomain || [],
|
|
316
|
-
technicalPatterns: metadata.technicalPatterns || [],
|
|
317
|
-
includes: {
|
|
318
|
-
imports: metadata.imports || [],
|
|
319
|
-
types: metadata.types || []
|
|
320
|
-
},
|
|
321
|
-
bundles: metadata.bundles || []
|
|
322
|
-
};
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// Vector Embedding Persistence
|
|
326
|
-
saveEmbedding(chunkId, embedding, modelName) {
|
|
327
|
-
try {
|
|
328
|
-
const stmt = this.db.prepare(`
|
|
329
|
-
INSERT OR REPLACE INTO vector_embeddings (chunk_id, embedding, model_name)
|
|
330
|
-
VALUES (?, ?, ?)
|
|
331
|
-
`);
|
|
332
|
-
// Convert Float32Array to Buffer for SQLite BLOB
|
|
333
|
-
const buffer = Buffer.from(embedding.buffer);
|
|
334
|
-
stmt.run(chunkId, buffer, modelName);
|
|
335
|
-
return true;
|
|
336
|
-
} catch (error) {
|
|
337
|
-
console.error(`Failed to save embedding for ${chunkId}:`, error.message);
|
|
338
|
-
return false;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
|
|
342
|
-
getEmbedding(chunkId) {
|
|
343
|
-
try {
|
|
344
|
-
const row = this.db.prepare('SELECT embedding FROM vector_embeddings WHERE chunk_id = ?').get(chunkId);
|
|
345
|
-
if (!row) return null;
|
|
346
|
-
// Convert Buffer back to Float32Array
|
|
347
|
-
return new Float32Array(row.embedding.buffer, row.embedding.byteOffset, row.embedding.byteLength / 4);
|
|
348
|
-
} catch (error) {
|
|
349
|
-
return null;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
|
|
353
|
-
// Agent Memory Methods
|
|
354
|
-
createSession(id, title) {
|
|
355
|
-
try {
|
|
356
|
-
const stmt = this.db.prepare('INSERT OR REPLACE INTO agent_sessions (id, title) VALUES (?, ?)');
|
|
357
|
-
stmt.run(id, title);
|
|
358
|
-
return true;
|
|
359
|
-
} catch (error) {
|
|
360
|
-
return false;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
|
|
364
|
-
addMessage(sessionId, role, content, metadata = {}) {
|
|
365
|
-
try {
|
|
366
|
-
const stmt = this.db.prepare(`
|
|
367
|
-
INSERT INTO agent_memory (session_id, role, content, metadata)
|
|
368
|
-
VALUES (?, ?, ?, ?)
|
|
369
|
-
`);
|
|
370
|
-
stmt.run(sessionId, role, content, JSON.stringify(metadata));
|
|
371
|
-
|
|
372
|
-
// Update session last_active_at
|
|
373
|
-
this.db.prepare('UPDATE agent_sessions SET last_active_at = CURRENT_TIMESTAMP WHERE id = ?').run(sessionId);
|
|
374
|
-
return true;
|
|
375
|
-
} catch (error) {
|
|
376
|
-
return false;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
getSessionHistory(sessionId) {
|
|
381
|
-
try {
|
|
382
|
-
return this.db.prepare('SELECT * FROM agent_memory WHERE session_id = ? ORDER BY timestamp ASC').all(sessionId);
|
|
383
|
-
} catch (error) {
|
|
384
|
-
return [];
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
// Close database connection
|
|
389
|
-
close() {
|
|
390
|
-
if (this.db) {
|
|
391
|
-
this.db.close();
|
|
392
|
-
if (this.verbose) {
|
|
393
|
-
console.log('📊 SQLite database connection closed');
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
}
|
|
397
|
-
}
|