-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-storage.js
More file actions
76 lines (64 loc) · 2.25 KB
/
Copy pathcustom-storage.js
File metadata and controls
76 lines (64 loc) · 2.25 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
/**
* Custom Storage Example
*
* Shows how to use the storage adapters and embedding providers
* directly, outside of the ContextEngine class.
*
* Useful when you want fine-grained control or are building
* a custom integration.
*/
import {
SQLiteStorage,
LocalEmbeddingProvider,
cosineSimilarity,
eventToText,
} from 'context-engine-ai'
import { randomUUID } from 'crypto'
async function main() {
// Initialize storage and embedder separately
const storage = new SQLiteStorage('./custom-storage.db')
await storage.init()
const embedder = new LocalEmbeddingProvider()
console.log(`Embedding dimensions: ${embedder.dimensions}`)
// Manually embed and store events
const events = [
{ type: 'code_edit', data: { file: 'auth.ts', action: 'added OAuth2 flow' } },
{ type: 'code_edit', data: { file: 'database.ts', action: 'added connection pooling' } },
{ type: 'test_run', data: { suite: 'auth', passed: 12, failed: 0 } },
{ type: 'deploy', data: { env: 'staging', version: '1.2.0', status: 'success' } },
]
console.log('\nStoring events...')
for (const event of events) {
const text = eventToText(event.type, event.data)
const embedding = await embedder.embed(text)
await storage.insert({
id: randomUUID(),
type: event.type,
data: event.data,
timestamp: Date.now(),
embedding,
relevance: 1.0,
})
console.log(` Stored: [${event.type}] ${text.slice(0, 60)}`)
}
// Manual similarity search
const query = 'what was deployed?'
const queryEmbedding = await embedder.embed(query)
const results = await storage.search(queryEmbedding, 2)
console.log(`\nQuery: "${query}"`)
console.log('Results:')
results.forEach((e, i) => {
const sim = cosineSimilarity(queryEmbedding, e.embedding)
console.log(` ${i + 1}. [${e.type}] similarity=${sim.toFixed(3)} -- ${JSON.stringify(e.data)}`)
})
// Check event count
const count = await storage.count()
console.log(`\nTotal events stored: ${count}`)
// Prune to keep only 2 most relevant
const pruned = await storage.prune(2)
console.log(`Pruned ${pruned} events (keeping 2)`)
console.log(`Events remaining: ${await storage.count()}`)
await storage.close()
console.log('\nDone!')
}
main().catch(console.error)