@membank/core 0.0.4 → 0.2.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/README.md +154 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
# @membank/core
|
|
2
|
+
|
|
3
|
+
Core library for membank — handles storage, embeddings, deduplication, and semantic search.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Provides the database layer, embedding inference, and query engine that all other membank packages build on. Uses SQLite with vector search for local, offline-capable memory storage.
|
|
8
|
+
|
|
9
|
+
## Requirements
|
|
10
|
+
|
|
11
|
+
- Node.js >=24
|
|
12
|
+
- Native modules: `better-sqlite3`, `sqlite-vec` (pre-built binaries, not compiled on install)
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @membank/core
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Storage
|
|
21
|
+
|
|
22
|
+
Memories are stored in `~/.membank/memory.db` (SQLite). Embeddings live in a `sqlite-vec` virtual table alongside the main `memories` table.
|
|
23
|
+
|
|
24
|
+
Default location can be overridden via `DatabaseManager.open(customPath)`.
|
|
25
|
+
|
|
26
|
+
## Usage
|
|
27
|
+
|
|
28
|
+
### Initialize
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { DatabaseManager, EmbeddingService, MemoryRepository, QueryEngine } from '@membank/core'
|
|
32
|
+
|
|
33
|
+
const db = DatabaseManager.open()
|
|
34
|
+
const embedding = new EmbeddingService()
|
|
35
|
+
const repo = new MemoryRepository(db, embedding)
|
|
36
|
+
const engine = new QueryEngine(db, embedding, repo)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Save a memory
|
|
40
|
+
|
|
41
|
+
```typescript
|
|
42
|
+
const memory = await repo.save({
|
|
43
|
+
content: 'Always use `--filter` when running pnpm commands in this monorepo',
|
|
44
|
+
type: 'preference',
|
|
45
|
+
tags: ['pnpm', 'monorepo'],
|
|
46
|
+
})
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
### Query memories
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
const results = await engine.query({
|
|
53
|
+
query: 'how to run commands in one package',
|
|
54
|
+
limit: 5,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
for (const { content, score } of results) {
|
|
58
|
+
console.log(score.toFixed(3), content)
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### Session injection
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { SessionContextBuilder } from '@membank/core'
|
|
66
|
+
|
|
67
|
+
const builder = new SessionContextBuilder(db, repo)
|
|
68
|
+
const { stats, pinnedGlobal, pinnedProject } = await builder.getSessionContext(projectScope)
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Memory types
|
|
72
|
+
|
|
73
|
+
Types are ranked by priority, which affects query scoring:
|
|
74
|
+
|
|
75
|
+
| Type | Weight | When to use |
|
|
76
|
+
|------|--------|-------------|
|
|
77
|
+
| `correction` | 1.0 | A mistake was made and corrected |
|
|
78
|
+
| `preference` | 0.8 | Tool, style, or pattern preference |
|
|
79
|
+
| `decision` | 0.6 | Architectural or design choice |
|
|
80
|
+
| `learning` | 0.4 | Concept understood or insight gained |
|
|
81
|
+
| `fact` | 0.2 | Static reference information |
|
|
82
|
+
|
|
83
|
+
## Deduplication
|
|
84
|
+
|
|
85
|
+
On every save, the new content is embedded and compared against existing memories of the same type and scope:
|
|
86
|
+
|
|
87
|
+
- **Similarity >0.92** — auto-overwrites the existing memory (merge semantics)
|
|
88
|
+
- **Similarity 0.75–0.92** — flags the existing memory with `needs_review=true` and creates a new entry
|
|
89
|
+
- **Similarity <0.75** — creates a new memory with no conflict
|
|
90
|
+
|
|
91
|
+
## Query scoring
|
|
92
|
+
|
|
93
|
+
Results are ranked by a weighted combination of signals:
|
|
94
|
+
|
|
95
|
+
```
|
|
96
|
+
score = 0.40 × type_weight
|
|
97
|
+
+ 0.30 × access_frequency # count / (count + 10)
|
|
98
|
+
+ 0.20 × recency # 1 / (1 + days_since_update)
|
|
99
|
+
+ 0.10 × is_pinned
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Scope
|
|
103
|
+
|
|
104
|
+
Each memory is tagged with a scope derived from the project's git remote URL (SHA256, first 16 chars). Falls back to a hash of the current working directory if git is unavailable. Global memories use `"global"` as scope.
|
|
105
|
+
|
|
106
|
+
```typescript
|
|
107
|
+
import { resolveScope } from '@membank/core'
|
|
108
|
+
|
|
109
|
+
const scope = await resolveScope()
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
## Embeddings
|
|
113
|
+
|
|
114
|
+
Uses `Xenova/bge-small-en-v1.5` (384 dimensions, ~33 MB) via `@huggingface/transformers`. The model is downloaded on first use and cached at `~/.membank/models/`. All inference runs locally on CPU — no network calls after initial download.
|
|
115
|
+
|
|
116
|
+
## API
|
|
117
|
+
|
|
118
|
+
### `DatabaseManager`
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
DatabaseManager.open(dbPath?: string): DatabaseManager
|
|
122
|
+
DatabaseManager.openInMemory(): DatabaseManager
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### `EmbeddingService`
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
new EmbeddingService(options?: { progressCallback? })
|
|
129
|
+
embed(text: string): Promise<Float32Array>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
### `MemoryRepository`
|
|
133
|
+
|
|
134
|
+
```typescript
|
|
135
|
+
save(options: SaveOptions): Promise<Memory>
|
|
136
|
+
update(id: string, patch: Partial<SaveOptions>): Promise<Memory>
|
|
137
|
+
delete(id: string): void
|
|
138
|
+
list(opts?: { type?: MemoryType; pinned?: boolean }): Memory[]
|
|
139
|
+
stats(): MemoryStats
|
|
140
|
+
incrementAccessCount(id: string): void
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### `QueryEngine`
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
query(options: QueryOptions): Promise<Array<Memory & { score: number }>>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### `SessionContextBuilder`
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
getSessionContext(projectScope: string): Promise<SessionContext>
|
|
153
|
+
listMemoryTypes(): MemoryType[]
|
|
154
|
+
```
|