Repository navigation
Expand file tree
/
Copy pathagentKnowledge.js
More file actions
473 lines (420 loc) · 12.7 KB
/
Copy pathagentKnowledge.js
File metadata and controls
473 lines (420 loc) · 12.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
const fs = require('fs')
const path = require('path')
const sqlite3 = require('sqlite3').verbose()
const { app } = require('electron')
const { randomUUID } = require('crypto')
const notesSearch = require('./notesSearch')
const agentProviders = require('./agentProviders')
let db = null
let initPromise = null
const CHUNK_SIZE = 800
const CHUNK_OVERLAP = 100
function normalizeAgentSettings(settings) {
if (!settings) return {}
if (settings.agent) {
return {
...settings.agent,
_legacyChatGptKey: settings.apiKeys?.chatgpt || ''
}
}
return settings
}
function getKnowledgeDir() {
const dir = path.join(app.getPath('userData'), 'knowledge')
fs.mkdirSync(dir, { recursive: true })
return dir
}
function getDbPath() {
const agentDir = path.join(app.getPath('userData'), 'agent')
fs.mkdirSync(agentDir, { recursive: true })
return path.join(agentDir, 'knowledge.db')
}
function runDb(sql, params = []) {
return new Promise((resolve, reject) => {
db.run(sql, params, function onRun(err) {
if (err) reject(err)
else resolve({ lastID: this.lastID, changes: this.changes })
})
})
}
function allDb(sql, params = []) {
return new Promise((resolve, reject) => {
db.all(sql, params, (err, rows) => {
if (err) reject(err)
else resolve(rows)
})
})
}
function getDb(sql, params = []) {
return new Promise((resolve, reject) => {
db.get(sql, params, (err, row) => {
if (err) reject(err)
else resolve(row)
})
})
}
async function initDatabase() {
if (db) return db
if (initPromise) return initPromise
initPromise = new Promise((resolve, reject) => {
db = new sqlite3.Database(getDbPath(), (err) => {
if (err) {
reject(err)
return
}
db.serialize(async () => {
try {
await runDb(`
CREATE TABLE IF NOT EXISTS documents (
id TEXT PRIMARY KEY,
source_type TEXT NOT NULL,
source_id TEXT,
title TEXT,
path TEXT,
updated_at TEXT,
indexed_at TEXT
)
`)
await runDb(`
CREATE TABLE IF NOT EXISTS chunks (
id TEXT PRIMARY KEY,
document_id TEXT NOT NULL,
chunk_index INTEGER NOT NULL,
content TEXT NOT NULL,
FOREIGN KEY(document_id) REFERENCES documents(id) ON DELETE CASCADE
)
`)
await runDb(`
CREATE TABLE IF NOT EXISTS embeddings (
chunk_id TEXT PRIMARY KEY,
vector BLOB NOT NULL,
FOREIGN KEY(chunk_id) REFERENCES chunks(id) ON DELETE CASCADE
)
`)
await runDb('CREATE INDEX IF NOT EXISTS idx_chunks_doc ON chunks(document_id)')
resolve(db)
} catch (e) {
reject(e)
}
})
})
})
return initPromise
}
function chunkText(text) {
const normalized = String(text || '').replace(/\s+/g, ' ').trim()
if (!normalized) return []
const chunks = []
let start = 0
while (start < normalized.length) {
const end = Math.min(normalized.length, start + CHUNK_SIZE)
chunks.push(normalized.slice(start, end))
if (end >= normalized.length) break
start = Math.max(0, end - CHUNK_OVERLAP)
}
return chunks
}
function vectorToBlob(vector) {
const buf = Buffer.alloc(vector.length * 4)
for (let i = 0; i < vector.length; i++) {
buf.writeFloatLE(vector[i], i * 4)
}
return buf
}
function blobToVector(blob) {
const buf = Buffer.from(blob)
const vector = []
for (let i = 0; i < buf.length; i += 4) {
vector.push(buf.readFloatLE(i))
}
return vector
}
function cosineSimilarity(a, b) {
let dot = 0
let normA = 0
let normB = 0
const len = Math.min(a.length, b.length)
for (let i = 0; i < len; i++) {
dot += a[i] * b[i]
normA += a[i] * a[i]
normB += b[i] * b[i]
}
if (!normA || !normB) return 0
return dot / (Math.sqrt(normA) * Math.sqrt(normB))
}
async function clearDocumentIndex(documentId) {
await initDatabase()
const chunks = await allDb('SELECT id FROM chunks WHERE document_id = ?', [documentId])
for (const chunk of chunks) {
await runDb('DELETE FROM embeddings WHERE chunk_id = ?', [chunk.id])
}
await runDb('DELETE FROM chunks WHERE document_id = ?', [documentId])
await runDb('DELETE FROM documents WHERE id = ?', [documentId])
}
async function indexDocument({ id, sourceType, sourceId, title, filePath, text, agentSettings }) {
await initDatabase()
await clearDocumentIndex(id)
const chunks = chunkText(text)
if (!chunks.length) return { id, chunks: 0 }
const now = new Date().toISOString()
await runDb(
'INSERT INTO documents (id, source_type, source_id, title, path, updated_at, indexed_at) VALUES (?, ?, ?, ?, ?, ?, ?)',
[id, sourceType, sourceId || id, title || 'Untitled', filePath || null, now, now]
)
const embeddings = await agentProviders.createEmbeddings({
agentSettings: normalizeAgentSettings(agentSettings),
texts: chunks
})
for (let i = 0; i < chunks.length; i++) {
const chunkId = randomUUID()
await runDb(
'INSERT INTO chunks (id, document_id, chunk_index, content) VALUES (?, ?, ?, ?)',
[chunkId, id, i, chunks[i]]
)
await runDb(
'INSERT INTO embeddings (chunk_id, vector) VALUES (?, ?)',
[chunkId, vectorToBlob(embeddings[i])]
)
}
return { id, chunks: chunks.length }
}
async function indexAllNotes(agentSettings, options = {}) {
await initDatabase()
const includeNotes = options.includeNotes !== false
if (!includeNotes) return { indexed: 0 }
const pages = notesSearch.enumerateAllNotesPages()
let indexed = 0
for (const page of pages) {
const docId = `note:${page.id}`
const text = `${page.title}\n\n${page.body}`.trim()
if (!text) continue
await indexDocument({
id: docId,
sourceType: 'note',
sourceId: page.id,
title: page.title,
text,
agentSettings
})
indexed += 1
}
return { indexed }
}
async function indexCustomDocuments(agentSettings, options = {}) {
await initDatabase()
const includeCustom = options.includeCustomDocs !== false
if (!includeCustom) return { indexed: 0 }
const dir = getKnowledgeDir()
const metaPath = path.join(dir, 'meta.json')
const meta = fs.existsSync(metaPath)
? JSON.parse(fs.readFileSync(metaPath, 'utf8'))
: { documents: [] }
let indexed = 0
for (const doc of meta.documents || []) {
const filePath = path.join(dir, doc.filename)
if (!fs.existsSync(filePath)) continue
const text = fs.readFileSync(filePath, 'utf8')
await indexDocument({
id: `custom:${doc.id}`,
sourceType: 'custom',
sourceId: doc.id,
title: doc.title,
filePath,
text,
agentSettings
})
indexed += 1
}
return { indexed }
}
async function reindexAll(agentSettings, kbSettings = {}) {
const notesResult = await indexAllNotes(agentSettings, kbSettings)
const customResult = await indexCustomDocuments(agentSettings, kbSettings)
return {
notesIndexed: notesResult.indexed,
customIndexed: customResult.indexed,
total: notesResult.indexed + customResult.indexed
}
}
async function searchKnowledge(query, agentSettings, limit = 8) {
await initDatabase()
const normalized = String(query || '').trim()
if (!normalized) return []
const [queryEmbedding] = await agentProviders.createEmbeddings({
agentSettings: normalizeAgentSettings(agentSettings),
texts: [normalized]
})
const rows = await allDb(`
SELECT c.id as chunk_id, c.content, c.chunk_index, d.id as document_id, d.title, d.source_type, d.source_id, e.vector
FROM chunks c
JOIN documents d ON d.id = c.document_id
JOIN embeddings e ON e.chunk_id = c.id
`)
const keywordResults = notesSearch.searchNotesPages(normalized)
const keywordBoost = new Map(keywordResults.map((r) => [`note:${r.id}`, 0.15]))
const scored = rows.map((row) => {
const vector = blobToVector(row.vector)
let score = cosineSimilarity(queryEmbedding, vector)
if (keywordBoost.has(row.document_id)) score += keywordBoost.get(row.document_id)
return {
score,
content: row.content,
title: row.title,
sourceType: row.source_type,
sourceId: row.source_id,
documentId: row.document_id,
chunkIndex: row.chunk_index
}
})
scored.sort((a, b) => b.score - a.score)
return scored.slice(0, limit)
}
function readCustomMeta() {
const metaPath = path.join(getKnowledgeDir(), 'meta.json')
if (!fs.existsSync(metaPath)) return { documents: [] }
try {
return JSON.parse(fs.readFileSync(metaPath, 'utf8'))
} catch {
return { documents: [] }
}
}
function writeCustomMeta(meta) {
fs.writeFileSync(path.join(getKnowledgeDir(), 'meta.json'), JSON.stringify(meta, null, 2))
}
function listCustomDocuments() {
const meta = readCustomMeta()
return (meta.documents || []).map((d) => ({
id: d.id,
title: d.title,
filename: d.filename,
updatedAt: d.updatedAt,
createdAt: d.createdAt
}))
}
function createCustomDocument({ title, content }) {
const meta = readCustomMeta()
const id = randomUUID()
const safeName = `${id}.md`
const now = new Date().toISOString()
const doc = { id, title: title || 'Untitled', filename: safeName, createdAt: now, updatedAt: now }
meta.documents = meta.documents || []
meta.documents.push(doc)
fs.writeFileSync(path.join(getKnowledgeDir(), safeName), String(content || ''))
writeCustomMeta(meta)
return doc
}
function updateCustomDocument(id, { title, content }) {
const meta = readCustomMeta()
const doc = (meta.documents || []).find((d) => d.id === id)
if (!doc) throw new Error('Document not found')
if (title !== undefined) doc.title = title
doc.updatedAt = new Date().toISOString()
if (content !== undefined) {
fs.writeFileSync(path.join(getKnowledgeDir(), doc.filename), String(content))
}
writeCustomMeta(meta)
return doc
}
function getCustomDocument(id) {
const meta = readCustomMeta()
const doc = (meta.documents || []).find((d) => d.id === id)
if (!doc) return null
const filePath = path.join(getKnowledgeDir(), doc.filename)
const content = fs.existsSync(filePath) ? fs.readFileSync(filePath, 'utf8') : ''
return { ...doc, content }
}
async function getIndexStatus() {
await initDatabase()
const docCount = await getDb('SELECT COUNT(*) as count FROM documents')
const chunkCount = await getDb('SELECT COUNT(*) as count FROM chunks')
return {
documents: docCount?.count || 0,
chunks: chunkCount?.count || 0,
customDocuments: listCustomDocuments().length
}
}
function clearKnowledgeDir() {
const dir = getKnowledgeDir()
if (!fs.existsSync(dir)) return
for (const name of fs.readdirSync(dir)) {
try {
fs.unlinkSync(path.join(dir, name))
} catch {}
}
}
function exportKnowledgePayload() {
const dir = getKnowledgeDir()
const files = []
if (!fs.existsSync(dir)) return { version: 1, files }
for (const name of fs.readdirSync(dir)) {
const full = path.join(dir, name)
if (!fs.statSync(full).isFile()) continue
files.push({
path: name,
data: fs.readFileSync(full).toString('base64')
})
}
return { version: 1, files }
}
function importKnowledgePayload(payload) {
if (!payload || !Array.isArray(payload.files)) return
clearKnowledgeDir()
const dir = getKnowledgeDir()
for (const f of payload.files) {
const name = String(f.path || '')
if (!name || name.includes('..') || path.isAbsolute(name)) continue
const dest = path.join(dir, name)
if (!dest.startsWith(dir)) continue
fs.writeFileSync(dest, Buffer.from(String(f.data || ''), 'base64'))
}
}
async function closeDatabase() {
if (!db) return
await new Promise((resolve, reject) => {
db.close((err) => {
if (err) reject(err)
else resolve()
})
})
db = null
initPromise = null
}
async function exportKnowledgeDb() {
const dbPath = getDbPath()
if (!fs.existsSync(dbPath)) return null
return fs.readFileSync(dbPath).toString('base64')
}
async function importKnowledgeDb(base64Data) {
if (!base64Data) return
await closeDatabase()
const dbPath = getDbPath()
fs.mkdirSync(path.dirname(dbPath), { recursive: true })
fs.writeFileSync(dbPath, Buffer.from(String(base64Data), 'base64'))
await initDatabase()
}
async function clearKnowledgeDb() {
await closeDatabase()
const dbPath = getDbPath()
if (fs.existsSync(dbPath)) {
try {
fs.unlinkSync(dbPath)
} catch {}
}
}
module.exports = {
initDatabase,
reindexAll,
searchKnowledge,
listCustomDocuments,
createCustomDocument,
updateCustomDocument,
getCustomDocument,
getIndexStatus,
getKnowledgeDir,
exportKnowledgePayload,
importKnowledgePayload,
exportKnowledgeDb,
importKnowledgeDb,
clearKnowledgeDb,
closeDatabase
}