@mastra/convex 1.0.7 → 1.0.8

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/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
1
  export { mastraStorage } from './chunk-SDTI6G3V.js';
2
2
  export { TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, mastraDocumentsTable, mastraMessagesTable, mastraResourcesTable, mastraScoresTable, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from './chunk-G5FLGAPE.js';
3
- import { MastraCompositeStore, createVectorErrorId, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, safelyParseJSON, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
4
- import { MessageList } from '@mastra/core/agent';
5
- import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
3
+ import { MastraCompositeStore, createVectorErrorId, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, safelyParseJSON, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, ScoresStorage, TABLE_SCORERS, BackgroundTasksStorage, TABLE_BACKGROUND_TASKS } from '@mastra/core/storage';
6
4
  import crypto from 'crypto';
7
5
  import { MastraBase } from '@mastra/core/base';
6
+ import { MessageList } from '@mastra/core/agent';
7
+ import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
8
8
  import { MastraVector } from '@mastra/core/vector';
9
9
 
10
10
  // src/storage/client.ts
@@ -81,6 +81,7 @@ var ConvexDB = class extends MastraBase {
81
81
  super({ name: "convex-db" });
82
82
  this.client = client;
83
83
  }
84
+ client;
84
85
  async hasColumn(_table, _column) {
85
86
  return true;
86
87
  }
@@ -175,7 +176,134 @@ var ConvexDB = class extends MastraBase {
175
176
  }
176
177
  };
177
178
 
178
- // src/storage/domains/memory/index.ts
179
+ // src/storage/domains/background-tasks/index.ts
180
+ function serializeJson(v) {
181
+ if (typeof v === "object" && v != null) return JSON.stringify(v);
182
+ return v ?? void 0;
183
+ }
184
+ function toStored(task) {
185
+ return {
186
+ ...task,
187
+ args: serializeJson(task.args),
188
+ result: serializeJson(task.result),
189
+ error: serializeJson(task.error),
190
+ createdAt: task.createdAt.toISOString(),
191
+ startedAt: task.startedAt?.toISOString(),
192
+ completedAt: task.completedAt?.toISOString()
193
+ };
194
+ }
195
+ function fromStored(stored) {
196
+ const parseJson = (val) => {
197
+ if (!val) return void 0;
198
+ try {
199
+ return JSON.parse(val);
200
+ } catch {
201
+ return val;
202
+ }
203
+ };
204
+ return {
205
+ id: stored.id,
206
+ status: stored.status,
207
+ toolName: stored.toolName,
208
+ toolCallId: stored.toolCallId,
209
+ args: parseJson(stored.args) ?? {},
210
+ agentId: stored.agentId,
211
+ threadId: stored.threadId,
212
+ resourceId: stored.resourceId,
213
+ runId: stored.runId,
214
+ result: parseJson(stored.result),
215
+ error: parseJson(stored.error),
216
+ retryCount: stored.retryCount,
217
+ maxRetries: stored.maxRetries,
218
+ timeoutMs: stored.timeoutMs,
219
+ createdAt: new Date(stored.createdAt),
220
+ startedAt: stored.startedAt ? new Date(stored.startedAt) : void 0,
221
+ completedAt: stored.completedAt ? new Date(stored.completedAt) : void 0
222
+ };
223
+ }
224
+ var BackgroundTasksConvex = class extends BackgroundTasksStorage {
225
+ #db;
226
+ constructor(config) {
227
+ super();
228
+ const client = resolveConvexConfig(config);
229
+ this.#db = new ConvexDB(client);
230
+ }
231
+ async dangerouslyClearAll() {
232
+ await this.#db.clearTable({ tableName: TABLE_BACKGROUND_TASKS });
233
+ }
234
+ async createTask(task) {
235
+ await this.#db.insert({ tableName: TABLE_BACKGROUND_TASKS, record: toStored(task) });
236
+ }
237
+ async updateTask(taskId, update) {
238
+ const existing = await this.getTask(taskId);
239
+ if (!existing) return;
240
+ const merged = { ...existing };
241
+ if ("status" in update) merged.status = update.status;
242
+ if ("result" in update) merged.result = update.result;
243
+ if ("error" in update) merged.error = update.error;
244
+ if ("retryCount" in update) merged.retryCount = update.retryCount;
245
+ if ("startedAt" in update) merged.startedAt = update.startedAt;
246
+ if ("completedAt" in update) merged.completedAt = update.completedAt;
247
+ await this.#db.deleteMany(TABLE_BACKGROUND_TASKS, [taskId]);
248
+ await this.#db.insert({ tableName: TABLE_BACKGROUND_TASKS, record: toStored(merged) });
249
+ }
250
+ async getTask(taskId) {
251
+ const data = await this.#db.load({ tableName: TABLE_BACKGROUND_TASKS, keys: { id: taskId } });
252
+ return data ? fromStored(data) : null;
253
+ }
254
+ async listTasks(filter) {
255
+ const all = await this.#db.queryTable(TABLE_BACKGROUND_TASKS);
256
+ let tasks = all.map(fromStored);
257
+ if (filter.status) {
258
+ const s = Array.isArray(filter.status) ? filter.status : [filter.status];
259
+ tasks = tasks.filter((t) => s.includes(t.status));
260
+ }
261
+ if (filter.agentId) tasks = tasks.filter((t) => t.agentId === filter.agentId);
262
+ if (filter.threadId) tasks = tasks.filter((t) => t.threadId === filter.threadId);
263
+ if (filter.toolName) tasks = tasks.filter((t) => t.toolName === filter.toolName);
264
+ if (filter.runId) tasks = tasks.filter((t) => t.runId === filter.runId);
265
+ const dateCol = filter.dateFilterBy ?? "createdAt";
266
+ if (filter.fromDate) {
267
+ tasks = tasks.filter((t) => {
268
+ const val = t[dateCol];
269
+ return val != null && val >= filter.fromDate;
270
+ });
271
+ }
272
+ if (filter.toDate) {
273
+ tasks = tasks.filter((t) => {
274
+ const val = t[dateCol];
275
+ return val != null && val < filter.toDate;
276
+ });
277
+ }
278
+ const orderBy = filter.orderBy ?? "createdAt";
279
+ const dir = filter.orderDirection === "desc" ? -1 : 1;
280
+ tasks.sort((a, b) => ((a[orderBy]?.getTime() ?? 0) - (b[orderBy]?.getTime() ?? 0)) * dir);
281
+ const total = tasks.length;
282
+ if (filter.page != null && filter.perPage != null) {
283
+ const start = filter.page * filter.perPage;
284
+ tasks = tasks.slice(start, start + filter.perPage);
285
+ } else if (filter.perPage != null) {
286
+ tasks = tasks.slice(0, filter.perPage);
287
+ }
288
+ return { tasks, total };
289
+ }
290
+ async deleteTask(taskId) {
291
+ await this.#db.deleteMany(TABLE_BACKGROUND_TASKS, [taskId]);
292
+ }
293
+ async deleteTasks(filter) {
294
+ const { tasks } = await this.listTasks(filter);
295
+ const taskIds = tasks.map((t) => t.id);
296
+ await this.#db.deleteMany(TABLE_BACKGROUND_TASKS, taskIds);
297
+ }
298
+ async getRunningCount() {
299
+ const { total } = await this.listTasks({ status: "running" });
300
+ return total;
301
+ }
302
+ async getRunningCountByAgent(agentId) {
303
+ const { total } = await this.listTasks({ status: "running", agentId });
304
+ return total;
305
+ }
306
+ };
179
307
  var MemoryConvex = class extends MemoryStorage {
180
308
  #db;
181
309
  constructor(config) {
@@ -933,7 +1061,8 @@ var ConvexStore = class extends MastraCompositeStore {
933
1061
  this.stores = {
934
1062
  memory,
935
1063
  workflows,
936
- scores
1064
+ scores,
1065
+ backgroundTasks: new BackgroundTasksConvex(domainConfig)
937
1066
  };
938
1067
  }
939
1068
  };