@mastra/longmemeval 0.0.0-bundle-recursion-20251030002519 → 0.0.0-bundle-version-20260121091645
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/CHANGELOG.md +566 -7
- package/package.json +12 -10
- package/scripts/generate-wm-templates.ts +2 -1
- package/src/commands/__tests__/prepare.test.ts +11 -6
- package/src/commands/__tests__/run.test.ts +10 -8
- package/src/commands/prepare.ts +2 -1
- package/src/commands/run.ts +4 -2
- package/src/storage/__tests__/benchmark-store.test.ts +18 -35
- package/src/storage/benchmark-store.ts +126 -124
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/longmemeval",
|
|
3
|
-
"version": "0.0.0-bundle-
|
|
3
|
+
"version": "0.0.0-bundle-version-20260121091645",
|
|
4
4
|
"description": "LongMemEval benchmark implementation for Mastra Memory",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@ai-sdk/openai": "^1.3.23",
|
|
@@ -16,21 +16,23 @@
|
|
|
16
16
|
"openai": "^4.73.1",
|
|
17
17
|
"ora": "^8.1.1",
|
|
18
18
|
"zod": "^3.23.8",
|
|
19
|
-
"@mastra/core": "0.0.0-bundle-
|
|
20
|
-
"@mastra/libsql": "0.0.0-bundle-
|
|
21
|
-
"@mastra/memory": "0.0.0-bundle-
|
|
22
|
-
"@mastra/
|
|
23
|
-
"@mastra/
|
|
19
|
+
"@mastra/core": "0.0.0-bundle-version-20260121091645",
|
|
20
|
+
"@mastra/libsql": "0.0.0-bundle-version-20260121091645",
|
|
21
|
+
"@mastra/memory": "0.0.0-bundle-version-20260121091645",
|
|
22
|
+
"@mastra/fastembed": "1.0.0",
|
|
23
|
+
"@mastra/rag": "0.0.0-bundle-version-20260121091645"
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@ai-sdk/google": "^1.2.19",
|
|
27
|
-
"@types/node": "
|
|
27
|
+
"@types/node": "22.13.17",
|
|
28
|
+
"@vitest/coverage-v8": "4.0.12",
|
|
29
|
+
"@vitest/ui": "4.0.12",
|
|
28
30
|
"tsx": "^4.19.2",
|
|
29
|
-
"typescript": "^5.
|
|
30
|
-
"vitest": "
|
|
31
|
+
"typescript": "^5.9.3",
|
|
32
|
+
"vitest": "4.0.16"
|
|
31
33
|
},
|
|
32
34
|
"engines": {
|
|
33
|
-
"node": ">=
|
|
35
|
+
"node": ">=22.13.0"
|
|
34
36
|
},
|
|
35
37
|
"homepage": "https://mastra.ai",
|
|
36
38
|
"repository": {
|
|
@@ -24,7 +24,8 @@ interface TemplateDatabase {
|
|
|
24
24
|
async function generateTemplate(question: LongMemEvalQuestion): Promise<string> {
|
|
25
25
|
// Create a simple agent for template generation
|
|
26
26
|
const agent = new Agent({
|
|
27
|
-
|
|
27
|
+
id: 'template-generator',
|
|
28
|
+
name: 'Template Generator',
|
|
28
29
|
instructions: `You are an expert at designing working memory templates for AI assistants.
|
|
29
30
|
|
|
30
31
|
Given a question and answer from a conversation history benchmark, generate a working memory instruction that would help an AI assistant extract and save the specific information needed to answer the question correctly.
|
|
@@ -15,9 +15,10 @@ vi.mock('@ai-sdk/openai', () => ({
|
|
|
15
15
|
}));
|
|
16
16
|
|
|
17
17
|
// Mock the DatasetLoader
|
|
18
|
-
vi.mock('../../data/loader', () =>
|
|
19
|
-
|
|
20
|
-
|
|
18
|
+
vi.mock('../../data/loader', () => {
|
|
19
|
+
// Use a class for constructor (Vitest v4 requirement)
|
|
20
|
+
class MockDatasetLoader {
|
|
21
|
+
loadDataset = vi.fn().mockResolvedValue([
|
|
21
22
|
{
|
|
22
23
|
question_id: 'test-q1',
|
|
23
24
|
question_type: 'single-session-user',
|
|
@@ -54,9 +55,13 @@ vi.mock('../../data/loader', () => ({
|
|
|
54
55
|
],
|
|
55
56
|
answer_session_ids: ['session-3'],
|
|
56
57
|
},
|
|
57
|
-
] as LongMemEvalQuestion[])
|
|
58
|
-
}
|
|
59
|
-
|
|
58
|
+
] as LongMemEvalQuestion[]);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
return {
|
|
62
|
+
DatasetLoader: MockDatasetLoader,
|
|
63
|
+
};
|
|
64
|
+
});
|
|
60
65
|
|
|
61
66
|
// Mock chalk and ora to avoid console output in tests
|
|
62
67
|
vi.mock('chalk', () => ({
|
|
@@ -28,11 +28,16 @@ vi.mock('@ai-sdk/openai', () => ({
|
|
|
28
28
|
}));
|
|
29
29
|
|
|
30
30
|
// Mock the LongMemEvalMetric
|
|
31
|
-
vi.mock('../../evaluation/longmemeval-metric', () =>
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
}
|
|
31
|
+
vi.mock('../../evaluation/longmemeval-metric', () => {
|
|
32
|
+
// Use a class for constructor (Vitest v4 requirement)
|
|
33
|
+
class MockLongMemEvalMetric {
|
|
34
|
+
measure = vi.fn().mockResolvedValue({ score: 1 }); // Always returns correct
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
LongMemEvalMetric: MockLongMemEvalMetric,
|
|
39
|
+
};
|
|
40
|
+
});
|
|
36
41
|
|
|
37
42
|
// Mock chalk and ora
|
|
38
43
|
vi.mock('chalk', () => ({
|
|
@@ -148,7 +153,6 @@ describe('RunCommand', () => {
|
|
|
148
153
|
],
|
|
149
154
|
mastra_resources: [],
|
|
150
155
|
mastra_workflow_snapshot: [],
|
|
151
|
-
mastra_evals: [],
|
|
152
156
|
mastra_traces: [],
|
|
153
157
|
}),
|
|
154
158
|
);
|
|
@@ -225,7 +229,6 @@ describe('RunCommand', () => {
|
|
|
225
229
|
],
|
|
226
230
|
mastra_resources: [],
|
|
227
231
|
mastra_workflow_snapshot: [],
|
|
228
|
-
mastra_evals: [],
|
|
229
232
|
mastra_traces: [],
|
|
230
233
|
}),
|
|
231
234
|
);
|
|
@@ -322,7 +325,6 @@ describe('RunCommand', () => {
|
|
|
322
325
|
],
|
|
323
326
|
mastra_resources: [],
|
|
324
327
|
mastra_workflow_snapshot: [],
|
|
325
|
-
mastra_evals: [],
|
|
326
328
|
mastra_traces: [],
|
|
327
329
|
}),
|
|
328
330
|
);
|
package/src/commands/prepare.ts
CHANGED
|
@@ -475,7 +475,8 @@ export class PrepareCommand {
|
|
|
475
475
|
|
|
476
476
|
// Create agent with appropriate model
|
|
477
477
|
const agent = new Agent({
|
|
478
|
-
|
|
478
|
+
id: 'prep-agent',
|
|
479
|
+
name: 'Prep Agent',
|
|
479
480
|
instructions:
|
|
480
481
|
"You are a helpful assistant. Process and store conversation history. Only store working memory information if it's in the template. Other information is not relevant",
|
|
481
482
|
model: model,
|
package/src/commands/run.ts
CHANGED
|
@@ -335,7 +335,8 @@ For example, if the user previously mentioned they prefer a specific software, t
|
|
|
335
335
|
Be specific rather than generic when the user has expressed clear preferences in past conversations. If there is a clear preference, focus in on that, and do not add additional irrelevant information.`;
|
|
336
336
|
|
|
337
337
|
const agent = new Agent({
|
|
338
|
-
|
|
338
|
+
id: 'longmemeval-agent',
|
|
339
|
+
name: 'LongMemEval Agent',
|
|
339
340
|
model: modelProvider,
|
|
340
341
|
instructions: agentInstructions,
|
|
341
342
|
memory,
|
|
@@ -354,7 +355,8 @@ Be specific rather than generic when the user has expressed clear preferences in
|
|
|
354
355
|
});
|
|
355
356
|
|
|
356
357
|
const evalAgent = new Agent({
|
|
357
|
-
|
|
358
|
+
id: 'longmemeval-metric-agent',
|
|
359
|
+
name: 'LongMemEval Metric Agent',
|
|
358
360
|
model: retry4o.model,
|
|
359
361
|
instructions: 'You are an evaluation assistant. Answer questions precisely and concisely.',
|
|
360
362
|
});
|
|
@@ -22,13 +22,6 @@ describe('BenchmarkStore', () => {
|
|
|
22
22
|
}
|
|
23
23
|
});
|
|
24
24
|
|
|
25
|
-
describe('supports', () => {
|
|
26
|
-
it('should support resource scope and working memory', () => {
|
|
27
|
-
expect(store.supports.selectByIncludeResourceScope).toBe(true);
|
|
28
|
-
expect(store.supports.resourceWorkingMemory).toBe(true);
|
|
29
|
-
});
|
|
30
|
-
});
|
|
31
|
-
|
|
32
25
|
describe('persist', () => {
|
|
33
26
|
it('should save store data to a JSON file', async () => {
|
|
34
27
|
// Add some test data
|
|
@@ -117,9 +110,9 @@ describe('BenchmarkStore', () => {
|
|
|
117
110
|
expect(restoredThread).toBeTruthy();
|
|
118
111
|
expect(restoredThread?.title).toBe('Test Thread');
|
|
119
112
|
|
|
120
|
-
const
|
|
121
|
-
expect(
|
|
122
|
-
expect(
|
|
113
|
+
const result = await store2.listMessages({ threadId: 'test-thread-1' });
|
|
114
|
+
expect(result.messages).toHaveLength(1);
|
|
115
|
+
expect(result.messages[0].content).toMatchObject({ text: 'Hello' });
|
|
123
116
|
});
|
|
124
117
|
|
|
125
118
|
it('should throw error if file does not exist', async () => {
|
|
@@ -184,24 +177,22 @@ describe('BenchmarkStore', () => {
|
|
|
184
177
|
],
|
|
185
178
|
});
|
|
186
179
|
|
|
187
|
-
// Query using
|
|
188
|
-
const
|
|
180
|
+
// Query using include to get messages from different threads
|
|
181
|
+
const result = await store.listMessages({
|
|
189
182
|
threadId: 'thread-1',
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
],
|
|
199
|
-
},
|
|
183
|
+
include: [
|
|
184
|
+
{
|
|
185
|
+
id: 'msg-2',
|
|
186
|
+
threadId: 'thread-2', // Different thread!
|
|
187
|
+
withPreviousMessages: 0,
|
|
188
|
+
withNextMessages: 1,
|
|
189
|
+
},
|
|
190
|
+
],
|
|
200
191
|
});
|
|
201
192
|
|
|
202
|
-
expect(messages).toHaveLength(2);
|
|
203
|
-
expect(messages[0].content).
|
|
204
|
-
expect(messages[1].content).
|
|
193
|
+
expect(result.messages).toHaveLength(2);
|
|
194
|
+
expect(result.messages[0].content).toMatchObject({ text: 'Message in thread 2' });
|
|
195
|
+
expect(result.messages[1].content).toMatchObject({ text: 'Response in thread 2' });
|
|
205
196
|
});
|
|
206
197
|
});
|
|
207
198
|
|
|
@@ -260,19 +251,11 @@ describe('BenchmarkStore', () => {
|
|
|
260
251
|
|
|
261
252
|
describe('getting messages', () => {
|
|
262
253
|
it('should throw when threadId is an empty string or whitespace only', async () => {
|
|
263
|
-
await expect(() => store.
|
|
264
|
-
'threadId must be a non-empty string',
|
|
265
|
-
);
|
|
266
|
-
|
|
267
|
-
await expect(() => store.getMessagesPaginated({ threadId: '' })).rejects.toThrowError(
|
|
268
|
-
'threadId must be a non-empty string',
|
|
269
|
-
);
|
|
270
|
-
|
|
271
|
-
await expect(() => store.getMessages({ threadId: ' ' })).rejects.toThrowError(
|
|
254
|
+
await expect(() => store.listMessages({ threadId: '' })).rejects.toThrowError(
|
|
272
255
|
'threadId must be a non-empty string',
|
|
273
256
|
);
|
|
274
257
|
|
|
275
|
-
await expect(() => store.
|
|
258
|
+
await expect(() => store.listMessages({ threadId: ' ' })).rejects.toThrowError(
|
|
276
259
|
'threadId must be a non-empty string',
|
|
277
260
|
);
|
|
278
261
|
});
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { MastraStorage } from '@mastra/core/storage';
|
|
2
2
|
import { MessageList } from '@mastra/core/agent';
|
|
3
|
-
import type {
|
|
4
|
-
import type { MastraMessageV1, StorageThreadType } from '@mastra/core/memory';
|
|
3
|
+
import type { MastraMessageV1, MastraDBMessage, StorageThreadType } from '@mastra/core/memory';
|
|
5
4
|
import type {
|
|
6
5
|
TABLE_NAMES,
|
|
7
6
|
StorageColumn,
|
|
8
|
-
StorageGetMessagesArg,
|
|
9
7
|
StorageResourceType,
|
|
10
|
-
EvalRow,
|
|
11
8
|
WorkflowRun,
|
|
12
9
|
WorkflowRuns,
|
|
13
|
-
|
|
10
|
+
StorageListWorkflowRunsInput,
|
|
11
|
+
StorageListThreadsInput,
|
|
12
|
+
StorageListThreadsOutput,
|
|
13
|
+
StorageListMessagesInput,
|
|
14
|
+
StorageListMessagesOutput,
|
|
14
15
|
} from '@mastra/core/storage';
|
|
15
16
|
import { writeFile, readFile } from 'fs/promises';
|
|
16
17
|
import { existsSync } from 'fs';
|
|
@@ -20,7 +21,6 @@ type DBMode = 'read' | 'read-write';
|
|
|
20
21
|
export class BenchmarkStore extends MastraStorage {
|
|
21
22
|
private data: Record<TABLE_NAMES, Map<string, any>> = {
|
|
22
23
|
mastra_workflow_snapshot: new Map(),
|
|
23
|
-
mastra_evals: new Map(),
|
|
24
24
|
mastra_messages: new Map(),
|
|
25
25
|
mastra_threads: new Map(),
|
|
26
26
|
mastra_traces: new Map(),
|
|
@@ -78,16 +78,6 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
78
78
|
return thread || null;
|
|
79
79
|
}
|
|
80
80
|
|
|
81
|
-
async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
|
|
82
|
-
const threads: StorageThreadType[] = [];
|
|
83
|
-
for (const thread of this.data.mastra_threads.values()) {
|
|
84
|
-
if (thread.resourceId === resourceId) {
|
|
85
|
-
threads.push(thread);
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
return threads;
|
|
89
|
-
}
|
|
90
|
-
|
|
91
81
|
async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
|
|
92
82
|
this.data.mastra_threads.set(thread.id, thread);
|
|
93
83
|
return thread;
|
|
@@ -176,76 +166,11 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
176
166
|
return resource;
|
|
177
167
|
}
|
|
178
168
|
|
|
179
|
-
async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
|
|
180
|
-
async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
181
|
-
async getMessages(
|
|
182
|
-
args: StorageGetMessagesArg & { format?: 'v1' | 'v2' },
|
|
183
|
-
): Promise<MastraMessageV1[] | MastraMessageV2[]> {
|
|
184
|
-
const { threadId, resourceId, selectBy, format = 'v1' } = args;
|
|
185
|
-
if (!threadId.trim()) throw new Error('threadId must be a non-empty string');
|
|
186
|
-
|
|
187
|
-
let messages: any[] = [];
|
|
188
|
-
const includedMessageIds = new Set<string>();
|
|
189
|
-
|
|
190
|
-
// First, handle selectBy.include for cross-thread queries (resource scope support)
|
|
191
|
-
if (selectBy?.include?.length) {
|
|
192
|
-
for (const inc of selectBy.include) {
|
|
193
|
-
// Use the included threadId if provided (resource scope), otherwise use main threadId
|
|
194
|
-
const queryThreadId = inc.threadId || threadId;
|
|
195
|
-
|
|
196
|
-
// Get the target message and surrounding context
|
|
197
|
-
const threadMessages = Array.from(this.data.mastra_messages.values())
|
|
198
|
-
.filter((msg: any) => msg.threadId === queryThreadId)
|
|
199
|
-
.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
200
|
-
|
|
201
|
-
const targetIndex = threadMessages.findIndex((msg: any) => msg.id === inc.id);
|
|
202
|
-
|
|
203
|
-
if (targetIndex >= 0) {
|
|
204
|
-
const startIdx = Math.max(0, targetIndex - (inc.withPreviousMessages || 0));
|
|
205
|
-
const endIdx = Math.min(threadMessages.length, targetIndex + (inc.withNextMessages || 0) + 1);
|
|
206
|
-
|
|
207
|
-
for (let i = startIdx; i < endIdx; i++) {
|
|
208
|
-
includedMessageIds.add(threadMessages[i].id);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// Get base messages for the thread
|
|
215
|
-
let baseMessages: any[] = [];
|
|
216
|
-
if (threadId || resourceId) {
|
|
217
|
-
baseMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => {
|
|
218
|
-
if (threadId && msg.threadId !== threadId) return false;
|
|
219
|
-
if (resourceId && msg.resourceId !== resourceId) return false;
|
|
220
|
-
return true;
|
|
221
|
-
});
|
|
222
|
-
|
|
223
|
-
// Apply selectBy.last to base messages only
|
|
224
|
-
if (selectBy?.last) {
|
|
225
|
-
// Sort first to ensure we get the actual last messages
|
|
226
|
-
baseMessages.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
227
|
-
baseMessages = baseMessages.slice(-selectBy.last);
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
// Combine base messages with included messages
|
|
232
|
-
const baseMessageIds = new Set(baseMessages.map((m: any) => m.id));
|
|
233
|
-
const allMessageIds = new Set([...baseMessageIds, ...includedMessageIds]);
|
|
234
|
-
|
|
235
|
-
// Get all unique messages
|
|
236
|
-
messages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => allMessageIds.has(msg.id));
|
|
237
|
-
// Sort by createdAt
|
|
238
|
-
messages.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
239
|
-
|
|
240
|
-
const list = new MessageList().add(messages, 'memory');
|
|
241
|
-
return format === 'v2' ? list.get.all.v2() : list.get.all.v1();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
169
|
async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
|
|
245
|
-
async saveMessages(args: { messages:
|
|
170
|
+
async saveMessages(args: { messages: MastraDBMessage[]; format: 'v2' }): Promise<MastraDBMessage[]>;
|
|
246
171
|
async saveMessages(
|
|
247
|
-
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages:
|
|
248
|
-
): Promise<
|
|
172
|
+
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraDBMessage[]; format: 'v2' },
|
|
173
|
+
): Promise<MastraDBMessage[] | MastraMessageV1[]> {
|
|
249
174
|
if (this.mode === `read`) return [];
|
|
250
175
|
|
|
251
176
|
const { messages, format = 'v1' } = args;
|
|
@@ -255,11 +180,11 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
255
180
|
}
|
|
256
181
|
|
|
257
182
|
const list = new MessageList().add(messages, 'memory');
|
|
258
|
-
return format === 'v2' ? list.get.all.
|
|
183
|
+
return format === 'v2' ? list.get.all.db() : list.get.all.v1();
|
|
259
184
|
}
|
|
260
185
|
|
|
261
|
-
async updateMessages(args: { messages: Partial<
|
|
262
|
-
const updatedMessages:
|
|
186
|
+
async updateMessages(args: { messages: Partial<MastraDBMessage> & { id: string }[] }): Promise<MastraDBMessage[]> {
|
|
187
|
+
const updatedMessages: MastraDBMessage[] = [];
|
|
263
188
|
|
|
264
189
|
if (this.mode === `read`) return [];
|
|
265
190
|
|
|
@@ -331,21 +256,14 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
331
256
|
return evals as EvalRow[];
|
|
332
257
|
}
|
|
333
258
|
|
|
334
|
-
async
|
|
259
|
+
async listWorkflowRuns({
|
|
335
260
|
workflowName,
|
|
336
261
|
fromDate,
|
|
337
262
|
toDate,
|
|
338
263
|
limit,
|
|
339
264
|
offset,
|
|
340
265
|
resourceId,
|
|
341
|
-
}: {
|
|
342
|
-
workflowName?: string;
|
|
343
|
-
fromDate?: Date;
|
|
344
|
-
toDate?: Date;
|
|
345
|
-
limit?: number;
|
|
346
|
-
offset?: number;
|
|
347
|
-
resourceId?: string;
|
|
348
|
-
} = {}): Promise<WorkflowRuns> {
|
|
266
|
+
}: StorageListWorkflowRunsInput = {}): Promise<WorkflowRuns> {
|
|
349
267
|
let runs = Array.from(this.data.mastra_workflow_snapshot.values());
|
|
350
268
|
|
|
351
269
|
if (workflowName) runs = runs.filter((run: any) => run.workflow_name === workflowName);
|
|
@@ -402,49 +320,133 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
402
320
|
return parsedRun as WorkflowRun;
|
|
403
321
|
}
|
|
404
322
|
|
|
405
|
-
async
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
323
|
+
async listThreads(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {
|
|
324
|
+
const { page = 0, perPage: perPageInput, filter, orderBy } = args;
|
|
325
|
+
let allThreads: StorageThreadType[] = Array.from(this.data.mastra_threads.values());
|
|
326
|
+
|
|
327
|
+
// Apply resourceId filter if provided
|
|
328
|
+
if (filter?.resourceId) {
|
|
329
|
+
allThreads = allThreads.filter(thread => thread.resourceId === filter.resourceId);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Apply metadata filter if provided (AND logic)
|
|
333
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
334
|
+
allThreads = allThreads.filter(thread => {
|
|
335
|
+
if (!thread.metadata) return false;
|
|
336
|
+
return Object.entries(filter.metadata!).every(([key, value]) => thread.metadata![key] === value);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
// Apply ordering - default to DESC by createdAt
|
|
341
|
+
const sortField = orderBy?.field || 'createdAt';
|
|
342
|
+
const sortDirection = orderBy?.direction || 'DESC';
|
|
343
|
+
const direction = sortDirection === 'ASC' ? 1 : -1;
|
|
344
|
+
|
|
345
|
+
allThreads.sort((a: any, b: any) => {
|
|
346
|
+
const aVal = a[sortField];
|
|
347
|
+
const bVal = b[sortField];
|
|
348
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
349
|
+
return direction * (aVal.getTime() - bVal.getTime());
|
|
350
|
+
}
|
|
351
|
+
return direction * (aVal < bVal ? -1 : aVal > bVal ? 1 : 0);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// Handle perPage: false (fetch all results)
|
|
355
|
+
const fetchAll = perPageInput === false;
|
|
356
|
+
const normalizedPerPage = fetchAll ? allThreads.length : typeof perPageInput === 'number' ? perPageInput : 100;
|
|
357
|
+
const normalizedPage = fetchAll ? 0 : Math.max(0, page);
|
|
358
|
+
const offset = normalizedPage * normalizedPerPage;
|
|
359
|
+
const threads = allThreads.slice(offset, fetchAll ? undefined : offset + normalizedPerPage);
|
|
413
360
|
|
|
414
361
|
return {
|
|
415
362
|
threads,
|
|
416
363
|
total: allThreads.length,
|
|
417
|
-
page:
|
|
418
|
-
perPage:
|
|
419
|
-
hasMore:
|
|
364
|
+
page: normalizedPage,
|
|
365
|
+
perPage: fetchAll ? false : normalizedPerPage,
|
|
366
|
+
hasMore: fetchAll ? false : offset + normalizedPerPage < allThreads.length,
|
|
420
367
|
};
|
|
421
368
|
}
|
|
422
369
|
|
|
423
|
-
async
|
|
424
|
-
|
|
425
|
-
): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
|
|
426
|
-
const { threadId, selectBy, format = 'v1' } = args;
|
|
370
|
+
async listMessages(args: StorageListMessagesInput): Promise<StorageListMessagesOutput> {
|
|
371
|
+
const { threadId, page = 0, perPage = 40, resourceId, filter, include, orderBy } = args;
|
|
427
372
|
if (!threadId.trim()) throw new Error('threadId must be a non-empty string');
|
|
428
373
|
|
|
429
|
-
|
|
374
|
+
let messages: any[] = [];
|
|
375
|
+
const includedMessageIds = new Set<string>();
|
|
376
|
+
|
|
377
|
+
// Handle include for cross-thread queries (resource scope support)
|
|
378
|
+
if (include?.length) {
|
|
379
|
+
for (const inc of include) {
|
|
380
|
+
// Use the included threadId if provided (resource scope), otherwise use main threadId
|
|
381
|
+
const queryThreadId = inc.threadId || threadId;
|
|
382
|
+
|
|
383
|
+
// Get the target message and surrounding context
|
|
384
|
+
const threadMessages = Array.from(this.data.mastra_messages.values())
|
|
385
|
+
.filter((msg: any) => msg.threadId === queryThreadId)
|
|
386
|
+
.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
387
|
+
|
|
388
|
+
const targetIndex = threadMessages.findIndex((msg: any) => msg.id === inc.id);
|
|
389
|
+
|
|
390
|
+
if (targetIndex >= 0) {
|
|
391
|
+
const startIdx = Math.max(0, targetIndex - (inc.withPreviousMessages || 0));
|
|
392
|
+
const endIdx = Math.min(threadMessages.length, targetIndex + (inc.withNextMessages || 0) + 1);
|
|
393
|
+
|
|
394
|
+
for (let i = startIdx; i < endIdx; i++) {
|
|
395
|
+
includedMessageIds.add(threadMessages[i].id);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// Get base messages for the thread
|
|
402
|
+
const baseMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => {
|
|
403
|
+
if (msg.threadId !== threadId) return false;
|
|
404
|
+
if (resourceId && msg.resourceId !== resourceId) return false;
|
|
405
|
+
return true;
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
// Combine base messages with included messages
|
|
409
|
+
const baseMessageIds = new Set(baseMessages.map((m: any) => m.id));
|
|
410
|
+
const allMessageIds = new Set([...baseMessageIds, ...includedMessageIds]);
|
|
411
|
+
|
|
412
|
+
// Get all unique messages and convert to v2 format
|
|
413
|
+
const allMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => allMessageIds.has(msg.id));
|
|
414
|
+
const list = new MessageList().add(allMessages, 'memory');
|
|
415
|
+
let filteredMessages = list.get.all.db();
|
|
430
416
|
|
|
431
|
-
//
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
417
|
+
// Apply date filters
|
|
418
|
+
if (filter?.dateRange?.start) {
|
|
419
|
+
filteredMessages = filteredMessages.filter((m: any) => new Date(m.createdAt) >= filter.dateRange!.start!);
|
|
420
|
+
}
|
|
421
|
+
if (filter?.dateRange?.end) {
|
|
422
|
+
filteredMessages = filteredMessages.filter((m: any) => new Date(m.createdAt) <= filter.dateRange!.end!);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
// Apply ordering - default to ASC by createdAt
|
|
426
|
+
const sortField = orderBy?.field || 'createdAt';
|
|
427
|
+
const sortDirection = orderBy?.direction || 'ASC';
|
|
428
|
+
const direction = sortDirection === 'ASC' ? 1 : -1;
|
|
429
|
+
|
|
430
|
+
filteredMessages.sort((a: any, b: any) => {
|
|
431
|
+
const aVal = a[sortField];
|
|
432
|
+
const bVal = b[sortField];
|
|
433
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
434
|
+
return direction * (aVal.getTime() - bVal.getTime());
|
|
435
|
+
}
|
|
436
|
+
return direction * (aVal < bVal ? -1 : aVal > bVal ? 1 : 0);
|
|
437
|
+
});
|
|
437
438
|
|
|
438
439
|
// Apply pagination
|
|
439
|
-
const
|
|
440
|
-
const
|
|
440
|
+
const normalizedPerPage = perPage === false ? filteredMessages.length : perPage;
|
|
441
|
+
const start = perPage === false ? 0 : page * normalizedPerPage;
|
|
442
|
+
messages = filteredMessages.slice(start, start + normalizedPerPage);
|
|
441
443
|
|
|
442
444
|
return {
|
|
443
|
-
messages,
|
|
444
|
-
total:
|
|
445
|
+
messages: messages as MastraDBMessage[],
|
|
446
|
+
total: filteredMessages.length,
|
|
445
447
|
page,
|
|
446
|
-
perPage,
|
|
447
|
-
hasMore:
|
|
448
|
+
perPage: perPage === false ? false : normalizedPerPage,
|
|
449
|
+
hasMore: perPage === false ? false : filteredMessages.length > (page + 1) * normalizedPerPage,
|
|
448
450
|
};
|
|
449
451
|
}
|
|
450
452
|
|