wolbarg 0.3.2 → 0.4.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/CHANGELOG.md +22 -0
- package/dist/index.cjs +2910 -459
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +261 -23
- package/dist/index.d.ts +261 -23
- package/dist/index.js +2910 -460
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Metadata filter AST for advanced recall filtering.
|
|
3
5
|
*/
|
|
@@ -224,8 +226,10 @@ interface PostgresDatabaseConfig {
|
|
|
224
226
|
connectionString?: string;
|
|
225
227
|
/** v0.3 preferred alias for the Postgres connection string. */
|
|
226
228
|
url?: string;
|
|
227
|
-
/** Optional max pool size. Defaults to
|
|
229
|
+
/** Optional max pool size. Defaults to 64. */
|
|
228
230
|
maxPoolSize?: number;
|
|
231
|
+
/** When false, uses asynchronous commit for higher write throughput. */
|
|
232
|
+
durableWrites?: boolean;
|
|
229
233
|
}
|
|
230
234
|
/** Discriminated union of database configs. */
|
|
231
235
|
type DatabaseConfig = SqliteDatabaseConfig | PostgresDatabaseConfig;
|
|
@@ -302,6 +306,30 @@ interface InitOptions {
|
|
|
302
306
|
*/
|
|
303
307
|
llm?: LlmConfig;
|
|
304
308
|
}
|
|
309
|
+
/** Write-time memory dedupe strategy. */
|
|
310
|
+
type MemoryDedupeStrategy = "exact" | "near" | "exact-or-near";
|
|
311
|
+
/** Constructor / per-call memory dedupe configuration. */
|
|
312
|
+
interface MemoryDedupeConfig {
|
|
313
|
+
enabled?: boolean;
|
|
314
|
+
strategy?: MemoryDedupeStrategy;
|
|
315
|
+
/** Cosine similarity threshold for near-dup matching. Default 0.92 */
|
|
316
|
+
nearThreshold?: number;
|
|
317
|
+
/** Max vector candidates considered for near-dup. Default 8 */
|
|
318
|
+
nearCandidateLimit?: number;
|
|
319
|
+
}
|
|
320
|
+
/** SQLite multi-writer concurrency tuning. */
|
|
321
|
+
interface ConcurrencyConfig$1 {
|
|
322
|
+
maxRetries?: number;
|
|
323
|
+
baseBackoffMs?: number;
|
|
324
|
+
maxBackoffMs?: number;
|
|
325
|
+
lockTimeoutMs?: number;
|
|
326
|
+
}
|
|
327
|
+
/** Transparent embedding cache configuration. */
|
|
328
|
+
interface EmbeddingCacheConfig {
|
|
329
|
+
enabled?: boolean;
|
|
330
|
+
ttlMs?: number;
|
|
331
|
+
maxEntries?: number;
|
|
332
|
+
}
|
|
305
333
|
/** Input for {@link Wolbarg.remember}. */
|
|
306
334
|
interface RememberOptions {
|
|
307
335
|
/** Agent identifier that owns this memory. */
|
|
@@ -310,6 +338,14 @@ interface RememberOptions {
|
|
|
310
338
|
content: MemoryContent;
|
|
311
339
|
/** Optional opaque metadata. Stored and returned as-is. */
|
|
312
340
|
metadata?: MemoryMetadata;
|
|
341
|
+
/** Per-call dedupe override; undefined uses constructor `memory.dedupe`. */
|
|
342
|
+
dedupe?: boolean | MemoryDedupeConfig;
|
|
343
|
+
}
|
|
344
|
+
/** Whether remember created a new row or updated an existing one. */
|
|
345
|
+
type RememberAction = "created" | "updated";
|
|
346
|
+
/** Result of {@link Wolbarg.remember} — MemoryRecord plus upsert action. */
|
|
347
|
+
interface RememberResult extends MemoryRecord {
|
|
348
|
+
action: RememberAction;
|
|
313
349
|
}
|
|
314
350
|
/** Optional filter applied to recall / forget / compress operations. */
|
|
315
351
|
interface MemoryFilter {
|
|
@@ -465,7 +501,7 @@ interface HistoryOptions {
|
|
|
465
501
|
interface HistoryEvent {
|
|
466
502
|
id: string;
|
|
467
503
|
memoryId: string;
|
|
468
|
-
eventType: "created" | "archived" | "compressed";
|
|
504
|
+
eventType: "created" | "archived" | "compressed" | "updated";
|
|
469
505
|
relatedMemoryId: string | null;
|
|
470
506
|
createdAt: Date;
|
|
471
507
|
}
|
|
@@ -768,6 +804,7 @@ interface MemoryRow {
|
|
|
768
804
|
metadata_json: string;
|
|
769
805
|
archived: number;
|
|
770
806
|
compressed_into: string | null;
|
|
807
|
+
content_hash?: string | null;
|
|
771
808
|
created_at: string;
|
|
772
809
|
updated_at: string;
|
|
773
810
|
rowid?: number;
|
|
@@ -776,7 +813,7 @@ interface MemoryRow {
|
|
|
776
813
|
interface HistoryRow {
|
|
777
814
|
id: string;
|
|
778
815
|
memory_id: string;
|
|
779
|
-
event_type: "created" | "archived" | "compressed";
|
|
816
|
+
event_type: "created" | "archived" | "compressed" | "updated";
|
|
780
817
|
related_memory_id: string | null;
|
|
781
818
|
created_at: string;
|
|
782
819
|
}
|
|
@@ -790,6 +827,7 @@ interface InsertMemoryInput {
|
|
|
790
827
|
embedding: Float32Array;
|
|
791
828
|
createdAt: string;
|
|
792
829
|
updatedAt: string;
|
|
830
|
+
contentHash?: string | null;
|
|
793
831
|
}
|
|
794
832
|
/** Payload for updating memory content / metadata. */
|
|
795
833
|
interface UpdateMemoryInput {
|
|
@@ -799,6 +837,7 @@ interface UpdateMemoryInput {
|
|
|
799
837
|
metadata?: MemoryMetadata;
|
|
800
838
|
embedding?: Float32Array;
|
|
801
839
|
updatedAt: string;
|
|
840
|
+
contentHash?: string | null;
|
|
802
841
|
}
|
|
803
842
|
/** Filters used by repository queries. */
|
|
804
843
|
interface RepositoryFilter {
|
|
@@ -834,6 +873,11 @@ interface StorageProvider {
|
|
|
834
873
|
insertMemoriesBatch(inputs: InsertMemoryInput[]): Promise<MemoryRow[]>;
|
|
835
874
|
/** Update memory fields and optionally replace embedding. */
|
|
836
875
|
updateMemory(input: UpdateMemoryInput): Promise<MemoryRow | null>;
|
|
876
|
+
/**
|
|
877
|
+
* Find an active (non-archived) memory by content hash within org+agent.
|
|
878
|
+
* Used by write-time exact dedupe.
|
|
879
|
+
*/
|
|
880
|
+
findActiveByContentHash?(organization: string, agent: string, contentHash: string): Promise<MemoryRow | null>;
|
|
837
881
|
/** Fetch a memory by UUID. */
|
|
838
882
|
getMemoryById(id: string, organization: string): Promise<MemoryRow | null>;
|
|
839
883
|
/** Fetch a memory by its integer rowid. */
|
|
@@ -1024,6 +1068,14 @@ interface WolbargOptionsBase {
|
|
|
1024
1068
|
chunking?: ChunkingStrategy;
|
|
1025
1069
|
/** Optional retrieval defaults. */
|
|
1026
1070
|
retrieval?: RetrievalConfig;
|
|
1071
|
+
/** SQLite multi-writer concurrency tuning (ignored for Postgres). */
|
|
1072
|
+
concurrency?: ConcurrencyConfig$1;
|
|
1073
|
+
/** Transparent embedding cache (default enabled). */
|
|
1074
|
+
embeddingCache?: EmbeddingCacheConfig;
|
|
1075
|
+
/** Memory write-path options (dedupe / upsert). */
|
|
1076
|
+
memory?: {
|
|
1077
|
+
dedupe?: MemoryDedupeConfig;
|
|
1078
|
+
};
|
|
1027
1079
|
}
|
|
1028
1080
|
interface WolbargOptionsWithoutLlm extends WolbargOptionsBase {
|
|
1029
1081
|
llm?: undefined;
|
|
@@ -1034,6 +1086,29 @@ interface WolbargOptionsWithLlm extends WolbargOptionsBase {
|
|
|
1034
1086
|
}
|
|
1035
1087
|
type WolbargOptions = WolbargOptionsWithoutLlm | WolbargOptionsWithLlm;
|
|
1036
1088
|
|
|
1089
|
+
/**
|
|
1090
|
+
* Public subscribe() types for real-time memory change events.
|
|
1091
|
+
*/
|
|
1092
|
+
type SubscribableEvent = "remember" | "update" | "forget" | "compress" | "ingest" | "*";
|
|
1093
|
+
interface SubscribeFilter {
|
|
1094
|
+
organization: string;
|
|
1095
|
+
agent?: string;
|
|
1096
|
+
event?: SubscribableEvent | SubscribableEvent[];
|
|
1097
|
+
}
|
|
1098
|
+
interface MemoryChangeEvent {
|
|
1099
|
+
event: Exclude<SubscribableEvent, "*">;
|
|
1100
|
+
organization: string;
|
|
1101
|
+
agent: string;
|
|
1102
|
+
memoryId: string | string[];
|
|
1103
|
+
timestamp: string;
|
|
1104
|
+
traceId?: string;
|
|
1105
|
+
sessionId?: string;
|
|
1106
|
+
/** Present when upsert path ran during remember/ingest. */
|
|
1107
|
+
upsertAction?: "created" | "updated" | "skipped";
|
|
1108
|
+
}
|
|
1109
|
+
type MemoryChangeCallback = (event: MemoryChangeEvent) => void;
|
|
1110
|
+
type Unsubscribe = () => void;
|
|
1111
|
+
|
|
1037
1112
|
/**
|
|
1038
1113
|
* Wolbarg — modular semantic memory SDK for AI agents (v0.3).
|
|
1039
1114
|
*
|
|
@@ -1082,6 +1157,11 @@ declare class Wolbarg<HasLlm extends boolean = false> {
|
|
|
1082
1157
|
private checkpointProvider;
|
|
1083
1158
|
private memoryDbPath;
|
|
1084
1159
|
private readonly transfer;
|
|
1160
|
+
private subscribeBackend;
|
|
1161
|
+
private pgListenBackend;
|
|
1162
|
+
private memoryDedupe;
|
|
1163
|
+
private embeddingCacheConfig;
|
|
1164
|
+
private rawEmbedding;
|
|
1085
1165
|
constructor(options: WolbargOptionsWithLlm);
|
|
1086
1166
|
constructor(options: WolbargOptionsWithoutLlm);
|
|
1087
1167
|
constructor();
|
|
@@ -1092,10 +1172,31 @@ declare class Wolbarg<HasLlm extends boolean = false> {
|
|
|
1092
1172
|
/** Ensure storage (and optional telemetry) are open. */
|
|
1093
1173
|
ready(): Promise<void>;
|
|
1094
1174
|
private boot;
|
|
1095
|
-
/** Store a semantic memory for an agent. */
|
|
1096
|
-
remember(options: RememberOptions): Promise<
|
|
1097
|
-
/** Batch remember —
|
|
1098
|
-
rememberBatch(items: RememberOptions[]): Promise<
|
|
1175
|
+
/** Store a semantic memory for an agent (may upsert when dedupe is enabled). */
|
|
1176
|
+
remember(options: RememberOptions): Promise<RememberResult>;
|
|
1177
|
+
/** Batch remember — sequential when dedupe enabled; otherwise one TX batch. */
|
|
1178
|
+
rememberBatch(items: RememberOptions[]): Promise<RememberResult[]>;
|
|
1179
|
+
/**
|
|
1180
|
+
* Update an existing memory by id (re-embeds when content changes).
|
|
1181
|
+
*/
|
|
1182
|
+
update(options: {
|
|
1183
|
+
id: string;
|
|
1184
|
+
content?: {
|
|
1185
|
+
text: string;
|
|
1186
|
+
};
|
|
1187
|
+
metadata?: MemoryMetadata;
|
|
1188
|
+
}): Promise<RememberResult>;
|
|
1189
|
+
/**
|
|
1190
|
+
* Subscribe to memory change events.
|
|
1191
|
+
*
|
|
1192
|
+
* **SQLite:** delivers events only within this Node.js process.
|
|
1193
|
+
* A second process writing the same `memory.db` will not notify subscribers here.
|
|
1194
|
+
*/
|
|
1195
|
+
subscribe(filter: SubscribeFilter, callback: (event: MemoryChangeEvent) => void): Unsubscribe;
|
|
1196
|
+
private emitChange;
|
|
1197
|
+
/** Internal remember with optional upsert/dedupe. */
|
|
1198
|
+
private rememberOne;
|
|
1199
|
+
private findNearDuplicate;
|
|
1099
1200
|
/**
|
|
1100
1201
|
* Semantic / hybrid search over stored memories.
|
|
1101
1202
|
* Pass `{ explain: true }` for enriched ranking diagnostics.
|
|
@@ -1132,6 +1233,11 @@ declare class Wolbarg<HasLlm extends boolean = false> {
|
|
|
1132
1233
|
flushTelemetry(): Promise<void>;
|
|
1133
1234
|
/** Session id for this SDK instance (telemetry traces). */
|
|
1134
1235
|
get sessionId(): string;
|
|
1236
|
+
/**
|
|
1237
|
+
* Optional process-wide lock for SQLite read-modify-write (dedupe upsert).
|
|
1238
|
+
* Plain inserts must NOT take this lock — the provider coalesces concurrent
|
|
1239
|
+
* insertMemory calls under a single BEGIN IMMEDIATE for multi-writer throughput.
|
|
1240
|
+
*/
|
|
1135
1241
|
private withWriteLock;
|
|
1136
1242
|
close(): Promise<void>;
|
|
1137
1243
|
get isInitialized(): boolean;
|
|
@@ -1190,6 +1296,17 @@ declare class DatabaseError extends WolbargError {
|
|
|
1190
1296
|
operation?: string;
|
|
1191
1297
|
});
|
|
1192
1298
|
}
|
|
1299
|
+
/**
|
|
1300
|
+
* Thrown when SQLite write-lock retries are exhausted.
|
|
1301
|
+
* Stable code: WOLBARG_STORAGE_LOCKED
|
|
1302
|
+
*/
|
|
1303
|
+
declare class StorageLockedError extends WolbargError {
|
|
1304
|
+
constructor(message: string, options?: ErrorOptions & {
|
|
1305
|
+
reason?: string;
|
|
1306
|
+
suggestion?: string;
|
|
1307
|
+
operation?: string;
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1193
1310
|
/** Thrown when an embedding request fails. */
|
|
1194
1311
|
declare class EmbeddingError extends WolbargError {
|
|
1195
1312
|
constructor(message: string, options?: ErrorOptions & {
|
|
@@ -1236,10 +1353,13 @@ declare function sqliteConfig(connectionString: string): SqliteDatabaseConfig;
|
|
|
1236
1353
|
declare function postgres(options: string | {
|
|
1237
1354
|
connectionString: string;
|
|
1238
1355
|
maxPoolSize?: number;
|
|
1356
|
+
/** Default true. Set false for higher write throughput (async commit). */
|
|
1357
|
+
durableWrites?: boolean;
|
|
1239
1358
|
}): StorageProvider;
|
|
1240
1359
|
/** Create a PostgreSQL storage config object. */
|
|
1241
1360
|
declare function postgresConfig(connectionString: string, options?: {
|
|
1242
1361
|
maxPoolSize?: number;
|
|
1362
|
+
durableWrites?: boolean;
|
|
1243
1363
|
}): PostgresDatabaseConfig;
|
|
1244
1364
|
/** Create a SQLite telemetry provider for an independent event database. */
|
|
1245
1365
|
declare function sqliteTelemetry(url: string): TelemetryProvider;
|
|
@@ -1252,6 +1372,20 @@ declare function createTelemetryProvider(config: TelemetryConfig): TelemetryProv
|
|
|
1252
1372
|
*/
|
|
1253
1373
|
declare function wolbarg(options: WolbargOptions): Wolbarg;
|
|
1254
1374
|
|
|
1375
|
+
/**
|
|
1376
|
+
* SQLite multi-writer concurrency defaults and validation.
|
|
1377
|
+
*/
|
|
1378
|
+
interface ConcurrencyConfig {
|
|
1379
|
+
/** Max retry attempts after SQLITE_BUSY. Default: 5 */
|
|
1380
|
+
maxRetries?: number;
|
|
1381
|
+
/** Base backoff in ms before jitter. Default: 50 */
|
|
1382
|
+
baseBackoffMs?: number;
|
|
1383
|
+
/** Cap on backoff delay in ms. Default: 2000 */
|
|
1384
|
+
maxBackoffMs?: number;
|
|
1385
|
+
/** SQLite busy_timeout pragma in ms. Default: 5000 */
|
|
1386
|
+
lockTimeoutMs?: number;
|
|
1387
|
+
}
|
|
1388
|
+
|
|
1255
1389
|
/**
|
|
1256
1390
|
* SQLite + sqlite-vec database provider (Node.js built-in `node:sqlite`).
|
|
1257
1391
|
*
|
|
@@ -1266,10 +1400,12 @@ declare function wolbarg(options: WolbargOptions): Wolbarg;
|
|
|
1266
1400
|
|
|
1267
1401
|
interface SqliteProviderOptions {
|
|
1268
1402
|
connectionString: string;
|
|
1403
|
+
concurrency?: ConcurrencyConfig;
|
|
1269
1404
|
}
|
|
1270
1405
|
declare class SqliteStorageProvider implements StorageProvider {
|
|
1271
1406
|
readonly name = "sqlite";
|
|
1272
1407
|
private readonly connectionString;
|
|
1408
|
+
private readonly concurrency;
|
|
1273
1409
|
private db;
|
|
1274
1410
|
private statements;
|
|
1275
1411
|
private vectorDimensions;
|
|
@@ -1280,17 +1416,41 @@ declare class SqliteStorageProvider implements StorageProvider {
|
|
|
1280
1416
|
private memoryIndexDirty;
|
|
1281
1417
|
/** Resolved absolute path (or `:memory:`) — avoid re-resolving on size checks. */
|
|
1282
1418
|
private resolvedPath;
|
|
1419
|
+
private retryLog;
|
|
1420
|
+
/** Cached prepared statements for `rowid IN (…)` lookups keyed by list length. */
|
|
1421
|
+
private rowidInStatements;
|
|
1422
|
+
/** Cached list SQL statements keyed by clause shape. */
|
|
1423
|
+
private listStatements;
|
|
1424
|
+
/** Cached multi-row insert statements keyed by row count. */
|
|
1425
|
+
private batchInsertStatements;
|
|
1426
|
+
/** Cached multi-row history inserts keyed by row count. */
|
|
1427
|
+
private batchHistoryStatements;
|
|
1428
|
+
/** Cached multi-row FTS inserts keyed by row count. */
|
|
1429
|
+
private batchFtsStatements;
|
|
1430
|
+
/** Cached multi-row blob embedding inserts keyed by row count. */
|
|
1431
|
+
private batchBlobEmbStatements;
|
|
1432
|
+
/** Coalesce concurrent insertMemory callers into one IMMEDIATE transaction. */
|
|
1433
|
+
private insertQueue;
|
|
1434
|
+
private insertFlushScheduled;
|
|
1283
1435
|
constructor(options: SqliteProviderOptions);
|
|
1284
1436
|
/** Absolute or relative path / `:memory:` used by this provider. */
|
|
1285
1437
|
get path(): string;
|
|
1438
|
+
/** Expose DB for embedding cache store (same connection). */
|
|
1439
|
+
getDatabase(): DatabaseSync | null;
|
|
1440
|
+
setRetryLogger(fn: ((msg: string) => void) | null): void;
|
|
1286
1441
|
open(): Promise<void>;
|
|
1287
1442
|
close(): Promise<void>;
|
|
1288
1443
|
ensureVectorSchema(dimensions: number): Promise<void>;
|
|
1289
1444
|
getEmbeddingDimensions(): Promise<number | null>;
|
|
1290
1445
|
setEmbeddingDimensions(dimensions: number): Promise<void>;
|
|
1291
1446
|
insertMemory(input: InsertMemoryInput): Promise<MemoryRow>;
|
|
1447
|
+
private scheduleInsertFlush;
|
|
1448
|
+
private flushInsertQueue;
|
|
1449
|
+
/** Single-row insert without coalescing (used by flush of size 1). */
|
|
1450
|
+
private insertMemoryImmediate;
|
|
1292
1451
|
insertMemoriesBatch(inputs: InsertMemoryInput[]): Promise<MemoryRow[]>;
|
|
1293
1452
|
updateMemory(input: UpdateMemoryInput): Promise<MemoryRow | null>;
|
|
1453
|
+
findActiveByContentHash(organization: string, agent: string, contentHash: string): Promise<MemoryRow | null>;
|
|
1294
1454
|
searchByMetadata(filter: RepositoryFilter, limit?: number): Promise<MemoryRow[]>;
|
|
1295
1455
|
/** Keyword search via FTS5 BM25. Returns memory IDs ranked by relevance. */
|
|
1296
1456
|
searchKeyword(query: string, organization: string, topK: number): Promise<Array<{
|
|
@@ -1303,8 +1463,8 @@ declare class SqliteStorageProvider implements StorageProvider {
|
|
|
1303
1463
|
listMemories(filter: RepositoryFilter, limit?: number): Promise<MemoryRow[]>;
|
|
1304
1464
|
searchVectors(embedding: Float32Array, topK: number): Promise<VectorSearchHit[]>;
|
|
1305
1465
|
/**
|
|
1306
|
-
* Org-scoped KNN + memory rows.
|
|
1307
|
-
*
|
|
1466
|
+
* Org-scoped KNN + memory rows with adaptive overfetch.
|
|
1467
|
+
* Global ANN is post-filtered by org/agent/archived; underfill triggers larger k.
|
|
1308
1468
|
*/
|
|
1309
1469
|
searchVectorsWithMemories(embedding: Float32Array, topK: number, organization: string, options?: {
|
|
1310
1470
|
agent?: string;
|
|
@@ -1326,9 +1486,21 @@ declare class SqliteStorageProvider implements StorageProvider {
|
|
|
1326
1486
|
totalAgents: number;
|
|
1327
1487
|
}>;
|
|
1328
1488
|
getDatabaseSizeBytes(): Promise<number>;
|
|
1329
|
-
withTransaction<T>(fn: () => T): T
|
|
1489
|
+
withTransaction<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
1330
1490
|
private tryLoadSqliteVec;
|
|
1491
|
+
private static sqliteVecUnsupported;
|
|
1331
1492
|
private runMigrations;
|
|
1493
|
+
/**
|
|
1494
|
+
* Schema v4: drop unused global created_at index, strip archived vectors
|
|
1495
|
+
* from ANN, add agent-active covering index (via CREATE_INDEXES).
|
|
1496
|
+
*/
|
|
1497
|
+
private migrateToV4;
|
|
1498
|
+
/**
|
|
1499
|
+
* Schema v3: content_hash column, history 'updated' event, embedding_cache.
|
|
1500
|
+
* SQLite cannot ALTER CHECK constraints — rebuild memory_history when needed.
|
|
1501
|
+
*/
|
|
1502
|
+
private migrateToV3;
|
|
1503
|
+
private migrateHistoryAllowUpdated;
|
|
1332
1504
|
/**
|
|
1333
1505
|
* Rebuild FTS from active (non-archived) memories when counts diverge.
|
|
1334
1506
|
* Guarantees keyword/hybrid correctness after crashes or interrupted writes.
|
|
@@ -1336,7 +1508,19 @@ declare class SqliteStorageProvider implements StorageProvider {
|
|
|
1336
1508
|
private ensureFtsConsistency;
|
|
1337
1509
|
private backfillFts;
|
|
1338
1510
|
private prepareStatements;
|
|
1339
|
-
private
|
|
1511
|
+
private listMemoriesIndexed;
|
|
1512
|
+
/**
|
|
1513
|
+
* Metadata-filtered list: push filters to json_extract when possible;
|
|
1514
|
+
* otherwise page with a hard scan cap (never load unbounded orgs into RAM).
|
|
1515
|
+
*/
|
|
1516
|
+
private listMemoriesWithMetadata;
|
|
1517
|
+
private getRowidInStatement;
|
|
1518
|
+
private insertMemoryChunk;
|
|
1519
|
+
private insertEmbeddingsBatch;
|
|
1520
|
+
private insertFtsBatch;
|
|
1521
|
+
private insertHistoryBatch;
|
|
1522
|
+
private deleteEmbeddingsForScope;
|
|
1523
|
+
private deleteFtsForScope;
|
|
1340
1524
|
/** Insert-only FTS row (new memory IDs never collide). */
|
|
1341
1525
|
private insertFtsRow;
|
|
1342
1526
|
private upsertFts;
|
|
@@ -1371,30 +1555,63 @@ declare const SqliteDatabaseProvider: typeof SqliteStorageProvider;
|
|
|
1371
1555
|
* - AsyncLocalStorage for nested transactions
|
|
1372
1556
|
* - Single-statement CTE inserts (no BEGIN/COMMIT for one memory)
|
|
1373
1557
|
* - Named prepared statements (parse/plan once per connection)
|
|
1374
|
-
* - Concurrent insert coalescing into unnest batches
|
|
1375
|
-
* -
|
|
1376
|
-
* - Org
|
|
1377
|
-
* -
|
|
1558
|
+
* - Concurrent insert coalescing into unnest batches
|
|
1559
|
+
* - float4[] vector bind (no text "[f,f,…]" parse tax)
|
|
1560
|
+
* - Org/agent/archived denormalized on embeddings for filtered ANN
|
|
1561
|
+
* - Large bulk inserts via sequential fat unnest (COPY-class amortization)
|
|
1562
|
+
* - JSONB metadata filter pushdown
|
|
1378
1563
|
*/
|
|
1379
1564
|
|
|
1565
|
+
type PgQueryResult = {
|
|
1566
|
+
rows: Record<string, unknown>[];
|
|
1567
|
+
rowCount: number | null;
|
|
1568
|
+
};
|
|
1569
|
+
type PgQueryable = {
|
|
1570
|
+
query: (textOrConfig: string | {
|
|
1571
|
+
name?: string;
|
|
1572
|
+
text: string;
|
|
1573
|
+
values?: unknown[];
|
|
1574
|
+
}, params?: unknown[]) => Promise<PgQueryResult>;
|
|
1575
|
+
};
|
|
1576
|
+
type PgPoolClient = PgQueryable & {
|
|
1577
|
+
release: () => void;
|
|
1578
|
+
query: PgQueryable["query"];
|
|
1579
|
+
};
|
|
1580
|
+
type PgPool = PgQueryable & {
|
|
1581
|
+
end: () => Promise<void>;
|
|
1582
|
+
connect: () => Promise<PgPoolClient>;
|
|
1583
|
+
on?: (event: "connect", listener: (client: PgPoolClient) => void) => void;
|
|
1584
|
+
totalCount?: number;
|
|
1585
|
+
idleCount?: number;
|
|
1586
|
+
waitingCount?: number;
|
|
1587
|
+
};
|
|
1380
1588
|
interface PostgresProviderOptions {
|
|
1381
1589
|
connectionString: string;
|
|
1382
1590
|
maxPoolSize?: number;
|
|
1591
|
+
/**
|
|
1592
|
+
* When false, uses `synchronous_commit=off` on every pool connection.
|
|
1593
|
+
* Much higher write throughput; recent commits can be lost on OS crash.
|
|
1594
|
+
* Default true (full durability).
|
|
1595
|
+
*/
|
|
1596
|
+
durableWrites?: boolean;
|
|
1383
1597
|
}
|
|
1384
1598
|
declare class PostgresStorageProvider implements StorageProvider {
|
|
1385
1599
|
readonly name = "postgres";
|
|
1386
1600
|
private readonly connectionString;
|
|
1387
1601
|
private readonly maxPoolSize;
|
|
1602
|
+
private readonly durableWrites;
|
|
1388
1603
|
private pool;
|
|
1389
1604
|
private vectorDimensions;
|
|
1390
1605
|
private hasPgvector;
|
|
1391
1606
|
private hnswIndexEnsured;
|
|
1392
1607
|
private hnswCreateFailures;
|
|
1608
|
+
/** Dedup concurrent CREATE INDEX so mixed read/write storms don't pile up. */
|
|
1609
|
+
private hnswBuildInFlight;
|
|
1393
1610
|
private hasContentTsv;
|
|
1394
|
-
private iterativeScanEnabled;
|
|
1395
1611
|
/** Coalesce concurrent insertMemory callers into one unnest batch. */
|
|
1396
1612
|
private insertQueue;
|
|
1397
1613
|
private insertFlushScheduled;
|
|
1614
|
+
private insertFlushTimer;
|
|
1398
1615
|
private insertFlushInFlight;
|
|
1399
1616
|
constructor(options: PostgresProviderOptions);
|
|
1400
1617
|
getPoolStats(): {
|
|
@@ -1403,10 +1620,26 @@ declare class PostgresStorageProvider implements StorageProvider {
|
|
|
1403
1620
|
idle: number;
|
|
1404
1621
|
waiting: number;
|
|
1405
1622
|
};
|
|
1623
|
+
/** Dedicated pool accessor for LISTEN/NOTIFY subscribe backend. */
|
|
1624
|
+
getPool(): PgPool | null;
|
|
1406
1625
|
open(): Promise<void>;
|
|
1407
1626
|
close(): Promise<void>;
|
|
1627
|
+
/**
|
|
1628
|
+
* Run a search query. HNSW GUCs are baked into pool startup options —
|
|
1629
|
+
* no per-recall connect()+set_config round-trip.
|
|
1630
|
+
*/
|
|
1631
|
+
private withSearchSession;
|
|
1632
|
+
/**
|
|
1633
|
+
* Drop HNSW so bulk / concurrent inserts avoid graph maintenance.
|
|
1634
|
+
* Next recall rebuilds the index (lazy).
|
|
1635
|
+
*/
|
|
1636
|
+
dropVectorIndex(): Promise<void>;
|
|
1637
|
+
/** Force HNSW build now (e.g. before timed recall benches). */
|
|
1638
|
+
ensureVectorIndex(): Promise<void>;
|
|
1408
1639
|
ensureVectorSchema(dimensions: number): Promise<void>;
|
|
1409
1640
|
private ensureVectorTables;
|
|
1641
|
+
/** Cross-process subscribe: NOTIFY after a committed write. */
|
|
1642
|
+
notifyChange(event: MemoryChangeEvent): Promise<void>;
|
|
1410
1643
|
/**
|
|
1411
1644
|
* Soft reset for a single organization. Drops HNSW only when the embeddings
|
|
1412
1645
|
* table is empty so other corpora on a shared bench DB stay intact.
|
|
@@ -1418,18 +1651,23 @@ declare class PostgresStorageProvider implements StorageProvider {
|
|
|
1418
1651
|
wipeAllData(): Promise<void>;
|
|
1419
1652
|
/** Build HNSW once before the first KNN query (bulk-friendly inserts). */
|
|
1420
1653
|
private ensureHnswIndex;
|
|
1654
|
+
private buildHnswIndex;
|
|
1421
1655
|
getEmbeddingDimensions(): Promise<number | null>;
|
|
1422
1656
|
setEmbeddingDimensions(dimensions: number): Promise<void>;
|
|
1423
1657
|
insertMemory(input: InsertMemoryInput): Promise<MemoryRow>;
|
|
1658
|
+
private clearInsertFlushTimer;
|
|
1659
|
+
/** Flush coalesced inserts after a turn so concurrent remember() can join. */
|
|
1660
|
+
private scheduleInsertFlush;
|
|
1424
1661
|
private flushInsertQueue;
|
|
1425
1662
|
/** Single-row insert without coalescing (used by flush + batch of 1). */
|
|
1426
1663
|
private insertMemoryImmediate;
|
|
1427
1664
|
insertMemoriesBatch(inputs: InsertMemoryInput[]): Promise<MemoryRow[]>;
|
|
1428
1665
|
private insertBatchPgvector;
|
|
1429
|
-
/**
|
|
1666
|
+
/** Parallel unnest when HNSW is absent; sequential when index must stay consistent. */
|
|
1430
1667
|
private insertBatchChunked;
|
|
1431
1668
|
private insertOneBlob;
|
|
1432
1669
|
updateMemory(input: UpdateMemoryInput): Promise<MemoryRow | null>;
|
|
1670
|
+
findActiveByContentHash(organization: string, agent: string, contentHash: string): Promise<MemoryRow | null>;
|
|
1433
1671
|
getMemoryById(id: string, organization: string): Promise<MemoryRow | null>;
|
|
1434
1672
|
getMemoryByRowid(rowid: number, organization: string): Promise<MemoryRow | null>;
|
|
1435
1673
|
getMemoriesByRowids(rowids: number[], organization: string): Promise<Map<number, MemoryRow>>;
|
|
@@ -1463,6 +1701,8 @@ declare class PostgresStorageProvider implements StorageProvider {
|
|
|
1463
1701
|
withTransaction<T>(fn: () => T | Promise<T>): Promise<T>;
|
|
1464
1702
|
private runMigrations;
|
|
1465
1703
|
private tryEnablePgvector;
|
|
1704
|
+
private static pgvectorByConn;
|
|
1705
|
+
private static vectorSchemaReady;
|
|
1466
1706
|
private insertEmbedding;
|
|
1467
1707
|
private deleteEmbedding;
|
|
1468
1708
|
private query;
|
|
@@ -1474,11 +1714,9 @@ declare class PostgresStorageProvider implements StorageProvider {
|
|
|
1474
1714
|
private describe;
|
|
1475
1715
|
}
|
|
1476
1716
|
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
declare function createStorageProvider(config: StorageConfig | DatabaseConfig): StorageProvider;
|
|
1717
|
+
declare function createStorageProvider(config: StorageConfig | DatabaseConfig, options?: {
|
|
1718
|
+
concurrency?: ConcurrencyConfig$1;
|
|
1719
|
+
}): StorageProvider;
|
|
1482
1720
|
/** @deprecated Prefer {@link createStorageProvider}. */
|
|
1483
1721
|
declare function createDatabaseProvider(config: DatabaseConfig): StorageProvider;
|
|
1484
1722
|
|
|
@@ -1690,4 +1928,4 @@ interface BenchmarkReport {
|
|
|
1690
1928
|
declare function runBenchmark(name: string, iterations: number, fn: () => Promise<void> | void): Promise<BenchmarkSample[]>;
|
|
1691
1929
|
declare function summarizeBenchmark(samples: BenchmarkSample[], startedAt: string, finishedAt: string): BenchmarkReport;
|
|
1692
1930
|
|
|
1693
|
-
export { type BenchmarkReport, type BenchmarkSample, type ChatMessage, type CheckpointInfo, type CheckpointMeta, type CheckpointOptions, type CheckpointProvider, type Chunk, type ChunkingOptions, type ChunkingStrategy, type ClearOptions, type CompressOptions, type CompressResult, CompressionError, type CompressionProvider, ConfigurationError, type CreateCheckpointOptions, type DatabaseConfig, DatabaseError, type DatabaseProvider, type DatabaseProviderName, type EmbeddingConfig, EmbeddingError, type EmbeddingProvider, type EventDatabase, type ExportResult, FixedChunkingStrategy, type ForgetByFilterOptions, type ForgetByIdOptions, type ForgetOptions, HeadingChunkingStrategy, type HistoryEvent, type HistoryOptions, type HistoryResult, type HybridConfig, type ImportResult, type IngestOptions, type IngestResult, type InitOptions, InitializationError, type KeywordDocument, type KeywordSearchHit, type KeywordSearchProvider, type LatencyBreakdown, LlmCompressionProvider, type LlmConfig, type LlmProvider, MarkdownChunkingStrategy, type MemoryContent, type MemoryFilter, type MemoryMetadata, MemoryNotFoundError, type MemoryProvider, type MemoryRecord, type MetadataFilter as MetaFilter, type MetadataComparison, type MetadataFilter, type MmrConfig, NoopTelemetryProvider, type OCRProvider, type OcrResult, ParagraphChunkingStrategy, type PersistedRecallExplainPayload, type PostgresDatabaseConfig, PostgresStorageProvider, ProviderNotConfiguredError, type RecallExplainResponse, type RecallExplanationHit, type RecallOptions, type RecallResult, type RememberOptions, type RerankDocument, type RerankHit, type RerankerProvider, type RetrievalConfig, SentenceChunkingStrategy, SqliteCheckpointProvider, type SqliteDatabaseConfig, SqliteDatabaseProvider, SqliteEventDatabase, SqliteStorageProvider, SqliteTelemetryProvider, type StageSpan, type StatsResult, type StorageConfig, type StorageProvider, type StorageProviderName, type TelemetryConfig, TelemetryEmitter, type TelemetryEvent, type TelemetryEventInput, type TelemetryLogLevel, type TelemetryOperation, type TelemetryProvider, type TelemetryQuery, type TelemetryQueryResult, type TelemetryStatus, ValidationError, type VisionProvider, type VisionResult, Wolbarg, WolbargError, WolbargLogger, type WolbargOptions, type WolbargOptionsWithLlm, type WolbargOptionsWithoutLlm, bgeReranker, bm25, cohereReranker, createChunkingStrategy, createCompressionProvider, createDatabaseProvider, createEmbeddingProvider, createLlmProvider, createStorageProvider, createTelemetryProvider, wolbarg as createWolbarg, crossEncoder, geminiEmbedding, geminiVision, jinaReranker, lmStudioEmbedding, meta, ollamaEmbedding, ollamaLlm, openRouterEmbedding, openRouterLlm, openaiCompatibleEmbedding, openaiCompatibleLlm, openaiEmbedding, openaiLlm, openaiReranker, openaiVision, postgres, postgresConfig, runBenchmark, sqlite, sqliteCheckpoint, sqliteConfig, sqliteTelemetry, summarizeBenchmark, tesseract, togetherEmbedding, vllmEmbedding, wolbarg$1 as wolbarg, wrapOperationError };
|
|
1931
|
+
export { type BenchmarkReport, type BenchmarkSample, type ChatMessage, type CheckpointInfo, type CheckpointMeta, type CheckpointOptions, type CheckpointProvider, type Chunk, type ChunkingOptions, type ChunkingStrategy, type ClearOptions, type CompressOptions, type CompressResult, CompressionError, type CompressionProvider, type ConcurrencyConfig$1 as ConcurrencyConfig, ConfigurationError, type CreateCheckpointOptions, type DatabaseConfig, DatabaseError, type DatabaseProvider, type DatabaseProviderName, type EmbeddingCacheConfig, type EmbeddingConfig, EmbeddingError, type EmbeddingProvider, type EventDatabase, type ExportResult, FixedChunkingStrategy, type ForgetByFilterOptions, type ForgetByIdOptions, type ForgetOptions, HeadingChunkingStrategy, type HistoryEvent, type HistoryOptions, type HistoryResult, type HybridConfig, type ImportResult, type IngestOptions, type IngestResult, type InitOptions, InitializationError, type KeywordDocument, type KeywordSearchHit, type KeywordSearchProvider, type LatencyBreakdown, LlmCompressionProvider, type LlmConfig, type LlmProvider, MarkdownChunkingStrategy, type MemoryChangeCallback, type MemoryChangeEvent, type MemoryContent, type MemoryDedupeConfig, type MemoryDedupeStrategy, type MemoryFilter, type MemoryMetadata, MemoryNotFoundError, type MemoryProvider, type MemoryRecord, type MetadataFilter as MetaFilter, type MetadataComparison, type MetadataFilter, type MmrConfig, NoopTelemetryProvider, type OCRProvider, type OcrResult, ParagraphChunkingStrategy, type PersistedRecallExplainPayload, type PostgresDatabaseConfig, PostgresStorageProvider, ProviderNotConfiguredError, type RecallExplainResponse, type RecallExplanationHit, type RecallOptions, type RecallResult, type RememberAction, type RememberOptions, type RememberResult, type RerankDocument, type RerankHit, type RerankerProvider, type RetrievalConfig, SentenceChunkingStrategy, SqliteCheckpointProvider, type SqliteDatabaseConfig, SqliteDatabaseProvider, SqliteEventDatabase, SqliteStorageProvider, SqliteTelemetryProvider, type StageSpan, type StatsResult, type StorageConfig, StorageLockedError, type StorageProvider, type StorageProviderName, type SubscribableEvent, type SubscribeFilter, type TelemetryConfig, TelemetryEmitter, type TelemetryEvent, type TelemetryEventInput, type TelemetryLogLevel, type TelemetryOperation, type TelemetryProvider, type TelemetryQuery, type TelemetryQueryResult, type TelemetryStatus, type Unsubscribe, ValidationError, type VisionProvider, type VisionResult, Wolbarg, WolbargError, WolbargLogger, type WolbargOptions, type WolbargOptionsWithLlm, type WolbargOptionsWithoutLlm, bgeReranker, bm25, cohereReranker, createChunkingStrategy, createCompressionProvider, createDatabaseProvider, createEmbeddingProvider, createLlmProvider, createStorageProvider, createTelemetryProvider, wolbarg as createWolbarg, crossEncoder, geminiEmbedding, geminiVision, jinaReranker, lmStudioEmbedding, meta, ollamaEmbedding, ollamaLlm, openRouterEmbedding, openRouterLlm, openaiCompatibleEmbedding, openaiCompatibleLlm, openaiEmbedding, openaiLlm, openaiReranker, openaiVision, postgres, postgresConfig, runBenchmark, sqlite, sqliteCheckpoint, sqliteConfig, sqliteTelemetry, summarizeBenchmark, tesseract, togetherEmbedding, vllmEmbedding, wolbarg$1 as wolbarg, wrapOperationError };
|