@mastra/memory 1.26.1-alpha.2 → 1.26.1-alpha.3

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.
@@ -18521,33 +18521,62 @@ var TokenCounter = class TokenCounter {
18521
18521
  if (isImageAttachment) await resolveImageDimensionsAsync(part);
18522
18522
  return this.countAttachmentPartSync(part);
18523
18523
  }
18524
+ /**
18525
+ * Count the name and the arguments of a tool call. Every state before the tool produces an
18526
+ * output holds the same call signature in the context window, so all of those states share
18527
+ * these cache kinds. `buildEstimateKey` hashes the text, so a shared kind stays correct and
18528
+ * keeps the estimate warm while the invocation moves from one state to the next.
18529
+ */
18530
+ countToolCallSignature(part, invocation) {
18531
+ let tokens = 0;
18532
+ let overheadDelta = 0;
18533
+ if (invocation.toolName) tokens += this.readOrPersistPartEstimate(part, "tool-call-name", invocation.toolName);
18534
+ if (invocation.args) if (typeof invocation.args === "string") tokens += this.readOrPersistPartEstimate(part, "tool-call-args", invocation.args);
18535
+ else {
18536
+ const argsJson = JSON.stringify(invocation.args);
18537
+ tokens += this.readOrPersistPartEstimate(part, "tool-call-args-json", argsJson);
18538
+ overheadDelta -= 12;
18539
+ }
18540
+ return {
18541
+ tokens,
18542
+ overheadDelta
18543
+ };
18544
+ }
18524
18545
  countNonAttachmentPart(part) {
18525
18546
  let overheadDelta = 0;
18526
- let toolResultDelta = 0;
18547
+ let extraMessageDelta = 0;
18527
18548
  if (part.type === "text") return {
18528
18549
  tokens: this.readOrPersistPartEstimate(part, "text", part.text),
18529
18550
  overheadDelta,
18530
- toolResultDelta
18551
+ extraMessageDelta
18531
18552
  };
18532
18553
  if (part.type === "tool-invocation") {
18533
18554
  const invocation = part.toolInvocation;
18555
+ const state = invocation.state;
18534
18556
  let tokens = 0;
18535
- if (invocation.state === "call" || invocation.state === "partial-call") {
18536
- if (invocation.toolName) tokens += this.readOrPersistPartEstimate(part, `tool-${invocation.state}-name`, invocation.toolName);
18537
- if (invocation.args) if (typeof invocation.args === "string") tokens += this.readOrPersistPartEstimate(part, `tool-${invocation.state}-args`, invocation.args);
18538
- else {
18539
- const argsJson = JSON.stringify(invocation.args);
18540
- tokens += this.readOrPersistPartEstimate(part, `tool-${invocation.state}-args-json`, argsJson);
18541
- overheadDelta -= 12;
18542
- }
18557
+ if (state === "call" || state === "partial-call" || state === "approval-requested") {
18558
+ const signature = this.countToolCallSignature(part, invocation);
18559
+ return {
18560
+ tokens: signature.tokens,
18561
+ overheadDelta: overheadDelta + signature.overheadDelta,
18562
+ extraMessageDelta
18563
+ };
18564
+ }
18565
+ if (state === "approval-responded") {
18566
+ extraMessageDelta++;
18567
+ const signature = this.countToolCallSignature(part, invocation);
18568
+ tokens += signature.tokens;
18569
+ overheadDelta += signature.overheadDelta;
18570
+ const reason = invocation.approval?.reason;
18571
+ if (reason) tokens += this.readOrPersistPartEstimate(part, "tool-approval-reason", reason);
18543
18572
  return {
18544
18573
  tokens,
18545
18574
  overheadDelta,
18546
- toolResultDelta
18575
+ extraMessageDelta
18547
18576
  };
18548
18577
  }
18549
- if (invocation.state === "result") {
18550
- toolResultDelta++;
18578
+ if (state === "result") {
18579
+ extraMessageDelta++;
18551
18580
  const { value: resultForCounting, usingStoredModelOutput } = this.resolveToolResultForTokenCounting(part, invocation.result);
18552
18581
  if (resultForCounting !== void 0) {
18553
18582
  const contentTokens = this.countMultimodalToolResultContent(part, resultForCounting);
@@ -18561,47 +18590,45 @@ var TokenCounter = class TokenCounter {
18561
18590
  return {
18562
18591
  tokens,
18563
18592
  overheadDelta,
18564
- toolResultDelta
18593
+ extraMessageDelta
18565
18594
  };
18566
18595
  }
18567
- if (invocation.state === "output-denied") {
18568
- toolResultDelta++;
18596
+ if (state === "output-denied") {
18597
+ extraMessageDelta++;
18569
18598
  const reason = invocation.approval?.reason ?? "Tool call was not approved by the user";
18570
18599
  tokens += this.readOrPersistPartEstimate(part, "tool-result-denied", reason);
18571
18600
  return {
18572
18601
  tokens,
18573
18602
  overheadDelta,
18574
- toolResultDelta
18603
+ extraMessageDelta
18575
18604
  };
18576
18605
  }
18577
- if (invocation.state === "output-error") {
18578
- toolResultDelta++;
18579
- const errorText = invocation.errorText;
18580
- const errorMessage = typeof errorText === "string" ? errorText : "Tool execution failed";
18606
+ if (state === "output-error") {
18607
+ extraMessageDelta++;
18608
+ const errorMessage = typeof invocation.errorText === "string" ? invocation.errorText : "Tool execution failed";
18581
18609
  tokens += this.readOrPersistPartEstimate(part, "tool-result-error", errorMessage);
18582
18610
  return {
18583
18611
  tokens,
18584
18612
  overheadDelta,
18585
- toolResultDelta
18613
+ extraMessageDelta
18586
18614
  };
18587
18615
  }
18588
- throw new Error(`Unhandled tool-invocation state '${part.toolInvocation?.state}' in token counting for part type '${part.type}'`);
18589
18616
  }
18590
18617
  if (typeof part.type === "string" && part.type.startsWith("data-")) return {
18591
18618
  tokens: 0,
18592
18619
  overheadDelta,
18593
- toolResultDelta
18620
+ extraMessageDelta
18594
18621
  };
18595
18622
  if (part.type === "reasoning") return {
18596
18623
  tokens: 0,
18597
18624
  overheadDelta,
18598
- toolResultDelta
18625
+ extraMessageDelta
18599
18626
  };
18600
18627
  const serialized = serializePartForTokenCounting(part);
18601
18628
  return {
18602
18629
  tokens: this.readOrPersistPartEstimate(part, `part-${part.type}`, serialized),
18603
18630
  overheadDelta,
18604
- toolResultDelta
18631
+ extraMessageDelta
18605
18632
  };
18606
18633
  }
18607
18634
  /**
@@ -18610,7 +18637,7 @@ var TokenCounter = class TokenCounter {
18610
18637
  countMessage(message) {
18611
18638
  let payloadTokens = this.countString(message.role);
18612
18639
  let overhead = TokenCounter.TOKENS_PER_MESSAGE;
18613
- let toolResultCount = 0;
18640
+ let extraMessageCount = 0;
18614
18641
  if (typeof message.content === "string") payloadTokens += this.readOrPersistMessageEstimate(message, "message-content", message.content);
18615
18642
  else if (message.content && typeof message.content === "object") {
18616
18643
  if (message.content.content && !Array.isArray(message.content.parts)) payloadTokens += this.readOrPersistMessageEstimate(message, "content-content", message.content.content);
@@ -18623,16 +18650,16 @@ var TokenCounter = class TokenCounter {
18623
18650
  const result = this.countNonAttachmentPart(part);
18624
18651
  payloadTokens += result.tokens;
18625
18652
  overhead += result.overheadDelta;
18626
- toolResultCount += result.toolResultDelta;
18653
+ extraMessageCount += result.extraMessageDelta;
18627
18654
  }
18628
18655
  }
18629
- if (toolResultCount > 0) overhead += toolResultCount * TokenCounter.TOKENS_PER_MESSAGE;
18656
+ if (extraMessageCount > 0) overhead += extraMessageCount * TokenCounter.TOKENS_PER_MESSAGE;
18630
18657
  return Math.round(payloadTokens + overhead);
18631
18658
  }
18632
18659
  async countMessageAsync(message) {
18633
18660
  let payloadTokens = this.countString(message.role);
18634
18661
  let overhead = TokenCounter.TOKENS_PER_MESSAGE;
18635
- let toolResultCount = 0;
18662
+ let extraMessageCount = 0;
18636
18663
  if (typeof message.content === "string") payloadTokens += this.readOrPersistMessageEstimate(message, "message-content", message.content);
18637
18664
  else if (message.content && typeof message.content === "object") {
18638
18665
  if (message.content.content && !Array.isArray(message.content.parts)) payloadTokens += this.readOrPersistMessageEstimate(message, "content-content", message.content.content);
@@ -18645,10 +18672,10 @@ var TokenCounter = class TokenCounter {
18645
18672
  const result = this.countNonAttachmentPart(part);
18646
18673
  payloadTokens += result.tokens;
18647
18674
  overhead += result.overheadDelta;
18648
- toolResultCount += result.toolResultDelta;
18675
+ extraMessageCount += result.extraMessageDelta;
18649
18676
  }
18650
18677
  }
18651
- if (toolResultCount > 0) overhead += toolResultCount * TokenCounter.TOKENS_PER_MESSAGE;
18678
+ if (extraMessageCount > 0) overhead += extraMessageCount * TokenCounter.TOKENS_PER_MESSAGE;
18652
18679
  return Math.round(payloadTokens + overhead);
18653
18680
  }
18654
18681
  /**
@@ -19751,6 +19778,7 @@ function deepMergeWorkingMemory(existing, update) {
19751
19778
  for (const key of Object.keys(update)) {
19752
19779
  const updateValue = update[key];
19753
19780
  const existingValue = result[key];
19781
+ if (updateValue === void 0) continue;
19754
19782
  if (updateValue === null) delete result[key];
19755
19783
  else if (Array.isArray(updateValue)) result[key] = updateValue;
19756
19784
  else if (typeof updateValue === "object" && updateValue !== null && typeof existingValue === "object" && existingValue !== null && !Array.isArray(existingValue)) result[key] = deepMergeWorkingMemory(existingValue, updateValue);
@@ -19803,7 +19831,7 @@ const updateWorkingMemoryTool = (memoryConfig) => {
19803
19831
  version: 1,
19804
19832
  vendor: "mastra",
19805
19833
  validate: (value) => {
19806
- const memoryValue = !!value && typeof value === "object" && !Array.isArray(value) && "memory" in value ? value.memory : stripNullsFromOptional(value, jsonSchema);
19834
+ const memoryValue = stripNullsFromOptional(!!value && typeof value === "object" && !Array.isArray(value) && "memory" in value ? value.memory : value, jsonSchema);
19807
19835
  const result = validateMemory(memoryValue);
19808
19836
  return result instanceof Promise ? result.then(toWrappedResult) : toWrappedResult(result);
19809
19837
  },
@@ -19820,6 +19848,7 @@ const updateWorkingMemoryTool = (memoryConfig) => {
19820
19848
  id: "update-working-memory",
19821
19849
  description: schema ? useStateSignals ? `${stateSignalsPreamble} Data is merged with existing memory — only include fields you want to add or update.` : `Update the working memory with new information. Data is merged with existing memory - only include fields you want to add or update. To preserve existing data, omit the field entirely. Arrays are replaced entirely when provided, so pass the complete array or omit it to keep the existing values.` : useStateSignals ? `${stateSignalsPreamble} Pass the full updated Markdown blob as a string in the memory field.` : `Update the working memory with new information. Any data not included will be overwritten. Always pass data as string to the memory field. Never pass an object.`,
19822
19850
  inputSchema,
19851
+ ...usesMergeSemantics ? { strict: false } : {},
19823
19852
  execute: async (inputData, context) => {
19824
19853
  const workingMemoryInput = inputData;
19825
19854
  const threadId = context?.agent?.threadId;
@@ -28584,4 +28613,4 @@ Notes:
28584
28613
  //#endregion
28585
28614
  export { extractCurrentTask as A, OBSERVATION_CONTEXT_INSTRUCTIONS as B, WorkingMemoryExtractor as C, OBSERVER_SYSTEM_PROMPT as D, TokenCounter as E, injectAnchorIds as F, OBSERVATION_CONTINUATION_HINT as H, parseAnchorId as I, stripEphemeralAnchorIds as L, hasCurrentTaskSection as M, optimizeObservationsForContext as N, buildObserverPrompt as O, parseObserverOutput as P, Extractor as R, deepMergeWorkingMemory as S, summarizeConversation as T, OBSERVATION_CONTEXT_PROMPT as V, reconcileObservationGroupsFromReflection as _, extractWorkingMemoryContent as a, wrapInObservationGroup as b, WORKING_MEMORY_STATE_ID as c, getObservationsAsOf as d, ObservationalMemoryProcessor as f, parseObservationGroups as g, deriveObservationGroupProvenance as h, WorkingMemory as i, formatMessagesForObserver as j, buildObserverSystemPrompt as k, WORKING_MEMORY_STATE_PROCESSOR_ID as l, combineObservationGroupRanges as m, MessageHistory$1 as n, extractWorkingMemoryTags as o, ObservationalMemory as p, SemanticRecall as r, removeWorkingMemoryTags as s, Memory as t, WorkingMemoryStateProcessor as u, renderObservationGroupsForReflection as v, SUMMARIZE_THREAD_DEFAULTS as w, ModelByInputTokens as x, stripObservationGroups as y, OBSERVATIONAL_MEMORY_DEFAULTS as z };
28586
28615
 
28587
- //# sourceMappingURL=src-CdqJP57F.js.map
28616
+ //# sourceMappingURL=src-MScpLVRh.js.map