@equationalapplications/core-llm-wiki 3.0.0 → 3.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/README.md +159 -1
- package/dist/index.d.mts +108 -7
- package/dist/index.d.ts +108 -7
- package/dist/index.js +518 -83
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +518 -83
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -124,13 +124,171 @@ const memory = await wikiMemory.read('user-123', 'my preferences', {
|
|
|
124
124
|
```
|
|
125
125
|
|
|
126
126
|
**Hybrid scoring blends:**
|
|
127
|
-
- `hybridWeight: 1.0` →
|
|
127
|
+
- `hybridWeight: 1.0` → all-semantic blend with semantic scores clamped to non-negative range (no keyword component)
|
|
128
128
|
- `hybridWeight: 0.5` → balanced semantic + keyword (50/50 blend)
|
|
129
129
|
- `hybridWeight: 0.0` → pure keyword ranking, skips `embed()` entirely (no LLM API cost)
|
|
130
130
|
|
|
131
|
+
True cosine-range pure semantic ranking (including negative cosine values) is used when `hybridWeight` is left `undefined`.
|
|
132
|
+
|
|
131
133
|
**Pre-filtering optimization:**
|
|
132
134
|
When `preFilterLimit: 50` is set with 1000 facts, cosine similarity is computed only for the top 50 MiniSearch keyword matches, reducing O(N) scoring to O(50).
|
|
133
135
|
|
|
136
|
+
## Pluggable Vector Retrieval
|
|
137
|
+
|
|
138
|
+
When your entity corpus grows, in-process cosine similarity scoring becomes a bottleneck. The optional **`VectorRanker`** interface lets you delegate semantic ranking to **sqlite-vec**, **sqlite-vss**, or an external vector database while `WikiMemory` handles embedding validation, hybrid scoring, and tier-2 row hydration.
|
|
139
|
+
|
|
140
|
+
### `VectorRanker` purpose
|
|
141
|
+
|
|
142
|
+
`VectorRanker` provides an optional injection point for approximate nearest-neighbor (ANN) ranking:
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
export interface VectorRanker {
|
|
146
|
+
/**
|
|
147
|
+
* Return semantic scores for facts in scope, sorted by similarity.
|
|
148
|
+
* - `entityId`: restricts results to one entity
|
|
149
|
+
* - `queryVec`: the embedded query (Float32Array or number[])
|
|
150
|
+
* - `candidateIds` (optional): when set, rank only within this set (MiniSearch pre-filter mode)
|
|
151
|
+
* - `limit`: requested top-K count
|
|
152
|
+
*/
|
|
153
|
+
rankBySimilarity(args: VectorRankerRankArgs): Promise<VectorRankerSemanticResult[]>;
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Optional hook called after embedding persistence (upsert, reembed, delete).
|
|
157
|
+
* Implementations use this to keep external indexes (sqlite-vec, remote ANN) in sync.
|
|
158
|
+
*/
|
|
159
|
+
onEmbeddingPersisted?(event: {
|
|
160
|
+
entityId: string;
|
|
161
|
+
factId: string;
|
|
162
|
+
vector: Float32Array | null; // null = embedding removed
|
|
163
|
+
}): void | Promise<void>;
|
|
164
|
+
}
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
**When no ranker is configured**, `WikiMemory` uses built-in JS cosine similarity — the same behavior as today. When a ranker is supplied and embeddings preconditions are met (`embed` available, dimensions match, no mismatches), `WikiMemory` delegates scoring to the ranker and blends results with keyword scores.
|
|
168
|
+
|
|
169
|
+
### Example: sqlite-vec adapter
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
import { WikiMemory } from '@equationalapplications/core-llm-wiki';
|
|
173
|
+
import type { VectorRanker, VectorRankerRankArgs, VectorRankerSemanticResult } from '@equationalapplications/core-llm-wiki';
|
|
174
|
+
|
|
175
|
+
// Minimal sqlite-vec adapter (pseudo-code)
|
|
176
|
+
const sqliteVecRanker: VectorRanker = {
|
|
177
|
+
async rankBySimilarity(args: VectorRankerRankArgs): Promise<VectorRankerSemanticResult[]> {
|
|
178
|
+
const { entityId, queryVec, candidateIds, limit } = args;
|
|
179
|
+
|
|
180
|
+
// Build KNN query using sqlite-vec's distance functions.
|
|
181
|
+
// sqlite-vec returns cosine distance (0 = identical, 2 = opposite) ascending.
|
|
182
|
+
// Invert to semanticScore: higher = more similar, matching VectorRanker contract.
|
|
183
|
+
let sql = `SELECT id, (1.0 - distance) AS semanticScore FROM vec_facts
|
|
184
|
+
WHERE entity_id = ? AND deleted_at IS NULL`;
|
|
185
|
+
const params: any[] = [entityId];
|
|
186
|
+
|
|
187
|
+
// Apply pre-filter if provided
|
|
188
|
+
if (candidateIds) {
|
|
189
|
+
sql += ` AND id IN (${candidateIds.map(() => '?').join(',')})`;
|
|
190
|
+
params.push(...candidateIds);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// KNN search (example syntax; adjust for your sqlite-vec version)
|
|
194
|
+
sql += ` ORDER BY vec MATCH vec_neighbor(?) LIMIT ?`;
|
|
195
|
+
params.push(queryVec, limit);
|
|
196
|
+
|
|
197
|
+
const rows = await db.getAllAsync<{ id: string; semanticScore: number }>(sql, params);
|
|
198
|
+
return rows; // sorted descending by semanticScore (closest distance → highest similarity)
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
async onEmbeddingPersisted(event) {
|
|
202
|
+
const { entityId, factId, vector } = event;
|
|
203
|
+
if (vector) {
|
|
204
|
+
// Upsert into sqlite-vec table
|
|
205
|
+
await db.runAsync(
|
|
206
|
+
`INSERT OR REPLACE INTO vec_facts (id, entity_id, vec) VALUES (?, ?, ?)`,
|
|
207
|
+
[factId, entityId, vector]
|
|
208
|
+
);
|
|
209
|
+
} else {
|
|
210
|
+
// Delete when embedding is removed
|
|
211
|
+
await db.runAsync(`DELETE FROM vec_facts WHERE id = ?`, [factId]);
|
|
212
|
+
}
|
|
213
|
+
},
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
const wikiMemory = new WikiMemory(db, {
|
|
217
|
+
llmProvider: { /* ... */ },
|
|
218
|
+
vectorRanker: sqliteVecRanker,
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
// read() now uses sqlite-vec for scoring instead of JS cosine
|
|
222
|
+
const memory = await wikiMemory.read('user-123', 'my preferences');
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
### Fallback policies
|
|
226
|
+
|
|
227
|
+
When `rankBySimilarity` rejects (e.g., ANN service outage, misconfiguration), `WikiMemory` applies a recovery policy:
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
export type VectorRankerFallback =
|
|
231
|
+
| 'js-cosine' // (default) Score candidates in-process with JS cosine — same as no ranker
|
|
232
|
+
| 'keyword' // Skip semantic ranking; return keyword-only results
|
|
233
|
+
| 'empty' // Semantic facts list empty for this read; tasks/events still included
|
|
234
|
+
| 'throw'; // Reject read() with the ranker error
|
|
235
|
+
|
|
236
|
+
const wikiMemory = new WikiMemory(db, {
|
|
237
|
+
llmProvider: { /* ... */ },
|
|
238
|
+
vectorRanker: sqliteVecRanker,
|
|
239
|
+
vectorRankerFallback: 'js-cosine', // default
|
|
240
|
+
onVectorRankerFallback: (info) => {
|
|
241
|
+
console.warn(
|
|
242
|
+
`Ranker failed (policy: ${info.policy}); error:`,
|
|
243
|
+
info.error
|
|
244
|
+
);
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
```
|
|
248
|
+
|
|
249
|
+
- **`'js-cosine'` (default):** Seamless degradation; same behavior as if no ranker was configured.
|
|
250
|
+
- **`'keyword'`:** Useful when semantic ranking is optional; keyword search proceeds normally.
|
|
251
|
+
- **`'empty'`:** Return no facts for this query (but tasks/events still load); useful for strict consistency.
|
|
252
|
+
- **`'throw'`:** Propagate the error and fail the read.
|
|
253
|
+
|
|
254
|
+
### `onEmbeddingPersisted` eventual consistency
|
|
255
|
+
|
|
256
|
+
If `vectorRanker.onEmbeddingPersisted` returns a pending Promise, the hook **may resolve asynchronously**. This supports ANN indexes that rebuild on a schedule (e.g., sqlite-vec triggers on transaction commit) or external services with eventual consistency.
|
|
257
|
+
|
|
258
|
+
**Best practice:**
|
|
259
|
+
- If your adapter has **synchronous guarantees** (in-process sqlite-vec, same transaction), await the promise.
|
|
260
|
+
- If your adapter is **eventually consistent** (remote ANN, async rebuild), document the lag and document that queries may miss recently-added facts until the index refreshes.
|
|
261
|
+
- The **SQLite blob remains the source of truth**; `WikiMemory` always writes embeddings to `embedding_blob` first before calling the hook.
|
|
262
|
+
|
|
263
|
+
### Hybrid scoring with ranker
|
|
264
|
+
|
|
265
|
+
When both `vectorRanker` and `hybridWeight` are configured, `WikiMemory` still applies hybrid blending after the ranker returns scores:
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
const wikiMemory = new WikiMemory(db, {
|
|
269
|
+
config: {
|
|
270
|
+
hybridWeight: 0.7, // 70% semantic, 30% keyword
|
|
271
|
+
},
|
|
272
|
+
vectorRanker: sqliteVecRanker,
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
// ranker returns semanticScore; WikiMemory blends with MiniSearch keyword score
|
|
276
|
+
const memory = await wikiMemory.read('user-123', 'my preferences', {
|
|
277
|
+
hybridWeight: 0.5, // per-call override to 50/50 blend
|
|
278
|
+
});
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
Note on semantics:
|
|
282
|
+
- Leave `hybridWeight` undefined for true pure-semantic cosine-range scoring.
|
|
283
|
+
- Set `hybridWeight: 1` for an all-semantic variant that clamps negative semantic scores to 0.
|
|
284
|
+
|
|
285
|
+
For details on hybrid scoring formulas and trade-offs, see [Retrieval Tuning](#retrieval-tuning) above.
|
|
286
|
+
|
|
287
|
+
### Spec and issue reference
|
|
288
|
+
|
|
289
|
+
- **Full spec:** [`docs/superpowers/specs/2026-05-07-pluggable-vector-retrieval.md`](https://github.com/equationalapplications/expo-llm-wiki/blob/main/docs/superpowers/specs/2026-05-07-pluggable-vector-retrieval.md)
|
|
290
|
+
- **GitHub issue:** [#15](https://github.com/equationalapplications/expo-llm-wiki/issues/15)
|
|
291
|
+
|
|
134
292
|
## Vector Cache
|
|
135
293
|
|
|
136
294
|
Parsed embedding vectors from full-scan `read()` calls are cached in memory, keyed by entity ID (max 16 entities, max 500 vectors per entity). This avoids redundant `Float32Array` parsing on repeated queries for the same entity. When the 16-entity limit is reached, the oldest-inserted entity is evicted to make room; if an entity exceeds 500 facts, its vectors are not cached at all for that read.
|
package/dist/index.d.mts
CHANGED
|
@@ -132,20 +132,100 @@ interface LLMProvider {
|
|
|
132
132
|
*/
|
|
133
133
|
embed?: (text: string) => Promise<number[]>;
|
|
134
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Result of semantic ranking for a single fact.
|
|
137
|
+
*/
|
|
138
|
+
interface VectorRankerSemanticResult {
|
|
139
|
+
id: string;
|
|
140
|
+
/** Cosine similarity in [-1, 1] when exact; implementations MAY document other monotonic scales. */
|
|
141
|
+
semanticScore: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Arguments passed to VectorRanker.rankBySimilarity.
|
|
145
|
+
*/
|
|
146
|
+
interface VectorRankerRankArgs {
|
|
147
|
+
entityId: string;
|
|
148
|
+
queryVec: Float32Array | number[];
|
|
149
|
+
/**
|
|
150
|
+
* When set (MiniSearch pre-filter path): ranker MUST only produce results for ids in this set.
|
|
151
|
+
* When omitted (full-entity semantic path): ranker scopes by entityId per its backing store contract.
|
|
152
|
+
*/
|
|
153
|
+
candidateIds?: readonly string[];
|
|
154
|
+
/**
|
|
155
|
+
* Upper bound on how many distinct fact ids should receive a semanticScore in this call.
|
|
156
|
+
* WikiMemory derives this from maxResults / candidate cardinality / documented oversampling policy.
|
|
157
|
+
*/
|
|
158
|
+
limit: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Optional backend for semantic candidate scoring / top-k retrieval.
|
|
162
|
+
* When omitted, WikiMemory scores rows with embedding_blob / embedding TEXT in JS (cosine).
|
|
163
|
+
*/
|
|
164
|
+
interface VectorRanker {
|
|
165
|
+
/**
|
|
166
|
+
* Return semantic scores for facts in scope, sorted descending by semanticScore (stable tie-breaking
|
|
167
|
+
* not required — WikiMemory reapplies existing tie-breakers after blending).
|
|
168
|
+
* Implementations SHOULD omit facts with no usable vector; callers treat missing ids like today's
|
|
169
|
+
* "no embedding" rows (pure semantic: -2; hybrid: keyword-only portion).
|
|
170
|
+
*/
|
|
171
|
+
rankBySimilarity(args: VectorRankerRankArgs): Promise<VectorRankerSemanticResult[]>;
|
|
172
|
+
/**
|
|
173
|
+
* Called after a fact's embedding is successfully persisted to embedding_blob (or cleared).
|
|
174
|
+
* Hosts use this to keep sqlite-vec / external indexes consistent with SQLite as source of truth.
|
|
175
|
+
* Optional: if omitted, hosts MUST document "index rebuilt separately" and accept stale ANN until rebuild.
|
|
176
|
+
*/
|
|
177
|
+
onEmbeddingPersisted?(event: {
|
|
178
|
+
entityId: string;
|
|
179
|
+
factId: string;
|
|
180
|
+
vector: Float32Array | null;
|
|
181
|
+
}): void | Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Fallback policy when rankBySimilarity rejects.
|
|
185
|
+
*/
|
|
186
|
+
type VectorRankerFallback = 'js-cosine' | 'keyword' | 'empty' | 'throw';
|
|
135
187
|
interface WikiOptions {
|
|
136
188
|
config?: WikiConfig;
|
|
137
189
|
llmProvider: LLMProvider;
|
|
138
190
|
/**
|
|
139
|
-
* Called when embedding-based retrieval is unavailable during `read()
|
|
140
|
-
*
|
|
141
|
-
* - `embed()` throws (e.g. network error, model unavailable)
|
|
142
|
-
* - `embed()` returns a vector with non-finite values (NaN / Infinity)
|
|
191
|
+
* Called when embedding-based retrieval is degraded or unavailable during `read()`.
|
|
192
|
+
* This can happen when:
|
|
193
|
+
* - `embed()` throws (e.g. network error, model unavailable) → falls back to keyword search
|
|
194
|
+
* - `embed()` returns a vector with non-finite values (NaN / Infinity) → falls back to keyword search
|
|
143
195
|
* - The query vector's dimension doesn't match stored embeddings (model switch;
|
|
144
|
-
* resolve by calling `runReembed()`)
|
|
196
|
+
* resolve by calling `runReembed()`) → falls back to keyword search
|
|
197
|
+
* - `vectorRanker` returns IDs that don't belong to the requested entity or don't exist
|
|
198
|
+
* (ranker integrity issue; returned rows will be filtered out, reducing result count) →
|
|
199
|
+
* may still use semantic ranking, but with degraded quality
|
|
145
200
|
*
|
|
146
|
-
* `read()`
|
|
201
|
+
* `read()` returns results (keyword fallback or degraded semantic) — this is a notification, not an error path.
|
|
147
202
|
*/
|
|
148
203
|
onRetrievalFallback?: (error: Error) => void;
|
|
204
|
+
/**
|
|
205
|
+
* Optional backend for semantic candidate scoring / top-k retrieval.
|
|
206
|
+
* When omitted, WikiMemory scores rows with embedding_blob / embedding TEXT in JS (cosine).
|
|
207
|
+
*/
|
|
208
|
+
vectorRanker?: VectorRanker;
|
|
209
|
+
/**
|
|
210
|
+
* When rankBySimilarity throws. Default `'js-cosine'`.
|
|
211
|
+
* Ignored when vectorRanker is undefined.
|
|
212
|
+
*/
|
|
213
|
+
vectorRankerFallback?: VectorRankerFallback;
|
|
214
|
+
/**
|
|
215
|
+
* Called only when rankBySimilarity rejects (after embeddings path succeeded).
|
|
216
|
+
* Invoked before applying vectorRankerFallback when that policy recovers or before rejecting when policy is 'throw'.
|
|
217
|
+
*/
|
|
218
|
+
onVectorRankerFallback?: (info: {
|
|
219
|
+
error: Error;
|
|
220
|
+
/** Effective policy core will apply for this read (same as WikiOptions.vectorRankerFallback, default js-cosine). */
|
|
221
|
+
policy: VectorRankerFallback;
|
|
222
|
+
}) => void;
|
|
223
|
+
/**
|
|
224
|
+
* When true: after rankBySimilarity failure, once the recoverable fallback has finished
|
|
225
|
+
* and read() will resolve, invoke onRetrievalFallback — after onVectorRankerFallback if set.
|
|
226
|
+
* Ignored when vectorRankerFallback is 'throw'. Default false.
|
|
227
|
+
*/
|
|
228
|
+
propagateRankerFailureToRetrievalFallback?: boolean;
|
|
149
229
|
}
|
|
150
230
|
interface MemoryBundle {
|
|
151
231
|
facts: WikiFact[];
|
|
@@ -234,6 +314,7 @@ declare class WikiMemory {
|
|
|
234
314
|
private _librarianKey;
|
|
235
315
|
private _healKey;
|
|
236
316
|
private _warnCrossEntityCollision;
|
|
317
|
+
private _notifyEmbeddingPersisted;
|
|
237
318
|
constructor(db: SQLiteAdapter, options: WikiOptions);
|
|
238
319
|
setup(): Promise<void>;
|
|
239
320
|
hasChanged(entityId: string, sourceRef: string, sourceHash: string): Promise<boolean>;
|
|
@@ -261,6 +342,26 @@ declare class WikiMemory {
|
|
|
261
342
|
events: number;
|
|
262
343
|
}>;
|
|
263
344
|
read(entityId: string, query: string, options?: ReadOptions): Promise<MemoryBundle>;
|
|
345
|
+
/**
|
|
346
|
+
* Stable tie-break sort: score desc → access_count desc → updated_at desc → id asc.
|
|
347
|
+
*/
|
|
348
|
+
private _tieBreakSort;
|
|
349
|
+
/**
|
|
350
|
+
* Comparator for score + deterministic tie-break fields.
|
|
351
|
+
* Negative return means "a ranks ahead of b" for descending score order.
|
|
352
|
+
*/
|
|
353
|
+
private _compareScoredRows;
|
|
354
|
+
/**
|
|
355
|
+
* Score candidate rows using in-process JS cosine similarity.
|
|
356
|
+
* Applies hybrid blending (if weight set) and tie-break sorting before returning.
|
|
357
|
+
*/
|
|
358
|
+
private _rankWithJsCosine;
|
|
359
|
+
/**
|
|
360
|
+
* Delegate semantic ranking to the injected VectorRanker.
|
|
361
|
+
* Caller should pass an oversampledLimit to preserve recall after re-ranking.
|
|
362
|
+
* Returns scored results ready for hybrid blending and tie-break sorting.
|
|
363
|
+
*/
|
|
364
|
+
private _rankWithVectorRanker;
|
|
264
365
|
getMemoryBundle(entityId: string): Promise<MemoryBundle>;
|
|
265
366
|
write(entityId: string, event: Omit<WikiEvent, 'id' | 'entity_id' | 'created_at'>): Promise<void>;
|
|
266
367
|
private runLibrarianThenMaybeHeal;
|
|
@@ -315,4 +416,4 @@ declare function formatMemoryDump(dump: MemoryDump): FormattedMemoryDump;
|
|
|
315
416
|
|
|
316
417
|
declare function createWiki(db: SQLiteAdapter, options: WikiOptions): WikiMemory;
|
|
317
418
|
|
|
318
|
-
export { type EntityStatus, type ExtractedFact, type ExtractedTask, type FormatContextOptions, type FormattedMemoryDump, type LLMProvider, type MemoryBundle, type MemoryDump, type ReadOptions, type SQLiteAdapter, WikiBusyError, type WikiBusyOperation, type WikiCheckpoint, type WikiConfig, type WikiEvent, type WikiFact, WikiMemory, type WikiOptions, type WikiTask, createWiki, formatContext, formatMemoryDump };
|
|
419
|
+
export { type EntityStatus, type ExtractedFact, type ExtractedTask, type FormatContextOptions, type FormattedMemoryDump, type LLMProvider, type MemoryBundle, type MemoryDump, type ReadOptions, type SQLiteAdapter, type VectorRanker, type VectorRankerFallback, type VectorRankerRankArgs, type VectorRankerSemanticResult, WikiBusyError, type WikiBusyOperation, type WikiCheckpoint, type WikiConfig, type WikiEvent, type WikiFact, WikiMemory, type WikiOptions, type WikiTask, createWiki, formatContext, formatMemoryDump };
|
package/dist/index.d.ts
CHANGED
|
@@ -132,20 +132,100 @@ interface LLMProvider {
|
|
|
132
132
|
*/
|
|
133
133
|
embed?: (text: string) => Promise<number[]>;
|
|
134
134
|
}
|
|
135
|
+
/**
|
|
136
|
+
* Result of semantic ranking for a single fact.
|
|
137
|
+
*/
|
|
138
|
+
interface VectorRankerSemanticResult {
|
|
139
|
+
id: string;
|
|
140
|
+
/** Cosine similarity in [-1, 1] when exact; implementations MAY document other monotonic scales. */
|
|
141
|
+
semanticScore: number;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Arguments passed to VectorRanker.rankBySimilarity.
|
|
145
|
+
*/
|
|
146
|
+
interface VectorRankerRankArgs {
|
|
147
|
+
entityId: string;
|
|
148
|
+
queryVec: Float32Array | number[];
|
|
149
|
+
/**
|
|
150
|
+
* When set (MiniSearch pre-filter path): ranker MUST only produce results for ids in this set.
|
|
151
|
+
* When omitted (full-entity semantic path): ranker scopes by entityId per its backing store contract.
|
|
152
|
+
*/
|
|
153
|
+
candidateIds?: readonly string[];
|
|
154
|
+
/**
|
|
155
|
+
* Upper bound on how many distinct fact ids should receive a semanticScore in this call.
|
|
156
|
+
* WikiMemory derives this from maxResults / candidate cardinality / documented oversampling policy.
|
|
157
|
+
*/
|
|
158
|
+
limit: number;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Optional backend for semantic candidate scoring / top-k retrieval.
|
|
162
|
+
* When omitted, WikiMemory scores rows with embedding_blob / embedding TEXT in JS (cosine).
|
|
163
|
+
*/
|
|
164
|
+
interface VectorRanker {
|
|
165
|
+
/**
|
|
166
|
+
* Return semantic scores for facts in scope, sorted descending by semanticScore (stable tie-breaking
|
|
167
|
+
* not required — WikiMemory reapplies existing tie-breakers after blending).
|
|
168
|
+
* Implementations SHOULD omit facts with no usable vector; callers treat missing ids like today's
|
|
169
|
+
* "no embedding" rows (pure semantic: -2; hybrid: keyword-only portion).
|
|
170
|
+
*/
|
|
171
|
+
rankBySimilarity(args: VectorRankerRankArgs): Promise<VectorRankerSemanticResult[]>;
|
|
172
|
+
/**
|
|
173
|
+
* Called after a fact's embedding is successfully persisted to embedding_blob (or cleared).
|
|
174
|
+
* Hosts use this to keep sqlite-vec / external indexes consistent with SQLite as source of truth.
|
|
175
|
+
* Optional: if omitted, hosts MUST document "index rebuilt separately" and accept stale ANN until rebuild.
|
|
176
|
+
*/
|
|
177
|
+
onEmbeddingPersisted?(event: {
|
|
178
|
+
entityId: string;
|
|
179
|
+
factId: string;
|
|
180
|
+
vector: Float32Array | null;
|
|
181
|
+
}): void | Promise<void>;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Fallback policy when rankBySimilarity rejects.
|
|
185
|
+
*/
|
|
186
|
+
type VectorRankerFallback = 'js-cosine' | 'keyword' | 'empty' | 'throw';
|
|
135
187
|
interface WikiOptions {
|
|
136
188
|
config?: WikiConfig;
|
|
137
189
|
llmProvider: LLMProvider;
|
|
138
190
|
/**
|
|
139
|
-
* Called when embedding-based retrieval is unavailable during `read()
|
|
140
|
-
*
|
|
141
|
-
* - `embed()` throws (e.g. network error, model unavailable)
|
|
142
|
-
* - `embed()` returns a vector with non-finite values (NaN / Infinity)
|
|
191
|
+
* Called when embedding-based retrieval is degraded or unavailable during `read()`.
|
|
192
|
+
* This can happen when:
|
|
193
|
+
* - `embed()` throws (e.g. network error, model unavailable) → falls back to keyword search
|
|
194
|
+
* - `embed()` returns a vector with non-finite values (NaN / Infinity) → falls back to keyword search
|
|
143
195
|
* - The query vector's dimension doesn't match stored embeddings (model switch;
|
|
144
|
-
* resolve by calling `runReembed()`)
|
|
196
|
+
* resolve by calling `runReembed()`) → falls back to keyword search
|
|
197
|
+
* - `vectorRanker` returns IDs that don't belong to the requested entity or don't exist
|
|
198
|
+
* (ranker integrity issue; returned rows will be filtered out, reducing result count) →
|
|
199
|
+
* may still use semantic ranking, but with degraded quality
|
|
145
200
|
*
|
|
146
|
-
* `read()`
|
|
201
|
+
* `read()` returns results (keyword fallback or degraded semantic) — this is a notification, not an error path.
|
|
147
202
|
*/
|
|
148
203
|
onRetrievalFallback?: (error: Error) => void;
|
|
204
|
+
/**
|
|
205
|
+
* Optional backend for semantic candidate scoring / top-k retrieval.
|
|
206
|
+
* When omitted, WikiMemory scores rows with embedding_blob / embedding TEXT in JS (cosine).
|
|
207
|
+
*/
|
|
208
|
+
vectorRanker?: VectorRanker;
|
|
209
|
+
/**
|
|
210
|
+
* When rankBySimilarity throws. Default `'js-cosine'`.
|
|
211
|
+
* Ignored when vectorRanker is undefined.
|
|
212
|
+
*/
|
|
213
|
+
vectorRankerFallback?: VectorRankerFallback;
|
|
214
|
+
/**
|
|
215
|
+
* Called only when rankBySimilarity rejects (after embeddings path succeeded).
|
|
216
|
+
* Invoked before applying vectorRankerFallback when that policy recovers or before rejecting when policy is 'throw'.
|
|
217
|
+
*/
|
|
218
|
+
onVectorRankerFallback?: (info: {
|
|
219
|
+
error: Error;
|
|
220
|
+
/** Effective policy core will apply for this read (same as WikiOptions.vectorRankerFallback, default js-cosine). */
|
|
221
|
+
policy: VectorRankerFallback;
|
|
222
|
+
}) => void;
|
|
223
|
+
/**
|
|
224
|
+
* When true: after rankBySimilarity failure, once the recoverable fallback has finished
|
|
225
|
+
* and read() will resolve, invoke onRetrievalFallback — after onVectorRankerFallback if set.
|
|
226
|
+
* Ignored when vectorRankerFallback is 'throw'. Default false.
|
|
227
|
+
*/
|
|
228
|
+
propagateRankerFailureToRetrievalFallback?: boolean;
|
|
149
229
|
}
|
|
150
230
|
interface MemoryBundle {
|
|
151
231
|
facts: WikiFact[];
|
|
@@ -234,6 +314,7 @@ declare class WikiMemory {
|
|
|
234
314
|
private _librarianKey;
|
|
235
315
|
private _healKey;
|
|
236
316
|
private _warnCrossEntityCollision;
|
|
317
|
+
private _notifyEmbeddingPersisted;
|
|
237
318
|
constructor(db: SQLiteAdapter, options: WikiOptions);
|
|
238
319
|
setup(): Promise<void>;
|
|
239
320
|
hasChanged(entityId: string, sourceRef: string, sourceHash: string): Promise<boolean>;
|
|
@@ -261,6 +342,26 @@ declare class WikiMemory {
|
|
|
261
342
|
events: number;
|
|
262
343
|
}>;
|
|
263
344
|
read(entityId: string, query: string, options?: ReadOptions): Promise<MemoryBundle>;
|
|
345
|
+
/**
|
|
346
|
+
* Stable tie-break sort: score desc → access_count desc → updated_at desc → id asc.
|
|
347
|
+
*/
|
|
348
|
+
private _tieBreakSort;
|
|
349
|
+
/**
|
|
350
|
+
* Comparator for score + deterministic tie-break fields.
|
|
351
|
+
* Negative return means "a ranks ahead of b" for descending score order.
|
|
352
|
+
*/
|
|
353
|
+
private _compareScoredRows;
|
|
354
|
+
/**
|
|
355
|
+
* Score candidate rows using in-process JS cosine similarity.
|
|
356
|
+
* Applies hybrid blending (if weight set) and tie-break sorting before returning.
|
|
357
|
+
*/
|
|
358
|
+
private _rankWithJsCosine;
|
|
359
|
+
/**
|
|
360
|
+
* Delegate semantic ranking to the injected VectorRanker.
|
|
361
|
+
* Caller should pass an oversampledLimit to preserve recall after re-ranking.
|
|
362
|
+
* Returns scored results ready for hybrid blending and tie-break sorting.
|
|
363
|
+
*/
|
|
364
|
+
private _rankWithVectorRanker;
|
|
264
365
|
getMemoryBundle(entityId: string): Promise<MemoryBundle>;
|
|
265
366
|
write(entityId: string, event: Omit<WikiEvent, 'id' | 'entity_id' | 'created_at'>): Promise<void>;
|
|
266
367
|
private runLibrarianThenMaybeHeal;
|
|
@@ -315,4 +416,4 @@ declare function formatMemoryDump(dump: MemoryDump): FormattedMemoryDump;
|
|
|
315
416
|
|
|
316
417
|
declare function createWiki(db: SQLiteAdapter, options: WikiOptions): WikiMemory;
|
|
317
418
|
|
|
318
|
-
export { type EntityStatus, type ExtractedFact, type ExtractedTask, type FormatContextOptions, type FormattedMemoryDump, type LLMProvider, type MemoryBundle, type MemoryDump, type ReadOptions, type SQLiteAdapter, WikiBusyError, type WikiBusyOperation, type WikiCheckpoint, type WikiConfig, type WikiEvent, type WikiFact, WikiMemory, type WikiOptions, type WikiTask, createWiki, formatContext, formatMemoryDump };
|
|
419
|
+
export { type EntityStatus, type ExtractedFact, type ExtractedTask, type FormatContextOptions, type FormattedMemoryDump, type LLMProvider, type MemoryBundle, type MemoryDump, type ReadOptions, type SQLiteAdapter, type VectorRanker, type VectorRankerFallback, type VectorRankerRankArgs, type VectorRankerSemanticResult, WikiBusyError, type WikiBusyOperation, type WikiCheckpoint, type WikiConfig, type WikiEvent, type WikiFact, WikiMemory, type WikiOptions, type WikiTask, createWiki, formatContext, formatMemoryDump };
|