@mastra/memory 0.12.0 → 0.12.1-alpha.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.
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/processors/token-limiter.ts","../../src/processors/tool-call-filter.ts"],"names":["MemoryProcessor","Tiktoken","o200k_base"],"mappings":";;;;;;;;;;;;AAoBO,IAAM,YAAA,GAAN,cAA2BA,sBAAA,CAAgB;AAAA,EACxC,OAAA;AAAA,EACA,SAAA;AAAA;AAAA;AAAA;AAAA,EAKD,kBAAA,GAAqB,GAAA;AAAA;AAAA,EACrB,uBAAA,GAA0B,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMjC,YAAY,OAAA,EAAuC;AACjD,IAAA,KAAA,CAAM;AAAA,MACJ,IAAA,EAAM;AAAA,KACP,CAAA;AAED,IAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAE/B,MAAA,IAAA,CAAK,SAAA,GAAY,OAAA;AACjB,MAAA,IAAA,CAAK,OAAA,GAAU,IAAIC,aAAA,CAASC,2BAAU,CAAA;AAAA,IACxC,CAAA,MAAO;AAEL,MAAA,IAAA,CAAK,YAAY,OAAA,CAAQ,KAAA;AACzB,MAAA,IAAA,CAAK,OAAA,GAAU,IAAID,aAAA,CAAS,OAAA,CAAQ,YAAYC,2BAAU,CAAA;AAAA,IAC5D;AAAA,EACF;AAAA,EAEA,OAAA,CACE,UACA,EAAE,aAAA,EAAe,qBAAqB,WAAA,EAAY,GAAyB,EAAC,EAC7D;AAEf,IAAA,IAAI,WAAA,GAAc,CAAA;AAGlB,IAAA,WAAA,IAAe,IAAA,CAAK,uBAAA;AAEpB,IAAA,IAAI,aAAA,EAAe;AACjB,MAAA,WAAA,IAAe,IAAA,CAAK,YAAY,aAAa,CAAA;AAC7C,MAAA,WAAA,IAAe,IAAA,CAAK,kBAAA;AAAA,IACtB;AAEA,IAAA,IAAI,mBAAA,EAAqB;AACvB,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from '@mastra/core/memory';\nimport { Tiktoken } from 'js-tiktoken/lite';\nimport type { TiktokenBPE } from 'js-tiktoken/lite';\nimport o200k_base from 'js-tiktoken/ranks/o200k_base';\n\n/**\n * Configuration options for TokenLimiter\n */\ninterface TokenLimiterOptions {\n /** Maximum number of tokens to allow */\n limit: number;\n /** Optional encoding to use (defaults to o200k_base which is used by gpt-4o) */\n encoding?: TiktokenBPE;\n}\n\n/**\n * Limits the total number of tokens in the messages.\n * Uses js-tiktoken with o200k_base encoding by default for accurate token counting with modern models.\n */\nexport class TokenLimiter extends MemoryProcessor {\n private encoder: Tiktoken;\n private maxTokens: number;\n\n // Token overheads per OpenAI's documentation\n // See: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#6-counting-tokens-for-chat-completions-api-calls\n // Every message follows <|start|>{role/name}\\n{content}<|end|>\n public TOKENS_PER_MESSAGE = 3.8; // tokens added for each message (start & end tokens)\n public TOKENS_PER_CONVERSATION = 24; // fixed overhead for the conversation\n\n /**\n * Create a token limiter for messages.\n * @param options Either a number (token limit) or a configuration object\n */\n constructor(options: number | TokenLimiterOptions) {\n super({\n name: 'TokenLimiter',\n });\n\n if (typeof options === 'number') {\n // Simple number format - just the token limit with default encoding\n this.maxTokens = options;\n this.encoder = new Tiktoken(o200k_base);\n } else {\n // Object format with limit and optional encoding\n this.maxTokens = options.limit;\n this.encoder = new Tiktoken(options.encoding || o200k_base);\n }\n }\n\n process(\n messages: CoreMessage[],\n { systemMessage, memorySystemMessage, newMessages }: MemoryProcessorOpts = {},\n ): CoreMessage[] {\n // Messages are already chronologically ordered - take most recent ones up to the token limit\n let totalTokens = 0;\n\n // Start with the conversation overhead\n totalTokens += this.TOKENS_PER_CONVERSATION;\n\n if (systemMessage) {\n totalTokens += this.countTokens(systemMessage);\n totalTokens += this.TOKENS_PER_MESSAGE; // Add message overhead for system message\n }\n\n if (memorySystemMessage) {\n totalTokens += this.countTokens(memorySystemMessage);\n totalTokens += this.TOKENS_PER_MESSAGE; // Add message overhead for memory system message\n }\n\n const allMessages = [...messages, ...(newMessages || [])];\n\n const result: CoreMessage[] = [];\n\n // Process messages in reverse (newest first) so that we stop estimating tokens on old messages. Once we get to our limit of tokens there's no reason to keep processing older messages\n for (let i = allMessages.length - 1; i >= 0; i--) {\n const message = allMessages[i];\n\n // Skip undefined messages (shouldn't happen, but TypeScript is concerned)\n if (!message) continue;\n\n const messageTokens = this.countTokens(message);\n\n if (totalTokens + messageTokens <= this.maxTokens) {\n // Insert at the beginning to maintain chronological order, but only if it's not a new message\n if (i < messages.length) {\n // less than messages.length because we're iterating in reverse. If the index is greater than messages.length it's a new message\n result.unshift(message);\n }\n totalTokens += messageTokens;\n } else {\n this.logger.info(\n `filtering ${allMessages.length - result.length}/${allMessages.length} messages, token limit of ${this.maxTokens} exceeded`,\n );\n // If we can't fit the message, we stop\n break;\n }\n }\n\n return result;\n }\n\n public countTokens(message: string | CoreMessage): number {\n if (typeof message === `string`) {\n return this.encoder.encode(message).length;\n }\n\n let tokenString = message.role;\n let overhead = 0;\n\n if (typeof message.content === 'string' && message.content) {\n tokenString += message.content;\n } else if (Array.isArray(message.content)) {\n // Calculate tokens for each content part\n for (const part of message.content) {\n if (part.type === 'text') {\n tokenString += part.text;\n } else if (part.type === 'tool-call' || part.type === `tool-result`) {\n if (`args` in part && part.args && part.type === `tool-call`) {\n tokenString += part.toolName as any;\n if (typeof part.args === 'string') {\n tokenString += part.args;\n } else {\n tokenString += JSON.stringify(part.args);\n // minus some tokens for JSON\n overhead -= 12;\n }\n }\n // Token cost for result if present\n if (`result` in part && part.result !== undefined && part.type === `tool-result`) {\n if (typeof part.result === 'string') {\n tokenString += part.result;\n } else {\n tokenString += JSON.stringify(part.result);\n // minus some tokens for JSON\n overhead -= 12;\n }\n }\n } else {\n tokenString += JSON.stringify(part);\n }\n }\n }\n\n if (\n typeof message.content === `string` ||\n // if the message included non-tool parts, add our message overhead\n message.content.some(p => p.type !== `tool-call` && p.type !== `tool-result`)\n ) {\n // Ensure we account for message formatting tokens\n // See: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#6-counting-tokens-for-chat-completions-api-calls\n overhead += this.TOKENS_PER_MESSAGE;\n }\n\n return this.encoder.encode(tokenString).length + overhead;\n }\n}\n","import type { CoreMessage } from '@mastra/core';\nimport { MemoryProcessor } from '@mastra/core';\n\n/**\n * Filters out tool calls and results from messages.\n * By default (with no arguments), excludes all tool calls and their results.\n * Can be configured to exclude only specific tools by name.\n */\nexport class ToolCallFilter extends MemoryProcessor {\n private exclude: string[] | 'all';\n\n /**\n * Create a filter for tool calls and results.\n * @param options Configuration options\n * @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.\n */\n constructor(options: { exclude?: string[] } = {}) {\n super({ name: 'ToolCallFilter' });\n // If no options or exclude is provided, exclude all tools\n if (!options || !options.exclude) {\n this.exclude = 'all'; // Exclude all tools\n } else {\n // Exclude specific tools\n this.exclude = Array.isArray(options.exclude) ? options.exclude : [];\n }\n }\n\n process(messages: CoreMessage[]): CoreMessage[] {\n // Case 1: Exclude all tool calls and tool results\n if (this.exclude === 'all') {\n return messages.filter(message => {\n if (Array.isArray(message.content)) {\n return !message.content.some(part => part.type === 'tool-call' || part.type === 'tool-result');\n }\n return true;\n });\n }\n\n // Case 2: Exclude specific tools by name\n if (this.exclude.length > 0) {\n // Single pass approach - track excluded tool call IDs while filtering\n const excludedToolCallIds = new Set<string>();\n\n return messages.filter(message => {\n if (!Array.isArray(message.content)) return true;\n\n // For assistant messages, check for excluded tool calls and track their IDs\n if (message.role === 'assistant') {\n let shouldExclude = false;\n\n for (const part of message.content) {\n if (part.type === 'tool-call' && this.exclude.includes(part.toolName)) {\n excludedToolCallIds.add(part.toolCallId);\n shouldExclude = true;\n }\n }\n\n return !shouldExclude;\n }\n\n // For tool messages, filter out results for excluded tool calls\n if (message.role === 'tool') {\n const shouldExclude = message.content.some(\n part => part.type === 'tool-result' && excludedToolCallIds.has(part.toolCallId),\n );\n\n return !shouldExclude;\n }\n\n return true;\n });\n }\n\n // Case 3: Empty exclude array, return original messages\n return messages;\n }\n}\n"]}
@@ -1,2 +1,3 @@
1
- export { TokenLimiter } from '../_tsup-dts-rollup.js';
2
- export { ToolCallFilter } from '../_tsup-dts-rollup.js';
1
+ export * from './token-limiter';
2
+ export * from './tool-call-filter';
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/processors/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,oBAAoB,CAAC"}
@@ -155,3 +155,5 @@ var ToolCallFilter = class extends MemoryProcessor$1 {
155
155
  };
156
156
 
157
157
  export { TokenLimiter, ToolCallFilter };
158
+ //# sourceMappingURL=index.js.map
159
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/processors/token-limiter.ts","../../src/processors/tool-call-filter.ts"],"names":["MemoryProcessor"],"mappings":";;;;;;AAoBO,IAAM,YAAA,GAAN,cAA2B,eAAA,CAAgB;AAAA,EACxC,OAAA;AAAA,EACA,SAAA;AAAA;AAAA;AAAA;AAAA,EAKD,kBAAA,GAAqB,GAAA;AAAA;AAAA,EACrB,uBAAA,GAA0B,EAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAMjC,YAAY,OAAA,EAAuC;AACjD,IAAA,KAAA,CAAM;AAAA,MACJ,IAAA,EAAM;AAAA,KACP,CAAA;AAED,IAAA,IAAI,OAAO,YAAY,QAAA,EAAU;AAE/B,MAAA,IAAA,CAAK,SAAA,GAAY,OAAA;AACjB,MAAA,IAAA,CAAK,OAAA,GAAU,IAAI,QAAA,CAAS,UAAU,CAAA;AAAA,IACxC,CAAA,MAAO;AAEL,MAAA,IAAA,CAAK,YAAY,OAAA,CAAQ,KAAA;AACzB,MAAA,IAAA,CAAK,OAAA,GAAU,IAAI,QAAA,CAAS,OAAA,CAAQ,YAAY,UAAU,CAA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AAC3B,UAAA,MAAM,aAAA,GAAgB,QAAQ,OAAA,CAAQ,IAAA;AAAA,YACpC,UAAQ,IAAA,CAAK,IAAA,KAAS,iBAAiB,mBAAA,CAAoB,GAAA,CAAI,KAAK,UAAU;AAAA,WAChF;AAEA,UAAA,OAAO,CAAC,aAAA;AAAA,QACV;AAEA,QAAA,OAAO,IAAA;AAAA,MACT,CAAC,CAAA;AAAA,IACH;AAGA,IAAA,OAAO,QAAA;AAAA,EACT;AACF","file":"index.js","sourcesContent":["import type { CoreMessage, MemoryProcessorOpts } from '@mastra/core';\nimport { MemoryProcessor } from '@mastra/core/memory';\nimport { Tiktoken } from 'js-tiktoken/lite';\nimport type { TiktokenBPE } from 'js-tiktoken/lite';\nimport o200k_base from 'js-tiktoken/ranks/o200k_base';\n\n/**\n * Configuration options for TokenLimiter\n */\ninterface TokenLimiterOptions {\n /** Maximum number of tokens to allow */\n limit: number;\n /** Optional encoding to use (defaults to o200k_base which is used by gpt-4o) */\n encoding?: TiktokenBPE;\n}\n\n/**\n * Limits the total number of tokens in the messages.\n * Uses js-tiktoken with o200k_base encoding by default for accurate token counting with modern models.\n */\nexport class TokenLimiter extends MemoryProcessor {\n private encoder: Tiktoken;\n private maxTokens: number;\n\n // Token overheads per OpenAI's documentation\n // See: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#6-counting-tokens-for-chat-completions-api-calls\n // Every message follows <|start|>{role/name}\\n{content}<|end|>\n public TOKENS_PER_MESSAGE = 3.8; // tokens added for each message (start & end tokens)\n public TOKENS_PER_CONVERSATION = 24; // fixed overhead for the conversation\n\n /**\n * Create a token limiter for messages.\n * @param options Either a number (token limit) or a configuration object\n */\n constructor(options: number | TokenLimiterOptions) {\n super({\n name: 'TokenLimiter',\n });\n\n if (typeof options === 'number') {\n // Simple number format - just the token limit with default encoding\n this.maxTokens = options;\n this.encoder = new Tiktoken(o200k_base);\n } else {\n // Object format with limit and optional encoding\n this.maxTokens = options.limit;\n this.encoder = new Tiktoken(options.encoding || o200k_base);\n }\n }\n\n process(\n messages: CoreMessage[],\n { systemMessage, memorySystemMessage, newMessages }: MemoryProcessorOpts = {},\n ): CoreMessage[] {\n // Messages are already chronologically ordered - take most recent ones up to the token limit\n let totalTokens = 0;\n\n // Start with the conversation overhead\n totalTokens += this.TOKENS_PER_CONVERSATION;\n\n if (systemMessage) {\n totalTokens += this.countTokens(systemMessage);\n totalTokens += this.TOKENS_PER_MESSAGE; // Add message overhead for system message\n }\n\n if (memorySystemMessage) {\n totalTokens += this.countTokens(memorySystemMessage);\n totalTokens += this.TOKENS_PER_MESSAGE; // Add message overhead for memory system message\n }\n\n const allMessages = [...messages, ...(newMessages || [])];\n\n const result: CoreMessage[] = [];\n\n // Process messages in reverse (newest first) so that we stop estimating tokens on old messages. Once we get to our limit of tokens there's no reason to keep processing older messages\n for (let i = allMessages.length - 1; i >= 0; i--) {\n const message = allMessages[i];\n\n // Skip undefined messages (shouldn't happen, but TypeScript is concerned)\n if (!message) continue;\n\n const messageTokens = this.countTokens(message);\n\n if (totalTokens + messageTokens <= this.maxTokens) {\n // Insert at the beginning to maintain chronological order, but only if it's not a new message\n if (i < messages.length) {\n // less than messages.length because we're iterating in reverse. If the index is greater than messages.length it's a new message\n result.unshift(message);\n }\n totalTokens += messageTokens;\n } else {\n this.logger.info(\n `filtering ${allMessages.length - result.length}/${allMessages.length} messages, token limit of ${this.maxTokens} exceeded`,\n );\n // If we can't fit the message, we stop\n break;\n }\n }\n\n return result;\n }\n\n public countTokens(message: string | CoreMessage): number {\n if (typeof message === `string`) {\n return this.encoder.encode(message).length;\n }\n\n let tokenString = message.role;\n let overhead = 0;\n\n if (typeof message.content === 'string' && message.content) {\n tokenString += message.content;\n } else if (Array.isArray(message.content)) {\n // Calculate tokens for each content part\n for (const part of message.content) {\n if (part.type === 'text') {\n tokenString += part.text;\n } else if (part.type === 'tool-call' || part.type === `tool-result`) {\n if (`args` in part && part.args && part.type === `tool-call`) {\n tokenString += part.toolName as any;\n if (typeof part.args === 'string') {\n tokenString += part.args;\n } else {\n tokenString += JSON.stringify(part.args);\n // minus some tokens for JSON\n overhead -= 12;\n }\n }\n // Token cost for result if present\n if (`result` in part && part.result !== undefined && part.type === `tool-result`) {\n if (typeof part.result === 'string') {\n tokenString += part.result;\n } else {\n tokenString += JSON.stringify(part.result);\n // minus some tokens for JSON\n overhead -= 12;\n }\n }\n } else {\n tokenString += JSON.stringify(part);\n }\n }\n }\n\n if (\n typeof message.content === `string` ||\n // if the message included non-tool parts, add our message overhead\n message.content.some(p => p.type !== `tool-call` && p.type !== `tool-result`)\n ) {\n // Ensure we account for message formatting tokens\n // See: https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken#6-counting-tokens-for-chat-completions-api-calls\n overhead += this.TOKENS_PER_MESSAGE;\n }\n\n return this.encoder.encode(tokenString).length + overhead;\n }\n}\n","import type { CoreMessage } from '@mastra/core';\nimport { MemoryProcessor } from '@mastra/core';\n\n/**\n * Filters out tool calls and results from messages.\n * By default (with no arguments), excludes all tool calls and their results.\n * Can be configured to exclude only specific tools by name.\n */\nexport class ToolCallFilter extends MemoryProcessor {\n private exclude: string[] | 'all';\n\n /**\n * Create a filter for tool calls and results.\n * @param options Configuration options\n * @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.\n */\n constructor(options: { exclude?: string[] } = {}) {\n super({ name: 'ToolCallFilter' });\n // If no options or exclude is provided, exclude all tools\n if (!options || !options.exclude) {\n this.exclude = 'all'; // Exclude all tools\n } else {\n // Exclude specific tools\n this.exclude = Array.isArray(options.exclude) ? options.exclude : [];\n }\n }\n\n process(messages: CoreMessage[]): CoreMessage[] {\n // Case 1: Exclude all tool calls and tool results\n if (this.exclude === 'all') {\n return messages.filter(message => {\n if (Array.isArray(message.content)) {\n return !message.content.some(part => part.type === 'tool-call' || part.type === 'tool-result');\n }\n return true;\n });\n }\n\n // Case 2: Exclude specific tools by name\n if (this.exclude.length > 0) {\n // Single pass approach - track excluded tool call IDs while filtering\n const excludedToolCallIds = new Set<string>();\n\n return messages.filter(message => {\n if (!Array.isArray(message.content)) return true;\n\n // For assistant messages, check for excluded tool calls and track their IDs\n if (message.role === 'assistant') {\n let shouldExclude = false;\n\n for (const part of message.content) {\n if (part.type === 'tool-call' && this.exclude.includes(part.toolName)) {\n excludedToolCallIds.add(part.toolCallId);\n shouldExclude = true;\n }\n }\n\n return !shouldExclude;\n }\n\n // For tool messages, filter out results for excluded tool calls\n if (message.role === 'tool') {\n const shouldExclude = message.content.some(\n part => part.type === 'tool-result' && excludedToolCallIds.has(part.toolCallId),\n );\n\n return !shouldExclude;\n }\n\n return true;\n });\n }\n\n // Case 3: Empty exclude array, return original messages\n return messages;\n }\n}\n"]}
@@ -0,0 +1,31 @@
1
+ import type { CoreMessage, MemoryProcessorOpts } from '@mastra/core';
2
+ import { MemoryProcessor } from '@mastra/core/memory';
3
+ import type { TiktokenBPE } from 'js-tiktoken/lite';
4
+ /**
5
+ * Configuration options for TokenLimiter
6
+ */
7
+ interface TokenLimiterOptions {
8
+ /** Maximum number of tokens to allow */
9
+ limit: number;
10
+ /** Optional encoding to use (defaults to o200k_base which is used by gpt-4o) */
11
+ encoding?: TiktokenBPE;
12
+ }
13
+ /**
14
+ * Limits the total number of tokens in the messages.
15
+ * Uses js-tiktoken with o200k_base encoding by default for accurate token counting with modern models.
16
+ */
17
+ export declare class TokenLimiter extends MemoryProcessor {
18
+ private encoder;
19
+ private maxTokens;
20
+ TOKENS_PER_MESSAGE: number;
21
+ TOKENS_PER_CONVERSATION: number;
22
+ /**
23
+ * Create a token limiter for messages.
24
+ * @param options Either a number (token limit) or a configuration object
25
+ */
26
+ constructor(options: number | TokenLimiterOptions);
27
+ process(messages: CoreMessage[], { systemMessage, memorySystemMessage, newMessages }?: MemoryProcessorOpts): CoreMessage[];
28
+ countTokens(message: string | CoreMessage): number;
29
+ }
30
+ export {};
31
+ //# sourceMappingURL=token-limiter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"token-limiter.d.ts","sourceRoot":"","sources":["../../src/processors/token-limiter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,mBAAmB,EAAE,MAAM,cAAc,CAAC;AACrE,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEtD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAGpD;;GAEG;AACH,UAAU,mBAAmB;IAC3B,wCAAwC;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,gFAAgF;IAChF,QAAQ,CAAC,EAAE,WAAW,CAAC;CACxB;AAED;;;GAGG;AACH,qBAAa,YAAa,SAAQ,eAAe;IAC/C,OAAO,CAAC,OAAO,CAAW;IAC1B,OAAO,CAAC,SAAS,CAAS;IAKnB,kBAAkB,SAAO;IACzB,uBAAuB,SAAM;IAEpC;;;OAGG;gBACS,OAAO,EAAE,MAAM,GAAG,mBAAmB;IAgBjD,OAAO,CACL,QAAQ,EAAE,WAAW,EAAE,EACvB,EAAE,aAAa,EAAE,mBAAmB,EAAE,WAAW,EAAE,GAAE,mBAAwB,GAC5E,WAAW,EAAE;IAiDT,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,WAAW,GAAG,MAAM;CAsD1D"}
@@ -0,0 +1,20 @@
1
+ import type { CoreMessage } from '@mastra/core';
2
+ import { MemoryProcessor } from '@mastra/core';
3
+ /**
4
+ * Filters out tool calls and results from messages.
5
+ * By default (with no arguments), excludes all tool calls and their results.
6
+ * Can be configured to exclude only specific tools by name.
7
+ */
8
+ export declare class ToolCallFilter extends MemoryProcessor {
9
+ private exclude;
10
+ /**
11
+ * Create a filter for tool calls and results.
12
+ * @param options Configuration options
13
+ * @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.
14
+ */
15
+ constructor(options?: {
16
+ exclude?: string[];
17
+ });
18
+ process(messages: CoreMessage[]): CoreMessage[];
19
+ }
20
+ //# sourceMappingURL=tool-call-filter.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tool-call-filter.d.ts","sourceRoot":"","sources":["../../src/processors/tool-call-filter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C;;;;GAIG;AACH,qBAAa,cAAe,SAAQ,eAAe;IACjD,OAAO,CAAC,OAAO,CAAmB;IAElC;;;;OAIG;gBACS,OAAO,GAAE;QAAE,OAAO,CAAC,EAAE,MAAM,EAAE,CAAA;KAAO;IAWhD,OAAO,CAAC,QAAQ,EAAE,WAAW,EAAE,GAAG,WAAW,EAAE;CAiDhD"}
@@ -0,0 +1,4 @@
1
+ import type { CoreTool, MemoryConfig } from '@mastra/core';
2
+ export declare const updateWorkingMemoryTool: (memoryConfig?: MemoryConfig) => CoreTool;
3
+ export declare const __experimental_updateWorkingMemoryToolVNext: (config: MemoryConfig) => CoreTool;
4
+ //# sourceMappingURL=working-memory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"working-memory.d.ts","sourceRoot":"","sources":["../../src/tools/working-memory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,cAAc,CAAC;AAG3D,eAAO,MAAM,uBAAuB,GAAI,eAAe,YAAY,KAAG,QA0CpE,CAAC;AAEH,eAAO,MAAM,2CAA2C,GAAI,QAAQ,YAAY,KAAG,QAqFjF,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/memory",
3
- "version": "0.12.0",
3
+ "version": "0.12.1-alpha.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -56,15 +56,15 @@
56
56
  "typescript": "^5.8.3",
57
57
  "typescript-eslint": "^8.38.0",
58
58
  "vitest": "^3.2.4",
59
- "@internal/lint": "0.0.24",
60
- "@mastra/core": "0.12.0"
59
+ "@internal/lint": "0.0.26",
60
+ "@mastra/core": "0.12.2-alpha.0"
61
61
  },
62
62
  "peerDependencies": {
63
63
  "@mastra/core": ">=0.12.0-0 <0.13.0-0"
64
64
  },
65
65
  "scripts": {
66
66
  "check": "tsc --noEmit",
67
- "build": "pnpm run check && tsup --silent src/index.ts src/processors/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting",
67
+ "build": "pnpm run check && tsup --silent --config tsup.config.ts",
68
68
  "build:watch": "pnpm build --watch",
69
69
  "test:integration": "cd integration-tests && pnpm run test",
70
70
  "test:unit": "pnpm vitest run ./src/*",
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@ import { MessageList } from '@mastra/core/agent';
4
4
  import type { MastraMessageV2, UIMessageWithMetadata } from '@mastra/core/agent';
5
5
  import { MastraMemory } from '@mastra/core/memory';
6
6
  import type { MemoryConfig, SharedMemoryConfig, StorageThreadType, WorkingMemoryTemplate } from '@mastra/core/memory';
7
- import type { StorageGetMessagesArg, ThreadSortOptions } from '@mastra/core/storage';
7
+ import type { StorageGetMessagesArg, ThreadSortOptions, PaginationInfo } from '@mastra/core/storage';
8
8
  import { embedMany } from 'ai';
9
9
  import type { CoreMessage, TextPart } from 'ai';
10
10
  import { Mutex } from 'async-mutex';
@@ -258,6 +258,30 @@ export class Memory extends MastraMemory {
258
258
  return this.storage.getThreadsByResourceId({ resourceId, orderBy, sortDirection });
259
259
  }
260
260
 
261
+ async getThreadsByResourceIdPaginated({
262
+ resourceId,
263
+ page,
264
+ perPage,
265
+ orderBy,
266
+ sortDirection,
267
+ }: {
268
+ resourceId: string;
269
+ page: number;
270
+ perPage: number;
271
+ } & ThreadSortOptions): Promise<
272
+ PaginationInfo & {
273
+ threads: StorageThreadType[];
274
+ }
275
+ > {
276
+ return this.storage.getThreadsByResourceIdPaginated({
277
+ resourceId,
278
+ page,
279
+ perPage,
280
+ orderBy,
281
+ sortDirection,
282
+ });
283
+ }
284
+
261
285
  async saveThread({ thread }: { thread: StorageThreadType; memoryConfig?: MemoryConfig }): Promise<StorageThreadType> {
262
286
  return this.storage.saveThread({ thread });
263
287
  }
@@ -0,0 +1,9 @@
1
+ {
2
+ "extends": ["./tsconfig.json", "../../tsconfig.build.json"],
3
+ "compilerOptions": {
4
+ "outDir": "./dist",
5
+ "rootDir": "./src"
6
+ },
7
+ "include": ["src/**/*"],
8
+ "exclude": ["node_modules", "**/*.test.ts", "src/**/*.mock.ts"]
9
+ }
package/tsconfig.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "extends": "../../tsconfig.node.json",
3
- "include": ["src/**/*"],
3
+ "include": ["src/**/*", "tsup.config.ts"],
4
4
  "exclude": ["node_modules", "**/*.test.ts"]
5
5
  }
package/tsup.config.ts ADDED
@@ -0,0 +1,22 @@
1
+ import { spawn } from 'child_process';
2
+ import { promisify } from 'util';
3
+ import { defineConfig } from 'tsup';
4
+
5
+ const exec = promisify(spawn);
6
+
7
+ export default defineConfig({
8
+ entry: ['src/index.ts', 'src/processors/index.ts'],
9
+ format: ['esm', 'cjs'],
10
+ clean: true,
11
+ dts: false,
12
+ splitting: true,
13
+ treeshake: {
14
+ preset: 'smallest',
15
+ },
16
+ sourcemap: true,
17
+ onSuccess: async () => {
18
+ await exec('pnpm', ['tsc', '-p', 'tsconfig.build.json'], {
19
+ stdio: 'inherit',
20
+ });
21
+ },
22
+ });
@@ -1,212 +0,0 @@
1
- import type { CoreMessage } from 'ai';
2
- import type { CoreMessage as CoreMessage_2 } from '@mastra/core';
3
- import type { CoreTool } from '@mastra/core';
4
- import { embedMany } from 'ai';
5
- import { MastraMemory } from '@mastra/core/memory';
6
- import type { MastraMessageV1 } from '@mastra/core';
7
- import type { MastraMessageV2 } from '@mastra/core/agent';
8
- import type { MemoryConfig } from '@mastra/core/memory';
9
- import type { MemoryConfig as MemoryConfig_2 } from '@mastra/core';
10
- import { MemoryProcessor } from '@mastra/core/memory';
11
- import { MemoryProcessor as MemoryProcessor_2 } from '@mastra/core';
12
- import type { MemoryProcessorOpts } from '@mastra/core';
13
- import type { SharedMemoryConfig } from '@mastra/core/memory';
14
- import type { StorageGetMessagesArg } from '@mastra/core/storage';
15
- import type { StorageThreadType } from '@mastra/core/memory';
16
- import type { ThreadSortOptions } from '@mastra/core/storage';
17
- import type { TiktokenBPE } from 'js-tiktoken/lite';
18
- import type { UIMessageWithMetadata } from '@mastra/core/agent';
19
- import type { WorkingMemoryTemplate } from '@mastra/core/memory';
20
-
21
- export declare const __experimental_updateWorkingMemoryToolVNext: (config: MemoryConfig_2) => CoreTool;
22
-
23
- /**
24
- * Concrete implementation of MastraMemory that adds support for thread configuration
25
- * and message injection.
26
- */
27
- export declare class Memory extends MastraMemory {
28
- constructor(config?: SharedMemoryConfig);
29
- protected validateThreadIsOwnedByResource(threadId: string, resourceId: string): Promise<void>;
30
- protected checkStorageFeatureSupport(config: MemoryConfig): void;
31
- query({ threadId, resourceId, selectBy, threadConfig, }: StorageGetMessagesArg & {
32
- threadConfig?: MemoryConfig;
33
- }): Promise<{
34
- messages: CoreMessage[];
35
- uiMessages: UIMessageWithMetadata[];
36
- messagesV2: MastraMessageV2[];
37
- }>;
38
- rememberMessages({ threadId, resourceId, vectorMessageSearch, config, }: {
39
- threadId: string;
40
- resourceId?: string;
41
- vectorMessageSearch?: string;
42
- config?: MemoryConfig;
43
- }): Promise<{
44
- messages: MastraMessageV1[];
45
- messagesV2: MastraMessageV2[];
46
- }>;
47
- getThreadById({ threadId }: {
48
- threadId: string;
49
- }): Promise<StorageThreadType | null>;
50
- getThreadsByResourceId({ resourceId, orderBy, sortDirection, }: {
51
- resourceId: string;
52
- } & ThreadSortOptions): Promise<StorageThreadType[]>;
53
- saveThread({ thread }: {
54
- thread: StorageThreadType;
55
- memoryConfig?: MemoryConfig;
56
- }): Promise<StorageThreadType>;
57
- updateThread({ id, title, metadata, }: {
58
- id: string;
59
- title: string;
60
- metadata: Record<string, unknown>;
61
- }): Promise<StorageThreadType>;
62
- deleteThread(threadId: string): Promise<void>;
63
- updateWorkingMemory({ threadId, resourceId, workingMemory, memoryConfig, }: {
64
- threadId: string;
65
- resourceId?: string;
66
- workingMemory: string;
67
- memoryConfig?: MemoryConfig;
68
- }): Promise<void>;
69
- private updateWorkingMemoryMutexes;
70
- /**
71
- * @warning experimental! can be removed or changed at any time
72
- */
73
- __experimental_updateWorkingMemoryVNext({ threadId, resourceId, workingMemory, searchString, memoryConfig, }: {
74
- threadId: string;
75
- resourceId?: string;
76
- workingMemory: string;
77
- searchString?: string;
78
- memoryConfig?: MemoryConfig;
79
- }): Promise<{
80
- success: boolean;
81
- reason: string;
82
- }>;
83
- protected chunkText(text: string, tokenSize?: number): string[];
84
- private hasher;
85
- private embeddingCache;
86
- private firstEmbed;
87
- protected embedMessageContent(content: string): Promise<{
88
- chunks: string[];
89
- embeddings: Awaited<ReturnType<typeof embedMany>>["embeddings"];
90
- dimension: number | undefined;
91
- }>;
92
- saveMessages(args: {
93
- messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];
94
- memoryConfig?: MemoryConfig | undefined;
95
- format?: 'v1';
96
- }): Promise<MastraMessageV1[]>;
97
- saveMessages(args: {
98
- messages: (MastraMessageV1 | MastraMessageV2)[] | MastraMessageV1[] | MastraMessageV2[];
99
- memoryConfig?: MemoryConfig | undefined;
100
- format: 'v2';
101
- }): Promise<MastraMessageV2[]>;
102
- protected updateMessageToHideWorkingMemory(message: MastraMessageV1): MastraMessageV1 | null;
103
- protected updateMessageToHideWorkingMemoryV2(message: MastraMessageV2): MastraMessageV2 | null;
104
- protected parseWorkingMemory(text: string): string | null;
105
- getWorkingMemory({ threadId, resourceId, memoryConfig, }: {
106
- threadId: string;
107
- resourceId?: string;
108
- memoryConfig?: MemoryConfig;
109
- }): Promise<string | null>;
110
- /**
111
- * Gets the working memory template for the current memory configuration.
112
- * Supports both ZodObject and JSONSchema7 schemas.
113
- *
114
- * @param memoryConfig - The memory configuration containing the working memory settings
115
- * @returns The working memory template with format and content, or null if working memory is disabled
116
- */
117
- getWorkingMemoryTemplate({ memoryConfig, }: {
118
- memoryConfig?: MemoryConfig;
119
- }): Promise<WorkingMemoryTemplate | null>;
120
- getSystemMessage({ threadId, resourceId, memoryConfig, }: {
121
- threadId: string;
122
- resourceId?: string;
123
- memoryConfig?: MemoryConfig;
124
- }): Promise<string | null>;
125
- defaultWorkingMemoryTemplate: string;
126
- protected getWorkingMemoryToolInstruction({ template, data, }: {
127
- template: WorkingMemoryTemplate;
128
- data: string | null;
129
- }): string;
130
- protected __experimental_getWorkingMemoryToolInstructionVNext({ template, data, }: {
131
- template: WorkingMemoryTemplate;
132
- data: string | null;
133
- }): string;
134
- private isVNextWorkingMemoryConfig;
135
- getTools(config?: MemoryConfig): Record<string, CoreTool>;
136
- /**
137
- * Updates the metadata of a list of messages
138
- * @param messages - The list of messages to update
139
- * @returns The list of updated messages
140
- */
141
- updateMessages({ messages, }: {
142
- messages: Partial<MastraMessageV2> & {
143
- id: string;
144
- }[];
145
- }): Promise<MastraMessageV2[]>;
146
- /**
147
- * Deletes one or more messages
148
- * @param input - Must be an array containing either:
149
- * - Message ID strings
150
- * - Message objects with 'id' properties
151
- * @returns Promise that resolves when all messages are deleted
152
- */
153
- deleteMessages(input: MessageDeleteInput): Promise<void>;
154
- }
155
-
156
- export declare type MessageDeleteInput = string[] | {
157
- id: string;
158
- }[];
159
-
160
- /**
161
- * Limits the total number of tokens in the messages.
162
- * Uses js-tiktoken with o200k_base encoding by default for accurate token counting with modern models.
163
- */
164
- declare class TokenLimiter extends MemoryProcessor {
165
- private encoder;
166
- private maxTokens;
167
- TOKENS_PER_MESSAGE: number;
168
- TOKENS_PER_CONVERSATION: number;
169
- /**
170
- * Create a token limiter for messages.
171
- * @param options Either a number (token limit) or a configuration object
172
- */
173
- constructor(options: number | TokenLimiterOptions);
174
- process(messages: CoreMessage_2[], { systemMessage, memorySystemMessage, newMessages }?: MemoryProcessorOpts): CoreMessage_2[];
175
- countTokens(message: string | CoreMessage_2): number;
176
- }
177
- export { TokenLimiter }
178
- export { TokenLimiter as TokenLimiter_alias_1 }
179
-
180
- /**
181
- * Configuration options for TokenLimiter
182
- */
183
- declare interface TokenLimiterOptions {
184
- /** Maximum number of tokens to allow */
185
- limit: number;
186
- /** Optional encoding to use (defaults to o200k_base which is used by gpt-4o) */
187
- encoding?: TiktokenBPE;
188
- }
189
-
190
- /**
191
- * Filters out tool calls and results from messages.
192
- * By default (with no arguments), excludes all tool calls and their results.
193
- * Can be configured to exclude only specific tools by name.
194
- */
195
- declare class ToolCallFilter extends MemoryProcessor_2 {
196
- private exclude;
197
- /**
198
- * Create a filter for tool calls and results.
199
- * @param options Configuration options
200
- * @param options.exclude List of specific tool names to exclude. If not provided, all tool calls are excluded.
201
- */
202
- constructor(options?: {
203
- exclude?: string[];
204
- });
205
- process(messages: CoreMessage_2[]): CoreMessage_2[];
206
- }
207
- export { ToolCallFilter }
208
- export { ToolCallFilter as ToolCallFilter_alias_1 }
209
-
210
- export declare const updateWorkingMemoryTool: (memoryConfig?: MemoryConfig_2) => CoreTool;
211
-
212
- export { }