@mastra/memory 0.2.0-alpha.0 → 0.2.0-alpha.10

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.
@@ -1,27 +1,27 @@
1
1
 
2
- > @mastra/memory@0.2.0-alpha.0 build /home/runner/work/mastra/mastra/packages/memory
2
+ > @mastra/memory@0.2.0-alpha.10 build /home/runner/work/mastra/mastra/packages/memory
3
3
  > pnpm run check && tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake
4
4
 
5
5
 
6
- > @mastra/memory@0.2.0-alpha.0 check /home/runner/work/mastra/mastra/packages/memory
6
+ > @mastra/memory@0.2.0-alpha.10 check /home/runner/work/mastra/mastra/packages/memory
7
7
  > tsc --noEmit
8
8
 
9
9
  CLI Building entry: src/index.ts
10
10
  CLI Using tsconfig: tsconfig.json
11
11
  CLI tsup v8.3.6
12
12
  TSC Build start
13
- TSC ⚡️ Build success in 7804ms
13
+ TSC ⚡️ Build success in 8086ms
14
14
  DTS Build start
15
15
  CLI Target: es2022
16
16
  Analysis will use the bundled TypeScript version 5.7.3
17
17
  Writing package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.ts
18
18
  Analysis will use the bundled TypeScript version 5.7.3
19
19
  Writing package typings: /home/runner/work/mastra/mastra/packages/memory/dist/_tsup-dts-rollup.d.cts
20
- DTS ⚡️ Build success in 10512ms
20
+ DTS ⚡️ Build success in 10514ms
21
21
  CLI Cleaning output folder
22
22
  ESM Build start
23
23
  CJS Build start
24
- ESM dist/index.js 12.07 KB
25
- ESM ⚡️ Build success in 709ms
26
- CJS dist/index.cjs 12.10 KB
27
- CJS ⚡️ Build success in 711ms
24
+ ESM dist/index.js 12.79 KB
25
+ ESM ⚡️ Build success in 657ms
26
+ CJS dist/index.cjs 12.83 KB
27
+ CJS ⚡️ Build success in 658ms
package/CHANGELOG.md CHANGED
@@ -1,5 +1,94 @@
1
1
  # @mastra/memory
2
2
 
3
+ ## 0.2.0-alpha.10
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies [a910463]
8
+ - @mastra/core@0.5.0-alpha.10
9
+
10
+ ## 0.2.0-alpha.9
11
+
12
+ ### Patch Changes
13
+
14
+ - f2301de: Added the ability to ensure the accessed thread in memory.query() is for the right resource id. ex memory.query({ threadId, resourceId }). If the resourceId doesn't own the thread it will throw an error.
15
+ - Updated dependencies [e9fbac5]
16
+ - Updated dependencies [1e8bcbc]
17
+ - Updated dependencies [aeb5e36]
18
+ - Updated dependencies [f2301de]
19
+ - @mastra/core@0.5.0-alpha.9
20
+
21
+ ## 0.2.0-alpha.8
22
+
23
+ ### Patch Changes
24
+
25
+ - Updated dependencies [506f1d5]
26
+ - @mastra/core@0.5.0-alpha.8
27
+
28
+ ## 0.2.0-alpha.7
29
+
30
+ ### Patch Changes
31
+
32
+ - Updated dependencies [ee667a2]
33
+ - @mastra/core@0.5.0-alpha.7
34
+
35
+ ## 0.2.0-alpha.6
36
+
37
+ ### Patch Changes
38
+
39
+ - Updated dependencies [f6678e4]
40
+ - @mastra/core@0.5.0-alpha.6
41
+
42
+ ## 0.2.0-alpha.5
43
+
44
+ ### Patch Changes
45
+
46
+ - c151ae6: Fixed an issue where models that don't support structured output would error when generating a thread title. Added an option to disable thread title llm generation `new Memory({ threads: { generateTitle: false }})`
47
+ - Updated dependencies [22643eb]
48
+ - Updated dependencies [6feb23f]
49
+ - Updated dependencies [f2d6727]
50
+ - Updated dependencies [301e4ee]
51
+ - Updated dependencies [dfbe4e9]
52
+ - Updated dependencies [9e81f35]
53
+ - Updated dependencies [caefaa2]
54
+ - Updated dependencies [c151ae6]
55
+ - Updated dependencies [52e0418]
56
+ - Updated dependencies [03236ec]
57
+ - Updated dependencies [3764e71]
58
+ - Updated dependencies [df982db]
59
+ - Updated dependencies [0461849]
60
+ - Updated dependencies [2259379]
61
+ - Updated dependencies [358f069]
62
+ - @mastra/core@0.5.0-alpha.5
63
+
64
+ ## 0.2.0-alpha.4
65
+
66
+ ### Patch Changes
67
+
68
+ - Updated dependencies [d79aedf]
69
+ - @mastra/core@0.5.0-alpha.4
70
+
71
+ ## 0.2.0-alpha.3
72
+
73
+ ### Patch Changes
74
+
75
+ - Updated dependencies [3d0e290]
76
+ - @mastra/core@0.5.0-alpha.3
77
+
78
+ ## 0.2.0-alpha.2
79
+
80
+ ### Patch Changes
81
+
82
+ - Updated dependencies [02ffb7b]
83
+ - @mastra/core@0.5.0-alpha.2
84
+
85
+ ## 0.2.0-alpha.1
86
+
87
+ ### Patch Changes
88
+
89
+ - Updated dependencies [dab255b]
90
+ - @mastra/core@0.5.0-alpha.1
91
+
3
92
  ## 0.2.0-alpha.0
4
93
 
5
94
  ### Minor Changes
@@ -1,8 +1,8 @@
1
+ import type { AiMessageType } from '@mastra/core';
1
2
  import type { CoreMessage } from '@mastra/core';
2
3
  import type { CoreTool } from '@mastra/core';
3
4
  import { MastraMemory } from '@mastra/core/memory';
4
5
  import type { MemoryConfig } from '@mastra/core/memory';
5
- import type { Message } from 'ai';
6
6
  import type { MessageType } from '@mastra/core/memory';
7
7
  import type { SharedMemoryConfig } from '@mastra/core/memory';
8
8
  import type { StorageGetMessagesArg } from '@mastra/core/storage';
@@ -14,17 +14,20 @@ import type { StorageThreadType } from '@mastra/core/memory';
14
14
  */
15
15
  export declare class Memory extends MastraMemory {
16
16
  constructor(config?: SharedMemoryConfig);
17
- query({ threadId, selectBy, threadConfig, }: StorageGetMessagesArg): Promise<{
17
+ private validateThreadIsOwnedByResource;
18
+ query({ threadId, resourceId, selectBy, threadConfig, }: StorageGetMessagesArg): Promise<{
18
19
  messages: CoreMessage[];
19
- uiMessages: Message[];
20
+ uiMessages: AiMessageType[];
20
21
  }>;
21
- rememberMessages({ threadId, vectorMessageSearch, config, }: {
22
+ rememberMessages({ threadId, resourceId, vectorMessageSearch, config, }: {
22
23
  threadId: string;
24
+ resourceId?: string;
23
25
  vectorMessageSearch?: string;
24
26
  config?: MemoryConfig;
25
27
  }): Promise<{
28
+ threadId: string;
26
29
  messages: CoreMessage[];
27
- uiMessages: Message[];
30
+ uiMessages: AiMessageType[];
28
31
  }>;
29
32
  getThreadById({ threadId }: {
30
33
  threadId: string;
@@ -1,8 +1,8 @@
1
+ import type { AiMessageType } from '@mastra/core';
1
2
  import type { CoreMessage } from '@mastra/core';
2
3
  import type { CoreTool } from '@mastra/core';
3
4
  import { MastraMemory } from '@mastra/core/memory';
4
5
  import type { MemoryConfig } from '@mastra/core/memory';
5
- import type { Message } from 'ai';
6
6
  import type { MessageType } from '@mastra/core/memory';
7
7
  import type { SharedMemoryConfig } from '@mastra/core/memory';
8
8
  import type { StorageGetMessagesArg } from '@mastra/core/storage';
@@ -14,17 +14,20 @@ import type { StorageThreadType } from '@mastra/core/memory';
14
14
  */
15
15
  export declare class Memory extends MastraMemory {
16
16
  constructor(config?: SharedMemoryConfig);
17
- query({ threadId, selectBy, threadConfig, }: StorageGetMessagesArg): Promise<{
17
+ private validateThreadIsOwnedByResource;
18
+ query({ threadId, resourceId, selectBy, threadConfig, }: StorageGetMessagesArg): Promise<{
18
19
  messages: CoreMessage[];
19
- uiMessages: Message[];
20
+ uiMessages: AiMessageType[];
20
21
  }>;
21
- rememberMessages({ threadId, vectorMessageSearch, config, }: {
22
+ rememberMessages({ threadId, resourceId, vectorMessageSearch, config, }: {
22
23
  threadId: string;
24
+ resourceId?: string;
23
25
  vectorMessageSearch?: string;
24
26
  config?: MemoryConfig;
25
27
  }): Promise<{
28
+ threadId: string;
26
29
  messages: CoreMessage[];
27
- uiMessages: Message[];
30
+ uiMessages: AiMessageType[];
28
31
  }>;
29
32
  getThreadById({ threadId }: {
30
33
  threadId: string;
package/dist/index.cjs CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  var core = require('@mastra/core');
4
4
  var memory = require('@mastra/core/memory');
5
- var zod = require('zod');
6
5
  var ai = require('ai');
6
+ var zod = require('zod');
7
7
 
8
8
  // src/index.ts
9
9
  var updateWorkingMemoryTool = {
@@ -11,7 +11,7 @@ var updateWorkingMemoryTool = {
11
11
  parameters: zod.z.object({
12
12
  memory: zod.z.string().describe("The XML-formatted working memory content to store")
13
13
  }),
14
- execute: async (params, _options) => {
14
+ execute: async (params) => {
15
15
  const { context, threadId, memory } = params;
16
16
  if (!threadId || !memory) {
17
17
  throw new Error("Thread ID and Memory instance are required for working memory updates");
@@ -32,6 +32,8 @@ var updateWorkingMemoryTool = {
32
32
  return { success: true };
33
33
  }
34
34
  };
35
+
36
+ // src/index.ts
35
37
  var Memory = class extends memory.MastraMemory {
36
38
  constructor(config = {}) {
37
39
  super({ name: "Memory", ...config });
@@ -43,11 +45,24 @@ var Memory = class extends memory.MastraMemory {
43
45
  });
44
46
  this.threadConfig = mergedConfig;
45
47
  }
48
+ async validateThreadIsOwnedByResource(threadId, resourceId) {
49
+ const thread = await this.storage.getThreadById({ threadId });
50
+ if (!thread) {
51
+ throw new Error(`No thread found with id ${threadId}`);
52
+ }
53
+ if (thread.resourceId !== resourceId) {
54
+ throw new Error(
55
+ `Thread with id ${threadId} is for resource with id ${thread.resourceId} but resource ${resourceId} was queried.`
56
+ );
57
+ }
58
+ }
46
59
  async query({
47
60
  threadId,
61
+ resourceId,
48
62
  selectBy,
49
63
  threadConfig
50
64
  }) {
65
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
51
66
  let vectorResults = null;
52
67
  this.logger.debug(`Memory query() with:`, {
53
68
  threadId,
@@ -97,14 +112,17 @@ var Memory = class extends memory.MastraMemory {
97
112
  }
98
113
  async rememberMessages({
99
114
  threadId,
115
+ resourceId,
100
116
  vectorMessageSearch,
101
117
  config
102
118
  }) {
119
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
103
120
  const threadConfig = this.getMergedThreadConfig(config || {});
104
121
  if (!threadConfig.lastMessages && !threadConfig.semanticRecall) {
105
122
  return {
106
123
  messages: [],
107
- uiMessages: []
124
+ uiMessages: [],
125
+ threadId
108
126
  };
109
127
  }
110
128
  const messages = await this.query({
@@ -116,7 +134,11 @@ var Memory = class extends memory.MastraMemory {
116
134
  threadConfig: config
117
135
  });
118
136
  this.logger.debug(`Remembered message history includes ${messages.messages.length} messages.`);
119
- return messages;
137
+ return {
138
+ threadId,
139
+ messages: messages.messages,
140
+ uiMessages: messages.uiMessages
141
+ };
120
142
  }
121
143
  async getThreadById({ threadId }) {
122
144
  return this.storage.__getThreadById({ threadId });
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { deepMerge } from '@mastra/core';
2
2
  import { MastraMemory } from '@mastra/core/memory';
3
- import { z } from 'zod';
4
3
  import { embed } from 'ai';
4
+ import { z } from 'zod';
5
5
 
6
6
  // src/index.ts
7
7
  var updateWorkingMemoryTool = {
@@ -9,7 +9,7 @@ var updateWorkingMemoryTool = {
9
9
  parameters: z.object({
10
10
  memory: z.string().describe("The XML-formatted working memory content to store")
11
11
  }),
12
- execute: async (params, _options) => {
12
+ execute: async (params) => {
13
13
  const { context, threadId, memory } = params;
14
14
  if (!threadId || !memory) {
15
15
  throw new Error("Thread ID and Memory instance are required for working memory updates");
@@ -30,6 +30,8 @@ var updateWorkingMemoryTool = {
30
30
  return { success: true };
31
31
  }
32
32
  };
33
+
34
+ // src/index.ts
33
35
  var Memory = class extends MastraMemory {
34
36
  constructor(config = {}) {
35
37
  super({ name: "Memory", ...config });
@@ -41,11 +43,24 @@ var Memory = class extends MastraMemory {
41
43
  });
42
44
  this.threadConfig = mergedConfig;
43
45
  }
46
+ async validateThreadIsOwnedByResource(threadId, resourceId) {
47
+ const thread = await this.storage.getThreadById({ threadId });
48
+ if (!thread) {
49
+ throw new Error(`No thread found with id ${threadId}`);
50
+ }
51
+ if (thread.resourceId !== resourceId) {
52
+ throw new Error(
53
+ `Thread with id ${threadId} is for resource with id ${thread.resourceId} but resource ${resourceId} was queried.`
54
+ );
55
+ }
56
+ }
44
57
  async query({
45
58
  threadId,
59
+ resourceId,
46
60
  selectBy,
47
61
  threadConfig
48
62
  }) {
63
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
49
64
  let vectorResults = null;
50
65
  this.logger.debug(`Memory query() with:`, {
51
66
  threadId,
@@ -95,14 +110,17 @@ var Memory = class extends MastraMemory {
95
110
  }
96
111
  async rememberMessages({
97
112
  threadId,
113
+ resourceId,
98
114
  vectorMessageSearch,
99
115
  config
100
116
  }) {
117
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
101
118
  const threadConfig = this.getMergedThreadConfig(config || {});
102
119
  if (!threadConfig.lastMessages && !threadConfig.semanticRecall) {
103
120
  return {
104
121
  messages: [],
105
- uiMessages: []
122
+ uiMessages: [],
123
+ threadId
106
124
  };
107
125
  }
108
126
  const messages = await this.query({
@@ -114,7 +132,11 @@ var Memory = class extends MastraMemory {
114
132
  threadConfig: config
115
133
  });
116
134
  this.logger.debug(`Remembered message history includes ${messages.messages.length} messages.`);
117
- return messages;
135
+ return {
136
+ threadId,
137
+ messages: messages.messages,
138
+ uiMessages: messages.uiMessages
139
+ };
118
140
  }
119
141
  async getThreadById({ threadId }) {
120
142
  return this.storage.__getThreadById({ threadId });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/memory",
3
- "version": "0.2.0-alpha.0",
3
+ "version": "0.2.0-alpha.10",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -49,7 +49,7 @@
49
49
  "postgres": "^3.4.5",
50
50
  "redis": "^4.7.0",
51
51
  "zod": "^3.24.1",
52
- "@mastra/core": "^0.5.0-alpha.0"
52
+ "@mastra/core": "^0.5.0-alpha.10"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@microsoft/api-extractor": "^7.49.2",
package/src/index.ts CHANGED
@@ -1,11 +1,10 @@
1
1
  import { deepMerge } from '@mastra/core';
2
- import type { CoreMessage, CoreTool } from '@mastra/core';
2
+ import type { AiMessageType, CoreMessage, CoreTool } from '@mastra/core';
3
3
  import { MastraMemory } from '@mastra/core/memory';
4
4
  import type { MessageType, MemoryConfig, SharedMemoryConfig, StorageThreadType } from '@mastra/core/memory';
5
5
  import type { StorageGetMessagesArg } from '@mastra/core/storage';
6
- import { updateWorkingMemoryTool } from './tools/working-memory';
7
6
  import { embed } from 'ai';
8
- import type { Message as AiMessage } from 'ai';
7
+ import { updateWorkingMemoryTool } from './tools/working-memory';
9
8
 
10
9
  /**
11
10
  * Concrete implementation of MastraMemory that adds support for thread configuration
@@ -24,11 +23,26 @@ export class Memory extends MastraMemory {
24
23
  this.threadConfig = mergedConfig;
25
24
  }
26
25
 
26
+ private async validateThreadIsOwnedByResource(threadId: string, resourceId: string) {
27
+ const thread = await this.storage.getThreadById({ threadId });
28
+ if (!thread) {
29
+ throw new Error(`No thread found with id ${threadId}`);
30
+ }
31
+ if (thread.resourceId !== resourceId) {
32
+ throw new Error(
33
+ `Thread with id ${threadId} is for resource with id ${thread.resourceId} but resource ${resourceId} was queried.`,
34
+ );
35
+ }
36
+ }
37
+
27
38
  async query({
28
39
  threadId,
40
+ resourceId,
29
41
  selectBy,
30
42
  threadConfig,
31
- }: StorageGetMessagesArg): Promise<{ messages: CoreMessage[]; uiMessages: AiMessage[] }> {
43
+ }: StorageGetMessagesArg): Promise<{ messages: CoreMessage[]; uiMessages: AiMessageType[] }> {
44
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
45
+
32
46
  let vectorResults:
33
47
  | null
34
48
  | {
@@ -108,20 +122,29 @@ export class Memory extends MastraMemory {
108
122
 
109
123
  async rememberMessages({
110
124
  threadId,
125
+ resourceId,
111
126
  vectorMessageSearch,
112
127
  config,
113
128
  }: {
114
129
  threadId: string;
130
+ resourceId?: string;
115
131
  vectorMessageSearch?: string;
116
132
  config?: MemoryConfig;
117
- }) {
133
+ }): Promise<{
134
+ threadId: string;
135
+ messages: CoreMessage[];
136
+ uiMessages: AiMessageType[];
137
+ }> {
138
+ if (resourceId) await this.validateThreadIsOwnedByResource(threadId, resourceId);
139
+
118
140
  const threadConfig = this.getMergedThreadConfig(config || {});
119
141
 
120
142
  if (!threadConfig.lastMessages && !threadConfig.semanticRecall) {
121
143
  return {
122
144
  messages: [],
123
145
  uiMessages: [],
124
- } satisfies Awaited<ReturnType<typeof this.query>>;
146
+ threadId,
147
+ };
125
148
  }
126
149
 
127
150
  const messages = await this.query({
@@ -134,7 +157,11 @@ export class Memory extends MastraMemory {
134
157
  });
135
158
 
136
159
  this.logger.debug(`Remembered message history includes ${messages.messages.length} messages.`);
137
- return messages;
160
+ return {
161
+ threadId,
162
+ messages: messages.messages,
163
+ uiMessages: messages.uiMessages,
164
+ };
138
165
  }
139
166
 
140
167
  async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
@@ -1,5 +1,4 @@
1
1
  import type { CoreTool } from '@mastra/core';
2
- import type { ToolExecutionOptions } from 'ai';
3
2
  import { z } from 'zod';
4
3
 
5
4
  export const updateWorkingMemoryTool: CoreTool = {
@@ -7,7 +6,7 @@ export const updateWorkingMemoryTool: CoreTool = {
7
6
  parameters: z.object({
8
7
  memory: z.string().describe('The XML-formatted working memory content to store'),
9
8
  }),
10
- execute: async (params: any, _options: ToolExecutionOptions) => {
9
+ execute: async (params: any) => {
11
10
  const { context, threadId, memory } = params;
12
11
  if (!threadId || !memory) {
13
12
  throw new Error('Thread ID and Memory instance are required for working memory updates');