@mastra/longmemeval 0.0.0-error-handler-fix-20251020202607 → 0.0.0-execa-dynamic-import-20260304221256
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 +847 -6
- package/LICENSE.md +15 -0
- package/docs/INVESTIGATION_WORKFLOW.md +314 -0
- package/package.json +34 -26
- package/scripts/download.ts +22 -1
- package/scripts/generate-wm-templates.ts +2 -1
- package/src/cli.ts +967 -155
- package/src/commands/__tests__/prepare.test.ts +11 -6
- package/src/commands/__tests__/run.test.ts +10 -8
- package/src/commands/clean.ts +147 -0
- package/src/commands/deterministic-ids.ts +126 -0
- package/src/commands/find-prohibited.ts +299 -0
- package/src/commands/investigate.ts +2437 -0
- package/src/commands/list-partial.ts +137 -0
- package/src/commands/obscure-thread-ids.ts +237 -0
- package/src/commands/partial-results.ts +198 -0
- package/src/commands/precompute-embeddings.ts +302 -0
- package/src/commands/prepare.ts +732 -105
- package/src/commands/reconcile.ts +383 -0
- package/src/commands/run.ts +1240 -121
- package/src/commands/sessions.ts +126 -0
- package/src/commands/sync.ts +152 -0
- package/src/commands/test-cleaned.ts +53 -0
- package/src/commands/test-incremental.ts +120 -0
- package/src/commands/test-messages.ts +95 -0
- package/src/commands/test-prohibited.ts +244 -0
- package/src/commands/test-thread.ts +136 -0
- package/src/commands/tokens.ts +625 -0
- package/src/config.ts +1253 -72
- package/src/data/types.ts +48 -6
- package/src/embeddings/cached-openai-embedding-model.ts +0 -1
- package/src/evaluation/longmemeval-metric.ts +186 -103
- package/src/processors/content-sanitizer.ts +153 -0
- package/src/processors/date-injector.ts +111 -0
- package/src/processors/index.ts +3 -0
- package/src/processors/observation-semantic-filter.ts +1142 -0
- package/src/retry-model.ts +5 -3
- package/src/storage/__tests__/benchmark-store.test.ts +18 -35
- package/src/storage/benchmark-store.ts +140 -153
- package/src/storage/benchmark-vector.ts +1 -1
- package/src/storage/index.ts +1 -0
- package/src/storage/persistable-inmemory.ts +133 -0
- package/src/test-utils/mock-model.ts +35 -0
package/src/retry-model.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { wrapLanguageModel } from 'ai';
|
|
2
|
+
import { LanguageModelV2 } from '@ai-sdk/provider';
|
|
2
3
|
|
|
3
|
-
export function makeRetryModel(model:
|
|
4
|
+
export function makeRetryModel(model: LanguageModelV2) {
|
|
4
5
|
const state = {
|
|
5
6
|
rateLimitCount: 0,
|
|
6
7
|
pause: null as null | Promise<void>,
|
|
@@ -10,7 +11,8 @@ export function makeRetryModel(model: LanguageModel) {
|
|
|
10
11
|
const wrapped = wrapLanguageModel({
|
|
11
12
|
model,
|
|
12
13
|
middleware: {
|
|
13
|
-
wrapGenerate: async ({ doGenerate }) => {
|
|
14
|
+
wrapGenerate: async ({ doGenerate, params }) => {
|
|
15
|
+
// console.log('params', params);
|
|
14
16
|
if (state.pause) await state.pause;
|
|
15
17
|
const maxRetries = 10;
|
|
16
18
|
let retries = 0;
|
|
@@ -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,17 +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';
|
|
5
|
-
import type { Trace } from '@mastra/core/telemetry';
|
|
3
|
+
import type { MastraMessageV1, MastraDBMessage, StorageThreadType } from '@mastra/core/memory';
|
|
6
4
|
import type {
|
|
7
5
|
TABLE_NAMES,
|
|
8
6
|
StorageColumn,
|
|
9
|
-
StorageGetMessagesArg,
|
|
10
7
|
StorageResourceType,
|
|
11
|
-
EvalRow,
|
|
12
8
|
WorkflowRun,
|
|
13
9
|
WorkflowRuns,
|
|
14
|
-
|
|
10
|
+
StorageListWorkflowRunsInput,
|
|
11
|
+
StorageListThreadsInput,
|
|
12
|
+
StorageListThreadsOutput,
|
|
13
|
+
StorageListMessagesInput,
|
|
14
|
+
StorageListMessagesOutput,
|
|
15
15
|
} from '@mastra/core/storage';
|
|
16
16
|
import { writeFile, readFile } from 'fs/promises';
|
|
17
17
|
import { existsSync } from 'fs';
|
|
@@ -21,17 +21,28 @@ type DBMode = 'read' | 'read-write';
|
|
|
21
21
|
export class BenchmarkStore extends MastraStorage {
|
|
22
22
|
private data: Record<TABLE_NAMES, Map<string, any>> = {
|
|
23
23
|
mastra_workflow_snapshot: new Map(),
|
|
24
|
-
mastra_evals: new Map(),
|
|
25
24
|
mastra_messages: new Map(),
|
|
26
25
|
mastra_threads: new Map(),
|
|
27
26
|
mastra_traces: new Map(),
|
|
28
27
|
mastra_resources: new Map(),
|
|
28
|
+
mastra_scorers: new Map(),
|
|
29
|
+
mastra_ai_spans: new Map(),
|
|
30
|
+
mastra_agents: new Map(),
|
|
31
|
+
mastra_agent_versions: new Map(),
|
|
32
|
+
mastra_datasets: new Map(),
|
|
33
|
+
mastra_dataset_items: new Map(),
|
|
34
|
+
mastra_dataset_item_versions: new Map(),
|
|
35
|
+
mastra_dataset_versions: new Map(),
|
|
36
|
+
mastra_experiments: new Map(),
|
|
37
|
+
mastra_experiment_results: new Map(),
|
|
38
|
+
mastra_prompt_blocks: new Map(),
|
|
39
|
+
mastra_prompt_block_versions: new Map(),
|
|
29
40
|
};
|
|
30
41
|
|
|
31
42
|
private mode: DBMode;
|
|
32
43
|
|
|
33
44
|
constructor(mode: DBMode = 'read-write') {
|
|
34
|
-
super({ name: 'BenchmarkStore' });
|
|
45
|
+
super({ id: 'benchmark-store', name: 'BenchmarkStore' });
|
|
35
46
|
this.hasInitialized = Promise.resolve(true);
|
|
36
47
|
this.mode = mode;
|
|
37
48
|
}
|
|
@@ -40,6 +51,9 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
40
51
|
return {
|
|
41
52
|
selectByIncludeResourceScope: true,
|
|
42
53
|
resourceWorkingMemory: true,
|
|
54
|
+
hasColumn: true,
|
|
55
|
+
createTable: true,
|
|
56
|
+
deleteMessages: true,
|
|
43
57
|
};
|
|
44
58
|
}
|
|
45
59
|
|
|
@@ -79,16 +93,6 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
79
93
|
return thread || null;
|
|
80
94
|
}
|
|
81
95
|
|
|
82
|
-
async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
|
|
83
|
-
const threads: StorageThreadType[] = [];
|
|
84
|
-
for (const thread of this.data.mastra_threads.values()) {
|
|
85
|
-
if (thread.resourceId === resourceId) {
|
|
86
|
-
threads.push(thread);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
return threads;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
96
|
async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
|
|
93
97
|
this.data.mastra_threads.set(thread.id, thread);
|
|
94
98
|
return thread;
|
|
@@ -177,76 +181,11 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
177
181
|
return resource;
|
|
178
182
|
}
|
|
179
183
|
|
|
180
|
-
async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
|
|
181
|
-
async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
182
|
-
async getMessages(
|
|
183
|
-
args: StorageGetMessagesArg & { format?: 'v1' | 'v2' },
|
|
184
|
-
): Promise<MastraMessageV1[] | MastraMessageV2[]> {
|
|
185
|
-
const { threadId, resourceId, selectBy, format = 'v1' } = args;
|
|
186
|
-
if (!threadId.trim()) throw new Error('threadId must be a non-empty string');
|
|
187
|
-
|
|
188
|
-
let messages: any[] = [];
|
|
189
|
-
const includedMessageIds = new Set<string>();
|
|
190
|
-
|
|
191
|
-
// First, handle selectBy.include for cross-thread queries (resource scope support)
|
|
192
|
-
if (selectBy?.include?.length) {
|
|
193
|
-
for (const inc of selectBy.include) {
|
|
194
|
-
// Use the included threadId if provided (resource scope), otherwise use main threadId
|
|
195
|
-
const queryThreadId = inc.threadId || threadId;
|
|
196
|
-
|
|
197
|
-
// Get the target message and surrounding context
|
|
198
|
-
const threadMessages = Array.from(this.data.mastra_messages.values())
|
|
199
|
-
.filter((msg: any) => msg.threadId === queryThreadId)
|
|
200
|
-
.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
201
|
-
|
|
202
|
-
const targetIndex = threadMessages.findIndex((msg: any) => msg.id === inc.id);
|
|
203
|
-
|
|
204
|
-
if (targetIndex >= 0) {
|
|
205
|
-
const startIdx = Math.max(0, targetIndex - (inc.withPreviousMessages || 0));
|
|
206
|
-
const endIdx = Math.min(threadMessages.length, targetIndex + (inc.withNextMessages || 0) + 1);
|
|
207
|
-
|
|
208
|
-
for (let i = startIdx; i < endIdx; i++) {
|
|
209
|
-
includedMessageIds.add(threadMessages[i].id);
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// Get base messages for the thread
|
|
216
|
-
let baseMessages: any[] = [];
|
|
217
|
-
if (threadId || resourceId) {
|
|
218
|
-
baseMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => {
|
|
219
|
-
if (threadId && msg.threadId !== threadId) return false;
|
|
220
|
-
if (resourceId && msg.resourceId !== resourceId) return false;
|
|
221
|
-
return true;
|
|
222
|
-
});
|
|
223
|
-
|
|
224
|
-
// Apply selectBy.last to base messages only
|
|
225
|
-
if (selectBy?.last) {
|
|
226
|
-
// Sort first to ensure we get the actual last messages
|
|
227
|
-
baseMessages.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
228
|
-
baseMessages = baseMessages.slice(-selectBy.last);
|
|
229
|
-
}
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
// Combine base messages with included messages
|
|
233
|
-
const baseMessageIds = new Set(baseMessages.map((m: any) => m.id));
|
|
234
|
-
const allMessageIds = new Set([...baseMessageIds, ...includedMessageIds]);
|
|
235
|
-
|
|
236
|
-
// Get all unique messages
|
|
237
|
-
messages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => allMessageIds.has(msg.id));
|
|
238
|
-
// Sort by createdAt
|
|
239
|
-
messages.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
240
|
-
|
|
241
|
-
const list = new MessageList().add(messages, 'memory');
|
|
242
|
-
return format === 'v2' ? list.get.all.v2() : list.get.all.v1();
|
|
243
|
-
}
|
|
244
|
-
|
|
245
184
|
async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
|
|
246
|
-
async saveMessages(args: { messages:
|
|
185
|
+
async saveMessages(args: { messages: MastraDBMessage[]; format: 'v2' }): Promise<MastraDBMessage[]>;
|
|
247
186
|
async saveMessages(
|
|
248
|
-
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages:
|
|
249
|
-
): Promise<
|
|
187
|
+
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraDBMessage[]; format: 'v2' },
|
|
188
|
+
): Promise<MastraDBMessage[] | MastraMessageV1[]> {
|
|
250
189
|
if (this.mode === `read`) return [];
|
|
251
190
|
|
|
252
191
|
const { messages, format = 'v1' } = args;
|
|
@@ -256,11 +195,11 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
256
195
|
}
|
|
257
196
|
|
|
258
197
|
const list = new MessageList().add(messages, 'memory');
|
|
259
|
-
return format === 'v2' ? list.get.all.
|
|
198
|
+
return format === 'v2' ? list.get.all.db() : list.get.all.v1();
|
|
260
199
|
}
|
|
261
200
|
|
|
262
|
-
async updateMessages(args: { messages: Partial<
|
|
263
|
-
const updatedMessages:
|
|
201
|
+
async updateMessages(args: { messages: Partial<MastraDBMessage> & { id: string }[] }): Promise<MastraDBMessage[]> {
|
|
202
|
+
const updatedMessages: MastraDBMessage[] = [];
|
|
264
203
|
|
|
265
204
|
if (this.mode === `read`) return [];
|
|
266
205
|
|
|
@@ -332,21 +271,14 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
332
271
|
return evals as EvalRow[];
|
|
333
272
|
}
|
|
334
273
|
|
|
335
|
-
async
|
|
274
|
+
async listWorkflowRuns({
|
|
336
275
|
workflowName,
|
|
337
276
|
fromDate,
|
|
338
277
|
toDate,
|
|
339
278
|
limit,
|
|
340
279
|
offset,
|
|
341
280
|
resourceId,
|
|
342
|
-
}: {
|
|
343
|
-
workflowName?: string;
|
|
344
|
-
fromDate?: Date;
|
|
345
|
-
toDate?: Date;
|
|
346
|
-
limit?: number;
|
|
347
|
-
offset?: number;
|
|
348
|
-
resourceId?: string;
|
|
349
|
-
} = {}): Promise<WorkflowRuns> {
|
|
281
|
+
}: StorageListWorkflowRunsInput = {}): Promise<WorkflowRuns> {
|
|
350
282
|
let runs = Array.from(this.data.mastra_workflow_snapshot.values());
|
|
351
283
|
|
|
352
284
|
if (workflowName) runs = runs.filter((run: any) => run.workflow_name === workflowName);
|
|
@@ -403,78 +335,133 @@ export class BenchmarkStore extends MastraStorage {
|
|
|
403
335
|
return parsedRun as WorkflowRun;
|
|
404
336
|
}
|
|
405
337
|
|
|
406
|
-
async
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
attributes,
|
|
410
|
-
page,
|
|
411
|
-
perPage,
|
|
412
|
-
fromDate,
|
|
413
|
-
toDate,
|
|
414
|
-
}: {
|
|
415
|
-
name?: string;
|
|
416
|
-
scope?: string;
|
|
417
|
-
attributes?: Record<string, string>;
|
|
418
|
-
page: number;
|
|
419
|
-
perPage: number;
|
|
420
|
-
fromDate?: Date;
|
|
421
|
-
toDate?: Date;
|
|
422
|
-
}): Promise<PaginationInfo & { traces: Trace[] }> {
|
|
423
|
-
const traces = await this.getTraces({ name, scope, page, perPage, attributes, fromDate, toDate });
|
|
424
|
-
const total = Array.from(this.data.mastra_traces.values()).length;
|
|
338
|
+
async listThreads(args: StorageListThreadsInput): Promise<StorageListThreadsOutput> {
|
|
339
|
+
const { page = 0, perPage: perPageInput, filter, orderBy } = args;
|
|
340
|
+
let allThreads: StorageThreadType[] = Array.from(this.data.mastra_threads.values());
|
|
425
341
|
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
perPage,
|
|
431
|
-
hasMore: total > (page + 1) * perPage,
|
|
432
|
-
};
|
|
433
|
-
}
|
|
342
|
+
// Apply resourceId filter if provided
|
|
343
|
+
if (filter?.resourceId) {
|
|
344
|
+
allThreads = allThreads.filter(thread => thread.resourceId === filter.resourceId);
|
|
345
|
+
}
|
|
434
346
|
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
347
|
+
// Apply metadata filter if provided (AND logic)
|
|
348
|
+
if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
|
|
349
|
+
allThreads = allThreads.filter(thread => {
|
|
350
|
+
if (!thread.metadata) return false;
|
|
351
|
+
return Object.entries(filter.metadata!).every(([key, value]) => thread.metadata![key] === value);
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// Apply ordering - default to DESC by createdAt
|
|
356
|
+
const sortField = orderBy?.field || 'createdAt';
|
|
357
|
+
const sortDirection = orderBy?.direction || 'DESC';
|
|
358
|
+
const direction = sortDirection === 'ASC' ? 1 : -1;
|
|
359
|
+
|
|
360
|
+
allThreads.sort((a: any, b: any) => {
|
|
361
|
+
const aVal = a[sortField];
|
|
362
|
+
const bVal = b[sortField];
|
|
363
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
364
|
+
return direction * (aVal.getTime() - bVal.getTime());
|
|
365
|
+
}
|
|
366
|
+
return direction * (aVal < bVal ? -1 : aVal > bVal ? 1 : 0);
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
// Handle perPage: false (fetch all results)
|
|
370
|
+
const fetchAll = perPageInput === false;
|
|
371
|
+
const normalizedPerPage = fetchAll ? allThreads.length : typeof perPageInput === 'number' ? perPageInput : 100;
|
|
372
|
+
const normalizedPage = fetchAll ? 0 : Math.max(0, page);
|
|
373
|
+
const offset = normalizedPage * normalizedPerPage;
|
|
374
|
+
const threads = allThreads.slice(offset, fetchAll ? undefined : offset + normalizedPerPage);
|
|
443
375
|
|
|
444
376
|
return {
|
|
445
377
|
threads,
|
|
446
378
|
total: allThreads.length,
|
|
447
|
-
page:
|
|
448
|
-
perPage:
|
|
449
|
-
hasMore:
|
|
379
|
+
page: normalizedPage,
|
|
380
|
+
perPage: fetchAll ? false : normalizedPerPage,
|
|
381
|
+
hasMore: fetchAll ? false : offset + normalizedPerPage < allThreads.length,
|
|
450
382
|
};
|
|
451
383
|
}
|
|
452
384
|
|
|
453
|
-
async
|
|
454
|
-
|
|
455
|
-
): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
|
|
456
|
-
const { threadId, selectBy, format = 'v1' } = args;
|
|
385
|
+
async listMessages(args: StorageListMessagesInput): Promise<StorageListMessagesOutput> {
|
|
386
|
+
const { threadId, page = 0, perPage = 40, resourceId, filter, include, orderBy } = args;
|
|
457
387
|
if (!threadId.trim()) throw new Error('threadId must be a non-empty string');
|
|
458
388
|
|
|
459
|
-
|
|
389
|
+
let messages: any[] = [];
|
|
390
|
+
const includedMessageIds = new Set<string>();
|
|
460
391
|
|
|
461
|
-
//
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
392
|
+
// Handle include for cross-thread queries (resource scope support)
|
|
393
|
+
if (include?.length) {
|
|
394
|
+
for (const inc of include) {
|
|
395
|
+
// Use the included threadId if provided (resource scope), otherwise use main threadId
|
|
396
|
+
const queryThreadId = inc.threadId || threadId;
|
|
397
|
+
|
|
398
|
+
// Get the target message and surrounding context
|
|
399
|
+
const threadMessages = Array.from(this.data.mastra_messages.values())
|
|
400
|
+
.filter((msg: any) => msg.threadId === queryThreadId)
|
|
401
|
+
.sort((a: any, b: any) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
|
|
402
|
+
|
|
403
|
+
const targetIndex = threadMessages.findIndex((msg: any) => msg.id === inc.id);
|
|
404
|
+
|
|
405
|
+
if (targetIndex >= 0) {
|
|
406
|
+
const startIdx = Math.max(0, targetIndex - (inc.withPreviousMessages || 0));
|
|
407
|
+
const endIdx = Math.min(threadMessages.length, targetIndex + (inc.withNextMessages || 0) + 1);
|
|
408
|
+
|
|
409
|
+
for (let i = startIdx; i < endIdx; i++) {
|
|
410
|
+
includedMessageIds.add(threadMessages[i].id);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
// Get base messages for the thread
|
|
417
|
+
const baseMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => {
|
|
418
|
+
if (msg.threadId !== threadId) return false;
|
|
419
|
+
if (resourceId && msg.resourceId !== resourceId) return false;
|
|
420
|
+
return true;
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Combine base messages with included messages
|
|
424
|
+
const baseMessageIds = new Set(baseMessages.map((m: any) => m.id));
|
|
425
|
+
const allMessageIds = new Set([...baseMessageIds, ...includedMessageIds]);
|
|
426
|
+
|
|
427
|
+
// Get all unique messages and convert to v2 format
|
|
428
|
+
const allMessages = Array.from(this.data.mastra_messages.values()).filter((msg: any) => allMessageIds.has(msg.id));
|
|
429
|
+
const list = new MessageList().add(allMessages, 'memory');
|
|
430
|
+
let filteredMessages = list.get.all.db();
|
|
431
|
+
|
|
432
|
+
// Apply date filters
|
|
433
|
+
if (filter?.dateRange?.start) {
|
|
434
|
+
filteredMessages = filteredMessages.filter((m: any) => new Date(m.createdAt) >= filter.dateRange!.start!);
|
|
435
|
+
}
|
|
436
|
+
if (filter?.dateRange?.end) {
|
|
437
|
+
filteredMessages = filteredMessages.filter((m: any) => new Date(m.createdAt) <= filter.dateRange!.end!);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Apply ordering - default to ASC by createdAt
|
|
441
|
+
const sortField = orderBy?.field || 'createdAt';
|
|
442
|
+
const sortDirection = orderBy?.direction || 'ASC';
|
|
443
|
+
const direction = sortDirection === 'ASC' ? 1 : -1;
|
|
444
|
+
|
|
445
|
+
filteredMessages.sort((a: any, b: any) => {
|
|
446
|
+
const aVal = a[sortField];
|
|
447
|
+
const bVal = b[sortField];
|
|
448
|
+
if (aVal instanceof Date && bVal instanceof Date) {
|
|
449
|
+
return direction * (aVal.getTime() - bVal.getTime());
|
|
450
|
+
}
|
|
451
|
+
return direction * (aVal < bVal ? -1 : aVal > bVal ? 1 : 0);
|
|
452
|
+
});
|
|
467
453
|
|
|
468
454
|
// Apply pagination
|
|
469
|
-
const
|
|
470
|
-
const
|
|
455
|
+
const normalizedPerPage = perPage === false ? filteredMessages.length : perPage;
|
|
456
|
+
const start = perPage === false ? 0 : page * normalizedPerPage;
|
|
457
|
+
messages = filteredMessages.slice(start, start + normalizedPerPage);
|
|
471
458
|
|
|
472
459
|
return {
|
|
473
|
-
messages,
|
|
474
|
-
total:
|
|
460
|
+
messages: messages as MastraDBMessage[],
|
|
461
|
+
total: filteredMessages.length,
|
|
475
462
|
page,
|
|
476
|
-
perPage,
|
|
477
|
-
hasMore:
|
|
463
|
+
perPage: perPage === false ? false : normalizedPerPage,
|
|
464
|
+
hasMore: perPage === false ? false : filteredMessages.length > (page + 1) * normalizedPerPage,
|
|
478
465
|
};
|
|
479
466
|
}
|
|
480
467
|
|
package/src/storage/index.ts
CHANGED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { InMemoryMemory, InMemoryDB } from '@mastra/core/storage';
|
|
2
|
+
import { writeFile, readFile } from 'fs/promises';
|
|
3
|
+
import { existsSync } from 'fs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Extended InMemoryMemory with persist/hydrate capabilities for benchmarking.
|
|
7
|
+
* Allows saving and loading the in-memory state to/from disk.
|
|
8
|
+
*
|
|
9
|
+
* NOTE: We store a reference to the InMemoryDB instance that is passed to the parent class.
|
|
10
|
+
* When hydrating, we must clear and repopulate the maps instead of replacing them,
|
|
11
|
+
* otherwise the parent class won't see the updated data.
|
|
12
|
+
*/
|
|
13
|
+
export class PersistableInMemoryMemory extends InMemoryMemory {
|
|
14
|
+
private _db: InMemoryDB;
|
|
15
|
+
private isReadonly = false;
|
|
16
|
+
|
|
17
|
+
constructor(args?: { readOnly: boolean }) {
|
|
18
|
+
const db = new InMemoryDB();
|
|
19
|
+
|
|
20
|
+
super({ db });
|
|
21
|
+
|
|
22
|
+
if (args?.readOnly) this.isReadonly = true;
|
|
23
|
+
// Store the SAME db instance that was passed to super()
|
|
24
|
+
this._db = db;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Persist the current storage state to a JSON file
|
|
29
|
+
*/
|
|
30
|
+
async persist(filePath: string): Promise<void> {
|
|
31
|
+
if (this.isReadonly) return;
|
|
32
|
+
const data: Record<string, any> = {
|
|
33
|
+
threads: Array.from(this._db.threads.entries()),
|
|
34
|
+
resources: Array.from(this._db.resources.entries()),
|
|
35
|
+
messages: Array.from(this._db.messages.entries()),
|
|
36
|
+
observationalMemory: Array.from(this._db.observationalMemory.entries()),
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
await writeFile(filePath, JSON.stringify(data, null, 2));
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Hydrate storage state from a JSON file.
|
|
44
|
+
* IMPORTANT: We clear and repopulate the existing maps to preserve references.
|
|
45
|
+
*/
|
|
46
|
+
async hydrate(filePath: string): Promise<void> {
|
|
47
|
+
if (!existsSync(filePath)) {
|
|
48
|
+
throw new Error(`Storage file not found: ${filePath}`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const content = await readFile(filePath, 'utf-8');
|
|
52
|
+
const data = JSON.parse(content);
|
|
53
|
+
|
|
54
|
+
// Clear existing data first
|
|
55
|
+
this._db.threads.clear();
|
|
56
|
+
this._db.resources.clear();
|
|
57
|
+
this._db.messages.clear();
|
|
58
|
+
this._db.observationalMemory.clear();
|
|
59
|
+
|
|
60
|
+
// Restore data into the SAME map instances (don't replace the maps!)
|
|
61
|
+
// Convert date strings back to Date objects for all record types
|
|
62
|
+
if (data.threads) {
|
|
63
|
+
for (const [key, value] of data.threads) {
|
|
64
|
+
const thread = {
|
|
65
|
+
...value,
|
|
66
|
+
createdAt: value.createdAt ? new Date(value.createdAt) : undefined,
|
|
67
|
+
updatedAt: value.updatedAt ? new Date(value.updatedAt) : undefined,
|
|
68
|
+
};
|
|
69
|
+
this._db.threads.set(key, thread);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (data.resources) {
|
|
73
|
+
for (const [key, value] of data.resources) {
|
|
74
|
+
const resource = {
|
|
75
|
+
...value,
|
|
76
|
+
createdAt: value.createdAt ? new Date(value.createdAt) : undefined,
|
|
77
|
+
updatedAt: value.updatedAt ? new Date(value.updatedAt) : undefined,
|
|
78
|
+
};
|
|
79
|
+
this._db.resources.set(key, resource);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (data.messages) {
|
|
83
|
+
for (const [key, value] of data.messages) {
|
|
84
|
+
// Convert date strings back to Date objects
|
|
85
|
+
const message = {
|
|
86
|
+
...value,
|
|
87
|
+
createdAt: value.createdAt ? new Date(value.createdAt) : undefined,
|
|
88
|
+
};
|
|
89
|
+
this._db.messages.set(key, message);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
if (data.observationalMemory) {
|
|
93
|
+
for (const [key, records] of data.observationalMemory) {
|
|
94
|
+
// records is an array of ObservationalMemoryRecord
|
|
95
|
+
// Convert date strings back to Date objects for each record
|
|
96
|
+
const convertedRecords = records.map((record: any) => ({
|
|
97
|
+
...record,
|
|
98
|
+
createdAt: record.createdAt ? new Date(record.createdAt) : undefined,
|
|
99
|
+
updatedAt: record.updatedAt ? new Date(record.updatedAt) : undefined,
|
|
100
|
+
lastObservedAt: record.lastObservedAt ? new Date(record.lastObservedAt) : undefined,
|
|
101
|
+
}));
|
|
102
|
+
this._db.observationalMemory.set(key, convertedRecords);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Clear all data
|
|
109
|
+
*/
|
|
110
|
+
async clear(): Promise<void> {
|
|
111
|
+
this._db.threads.clear();
|
|
112
|
+
this._db.resources.clear();
|
|
113
|
+
this._db.messages.clear();
|
|
114
|
+
this._db.observationalMemory.clear();
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Get stats about stored data
|
|
119
|
+
*/
|
|
120
|
+
getStats(): {
|
|
121
|
+
threads: number;
|
|
122
|
+
resources: number;
|
|
123
|
+
messages: number;
|
|
124
|
+
observationalMemoryRecords: number;
|
|
125
|
+
} {
|
|
126
|
+
return {
|
|
127
|
+
threads: this._db.threads.size,
|
|
128
|
+
resources: this._db.resources.size,
|
|
129
|
+
messages: this._db.messages.size,
|
|
130
|
+
observationalMemoryRecords: this._db.observationalMemory.size,
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|