@mastra/memory 1.36.0-alpha.0 → 1.36.0-alpha.2
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/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-memory-observational-memory.md +1 -1
- package/dist/docs/references/docs-memory-semantic-recall.md +35 -1
- package/dist/docs/references/docs-storage.md +1 -0
- package/dist/docs/references/reference-memory-observational-memory.md +2 -2
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/processors/index.cjs +1 -1
- package/dist/processors/index.js +1 -1
- package/dist/processors/observational-memory/observation-turn/step.d.ts +2 -2
- package/dist/processors/observational-memory/observation-turn/step.d.ts.map +1 -1
- package/dist/processors/observational-memory/observational-memory.d.ts +6 -0
- package/dist/processors/observational-memory/observational-memory.d.ts.map +1 -1
- package/dist/processors/observational-memory/token-counter.d.ts +0 -1
- package/dist/processors/observational-memory/token-counter.d.ts.map +1 -1
- package/dist/processors/observational-memory/types.d.ts +2 -2
- package/dist/{src-BV9eXuMM.js → src-BxI-PDpC.js} +171 -79
- package/dist/{src-BV9eXuMM.js.map → src-BxI-PDpC.js.map} +1 -1
- package/dist/{src-DhCg-LqR.cjs → src-OOB19zXe.cjs} +171 -79
- package/dist/{src-DhCg-LqR.cjs.map → src-OOB19zXe.cjs.map} +1 -1
- package/package.json +3 -3
|
@@ -3733,7 +3733,7 @@ function trimStartOfStream() {
|
|
|
3733
3733
|
};
|
|
3734
3734
|
}
|
|
3735
3735
|
//#endregion
|
|
3736
|
-
//#region ../_vendored/ai_v5/dist/dist-
|
|
3736
|
+
//#region ../_vendored/ai_v5/dist/dist-CwCWl16R.js
|
|
3737
3737
|
var marker$2$4 = "vercel.ai.error";
|
|
3738
3738
|
var symbol$2$4 = Symbol.for(marker$2$4);
|
|
3739
3739
|
var _a$2$4;
|
|
@@ -9020,7 +9020,7 @@ var object$1 = ({ schema: inputSchema }) => {
|
|
|
9020
9020
|
};
|
|
9021
9021
|
};
|
|
9022
9022
|
//#endregion
|
|
9023
|
-
//#region ../_vendored/ai_v6/dist/dist-
|
|
9023
|
+
//#region ../_vendored/ai_v6/dist/dist-B5Rt0qDx.js
|
|
9024
9024
|
var marker$2 = "vercel.ai.error";
|
|
9025
9025
|
var symbol$2 = Symbol.for(marker$2);
|
|
9026
9026
|
var _a$2;
|
|
@@ -11565,7 +11565,7 @@ var _b14;
|
|
|
11565
11565
|
}
|
|
11566
11566
|
});
|
|
11567
11567
|
//#endregion
|
|
11568
|
-
//#region ../_vendored/ai_v6/dist/dist-
|
|
11568
|
+
//#region ../_vendored/ai_v6/dist/dist-BgUIUei9.js
|
|
11569
11569
|
function getContext() {
|
|
11570
11570
|
return { headers: {} };
|
|
11571
11571
|
}
|
|
@@ -22201,6 +22201,7 @@ function serializeNonImageFilePartForTokenCounting(part) {
|
|
|
22201
22201
|
function isValidCacheEntry(entry, expectedKey, expectedSource) {
|
|
22202
22202
|
return Boolean(entry && entry.v === TOKEN_ESTIMATE_CACHE_VERSION && entry.source === expectedSource && entry.key === expectedKey && Number.isFinite(entry.tokens));
|
|
22203
22203
|
}
|
|
22204
|
+
const modelContextStorage = new async_hooks.AsyncLocalStorage();
|
|
22204
22205
|
function parseModelContext(model) {
|
|
22205
22206
|
if (!model) return void 0;
|
|
22206
22207
|
if (typeof model === "object") return model.provider || model.modelId ? {
|
|
@@ -22822,7 +22823,6 @@ async function fetchGoogleAttachmentTokenEstimate(modelId, part) {
|
|
|
22822
22823
|
var TokenCounter = class TokenCounter {
|
|
22823
22824
|
cacheSource;
|
|
22824
22825
|
defaultModelContext;
|
|
22825
|
-
modelContextStorage = new async_hooks.AsyncLocalStorage();
|
|
22826
22826
|
inFlightAttachmentCounts = /* @__PURE__ */ new Map();
|
|
22827
22827
|
multimodalToolResultCounts = /* @__PURE__ */ new WeakMap();
|
|
22828
22828
|
static TOKENS_PER_MESSAGE = 3.8;
|
|
@@ -22832,10 +22832,12 @@ var TokenCounter = class TokenCounter {
|
|
|
22832
22832
|
this.defaultModelContext = parseModelContext(options?.model);
|
|
22833
22833
|
}
|
|
22834
22834
|
runWithModelContext(model, fn) {
|
|
22835
|
-
|
|
22835
|
+
const contexts = new Map(modelContextStorage.getStore());
|
|
22836
|
+
contexts.set(this, parseModelContext(model));
|
|
22837
|
+
return modelContextStorage.run(contexts, fn);
|
|
22836
22838
|
}
|
|
22837
22839
|
getModelContext() {
|
|
22838
|
-
return
|
|
22840
|
+
return modelContextStorage.getStore()?.get(this) ?? this.defaultModelContext;
|
|
22839
22841
|
}
|
|
22840
22842
|
/**
|
|
22841
22843
|
* Count tokens in a plain string
|
|
@@ -28039,7 +28041,7 @@ var ObservationStep = class {
|
|
|
28039
28041
|
if (obsResult.succeeded) {
|
|
28040
28042
|
observed = true;
|
|
28041
28043
|
didThresholdCleanup = true;
|
|
28042
|
-
const observedIds = obsResult.
|
|
28044
|
+
const observedIds = obsResult.cleanupMessageIds;
|
|
28043
28045
|
const minRemaining = resolveRetentionFloor(om.getObservationConfig().bufferActivation ?? 1, statusSnapshot.threshold);
|
|
28044
28046
|
await om.cleanupMessages({
|
|
28045
28047
|
threadId,
|
|
@@ -28106,8 +28108,8 @@ var ObservationStep = class {
|
|
|
28106
28108
|
}
|
|
28107
28109
|
/**
|
|
28108
28110
|
* Run the full threshold observation pipeline:
|
|
28109
|
-
* waitForBuffering → re-check → activate → reflect → observe
|
|
28110
|
-
*
|
|
28111
|
+
* waitForBuffering → re-check → activate buffered chunks → reflect → observe
|
|
28112
|
+
* (sync fallback when pending tokens are still at or above the threshold)
|
|
28111
28113
|
*/
|
|
28112
28114
|
async runThresholdObservation() {
|
|
28113
28115
|
const { threadId, resourceId, messageList } = this.turn;
|
|
@@ -28115,59 +28117,76 @@ var ObservationStep = class {
|
|
|
28115
28117
|
await om.waitForBuffering(threadId, resourceId);
|
|
28116
28118
|
await this.turn.refreshRecord();
|
|
28117
28119
|
const observableMessages = this.seededResponseMessage ? getObservableMessages(messageList).filter((msg) => msg.id !== this.turn.responseMessageId) : getObservableMessages(messageList);
|
|
28118
|
-
|
|
28120
|
+
let status = await om.getStatus({
|
|
28119
28121
|
threadId,
|
|
28120
28122
|
resourceId,
|
|
28121
28123
|
record: this.turn.record,
|
|
28122
28124
|
messages: observableMessages
|
|
28123
28125
|
});
|
|
28124
|
-
if (!
|
|
28126
|
+
if (!status.shouldObserve) return {
|
|
28125
28127
|
succeeded: false,
|
|
28126
|
-
record:
|
|
28128
|
+
record: status.record,
|
|
28129
|
+
cleanupMessageIds: []
|
|
28127
28130
|
};
|
|
28128
|
-
|
|
28131
|
+
let pendingMessages = observableMessages;
|
|
28132
|
+
const activatedMessageIds = [];
|
|
28133
|
+
let activated = false;
|
|
28134
|
+
while (status.shouldObserve && status.canActivate) {
|
|
28129
28135
|
const activation = await om.activate({
|
|
28130
28136
|
threadId,
|
|
28131
28137
|
resourceId,
|
|
28132
28138
|
record: this.turn.record,
|
|
28133
|
-
messages:
|
|
28139
|
+
messages: pendingMessages,
|
|
28140
|
+
pendingTokens: status.pendingTokens,
|
|
28134
28141
|
currentModel: this.turn.actorModelContext,
|
|
28135
28142
|
writer: this.turn.writer,
|
|
28136
28143
|
messageList
|
|
28137
28144
|
});
|
|
28138
28145
|
this.turn.setRecord(activation.record);
|
|
28139
|
-
if (activation.activated)
|
|
28140
|
-
|
|
28141
|
-
|
|
28142
|
-
|
|
28143
|
-
|
|
28146
|
+
if (!activation.activated) break;
|
|
28147
|
+
activated = true;
|
|
28148
|
+
const ids = new Set(activation.activatedMessageIds ?? []);
|
|
28149
|
+
activatedMessageIds.push(...ids);
|
|
28150
|
+
pendingMessages = pendingMessages.filter((msg) => !ids.has(msg.id));
|
|
28151
|
+
status = await om.getStatus({
|
|
28152
|
+
threadId,
|
|
28153
|
+
resourceId,
|
|
28154
|
+
record: this.turn.record,
|
|
28155
|
+
messages: pendingMessages
|
|
28156
|
+
});
|
|
28157
|
+
}
|
|
28158
|
+
if (activated) {
|
|
28159
|
+
const postActivationRecord = this.turn.record;
|
|
28160
|
+
await om.reflector.maybeReflect({
|
|
28161
|
+
record: postActivationRecord,
|
|
28162
|
+
observationTokens: postActivationRecord.observationTokenCount ?? 0,
|
|
28163
|
+
threadId,
|
|
28164
|
+
writer: this.turn.writer,
|
|
28165
|
+
messageList,
|
|
28166
|
+
currentModel: this.turn.actorModelContext,
|
|
28167
|
+
requestContext: this.turn.requestContext,
|
|
28168
|
+
observabilityContext: this.turn.observabilityContext,
|
|
28169
|
+
lastActivityAt: getLastActivityFromMessages(getObservableMessages(messageList)),
|
|
28170
|
+
reflectionHooks: om.composeHooks(void 0, {
|
|
28144
28171
|
threadId,
|
|
28145
|
-
|
|
28146
|
-
messageList,
|
|
28147
|
-
currentModel: this.turn.actorModelContext,
|
|
28148
|
-
requestContext: this.turn.requestContext,
|
|
28149
|
-
observabilityContext: this.turn.observabilityContext,
|
|
28150
|
-
lastActivityAt: getLastActivityFromMessages(getObservableMessages(messageList)),
|
|
28151
|
-
reflectionHooks: om.composeHooks(void 0, {
|
|
28152
|
-
threadId,
|
|
28153
|
-
resourceId,
|
|
28154
|
-
trigger: "turn-sync"
|
|
28155
|
-
}),
|
|
28172
|
+
resourceId,
|
|
28156
28173
|
trigger: "turn-sync"
|
|
28157
|
-
})
|
|
28158
|
-
|
|
28159
|
-
|
|
28160
|
-
|
|
28161
|
-
|
|
28162
|
-
|
|
28163
|
-
|
|
28174
|
+
}),
|
|
28175
|
+
trigger: "turn-sync"
|
|
28176
|
+
});
|
|
28177
|
+
if (!status.shouldObserve) return {
|
|
28178
|
+
succeeded: true,
|
|
28179
|
+
record: postActivationRecord,
|
|
28180
|
+
activatedMessageIds,
|
|
28181
|
+
cleanupMessageIds: activatedMessageIds
|
|
28182
|
+
};
|
|
28164
28183
|
}
|
|
28165
28184
|
let obsResult;
|
|
28166
28185
|
try {
|
|
28167
28186
|
obsResult = await om.observe({
|
|
28168
28187
|
threadId,
|
|
28169
28188
|
resourceId,
|
|
28170
|
-
messages:
|
|
28189
|
+
messages: pendingMessages,
|
|
28171
28190
|
messageList,
|
|
28172
28191
|
trigger: "turn-sync",
|
|
28173
28192
|
agent: this.turn.agent,
|
|
@@ -28180,8 +28199,10 @@ var ObservationStep = class {
|
|
|
28180
28199
|
} catch (error) {
|
|
28181
28200
|
if (om.config.observation.failurePolicy !== "continue" || !isOmModelExecutionError(error) || error.failureKind !== "observer-model") throw error;
|
|
28182
28201
|
return {
|
|
28183
|
-
succeeded:
|
|
28184
|
-
record:
|
|
28202
|
+
succeeded: activated,
|
|
28203
|
+
record: this.turn.record,
|
|
28204
|
+
activatedMessageIds: activated ? activatedMessageIds : void 0,
|
|
28205
|
+
cleanupMessageIds: activatedMessageIds
|
|
28185
28206
|
};
|
|
28186
28207
|
}
|
|
28187
28208
|
if (obsResult.observed) {
|
|
@@ -28208,8 +28229,10 @@ var ObservationStep = class {
|
|
|
28208
28229
|
if (messagesToSeal.length > 0) await om.persistMessages(messagesToSeal, threadId, resourceId);
|
|
28209
28230
|
}
|
|
28210
28231
|
return {
|
|
28211
|
-
succeeded: obsResult.observed,
|
|
28232
|
+
succeeded: activated || obsResult.observed,
|
|
28212
28233
|
record: obsResult.record,
|
|
28234
|
+
activatedMessageIds: activated ? activatedMessageIds : void 0,
|
|
28235
|
+
cleanupMessageIds: [.../* @__PURE__ */ new Set([...activatedMessageIds, ...obsResult.observed ? obsResult.record.observedMessageIds ?? [] : []])],
|
|
28213
28236
|
observerExchange: om.observer.lastExchange
|
|
28214
28237
|
};
|
|
28215
28238
|
}
|
|
@@ -31897,6 +31920,7 @@ ${formattedMessages}
|
|
|
31897
31920
|
let activateAfterIdleExpiredMs;
|
|
31898
31921
|
let previousModel;
|
|
31899
31922
|
let currentModel;
|
|
31923
|
+
let livePendingTokens = opts.pendingTokens;
|
|
31900
31924
|
if (opts.checkThreshold) {
|
|
31901
31925
|
const thresholdMessages = opts.messages ?? await this.loadMessagesFromStorage(threadId, resourceId, record.lastObservedAt ? new Date(record.lastObservedAt) : void 0);
|
|
31902
31926
|
const activateAfterIdle = resolveActivationTTL(this.observationConfig.activateAfterIdle, opts.currentModel);
|
|
@@ -31925,6 +31949,7 @@ ${formattedMessages}
|
|
|
31925
31949
|
activated: false,
|
|
31926
31950
|
record
|
|
31927
31951
|
};
|
|
31952
|
+
livePendingTokens ??= status.pendingTokens;
|
|
31928
31953
|
}
|
|
31929
31954
|
}
|
|
31930
31955
|
if (record.isBufferingObservation) {
|
|
@@ -31948,8 +31973,9 @@ ${formattedMessages}
|
|
|
31948
31973
|
const messageTokensThreshold = getMaxThreshold(this.getEffectiveMessageTokens(freshRecord));
|
|
31949
31974
|
const activationRatio = resolveActivationRatio(this.observationConfig.bufferActivation ?? .7, messageTokensThreshold);
|
|
31950
31975
|
const totalChunkMessageTokens = freshChunks.reduce((sum, c) => sum + (c.messageTokens ?? 0), 0);
|
|
31951
|
-
const currentPendingTokens = freshRecord.pendingMessageTokens || totalChunkMessageTokens;
|
|
31976
|
+
const currentPendingTokens = livePendingTokens ?? (freshRecord.pendingMessageTokens || totalChunkMessageTokens);
|
|
31952
31977
|
const forceMaxActivation = !!(this.observationConfig.blockAfter && currentPendingTokens >= this.observationConfig.blockAfter);
|
|
31978
|
+
if (freshRecord.pendingMessageTokens !== currentPendingTokens) await this.storage.setPendingMessageTokens(freshRecord.id, currentPendingTokens);
|
|
31953
31979
|
const activationResult = await this.storage.swapBufferedToActive({
|
|
31954
31980
|
id: freshRecord.id,
|
|
31955
31981
|
activationRatio,
|
|
@@ -33294,22 +33320,32 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
|
|
|
33294
33320
|
if (resourceScope && !resourceId && config?.semanticRecall && vectorSearchString) throw new Error("Memory error: Resource-scoped semantic recall is enabled but no resourceId was provided. Either provide a resourceId or explicitly set semanticRecall.scope to 'thread'.");
|
|
33295
33321
|
let usage;
|
|
33296
33322
|
if (config?.semanticRecall && vectorSearchString && this.vector) {
|
|
33297
|
-
const
|
|
33298
|
-
|
|
33299
|
-
const {
|
|
33300
|
-
|
|
33301
|
-
|
|
33302
|
-
if (typeof this.vector === `undefined`) throw new Error(`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`);
|
|
33303
|
-
const scopeFilter = resourceScope ? { resource_id: resourceId } : { thread_id: threadId };
|
|
33304
|
-
const userFilter = typeof config.semanticRecall === "object" ? config.semanticRecall.filter : void 0;
|
|
33305
|
-
const combinedFilter = userFilter ? { $and: [scopeFilter, userFilter] } : scopeFilter;
|
|
33323
|
+
const scopeFilter = resourceScope ? { resource_id: resourceId } : { thread_id: threadId };
|
|
33324
|
+
const userFilter = typeof config.semanticRecall === "object" ? config.semanticRecall.filter : void 0;
|
|
33325
|
+
const combinedFilter = userFilter ? { $and: [scopeFilter, userFilter] } : scopeFilter;
|
|
33326
|
+
if (this.isSelfEmbedding) {
|
|
33327
|
+
const { indexName } = await this.createEmbeddingIndex(void 0, config);
|
|
33306
33328
|
vectorResults.push(...await this.vector.query({
|
|
33307
33329
|
indexName,
|
|
33308
|
-
|
|
33330
|
+
queryText: vectorSearchString,
|
|
33309
33331
|
topK: vectorConfig.topK,
|
|
33310
33332
|
filter: combinedFilter
|
|
33311
33333
|
}));
|
|
33312
|
-
}
|
|
33334
|
+
} else {
|
|
33335
|
+
const result = await this.embedMessageContent(vectorSearchString);
|
|
33336
|
+
usage = result.usage;
|
|
33337
|
+
const { embeddings, dimension } = result;
|
|
33338
|
+
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
33339
|
+
await Promise.all(embeddings.map(async (embedding) => {
|
|
33340
|
+
if (typeof this.vector === `undefined`) throw new Error(`Tried to query vector index ${indexName} but this Memory instance doesn't have an attached vector db.`);
|
|
33341
|
+
vectorResults.push(...await this.vector.query({
|
|
33342
|
+
indexName,
|
|
33343
|
+
queryVector: embedding,
|
|
33344
|
+
topK: vectorConfig.topK,
|
|
33345
|
+
filter: combinedFilter
|
|
33346
|
+
}));
|
|
33347
|
+
}));
|
|
33348
|
+
}
|
|
33313
33349
|
}
|
|
33314
33350
|
const threshold = (typeof config.semanticRecall === "object" ? config.semanticRecall : void 0)?.threshold;
|
|
33315
33351
|
const filteredVectorResults = threshold !== void 0 ? vectorResults.filter((r) => r.score >= threshold) : vectorResults;
|
|
@@ -33467,11 +33503,13 @@ var Memory = class Memory extends _mastra_core_memory.MastraMemory {
|
|
|
33467
33503
|
if (this.vector) this.trackVectorCleanup(this.deleteThreadVectors(threadId));
|
|
33468
33504
|
}
|
|
33469
33505
|
/**
|
|
33470
|
-
* Prefix shared by every message index. The index for the default embedding
|
|
33471
|
-
*
|
|
33506
|
+
* Prefix shared by every message index. The index for the default embedding dimension is named
|
|
33507
|
+
* with the bare prefix; other dimensions add a dimension suffix, and a store that embeds the
|
|
33508
|
+
* text itself adds its own. Built here rather than read from `getEmbeddingIndexName()`, which
|
|
33509
|
+
* answers for one configuration and so would not match indexes written under another.
|
|
33472
33510
|
*/
|
|
33473
33511
|
get messageIndexPrefix() {
|
|
33474
|
-
return this.
|
|
33512
|
+
return `memory${this.vector?.indexSeparator ?? "_"}messages`;
|
|
33475
33513
|
}
|
|
33476
33514
|
/**
|
|
33477
33515
|
* Prefix shared by every observation index. Each observation index adds a dimension suffix.
|
|
@@ -33756,13 +33794,24 @@ ${workingMemory}`;
|
|
|
33756
33794
|
if (joined) textForEmbedding = joined;
|
|
33757
33795
|
}
|
|
33758
33796
|
if (!textForEmbedding) return;
|
|
33759
|
-
|
|
33760
|
-
|
|
33761
|
-
|
|
33797
|
+
let embeddings = [];
|
|
33798
|
+
let documents;
|
|
33799
|
+
let chunks;
|
|
33800
|
+
if (this.isSelfEmbedding) {
|
|
33801
|
+
chunks = this.chunkText(textForEmbedding);
|
|
33802
|
+
documents = chunks;
|
|
33803
|
+
} else {
|
|
33804
|
+
const result = await this.embedMessageContent(textForEmbedding);
|
|
33805
|
+
dimension = result.dimension;
|
|
33806
|
+
if (result.usage?.tokens) totalTokens += result.usage.tokens;
|
|
33807
|
+
embeddings = result.embeddings;
|
|
33808
|
+
chunks = result.chunks;
|
|
33809
|
+
}
|
|
33762
33810
|
const threadMetadata = message.threadId ? threadMetadataMap.get(message.threadId) || {} : {};
|
|
33763
33811
|
embeddingData.push({
|
|
33764
|
-
embeddings
|
|
33765
|
-
|
|
33812
|
+
embeddings,
|
|
33813
|
+
documents,
|
|
33814
|
+
metadata: chunks.map(() => ({
|
|
33766
33815
|
...threadMetadata,
|
|
33767
33816
|
message_id: message.id,
|
|
33768
33817
|
thread_id: message.threadId,
|
|
@@ -33773,16 +33822,23 @@ ${workingMemory}`;
|
|
|
33773
33822
|
}))
|
|
33774
33823
|
});
|
|
33775
33824
|
}));
|
|
33776
|
-
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
33825
|
+
if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
|
|
33777
33826
|
if (typeof this.vector === `undefined`) throw new Error(`Tried to upsert embeddings but this Memory instance doesn't have an attached vector db.`);
|
|
33778
33827
|
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
33779
33828
|
const allVectors = [];
|
|
33780
33829
|
const allMetadata = [];
|
|
33830
|
+
const allDocuments = [];
|
|
33781
33831
|
for (const data of embeddingData) {
|
|
33782
33832
|
allVectors.push(...data.embeddings);
|
|
33833
|
+
if (data.documents) allDocuments.push(...data.documents);
|
|
33783
33834
|
allMetadata.push(...data.metadata);
|
|
33784
33835
|
}
|
|
33785
|
-
await this.vector.upsert({
|
|
33836
|
+
if (this.isSelfEmbedding) await this.vector.upsert({
|
|
33837
|
+
indexName,
|
|
33838
|
+
documents: allDocuments,
|
|
33839
|
+
metadata: allMetadata
|
|
33840
|
+
});
|
|
33841
|
+
else await this.vector.upsert({
|
|
33786
33842
|
indexName,
|
|
33787
33843
|
vectors: allVectors,
|
|
33788
33844
|
metadata: allMetadata
|
|
@@ -34567,11 +34623,22 @@ Notes:
|
|
|
34567
34623
|
}
|
|
34568
34624
|
}
|
|
34569
34625
|
if (textForEmbedding) {
|
|
34570
|
-
|
|
34571
|
-
|
|
34626
|
+
let embeddings = [];
|
|
34627
|
+
let documents;
|
|
34628
|
+
let chunks;
|
|
34629
|
+
if (this.isSelfEmbedding) {
|
|
34630
|
+
chunks = this.chunkText(textForEmbedding);
|
|
34631
|
+
documents = chunks;
|
|
34632
|
+
} else {
|
|
34633
|
+
const result = await this.embedMessageContent(textForEmbedding);
|
|
34634
|
+
dimension = result.dimension;
|
|
34635
|
+
embeddings = result.embeddings;
|
|
34636
|
+
chunks = result.chunks;
|
|
34637
|
+
}
|
|
34572
34638
|
embeddingData.push({
|
|
34573
|
-
embeddings
|
|
34574
|
-
|
|
34639
|
+
embeddings,
|
|
34640
|
+
documents,
|
|
34641
|
+
metadata: chunks.map(() => ({
|
|
34575
34642
|
message_id: message.id,
|
|
34576
34643
|
thread_id: existingMessage.threadId,
|
|
34577
34644
|
resource_id: existingMessage.resourceId,
|
|
@@ -34606,15 +34673,22 @@ Notes:
|
|
|
34606
34673
|
} catch {
|
|
34607
34674
|
this.logger.debug("Failed to clean up old vectors during message update");
|
|
34608
34675
|
}
|
|
34609
|
-
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
34676
|
+
if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
|
|
34610
34677
|
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
34611
34678
|
const allVectors = [];
|
|
34612
34679
|
const allMetadata = [];
|
|
34680
|
+
const allDocuments = [];
|
|
34613
34681
|
for (const data of embeddingData) {
|
|
34614
34682
|
allVectors.push(...data.embeddings);
|
|
34683
|
+
if (data.documents) allDocuments.push(...data.documents);
|
|
34615
34684
|
allMetadata.push(...data.metadata);
|
|
34616
34685
|
}
|
|
34617
|
-
await this.vector.upsert({
|
|
34686
|
+
if (this.isSelfEmbedding) await this.vector.upsert({
|
|
34687
|
+
indexName,
|
|
34688
|
+
documents: allDocuments,
|
|
34689
|
+
metadata: allMetadata
|
|
34690
|
+
});
|
|
34691
|
+
else await this.vector.upsert({
|
|
34618
34692
|
indexName,
|
|
34619
34693
|
vectors: allVectors,
|
|
34620
34694
|
metadata: allMetadata
|
|
@@ -34801,7 +34875,7 @@ Notes:
|
|
|
34801
34875
|
await this.rollbackCopiedThread(memoryStore, result.thread, "OM clone", false, priorDestinationResourceWm);
|
|
34802
34876
|
throw error;
|
|
34803
34877
|
}
|
|
34804
|
-
if (this.vector && this.embedder && config.semanticRecall) try {
|
|
34878
|
+
if (this.vector && (this.embedder || this.isSelfEmbedding) && config.semanticRecall) try {
|
|
34805
34879
|
await this.embedCopiedMessagesInBatches(memoryStore, result, config);
|
|
34806
34880
|
} catch (error) {
|
|
34807
34881
|
await this.rollbackCopiedThread(memoryStore, result.thread, "embedding", true, priorDestinationResourceWm);
|
|
@@ -34870,7 +34944,7 @@ Notes:
|
|
|
34870
34944
|
async updateThreadResourceId({ threadId, resourceId, memoryConfig }) {
|
|
34871
34945
|
const memoryStore = await this.getMemoryStore();
|
|
34872
34946
|
const config = this.getMergedThreadConfig(memoryConfig);
|
|
34873
|
-
const migratesVectors = Boolean(this.vector && this.embedder && config.semanticRecall);
|
|
34947
|
+
const migratesVectors = Boolean(this.vector && (this.embedder || this.isSelfEmbedding) && config.semanticRecall);
|
|
34874
34948
|
if (!migratesVectors) {
|
|
34875
34949
|
const existing = await memoryStore.getThreadById({ threadId });
|
|
34876
34950
|
if (existing && existing.resourceId === resourceId) return existing;
|
|
@@ -34976,7 +35050,7 @@ Notes:
|
|
|
34976
35050
|
* This is similar to the embedding logic in saveMessages but operates on already-saved messages.
|
|
34977
35051
|
*/
|
|
34978
35052
|
async embedClonedMessages(messages, config) {
|
|
34979
|
-
if (!this.vector || !this.embedder) return;
|
|
35053
|
+
if (!this.vector || !this.embedder && !this.isSelfEmbedding) return;
|
|
34980
35054
|
const embeddingData = [];
|
|
34981
35055
|
let dimension;
|
|
34982
35056
|
await Promise.all(messages.map(async (message) => {
|
|
@@ -34987,11 +35061,22 @@ Notes:
|
|
|
34987
35061
|
if (joined) textForEmbedding = joined;
|
|
34988
35062
|
}
|
|
34989
35063
|
if (!textForEmbedding) return;
|
|
34990
|
-
|
|
34991
|
-
|
|
35064
|
+
let embeddings = [];
|
|
35065
|
+
let documents;
|
|
35066
|
+
let chunks;
|
|
35067
|
+
if (this.isSelfEmbedding) {
|
|
35068
|
+
chunks = this.chunkText(textForEmbedding);
|
|
35069
|
+
documents = chunks;
|
|
35070
|
+
} else {
|
|
35071
|
+
const result = await this.embedMessageContent(textForEmbedding);
|
|
35072
|
+
dimension = result.dimension;
|
|
35073
|
+
embeddings = result.embeddings;
|
|
35074
|
+
chunks = result.chunks;
|
|
35075
|
+
}
|
|
34992
35076
|
embeddingData.push({
|
|
34993
|
-
embeddings
|
|
34994
|
-
|
|
35077
|
+
embeddings,
|
|
35078
|
+
documents,
|
|
35079
|
+
metadata: chunks.map(() => ({
|
|
34995
35080
|
message_id: message.id,
|
|
34996
35081
|
thread_id: message.threadId,
|
|
34997
35082
|
resource_id: message.resourceId,
|
|
@@ -35001,15 +35086,22 @@ Notes:
|
|
|
35001
35086
|
}))
|
|
35002
35087
|
});
|
|
35003
35088
|
}));
|
|
35004
|
-
if (embeddingData.length > 0 && dimension !== void 0) {
|
|
35089
|
+
if (embeddingData.length > 0 && (this.isSelfEmbedding || dimension !== void 0)) {
|
|
35005
35090
|
const { indexName } = await this.createEmbeddingIndex(dimension, config);
|
|
35006
35091
|
const allVectors = [];
|
|
35007
35092
|
const allMetadata = [];
|
|
35093
|
+
const allDocuments = [];
|
|
35008
35094
|
for (const data of embeddingData) {
|
|
35009
35095
|
allVectors.push(...data.embeddings);
|
|
35096
|
+
if (data.documents) allDocuments.push(...data.documents);
|
|
35010
35097
|
allMetadata.push(...data.metadata);
|
|
35011
35098
|
}
|
|
35012
|
-
await this.vector.upsert({
|
|
35099
|
+
if (this.isSelfEmbedding) await this.vector.upsert({
|
|
35100
|
+
indexName,
|
|
35101
|
+
documents: allDocuments,
|
|
35102
|
+
metadata: allMetadata
|
|
35103
|
+
});
|
|
35104
|
+
else await this.vector.upsert({
|
|
35013
35105
|
indexName,
|
|
35014
35106
|
vectors: allVectors,
|
|
35015
35107
|
metadata: allMetadata
|
|
@@ -35498,4 +35590,4 @@ Object.defineProperty(exports, "wrapInObservationGroup", {
|
|
|
35498
35590
|
}
|
|
35499
35591
|
});
|
|
35500
35592
|
|
|
35501
|
-
//# sourceMappingURL=src-
|
|
35593
|
+
//# sourceMappingURL=src-OOB19zXe.cjs.map
|