@mastra/upstash 1.0.0-beta.11 → 1.0.0-beta.13

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,4 +1,4 @@
1
- import { MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraStorage, createVectorErrorId, serializeDate, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
1
+ import { MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, filterByDateRange, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, createVectorErrorId, serializeDate, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
2
2
  import { Redis } from '@upstash/redis';
3
3
  import { MessageList } from '@mastra/core/agent';
4
4
  import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
@@ -190,19 +190,34 @@ var StoreMemoryUpstash = class extends MemoryStorage {
190
190
  );
191
191
  }
192
192
  }
193
- async listThreadsByResourceId(args) {
194
- const { resourceId, page = 0, perPage: perPageInput, orderBy } = args;
193
+ async listThreads(args) {
194
+ const { page = 0, perPage: perPageInput, orderBy, filter } = args;
195
195
  const { field, direction } = this.parseOrderBy(orderBy);
196
+ try {
197
+ this.validatePaginationInput(page, perPageInput ?? 100);
198
+ } catch (error) {
199
+ throw new MastraError(
200
+ {
201
+ id: createStorageErrorId("UPSTASH", "LIST_THREADS", "INVALID_PAGE"),
202
+ domain: ErrorDomain.STORAGE,
203
+ category: ErrorCategory.USER,
204
+ details: { page, ...perPageInput !== void 0 && { perPage: perPageInput } }
205
+ },
206
+ error instanceof Error ? error : new Error("Invalid pagination parameters")
207
+ );
208
+ }
196
209
  const perPage = normalizePerPage(perPageInput, 100);
197
- if (page < 0) {
210
+ try {
211
+ this.validateMetadataKeys(filter?.metadata);
212
+ } catch (error) {
198
213
  throw new MastraError(
199
214
  {
200
- id: createStorageErrorId("UPSTASH", "LIST_THREADS_BY_RESOURCE_ID", "INVALID_PAGE"),
215
+ id: createStorageErrorId("UPSTASH", "LIST_THREADS", "INVALID_METADATA_KEY"),
201
216
  domain: ErrorDomain.STORAGE,
202
217
  category: ErrorCategory.USER,
203
- details: { page }
218
+ details: { metadataKeys: filter?.metadata ? Object.keys(filter.metadata).join(", ") : "" }
204
219
  },
205
- new Error("page must be >= 0")
220
+ error instanceof Error ? error : new Error("Invalid metadata key")
206
221
  );
207
222
  }
208
223
  const { offset, perPage: perPageForResponse } = calculatePagination(page, perPageInput, perPage);
@@ -210,19 +225,35 @@ var StoreMemoryUpstash = class extends MemoryStorage {
210
225
  let allThreads = [];
211
226
  const pattern = `${TABLE_THREADS}:*`;
212
227
  const keys = await this.#db.scanKeys(pattern);
228
+ if (keys.length === 0) {
229
+ return {
230
+ threads: [],
231
+ total: 0,
232
+ page,
233
+ perPage: perPageForResponse,
234
+ hasMore: false
235
+ };
236
+ }
213
237
  const pipeline = this.client.pipeline();
214
238
  keys.forEach((key) => pipeline.get(key));
215
239
  const results = await pipeline.exec();
216
240
  for (let i = 0; i < results.length; i++) {
217
241
  const thread = results[i];
218
- if (thread && thread.resourceId === resourceId) {
219
- allThreads.push({
220
- ...thread,
221
- createdAt: ensureDate(thread.createdAt),
222
- updatedAt: ensureDate(thread.updatedAt),
223
- metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
224
- });
242
+ if (!thread) continue;
243
+ if (filter?.resourceId && thread.resourceId !== filter.resourceId) {
244
+ continue;
225
245
  }
246
+ if (filter?.metadata && Object.keys(filter.metadata).length > 0) {
247
+ const threadMetadata = typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata;
248
+ const matches = Object.entries(filter.metadata).every(([key, value]) => threadMetadata?.[key] === value);
249
+ if (!matches) continue;
250
+ }
251
+ allThreads.push({
252
+ ...thread,
253
+ createdAt: ensureDate(thread.createdAt),
254
+ updatedAt: ensureDate(thread.updatedAt),
255
+ metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
256
+ });
226
257
  }
227
258
  const sortedThreads = this.sortThreads(allThreads, field, direction);
228
259
  const total = sortedThreads.length;
@@ -239,11 +270,12 @@ var StoreMemoryUpstash = class extends MemoryStorage {
239
270
  } catch (error) {
240
271
  const mastraError = new MastraError(
241
272
  {
242
- id: createStorageErrorId("UPSTASH", "LIST_THREADS_BY_RESOURCE_ID", "FAILED"),
273
+ id: createStorageErrorId("UPSTASH", "LIST_THREADS", "FAILED"),
243
274
  domain: ErrorDomain.STORAGE,
244
275
  category: ErrorCategory.THIRD_PARTY,
245
276
  details: {
246
- resourceId,
277
+ ...filter?.resourceId && { resourceId: filter.resourceId },
278
+ hasMetadataFilter: !!filter?.metadata,
247
279
  page,
248
280
  perPage
249
281
  }
@@ -1703,7 +1735,7 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1703
1735
  var isClientConfig = (config) => {
1704
1736
  return "client" in config;
1705
1737
  };
1706
- var UpstashStore = class extends MastraStorage {
1738
+ var UpstashStore = class extends MastraCompositeStore {
1707
1739
  redis;
1708
1740
  stores;
1709
1741
  constructor(config) {