@mastra/lance 0.0.0-memory-system-message-error-20250813233316 → 0.0.0-message-file-url-handling-fix-20250904234524
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 +178 -7
- package/README.md +3 -3
- package/dist/index.cjs +102 -24
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +102 -24
- package/dist/index.js.map +1 -1
- package/dist/storage/domains/memory/index.d.ts +9 -0
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +19 -1
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +27 -1
- package/dist/storage/index.d.ts.map +1 -1
- package/dist/vector/index.d.ts +1 -1
- package/dist/vector/index.d.ts.map +1 -1
- package/package.json +19 -6
- package/eslint.config.js +0 -6
- package/src/index.ts +0 -2
- package/src/storage/domains/legacy-evals/index.ts +0 -156
- package/src/storage/domains/memory/index.ts +0 -947
- package/src/storage/domains/operations/index.ts +0 -489
- package/src/storage/domains/scores/index.ts +0 -243
- package/src/storage/domains/traces/index.ts +0 -212
- package/src/storage/domains/utils.ts +0 -158
- package/src/storage/domains/workflows/index.ts +0 -207
- package/src/storage/index.test.ts +0 -10
- package/src/storage/index.ts +0 -448
- package/src/vector/filter.test.ts +0 -295
- package/src/vector/filter.ts +0 -443
- package/src/vector/index.test.ts +0 -1493
- package/src/vector/index.ts +0 -941
- package/src/vector/types.ts +0 -16
- package/tsconfig.build.json +0 -9
- package/tsconfig.json +0 -5
- package/tsup.config.ts +0 -17
- package/vitest.config.ts +0 -11
|
@@ -1,207 +0,0 @@
|
|
|
1
|
-
import type { Connection } from '@lancedb/lancedb';
|
|
2
|
-
import type { WorkflowRun, WorkflowRunState, WorkflowRuns } from '@mastra/core';
|
|
3
|
-
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
4
|
-
import { ensureDate, TABLE_WORKFLOW_SNAPSHOT, WorkflowsStorage } from '@mastra/core/storage';
|
|
5
|
-
|
|
6
|
-
function parseWorkflowRun(row: any): WorkflowRun {
|
|
7
|
-
let parsedSnapshot: WorkflowRunState | string = row.snapshot;
|
|
8
|
-
if (typeof parsedSnapshot === 'string') {
|
|
9
|
-
try {
|
|
10
|
-
parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;
|
|
11
|
-
} catch (e) {
|
|
12
|
-
// If parsing fails, return the raw snapshot string
|
|
13
|
-
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
14
|
-
}
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return {
|
|
18
|
-
workflowName: row.workflow_name,
|
|
19
|
-
runId: row.run_id,
|
|
20
|
-
snapshot: parsedSnapshot,
|
|
21
|
-
createdAt: ensureDate(row.createdAt)!,
|
|
22
|
-
updatedAt: ensureDate(row.updatedAt)!,
|
|
23
|
-
resourceId: row.resourceId,
|
|
24
|
-
};
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
export class StoreWorkflowsLance extends WorkflowsStorage {
|
|
28
|
-
client: Connection;
|
|
29
|
-
constructor({ client }: { client: Connection }) {
|
|
30
|
-
super();
|
|
31
|
-
this.client = client;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
async persistWorkflowSnapshot({
|
|
35
|
-
workflowName,
|
|
36
|
-
runId,
|
|
37
|
-
snapshot,
|
|
38
|
-
}: {
|
|
39
|
-
workflowName: string;
|
|
40
|
-
runId: string;
|
|
41
|
-
snapshot: WorkflowRunState;
|
|
42
|
-
}): Promise<void> {
|
|
43
|
-
try {
|
|
44
|
-
const table = await this.client.openTable(TABLE_WORKFLOW_SNAPSHOT);
|
|
45
|
-
|
|
46
|
-
// Try to find the existing record
|
|
47
|
-
const query = table.query().where(`workflow_name = '${workflowName}' AND run_id = '${runId}'`);
|
|
48
|
-
const records = await query.toArray();
|
|
49
|
-
let createdAt: number;
|
|
50
|
-
const now = Date.now();
|
|
51
|
-
|
|
52
|
-
if (records.length > 0) {
|
|
53
|
-
createdAt = records[0].createdAt ?? now;
|
|
54
|
-
} else {
|
|
55
|
-
createdAt = now;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
const record = {
|
|
59
|
-
workflow_name: workflowName,
|
|
60
|
-
run_id: runId,
|
|
61
|
-
snapshot: JSON.stringify(snapshot),
|
|
62
|
-
createdAt,
|
|
63
|
-
updatedAt: now,
|
|
64
|
-
};
|
|
65
|
-
|
|
66
|
-
await table
|
|
67
|
-
.mergeInsert(['workflow_name', 'run_id'])
|
|
68
|
-
.whenMatchedUpdateAll()
|
|
69
|
-
.whenNotMatchedInsertAll()
|
|
70
|
-
.execute([record]);
|
|
71
|
-
} catch (error: any) {
|
|
72
|
-
throw new MastraError(
|
|
73
|
-
{
|
|
74
|
-
id: 'LANCE_STORE_PERSIST_WORKFLOW_SNAPSHOT_FAILED',
|
|
75
|
-
domain: ErrorDomain.STORAGE,
|
|
76
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
77
|
-
details: { workflowName, runId },
|
|
78
|
-
},
|
|
79
|
-
error,
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
async loadWorkflowSnapshot({
|
|
84
|
-
workflowName,
|
|
85
|
-
runId,
|
|
86
|
-
}: {
|
|
87
|
-
workflowName: string;
|
|
88
|
-
runId: string;
|
|
89
|
-
}): Promise<WorkflowRunState | null> {
|
|
90
|
-
try {
|
|
91
|
-
const table = await this.client.openTable(TABLE_WORKFLOW_SNAPSHOT);
|
|
92
|
-
const query = table.query().where(`workflow_name = '${workflowName}' AND run_id = '${runId}'`);
|
|
93
|
-
const records = await query.toArray();
|
|
94
|
-
return records.length > 0 ? JSON.parse(records[0].snapshot) : null;
|
|
95
|
-
} catch (error: any) {
|
|
96
|
-
throw new MastraError(
|
|
97
|
-
{
|
|
98
|
-
id: 'LANCE_STORE_LOAD_WORKFLOW_SNAPSHOT_FAILED',
|
|
99
|
-
domain: ErrorDomain.STORAGE,
|
|
100
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
101
|
-
details: { workflowName, runId },
|
|
102
|
-
},
|
|
103
|
-
error,
|
|
104
|
-
);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async getWorkflowRunById(args: { runId: string; workflowName?: string }): Promise<{
|
|
109
|
-
workflowName: string;
|
|
110
|
-
runId: string;
|
|
111
|
-
snapshot: any;
|
|
112
|
-
createdAt: Date;
|
|
113
|
-
updatedAt: Date;
|
|
114
|
-
} | null> {
|
|
115
|
-
try {
|
|
116
|
-
const table = await this.client.openTable(TABLE_WORKFLOW_SNAPSHOT);
|
|
117
|
-
let whereClause = `run_id = '${args.runId}'`;
|
|
118
|
-
if (args.workflowName) {
|
|
119
|
-
whereClause += ` AND workflow_name = '${args.workflowName}'`;
|
|
120
|
-
}
|
|
121
|
-
const query = table.query().where(whereClause);
|
|
122
|
-
const records = await query.toArray();
|
|
123
|
-
if (records.length === 0) return null;
|
|
124
|
-
const record = records[0];
|
|
125
|
-
return parseWorkflowRun(record);
|
|
126
|
-
} catch (error: any) {
|
|
127
|
-
throw new MastraError(
|
|
128
|
-
{
|
|
129
|
-
id: 'LANCE_STORE_GET_WORKFLOW_RUN_BY_ID_FAILED',
|
|
130
|
-
domain: ErrorDomain.STORAGE,
|
|
131
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
132
|
-
details: { runId: args.runId, workflowName: args.workflowName ?? '' },
|
|
133
|
-
},
|
|
134
|
-
error,
|
|
135
|
-
);
|
|
136
|
-
}
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async getWorkflowRuns(args?: {
|
|
140
|
-
namespace?: string;
|
|
141
|
-
resourceId?: string;
|
|
142
|
-
workflowName?: string;
|
|
143
|
-
fromDate?: Date;
|
|
144
|
-
toDate?: Date;
|
|
145
|
-
limit?: number;
|
|
146
|
-
offset?: number;
|
|
147
|
-
}): Promise<WorkflowRuns> {
|
|
148
|
-
try {
|
|
149
|
-
const table = await this.client.openTable(TABLE_WORKFLOW_SNAPSHOT);
|
|
150
|
-
|
|
151
|
-
let query = table.query();
|
|
152
|
-
|
|
153
|
-
const conditions: string[] = [];
|
|
154
|
-
|
|
155
|
-
if (args?.workflowName) {
|
|
156
|
-
conditions.push(`workflow_name = '${args.workflowName.replace(/'/g, "''")}'`);
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
if (args?.resourceId) {
|
|
160
|
-
conditions.push(`\`resourceId\` = '${args.resourceId}'`);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
if (args?.fromDate instanceof Date) {
|
|
164
|
-
conditions.push(`\`createdAt\` >= ${args.fromDate.getTime()}`);
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
if (args?.toDate instanceof Date) {
|
|
168
|
-
conditions.push(`\`createdAt\` <= ${args.toDate.getTime()}`);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
let total = 0;
|
|
172
|
-
|
|
173
|
-
// Apply all conditions
|
|
174
|
-
if (conditions.length > 0) {
|
|
175
|
-
query = query.where(conditions.join(' AND '));
|
|
176
|
-
total = await table.countRows(conditions.join(' AND '));
|
|
177
|
-
} else {
|
|
178
|
-
total = await table.countRows();
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
if (args?.limit) {
|
|
182
|
-
query.limit(args.limit);
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (args?.offset) {
|
|
186
|
-
query.offset(args.offset);
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
const records = await query.toArray();
|
|
190
|
-
|
|
191
|
-
return {
|
|
192
|
-
runs: records.map(record => parseWorkflowRun(record)),
|
|
193
|
-
total: total || records.length,
|
|
194
|
-
};
|
|
195
|
-
} catch (error: any) {
|
|
196
|
-
throw new MastraError(
|
|
197
|
-
{
|
|
198
|
-
id: 'LANCE_STORE_GET_WORKFLOW_RUNS_FAILED',
|
|
199
|
-
domain: ErrorDomain.STORAGE,
|
|
200
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
201
|
-
details: { namespace: args?.namespace ?? '', workflowName: args?.workflowName ?? '' },
|
|
202
|
-
},
|
|
203
|
-
error,
|
|
204
|
-
);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import { createTestSuite } from '@internal/storage-test-utils';
|
|
2
|
-
import { vi } from 'vitest';
|
|
3
|
-
import { LanceStorage } from './index';
|
|
4
|
-
|
|
5
|
-
// Increase timeout for all tests in this file to 30 seconds
|
|
6
|
-
vi.setConfig({ testTimeout: 200_000, hookTimeout: 200_000 });
|
|
7
|
-
|
|
8
|
-
const storage = await LanceStorage.create('test', 'lancedb-storage');
|
|
9
|
-
|
|
10
|
-
createTestSuite(storage);
|
package/src/storage/index.ts
DELETED
|
@@ -1,448 +0,0 @@
|
|
|
1
|
-
import { connect } from '@lancedb/lancedb';
|
|
2
|
-
import type { Connection, ConnectionOptions } from '@lancedb/lancedb';
|
|
3
|
-
import type { MastraMessageContentV2 } from '@mastra/core/agent';
|
|
4
|
-
import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
|
|
5
|
-
import type { MastraMessageV1, MastraMessageV2, StorageThreadType, TraceType } from '@mastra/core/memory';
|
|
6
|
-
import type { ScoreRowData, ScoringSource } from '@mastra/core/scores';
|
|
7
|
-
import { MastraStorage } from '@mastra/core/storage';
|
|
8
|
-
import type {
|
|
9
|
-
TABLE_NAMES,
|
|
10
|
-
PaginationInfo,
|
|
11
|
-
StorageGetMessagesArg,
|
|
12
|
-
StorageColumn,
|
|
13
|
-
EvalRow,
|
|
14
|
-
WorkflowRuns,
|
|
15
|
-
StoragePagination,
|
|
16
|
-
StorageDomains,
|
|
17
|
-
StorageGetTracesPaginatedArg,
|
|
18
|
-
StorageResourceType,
|
|
19
|
-
} from '@mastra/core/storage';
|
|
20
|
-
import type { Trace } from '@mastra/core/telemetry';
|
|
21
|
-
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
22
|
-
import { StoreLegacyEvalsLance } from './domains/legacy-evals';
|
|
23
|
-
import { StoreMemoryLance } from './domains/memory';
|
|
24
|
-
import { StoreOperationsLance } from './domains/operations';
|
|
25
|
-
import { StoreScoresLance } from './domains/scores';
|
|
26
|
-
import { StoreTracesLance } from './domains/traces';
|
|
27
|
-
import { StoreWorkflowsLance } from './domains/workflows';
|
|
28
|
-
|
|
29
|
-
export class LanceStorage extends MastraStorage {
|
|
30
|
-
stores: StorageDomains;
|
|
31
|
-
private lanceClient!: Connection;
|
|
32
|
-
/**
|
|
33
|
-
* Creates a new instance of LanceStorage
|
|
34
|
-
* @param uri The URI to connect to LanceDB
|
|
35
|
-
* @param options connection options
|
|
36
|
-
*
|
|
37
|
-
* Usage:
|
|
38
|
-
*
|
|
39
|
-
* Connect to a local database
|
|
40
|
-
* ```ts
|
|
41
|
-
* const store = await LanceStorage.create('/path/to/db');
|
|
42
|
-
* ```
|
|
43
|
-
*
|
|
44
|
-
* Connect to a LanceDB cloud database
|
|
45
|
-
* ```ts
|
|
46
|
-
* const store = await LanceStorage.create('db://host:port');
|
|
47
|
-
* ```
|
|
48
|
-
*
|
|
49
|
-
* Connect to a cloud database
|
|
50
|
-
* ```ts
|
|
51
|
-
* const store = await LanceStorage.create('s3://bucket/db', { storageOptions: { timeout: '60s' } });
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
public static async create(name: string, uri: string, options?: ConnectionOptions): Promise<LanceStorage> {
|
|
55
|
-
const instance = new LanceStorage(name);
|
|
56
|
-
try {
|
|
57
|
-
instance.lanceClient = await connect(uri, options);
|
|
58
|
-
const operations = new StoreOperationsLance({ client: instance.lanceClient });
|
|
59
|
-
instance.stores = {
|
|
60
|
-
operations: new StoreOperationsLance({ client: instance.lanceClient }),
|
|
61
|
-
workflows: new StoreWorkflowsLance({ client: instance.lanceClient }),
|
|
62
|
-
traces: new StoreTracesLance({ client: instance.lanceClient, operations }),
|
|
63
|
-
scores: new StoreScoresLance({ client: instance.lanceClient }),
|
|
64
|
-
memory: new StoreMemoryLance({ client: instance.lanceClient, operations }),
|
|
65
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: instance.lanceClient }),
|
|
66
|
-
};
|
|
67
|
-
return instance;
|
|
68
|
-
} catch (e: any) {
|
|
69
|
-
throw new MastraError(
|
|
70
|
-
{
|
|
71
|
-
id: 'STORAGE_LANCE_STORAGE_CONNECT_FAILED',
|
|
72
|
-
domain: ErrorDomain.STORAGE,
|
|
73
|
-
category: ErrorCategory.THIRD_PARTY,
|
|
74
|
-
text: `Failed to connect to LanceDB: ${e.message || e}`,
|
|
75
|
-
details: { uri, optionsProvided: !!options },
|
|
76
|
-
},
|
|
77
|
-
e,
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* @internal
|
|
84
|
-
* Private constructor to enforce using the create factory method
|
|
85
|
-
*/
|
|
86
|
-
private constructor(name: string) {
|
|
87
|
-
super({ name });
|
|
88
|
-
const operations = new StoreOperationsLance({ client: this.lanceClient });
|
|
89
|
-
|
|
90
|
-
this.stores = {
|
|
91
|
-
operations: new StoreOperationsLance({ client: this.lanceClient }),
|
|
92
|
-
workflows: new StoreWorkflowsLance({ client: this.lanceClient }),
|
|
93
|
-
traces: new StoreTracesLance({ client: this.lanceClient, operations }),
|
|
94
|
-
scores: new StoreScoresLance({ client: this.lanceClient }),
|
|
95
|
-
legacyEvals: new StoreLegacyEvalsLance({ client: this.lanceClient }),
|
|
96
|
-
memory: new StoreMemoryLance({ client: this.lanceClient, operations }),
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
async createTable({
|
|
101
|
-
tableName,
|
|
102
|
-
schema,
|
|
103
|
-
}: {
|
|
104
|
-
tableName: TABLE_NAMES;
|
|
105
|
-
schema: Record<string, StorageColumn>;
|
|
106
|
-
}): Promise<void> {
|
|
107
|
-
return this.stores.operations.createTable({ tableName, schema });
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
async dropTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
|
|
111
|
-
return this.stores.operations.dropTable({ tableName });
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
async alterTable({
|
|
115
|
-
tableName,
|
|
116
|
-
schema,
|
|
117
|
-
ifNotExists,
|
|
118
|
-
}: {
|
|
119
|
-
tableName: TABLE_NAMES;
|
|
120
|
-
schema: Record<string, StorageColumn>;
|
|
121
|
-
ifNotExists: string[];
|
|
122
|
-
}): Promise<void> {
|
|
123
|
-
return this.stores.operations.alterTable({ tableName, schema, ifNotExists });
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
async clearTable({ tableName }: { tableName: TABLE_NAMES }): Promise<void> {
|
|
127
|
-
return this.stores.operations.clearTable({ tableName });
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
async insert({ tableName, record }: { tableName: TABLE_NAMES; record: Record<string, any> }): Promise<void> {
|
|
131
|
-
return this.stores.operations.insert({ tableName, record });
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
async batchInsert({ tableName, records }: { tableName: TABLE_NAMES; records: Record<string, any>[] }): Promise<void> {
|
|
135
|
-
return this.stores.operations.batchInsert({ tableName, records });
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
async load({ tableName, keys }: { tableName: TABLE_NAMES; keys: Record<string, any> }): Promise<any> {
|
|
139
|
-
return this.stores.operations.load({ tableName, keys });
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
async getThreadById({ threadId }: { threadId: string }): Promise<StorageThreadType | null> {
|
|
143
|
-
return this.stores.memory.getThreadById({ threadId });
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
async getThreadsByResourceId({ resourceId }: { resourceId: string }): Promise<StorageThreadType[]> {
|
|
147
|
-
return this.stores.memory.getThreadsByResourceId({ resourceId });
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Saves a thread to the database. This function doesn't overwrite existing threads.
|
|
152
|
-
* @param thread - The thread to save
|
|
153
|
-
* @returns The saved thread
|
|
154
|
-
*/
|
|
155
|
-
async saveThread({ thread }: { thread: StorageThreadType }): Promise<StorageThreadType> {
|
|
156
|
-
return this.stores.memory.saveThread({ thread });
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
async updateThread({
|
|
160
|
-
id,
|
|
161
|
-
title,
|
|
162
|
-
metadata,
|
|
163
|
-
}: {
|
|
164
|
-
id: string;
|
|
165
|
-
title: string;
|
|
166
|
-
metadata: Record<string, unknown>;
|
|
167
|
-
}): Promise<StorageThreadType> {
|
|
168
|
-
return this.stores.memory.updateThread({ id, title, metadata });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
async deleteThread({ threadId }: { threadId: string }): Promise<void> {
|
|
172
|
-
return this.stores.memory.deleteThread({ threadId });
|
|
173
|
-
}
|
|
174
|
-
|
|
175
|
-
public get supports() {
|
|
176
|
-
return {
|
|
177
|
-
selectByIncludeResourceScope: true,
|
|
178
|
-
resourceWorkingMemory: true,
|
|
179
|
-
hasColumn: true,
|
|
180
|
-
createTable: true,
|
|
181
|
-
deleteMessages: false,
|
|
182
|
-
};
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
|
|
186
|
-
return this.stores.memory.getResourceById({ resourceId });
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
async saveResource({ resource }: { resource: StorageResourceType }): Promise<StorageResourceType> {
|
|
190
|
-
return this.stores.memory.saveResource({ resource });
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async updateResource({
|
|
194
|
-
resourceId,
|
|
195
|
-
workingMemory,
|
|
196
|
-
metadata,
|
|
197
|
-
}: {
|
|
198
|
-
resourceId: string;
|
|
199
|
-
workingMemory?: string;
|
|
200
|
-
metadata?: Record<string, unknown>;
|
|
201
|
-
}): Promise<StorageResourceType> {
|
|
202
|
-
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* Processes messages to include context messages based on withPreviousMessages and withNextMessages
|
|
207
|
-
* @param records - The sorted array of records to process
|
|
208
|
-
* @param include - The array of include specifications with context parameters
|
|
209
|
-
* @returns The processed array with context messages included
|
|
210
|
-
*/
|
|
211
|
-
private processMessagesWithContext(
|
|
212
|
-
records: any[],
|
|
213
|
-
include: { id: string; withPreviousMessages?: number; withNextMessages?: number }[],
|
|
214
|
-
): any[] {
|
|
215
|
-
const messagesWithContext = include.filter(item => item.withPreviousMessages || item.withNextMessages);
|
|
216
|
-
|
|
217
|
-
if (messagesWithContext.length === 0) {
|
|
218
|
-
return records;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
// Create a map of message id to index in the sorted array for quick lookup
|
|
222
|
-
const messageIndexMap = new Map<string, number>();
|
|
223
|
-
records.forEach((message, index) => {
|
|
224
|
-
messageIndexMap.set(message.id, index);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
// Keep track of additional indices to include
|
|
228
|
-
const additionalIndices = new Set<number>();
|
|
229
|
-
|
|
230
|
-
for (const item of messagesWithContext) {
|
|
231
|
-
const messageIndex = messageIndexMap.get(item.id);
|
|
232
|
-
|
|
233
|
-
if (messageIndex !== undefined) {
|
|
234
|
-
// Add previous messages if requested
|
|
235
|
-
if (item.withPreviousMessages) {
|
|
236
|
-
const startIdx = Math.max(0, messageIndex - item.withPreviousMessages);
|
|
237
|
-
for (let i = startIdx; i < messageIndex; i++) {
|
|
238
|
-
additionalIndices.add(i);
|
|
239
|
-
}
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
// Add next messages if requested
|
|
243
|
-
if (item.withNextMessages) {
|
|
244
|
-
const endIdx = Math.min(records.length - 1, messageIndex + item.withNextMessages);
|
|
245
|
-
for (let i = messageIndex + 1; i <= endIdx; i++) {
|
|
246
|
-
additionalIndices.add(i);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
// If we need to include additional messages, create a new set of records
|
|
253
|
-
if (additionalIndices.size === 0) {
|
|
254
|
-
return records;
|
|
255
|
-
}
|
|
256
|
-
|
|
257
|
-
// Get IDs of the records that matched the original query
|
|
258
|
-
const originalMatchIds = new Set(include.map(item => item.id));
|
|
259
|
-
|
|
260
|
-
// Create a set of all indices we need to include
|
|
261
|
-
const allIndices = new Set<number>();
|
|
262
|
-
|
|
263
|
-
// Add indices of originally matched messages
|
|
264
|
-
records.forEach((record, index) => {
|
|
265
|
-
if (originalMatchIds.has(record.id)) {
|
|
266
|
-
allIndices.add(index);
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
|
|
270
|
-
// Add the additional context message indices
|
|
271
|
-
additionalIndices.forEach(index => {
|
|
272
|
-
allIndices.add(index);
|
|
273
|
-
});
|
|
274
|
-
|
|
275
|
-
// Create a new filtered array with only the required messages
|
|
276
|
-
// while maintaining chronological order
|
|
277
|
-
return Array.from(allIndices)
|
|
278
|
-
.sort((a, b) => a - b)
|
|
279
|
-
.map(index => records[index]);
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
public async getMessages(args: StorageGetMessagesArg & { format?: 'v1' }): Promise<MastraMessageV1[]>;
|
|
283
|
-
public async getMessages(args: StorageGetMessagesArg & { format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
284
|
-
public async getMessages({
|
|
285
|
-
threadId,
|
|
286
|
-
resourceId,
|
|
287
|
-
selectBy,
|
|
288
|
-
format,
|
|
289
|
-
threadConfig,
|
|
290
|
-
}: StorageGetMessagesArg & { format?: 'v1' | 'v2' }): Promise<MastraMessageV1[] | MastraMessageV2[]> {
|
|
291
|
-
return this.stores.memory.getMessages({ threadId, resourceId, selectBy, format, threadConfig });
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
async saveMessages(args: { messages: MastraMessageV1[]; format?: undefined | 'v1' }): Promise<MastraMessageV1[]>;
|
|
295
|
-
async saveMessages(args: { messages: MastraMessageV2[]; format: 'v2' }): Promise<MastraMessageV2[]>;
|
|
296
|
-
async saveMessages(
|
|
297
|
-
args: { messages: MastraMessageV1[]; format?: undefined | 'v1' } | { messages: MastraMessageV2[]; format: 'v2' },
|
|
298
|
-
): Promise<MastraMessageV2[] | MastraMessageV1[]> {
|
|
299
|
-
return this.stores.memory.saveMessages(args);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
async getThreadsByResourceIdPaginated(args: {
|
|
303
|
-
resourceId: string;
|
|
304
|
-
page: number;
|
|
305
|
-
perPage: number;
|
|
306
|
-
}): Promise<PaginationInfo & { threads: StorageThreadType[] }> {
|
|
307
|
-
return this.stores.memory.getThreadsByResourceIdPaginated(args);
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
async getMessagesPaginated(
|
|
311
|
-
args: StorageGetMessagesArg & { format?: 'v1' | 'v2' },
|
|
312
|
-
): Promise<PaginationInfo & { messages: MastraMessageV1[] | MastraMessageV2[] }> {
|
|
313
|
-
return this.stores.memory.getMessagesPaginated(args);
|
|
314
|
-
}
|
|
315
|
-
|
|
316
|
-
async updateMessages(_args: {
|
|
317
|
-
messages: Partial<Omit<MastraMessageV2, 'createdAt'>> &
|
|
318
|
-
{
|
|
319
|
-
id: string;
|
|
320
|
-
content?: { metadata?: MastraMessageContentV2['metadata']; content?: MastraMessageContentV2['content'] };
|
|
321
|
-
}[];
|
|
322
|
-
}): Promise<MastraMessageV2[]> {
|
|
323
|
-
return this.stores.memory.updateMessages(_args);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
async getTraceById(args: { traceId: string }): Promise<TraceType> {
|
|
327
|
-
return (this.stores as any).traces.getTraceById(args);
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
async getTraces(args: {
|
|
331
|
-
name?: string;
|
|
332
|
-
scope?: string;
|
|
333
|
-
page: number;
|
|
334
|
-
perPage: number;
|
|
335
|
-
attributes?: Record<string, string>;
|
|
336
|
-
}): Promise<Trace[]> {
|
|
337
|
-
return (this.stores as any).traces.getTraces(args);
|
|
338
|
-
}
|
|
339
|
-
|
|
340
|
-
async getTracesPaginated(args: StorageGetTracesPaginatedArg): Promise<PaginationInfo & { traces: Trace[] }> {
|
|
341
|
-
return (this.stores as any).traces.getTracesPaginated(args);
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
async getEvalsByAgentName(agentName: string, type?: 'test' | 'live'): Promise<EvalRow[]> {
|
|
345
|
-
return this.stores.legacyEvals.getEvalsByAgentName(agentName, type);
|
|
346
|
-
}
|
|
347
|
-
|
|
348
|
-
async getEvals(options: {
|
|
349
|
-
agentName?: string;
|
|
350
|
-
type?: 'test' | 'live';
|
|
351
|
-
page?: number;
|
|
352
|
-
perPage?: number;
|
|
353
|
-
fromDate?: Date;
|
|
354
|
-
toDate?: Date;
|
|
355
|
-
dateRange?: { start?: Date; end?: Date };
|
|
356
|
-
}): Promise<PaginationInfo & { evals: EvalRow[] }> {
|
|
357
|
-
return this.stores.legacyEvals.getEvals(options);
|
|
358
|
-
}
|
|
359
|
-
|
|
360
|
-
async getWorkflowRuns(args?: {
|
|
361
|
-
namespace?: string;
|
|
362
|
-
workflowName?: string;
|
|
363
|
-
fromDate?: Date;
|
|
364
|
-
toDate?: Date;
|
|
365
|
-
limit?: number;
|
|
366
|
-
offset?: number;
|
|
367
|
-
}): Promise<WorkflowRuns> {
|
|
368
|
-
return this.stores.workflows.getWorkflowRuns(args);
|
|
369
|
-
}
|
|
370
|
-
|
|
371
|
-
async getWorkflowRunById(args: { runId: string; workflowName?: string }): Promise<{
|
|
372
|
-
workflowName: string;
|
|
373
|
-
runId: string;
|
|
374
|
-
snapshot: any;
|
|
375
|
-
createdAt: Date;
|
|
376
|
-
updatedAt: Date;
|
|
377
|
-
} | null> {
|
|
378
|
-
return this.stores.workflows.getWorkflowRunById(args);
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
async persistWorkflowSnapshot({
|
|
382
|
-
workflowName,
|
|
383
|
-
runId,
|
|
384
|
-
snapshot,
|
|
385
|
-
}: {
|
|
386
|
-
workflowName: string;
|
|
387
|
-
runId: string;
|
|
388
|
-
snapshot: WorkflowRunState;
|
|
389
|
-
}): Promise<void> {
|
|
390
|
-
return this.stores.workflows.persistWorkflowSnapshot({ workflowName, runId, snapshot });
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
async loadWorkflowSnapshot({
|
|
394
|
-
workflowName,
|
|
395
|
-
runId,
|
|
396
|
-
}: {
|
|
397
|
-
workflowName: string;
|
|
398
|
-
runId: string;
|
|
399
|
-
}): Promise<WorkflowRunState | null> {
|
|
400
|
-
return this.stores.workflows.loadWorkflowSnapshot({ workflowName, runId });
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
async getScoreById({ id: _id }: { id: string }): Promise<ScoreRowData | null> {
|
|
404
|
-
return this.stores.scores.getScoreById({ id: _id });
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
async getScoresByScorerId({
|
|
408
|
-
scorerId,
|
|
409
|
-
source,
|
|
410
|
-
entityId,
|
|
411
|
-
entityType,
|
|
412
|
-
pagination,
|
|
413
|
-
}: {
|
|
414
|
-
scorerId: string;
|
|
415
|
-
pagination: StoragePagination;
|
|
416
|
-
source?: ScoringSource;
|
|
417
|
-
entityId?: string;
|
|
418
|
-
entityType?: string;
|
|
419
|
-
}): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
|
|
420
|
-
return this.stores.scores.getScoresByScorerId({ scorerId, source, pagination, entityId, entityType });
|
|
421
|
-
}
|
|
422
|
-
|
|
423
|
-
async saveScore(_score: ScoreRowData): Promise<{ score: ScoreRowData }> {
|
|
424
|
-
return this.stores.scores.saveScore(_score);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
async getScoresByRunId({
|
|
428
|
-
runId,
|
|
429
|
-
pagination,
|
|
430
|
-
}: {
|
|
431
|
-
runId: string;
|
|
432
|
-
pagination: StoragePagination;
|
|
433
|
-
}): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
|
|
434
|
-
return this.stores.scores.getScoresByRunId({ runId, pagination });
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
async getScoresByEntityId({
|
|
438
|
-
entityId,
|
|
439
|
-
entityType,
|
|
440
|
-
pagination,
|
|
441
|
-
}: {
|
|
442
|
-
pagination: StoragePagination;
|
|
443
|
-
entityId: string;
|
|
444
|
-
entityType: string;
|
|
445
|
-
}): Promise<{ pagination: PaginationInfo; scores: ScoreRowData[] }> {
|
|
446
|
-
return this.stores.scores.getScoresByEntityId({ entityId, entityType, pagination });
|
|
447
|
-
}
|
|
448
|
-
}
|