@mastra/convex 0.0.1 → 0.1.0-beta.1

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,5 +1,5 @@
1
1
  export { TABLE_MESSAGES, TABLE_RESOURCES, TABLE_SCORERS, TABLE_THREADS, TABLE_WORKFLOW_SNAPSHOT, mastraDocumentsTable, mastraMessagesTable, mastraResourcesTable, mastraScoresTable, mastraStorage, mastraThreadsTable, mastraVectorIndexesTable, mastraVectorsTable, mastraWorkflowSnapshotsTable } from './chunk-NZCHEPNU.js';
2
- import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, normalizePerPage, calculatePagination, safelyParseJSON, TABLE_RESOURCES, WorkflowsStorage, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
2
+ import { MastraStorage, StoreOperations, TABLE_WORKFLOW_SNAPSHOT, MemoryStorage, TABLE_THREADS, createStorageErrorId, TABLE_MESSAGES, normalizePerPage, calculatePagination, safelyParseJSON, TABLE_RESOURCES, WorkflowsStorage, ScoresStorage, TABLE_SCORERS } from '@mastra/core/storage';
3
3
  import { MessageList } from '@mastra/core/agent';
4
4
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
5
5
  import crypto from 'crypto';
@@ -104,7 +104,7 @@ var MemoryConvex = class extends MemoryStorage {
104
104
  const existing = await this.getThreadById({ threadId: id });
105
105
  if (!existing) {
106
106
  throw new MastraError({
107
- id: "CONVEX_STORAGE_THREAD_NOT_FOUND",
107
+ id: createStorageErrorId("CONVEX", "UPDATE_THREAD", "THREAD_NOT_FOUND"),
108
108
  domain: ErrorDomain.STORAGE,
109
109
  category: ErrorCategory.USER,
110
110
  text: `Thread ${id} not found`
@@ -163,23 +163,28 @@ var MemoryConvex = class extends MemoryStorage {
163
163
  }
164
164
  async listMessages(args) {
165
165
  const { threadId, resourceId, include, filter, perPage: perPageInput, page = 0, orderBy } = args;
166
- if (!threadId.trim()) {
166
+ const threadIds = Array.isArray(threadId) ? threadId : [threadId];
167
+ if (threadIds.length === 0 || threadIds.some((id) => !id.trim())) {
167
168
  throw new MastraError(
168
169
  {
169
- id: "CONVEX_STORAGE_LIST_MESSAGES_INVALID_THREAD_ID",
170
+ id: createStorageErrorId("CONVEX", "LIST_MESSAGES", "INVALID_THREAD_ID"),
170
171
  domain: ErrorDomain.STORAGE,
171
172
  category: ErrorCategory.USER,
172
- details: { threadId }
173
+ details: { threadId: Array.isArray(threadId) ? threadId.join(",") : threadId }
173
174
  },
174
- new Error("threadId must be a non-empty string")
175
+ new Error("threadId must be a non-empty string or array of non-empty strings")
175
176
  );
176
177
  }
177
178
  const perPage = normalizePerPage(perPageInput, 40);
178
179
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
179
180
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
180
- let rows = await this.operations.queryTable(TABLE_MESSAGES, [
181
- { field: "thread_id", value: threadId }
182
- ]);
181
+ let rows = [];
182
+ for (const tid of threadIds) {
183
+ const threadRows = await this.operations.queryTable(TABLE_MESSAGES, [
184
+ { field: "thread_id", value: tid }
185
+ ]);
186
+ rows.push(...threadRows);
187
+ }
183
188
  if (resourceId) {
184
189
  rows = rows.filter((row) => row.resourceId === resourceId);
185
190
  }
@@ -206,21 +211,41 @@ var MemoryConvex = class extends MemoryStorage {
206
211
  const messageIds = new Set(messages.map((msg) => msg.id));
207
212
  if (include && include.length > 0) {
208
213
  const threadMessagesCache = /* @__PURE__ */ new Map();
209
- threadMessagesCache.set(threadId, rows);
214
+ for (const tid of threadIds) {
215
+ const tidRows = rows.filter((r) => r.thread_id === tid);
216
+ threadMessagesCache.set(tid, tidRows);
217
+ }
210
218
  for (const includeItem of include) {
211
- const targetThreadId = includeItem.threadId || threadId;
212
- if (!threadMessagesCache.has(targetThreadId)) {
213
- const otherThreadRows = await this.operations.queryTable(TABLE_MESSAGES, [
214
- { field: "thread_id", value: targetThreadId }
219
+ let targetThreadId;
220
+ let target;
221
+ for (const [tid, cachedRows] of threadMessagesCache) {
222
+ target = cachedRows.find((row) => row.id === includeItem.id);
223
+ if (target) {
224
+ targetThreadId = tid;
225
+ break;
226
+ }
227
+ }
228
+ if (!target) {
229
+ const messageRows = await this.operations.queryTable(TABLE_MESSAGES, [
230
+ { field: "id", value: includeItem.id }
215
231
  ]);
216
- threadMessagesCache.set(targetThreadId, otherThreadRows);
232
+ if (messageRows.length > 0) {
233
+ target = messageRows[0];
234
+ targetThreadId = target.thread_id;
235
+ if (targetThreadId && !threadMessagesCache.has(targetThreadId)) {
236
+ const otherThreadRows = await this.operations.queryTable(TABLE_MESSAGES, [
237
+ { field: "thread_id", value: targetThreadId }
238
+ ]);
239
+ threadMessagesCache.set(targetThreadId, otherThreadRows);
240
+ }
241
+ }
217
242
  }
218
- const targetThreadRows = threadMessagesCache.get(targetThreadId) || [];
219
- const target = targetThreadRows.find((row) => row.id === includeItem.id);
220
- if (target && !messageIds.has(target.id)) {
243
+ if (!target || !targetThreadId) continue;
244
+ if (!messageIds.has(target.id)) {
221
245
  messages.push(this.parseStoredMessage(target));
222
246
  messageIds.add(target.id);
223
247
  }
248
+ const targetThreadRows = threadMessagesCache.get(targetThreadId) || [];
224
249
  await this.addContextMessages({
225
250
  includeItem,
226
251
  allMessages: targetThreadRows,
@@ -525,7 +550,7 @@ var ScoresConvex = class extends ScoresStorage {
525
550
  if (pagination.page < 0) {
526
551
  throw new MastraError(
527
552
  {
528
- id: "CONVEX_STORAGE_INVALID_PAGINATION",
553
+ id: createStorageErrorId("CONVEX", "LIST_SCORES", "INVALID_PAGINATION"),
529
554
  domain: ErrorDomain.STORAGE,
530
555
  category: ErrorCategory.USER
531
556
  },
@@ -804,7 +829,7 @@ var ConvexStore = class extends MastraStorage {
804
829
  workflows;
805
830
  scores;
806
831
  constructor(config) {
807
- super({ id: config.id, name: config.name ?? "ConvexStore" });
832
+ super({ id: config.id, name: config.name ?? "ConvexStore", disableInit: config.disableInit });
808
833
  const client = new ConvexAdminClient(config);
809
834
  this.operations = new StoreOperationsConvex(client);
810
835
  this.memory = new MemoryConvex(this.operations);