@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.cjs
CHANGED
|
@@ -5997,7 +5997,7 @@ function extractUserMessageFromMessages(messages) {
|
|
|
5997
5997
|
}
|
|
5998
5998
|
|
|
5999
5999
|
// src/lib/db/chat/operations.ts
|
|
6000
|
-
function messageToStoredRaw(message,
|
|
6000
|
+
function messageToStoredRaw(message, projection2) {
|
|
6001
6001
|
const convId = String(message._getRaw("conversation_id"));
|
|
6002
6002
|
const parseJsonField = (rawValue) => {
|
|
6003
6003
|
if (!rawValue) return void 0;
|
|
@@ -6011,7 +6011,7 @@ function messageToStoredRaw(message, projection) {
|
|
|
6011
6011
|
}
|
|
6012
6012
|
return rawValue;
|
|
6013
6013
|
};
|
|
6014
|
-
const skipEmbeddings =
|
|
6014
|
+
const skipEmbeddings = projection2?.skipEmbeddings === true;
|
|
6015
6015
|
const sourcesRaw = message._getRaw("sources");
|
|
6016
6016
|
const vectorRaw = skipEmbeddings ? null : message._getRaw("vector");
|
|
6017
6017
|
const chunksRaw = skipEmbeddings ? null : message._getRaw("chunks");
|
|
@@ -6045,8 +6045,8 @@ function messageToStoredRaw(message, projection) {
|
|
|
6045
6045
|
origin: message.origin
|
|
6046
6046
|
};
|
|
6047
6047
|
}
|
|
6048
|
-
async function messageToStored(message, walletAddress, signMessage, embeddedWalletSigner,
|
|
6049
|
-
const baseMessage = messageToStoredRaw(message,
|
|
6048
|
+
async function messageToStored(message, walletAddress, signMessage, embeddedWalletSigner, projection2) {
|
|
6049
|
+
const baseMessage = messageToStoredRaw(message, projection2);
|
|
6050
6050
|
if (walletAddress) {
|
|
6051
6051
|
return await decryptMessageFields(
|
|
6052
6052
|
baseMessage,
|
|
@@ -6060,7 +6060,7 @@ async function messageToStored(message, walletAddress, signMessage, embeddedWall
|
|
|
6060
6060
|
function rawDate(value) {
|
|
6061
6061
|
return new Date(typeof value === "number" ? value : 0);
|
|
6062
6062
|
}
|
|
6063
|
-
function messageRawToStoredRaw(raw,
|
|
6063
|
+
function messageRawToStoredRaw(raw, projection2) {
|
|
6064
6064
|
const parseJsonField = (rawValue) => {
|
|
6065
6065
|
if (!rawValue) return void 0;
|
|
6066
6066
|
if (typeof rawValue === "string") {
|
|
@@ -6073,7 +6073,7 @@ function messageRawToStoredRaw(raw, projection) {
|
|
|
6073
6073
|
}
|
|
6074
6074
|
return rawValue;
|
|
6075
6075
|
};
|
|
6076
|
-
const skipEmbeddings =
|
|
6076
|
+
const skipEmbeddings = projection2?.skipEmbeddings === true;
|
|
6077
6077
|
const vectorRaw = skipEmbeddings ? null : raw.vector;
|
|
6078
6078
|
const chunksRaw = skipEmbeddings ? null : raw.chunks;
|
|
6079
6079
|
return {
|
|
@@ -6104,8 +6104,8 @@ function messageRawToStoredRaw(raw, projection) {
|
|
|
6104
6104
|
origin: raw.origin
|
|
6105
6105
|
};
|
|
6106
6106
|
}
|
|
6107
|
-
async function messageRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner,
|
|
6108
|
-
const baseMessage = messageRawToStoredRaw(raw,
|
|
6107
|
+
async function messageRawToStored(raw, walletAddress, signMessage, embeddedWalletSigner, projection2) {
|
|
6108
|
+
const baseMessage = messageRawToStoredRaw(raw, projection2);
|
|
6109
6109
|
if (walletAddress) {
|
|
6110
6110
|
return await decryptMessageFields(
|
|
6111
6111
|
baseMessage,
|
|
@@ -15908,22 +15908,35 @@ async function vaultMemoryRawToStored(raw, walletAddress, signMessage, embeddedW
|
|
|
15908
15908
|
return stored2;
|
|
15909
15909
|
}
|
|
15910
15910
|
async function getAllVaultMemoriesOp(ctx, options) {
|
|
15911
|
-
const
|
|
15911
|
+
const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
|
|
15912
|
+
return mapInBatches(
|
|
15913
|
+
results,
|
|
15914
|
+
(raw) => vaultMemoryRawToStored(raw, ctx.walletAddress, ctx.signMessage, ctx.embeddedWalletSigner)
|
|
15915
|
+
);
|
|
15916
|
+
}
|
|
15917
|
+
function vaultListConditions(ctx, options) {
|
|
15918
|
+
return [
|
|
15912
15919
|
...baseVaultConditions(ctx, options),
|
|
15913
15920
|
...options?.scopes?.length ? [import_watermelondb23.Q.where("scope", import_watermelondb23.Q.oneOf(options.scopes))] : [],
|
|
15914
15921
|
...visibilityConditions(options?.visibility),
|
|
15915
15922
|
...levelConditions(options?.levels),
|
|
15916
15923
|
...options?.kinds?.length ? [import_watermelondb23.Q.where("kind", import_watermelondb23.Q.oneOf(options.kinds))] : [],
|
|
15917
15924
|
...options?.folderId !== void 0 ? [import_watermelondb23.Q.where("folder_id", options.folderId)] : [],
|
|
15918
|
-
...options?.factTypes?.length ? [import_watermelondb23.Q.where("fact_type", import_watermelondb23.Q.oneOf(options.factTypes))] : []
|
|
15925
|
+
...options?.factTypes?.length ? [import_watermelondb23.Q.where("fact_type", import_watermelondb23.Q.oneOf(options.factTypes))] : []
|
|
15926
|
+
];
|
|
15927
|
+
}
|
|
15928
|
+
function vaultListOrder(options) {
|
|
15929
|
+
return [
|
|
15919
15930
|
import_watermelondb23.Q.sortBy(options?.since ? "updated_at" : "created_at", import_watermelondb23.Q.desc),
|
|
15920
15931
|
...options?.limit !== null && options?.limit !== void 0 && options.limit > 0 ? [import_watermelondb23.Q.take(options.limit)] : []
|
|
15921
15932
|
];
|
|
15922
|
-
|
|
15923
|
-
|
|
15924
|
-
|
|
15925
|
-
|
|
15926
|
-
|
|
15933
|
+
}
|
|
15934
|
+
async function getVaultMemoryProjectionsOp(ctx, options) {
|
|
15935
|
+
const results = await ctx.vaultMemoryCollection.query(...vaultListConditions(ctx, options), ...vaultListOrder(options)).unsafeFetchRaw();
|
|
15936
|
+
return results.map((raw) => ({
|
|
15937
|
+
...vaultMemoryRawToRankable(raw),
|
|
15938
|
+
topicsUserManaged: raw.topics_user_managed === true || raw.topics_user_managed === 1
|
|
15939
|
+
}));
|
|
15927
15940
|
}
|
|
15928
15941
|
function vaultMemoryRawToRankable(raw) {
|
|
15929
15942
|
return {
|
|
@@ -22063,7 +22076,6 @@ async function recall(query, ctx, options = {}) {
|
|
|
22063
22076
|
}
|
|
22064
22077
|
const needsChunkEmbedding = types.includes("chunk") && ctx.storageCtx;
|
|
22065
22078
|
const wantsTemporal = types.includes("fact") && (ctx.factSource || ctx.vaultCtx);
|
|
22066
|
-
const graphRefiner = flags.traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
|
|
22067
22079
|
const queryEmbedTotalTimeoutMs = options.queryEmbedTotalTimeoutMs ?? DEFAULT_QUERY_EMBED_TOTAL_TIMEOUT_MS;
|
|
22068
22080
|
const prepStart = nowMs2();
|
|
22069
22081
|
let sharedEmbedMs = 0;
|
|
@@ -22092,13 +22104,12 @@ async function recall(query, ctx, options = {}) {
|
|
|
22092
22104
|
safeLane(
|
|
22093
22105
|
"graph",
|
|
22094
22106
|
() => graphLaneFailed = true,
|
|
22095
|
-
() => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
|
|
22096
|
-
|
|
22097
|
-
|
|
22098
|
-
|
|
22099
|
-
|
|
22100
|
-
|
|
22101
|
-
})
|
|
22107
|
+
() => ctx.factSource ? ctx.factSource.graphRanking(query, flags.traverse, options) : buildGraphLaneRanking(
|
|
22108
|
+
query,
|
|
22109
|
+
ctx,
|
|
22110
|
+
flags.traverse,
|
|
22111
|
+
graphTraversalOptions(options, flags.traverse)
|
|
22112
|
+
)
|
|
22102
22113
|
),
|
|
22103
22114
|
wantsTemporal ? safeLane(
|
|
22104
22115
|
"temporal",
|
|
@@ -22317,6 +22328,16 @@ function toFactMemory(r, now) {
|
|
|
22317
22328
|
updatedAt: r.updatedAt ?? /* @__PURE__ */ new Date(0)
|
|
22318
22329
|
};
|
|
22319
22330
|
}
|
|
22331
|
+
function graphTraversalOptions(options, traverse) {
|
|
22332
|
+
const refineNeighbors = traverse && options.graphRefine && options.decomposeOptions ? createLlmNeighborRefiner(options.decomposeOptions) : void 0;
|
|
22333
|
+
return {
|
|
22334
|
+
...options.maxHops !== void 0 && { maxHops: options.maxHops },
|
|
22335
|
+
...options.entityFanout !== void 0 && { entityFanout: options.entityFanout },
|
|
22336
|
+
...options.nodeBudget !== void 0 && { nodeBudget: options.nodeBudget },
|
|
22337
|
+
...options.rrfK !== void 0 && { rrfK: options.rrfK },
|
|
22338
|
+
...refineNeighbors && { refineNeighbors }
|
|
22339
|
+
};
|
|
22340
|
+
}
|
|
22320
22341
|
async function buildGraphLaneRanking(query, ctx, traverse = false, traversalOptions = {}) {
|
|
22321
22342
|
const entityCtx = ctx.entityCtx ?? ctx.vaultCtx?.entityCtx;
|
|
22322
22343
|
if (!entityCtx) return [];
|
|
@@ -22482,24 +22503,33 @@ function isDumpQuery(query) {
|
|
|
22482
22503
|
return false;
|
|
22483
22504
|
}
|
|
22484
22505
|
async function listRecentFacts(ctx, limit, toolOptions) {
|
|
22506
|
+
const store = toolOptions?.memoryStore;
|
|
22485
22507
|
const vaultCtx = ctx.vaultCtx;
|
|
22486
|
-
if (!vaultCtx) return [];
|
|
22487
|
-
const
|
|
22508
|
+
if (!vaultCtx && !store) return [];
|
|
22509
|
+
const read = (ids) => store ? store.list({ memoryIds: ids }) : getVaultMemoriesByIdsOp(vaultCtx, ids);
|
|
22510
|
+
const filters = {
|
|
22488
22511
|
...toolOptions?.scopes && { scopes: toolOptions.scopes },
|
|
22489
|
-
...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
|
|
22512
|
+
...toolOptions?.memoryIds !== void 0 && { memoryIds: toolOptions.memoryIds }
|
|
22513
|
+
};
|
|
22514
|
+
const project = (size) => store ? store.listProjections({ ...filters, limit: size }) : getVaultRankingProjectionsOp(vaultCtx, {
|
|
22515
|
+
...filters,
|
|
22490
22516
|
...toolOptions?.folderId !== void 0 && { folderId: toolOptions.folderId }
|
|
22491
22517
|
});
|
|
22492
22518
|
const readable = [];
|
|
22493
|
-
|
|
22494
|
-
|
|
22495
|
-
const
|
|
22496
|
-
|
|
22497
|
-
|
|
22498
|
-
|
|
22499
|
-
const
|
|
22500
|
-
|
|
22501
|
-
|
|
22519
|
+
let start = 0;
|
|
22520
|
+
for (let size = limit; readable.length < limit; size *= 2) {
|
|
22521
|
+
const ordered = await project(size);
|
|
22522
|
+
for (let i = start; i < ordered.length && readable.length < limit; i += limit) {
|
|
22523
|
+
const batch = ordered.slice(i, i + limit).map((p) => p.uniqueId);
|
|
22524
|
+
const byId = new Map((await read(batch)).map((m) => [m.uniqueId, m]));
|
|
22525
|
+
for (const id of batch) {
|
|
22526
|
+
const m = byId.get(id);
|
|
22527
|
+
if (m && !isEncrypted(m.content)) readable.push(m);
|
|
22528
|
+
if (readable.length >= limit) break;
|
|
22529
|
+
}
|
|
22502
22530
|
}
|
|
22531
|
+
if (!store || ordered.length < size) break;
|
|
22532
|
+
start = ordered.length;
|
|
22503
22533
|
}
|
|
22504
22534
|
return readable.map((m) => ({
|
|
22505
22535
|
id: m.uniqueId,
|
|
@@ -22516,6 +22546,8 @@ async function listRecentFacts(ctx, limit, toolOptions) {
|
|
|
22516
22546
|
}));
|
|
22517
22547
|
}
|
|
22518
22548
|
function createRecallTool(ctx, toolOptions, callbacks) {
|
|
22549
|
+
if (toolOptions?.memoryStore && toolOptions.folderId !== void 0)
|
|
22550
|
+
throw new Error("Folders are not supported with a memoryStore");
|
|
22519
22551
|
const defaultTypes = toolOptions?.types ?? ["fact", "chunk"];
|
|
22520
22552
|
const defaultLimit = toolOptions?.limit ?? DEFAULT_LIMIT2;
|
|
22521
22553
|
const defaultBudget = toolOptions?.budget ?? DEFAULT_BUDGET2;
|
|
@@ -22587,7 +22619,7 @@ function createRecallTool(ctx, toolOptions, callbacks) {
|
|
|
22587
22619
|
let surfaced = 0;
|
|
22588
22620
|
try {
|
|
22589
22621
|
let recallDegraded = [];
|
|
22590
|
-
const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && ctx.vaultCtx !== void 0;
|
|
22622
|
+
const wantsRecent = args.sort === "recent" && defaultTypes.includes("fact") && (ctx.vaultCtx !== void 0 || toolOptions?.memoryStore !== void 0);
|
|
22591
22623
|
let result;
|
|
22592
22624
|
if (wantsRecent) {
|
|
22593
22625
|
result = { memories: await listRecentFacts(ctx, effectiveLimit, toolOptions) };
|
|
@@ -23064,6 +23096,7 @@ function createLocalMemoryStore(options) {
|
|
|
23064
23096
|
};
|
|
23065
23097
|
const vaultCache = options.vaultCache ?? createVaultEmbeddingCache();
|
|
23066
23098
|
const ownedBy = userId !== void 0 ? [import_watermelondb32.Q.where("user_id", userId)] : [];
|
|
23099
|
+
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [import_watermelondb32.Q.or(import_watermelondb32.Q.where("user_id", userId), import_watermelondb32.Q.where("user_id", null))] : [import_watermelondb32.Q.where("user_id", userId)];
|
|
23067
23100
|
const watchedColumns = [
|
|
23068
23101
|
"content",
|
|
23069
23102
|
"scope",
|
|
@@ -23096,6 +23129,7 @@ function createLocalMemoryStore(options) {
|
|
|
23096
23129
|
});
|
|
23097
23130
|
};
|
|
23098
23131
|
return {
|
|
23132
|
+
masksEmbeddingInput: options.embeddingOptions.maskInput !== void 0,
|
|
23099
23133
|
list: (listOptions) => getAllVaultMemoriesOp(vaultCtx, listOptions),
|
|
23100
23134
|
get: (id) => getVaultMemoryOp(vaultCtx, id),
|
|
23101
23135
|
listArchived: async () => {
|
|
@@ -23106,6 +23140,29 @@ function createLocalMemoryStore(options) {
|
|
|
23106
23140
|
},
|
|
23107
23141
|
memoriesByTopics: (names) => getMemoriesByEntityNamesOp(entityCtx, names),
|
|
23108
23142
|
topicsByMemories: (memoryIds) => getEntitiesByMemoryIdsOp(entityCtx, memoryIds),
|
|
23143
|
+
listTopics: async () => {
|
|
23144
|
+
const links = await entityCtx.memoryEntityCollection.query(...linksOwnedBy).fetch();
|
|
23145
|
+
const members = /* @__PURE__ */ new Map();
|
|
23146
|
+
for (const link of links) {
|
|
23147
|
+
const entityId = String(link.entityId);
|
|
23148
|
+
const set = members.get(entityId) ?? /* @__PURE__ */ new Set();
|
|
23149
|
+
set.add(String(link.memoryId));
|
|
23150
|
+
members.set(entityId, set);
|
|
23151
|
+
}
|
|
23152
|
+
const entities = userId === void 0 ? await entityCtx.entityCollection.query().fetch() : members.size ? await entityCtx.entityCollection.query(import_watermelondb32.Q.where("id", import_watermelondb32.Q.oneOf([...members.keys()]))).fetch() : [];
|
|
23153
|
+
const byName = /* @__PURE__ */ new Map();
|
|
23154
|
+
for (const entity of entities) {
|
|
23155
|
+
const count2 = members.get(entity.id)?.size ?? 0;
|
|
23156
|
+
const existing = byName.get(entity.canonicalName);
|
|
23157
|
+
byName.set(entity.canonicalName, {
|
|
23158
|
+
name: entity.canonicalName,
|
|
23159
|
+
kind: existing?.kind ?? entity.kind ?? null,
|
|
23160
|
+
memoryCount: (existing?.memoryCount ?? 0) + count2
|
|
23161
|
+
});
|
|
23162
|
+
}
|
|
23163
|
+
return [...byName.values()].sort((a, b) => a.name.localeCompare(b.name));
|
|
23164
|
+
},
|
|
23165
|
+
listProjections: (listOptions) => getVaultMemoryProjectionsOp(vaultCtx, listOptions),
|
|
23109
23166
|
create: async (input) => {
|
|
23110
23167
|
const created = await createVaultMemoryOp(vaultCtx, input);
|
|
23111
23168
|
if (input.embedding === void 0) embedInBackground(created);
|
|
@@ -23154,6 +23211,22 @@ function createLocalMemoryStore(options) {
|
|
|
23154
23211
|
{ vaultCtx, embeddingOptions: options.embeddingOptions, vaultCache },
|
|
23155
23212
|
retainOptions
|
|
23156
23213
|
),
|
|
23214
|
+
factSource: {
|
|
23215
|
+
search: (query, searchOptions) => searchVaultMemoriesWithSize(
|
|
23216
|
+
query,
|
|
23217
|
+
vaultCtx,
|
|
23218
|
+
options.embeddingOptions,
|
|
23219
|
+
vaultCache,
|
|
23220
|
+
searchOptions
|
|
23221
|
+
),
|
|
23222
|
+
graphRanking: (query, traverse, recallOptions) => buildGraphLaneRanking(
|
|
23223
|
+
query,
|
|
23224
|
+
{ vaultCtx, entityCtx, embeddingOptions: options.embeddingOptions },
|
|
23225
|
+
traverse,
|
|
23226
|
+
graphTraversalOptions(recallOptions, traverse)
|
|
23227
|
+
),
|
|
23228
|
+
temporalRanking: (query, now) => buildTemporalLaneRanking(query, vaultCtx, now)
|
|
23229
|
+
},
|
|
23157
23230
|
subscribe: (onChange, subscribeOptions) => {
|
|
23158
23231
|
const afterFirst = () => {
|
|
23159
23232
|
let first = true;
|
|
@@ -23167,7 +23240,6 @@ function createLocalMemoryStore(options) {
|
|
|
23167
23240
|
);
|
|
23168
23241
|
const subscriptions = [memories.subscribe(afterFirst())];
|
|
23169
23242
|
if (subscribeOptions?.topics) {
|
|
23170
|
-
const linksOwnedBy = userId === void 0 ? [] : options.allowUnscopedRows ? [import_watermelondb32.Q.or(import_watermelondb32.Q.where("user_id", userId), import_watermelondb32.Q.where("user_id", null))] : [import_watermelondb32.Q.where("user_id", userId)];
|
|
23171
23243
|
subscriptions.push(
|
|
23172
23244
|
entityCtx.memoryEntityCollection.query(...linksOwnedBy).observe().subscribe(afterFirst())
|
|
23173
23245
|
);
|
|
@@ -23577,6 +23649,31 @@ async function createRemoteMemoryPersistence(options) {
|
|
|
23577
23649
|
throw new Error("Invalid nearby candidate response");
|
|
23578
23650
|
return result;
|
|
23579
23651
|
};
|
|
23652
|
+
const fetchQueryPage = async (queryOptions) => {
|
|
23653
|
+
const { signal, ...query } = queryOptions;
|
|
23654
|
+
if (query.limit !== void 0 && (!Number.isInteger(query.limit) || query.limit < 1 || query.limit > 200))
|
|
23655
|
+
throw new Error("Page limit must be 1\u2013200");
|
|
23656
|
+
for (const [key, max] of queryListLimits) {
|
|
23657
|
+
const values = query[key];
|
|
23658
|
+
if (values && values.length > max)
|
|
23659
|
+
throw new Error(`Query ${key} accepts at most ${max} entries (got ${values.length})`);
|
|
23660
|
+
}
|
|
23661
|
+
const page = await request(
|
|
23662
|
+
"/memories/query",
|
|
23663
|
+
{
|
|
23664
|
+
method: "POST",
|
|
23665
|
+
body: JSON.stringify(
|
|
23666
|
+
Object.fromEntries(
|
|
23667
|
+
queryFields.filter((key) => query[key] !== void 0).map((key) => [key, query[key]])
|
|
23668
|
+
)
|
|
23669
|
+
)
|
|
23670
|
+
},
|
|
23671
|
+
signal
|
|
23672
|
+
);
|
|
23673
|
+
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")
|
|
23674
|
+
throw new Error("Invalid nearby private-memory page");
|
|
23675
|
+
return page;
|
|
23676
|
+
};
|
|
23580
23677
|
const candidateSet = async (embedding, candidateOptions = {}) => {
|
|
23581
23678
|
const result = await fetchCandidates(embedding, candidateOptions, true);
|
|
23582
23679
|
if (!Number.isSafeInteger(result.total_count) || result.total_count < 0 || !Number.isSafeInteger(result.unavailable_count) || result.unavailable_count < 0)
|
|
@@ -23655,34 +23752,27 @@ async function createRemoteMemoryPersistence(options) {
|
|
|
23655
23752
|
return Promise.all(result.items.map((item, i) => decodeCommitted(item, prepared[i])));
|
|
23656
23753
|
},
|
|
23657
23754
|
query: async (queryOptions = {}) => {
|
|
23658
|
-
const
|
|
23659
|
-
if (query.limit !== void 0 && (!Number.isInteger(query.limit) || query.limit < 1 || query.limit > 200))
|
|
23660
|
-
throw new Error("Page limit must be 1\u2013200");
|
|
23661
|
-
for (const [key, max] of queryListLimits) {
|
|
23662
|
-
const values = query[key];
|
|
23663
|
-
if (values && values.length > max)
|
|
23664
|
-
throw new Error(`Query ${key} accepts at most ${max} entries (got ${values.length})`);
|
|
23665
|
-
}
|
|
23666
|
-
const page = await request(
|
|
23667
|
-
"/memories/query",
|
|
23668
|
-
{
|
|
23669
|
-
method: "POST",
|
|
23670
|
-
body: JSON.stringify(
|
|
23671
|
-
Object.fromEntries(
|
|
23672
|
-
queryFields.filter((key) => query[key] !== void 0).map((key) => [key, query[key]])
|
|
23673
|
-
)
|
|
23674
|
-
)
|
|
23675
|
-
},
|
|
23676
|
-
signal
|
|
23677
|
-
);
|
|
23678
|
-
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")
|
|
23679
|
-
throw new Error("Invalid nearby private-memory page");
|
|
23755
|
+
const page = await fetchQueryPage(queryOptions);
|
|
23680
23756
|
return {
|
|
23681
23757
|
...await decodeAll(page.items),
|
|
23682
23758
|
...page.next_cursor !== void 0 && { next_cursor: page.next_cursor },
|
|
23683
23759
|
...page.changes_cursor !== void 0 && { changes_cursor: page.changes_cursor }
|
|
23684
23760
|
};
|
|
23685
23761
|
},
|
|
23762
|
+
queryMetadata: async (queryOptions = {}) => {
|
|
23763
|
+
const page = await fetchQueryPage(queryOptions);
|
|
23764
|
+
return {
|
|
23765
|
+
items: page.items.map((item) => {
|
|
23766
|
+
const {
|
|
23767
|
+
memory: { content: _content, kind_value: _kindValue, ...memory },
|
|
23768
|
+
version,
|
|
23769
|
+
server_updated_at
|
|
23770
|
+
} = memoryRecord(item);
|
|
23771
|
+
return { memory, version, server_updated_at };
|
|
23772
|
+
}),
|
|
23773
|
+
...page.next_cursor !== void 0 && { next_cursor: page.next_cursor }
|
|
23774
|
+
};
|
|
23775
|
+
},
|
|
23686
23776
|
candidateSet,
|
|
23687
23777
|
candidates: async (embedding, candidateOptions = {}) => decodeAll((await fetchCandidates(embedding, candidateOptions, false)).items)
|
|
23688
23778
|
};
|
|
@@ -24024,38 +24114,31 @@ function createRemoteMemoryPipeline(options) {
|
|
|
24024
24114
|
};
|
|
24025
24115
|
return { prepare, port };
|
|
24026
24116
|
};
|
|
24027
|
-
|
|
24028
|
-
|
|
24117
|
+
const factSource = {
|
|
24118
|
+
graphRanking: options.graphRanking,
|
|
24119
|
+
temporalRanking: options.temporalRanking,
|
|
24120
|
+
search: async (text13, searchOptions) => {
|
|
24029
24121
|
const { prepare } = operation("recall");
|
|
24030
|
-
|
|
24031
|
-
|
|
24032
|
-
|
|
24033
|
-
|
|
24034
|
-
|
|
24035
|
-
|
|
24036
|
-
temporalRanking: options.temporalRanking,
|
|
24037
|
-
search: async (text13, searchOptions) => {
|
|
24038
|
-
const prepared = await prepare(text13, searchOptions);
|
|
24039
|
-
const ranked = await rankPreparedVaultCandidates(
|
|
24040
|
-
text13,
|
|
24041
|
-
prepared,
|
|
24042
|
-
embeddingOptions,
|
|
24043
|
-
searchOptions
|
|
24044
|
-
);
|
|
24045
|
-
return {
|
|
24046
|
-
...ranked,
|
|
24047
|
-
rankedOnCosine: prepared.memories.length > 0 && !prepared.embeddingsUnavailable,
|
|
24048
|
-
decryptLast: true,
|
|
24049
|
-
rowsDecrypted: prepared.rowsDecrypted,
|
|
24050
|
-
queryEmbedMs: prepared.queryEmbedMs,
|
|
24051
|
-
rowsEmbedded: 0
|
|
24052
|
-
};
|
|
24053
|
-
}
|
|
24054
|
-
}
|
|
24055
|
-
},
|
|
24056
|
-
{ ...recallOptions, types: ["fact"] }
|
|
24122
|
+
const prepared = await prepare(text13, searchOptions);
|
|
24123
|
+
const ranked = await rankPreparedVaultCandidates(
|
|
24124
|
+
text13,
|
|
24125
|
+
prepared,
|
|
24126
|
+
embeddingOptions,
|
|
24127
|
+
searchOptions
|
|
24057
24128
|
);
|
|
24058
|
-
|
|
24129
|
+
return {
|
|
24130
|
+
...ranked,
|
|
24131
|
+
rankedOnCosine: prepared.memories.length > 0 && !prepared.embeddingsUnavailable,
|
|
24132
|
+
decryptLast: true,
|
|
24133
|
+
rowsDecrypted: prepared.rowsDecrypted,
|
|
24134
|
+
queryEmbedMs: prepared.queryEmbedMs,
|
|
24135
|
+
rowsEmbedded: 0
|
|
24136
|
+
};
|
|
24137
|
+
}
|
|
24138
|
+
};
|
|
24139
|
+
return {
|
|
24140
|
+
factSource,
|
|
24141
|
+
recall: async (query, recallOptions = {}) => recall(query, { embeddingOptions, factSource }, { ...recallOptions, types: ["fact"] }),
|
|
24059
24142
|
retain: async (content, retainOptions) => {
|
|
24060
24143
|
const { port } = operation("retain");
|
|
24061
24144
|
return retainWithPersistence(
|
|
@@ -24075,6 +24158,19 @@ function createRemoteMemoryPipeline(options) {
|
|
|
24075
24158
|
// src/lib/memory/store/remoteStore.ts
|
|
24076
24159
|
var import_uuid6 = require("uuid");
|
|
24077
24160
|
init_logger();
|
|
24161
|
+
function projection({ memory }) {
|
|
24162
|
+
return {
|
|
24163
|
+
uniqueId: memory.memory_id,
|
|
24164
|
+
scope: memory.scope,
|
|
24165
|
+
folderId: memory.folder_id ?? null,
|
|
24166
|
+
embedding: memory.embedding?.length ? JSON.stringify(memory.embedding) : null,
|
|
24167
|
+
embeddingModel: memory.embedding_model ?? null,
|
|
24168
|
+
createdAt: new Date(memory.created_at),
|
|
24169
|
+
updatedAt: new Date(memory.updated_at),
|
|
24170
|
+
lastObservedAt: memory.last_observed_at ?? null,
|
|
24171
|
+
topicsUserManaged: memory.topics_user_managed ?? false
|
|
24172
|
+
};
|
|
24173
|
+
}
|
|
24078
24174
|
function relevant(change, options) {
|
|
24079
24175
|
return options.includeDeleted ? change === "membership" : change !== "embedding" || !!options.embeddings;
|
|
24080
24176
|
}
|
|
@@ -24109,6 +24205,9 @@ function bounded(filters, key) {
|
|
|
24109
24205
|
if (!values || values.length <= MAX_QUERY_FILTER) return [filters];
|
|
24110
24206
|
return chunks(values, MAX_QUERY_FILTER).map((chunk) => ({ ...filters, [key]: chunk }));
|
|
24111
24207
|
}
|
|
24208
|
+
function maxOf(options) {
|
|
24209
|
+
return options.limit && options.limit > 0 ? options.limit : Infinity;
|
|
24210
|
+
}
|
|
24112
24211
|
function chunks(values, size) {
|
|
24113
24212
|
const out = [];
|
|
24114
24213
|
for (let i = 0; i < values.length; i += size) out.push(values.slice(i, i + size));
|
|
@@ -24180,26 +24279,49 @@ function createRemoteMemoryStore(options) {
|
|
|
24180
24279
|
for (const subscriber of [...subscribers])
|
|
24181
24280
|
if (relevant(change, subscriber.options)) safely(subscriber.onChange);
|
|
24182
24281
|
};
|
|
24183
|
-
const
|
|
24282
|
+
const pages = async (read, filters, max = Infinity) => {
|
|
24184
24283
|
const records = [];
|
|
24185
24284
|
let cursor2;
|
|
24186
24285
|
do {
|
|
24187
|
-
const page = await
|
|
24286
|
+
const page = await read({
|
|
24188
24287
|
...filters,
|
|
24189
24288
|
limit: Math.min(200, Math.max(1, max - records.length)),
|
|
24190
24289
|
...cursor2 && { cursor: cursor2 }
|
|
24191
24290
|
});
|
|
24192
|
-
warnFailed(page.failed);
|
|
24291
|
+
if (page.failed) warnFailed(page.failed);
|
|
24193
24292
|
records.push(...page.items);
|
|
24194
24293
|
cursor2 = page.next_cursor;
|
|
24195
24294
|
} while (cursor2 && records.length < max);
|
|
24196
24295
|
return records.slice(0, max);
|
|
24197
24296
|
};
|
|
24198
|
-
const
|
|
24297
|
+
const decrypted = (options2) => persistence.query(options2);
|
|
24298
|
+
const metadata = (options2) => persistence.queryMetadata(options2);
|
|
24299
|
+
const enumerate = (filters, max = Infinity) => pages(decrypted, filters, max);
|
|
24300
|
+
const enumerateIds = async (read, ids, filters) => (await Promise.all(
|
|
24199
24301
|
chunks([...new Set(ids)], MAX_QUERY_IDS).map(
|
|
24200
|
-
(memory_ids) =>
|
|
24302
|
+
(memory_ids) => pages(read, { ...filters, memory_ids })
|
|
24201
24303
|
)
|
|
24202
24304
|
)).flat();
|
|
24305
|
+
const listFilters = (listOptions) => ({
|
|
24306
|
+
...stateFilters(listOptions),
|
|
24307
|
+
include_embeddings: true,
|
|
24308
|
+
order: listOptions.since ? "updated" : "created",
|
|
24309
|
+
...listOptions.scopes?.length && { scopes: listOptions.scopes },
|
|
24310
|
+
...listOptions.factTypes?.length && { fact_types: listOptions.factTypes },
|
|
24311
|
+
...listOptions.visibility?.length && { visibility: listOptions.visibility },
|
|
24312
|
+
...listOptions.since && { updated_after: listOptions.since.getTime() }
|
|
24313
|
+
});
|
|
24314
|
+
const listRows = async (read, listOptions, filters, max) => {
|
|
24315
|
+
const variants = bounded(filters, "scopes").flatMap((f) => bounded(f, "fact_types"));
|
|
24316
|
+
if (!listOptions.memoryIds && variants.length === 1) return pages(read, filters, max);
|
|
24317
|
+
const fetched = await Promise.all(
|
|
24318
|
+
variants.map(
|
|
24319
|
+
(variant) => listOptions.memoryIds ? enumerateIds(read, listOptions.memoryIds, variant) : pages(read, variant, max)
|
|
24320
|
+
)
|
|
24321
|
+
);
|
|
24322
|
+
const time = (row) => filters.order === "updated" ? row.memory.updated_at : row.memory.created_at;
|
|
24323
|
+
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);
|
|
24324
|
+
};
|
|
24203
24325
|
const queues = /* @__PURE__ */ new Map();
|
|
24204
24326
|
const serialized = (id, task) => {
|
|
24205
24327
|
const run = (queues.get(id) ?? Promise.resolve()).then(task, task);
|
|
@@ -24241,12 +24363,15 @@ function createRemoteMemoryStore(options) {
|
|
|
24241
24363
|
};
|
|
24242
24364
|
const newRow = (input) => {
|
|
24243
24365
|
const now = Date.now();
|
|
24244
|
-
const scope = input
|
|
24366
|
+
const { level, scope } = resolveCreateLevel(input);
|
|
24367
|
+
assertValidKindLevel(input.kind, level);
|
|
24245
24368
|
return {
|
|
24246
24369
|
memory_id: (0, import_uuid6.v7)(),
|
|
24247
24370
|
content: input.content,
|
|
24248
24371
|
scope,
|
|
24249
|
-
level
|
|
24372
|
+
level,
|
|
24373
|
+
...input.kind !== void 0 && input.kind !== null && { kind: input.kind },
|
|
24374
|
+
...typeof input.kindValue === "string" && { kind_value: input.kindValue },
|
|
24250
24375
|
created_at: now,
|
|
24251
24376
|
updated_at: now,
|
|
24252
24377
|
is_deleted: false,
|
|
@@ -24275,6 +24400,30 @@ function createRemoteMemoryStore(options) {
|
|
|
24275
24400
|
return records.map((record2) => stored(record2.memory));
|
|
24276
24401
|
};
|
|
24277
24402
|
const asStored = (record2) => record2 ? stored(record2.memory) : null;
|
|
24403
|
+
const withKnownKinds = async (topics) => {
|
|
24404
|
+
const unkinded = [
|
|
24405
|
+
...new Set(topics.filter((t) => !t.kind).map((t) => normalizeEntityName(t.name)))
|
|
24406
|
+
];
|
|
24407
|
+
if (unkinded.length === 0) return topics;
|
|
24408
|
+
const kinds = /* @__PURE__ */ new Map();
|
|
24409
|
+
for (const names of chunks(unkinded, MAX_QUERY_TOPICS)) {
|
|
24410
|
+
for (const { memory } of await pages(metadata, {
|
|
24411
|
+
topics: names,
|
|
24412
|
+
include_archived: true,
|
|
24413
|
+
include_quarantined: true,
|
|
24414
|
+
include_superseded: true
|
|
24415
|
+
})) {
|
|
24416
|
+
for (const topic of parseTopics(memory.topics) ?? []) {
|
|
24417
|
+
const name = normalizeEntityName(topic.name);
|
|
24418
|
+
if (topic.kind && !kinds.has(name)) kinds.set(name, topic.kind);
|
|
24419
|
+
}
|
|
24420
|
+
}
|
|
24421
|
+
}
|
|
24422
|
+
return topics.map((topic) => {
|
|
24423
|
+
const kind = topic.kind ?? kinds.get(normalizeEntityName(topic.name));
|
|
24424
|
+
return kind ? { ...topic, kind } : topic;
|
|
24425
|
+
});
|
|
24426
|
+
};
|
|
24278
24427
|
const everyState = {
|
|
24279
24428
|
include_deleted: true,
|
|
24280
24429
|
include_archived: true,
|
|
@@ -24351,30 +24500,35 @@ function createRemoteMemoryStore(options) {
|
|
|
24351
24500
|
});
|
|
24352
24501
|
};
|
|
24353
24502
|
return {
|
|
24354
|
-
|
|
24355
|
-
|
|
24356
|
-
|
|
24357
|
-
|
|
24358
|
-
|
|
24359
|
-
|
|
24360
|
-
|
|
24361
|
-
|
|
24362
|
-
|
|
24363
|
-
|
|
24364
|
-
|
|
24365
|
-
|
|
24366
|
-
|
|
24367
|
-
|
|
24368
|
-
|
|
24369
|
-
|
|
24370
|
-
|
|
24371
|
-
|
|
24372
|
-
|
|
24373
|
-
|
|
24374
|
-
|
|
24375
|
-
|
|
24376
|
-
|
|
24377
|
-
|
|
24503
|
+
masksEmbeddingInput: embeddingOptions.maskInput !== void 0,
|
|
24504
|
+
list: async (listOptions = {}) => (await listRows(decrypted, listOptions, listFilters(listOptions), maxOf(listOptions))).map(
|
|
24505
|
+
(r) => stored(r.memory)
|
|
24506
|
+
),
|
|
24507
|
+
listProjections: async (listOptions = {}) => (await listRows(metadata, listOptions, listFilters(listOptions), maxOf(listOptions))).map(
|
|
24508
|
+
projection
|
|
24509
|
+
),
|
|
24510
|
+
listTopics: async () => {
|
|
24511
|
+
const topics = /* @__PURE__ */ new Map();
|
|
24512
|
+
for (const { memory } of await pages(metadata, {
|
|
24513
|
+
include_archived: true,
|
|
24514
|
+
include_quarantined: true,
|
|
24515
|
+
include_superseded: true
|
|
24516
|
+
})) {
|
|
24517
|
+
for (const topic of parseTopics(memory.topics) ?? []) {
|
|
24518
|
+
const name = normalizeEntityName(topic.name);
|
|
24519
|
+
if (!name) continue;
|
|
24520
|
+
const entry = topics.get(name) ?? {
|
|
24521
|
+
name,
|
|
24522
|
+
kind: null,
|
|
24523
|
+
memoryCount: 0,
|
|
24524
|
+
members: /* @__PURE__ */ new Set()
|
|
24525
|
+
};
|
|
24526
|
+
entry.kind ?? (entry.kind = topic.kind ?? null);
|
|
24527
|
+
entry.members.add(memory.memory_id);
|
|
24528
|
+
topics.set(name, entry);
|
|
24529
|
+
}
|
|
24530
|
+
}
|
|
24531
|
+
return [...topics.values()].map(({ members, ...topic }) => ({ ...topic, memoryCount: members.size })).sort((a, b) => a.name.localeCompare(b.name));
|
|
24378
24532
|
},
|
|
24379
24533
|
get: async (id) => asStored(await live(id)),
|
|
24380
24534
|
listArchived: async () => (await enumerate({ archived_only: true, order: "archived", include_embeddings: true })).map(
|
|
@@ -24383,10 +24537,10 @@ function createRemoteMemoryStore(options) {
|
|
|
24383
24537
|
memoriesByTopics: async (names) => {
|
|
24384
24538
|
const wanted = new Set(names.map(normalizeEntityName).filter(Boolean));
|
|
24385
24539
|
const out = /* @__PURE__ */ new Map();
|
|
24386
|
-
const
|
|
24540
|
+
const pages2 = await Promise.all(
|
|
24387
24541
|
chunks([...wanted], MAX_QUERY_TOPICS).map((topics) => enumerate({ topics }))
|
|
24388
24542
|
);
|
|
24389
|
-
for (const { memory } of
|
|
24543
|
+
for (const { memory } of pages2.flat()) {
|
|
24390
24544
|
const matched = [...canonicalTopics(memory)].filter((name) => wanted.has(name));
|
|
24391
24545
|
if (matched.length) out.set(memory.memory_id, new Set(matched));
|
|
24392
24546
|
}
|
|
@@ -24394,7 +24548,7 @@ function createRemoteMemoryStore(options) {
|
|
|
24394
24548
|
},
|
|
24395
24549
|
topicsByMemories: async (memoryIds) => {
|
|
24396
24550
|
const out = /* @__PURE__ */ new Map();
|
|
24397
|
-
for (const { memory } of await enumerateIds(memoryIds, {
|
|
24551
|
+
for (const { memory } of await enumerateIds(decrypted, memoryIds, {
|
|
24398
24552
|
include_archived: true,
|
|
24399
24553
|
include_superseded: true,
|
|
24400
24554
|
include_quarantined: true
|
|
@@ -24427,15 +24581,26 @@ function createRemoteMemoryStore(options) {
|
|
|
24427
24581
|
update: async (id, patch) => {
|
|
24428
24582
|
const saved = await mutate(id, "edit", (memory) => {
|
|
24429
24583
|
if (memory.superseded_by) return null;
|
|
24584
|
+
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);
|
|
24585
|
+
const kind = patch.kind !== void 0 ? patch.kind : memory.kind;
|
|
24586
|
+
assertValidKindLevel(kind, level);
|
|
24430
24587
|
memory.content = patch.content;
|
|
24431
24588
|
memory.updated_at = Date.now();
|
|
24432
|
-
if (patch.
|
|
24433
|
-
memory.level =
|
|
24434
|
-
|
|
24435
|
-
|
|
24436
|
-
|
|
24589
|
+
if (patch.level !== void 0) {
|
|
24590
|
+
memory.level = level;
|
|
24591
|
+
memory.scope = scopeForLevel(level);
|
|
24592
|
+
} else if (patch.scope !== void 0) {
|
|
24593
|
+
memory.level = level;
|
|
24437
24594
|
memory.scope = patch.scope;
|
|
24438
24595
|
}
|
|
24596
|
+
if (patch.kind !== void 0) {
|
|
24597
|
+
if (patch.kind === null) delete memory.kind;
|
|
24598
|
+
else memory.kind = patch.kind;
|
|
24599
|
+
}
|
|
24600
|
+
if (patch.kindValue !== void 0) {
|
|
24601
|
+
if (patch.kindValue === null) delete memory.kind_value;
|
|
24602
|
+
else memory.kind_value = patch.kindValue;
|
|
24603
|
+
}
|
|
24439
24604
|
if (patch.factType !== void 0) memory.fact_type = patch.factType ?? void 0;
|
|
24440
24605
|
if (patch.eventTime !== void 0) {
|
|
24441
24606
|
memory.event_time_start = patch.eventTime?.start ?? void 0;
|
|
@@ -24490,16 +24655,19 @@ function createRemoteMemoryStore(options) {
|
|
|
24490
24655
|
notify("edit", [await persistence.put(memory, record2)]);
|
|
24491
24656
|
return true;
|
|
24492
24657
|
}),
|
|
24493
|
-
setTopics: async (memoryId, topics) =>
|
|
24494
|
-
await
|
|
24495
|
-
|
|
24496
|
-
|
|
24497
|
-
|
|
24498
|
-
|
|
24499
|
-
|
|
24500
|
-
|
|
24658
|
+
setTopics: async (memoryId, topics) => {
|
|
24659
|
+
const chosen = await withKnownKinds(topicInputs(topics, "user"));
|
|
24660
|
+
return asStored(
|
|
24661
|
+
await mutate(memoryId, "edit", (memory) => ({
|
|
24662
|
+
...memory,
|
|
24663
|
+
topics: serializeTopics(mergeTopics([], chosen)),
|
|
24664
|
+
topics_user_managed: true,
|
|
24665
|
+
topics_updated_at: Date.now()
|
|
24666
|
+
}))
|
|
24667
|
+
);
|
|
24668
|
+
},
|
|
24501
24669
|
addTopics: async (memoryId, topics) => {
|
|
24502
|
-
const added = topicInputs(topics, "auto");
|
|
24670
|
+
const added = await withKnownKinds(topicInputs(topics, "auto"));
|
|
24503
24671
|
const saved = await mutate(memoryId, "edit", (memory) => ({
|
|
24504
24672
|
...memory,
|
|
24505
24673
|
topics: serializeTopics(mergeTopics(parseTopics(memory.topics) ?? [], added)),
|
|
@@ -24528,6 +24696,7 @@ function createRemoteMemoryStore(options) {
|
|
|
24528
24696
|
})
|
|
24529
24697
|
),
|
|
24530
24698
|
recall: (query, recallOptions) => pipeline.recall(query, recallOptions),
|
|
24699
|
+
factSource: pipeline.factSource,
|
|
24531
24700
|
retain: async (content, retainOptions) => {
|
|
24532
24701
|
const result = await pipeline.retain(content, retainOptions);
|
|
24533
24702
|
if (result.action === "create" || result.action === "supersede") notify("membership");
|
|
@@ -26296,19 +26465,20 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
|
|
|
26296
26465
|
const budget = wantsDecompose ? "high" : searchOptions?.rerank ? "mid" : "low";
|
|
26297
26466
|
const folderId = searchOptions?.folderId ?? argFolderId;
|
|
26298
26467
|
if (searchOptions?.useFusion === false) {
|
|
26299
|
-
const
|
|
26468
|
+
const legacyOptions = {
|
|
26469
|
+
limit: requestLimit,
|
|
26470
|
+
minSimilarity,
|
|
26471
|
+
useFusion: false,
|
|
26472
|
+
...tuningForward,
|
|
26473
|
+
...folderId !== void 0 && { folderId },
|
|
26474
|
+
...searchOptions?.scopes && { scopes: searchOptions.scopes }
|
|
26475
|
+
};
|
|
26476
|
+
const { results: legacy, embeddingsUnavailable } = "retain" in vaultCtx ? await vaultCtx.factSource.search(query, legacyOptions) : await searchVaultMemoriesWithSize(
|
|
26300
26477
|
query,
|
|
26301
26478
|
vaultCtx,
|
|
26302
26479
|
embeddingOptions,
|
|
26303
26480
|
cache,
|
|
26304
|
-
|
|
26305
|
-
limit: requestLimit,
|
|
26306
|
-
minSimilarity,
|
|
26307
|
-
useFusion: false,
|
|
26308
|
-
...tuningForward,
|
|
26309
|
-
...folderId !== void 0 && { folderId },
|
|
26310
|
-
...searchOptions?.scopes && { scopes: searchOptions.scopes }
|
|
26311
|
-
}
|
|
26481
|
+
legacyOptions
|
|
26312
26482
|
);
|
|
26313
26483
|
if (legacy.length === 0) {
|
|
26314
26484
|
return embeddingsUnavailable ? EMBEDDINGS_DEGRADED_EMPTY_NO_LEXICAL : "No relevant memories found in the vault.";
|
|
@@ -26327,7 +26497,7 @@ function createMemoryVaultSearchTool(vaultCtx, embeddingOptions, cache, searchOp
|
|
|
26327
26497
|
let recallDegraded = [];
|
|
26328
26498
|
const result = await recall(
|
|
26329
26499
|
query,
|
|
26330
|
-
{ vaultCtx, embeddingOptions, vaultCache: cache },
|
|
26500
|
+
"retain" in vaultCtx ? { factSource: vaultCtx.factSource, embeddingOptions } : { vaultCtx, embeddingOptions, vaultCache: cache },
|
|
26331
26501
|
{
|
|
26332
26502
|
onDiagnostics: (d) => {
|
|
26333
26503
|
recallDegraded = d.degraded;
|
|
@@ -26407,8 +26577,10 @@ function describeWriteOutcome(outcome2) {
|
|
|
26407
26577
|
}
|
|
26408
26578
|
}
|
|
26409
26579
|
function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
26580
|
+
const store = "retain" in vaultCtx ? vaultCtx : void 0;
|
|
26581
|
+
const ctx = "retain" in vaultCtx ? void 0 : vaultCtx;
|
|
26410
26582
|
const hasOnSave = !!options?.onSave;
|
|
26411
|
-
const folderNames = options?.folderMap ? Array.from(options.folderMap.keys()) : [];
|
|
26583
|
+
const folderNames = !store && options?.folderMap ? Array.from(options.folderMap.keys()) : [];
|
|
26412
26584
|
return {
|
|
26413
26585
|
type: "function",
|
|
26414
26586
|
function: {
|
|
@@ -26453,7 +26625,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
26453
26625
|
let previousContent;
|
|
26454
26626
|
const scope = options?.scope ?? "private";
|
|
26455
26627
|
if (isUpdate) {
|
|
26456
|
-
const existing = await getVaultMemoryOp(
|
|
26628
|
+
const existing = store ? await store.get(id) : await getVaultMemoryOp(ctx, id);
|
|
26457
26629
|
if (!existing) {
|
|
26458
26630
|
return `Error: Memory with ID "${id}" not found. Creating a new memory instead would require a separate call without an ID.`;
|
|
26459
26631
|
}
|
|
@@ -26474,9 +26646,18 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
26474
26646
|
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.";
|
|
26475
26647
|
}
|
|
26476
26648
|
}
|
|
26477
|
-
if (isUpdate) {
|
|
26649
|
+
if (isUpdate && store) {
|
|
26650
|
+
const updated = await store.update(id, {
|
|
26651
|
+
content,
|
|
26652
|
+
...factType !== void 0 && { factType }
|
|
26653
|
+
});
|
|
26654
|
+
if (!updated) {
|
|
26655
|
+
return `Error: Failed to update memory "${id}".`;
|
|
26656
|
+
}
|
|
26657
|
+
return `Memory updated successfully (ID: ${updated.uniqueId}).`;
|
|
26658
|
+
} else if (isUpdate) {
|
|
26478
26659
|
const folderId = folderName ? options?.folderMap?.get(folderName) : void 0;
|
|
26479
|
-
const updated = await updateVaultMemoryOp(
|
|
26660
|
+
const updated = await updateVaultMemoryOp(ctx, id, {
|
|
26480
26661
|
content,
|
|
26481
26662
|
embedding: null,
|
|
26482
26663
|
folderId,
|
|
@@ -26491,7 +26672,7 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
26491
26672
|
content,
|
|
26492
26673
|
embeddingOptions,
|
|
26493
26674
|
cache,
|
|
26494
|
-
|
|
26675
|
+
ctx,
|
|
26495
26676
|
id,
|
|
26496
26677
|
updated.updatedAt
|
|
26497
26678
|
).catch(() => {
|
|
@@ -26511,18 +26692,22 @@ function createMemoryVaultTool(vaultCtx, options, embeddingOptions, cache) {
|
|
|
26511
26692
|
await notifyWritten(options, input, outcome2);
|
|
26512
26693
|
return describeWriteOutcome(outcome2);
|
|
26513
26694
|
}
|
|
26514
|
-
const created = await
|
|
26695
|
+
const created = store ? await store.create({
|
|
26696
|
+
content,
|
|
26697
|
+
scope,
|
|
26698
|
+
...factType !== void 0 && { factType }
|
|
26699
|
+
}) : await createVaultMemoryOp(ctx, {
|
|
26515
26700
|
content,
|
|
26516
26701
|
scope,
|
|
26517
26702
|
folderId,
|
|
26518
26703
|
...factType !== void 0 && { factType }
|
|
26519
26704
|
});
|
|
26520
|
-
if (embeddingOptions && cache) {
|
|
26705
|
+
if (ctx && embeddingOptions && cache) {
|
|
26521
26706
|
eagerEmbedContent(
|
|
26522
26707
|
content,
|
|
26523
26708
|
embeddingOptions,
|
|
26524
26709
|
cache,
|
|
26525
|
-
|
|
26710
|
+
ctx,
|
|
26526
26711
|
created.uniqueId,
|
|
26527
26712
|
created.updatedAt
|
|
26528
26713
|
).catch(() => {
|
|
@@ -33506,6 +33691,8 @@ async function storedToLlmapiMessage(stored2, encryptionKey, resolveMediaByIds)
|
|
|
33506
33691
|
}
|
|
33507
33692
|
var CONVERSATION_REDACTOR_LIMIT = 50;
|
|
33508
33693
|
var NO_CONVERSATION_KEY = "__no_conversation__";
|
|
33694
|
+
var NO_STORE_FOLDERS = "Folders are not supported with a memoryStore";
|
|
33695
|
+
var NO_STORE_MASK = "piiRedaction needs a memoryStore built with embeddingOptions.maskInput";
|
|
33509
33696
|
var conversationRedactors = /* @__PURE__ */ new Map();
|
|
33510
33697
|
function getConversationRedactor(conversationId, nerDetector) {
|
|
33511
33698
|
const key = conversationId ?? NO_CONVERSATION_KEY;
|
|
@@ -33549,6 +33736,7 @@ function useChatStorage(options) {
|
|
|
33549
33736
|
signMessage,
|
|
33550
33737
|
embeddedWalletSigner,
|
|
33551
33738
|
getWalletAddress,
|
|
33739
|
+
memoryStore,
|
|
33552
33740
|
enableQueue = true,
|
|
33553
33741
|
autoFlushOnKeyAvailable = true,
|
|
33554
33742
|
extraToolSets,
|
|
@@ -33594,6 +33782,7 @@ function useChatStorage(options) {
|
|
|
33594
33782
|
() => ({ getToken, baseUrl, model: embeddingModel, maskInput: maskEmbeddingInput }),
|
|
33595
33783
|
[getToken, baseUrl, embeddingModel, maskEmbeddingInput]
|
|
33596
33784
|
);
|
|
33785
|
+
const storeMaskMissing = memoryStore !== void 0 && maskEmbeddingInput !== void 0 && !memoryStore.masksEmbeddingInput;
|
|
33597
33786
|
const blobManagerRef = (0, import_react6.useRef)(new BlobUrlManager());
|
|
33598
33787
|
(0, import_react6.useEffect)(() => {
|
|
33599
33788
|
const manager = blobManagerRef.current;
|
|
@@ -33870,6 +34059,15 @@ function useChatStorage(options) {
|
|
|
33870
34059
|
);
|
|
33871
34060
|
const retainVaultMemory = (0, import_react6.useCallback)(
|
|
33872
34061
|
async (input) => {
|
|
34062
|
+
if (memoryStore) {
|
|
34063
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34064
|
+
if (input.folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
34065
|
+
return memoryStore.retain(input.content, {
|
|
34066
|
+
source: "manual",
|
|
34067
|
+
scope: input.scope,
|
|
34068
|
+
...input.factType !== void 0 && { factType: input.factType }
|
|
34069
|
+
});
|
|
34070
|
+
}
|
|
33873
34071
|
if (!getToken) {
|
|
33874
34072
|
throw new Error("getToken is required to retain a vault memory");
|
|
33875
34073
|
}
|
|
@@ -33888,28 +34086,44 @@ function useChatStorage(options) {
|
|
|
33888
34086
|
}
|
|
33889
34087
|
);
|
|
33890
34088
|
},
|
|
33891
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
34089
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
33892
34090
|
);
|
|
33893
34091
|
const createMemoryVaultTool2 = (0, import_react6.useCallback)(
|
|
33894
34092
|
(options2) => {
|
|
34093
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
33895
34094
|
const embOpts = getToken ? vaultEmbeddingOptions : void 0;
|
|
33896
34095
|
return createMemoryVaultTool(
|
|
33897
|
-
vaultCtx,
|
|
33898
|
-
embOpts ? { write: retainVaultMemory, ...options2 } : options2,
|
|
34096
|
+
memoryStore ?? vaultCtx,
|
|
34097
|
+
embOpts || memoryStore ? { write: retainVaultMemory, ...options2 } : options2,
|
|
33899
34098
|
embOpts,
|
|
33900
34099
|
embOpts ? vaultEmbeddingCache : void 0
|
|
33901
34100
|
);
|
|
33902
34101
|
},
|
|
33903
|
-
[
|
|
34102
|
+
[
|
|
34103
|
+
vaultCtx,
|
|
34104
|
+
getToken,
|
|
34105
|
+
vaultEmbeddingOptions,
|
|
34106
|
+
vaultEmbeddingCache,
|
|
34107
|
+
retainVaultMemory,
|
|
34108
|
+
memoryStore,
|
|
34109
|
+
storeMaskMissing
|
|
34110
|
+
]
|
|
33904
34111
|
);
|
|
33905
34112
|
const getVaultMemories = (0, import_react6.useCallback)(
|
|
33906
34113
|
(options2) => {
|
|
33907
|
-
return getAllVaultMemoriesOp(vaultCtx, options2);
|
|
34114
|
+
if (!memoryStore) return getAllVaultMemoriesOp(vaultCtx, options2);
|
|
34115
|
+
const { folderId, levels, kinds, ...listOptions } = options2 ?? {};
|
|
34116
|
+
if (folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
34117
|
+
if (levels !== void 0 || kinds !== void 0)
|
|
34118
|
+
throw new Error("level and kind filters are not supported with a memoryStore");
|
|
34119
|
+
return memoryStore.list(listOptions);
|
|
33908
34120
|
},
|
|
33909
|
-
[vaultCtx]
|
|
34121
|
+
[vaultCtx, memoryStore]
|
|
33910
34122
|
);
|
|
33911
34123
|
const createVaultMemory = (0, import_react6.useCallback)(
|
|
33912
34124
|
async (content, scope) => {
|
|
34125
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34126
|
+
if (memoryStore) return memoryStore.create({ content, scope });
|
|
33913
34127
|
const result = await createVaultMemoryOp(vaultCtx, { content, scope });
|
|
33914
34128
|
if (getToken) {
|
|
33915
34129
|
eagerEmbedContent(
|
|
@@ -33925,10 +34139,12 @@ function useChatStorage(options) {
|
|
|
33925
34139
|
}
|
|
33926
34140
|
return result;
|
|
33927
34141
|
},
|
|
33928
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
34142
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
33929
34143
|
);
|
|
33930
34144
|
const updateVaultMemory = (0, import_react6.useCallback)(
|
|
33931
34145
|
async (id, content, scope) => {
|
|
34146
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34147
|
+
if (memoryStore) return memoryStore.update(id, { content, scope });
|
|
33932
34148
|
const existing = await getVaultMemoryOp(vaultCtx, id);
|
|
33933
34149
|
const result = await updateVaultMemoryOp(vaultCtx, id, { content, scope, embedding: null });
|
|
33934
34150
|
if (result && getToken) {
|
|
@@ -33948,10 +34164,11 @@ function useChatStorage(options) {
|
|
|
33948
34164
|
}
|
|
33949
34165
|
return result;
|
|
33950
34166
|
},
|
|
33951
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
34167
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
33952
34168
|
);
|
|
33953
34169
|
const deleteVaultMemory = (0, import_react6.useCallback)(
|
|
33954
34170
|
async (id) => {
|
|
34171
|
+
if (memoryStore) return memoryStore.delete(id);
|
|
33955
34172
|
const existing = await getVaultMemoryOp(vaultCtx, id);
|
|
33956
34173
|
const result = await deleteVaultMemoryOp(vaultCtx, id);
|
|
33957
34174
|
if (result && existing) {
|
|
@@ -33959,19 +34176,19 @@ function useChatStorage(options) {
|
|
|
33959
34176
|
}
|
|
33960
34177
|
return result;
|
|
33961
34178
|
},
|
|
33962
|
-
[vaultCtx, vaultEmbeddingCache]
|
|
34179
|
+
[vaultCtx, vaultEmbeddingCache, memoryStore]
|
|
33963
34180
|
);
|
|
33964
34181
|
const chunkVectorCacheRef = (0, import_react6.useRef)(createChunkVectorCache());
|
|
33965
34182
|
const clientToolEmbeddingsCacheRef = (0, import_react6.useRef)(/* @__PURE__ */ new Map());
|
|
33966
34183
|
(0, import_react6.useEffect)(() => {
|
|
33967
|
-
if (!getToken) return;
|
|
34184
|
+
if (!getToken || memoryStore) return;
|
|
33968
34185
|
void (async () => {
|
|
33969
34186
|
try {
|
|
33970
34187
|
await preEmbedVaultMemories(vaultCtx, vaultEmbeddingOptions, vaultEmbeddingCache);
|
|
33971
34188
|
} catch {
|
|
33972
34189
|
}
|
|
33973
34190
|
})();
|
|
33974
|
-
}, [vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]);
|
|
34191
|
+
}, [vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore]);
|
|
33975
34192
|
(0, import_react6.useEffect)(() => {
|
|
33976
34193
|
return onClearAllEncryptionState(() => {
|
|
33977
34194
|
vaultEmbeddingCache.clear();
|
|
@@ -33980,26 +34197,35 @@ function useChatStorage(options) {
|
|
|
33980
34197
|
}, [vaultEmbeddingCache]);
|
|
33981
34198
|
const createMemoryVaultSearchTool2 = (0, import_react6.useCallback)(
|
|
33982
34199
|
(searchOptions) => {
|
|
33983
|
-
if (!getToken) {
|
|
34200
|
+
if (!getToken && !memoryStore) {
|
|
33984
34201
|
throw new Error("getToken is required for memory vault search tool");
|
|
33985
34202
|
}
|
|
34203
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
33986
34204
|
return createMemoryVaultSearchTool(
|
|
33987
|
-
vaultCtx,
|
|
34205
|
+
memoryStore ?? vaultCtx,
|
|
33988
34206
|
vaultEmbeddingOptions,
|
|
33989
34207
|
vaultEmbeddingCache,
|
|
33990
34208
|
searchOptions
|
|
33991
34209
|
);
|
|
33992
34210
|
},
|
|
33993
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
34211
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
33994
34212
|
);
|
|
33995
34213
|
const createRecallTool2 = (0, import_react6.useCallback)(
|
|
33996
34214
|
(toolOptions, callbacks) => {
|
|
33997
|
-
if (!getToken) {
|
|
34215
|
+
if (!getToken && !memoryStore) {
|
|
33998
34216
|
throw new Error("getToken is required for recall tool");
|
|
33999
34217
|
}
|
|
34218
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34000
34219
|
const resolvedToolOptions = toolOptions?.excludeConversationId !== void 0 || !currentConversationId ? toolOptions : { ...toolOptions, excludeConversationId: currentConversationId };
|
|
34220
|
+
if (memoryStore && resolvedToolOptions?.folderId !== void 0)
|
|
34221
|
+
throw new Error(NO_STORE_FOLDERS);
|
|
34001
34222
|
return createRecallTool(
|
|
34002
|
-
{
|
|
34223
|
+
memoryStore ? {
|
|
34224
|
+
factSource: memoryStore.factSource,
|
|
34225
|
+
...getToken && { storageCtx },
|
|
34226
|
+
embeddingOptions: vaultEmbeddingOptions,
|
|
34227
|
+
chunkCache: chunkVectorCacheRef.current
|
|
34228
|
+
} : {
|
|
34003
34229
|
vaultCtx,
|
|
34004
34230
|
storageCtx,
|
|
34005
34231
|
embeddingOptions: vaultEmbeddingOptions,
|
|
@@ -34007,7 +34233,7 @@ function useChatStorage(options) {
|
|
|
34007
34233
|
chunkCache: chunkVectorCacheRef.current,
|
|
34008
34234
|
entityCtx
|
|
34009
34235
|
},
|
|
34010
|
-
resolvedToolOptions,
|
|
34236
|
+
memoryStore ? { ...resolvedToolOptions, memoryStore } : resolvedToolOptions,
|
|
34011
34237
|
callbacks
|
|
34012
34238
|
);
|
|
34013
34239
|
},
|
|
@@ -34018,11 +34244,15 @@ function useChatStorage(options) {
|
|
|
34018
34244
|
getToken,
|
|
34019
34245
|
vaultEmbeddingOptions,
|
|
34020
34246
|
vaultEmbeddingCache,
|
|
34021
|
-
currentConversationId
|
|
34247
|
+
currentConversationId,
|
|
34248
|
+
memoryStore,
|
|
34249
|
+
storeMaskMissing
|
|
34022
34250
|
]
|
|
34023
34251
|
);
|
|
34024
34252
|
const searchVaultMemoriesFn = (0, import_react6.useCallback)(
|
|
34025
34253
|
async (query, searchOptions) => {
|
|
34254
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34255
|
+
if (memoryStore) return (await memoryStore.factSource.search(query, searchOptions)).results;
|
|
34026
34256
|
if (!getToken) return [];
|
|
34027
34257
|
return searchVaultMemories(
|
|
34028
34258
|
query,
|
|
@@ -34032,11 +34262,11 @@ function useChatStorage(options) {
|
|
|
34032
34262
|
searchOptions
|
|
34033
34263
|
);
|
|
34034
34264
|
},
|
|
34035
|
-
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache]
|
|
34265
|
+
[vaultCtx, getToken, vaultEmbeddingOptions, vaultEmbeddingCache, memoryStore, storeMaskMissing]
|
|
34036
34266
|
);
|
|
34037
34267
|
const recallFn = (0, import_react6.useCallback)(
|
|
34038
34268
|
async (query, options2) => {
|
|
34039
|
-
if (!getToken) {
|
|
34269
|
+
if (!getToken && !memoryStore) {
|
|
34040
34270
|
return {
|
|
34041
34271
|
memories: [],
|
|
34042
34272
|
usedBudget: options2?.budget ?? "low",
|
|
@@ -34045,9 +34275,16 @@ function useChatStorage(options) {
|
|
|
34045
34275
|
};
|
|
34046
34276
|
}
|
|
34047
34277
|
const resolvedOptions = options2?.excludeConversationId !== void 0 || !currentConversationId ? options2 : { ...options2, excludeConversationId: currentConversationId };
|
|
34278
|
+
if (storeMaskMissing) throw new Error(NO_STORE_MASK);
|
|
34279
|
+
if (memoryStore && resolvedOptions?.folderId !== void 0) throw new Error(NO_STORE_FOLDERS);
|
|
34048
34280
|
return recall(
|
|
34049
34281
|
query,
|
|
34050
|
-
{
|
|
34282
|
+
memoryStore ? {
|
|
34283
|
+
factSource: memoryStore.factSource,
|
|
34284
|
+
...getToken && { storageCtx },
|
|
34285
|
+
embeddingOptions: vaultEmbeddingOptions,
|
|
34286
|
+
chunkCache: chunkVectorCacheRef.current
|
|
34287
|
+
} : {
|
|
34051
34288
|
vaultCtx,
|
|
34052
34289
|
storageCtx,
|
|
34053
34290
|
embeddingOptions: vaultEmbeddingOptions,
|
|
@@ -34065,7 +34302,9 @@ function useChatStorage(options) {
|
|
|
34065
34302
|
getToken,
|
|
34066
34303
|
currentConversationId,
|
|
34067
34304
|
vaultEmbeddingOptions,
|
|
34068
|
-
vaultEmbeddingCache
|
|
34305
|
+
vaultEmbeddingCache,
|
|
34306
|
+
memoryStore,
|
|
34307
|
+
storeMaskMissing
|
|
34069
34308
|
]
|
|
34070
34309
|
);
|
|
34071
34310
|
const {
|