@reverbia/sdk 1.0.0-next.20251209143100 → 1.0.0-next.20251212012743
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/dist/react/index.cjs +229 -3
- package/dist/react/index.d.mts +51 -0
- package/dist/react/index.d.ts +51 -0
- package/dist/react/index.mjs +229 -3
- package/package.json +1 -1
package/dist/react/index.cjs
CHANGED
|
@@ -1279,7 +1279,8 @@ function useChat(options) {
|
|
|
1279
1279
|
messages,
|
|
1280
1280
|
model,
|
|
1281
1281
|
onData,
|
|
1282
|
-
runTools = true
|
|
1282
|
+
runTools = true,
|
|
1283
|
+
headers
|
|
1283
1284
|
}) => {
|
|
1284
1285
|
const messagesValidation = validateMessages(messages);
|
|
1285
1286
|
if (!messagesValidation.valid) {
|
|
@@ -1468,7 +1469,8 @@ Please inform the user about this issue and try to help them alternatively.`
|
|
|
1468
1469
|
},
|
|
1469
1470
|
headers: {
|
|
1470
1471
|
"Content-Type": "application/json",
|
|
1471
|
-
Authorization: `Bearer ${token}
|
|
1472
|
+
Authorization: `Bearer ${token}`,
|
|
1473
|
+
...headers
|
|
1472
1474
|
},
|
|
1473
1475
|
signal: abortController.signal
|
|
1474
1476
|
});
|
|
@@ -1856,9 +1858,69 @@ var saveMemory = async (memory) => {
|
|
|
1856
1858
|
var saveMemories = async (memories) => {
|
|
1857
1859
|
await Promise.all(memories.map((memory) => saveMemory(memory)));
|
|
1858
1860
|
};
|
|
1861
|
+
var updateMemoryById = async (id, updates, existingMemory, embedding, embeddingModel) => {
|
|
1862
|
+
const now = Date.now();
|
|
1863
|
+
const updatedMemory = {
|
|
1864
|
+
...updates,
|
|
1865
|
+
updatedAt: now
|
|
1866
|
+
};
|
|
1867
|
+
if ("namespace" in updates || "key" in updates || "value" in updates) {
|
|
1868
|
+
const namespace = updates.namespace ?? existingMemory.namespace;
|
|
1869
|
+
const key = updates.key ?? existingMemory.key;
|
|
1870
|
+
const value = updates.value ?? existingMemory.value;
|
|
1871
|
+
const newUniqueKey = `${namespace}:${key}:${value}`;
|
|
1872
|
+
if (newUniqueKey !== existingMemory.uniqueKey) {
|
|
1873
|
+
const conflicting = await getMemory(namespace, key, value);
|
|
1874
|
+
if (conflicting) {
|
|
1875
|
+
throw new Error(
|
|
1876
|
+
`A memory with uniqueKey "${newUniqueKey}" already exists (id: ${conflicting.id})`
|
|
1877
|
+
);
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
updatedMemory.compositeKey = `${namespace}:${key}`;
|
|
1881
|
+
updatedMemory.uniqueKey = newUniqueKey;
|
|
1882
|
+
}
|
|
1883
|
+
updatedMemory.embedding = embedding;
|
|
1884
|
+
updatedMemory.embeddingModel = embeddingModel;
|
|
1885
|
+
return await memoryDb.transaction("rw", memoryDb.memories, async () => {
|
|
1886
|
+
await memoryDb.memories.update(id, updatedMemory);
|
|
1887
|
+
return await memoryDb.memories.get(id);
|
|
1888
|
+
});
|
|
1889
|
+
};
|
|
1859
1890
|
var getAllMemories = async () => {
|
|
1860
1891
|
return memoryDb.memories.toArray();
|
|
1861
1892
|
};
|
|
1893
|
+
var getMemoryById = async (id) => {
|
|
1894
|
+
return memoryDb.memories.get(id);
|
|
1895
|
+
};
|
|
1896
|
+
var getMemoriesByNamespace = async (namespace) => {
|
|
1897
|
+
return memoryDb.memories.where("namespace").equals(namespace).toArray();
|
|
1898
|
+
};
|
|
1899
|
+
var getMemories = async (namespace, key) => {
|
|
1900
|
+
const compositeKey = `${namespace}:${key}`;
|
|
1901
|
+
return memoryDb.memories.where("compositeKey").equals(compositeKey).toArray();
|
|
1902
|
+
};
|
|
1903
|
+
var getMemory = async (namespace, key, value) => {
|
|
1904
|
+
const uniqueKey = `${namespace}:${key}:${value}`;
|
|
1905
|
+
return memoryDb.memories.where("uniqueKey").equals(uniqueKey).first();
|
|
1906
|
+
};
|
|
1907
|
+
var deleteMemories = async (namespace, key) => {
|
|
1908
|
+
const compositeKey = `${namespace}:${key}`;
|
|
1909
|
+
await memoryDb.memories.where("compositeKey").equals(compositeKey).delete();
|
|
1910
|
+
};
|
|
1911
|
+
var deleteMemory = async (namespace, key, value) => {
|
|
1912
|
+
const uniqueKey = `${namespace}:${key}:${value}`;
|
|
1913
|
+
const existing = await memoryDb.memories.where("uniqueKey").equals(uniqueKey).first();
|
|
1914
|
+
if (existing?.id) {
|
|
1915
|
+
await memoryDb.memories.delete(existing.id);
|
|
1916
|
+
}
|
|
1917
|
+
};
|
|
1918
|
+
var deleteMemoryById = async (id) => {
|
|
1919
|
+
await memoryDb.memories.delete(id);
|
|
1920
|
+
};
|
|
1921
|
+
var clearAllMemories = async () => {
|
|
1922
|
+
await memoryDb.memories.clear();
|
|
1923
|
+
};
|
|
1862
1924
|
var cosineSimilarity = (a, b) => {
|
|
1863
1925
|
if (a.length !== b.length) {
|
|
1864
1926
|
throw new Error("Vectors must have the same length");
|
|
@@ -2325,9 +2387,173 @@ function useMemory(options = {}) {
|
|
|
2325
2387
|
},
|
|
2326
2388
|
[embeddingModel, embeddingProvider, getToken, baseUrl]
|
|
2327
2389
|
);
|
|
2390
|
+
const fetchAllMemories = (0, import_react2.useCallback)(async () => {
|
|
2391
|
+
try {
|
|
2392
|
+
return await getAllMemories();
|
|
2393
|
+
} catch (error) {
|
|
2394
|
+
throw new Error(
|
|
2395
|
+
"Failed to fetch all memories: " + (error instanceof Error ? error.message : String(error))
|
|
2396
|
+
);
|
|
2397
|
+
}
|
|
2398
|
+
}, []);
|
|
2399
|
+
const fetchMemoriesByNamespace = (0, import_react2.useCallback)(
|
|
2400
|
+
async (namespace) => {
|
|
2401
|
+
if (!namespace) {
|
|
2402
|
+
throw new Error("Missing required field: namespace");
|
|
2403
|
+
}
|
|
2404
|
+
try {
|
|
2405
|
+
return await getMemoriesByNamespace(namespace);
|
|
2406
|
+
} catch (error) {
|
|
2407
|
+
throw new Error(
|
|
2408
|
+
`Failed to fetch memories for namespace "${namespace}": ` + (error instanceof Error ? error.message : String(error))
|
|
2409
|
+
);
|
|
2410
|
+
}
|
|
2411
|
+
},
|
|
2412
|
+
[]
|
|
2413
|
+
);
|
|
2414
|
+
const fetchMemoriesByKey = (0, import_react2.useCallback)(
|
|
2415
|
+
async (namespace, key) => {
|
|
2416
|
+
if (!namespace || !key) {
|
|
2417
|
+
throw new Error("Missing required fields: namespace, key");
|
|
2418
|
+
}
|
|
2419
|
+
try {
|
|
2420
|
+
return await getMemories(namespace, key);
|
|
2421
|
+
} catch (error) {
|
|
2422
|
+
throw new Error(
|
|
2423
|
+
`Failed to fetch memories for "${namespace}:${key}": ` + (error instanceof Error ? error.message : String(error))
|
|
2424
|
+
);
|
|
2425
|
+
}
|
|
2426
|
+
},
|
|
2427
|
+
[]
|
|
2428
|
+
);
|
|
2429
|
+
const updateMemory = (0, import_react2.useCallback)(
|
|
2430
|
+
async (id, updates) => {
|
|
2431
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
2432
|
+
throw new Error("id must be a non-negative integer");
|
|
2433
|
+
}
|
|
2434
|
+
try {
|
|
2435
|
+
const embeddingModelToUse = embeddingProvider === "api" ? embeddingModel ?? DEFAULT_API_EMBEDDING_MODEL : embeddingModel && embeddingModel !== DEFAULT_API_EMBEDDING_MODEL ? embeddingModel : DEFAULT_LOCAL_EMBEDDING_MODEL;
|
|
2436
|
+
const embeddingOptions = {
|
|
2437
|
+
model: embeddingModelToUse,
|
|
2438
|
+
provider: embeddingProvider,
|
|
2439
|
+
getToken: getToken || void 0,
|
|
2440
|
+
baseUrl
|
|
2441
|
+
};
|
|
2442
|
+
if (!updates.type || !updates.namespace || !updates.key || !updates.value || !updates.rawEvidence || updates.confidence === void 0 || updates.confidence === null || updates.pii === void 0 || updates.pii === null) {
|
|
2443
|
+
throw new Error(
|
|
2444
|
+
"Missing required fields: type, namespace, key, value, rawEvidence, confidence, pii"
|
|
2445
|
+
);
|
|
2446
|
+
}
|
|
2447
|
+
const existingMemory = await getMemoryById(id);
|
|
2448
|
+
if (!existingMemory) {
|
|
2449
|
+
throw new Error(`Memory with id ${id} not found`);
|
|
2450
|
+
}
|
|
2451
|
+
const embeddingFieldsChanged = updates.value !== void 0 && updates.value !== existingMemory.value || updates.rawEvidence !== void 0 && updates.rawEvidence !== existingMemory.rawEvidence || updates.type !== void 0 && updates.type !== existingMemory.type || updates.namespace !== void 0 && updates.namespace !== existingMemory.namespace || updates.key !== void 0 && updates.key !== existingMemory.key;
|
|
2452
|
+
if (!embeddingFieldsChanged) {
|
|
2453
|
+
return existingMemory;
|
|
2454
|
+
}
|
|
2455
|
+
const memory = {
|
|
2456
|
+
type: updates.type,
|
|
2457
|
+
namespace: updates.namespace,
|
|
2458
|
+
key: updates.key,
|
|
2459
|
+
value: updates.value,
|
|
2460
|
+
rawEvidence: updates.rawEvidence,
|
|
2461
|
+
confidence: updates.confidence,
|
|
2462
|
+
pii: updates.pii
|
|
2463
|
+
};
|
|
2464
|
+
let embedding = existingMemory.embedding ?? [];
|
|
2465
|
+
let embeddingModelToStore = existingMemory.embeddingModel ?? "";
|
|
2466
|
+
if (generateEmbeddings && embeddingModelToUse) {
|
|
2467
|
+
try {
|
|
2468
|
+
embedding = await generateEmbeddingForMemory(
|
|
2469
|
+
memory,
|
|
2470
|
+
embeddingOptions
|
|
2471
|
+
);
|
|
2472
|
+
embeddingModelToStore = embeddingModelToUse;
|
|
2473
|
+
} catch (embeddingError) {
|
|
2474
|
+
console.error(
|
|
2475
|
+
"Failed to generate embedding, keeping existing:",
|
|
2476
|
+
embeddingError
|
|
2477
|
+
);
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
return await updateMemoryById(
|
|
2481
|
+
id,
|
|
2482
|
+
updates,
|
|
2483
|
+
existingMemory,
|
|
2484
|
+
embedding,
|
|
2485
|
+
embeddingModelToStore
|
|
2486
|
+
);
|
|
2487
|
+
} catch (error) {
|
|
2488
|
+
throw new Error(
|
|
2489
|
+
`Failed to update memory ${id}: ` + (error instanceof Error ? error.message : String(error))
|
|
2490
|
+
);
|
|
2491
|
+
}
|
|
2492
|
+
},
|
|
2493
|
+
[embeddingModel, embeddingProvider, generateEmbeddings, getToken, baseUrl]
|
|
2494
|
+
);
|
|
2495
|
+
const removeMemory = (0, import_react2.useCallback)(
|
|
2496
|
+
async (namespace, key, value) => {
|
|
2497
|
+
if (!namespace || !key || !value) {
|
|
2498
|
+
throw new Error("Missing required fields: namespace, key, value");
|
|
2499
|
+
}
|
|
2500
|
+
try {
|
|
2501
|
+
await deleteMemory(namespace, key, value);
|
|
2502
|
+
} catch (error) {
|
|
2503
|
+
throw new Error(
|
|
2504
|
+
`Failed to delete memory "${namespace}:${key}:${value}": ` + (error instanceof Error ? error.message : String(error))
|
|
2505
|
+
);
|
|
2506
|
+
}
|
|
2507
|
+
},
|
|
2508
|
+
[]
|
|
2509
|
+
);
|
|
2510
|
+
const removeMemoryById = (0, import_react2.useCallback)(async (id) => {
|
|
2511
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
2512
|
+
throw new Error("id must be a non-negative integer");
|
|
2513
|
+
}
|
|
2514
|
+
try {
|
|
2515
|
+
await deleteMemoryById(id);
|
|
2516
|
+
} catch (error) {
|
|
2517
|
+
throw new Error(
|
|
2518
|
+
`Failed to delete memory with id ${id}: ` + (error instanceof Error ? error.message : String(error))
|
|
2519
|
+
);
|
|
2520
|
+
}
|
|
2521
|
+
}, []);
|
|
2522
|
+
const removeMemories = (0, import_react2.useCallback)(
|
|
2523
|
+
async (namespace, key) => {
|
|
2524
|
+
if (!namespace || !key) {
|
|
2525
|
+
throw new Error("Missing required fields: namespace, key");
|
|
2526
|
+
}
|
|
2527
|
+
try {
|
|
2528
|
+
await deleteMemories(namespace, key);
|
|
2529
|
+
} catch (error) {
|
|
2530
|
+
throw new Error(
|
|
2531
|
+
`Failed to delete memories for "${namespace}:${key}": ` + (error instanceof Error ? error.message : String(error))
|
|
2532
|
+
);
|
|
2533
|
+
}
|
|
2534
|
+
},
|
|
2535
|
+
[]
|
|
2536
|
+
);
|
|
2537
|
+
const clearMemories = (0, import_react2.useCallback)(async () => {
|
|
2538
|
+
try {
|
|
2539
|
+
await clearAllMemories();
|
|
2540
|
+
} catch (error) {
|
|
2541
|
+
throw new Error(
|
|
2542
|
+
"Failed to clear all memories: " + (error instanceof Error ? error.message : String(error))
|
|
2543
|
+
);
|
|
2544
|
+
}
|
|
2545
|
+
}, []);
|
|
2328
2546
|
return {
|
|
2329
2547
|
extractMemoriesFromMessage,
|
|
2330
|
-
searchMemories
|
|
2548
|
+
searchMemories,
|
|
2549
|
+
fetchAllMemories,
|
|
2550
|
+
fetchMemoriesByNamespace,
|
|
2551
|
+
fetchMemoriesByKey,
|
|
2552
|
+
updateMemory,
|
|
2553
|
+
removeMemory,
|
|
2554
|
+
removeMemoryById,
|
|
2555
|
+
removeMemories,
|
|
2556
|
+
clearMemories
|
|
2331
2557
|
};
|
|
2332
2558
|
}
|
|
2333
2559
|
|
package/dist/react/index.d.mts
CHANGED
|
@@ -456,6 +456,10 @@ type SendMessageArgs = BaseSendMessageArgs & {
|
|
|
456
456
|
* Defaults to true if tools are configured.
|
|
457
457
|
*/
|
|
458
458
|
runTools?: boolean;
|
|
459
|
+
/**
|
|
460
|
+
* Optional custom headers to include with the request.
|
|
461
|
+
*/
|
|
462
|
+
headers?: Record<string, string>;
|
|
459
463
|
};
|
|
460
464
|
type SendMessageResult = {
|
|
461
465
|
data: LlmapiChatCompletionResponse;
|
|
@@ -694,6 +698,53 @@ type UseMemoryResult = {
|
|
|
694
698
|
searchMemories: (query: string, limit?: number, minSimilarity?: number) => Promise<Array<StoredMemoryItem & {
|
|
695
699
|
similarity: number;
|
|
696
700
|
}>>;
|
|
701
|
+
/**
|
|
702
|
+
* Get all memories stored in IndexedDB
|
|
703
|
+
* @returns Array of all stored memories
|
|
704
|
+
*/
|
|
705
|
+
fetchAllMemories: () => Promise<StoredMemoryItem[]>;
|
|
706
|
+
/**
|
|
707
|
+
* Get memories filtered by namespace
|
|
708
|
+
* @param namespace The namespace to filter by
|
|
709
|
+
* @returns Array of memories in the specified namespace
|
|
710
|
+
*/
|
|
711
|
+
fetchMemoriesByNamespace: (namespace: string) => Promise<StoredMemoryItem[]>;
|
|
712
|
+
/**
|
|
713
|
+
* Get memories by namespace and key
|
|
714
|
+
* @param namespace The namespace
|
|
715
|
+
* @param key The key within the namespace
|
|
716
|
+
* @returns Array of memories matching the namespace and key
|
|
717
|
+
*/
|
|
718
|
+
fetchMemoriesByKey: (namespace: string, key: string) => Promise<StoredMemoryItem[]>;
|
|
719
|
+
/**
|
|
720
|
+
* Update a memory by its ID
|
|
721
|
+
* @param id The memory ID
|
|
722
|
+
* @param updates Partial memory fields to update
|
|
723
|
+
* @returns The updated memory or undefined if not found
|
|
724
|
+
*/
|
|
725
|
+
updateMemory: (id: number, updates: Partial<StoredMemoryItem & MemoryItem>) => Promise<StoredMemoryItem | undefined>;
|
|
726
|
+
/**
|
|
727
|
+
* Delete a specific memory by namespace, key, and value
|
|
728
|
+
* @param namespace The namespace
|
|
729
|
+
* @param key The key
|
|
730
|
+
* @param value The value
|
|
731
|
+
*/
|
|
732
|
+
removeMemory: (namespace: string, key: string, value: string) => Promise<void>;
|
|
733
|
+
/**
|
|
734
|
+
* Delete a memory by its ID
|
|
735
|
+
* @param id The memory ID
|
|
736
|
+
*/
|
|
737
|
+
removeMemoryById: (id: number) => Promise<void>;
|
|
738
|
+
/**
|
|
739
|
+
* Delete all memories by namespace and key
|
|
740
|
+
* @param namespace The namespace
|
|
741
|
+
* @param key The key
|
|
742
|
+
*/
|
|
743
|
+
removeMemories: (namespace: string, key: string) => Promise<void>;
|
|
744
|
+
/**
|
|
745
|
+
* Clear all memories from IndexedDB
|
|
746
|
+
*/
|
|
747
|
+
clearMemories: () => Promise<void>;
|
|
697
748
|
};
|
|
698
749
|
/**
|
|
699
750
|
* Standalone hook for extracting memories from user messages.
|
package/dist/react/index.d.ts
CHANGED
|
@@ -456,6 +456,10 @@ type SendMessageArgs = BaseSendMessageArgs & {
|
|
|
456
456
|
* Defaults to true if tools are configured.
|
|
457
457
|
*/
|
|
458
458
|
runTools?: boolean;
|
|
459
|
+
/**
|
|
460
|
+
* Optional custom headers to include with the request.
|
|
461
|
+
*/
|
|
462
|
+
headers?: Record<string, string>;
|
|
459
463
|
};
|
|
460
464
|
type SendMessageResult = {
|
|
461
465
|
data: LlmapiChatCompletionResponse;
|
|
@@ -694,6 +698,53 @@ type UseMemoryResult = {
|
|
|
694
698
|
searchMemories: (query: string, limit?: number, minSimilarity?: number) => Promise<Array<StoredMemoryItem & {
|
|
695
699
|
similarity: number;
|
|
696
700
|
}>>;
|
|
701
|
+
/**
|
|
702
|
+
* Get all memories stored in IndexedDB
|
|
703
|
+
* @returns Array of all stored memories
|
|
704
|
+
*/
|
|
705
|
+
fetchAllMemories: () => Promise<StoredMemoryItem[]>;
|
|
706
|
+
/**
|
|
707
|
+
* Get memories filtered by namespace
|
|
708
|
+
* @param namespace The namespace to filter by
|
|
709
|
+
* @returns Array of memories in the specified namespace
|
|
710
|
+
*/
|
|
711
|
+
fetchMemoriesByNamespace: (namespace: string) => Promise<StoredMemoryItem[]>;
|
|
712
|
+
/**
|
|
713
|
+
* Get memories by namespace and key
|
|
714
|
+
* @param namespace The namespace
|
|
715
|
+
* @param key The key within the namespace
|
|
716
|
+
* @returns Array of memories matching the namespace and key
|
|
717
|
+
*/
|
|
718
|
+
fetchMemoriesByKey: (namespace: string, key: string) => Promise<StoredMemoryItem[]>;
|
|
719
|
+
/**
|
|
720
|
+
* Update a memory by its ID
|
|
721
|
+
* @param id The memory ID
|
|
722
|
+
* @param updates Partial memory fields to update
|
|
723
|
+
* @returns The updated memory or undefined if not found
|
|
724
|
+
*/
|
|
725
|
+
updateMemory: (id: number, updates: Partial<StoredMemoryItem & MemoryItem>) => Promise<StoredMemoryItem | undefined>;
|
|
726
|
+
/**
|
|
727
|
+
* Delete a specific memory by namespace, key, and value
|
|
728
|
+
* @param namespace The namespace
|
|
729
|
+
* @param key The key
|
|
730
|
+
* @param value The value
|
|
731
|
+
*/
|
|
732
|
+
removeMemory: (namespace: string, key: string, value: string) => Promise<void>;
|
|
733
|
+
/**
|
|
734
|
+
* Delete a memory by its ID
|
|
735
|
+
* @param id The memory ID
|
|
736
|
+
*/
|
|
737
|
+
removeMemoryById: (id: number) => Promise<void>;
|
|
738
|
+
/**
|
|
739
|
+
* Delete all memories by namespace and key
|
|
740
|
+
* @param namespace The namespace
|
|
741
|
+
* @param key The key
|
|
742
|
+
*/
|
|
743
|
+
removeMemories: (namespace: string, key: string) => Promise<void>;
|
|
744
|
+
/**
|
|
745
|
+
* Clear all memories from IndexedDB
|
|
746
|
+
*/
|
|
747
|
+
clearMemories: () => Promise<void>;
|
|
697
748
|
};
|
|
698
749
|
/**
|
|
699
750
|
* Standalone hook for extracting memories from user messages.
|
package/dist/react/index.mjs
CHANGED
|
@@ -1225,7 +1225,8 @@ function useChat(options) {
|
|
|
1225
1225
|
messages,
|
|
1226
1226
|
model,
|
|
1227
1227
|
onData,
|
|
1228
|
-
runTools = true
|
|
1228
|
+
runTools = true,
|
|
1229
|
+
headers
|
|
1229
1230
|
}) => {
|
|
1230
1231
|
const messagesValidation = validateMessages(messages);
|
|
1231
1232
|
if (!messagesValidation.valid) {
|
|
@@ -1414,7 +1415,8 @@ Please inform the user about this issue and try to help them alternatively.`
|
|
|
1414
1415
|
},
|
|
1415
1416
|
headers: {
|
|
1416
1417
|
"Content-Type": "application/json",
|
|
1417
|
-
Authorization: `Bearer ${token}
|
|
1418
|
+
Authorization: `Bearer ${token}`,
|
|
1419
|
+
...headers
|
|
1418
1420
|
},
|
|
1419
1421
|
signal: abortController.signal
|
|
1420
1422
|
});
|
|
@@ -1802,9 +1804,69 @@ var saveMemory = async (memory) => {
|
|
|
1802
1804
|
var saveMemories = async (memories) => {
|
|
1803
1805
|
await Promise.all(memories.map((memory) => saveMemory(memory)));
|
|
1804
1806
|
};
|
|
1807
|
+
var updateMemoryById = async (id, updates, existingMemory, embedding, embeddingModel) => {
|
|
1808
|
+
const now = Date.now();
|
|
1809
|
+
const updatedMemory = {
|
|
1810
|
+
...updates,
|
|
1811
|
+
updatedAt: now
|
|
1812
|
+
};
|
|
1813
|
+
if ("namespace" in updates || "key" in updates || "value" in updates) {
|
|
1814
|
+
const namespace = updates.namespace ?? existingMemory.namespace;
|
|
1815
|
+
const key = updates.key ?? existingMemory.key;
|
|
1816
|
+
const value = updates.value ?? existingMemory.value;
|
|
1817
|
+
const newUniqueKey = `${namespace}:${key}:${value}`;
|
|
1818
|
+
if (newUniqueKey !== existingMemory.uniqueKey) {
|
|
1819
|
+
const conflicting = await getMemory(namespace, key, value);
|
|
1820
|
+
if (conflicting) {
|
|
1821
|
+
throw new Error(
|
|
1822
|
+
`A memory with uniqueKey "${newUniqueKey}" already exists (id: ${conflicting.id})`
|
|
1823
|
+
);
|
|
1824
|
+
}
|
|
1825
|
+
}
|
|
1826
|
+
updatedMemory.compositeKey = `${namespace}:${key}`;
|
|
1827
|
+
updatedMemory.uniqueKey = newUniqueKey;
|
|
1828
|
+
}
|
|
1829
|
+
updatedMemory.embedding = embedding;
|
|
1830
|
+
updatedMemory.embeddingModel = embeddingModel;
|
|
1831
|
+
return await memoryDb.transaction("rw", memoryDb.memories, async () => {
|
|
1832
|
+
await memoryDb.memories.update(id, updatedMemory);
|
|
1833
|
+
return await memoryDb.memories.get(id);
|
|
1834
|
+
});
|
|
1835
|
+
};
|
|
1805
1836
|
var getAllMemories = async () => {
|
|
1806
1837
|
return memoryDb.memories.toArray();
|
|
1807
1838
|
};
|
|
1839
|
+
var getMemoryById = async (id) => {
|
|
1840
|
+
return memoryDb.memories.get(id);
|
|
1841
|
+
};
|
|
1842
|
+
var getMemoriesByNamespace = async (namespace) => {
|
|
1843
|
+
return memoryDb.memories.where("namespace").equals(namespace).toArray();
|
|
1844
|
+
};
|
|
1845
|
+
var getMemories = async (namespace, key) => {
|
|
1846
|
+
const compositeKey = `${namespace}:${key}`;
|
|
1847
|
+
return memoryDb.memories.where("compositeKey").equals(compositeKey).toArray();
|
|
1848
|
+
};
|
|
1849
|
+
var getMemory = async (namespace, key, value) => {
|
|
1850
|
+
const uniqueKey = `${namespace}:${key}:${value}`;
|
|
1851
|
+
return memoryDb.memories.where("uniqueKey").equals(uniqueKey).first();
|
|
1852
|
+
};
|
|
1853
|
+
var deleteMemories = async (namespace, key) => {
|
|
1854
|
+
const compositeKey = `${namespace}:${key}`;
|
|
1855
|
+
await memoryDb.memories.where("compositeKey").equals(compositeKey).delete();
|
|
1856
|
+
};
|
|
1857
|
+
var deleteMemory = async (namespace, key, value) => {
|
|
1858
|
+
const uniqueKey = `${namespace}:${key}:${value}`;
|
|
1859
|
+
const existing = await memoryDb.memories.where("uniqueKey").equals(uniqueKey).first();
|
|
1860
|
+
if (existing?.id) {
|
|
1861
|
+
await memoryDb.memories.delete(existing.id);
|
|
1862
|
+
}
|
|
1863
|
+
};
|
|
1864
|
+
var deleteMemoryById = async (id) => {
|
|
1865
|
+
await memoryDb.memories.delete(id);
|
|
1866
|
+
};
|
|
1867
|
+
var clearAllMemories = async () => {
|
|
1868
|
+
await memoryDb.memories.clear();
|
|
1869
|
+
};
|
|
1808
1870
|
var cosineSimilarity = (a, b) => {
|
|
1809
1871
|
if (a.length !== b.length) {
|
|
1810
1872
|
throw new Error("Vectors must have the same length");
|
|
@@ -2271,9 +2333,173 @@ function useMemory(options = {}) {
|
|
|
2271
2333
|
},
|
|
2272
2334
|
[embeddingModel, embeddingProvider, getToken, baseUrl]
|
|
2273
2335
|
);
|
|
2336
|
+
const fetchAllMemories = useCallback2(async () => {
|
|
2337
|
+
try {
|
|
2338
|
+
return await getAllMemories();
|
|
2339
|
+
} catch (error) {
|
|
2340
|
+
throw new Error(
|
|
2341
|
+
"Failed to fetch all memories: " + (error instanceof Error ? error.message : String(error))
|
|
2342
|
+
);
|
|
2343
|
+
}
|
|
2344
|
+
}, []);
|
|
2345
|
+
const fetchMemoriesByNamespace = useCallback2(
|
|
2346
|
+
async (namespace) => {
|
|
2347
|
+
if (!namespace) {
|
|
2348
|
+
throw new Error("Missing required field: namespace");
|
|
2349
|
+
}
|
|
2350
|
+
try {
|
|
2351
|
+
return await getMemoriesByNamespace(namespace);
|
|
2352
|
+
} catch (error) {
|
|
2353
|
+
throw new Error(
|
|
2354
|
+
`Failed to fetch memories for namespace "${namespace}": ` + (error instanceof Error ? error.message : String(error))
|
|
2355
|
+
);
|
|
2356
|
+
}
|
|
2357
|
+
},
|
|
2358
|
+
[]
|
|
2359
|
+
);
|
|
2360
|
+
const fetchMemoriesByKey = useCallback2(
|
|
2361
|
+
async (namespace, key) => {
|
|
2362
|
+
if (!namespace || !key) {
|
|
2363
|
+
throw new Error("Missing required fields: namespace, key");
|
|
2364
|
+
}
|
|
2365
|
+
try {
|
|
2366
|
+
return await getMemories(namespace, key);
|
|
2367
|
+
} catch (error) {
|
|
2368
|
+
throw new Error(
|
|
2369
|
+
`Failed to fetch memories for "${namespace}:${key}": ` + (error instanceof Error ? error.message : String(error))
|
|
2370
|
+
);
|
|
2371
|
+
}
|
|
2372
|
+
},
|
|
2373
|
+
[]
|
|
2374
|
+
);
|
|
2375
|
+
const updateMemory = useCallback2(
|
|
2376
|
+
async (id, updates) => {
|
|
2377
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
2378
|
+
throw new Error("id must be a non-negative integer");
|
|
2379
|
+
}
|
|
2380
|
+
try {
|
|
2381
|
+
const embeddingModelToUse = embeddingProvider === "api" ? embeddingModel ?? DEFAULT_API_EMBEDDING_MODEL : embeddingModel && embeddingModel !== DEFAULT_API_EMBEDDING_MODEL ? embeddingModel : DEFAULT_LOCAL_EMBEDDING_MODEL;
|
|
2382
|
+
const embeddingOptions = {
|
|
2383
|
+
model: embeddingModelToUse,
|
|
2384
|
+
provider: embeddingProvider,
|
|
2385
|
+
getToken: getToken || void 0,
|
|
2386
|
+
baseUrl
|
|
2387
|
+
};
|
|
2388
|
+
if (!updates.type || !updates.namespace || !updates.key || !updates.value || !updates.rawEvidence || updates.confidence === void 0 || updates.confidence === null || updates.pii === void 0 || updates.pii === null) {
|
|
2389
|
+
throw new Error(
|
|
2390
|
+
"Missing required fields: type, namespace, key, value, rawEvidence, confidence, pii"
|
|
2391
|
+
);
|
|
2392
|
+
}
|
|
2393
|
+
const existingMemory = await getMemoryById(id);
|
|
2394
|
+
if (!existingMemory) {
|
|
2395
|
+
throw new Error(`Memory with id ${id} not found`);
|
|
2396
|
+
}
|
|
2397
|
+
const embeddingFieldsChanged = updates.value !== void 0 && updates.value !== existingMemory.value || updates.rawEvidence !== void 0 && updates.rawEvidence !== existingMemory.rawEvidence || updates.type !== void 0 && updates.type !== existingMemory.type || updates.namespace !== void 0 && updates.namespace !== existingMemory.namespace || updates.key !== void 0 && updates.key !== existingMemory.key;
|
|
2398
|
+
if (!embeddingFieldsChanged) {
|
|
2399
|
+
return existingMemory;
|
|
2400
|
+
}
|
|
2401
|
+
const memory = {
|
|
2402
|
+
type: updates.type,
|
|
2403
|
+
namespace: updates.namespace,
|
|
2404
|
+
key: updates.key,
|
|
2405
|
+
value: updates.value,
|
|
2406
|
+
rawEvidence: updates.rawEvidence,
|
|
2407
|
+
confidence: updates.confidence,
|
|
2408
|
+
pii: updates.pii
|
|
2409
|
+
};
|
|
2410
|
+
let embedding = existingMemory.embedding ?? [];
|
|
2411
|
+
let embeddingModelToStore = existingMemory.embeddingModel ?? "";
|
|
2412
|
+
if (generateEmbeddings && embeddingModelToUse) {
|
|
2413
|
+
try {
|
|
2414
|
+
embedding = await generateEmbeddingForMemory(
|
|
2415
|
+
memory,
|
|
2416
|
+
embeddingOptions
|
|
2417
|
+
);
|
|
2418
|
+
embeddingModelToStore = embeddingModelToUse;
|
|
2419
|
+
} catch (embeddingError) {
|
|
2420
|
+
console.error(
|
|
2421
|
+
"Failed to generate embedding, keeping existing:",
|
|
2422
|
+
embeddingError
|
|
2423
|
+
);
|
|
2424
|
+
}
|
|
2425
|
+
}
|
|
2426
|
+
return await updateMemoryById(
|
|
2427
|
+
id,
|
|
2428
|
+
updates,
|
|
2429
|
+
existingMemory,
|
|
2430
|
+
embedding,
|
|
2431
|
+
embeddingModelToStore
|
|
2432
|
+
);
|
|
2433
|
+
} catch (error) {
|
|
2434
|
+
throw new Error(
|
|
2435
|
+
`Failed to update memory ${id}: ` + (error instanceof Error ? error.message : String(error))
|
|
2436
|
+
);
|
|
2437
|
+
}
|
|
2438
|
+
},
|
|
2439
|
+
[embeddingModel, embeddingProvider, generateEmbeddings, getToken, baseUrl]
|
|
2440
|
+
);
|
|
2441
|
+
const removeMemory = useCallback2(
|
|
2442
|
+
async (namespace, key, value) => {
|
|
2443
|
+
if (!namespace || !key || !value) {
|
|
2444
|
+
throw new Error("Missing required fields: namespace, key, value");
|
|
2445
|
+
}
|
|
2446
|
+
try {
|
|
2447
|
+
await deleteMemory(namespace, key, value);
|
|
2448
|
+
} catch (error) {
|
|
2449
|
+
throw new Error(
|
|
2450
|
+
`Failed to delete memory "${namespace}:${key}:${value}": ` + (error instanceof Error ? error.message : String(error))
|
|
2451
|
+
);
|
|
2452
|
+
}
|
|
2453
|
+
},
|
|
2454
|
+
[]
|
|
2455
|
+
);
|
|
2456
|
+
const removeMemoryById = useCallback2(async (id) => {
|
|
2457
|
+
if (!Number.isInteger(id) || id <= 0) {
|
|
2458
|
+
throw new Error("id must be a non-negative integer");
|
|
2459
|
+
}
|
|
2460
|
+
try {
|
|
2461
|
+
await deleteMemoryById(id);
|
|
2462
|
+
} catch (error) {
|
|
2463
|
+
throw new Error(
|
|
2464
|
+
`Failed to delete memory with id ${id}: ` + (error instanceof Error ? error.message : String(error))
|
|
2465
|
+
);
|
|
2466
|
+
}
|
|
2467
|
+
}, []);
|
|
2468
|
+
const removeMemories = useCallback2(
|
|
2469
|
+
async (namespace, key) => {
|
|
2470
|
+
if (!namespace || !key) {
|
|
2471
|
+
throw new Error("Missing required fields: namespace, key");
|
|
2472
|
+
}
|
|
2473
|
+
try {
|
|
2474
|
+
await deleteMemories(namespace, key);
|
|
2475
|
+
} catch (error) {
|
|
2476
|
+
throw new Error(
|
|
2477
|
+
`Failed to delete memories for "${namespace}:${key}": ` + (error instanceof Error ? error.message : String(error))
|
|
2478
|
+
);
|
|
2479
|
+
}
|
|
2480
|
+
},
|
|
2481
|
+
[]
|
|
2482
|
+
);
|
|
2483
|
+
const clearMemories = useCallback2(async () => {
|
|
2484
|
+
try {
|
|
2485
|
+
await clearAllMemories();
|
|
2486
|
+
} catch (error) {
|
|
2487
|
+
throw new Error(
|
|
2488
|
+
"Failed to clear all memories: " + (error instanceof Error ? error.message : String(error))
|
|
2489
|
+
);
|
|
2490
|
+
}
|
|
2491
|
+
}, []);
|
|
2274
2492
|
return {
|
|
2275
2493
|
extractMemoriesFromMessage,
|
|
2276
|
-
searchMemories
|
|
2494
|
+
searchMemories,
|
|
2495
|
+
fetchAllMemories,
|
|
2496
|
+
fetchMemoriesByNamespace,
|
|
2497
|
+
fetchMemoriesByKey,
|
|
2498
|
+
updateMemory,
|
|
2499
|
+
removeMemory,
|
|
2500
|
+
removeMemoryById,
|
|
2501
|
+
removeMemories,
|
|
2502
|
+
clearMemories
|
|
2277
2503
|
};
|
|
2278
2504
|
}
|
|
2279
2505
|
|