@anuma/sdk 6.37.0 → 6.39.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/dist/expo/index.cjs +416 -182
- package/dist/expo/index.d.mts +5675 -5636
- package/dist/expo/index.d.ts +5675 -5636
- package/dist/expo/index.mjs +416 -182
- package/dist/index.d.mts +62 -62
- package/dist/index.d.ts +62 -62
- package/dist/react/index.cjs +416 -177
- package/dist/react/index.d.mts +7043 -7004
- package/dist/react/index.d.ts +7043 -7004
- package/dist/react/index.mjs +416 -177
- package/dist/server/index.cjs +135 -47
- package/dist/server/index.d.mts +6118 -6057
- package/dist/server/index.d.ts +6118 -6057
- package/dist/server/index.mjs +135 -47
- package/package.json +1 -1
package/dist/react/index.mjs
CHANGED
|
@@ -4163,7 +4163,7 @@ function extractUserMessageFromMessages(messages) {
|
|
|
4163
4163
|
}
|
|
4164
4164
|
|
|
4165
4165
|
// src/lib/db/chat/operations.ts
|
|
4166
|
-
function messageToStoredRaw(message,
|
|
4166
|
+
function messageToStoredRaw(message, projection2) {
|
|
4167
4167
|
const convId = String(message._getRaw("conversation_id"));
|
|
4168
4168
|
const parseJsonField = (rawValue) => {
|
|
4169
4169
|
if (!rawValue) return void 0;
|
|
@@ -4177,7 +4177,7 @@ function messageToStoredRaw(message, projection) {
|
|
|
4177
4177
|
}
|
|
4178
4178
|
return rawValue;
|
|
4179
4179
|
};
|
|
4180
|
-
const skipEmbeddings =
|
|
4180
|
+
const skipEmbeddings = projection2?.skipEmbeddings === true;
|
|
4181
4181
|
const sourcesRaw = message._getRaw("sources");
|
|
4182
4182
|
const vectorRaw = skipEmbeddings ? null : message._getRaw("vector");
|
|
4183
4183
|
const chunksRaw = skipEmbeddings ? null : message._getRaw("chunks");
|
|
@@ -4211,8 +4211,8 @@ function messageToStoredRaw(message, projection) {
|
|
|
4211
4211
|
origin: message.origin
|
|
4212
4212
|
};
|
|
4213
4213
|
}
|
|
4214
|
-
async function messageToStored(message, walletAddress, signMessage, embeddedWalletSigner,
|
|
4215
|
-
const baseMessage = messageToStoredRaw(message,
|
|
4214
|
+
async function messageToStored(message, walletAddress, signMessage, embeddedWalletSigner, projection2) {
|
|
4215
|
+
const baseMessage = messageToStoredRaw(message, projection2);
|
|
4216
4216
|
if (walletAddress) {
|
|
4217
4217
|
return await decryptMessageFields(
|
|
4218
4218
|
baseMessage,
|
|
@@ -4226,7 +4226,7 @@ async function messageToStored(message, walletAddress, signMessage, embeddedWall
|
|
|
4226
4226
|
function rawDate(value) {
|
|
4227
4227
|
return new Date(typeof value === "number" ? value : 0);
|
|
4228
4228
|
}
|
|
4229
|
-
function messageRawToStoredRaw(raw,
|
|
4229
|
+
function messageRawToStoredRaw(raw, projection2) {
|
|
4230
4230
|
const parseJsonField = (rawValue) => {
|
|
4231
4231
|
if (!rawValue) return void 0;
|
|
4232
4232
|
if (typeof rawValue === "string") {
|
|
@@ -4239,7 +4239,7 @@ function messageRawToStoredRaw(raw, projection) {
|
|
|
4239
4239
|
}
|
|
4240
4240
|
return rawValue;
|
|
4241
4241
|
};
|
|
4242
|
-
const skipEmbeddings =
|
|
4242
|
+
const skipEmbeddings = projection2?.skipEmbeddings === true;
|
|
4243
4243
|
const vectorRaw = skipEmbeddings ? null : raw.vector;
|
|
4244
4244
|
const chunksRaw = skipEmbeddings ? null : raw.chunks;
|
|
4245
4245
|
return {
|
|
@@ -4270,8 +4270,8 @@ function messageRawToStoredRaw(raw, projection) {
|
|
|
4270
4270
|
origin: raw.origin
|
|
4271
4271
|
};
|
|
4272
4272
|
}
|
|
4273
|
-
async function messageRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner,
|
|
4274
|
-
const baseMessage = messageRawToStoredRaw(raw,
|
|
4273
|
+
async function messageRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner, projection2) {
|
|
4274
|
+
const baseMessage = messageRawToStoredRaw(raw, projection2);
|
|
4275
4275
|
if (walletAddress) {
|
|
4276
4276
|
return await decryptMessageFields(
|
|
4277
4277
|
baseMessage,
|
|
@@ -14056,22 +14056,35 @@ async function vaultMemoryRawToStored(raw, walletAddress, signMessage, embeddedW
|
|
|
14056
14056
|
return stored2;
|
|
14057
14057
|
}
|
|
14058
14058
|
async function getAllVaultMemoriesOp(ctx, options) {
|
|
14059
|
-
const
|
|
14059
|
+
const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
|
|
14060
|
+
return mapInBatches(
|
|
14061
|
+
results,
|
|
14062
|
+
(raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
|
|
14063
|
+
);
|
|
14064
|
+
}
|
|
14065
|
+
function vaultListConditions(ctx, options) {
|
|
14066
|
+
return [
|
|
14060
14067
|
...baseVaultConditions(ctx, options),
|
|
14061
14068
|
...options?.scopes?.length ? [Q8.where("scope", Q8.oneOf(options.scopes))] : [],
|
|
14062
14069
|
...visibilityConditions(options?.visibility),
|
|
14063
14070
|
...levelConditions(options?.levels),
|
|
14064
14071
|
...options?.kinds?.length ? [Q8.where("kind", Q8.oneOf(options.kinds))] : [],
|
|
14065
14072
|
...options?.folderId !== void 0 ? [Q8.where("folder_id", options.folderId)] : [],
|
|
14066
|
-
...options?.factTypes?.length ? [Q8.where("fact_type", Q8.oneOf(options.factTypes))] : []
|
|
14073
|
+
...options?.factTypes?.length ? [Q8.where("fact_type", Q8.oneOf(options.factTypes))] : []
|
|
14074
|
+
];
|
|
14075
|
+
}
|
|
14076
|
+
function vaultListOrder(options) {
|
|
14077
|
+
return [
|
|
14067
14078
|
Q8.sortBy(options?.since ? "updated_at" : "created_at", Q8.desc),
|
|
14068
14079
|
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [Q8.take(options.limit)] : []
|
|
14069
14080
|
];
|
|
14070
|
-
|
|
14071
|
-
|
|
14072
|
-
|
|
14073
|
-
|
|
14074
|
-
|
|
14081
|
+
}
|
|
14082
|
+
async function getVaultMemoryProjectionsOp(ctx, options) {
|
|
14083
|
+
const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
|
|
14084
|
+
return results.map((raw) => ({
|
|
14085
|
+
...vaultMemoryRawToRankable(raw),
|
|
14086
|
+
topicsUserManaged: raw.topics_user_managed === true || raw.topics_user_managed === 1
|
|
14087
|
+
}));
|
|
14075
14088
|
}
|
|
14076
14089
|
function vaultMemoryRawToRankable(raw) {
|
|
14077
14090
|
return {
|
|
@@ -20171,7 +20184,6 @@ async function recall(query, ctx, options = {}) {
|
|
|
20171
20184
|
}
|
|
20172
20185
|
const needsChunkEmbedding = types.includes("chunk") && ctx.storageCtx;
|
|
20173
20186
|
const wantsTemporal = types.includes("fact") && (ctx.factSource || ctx.vaultCtx);
|
|
20174
|
-
const graphRefiner = flags.traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
|
|
20175
20187
|
const queryEmbedTotalTimeoutMs = options.queryEmbedTotalTimeoutMs ?? DEFAULT_QUERY_EMBED_TOTAL_TIMEOUT_MS;
|
|
20176
20188
|
const prepStart = nowMs2();
|
|
20177
20189
|
let sharedEmbedMs = 0;
|
|
@@ -20200,13 +20212,12 @@ async function recall(query, ctx, options = {}) {
|
|
|
20200
20212
|
safeLane(
|
|
20201
20213
|
"graph",
|
|
20202
20214
|
() => graphLaneFailed = true,
|
|
20203
|
-
() => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
|
|
20204
|
-
|
|
20205
|
-
|
|
20206
|
-
|
|
20207
|
-
|
|
20208
|
-
|
|
20209
|
-
})
|
|
20215
|
+
() => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
|
|
20216
|
+
query,
|
|
20217
|
+
ctx,
|
|
20218
|
+
flags.traverse,
|
|
20219
|
+
graphTraversalOptions(options, flags.traverse)
|
|
20220
|
+
)
|
|
20210
20221
|
),
|
|
20211
20222
|
wantsTemporal ? safeLane(
|
|
20212
20223
|
"temporal",
|
|
@@ -20425,6 +20436,16 @@ function toFactMemory(r, now) {
|
|
|
20425
20436
|
updatedAt: r.updatedAt ?? /* @__PURE__ */ new Date(0)
|
|
20426
20437
|
};
|
|
20427
20438
|
}
|
|
20439
|
+
function graphTraversalOptions(options, traverse) {
|
|
20440
|
+
const refineNeighbors = traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
|
|
20441
|
+
return {
|
|
20442
|
+
...options.maxHops !== void 0 && { maxHops: options.maxHops },
|
|
20443
|
+
...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
|
|
20444
|
+
...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
|
|
20445
|
+
...options.rrfK !== void 0 && { rrfK: options.rrfK },
|
|
20446
|
+
...refineNeighbors && { refineNeighbors }
|
|
20447
|
+
};
|
|
20448
|
+
}
|
|
20428
20449
|
async function buildGraphLaneRanking(query, ctx, traverse = false, traversalOptions = {}) {
|
|
20429
20450
|
const entityCtx = ctx.entityCtx ?? ctx.vaultCtx?.entityCtx;
|
|
20430
20451
|
if (!entityCtx) return [];
|
|
@@ -20590,24 +20611,33 @@ function isDumpQuery(query) {
|
|
|
20590
20611
|
return false;
|
|
20591
20612
|
}
|
|
20592
20613
|
async function listRecentFacts(ctx, limit, toolOptions) {
|
|
20614
|
+
const store = toolOptions?.memoryStore;
|
|
20593
20615
|
const vaultCtx = ctx.vaultCtx;
|
|
20594
|
-
if (!vaultCtx) return [];
|
|
20595
|
-
const
|
|
20616
|
+
if (!vaultCtx && !store) return [];
|
|
20617
|
+
const read = (ids) => store ? store.list({ memoryIds: ids }) : getVaultMemoriesByIdsOp(vaultCtx, ids);
|
|
20618
|
+
const filters = {
|
|
20596
20619
|
...toolOptions?.scopes && { scopes: toolOptions.scopes },
|
|
20597
|
-
...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
|
|
20620
|
+
...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
|
|
20621
|
+
};
|
|
20622
|
+
const project = (size) => store ? store.listProjections({ ...filters, limit: size }) : getVaultRankingProjectionsOp(vaultCtx, {
|
|
20623
|
+
...filters,
|
|
20598
20624
|
...toolOptions?.folderId !== void 0 && { folderId: toolOptions.folderId }
|
|
20599
20625
|
});
|
|
20600
20626
|
const readable = [];
|
|
20601
|
-
|
|
20602
|
-
|
|
20603
|
-
const
|
|
20604
|
-
|
|
20605
|
-
|
|
20606
|
-
|
|
20607
|
-
const
|
|
20608
|
-
|
|
20609
|
-
|
|
20627
|
+
let start = 0;
|
|
20628
|
+
for (let size = limit; readable.length < limit; size *= 2) {
|
|
20629
|
+
const ordered = await project(size);
|
|
20630
|
+
for (let i = start; i < ordered.length && readable.length < limit; i += limit) {
|
|
20631
|
+
const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
|
|
20632
|
+
const byId = new Map((await read(batch)).map((m) => [m.uniqueId, m]));
|
|
20633
|
+
for (const id of batch) {
|
|
20634
|
+
const m = byId.get(id);
|
|
20635
|
+
if (m && !isEncrypted(m.content)) readable.push(m);
|
|
20636
|
+
if (readable.length >= limit) break;
|
|
20637
|
+
}
|
|
20610
20638
|
}
|
|
20639
|
+
if (!store || ordered.length < size) break;
|
|
20640
|
+
start = ordered.length;
|
|
20611
20641
|
}
|
|
20612
20642
|
return readable.map((m) => ({
|
|
20613
20643
|
id: m.uniqueId,
|
|
@@ -20624,6 +20654,8 @@ async function listRecentFacts(ctx, limit, toolOptions) {
|
|
|
20624
20654
|
}));
|
|
20625
20655
|
}
|
|
20626
20656
|
function createRecallTool(ctx, toolOptions, callbacks) {
|
|
20657
|
+
if (toolOptions?.memoryStore && toolOptions.folderId !== void 0)
|
|
20658
|
+
throw new Error("Folders are not supported with a memoryStore");
|
|
20627
20659
|
const defaultTypes = toolOptions?.types ?? ["fact", "chunk"];
|
|
20628
20660
|
const defaultLimit = toolOptions?.limit ?? DEFAULT_LIMIT2;
|
|
20629
20661
|
const defaultBudget = toolOptions?.budget ?? DEFAULT_BUDGET2;
|
|
@@ -20695,7 +20727,7 @@ function createRecallTool(ctx, toolOptions, callbacks) {
|
|
|
20695
20727
|
let surfaced = 0;
|
|
20696
20728
|
try {
|
|
20697
20729
|
let recallDegraded = [];
|
|
20698
|
-
const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && ctx.vaultCtx !== void 0;
|
|
20730
|
+
const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && (ctx.vaultCtx !== void 0 || toolOptions?.memoryStore !== void 0);
|
|
20699
20731
|
let result;
|
|
20700
20732
|
if (wantsRecent) {
|
|
20701
20733
|
result = { memories: await listRecentFacts(ctx, effectiveLimit, toolOptions) };
|
|
@@ -21164,6 +21196,7 @@ function createLocalMemoryStore(options) {
|
|
|
21164
21196
|
};
|
|
21165
21197
|
const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
|
|
21166
21198
|
const ownedBy = userId !== void 0 ? [Q15.where("user_id", userId)] : [];
|
|
21199
|
+
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [Q15.or(Q15.where("user_id", userId), Q15.where("user_id", null))] : [Q15.where("user_id", userId)];
|
|
21167
21200
|
const watchedColumns = [
|
|
21168
21201
|
"content",
|
|
21169
21202
|
"scope",
|
|
@@ -21196,6 +21229,7 @@ function createLocalMemoryStore(options) {
|
|
|
21196
21229
|
});
|
|
21197
21230
|
};
|
|
21198
21231
|
return {
|
|
21232
|
+
masksEmbeddingInput: options.embeddingOptions.maskInput !== void 0,
|
|
21199
21233
|
list: (listOptions) => getAllVaultMemoriesOp(vaultCtx, listOptions),
|
|
21200
21234
|
get: (id) => getVaultMemoryOp(vaultCtx, id),
|
|
21201
21235
|
listArchived: async () => {
|
|
@@ -21206,6 +21240,29 @@ function createLocalMemoryStore(options) {
|
|
|
21206
21240
|
},
|
|
21207
21241
|
memoriesByTopics: (names) => getMemoriesByEntityNamesOp(entityCtx, names),
|
|
21208
21242
|
topicsByMemories: (memoryIds) => getEntitiesByMemoryIdsOp(entityCtx, memoryIds),
|
|
21243
|
+
listTopics: async () => {
|
|
21244
|
+
const links = await entityCtx.memoryEntityCollection.query(...linksOwnedBy).fetch();
|
|
21245
|
+
const members = /* @__PURE__ */ new Map();
|
|
21246
|
+
for (const link of links) {
|
|
21247
|
+
const entityId = String(link.entityId);
|
|
21248
|
+
const set = members.get(entityId) ?? /* @__PURE__ */ new Set();
|
|
21249
|
+
set.add(String(link.memoryId));
|
|
21250
|
+
members.set(entityId, set);
|
|
21251
|
+
}
|
|
21252
|
+
const entities = userId === void 0 ? await entityCtx.entityCollection.query().fetch() : members.size ? await entityCtx.entityCollection.query(Q15.where("id", Q15.oneOf([...members.keys()]))).fetch() : [];
|
|
21253
|
+
const byName = /* @__PURE__ */ new Map();
|
|
21254
|
+
for (const entity of entities) {
|
|
21255
|
+
const count2 = members.get(entity.id)?.size ?? 0;
|
|
21256
|
+
const existing = byName.get(entity.canonicalName);
|
|
21257
|
+
byName.set(entity.canonicalName, {
|
|
21258
|
+
name: entity.canonicalName,
|
|
21259
|
+
kind: existing?.kind ?? entity.kind ?? null,
|
|
21260
|
+
memoryCount: (existing?.memoryCount ?? 0) + count2
|
|
21261
|
+
});
|
|
21262
|
+
}
|
|
21263
|
+
return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
21264
|
+
},
|
|
21265
|
+
listProjections: (listOptions) => getVaultMemoryProjectionsOp(vaultCtx, listOptions),
|
|
21209
21266
|
create: async (input) => {
|
|
21210
21267
|
const created = await createVaultMemoryOp(vaultCtx, input);
|
|
21211
21268
|
if (input.embedding === void 0) embedInBackground(created);
|
|
@@ -21254,6 +21311,22 @@ function createLocalMemoryStore(options) {
|
|
|
21254
21311
|
{ vaultCtx, embeddingOptions: options.embeddingOptions, vaultCache },
|
|
21255
21312
|
retainOptions
|
|
21256
21313
|
),
|
|
21314
|
+
factSource: {
|
|
21315
|
+
search: (query, searchOptions) => searchVaultMemoriesWithSize(
|
|
21316
|
+
query,
|
|
21317
|
+
vaultCtx,
|
|
21318
|
+
options.embeddingOptions,
|
|
21319
|
+
vaultCache,
|
|
21320
|
+
searchOptions
|
|
21321
|
+
),
|
|
21322
|
+
graphRanking: (query, traverse, recallOptions) => buildGraphLaneRanking(
|
|
21323
|
+
query,
|
|
21324
|
+
{ vaultCtx, entityCtx, embeddingOptions: options.embeddingOptions },
|
|
21325
|
+
traverse,
|
|
21326
|
+
graphTraversalOptions(recallOptions, traverse)
|
|
21327
|
+
),
|
|
21328
|
+
temporalRanking: (query, now) => buildTemporalLaneRanking(query, vaultCtx, now)
|
|
21329
|
+
},
|
|
21257
21330
|
subscribe: (onChange, subscribeOptions) => {
|
|
21258
21331
|
const afterFirst = () => {
|
|
21259
21332
|
let first = true;
|
|
@@ -21267,7 +21340,6 @@ function createLocalMemoryStore(options) {
|
|
|
21267
21340
|
);
|
|
21268
21341
|
const subscriptions = [memories.subscribe(afterFirst())];
|
|
21269
21342
|
if (subscribeOptions?.topics) {
|
|
21270
|
-
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [Q15.or(Q15.where("user_id", userId), Q15.where("user_id", null))] : [Q15.where("user_id", userId)];
|
|
21271
21343
|
subscriptions.push(
|
|
21272
21344
|
entityCtx.memoryEntityCollection.query(...linksOwnedBy).observe().subscribe(afterFirst())
|
|
21273
21345
|
);
|
|
@@ -21677,6 +21749,31 @@ async function createRemoteMemoryPersistence(options) {
|
|
|
21677
21749
|
throw new Error("Invalid nearby candidate response");
|
|
21678
21750
|
return result;
|
|
21679
21751
|
};
|
|
21752
|
+
const fetchQueryPage = async (queryOptions) => {
|
|
21753
|
+
const { signal, ...query } = queryOptions;
|
|
21754
|
+
if (query.limit !== void 0 && (!Number.isInteger(query.limit) || query.limit < 1 || query.limit > 200))
|
|
21755
|
+
throw new Error("Page limit must be 1\u2013200");
|
|
21756
|
+
for (const [key, max] of queryListLimits) {
|
|
21757
|
+
const values = query[key];
|
|
21758
|
+
if (values && values.length > max)
|
|
21759
|
+
throw new Error(`Query ${key} accepts at most ${max} entries (got ${values.length})`);
|
|
21760
|
+
}
|
|
21761
|
+
const page = await request(
|
|
21762
|
+
"/memories/query",
|
|
21763
|
+
{
|
|
21764
|
+
method: "POST",
|
|
21765
|
+
body: JSON.stringify(
|
|
21766
|
+
Object.fromEntries(
|
|
21767
|
+
queryFields.filter((key) => query[key] !== void 0).map((key) => [key, query[key]])
|
|
21768
|
+
)
|
|
21769
|
+
)
|
|
21770
|
+
},
|
|
21771
|
+
signal
|
|
21772
|
+
);
|
|
21773
|
+
if (!record(page) || !Array.isArray(page.items) || page.next_cursor !== void 0 && typeof page.next_cursor !== "string" || page.changes_cursor !== void 0 && typeof page.changes_cursor !== "string")
|
|
21774
|
+
throw new Error("Invalid nearby private-memory page");
|
|
21775
|
+
return page;
|
|
21776
|
+
};
|
|
21680
21777
|
const candidateSet = async (embedding, candidateOptions = {}) => {
|
|
21681
21778
|
const result = await fetchCandidates(embedding, candidateOptions, true);
|
|
21682
21779
|
if (!Number.isSafeInteger(result.total_count) || result.total_count < 0 || !Number.isSafeInteger(result.unavailable_count) || result.unavailable_count < 0)
|
|
@@ -21755,34 +21852,27 @@ async function createRemoteMemoryPersistence(options) {
|
|
|
21755
21852
|
return Promise.all(result.items.map((item, i) => decodeCommitted(item, prepared[i])));
|
|
21756
21853
|
},
|
|
21757
21854
|
query: async (queryOptions = {}) => {
|
|
21758
|
-
const
|
|
21759
|
-
if (query.limit !== void 0 && (!Number.isInteger(query.limit) || query.limit < 1 || query.limit > 200))
|
|
21760
|
-
throw new Error("Page limit must be 1\u2013200");
|
|
21761
|
-
for (const [key, max] of queryListLimits) {
|
|
21762
|
-
const values = query[key];
|
|
21763
|
-
if (values && values.length > max)
|
|
21764
|
-
throw new Error(`Query ${key} accepts at most ${max} entries (got ${values.length})`);
|
|
21765
|
-
}
|
|
21766
|
-
const page = await request(
|
|
21767
|
-
"/memories/query",
|
|
21768
|
-
{
|
|
21769
|
-
method: "POST",
|
|
21770
|
-
body: JSON.stringify(
|
|
21771
|
-
Object.fromEntries(
|
|
21772
|
-
queryFields.filter((key) => query[key] !== void 0).map((key) => [key, query[key]])
|
|
21773
|
-
)
|
|
21774
|
-
)
|
|
21775
|
-
},
|
|
21776
|
-
signal
|
|
21777
|
-
);
|
|
21778
|
-
if (!record(page) || !Array.isArray(page.items) || page.next_cursor !== void 0 && typeof page.next_cursor !== "string" || page.changes_cursor !== void 0 && typeof page.changes_cursor !== "string")
|
|
21779
|
-
throw new Error("Invalid nearby private-memory page");
|
|
21855
|
+
const page = await fetchQueryPage(queryOptions);
|
|
21780
21856
|
return {
|
|
21781
21857
|
...await decodeAll(page.items),
|
|
21782
21858
|
...page.next_cursor !== void 0 && { next_cursor: page.next_cursor },
|
|
21783
21859
|
...page.changes_cursor !== void 0 && { changes_cursor: page.changes_cursor }
|
|
21784
21860
|
};
|
|
21785
21861
|
},
|
|
21862
|
+
queryMetadata: async (queryOptions = {}) => {
|
|
21863
|
+
const page = await fetchQueryPage(queryOptions);
|
|
21864
|
+
return {
|
|
21865
|
+
items: page.items.map((item) => {
|
|
21866
|
+
const {
|
|
21867
|
+
memory: { content: _content, kind_value: _kindValue, ...memory },
|
|
21868
|
+
version,
|
|
21869
|
+
server_updated_at
|
|
21870
|
+
} = memoryRecord(item);
|
|
21871
|
+
return { memory, version, server_updated_at };
|
|
21872
|
+
}),
|
|
21873
|
+
...page.next_cursor !== void 0 && { next_cursor: page.next_cursor }
|
|
21874
|
+
};
|
|
21875
|
+
},
|
|
21786
21876
|
candidateSet,
|
|
21787
21877
|
candidates: async (embedding, candidateOptions = {}) => decodeAll((await fetchCandidates(embedding, candidateOptions, false)).items)
|
|
21788
21878
|
};
|
|
@@ -22123,38 +22213,31 @@ function createRemoteMemoryPipeline(options) {
|
|
|
22123
22213
|
};
|
|
22124
22214
|
return { prepare, port };
|
|
22125
22215
|
};
|
|
22126
|
-
|
|
22127
|
-
|
|
22216
|
+
const factSource = {
|
|
22217
|
+
graphRanking: options.graphRanking,
|
|
22218
|
+
temporalRanking: options.temporalRanking,
|
|
22219
|
+
search: async (text13, searchOptions) => {
|
|
22128
22220
|
const { prepare } = operation("recall");
|
|
22129
|
-
|
|
22130
|
-
|
|
22131
|
-
|
|
22132
|
-
|
|
22133
|
-
|
|
22134
|
-
|
|
22135
|
-
temporalRanking: options.temporalRanking,
|
|
22136
|
-
search: async (text13, searchOptions) => {
|
|
22137
|
-
const prepared = await prepare(text13, searchOptions);
|
|
22138
|
-
const ranked = await rankPreparedVaultCandidates(
|
|
22139
|
-
text13,
|
|
22140
|
-
prepared,
|
|
22141
|
-
embeddingOptions,
|
|
22142
|
-
searchOptions
|
|
22143
|
-
);
|
|
22144
|
-
return {
|
|
22145
|
-
...ranked,
|
|
22146
|
-
rankedOnCosine: prepared.memories.length > 0 && !prepared.embeddingsUnavailable,
|
|
22147
|
-
decryptLast: true,
|
|
22148
|
-
rowsDecrypted: prepared.rowsDecrypted,
|
|
22149
|
-
queryEmbedMs: prepared.queryEmbedMs,
|
|
22150
|
-
rowsEmbedded: 0
|
|
22151
|
-
};
|
|
22152
|
-
}
|
|
22153
|
-
}
|
|
22154
|
-
},
|
|
22155
|
-
{ ...recallOptions, types: ["fact"] }
|
|
22221
|
+
const prepared = await prepare(text13, searchOptions);
|
|
22222
|
+
const ranked = await rankPreparedVaultCandidates(
|
|
22223
|
+
text13,
|
|
22224
|
+
prepared,
|
|
22225
|
+
embeddingOptions,
|
|
22226
|
+
searchOptions
|
|
22156
22227
|
);
|
|
22157
|
-
|
|
22228
|
+
return {
|
|
22229
|
+
...ranked,
|
|
22230
|
+
rankedOnCosine: prepared.memories.length > 0 && !prepared.embeddingsUnavailable,
|
|
22231
|
+
decryptLast: true,
|
|
22232
|
+
rowsDecrypted: prepared.rowsDecrypted,
|
|
22233
|
+
queryEmbedMs: prepared.queryEmbedMs,
|
|
22234
|
+
rowsEmbedded: 0
|
|
22235
|
+
};
|
|
22236
|
+
}
|
|
22237
|
+
};
|
|
22238
|
+
return {
|
|
22239
|
+
factSource,
|
|
22240
|
+
recall: async (query, recallOptions = {}) => recall(query, { embeddingOptions, factSource }, { ...recallOptions, types: ["fact"] }),
|
|
22158
22241
|
retain: async (content, retainOptions) => {
|
|
22159
22242
|
const { port } = operation("retain");
|
|
22160
22243
|
return retainWithPersistence(
|
|
@@ -22173,6 +22256,19 @@ function createRemoteMemoryPipeline(options) {
|
|
|
22173
22256
|
|
|
22174
22257
|
// src/lib/memory/store/remoteStore.ts
|
|
22175
22258
|
import { v7 as uuidv76 } from "uuid";
|
|
22259
|
+
function projection({ memory }) {
|
|
22260
|
+
return {
|
|
22261
|
+
uniqueId: memory.memory_id,
|
|
22262
|
+
scope: memory.scope,
|
|
22263
|
+
folderId: memory.folder_id ?? null,
|
|
22264
|
+
embedding: memory.embedding?.length ? JSON.stringify(memory.embedding) : null,
|
|
22265
|
+
embeddingModel: memory.embedding_model ?? null,
|
|
22266
|
+
createdAt: new Date(memory.created_at),
|
|
22267
|
+
updatedAt: new Date(memory.updated_at),
|
|
22268
|
+
lastObservedAt: memory.last_observed_at ?? null,
|
|
22269
|
+
topicsUserManaged: memory.topics_user_managed ?? false
|
|
22270
|
+
};
|
|
22271
|
+
}
|
|
22176
22272
|
function relevant(change, options) {
|
|
22177
22273
|
return options.includeDeleted ? change === "membership" : change !== "embedding" || !!options.embeddings;
|
|
22178
22274
|
}
|
|
@@ -22207,6 +22303,9 @@ function bounded(filters, key) {
|
|
|
22207
22303
|
if (!values || values.length <= MAX_QUERY_FILTER) return [filters];
|
|
22208
22304
|
return chunks(values, MAX_QUERY_FILTER).map((chunk) => ({ ...filters, [key]: chunk }));
|
|
22209
22305
|
}
|
|
22306
|
+
function maxOf(options) {
|
|
22307
|
+
return options.limit && options.limit > 0 ? options.limit : Infinity;
|
|
22308
|
+
}
|
|
22210
22309
|
function chunks(values, size) {
|
|
22211
22310
|
const out = [];
|
|
22212
22311
|
for (let i = 0; i < values.length; i += size) out.push(values.slice(i, i + size));
|
|
@@ -22278,26 +22377,49 @@ function createRemoteMemoryStore(options) {
|
|
|
22278
22377
|
for (const subscriber of [...subscribers])
|
|
22279
22378
|
if (relevant(change, subscriber.options)) safely(subscriber.onChange);
|
|
22280
22379
|
};
|
|
22281
|
-
const
|
|
22380
|
+
const pages = async (read, filters, max = Infinity) => {
|
|
22282
22381
|
const records = [];
|
|
22283
22382
|
let cursor2;
|
|
22284
22383
|
do {
|
|
22285
|
-
const page = await
|
|
22384
|
+
const page = await read({
|
|
22286
22385
|
...filters,
|
|
22287
22386
|
limit: Math.min(200, Math.max(1, max - records.length)),
|
|
22288
22387
|
...cursor2 && { cursor: cursor2 }
|
|
22289
22388
|
});
|
|
22290
|
-
warnFailed(page.failed);
|
|
22389
|
+
if (page.failed) warnFailed(page.failed);
|
|
22291
22390
|
records.push(...page.items);
|
|
22292
22391
|
cursor2 = page.next_cursor;
|
|
22293
22392
|
} while (cursor2 && records.length < max);
|
|
22294
22393
|
return records.slice(0, max);
|
|
22295
22394
|
};
|
|
22296
|
-
const
|
|
22395
|
+
const decrypted = (options2) => persistence.query(options2);
|
|
22396
|
+
const metadata = (options2) => persistence.queryMetadata(options2);
|
|
22397
|
+
const enumerate = (filters, max = Infinity) => pages(decrypted, filters, max);
|
|
22398
|
+
const enumerateIds = async (read, ids, filters) => (await Promise.all(
|
|
22297
22399
|
chunks([...new Set(ids)], MAX_QUERY_IDS).map(
|
|
22298
|
-
(memory_ids) =>
|
|
22400
|
+
(memory_ids) => pages(read, { ...filters, memory_ids })
|
|
22299
22401
|
)
|
|
22300
22402
|
)).flat();
|
|
22403
|
+
const listFilters = (listOptions) => ({
|
|
22404
|
+
...stateFilters(listOptions),
|
|
22405
|
+
include_embeddings: true,
|
|
22406
|
+
order: listOptions.since ? "updated" : "created",
|
|
22407
|
+
...listOptions.scopes?.length && { scopes: listOptions.scopes },
|
|
22408
|
+
...listOptions.factTypes?.length && { fact_types: listOptions.factTypes },
|
|
22409
|
+
...listOptions.visibility?.length && { visibility: listOptions.visibility },
|
|
22410
|
+
...listOptions.since && { updated_after: listOptions.since.getTime() }
|
|
22411
|
+
});
|
|
22412
|
+
const listRows = async (read, listOptions, filters, max) => {
|
|
22413
|
+
const variants = bounded(filters, "scopes").flatMap((f) => bounded(f, "fact_types"));
|
|
22414
|
+
if (!listOptions.memoryIds && variants.length === 1) return pages(read, filters, max);
|
|
22415
|
+
const fetched = await Promise.all(
|
|
22416
|
+
variants.map(
|
|
22417
|
+
(variant) => listOptions.memoryIds ? enumerateIds(read, listOptions.memoryIds, variant) : pages(read, variant, max)
|
|
22418
|
+
)
|
|
22419
|
+
);
|
|
22420
|
+
const time = (row) => filters.order === "updated" ? row.memory.updated_at : row.memory.created_at;
|
|
22421
|
+
return [...new Map(fetched.flat().map((r) => [r.memory.memory_id, r])).values()].sort((a, b) => time(b) - time(a) || (a.memory.memory_id < b.memory.memory_id ? 1 : -1)).slice(0, max);
|
|
22422
|
+
};
|
|
22301
22423
|
const queues = /* @__PURE__ */ new Map();
|
|
22302
22424
|
const serialized = (id, task) => {
|
|
22303
22425
|
const run = (queues.get(id) ?? Promise.resolve()).then(task, task);
|
|
@@ -22339,12 +22461,15 @@ function createRemoteMemoryStore(options) {
|
|
|
22339
22461
|
};
|
|
22340
22462
|
const newRow = (input) => {
|
|
22341
22463
|
const now = Date.now();
|
|
22342
|
-
const scope = input
|
|
22464
|
+
const { level, scope } = resolveCreateLevel(input);
|
|
22465
|
+
assertValidKindLevel(input.kind, level);
|
|
22343
22466
|
return {
|
|
22344
22467
|
memory_id: uuidv76(),
|
|
22345
22468
|
content: input.content,
|
|
22346
22469
|
scope,
|
|
22347
|
-
level
|
|
22470
|
+
level,
|
|
22471
|
+
...input.kind !== void 0 && input.kind !== null && { kind: input.kind },
|
|
22472
|
+
...typeof input.kindValue === "string" && { kind_value: input.kindValue },
|
|
22348
22473
|
created_at: now,
|
|
22349
22474
|
updated_at: now,
|
|
22350
22475
|
is_deleted: false,
|
|
@@ -22373,6 +22498,30 @@ function createRemoteMemoryStore(options) {
|
|
|
22373
22498
|
return records.map((record2) => stored(record2.memory));
|
|
22374
22499
|
};
|
|
22375
22500
|
const asStored = (record2) => record2 ? stored(record2.memory) : null;
|
|
22501
|
+
const withKnownKinds = async (topics) => {
|
|
22502
|
+
const unkinded = [
|
|
22503
|
+
...new Set(topics.filter((t) => !t.kind).map((t) => normalizeEntityName(t.name)))
|
|
22504
|
+
];
|
|
22505
|
+
if (unkinded.length === 0) return topics;
|
|
22506
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
22507
|
+
for (const names of chunks(unkinded, MAX_QUERY_TOPICS)) {
|
|
22508
|
+
for (const { memory } of await pages(metadata, {
|
|
22509
|
+
topics: names,
|
|
22510
|
+
include_archived: true,
|
|
22511
|
+
include_quarantined: true,
|
|
22512
|
+
include_superseded: true
|
|
22513
|
+
})) {
|
|
22514
|
+
for (const topic of parseTopics(memory.topics) ?? []) {
|
|
22515
|
+
const name = normalizeEntityName(topic.name);
|
|
22516
|
+
if (topic.kind && !kinds.has(name)) kinds.set(name, topic.kind);
|
|
22517
|
+
}
|
|
22518
|
+
}
|
|
22519
|
+
}
|
|
22520
|
+
return topics.map((topic) => {
|
|
22521
|
+
const kind = topic.kind ?? kinds.get(normalizeEntityName(topic.name));
|
|
22522
|
+
return kind ? { ...topic, kind } : topic;
|
|
22523
|
+
});
|
|
22524
|
+
};
|
|
22376
22525
|
const everyState = {
|
|
22377
22526
|
include_deleted: true,
|
|
22378
22527
|
include_archived: true,
|
|
@@ -22449,30 +22598,35 @@ function createRemoteMemoryStore(options) {
|
|
|
22449
22598
|
});
|
|
22450
22599
|
};
|
|
22451
22600
|
return {
|
|
22452
|
-
|
|
22453
|
-
|
|
22454
|
-
|
|
22455
|
-
|
|
22456
|
-
|
|
22457
|
-
|
|
22458
|
-
|
|
22459
|
-
|
|
22460
|
-
|
|
22461
|
-
|
|
22462
|
-
|
|
22463
|
-
|
|
22464
|
-
|
|
22465
|
-
|
|
22466
|
-
|
|
22467
|
-
|
|
22468
|
-
|
|
22469
|
-
|
|
22470
|
-
|
|
22471
|
-
|
|
22472
|
-
|
|
22473
|
-
|
|
22474
|
-
|
|
22475
|
-
|
|
22601
|
+
masksEmbeddingInput: embeddingOptions.maskInput !== void 0,
|
|
22602
|
+
list: async (listOptions = {}) => (await listRows(decrypted, listOptions, listFilters(listOptions), maxOf(listOptions))).map(
|
|
22603
|
+
(r) => stored(r.memory)
|
|
22604
|
+
),
|
|
22605
|
+
listProjections: async (listOptions = {}) => (await listRows(metadata, listOptions, listFilters(listOptions), maxOf(listOptions))).map(
|
|
22606
|
+
projection
|
|
22607
|
+
),
|
|
22608
|
+
listTopics: async () => {
|
|
22609
|
+
const topics = /* @__PURE__ */ new Map();
|
|
22610
|
+
for (const { memory } of await pages(metadata, {
|
|
22611
|
+
include_archived: true,
|
|
22612
|
+
include_quarantined: true,
|
|
22613
|
+
include_superseded: true
|
|
22614
|
+
})) {
|
|
22615
|
+
for (const topic of parseTopics(memory.topics) ?? []) {
|
|
22616
|
+
const name = normalizeEntityName(topic.name);
|
|
22617
|
+
if (!name) continue;
|
|
22618
|
+
const entry = topics.get(name) ?? {
|
|
22619
|
+
name,
|
|
22620
|
+
kind: null,
|
|
22621
|
+
memoryCount: 0,
|
|
22622
|
+
members: /* @__PURE__ */ new Set()
|
|
22623
|
+
};
|
|
22624
|
+
entry.kind ?? (entry.kind = topic.kind ?? null);
|
|
22625
|
+
entry.members.add(memory.memory_id);
|
|
22626
|
+
topics.set(name, entry);
|
|
22627
|
+
}
|
|
22628
|
+
}
|
|
22629
|
+
return [...topics.values()].map(({ members, ...topic }) => ({ ...topic, memoryCount: members.size })).sort((a, b) => a.name.localeCompare(b.name));
|
|
22476
22630
|
},
|
|
22477
22631
|
get: async (id) => asStored(await live(id)),
|
|
22478
22632
|
listArchived: async () => (await enumerate({ archived_only: true, order: "archived", include_embeddings: true })).map(
|
|
@@ -22481,10 +22635,10 @@ function createRemoteMemoryStore(options) {
|
|
|
22481
22635
|
memoriesByTopics: async (names) => {
|
|
22482
22636
|
const wanted = new Set(names.map(normalizeEntityName).filter(Boolean));
|
|
22483
22637
|
const out = /* @__PURE__ */ new Map();
|
|
22484
|
-
const
|
|
22638
|
+
const pages2 = await Promise.all(
|
|
22485
22639
|
chunks([...wanted], MAX_QUERY_TOPICS).map((topics) => enumerate({ topics }))
|
|
22486
22640
|
);
|
|
22487
|
-
for (const { memory } of
|
|
22641
|
+
for (const { memory } of pages2.flat()) {
|
|
22488
22642
|
const matched = [...canonicalTopics(memory)].filter((name) => wanted.has(name));
|
|
22489
22643
|
if (matched.length) out.set(memory.memory_id, new Set(matched));
|
|
22490
22644
|
}
|
|
@@ -22492,7 +22646,7 @@ function createRemoteMemoryStore(options) {
|
|
|
22492
22646
|
},
|
|
22493
22647
|
topicsByMemories: async (memoryIds) => {
|
|
22494
22648
|
const out = /* @__PURE__ */ new Map();
|
|
22495
|
-
for (const { memory } of await enumerateIds(memoryIds, {
|
|
22649
|
+
for (const { memory } of await enumerateIds(decrypted, memoryIds, {
|
|
22496
22650
|
include_archived: true,
|
|
22497
22651
|
include_superseded: true,
|
|
22498
22652
|
include_quarantined: true
|
|
@@ -22525,15 +22679,26 @@ function createRemoteMemoryStore(options) {
|
|
|
22525
22679
|
update: async (id, patch) => {
|
|
22526
22680
|
const saved = await mutate(id, "edit", (memory) => {
|
|
22527
22681
|
if (memory.superseded_by) return null;
|
|
22682
|
+
const level = patch.level !== void 0 ? patch.level : patch.scope !== void 0 ? levelForScopeChange({ level: memory.level, scope: memory.scope }, patch.scope) : resolveMemoryLevel(memory.level, memory.scope);
|
|
22683
|
+
const kind = patch.kind !== void 0 ? patch.kind : memory.kind;
|
|
22684
|
+
assertValidKindLevel(kind, level);
|
|
22528
22685
|
memory.content = patch.content;
|
|
22529
22686
|
memory.updated_at = Date.now();
|
|
22530
|
-
if (patch.
|
|
22531
|
-
memory.level =
|
|
22532
|
-
|
|
22533
|
-
|
|
22534
|
-
|
|
22687
|
+
if (patch.level !== void 0) {
|
|
22688
|
+
memory.level = level;
|
|
22689
|
+
memory.scope = scopeForLevel(level);
|
|
22690
|
+
} else if (patch.scope !== void 0) {
|
|
22691
|
+
memory.level = level;
|
|
22535
22692
|
memory.scope = patch.scope;
|
|
22536
22693
|
}
|
|
22694
|
+
if (patch.kind !== void 0) {
|
|
22695
|
+
if (patch.kind === null) delete memory.kind;
|
|
22696
|
+
else memory.kind = patch.kind;
|
|
22697
|
+
}
|
|
22698
|
+
if (patch.kindValue !== void 0) {
|
|
22699
|
+
if (patch.kindValue === null) delete memory.kind_value;
|
|
22700
|
+
else memory.kind_value = patch.kindValue;
|
|
22701
|
+
}
|
|
22537
22702
|
if (patch.factType !== void 0) memory.fact_type = patch.factType ?? void 0;
|
|
22538
22703
|
if (patch.eventTime !== void 0) {
|
|
22539
22704
|
memory.event_time_start = patch.eventTime?.start ?? void 0;
|
|
@@ -22588,16 +22753,19 @@ function createRemoteMemoryStore(options) {
|
|
|
22588
22753
|
notify("edit", [await persistence.put(memory, record2)]);
|
|
22589
22754
|
return true;
|
|
22590
22755
|
}),
|
|
22591
|
-
setTopics: async (memoryId, topics) =>
|
|
22592
|
-
await
|
|
22593
|
-
|
|
22594
|
-
|
|
22595
|
-
|
|
22596
|
-
|
|
22597
|
-
|
|
22598
|
-
|
|
22756
|
+
setTopics: async (memoryId, topics) => {
|
|
22757
|
+
const chosen = await withKnownKinds(topicInputs(topics, "user"));
|
|
22758
|
+
return asStored(
|
|
22759
|
+
await mutate(memoryId, "edit", (memory) => ({
|
|
22760
|
+
...memory,
|
|
22761
|
+
topics: serializeTopics(mergeTopics([], chosen)),
|
|
22762
|
+
topics_user_managed: true,
|
|
22763
|
+
topics_updated_at: Date.now()
|
|
22764
|
+
}))
|
|
22765
|
+
);
|
|
22766
|
+
},
|
|
22599
22767
|
addTopics: async (memoryId, topics) => {
|
|
22600
|
-
const added = topicInputs(topics, "auto");
|
|
22768
|
+
const added = await withKnownKinds(topicInputs(topics, "auto"));
|
|
22601
22769
|
const saved = await mutate(memoryId, "edit", (memory) => ({
|
|
22602
22770
|
...memory,
|
|
22603
22771
|
topics: serializeTopics(mergeTopics(parseTopics(memory.topics) ?? [], added)),
|
|
@@ -22626,6 +22794,7 @@ function createRemoteMemoryStore(options) {
|
|
|
22626
22794
|
})
|
|
22627
22795
|
),
|
|
22628
22796
|
recall: (query, recallOptions) => pipeline.recall(query, recallOptions),
|
|
22797
|
+
factSource: pipeline.factSource,
|
|
22629
22798
|
retain: async (content, retainOptions) => {
|
|
22630
22799
|
const result = await pipeline.retain(content, retainOptions);
|
|
22631
22800
|
if (result.action === "create" || result.action === "supersede") notify("membership");
|
|
@@ -24389,19 +24558,20 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
|
|
|
24389
24558
|
const budget = wantsDecompose ? "high" : searchOptions?.rerank ? "mid" : "low";
|
|
24390
24559
|
const folderId = searchOptions?.folderId ?? argFolderId;
|
|
24391
24560
|
if (searchOptions?.useFusion === false) {
|
|
24392
|
-
const
|
|
24561
|
+
const legacyOptions = {
|
|
24562
|
+
limit: requestLimit,
|
|
24563
|
+
minSimilarity,
|
|
24564
|
+
useFusion: false,
|
|
24565
|
+
...tuningForward,
|
|
24566
|
+
...folderId !== void 0 && { folderId },
|
|
24567
|
+
...searchOptions?.scopes && { scopes: searchOptions.scopes }
|
|
24568
|
+
};
|
|
24569
|
+
const { results: legacy, embeddingsUnavailable } = "retain" in vaultCtx ? await vaultCtx.factSource.search(query, legacyOptions) : await searchVaultMemoriesWithSize(
|
|
24393
24570
|
query,
|
|
24394
24571
|
vaultCtx,
|
|
24395
24572
|
embeddingOptions,
|
|
24396
24573
|
cache,
|
|
24397
|
-
|
|
24398
|
-
limit: requestLimit,
|
|
24399
|
-
minSimilarity,
|
|
24400
|
-
useFusion: false,
|
|
24401
|
-
...tuningForward,
|
|
24402
|
-
...folderId !== void 0 && { folderId },
|
|
24403
|
-
...searchOptions?.scopes && { scopes: searchOptions.scopes }
|
|
24404
|
-
}
|
|
24574
|
+
legacyOptions
|
|
24405
24575
|
);
|
|
24406
24576
|
if (legacy.length === 0) {
|
|
24407
24577
|
return embeddingsUnavailable ? EMBEDDINGS_DEGRADED_EMPTY_NO_LEXICAL : "No relevant memories found in the vault.";
|
|
@@ -24420,7 +24590,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
|
|
|
24420
24590
|
let recallDegraded = [];
|
|
24421
24591
|
const result = await recall(
|
|
24422
24592
|
query,
|
|
24423
|
-
{ vaultCtx, embeddingOptions, vaultCache: cache },
|
|
24593
|
+
"retain" in vaultCtx ? { factSource: vaultCtx.factSource, embeddingOptions } : { vaultCtx, embeddingOptions, vaultCache: cache },
|
|
24424
24594
|
{
|
|
24425
24595
|
onDiagnostics: (d) => {
|
|
24426
24596
|
recallDegraded = d.degraded;
|
|
@@ -24500,8 +24670,10 @@ function describeWriteOutcome(outcome2) {
|
|
|
24500
24670
|
}
|
|
24501
24671
|
}
|
|
24502
24672
|
function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
24673
|
+
const store = "retain" in vaultCtx ? vaultCtx : void 0;
|
|
24674
|
+
const ctx = "retain" in vaultCtx ? void 0 : vaultCtx;
|
|
24503
24675
|
const hasOnSave = !!options?.onSave;
|
|
24504
|
-
const folderNames = options?.folderMap ? Array.from(options.folderMap.keys()) : [];
|
|
24676
|
+
const folderNames = !store && options?.folderMap ? Array.from(options.folderMap.keys()) : [];
|
|
24505
24677
|
return {
|
|
24506
24678
|
type: "function",
|
|
24507
24679
|
function: {
|
|
@@ -24546,7 +24718,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
24546
24718
|
let previousContent;
|
|
24547
24719
|
const scope = options?.scope ?? "private";
|
|
24548
24720
|
if (isUpdate) {
|
|
24549
|
-
const existing = await getVaultMemoryOp(
|
|
24721
|
+
const existing = store ? await store.get(id) : await getVaultMemoryOp(ctx, id);
|
|
24550
24722
|
if (!existing) {
|
|
24551
24723
|
return `Error: Memory with ID "${id}" not found. Creating a new memory instead would require a separate call without an ID.`;
|
|
24552
24724
|
}
|
|
@@ -24567,9 +24739,18 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
24567
24739
|
return isUpdate ? `Memory update was cancelled by the user. The memory "${id}" was not modified.` : "Memory save was cancelled by the user. No memory was created.";
|
|
24568
24740
|
}
|
|
24569
24741
|
}
|
|
24570
|
-
if (isUpdate) {
|
|
24742
|
+
if (isUpdate && store) {
|
|
24743
|
+
const updated = await store.update(id, {
|
|
24744
|
+
content,
|
|
24745
|
+
...factType !== void 0 && { factType }
|
|
24746
|
+
});
|
|
24747
|
+
if (!updated) {
|
|
24748
|
+
return `Error: Failed to update memory "${id}".`;
|
|
24749
|
+
}
|
|
24750
|
+
return `Memory updated successfully (ID: ${updated.uniqueId}).`;
|
|
24751
|
+
} else if (isUpdate) {
|
|
24571
24752
|
const folderId = folderName ? options?.folderMap?.get(folderName) : void 0;
|
|
24572
|
-
const updated = await updateVaultMemoryOp(
|
|
24753
|
+
const updated = await updateVaultMemoryOp(ctx, id, {
|
|
24573
24754
|
content,
|
|
24574
24755
|
embedding: null,
|
|
24575
24756
|
folderId,
|
|
@@ -24584,7 +24765,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
24584
24765
|
content,
|
|
24585
24766
|
embeddingOptions,
|
|
24586
24767
|
cache,
|
|
24587
|
-
|
|
24768
|
+
ctx,
|
|
24588
24769
|
id,
|
|
24589
24770
|
updated.updatedAt
|
|
24590
24771
|
).catch(() => {
|
|
@@ -24604,18 +24785,22 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
24604
24785
|
await notifyWritten(options, input, outcome2);
|
|
24605
24786
|
return describeWriteOutcome(outcome2);
|
|
24606
24787
|
}
|
|
24607
|
-
const created = await
|
|
24788
|
+
const created = store ? await store.create({
|
|
24789
|
+
content,
|
|
24790
|
+
scope,
|
|
24791
|
+
...factType !== void 0 && { factType }
|
|
24792
|
+
}) : await createVaultMemoryOp(ctx, {
|
|
24608
24793
|
content,
|
|
24609
24794
|
scope,
|
|
24610
24795
|
folderId,
|
|
24611
24796
|
...factType !== void 0 && { factType }
|
|
24612
24797
|
});
|
|
24613
|
-
if (embeddingOptions && cache) {
|
|
24798
|
+
if (ctx && embeddingOptions && cache) {
|
|
24614
24799
|
eagerEmbedContent(
|
|
24615
24800
|
content,
|
|
24616
24801
|
embeddingOptions,
|
|
24617
24802
|
cache,
|
|
24618
|
-
|
|
24803
|
+
ctx,
|
|
24619
24804
|
created.uniqueId,
|
|
24620
24805
|
created.updatedAt
|
|
24621
24806
|
).catch(() => {
|
|
@@ -31587,6 +31772,8 @@ async function storedToLlmapiMessage(stored2, encryptionKey, resolveMediaByIds)
|
|
|
31587
31772
|
}
|
|
31588
31773
|
var CONVERSATION_REDACTOR_LIMIT = 50;
|
|
31589
31774
|
var NO_CONVERSATION_KEY = "__no_conversation__";
|
|
31775
|
+
var NO_STORE_FOLDERS = "Folders are not supported with a memoryStore";
|
|
31776
|
+
var NO_STORE_MASK = "piiRedaction needs a memoryStore built with embeddingOptions.maskInput";
|
|
31590
31777
|
var conversationRedactors = /* @__PURE__ */ new Map();
|
|
31591
31778
|
function getConversationRedactor(conversationId, nerDetector) {
|
|
31592
31779
|
const key = conversationId ?? NO_CONVERSATION_KEY;
|
|
@@ -31630,6 +31817,7 @@ function useChatStorage(options) {
|
|
|
31630
31817
|
signMessage,
|
|
31631
31818
|
embeddedWalletSigner,
|
|
31632
31819
|
getWalletAddress,
|
|
31820
|
+
memoryStore,
|
|
31633
31821
|
enableQueue = true,
|
|
31634
31822
|
autoFlushOnKeyAvailable = true,
|
|
31635
31823
|
extraToolSets,
|
|
@@ -31675,6 +31863,7 @@ function useChatStorage(options) {
|
|
|
31675
31863
|
() => ({ getToken, baseUrl, model: embeddingModel, maskInput: maskEmbeddingInput }),
|
|
31676
31864
|
[getToken, baseUrl, embeddingModel, maskEmbeddingInput]
|
|
31677
31865
|
);
|
|
31866
|
+
const storeMaskMissing = memoryStore !== void 0 && maskEmbeddingInput !== void 0 && !memoryStore.masksEmbeddingInput;
|
|
31678
31867
|
const blobManagerRef = useRef4(new BlobUrlManager());
|
|
31679
31868
|
useEffect4(() => {
|
|
31680
31869
|
const manager = blobManagerRef.current;
|
|
@@ -31951,6 +32140,15 @@ function useChatStorage(options) {
|
|
|
31951
32140
|
);
|
|
31952
32141
|
const retainVaultMemory = useCallback5(
|
|
31953
32142
|
async (input) => {
|
|
32143
|
+
if (memoryStore) {
|
|
32144
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32145
|
+
if (input.folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
32146
|
+
return memoryStore.retain(input.content, {
|
|
32147
|
+
source: "manual",
|
|
32148
|
+
scope: input.scope,
|
|
32149
|
+
...input.factType !== void 0 && { factType: input.factType }
|
|
32150
|
+
});
|
|
32151
|
+
}
|
|
31954
32152
|
if (!getToken) {
|
|
31955
32153
|
throw new Error("getToken is required to retain a vault memory");
|
|
31956
32154
|
}
|
|
@@ -31969,28 +32167,44 @@ function useChatStorage(options) {
|
|
|
31969
32167
|
}
|
|
31970
32168
|
);
|
|
31971
32169
|
},
|
|
31972
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
32170
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
31973
32171
|
);
|
|
31974
32172
|
const createMemoryVaultTool2 = useCallback5(
|
|
31975
32173
|
(options2) => {
|
|
32174
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
31976
32175
|
const embOpts = getToken ? vaultEmbeddingOptions : void 0;
|
|
31977
32176
|
return createMemoryVaultTool(
|
|
31978
|
-
vaultCtx,
|
|
31979
|
-
embOpts ? { write: retainVaultMemory, ...options2 } : options2,
|
|
32177
|
+
memoryStore ?? vaultCtx,
|
|
32178
|
+
embOpts || memoryStore ? { write: retainVaultMemory, ...options2 } : options2,
|
|
31980
32179
|
embOpts,
|
|
31981
32180
|
embOpts ? vaultEmbeddingCache : void 0
|
|
31982
32181
|
);
|
|
31983
32182
|
},
|
|
31984
|
-
[
|
|
32183
|
+
[
|
|
32184
|
+
vaultCtx,
|
|
32185
|
+
getToken,
|
|
32186
|
+
vaultEmbeddingOptions,
|
|
32187
|
+
vaultEmbeddingCache,
|
|
32188
|
+
retainVaultMemory,
|
|
32189
|
+
memoryStore,
|
|
32190
|
+
storeMaskMissing
|
|
32191
|
+
]
|
|
31985
32192
|
);
|
|
31986
32193
|
const getVaultMemories = useCallback5(
|
|
31987
32194
|
(options2) => {
|
|
31988
|
-
return getAllVaultMemoriesOp(vaultCtx, options2);
|
|
32195
|
+
if (!memoryStore) return getAllVaultMemoriesOp(vaultCtx, options2);
|
|
32196
|
+
const { folderId, levels, kinds, ...listOptions } = options2 ?? {};
|
|
32197
|
+
if (folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
32198
|
+
if (levels !== void 0 || kinds !== void 0)
|
|
32199
|
+
throw new Error("level and kind filters are not supported with a memoryStore");
|
|
32200
|
+
return memoryStore.list(listOptions);
|
|
31989
32201
|
},
|
|
31990
|
-
[vaultCtx]
|
|
32202
|
+
[vaultCtx, memoryStore]
|
|
31991
32203
|
);
|
|
31992
32204
|
const createVaultMemory = useCallback5(
|
|
31993
32205
|
async (content, scope) => {
|
|
32206
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32207
|
+
if (memoryStore) return memoryStore.create({ content, scope });
|
|
31994
32208
|
const result = await createVaultMemoryOp(vaultCtx, { content, scope });
|
|
31995
32209
|
if (getToken) {
|
|
31996
32210
|
eagerEmbedContent(
|
|
@@ -32006,10 +32220,12 @@ function useChatStorage(options) {
|
|
|
32006
32220
|
}
|
|
32007
32221
|
return result;
|
|
32008
32222
|
},
|
|
32009
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
32223
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
32010
32224
|
);
|
|
32011
32225
|
const updateVaultMemory = useCallback5(
|
|
32012
32226
|
async (id, content, scope) => {
|
|
32227
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32228
|
+
if (memoryStore) return memoryStore.update(id, { content, scope });
|
|
32013
32229
|
const existing = await getVaultMemoryOp(vaultCtx, id);
|
|
32014
32230
|
const result = await updateVaultMemoryOp(vaultCtx, id, { content, scope, embedding: null });
|
|
32015
32231
|
if (result && getToken) {
|
|
@@ -32029,10 +32245,11 @@ function useChatStorage(options) {
|
|
|
32029
32245
|
}
|
|
32030
32246
|
return result;
|
|
32031
32247
|
},
|
|
32032
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
32248
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
32033
32249
|
);
|
|
32034
32250
|
const deleteVaultMemory = useCallback5(
|
|
32035
32251
|
async (id) => {
|
|
32252
|
+
if (memoryStore) return memoryStore.delete(id);
|
|
32036
32253
|
const existing = await getVaultMemoryOp(vaultCtx, id);
|
|
32037
32254
|
const result = await deleteVaultMemoryOp(vaultCtx, id);
|
|
32038
32255
|
if (result && existing) {
|
|
@@ -32040,19 +32257,19 @@ function useChatStorage(options) {
|
|
|
32040
32257
|
}
|
|
32041
32258
|
return result;
|
|
32042
32259
|
},
|
|
32043
|
-
[vaultCtx, vaultEmbeddingCache]
|
|
32260
|
+
[vaultCtx, vaultEmbeddingCache, memoryStore]
|
|
32044
32261
|
);
|
|
32045
32262
|
const chunkVectorCacheRef = useRef4(createChunkVectorCache());
|
|
32046
32263
|
const clientToolEmbeddingsCacheRef = useRef4(/* @__PURE__ */ new Map());
|
|
32047
32264
|
useEffect4(() => {
|
|
32048
|
-
if (!getToken) return;
|
|
32265
|
+
if (!getToken || memoryStore) return;
|
|
32049
32266
|
void (async () => {
|
|
32050
32267
|
try {
|
|
32051
32268
|
await preEmbedVaultMemories(vaultCtx, vaultEmbeddingOptions, vaultEmbeddingCache);
|
|
32052
32269
|
} catch {
|
|
32053
32270
|
}
|
|
32054
32271
|
})();
|
|
32055
|
-
}, [vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]);
|
|
32272
|
+
}, [vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore]);
|
|
32056
32273
|
useEffect4(() => {
|
|
32057
32274
|
return onClearAllEncryptionState(() => {
|
|
32058
32275
|
vaultEmbeddingCache.clear();
|
|
@@ -32061,26 +32278,35 @@ function useChatStorage(options) {
|
|
|
32061
32278
|
}, [vaultEmbeddingCache]);
|
|
32062
32279
|
const createMemoryVaultSearchTool2 = useCallback5(
|
|
32063
32280
|
(searchOptions) => {
|
|
32064
|
-
if (!getToken) {
|
|
32281
|
+
if (!getToken && !memoryStore) {
|
|
32065
32282
|
throw new Error("getToken is required for memory vault search tool");
|
|
32066
32283
|
}
|
|
32284
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32067
32285
|
return createMemoryVaultSearchTool(
|
|
32068
|
-
vaultCtx,
|
|
32286
|
+
memoryStore ?? vaultCtx,
|
|
32069
32287
|
vaultEmbeddingOptions,
|
|
32070
32288
|
vaultEmbeddingCache,
|
|
32071
32289
|
searchOptions
|
|
32072
32290
|
);
|
|
32073
32291
|
},
|
|
32074
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
32292
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
32075
32293
|
);
|
|
32076
32294
|
const createRecallTool2 = useCallback5(
|
|
32077
32295
|
(toolOptions, callbacks) => {
|
|
32078
|
-
if (!getToken) {
|
|
32296
|
+
if (!getToken && !memoryStore) {
|
|
32079
32297
|
throw new Error("getToken is required for recall tool");
|
|
32080
32298
|
}
|
|
32299
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32081
32300
|
const resolvedToolOptions = toolOptions?.excludeConversationId !== void 0 || !currentConversationId ? toolOptions : { ...toolOptions, excludeConversationId: currentConversationId };
|
|
32301
|
+
if (memoryStore && resolvedToolOptions?.folderId !== void 0)
|
|
32302
|
+
throw new Error(NO_STORE_FOLDERS);
|
|
32082
32303
|
return createRecallTool(
|
|
32083
|
-
{
|
|
32304
|
+
memoryStore ? {
|
|
32305
|
+
factSource: memoryStore.factSource,
|
|
32306
|
+
...getToken && { storageCtx },
|
|
32307
|
+
embeddingOptions: vaultEmbeddingOptions,
|
|
32308
|
+
chunkCache: chunkVectorCacheRef.current
|
|
32309
|
+
} : {
|
|
32084
32310
|
vaultCtx,
|
|
32085
32311
|
storageCtx,
|
|
32086
32312
|
embeddingOptions: vaultEmbeddingOptions,
|
|
@@ -32088,7 +32314,7 @@ function useChatStorage(options) {
|
|
|
32088
32314
|
chunkCache: chunkVectorCacheRef.current,
|
|
32089
32315
|
entityCtx
|
|
32090
32316
|
},
|
|
32091
|
-
resolvedToolOptions,
|
|
32317
|
+
memoryStore ? { ...resolvedToolOptions, memoryStore } : resolvedToolOptions,
|
|
32092
32318
|
callbacks
|
|
32093
32319
|
);
|
|
32094
32320
|
},
|
|
@@ -32099,11 +32325,15 @@ function useChatStorage(options) {
|
|
|
32099
32325
|
getToken,
|
|
32100
32326
|
vaultEmbeddingOptions,
|
|
32101
32327
|
vaultEmbeddingCache,
|
|
32102
|
-
currentConversationId
|
|
32328
|
+
currentConversationId,
|
|
32329
|
+
memoryStore,
|
|
32330
|
+
storeMaskMissing
|
|
32103
32331
|
]
|
|
32104
32332
|
);
|
|
32105
32333
|
const searchVaultMemoriesFn = useCallback5(
|
|
32106
32334
|
async (query, searchOptions) => {
|
|
32335
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32336
|
+
if (memoryStore) return (await memoryStore.factSource.search(query, searchOptions)).results;
|
|
32107
32337
|
if (!getToken) return [];
|
|
32108
32338
|
return searchVaultMemories(
|
|
32109
32339
|
query,
|
|
@@ -32113,11 +32343,11 @@ function useChatStorage(options) {
|
|
|
32113
32343
|
searchOptions
|
|
32114
32344
|
);
|
|
32115
32345
|
},
|
|
32116
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
32346
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
32117
32347
|
);
|
|
32118
32348
|
const recallFn = useCallback5(
|
|
32119
32349
|
async (query, options2) => {
|
|
32120
|
-
if (!getToken) {
|
|
32350
|
+
if (!getToken && !memoryStore) {
|
|
32121
32351
|
return {
|
|
32122
32352
|
memories: [],
|
|
32123
32353
|
usedBudget: options2?.budget ?? "low",
|
|
@@ -32126,9 +32356,16 @@ function useChatStorage(options) {
|
|
|
32126
32356
|
};
|
|
32127
32357
|
}
|
|
32128
32358
|
const resolvedOptions = options2?.excludeConversationId !== void 0 || !currentConversationId ? options2 : { ...options2, excludeConversationId: currentConversationId };
|
|
32359
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
32360
|
+
if (memoryStore && resolvedOptions?.folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
32129
32361
|
return recall(
|
|
32130
32362
|
query,
|
|
32131
|
-
{
|
|
32363
|
+
memoryStore ? {
|
|
32364
|
+
factSource: memoryStore.factSource,
|
|
32365
|
+
...getToken && { storageCtx },
|
|
32366
|
+
embeddingOptions: vaultEmbeddingOptions,
|
|
32367
|
+
chunkCache: chunkVectorCacheRef.current
|
|
32368
|
+
} : {
|
|
32132
32369
|
vaultCtx,
|
|
32133
32370
|
storageCtx,
|
|
32134
32371
|
embeddingOptions: vaultEmbeddingOptions,
|
|
@@ -32146,7 +32383,9 @@ function useChatStorage(options) {
|
|
|
32146
32383
|
getToken,
|
|
32147
32384
|
currentConversationId,
|
|
32148
32385
|
vaultEmbeddingOptions,
|
|
32149
|
-
vaultEmbeddingCache
|
|
32386
|
+
vaultEmbeddingCache,
|
|
32387
|
+
memoryStore,
|
|
32388
|
+
storeMaskMissing
|
|
32150
32389
|
]
|
|
32151
32390
|
);
|
|
32152
32391
|
const {
|