@mastra/upstash 0.12.3 → 0.12.4-alpha.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
@@ -949,6 +949,60 @@ var StoreMemoryUpstash = class extends MemoryStorage {
949
949
  );
950
950
  }
951
951
  }
952
+ async deleteMessages(messageIds) {
953
+ if (!messageIds || messageIds.length === 0) {
954
+ return;
955
+ }
956
+ try {
957
+ const threadIds = /* @__PURE__ */ new Set();
958
+ const messageKeys = [];
959
+ for (const messageId of messageIds) {
960
+ const pattern = getMessageKey("*", messageId);
961
+ const keys = await this.operations.scanKeys(pattern);
962
+ for (const key of keys) {
963
+ const message = await this.client.get(key);
964
+ if (message && message.id === messageId) {
965
+ messageKeys.push(key);
966
+ if (message.threadId) {
967
+ threadIds.add(message.threadId);
968
+ }
969
+ break;
970
+ }
971
+ }
972
+ }
973
+ if (messageKeys.length === 0) {
974
+ return;
975
+ }
976
+ const pipeline = this.client.pipeline();
977
+ for (const key of messageKeys) {
978
+ pipeline.del(key);
979
+ }
980
+ if (threadIds.size > 0) {
981
+ for (const threadId of threadIds) {
982
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
983
+ const thread = await this.client.get(threadKey);
984
+ if (thread) {
985
+ const updatedThread = {
986
+ ...thread,
987
+ updatedAt: /* @__PURE__ */ new Date()
988
+ };
989
+ pipeline.set(threadKey, processRecord(TABLE_THREADS, updatedThread).processedRecord);
990
+ }
991
+ }
992
+ }
993
+ await pipeline.exec();
994
+ } catch (error) {
995
+ throw new MastraError(
996
+ {
997
+ id: "STORAGE_UPSTASH_DELETE_MESSAGES_FAILED",
998
+ domain: ErrorDomain.STORAGE,
999
+ category: ErrorCategory.THIRD_PARTY,
1000
+ details: { messageIds: messageIds.join(", ") }
1001
+ },
1002
+ error
1003
+ );
1004
+ }
1005
+ }
952
1006
  };
953
1007
  var StoreOperationsUpstash = class extends StoreOperations {
954
1008
  client;
@@ -1613,7 +1667,8 @@ var UpstashStore = class extends MastraStorage {
1613
1667
  selectByIncludeResourceScope: true,
1614
1668
  resourceWorkingMemory: true,
1615
1669
  hasColumn: false,
1616
- createTable: false
1670
+ createTable: false,
1671
+ deleteMessages: true
1617
1672
  };
1618
1673
  }
1619
1674
  /**
@@ -1737,6 +1792,9 @@ var UpstashStore = class extends MastraStorage {
1737
1792
  async updateMessages(args) {
1738
1793
  return this.stores.memory.updateMessages(args);
1739
1794
  }
1795
+ async deleteMessages(messageIds) {
1796
+ return this.stores.memory.deleteMessages(messageIds);
1797
+ }
1740
1798
  async getResourceById({ resourceId }) {
1741
1799
  return this.stores.memory.getResourceById({ resourceId });
1742
1800
  }
@@ -1993,11 +2051,18 @@ var UpstashVector = class extends MastraVector {
1993
2051
  * @param {UpsertVectorParams} params - The parameters for the upsert operation.
1994
2052
  * @returns {Promise<string[]>} A promise that resolves to the IDs of the upserted vectors.
1995
2053
  */
1996
- async upsert({ indexName: namespace, vectors, metadata, ids }) {
2054
+ async upsert({
2055
+ indexName: namespace,
2056
+ vectors,
2057
+ metadata,
2058
+ ids,
2059
+ sparseVectors
2060
+ }) {
1997
2061
  const generatedIds = ids || vectors.map(() => crypto.randomUUID());
1998
2062
  const points = vectors.map((vector, index) => ({
1999
2063
  id: generatedIds[index],
2000
2064
  vector,
2065
+ ...sparseVectors?.[index] && { sparseVector: sparseVectors[index] },
2001
2066
  metadata: metadata?.[index]
2002
2067
  }));
2003
2068
  try {
@@ -2044,7 +2109,10 @@ var UpstashVector = class extends MastraVector {
2044
2109
  queryVector,
2045
2110
  topK = 10,
2046
2111
  filter,
2047
- includeVector = false
2112
+ includeVector = false,
2113
+ sparseVector,
2114
+ fusionAlgorithm,
2115
+ queryMode
2048
2116
  }) {
2049
2117
  try {
2050
2118
  const ns = this.client.namespace(namespace);
@@ -2052,9 +2120,12 @@ var UpstashVector = class extends MastraVector {
2052
2120
  const results = await ns.query({
2053
2121
  topK,
2054
2122
  vector: queryVector,
2123
+ ...sparseVector && { sparseVector },
2055
2124
  includeVectors: includeVector,
2056
2125
  includeMetadata: true,
2057
- ...filterString ? { filter: filterString } : {}
2126
+ ...filterString ? { filter: filterString } : {},
2127
+ ...fusionAlgorithm && { fusionAlgorithm },
2128
+ ...queryMode && { queryMode }
2058
2129
  });
2059
2130
  return (results || []).map((result) => ({
2060
2131
  id: `${result.id}`,
@@ -2150,40 +2221,30 @@ var UpstashVector = class extends MastraVector {
2150
2221
  * @throws Will throw an error if no updates are provided or if the update operation fails.
2151
2222
  */
2152
2223
  async updateVector({ indexName: namespace, id, update }) {
2153
- try {
2154
- if (!update.vector && !update.metadata) {
2155
- throw new Error("No update data provided");
2156
- }
2157
- if (!update.vector && update.metadata) {
2158
- throw new Error("Both vector and metadata must be provided for an update");
2159
- }
2160
- } catch (error) {
2161
- throw new MastraError(
2162
- {
2163
- id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
2164
- domain: ErrorDomain.STORAGE,
2165
- category: ErrorCategory.THIRD_PARTY,
2166
- details: { namespace, id }
2167
- },
2168
- error
2169
- );
2224
+ if (!update.vector && !update.metadata && !update.sparseVector) {
2225
+ throw new MastraError({
2226
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
2227
+ domain: ErrorDomain.STORAGE,
2228
+ category: ErrorCategory.THIRD_PARTY,
2229
+ details: { namespace, id },
2230
+ text: "No update data provided"
2231
+ });
2170
2232
  }
2171
- try {
2172
- const updatePayload = { id };
2173
- if (update.vector) {
2174
- updatePayload.vector = update.vector;
2175
- }
2176
- if (update.metadata) {
2177
- updatePayload.metadata = update.metadata;
2178
- }
2179
- const points = {
2180
- id: updatePayload.id,
2181
- vector: updatePayload.vector,
2182
- metadata: updatePayload.metadata
2183
- };
2184
- await this.client.upsert(points, {
2185
- namespace
2233
+ if (!update.vector && !update.sparseVector && update.metadata) {
2234
+ throw new MastraError({
2235
+ id: "STORAGE_UPSTASH_VECTOR_UPDATE_VECTOR_FAILED",
2236
+ domain: ErrorDomain.STORAGE,
2237
+ category: ErrorCategory.THIRD_PARTY,
2238
+ details: { namespace, id },
2239
+ text: "Both vector and metadata must be provided for an update"
2186
2240
  });
2241
+ }
2242
+ try {
2243
+ const points = { id };
2244
+ if (update.vector) points.vector = update.vector;
2245
+ if (update.metadata) points.metadata = update.metadata;
2246
+ if (update.sparseVector) points.sparseVector = update.sparseVector;
2247
+ await this.client.upsert(points, { namespace });
2187
2248
  } catch (error) {
2188
2249
  throw new MastraError(
2189
2250
  {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mastra/upstash",
3
- "version": "0.12.3",
3
+ "version": "0.12.4-alpha.1",
4
4
  "description": "Upstash provider for Mastra - includes both vector and db storage capabilities",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -20,7 +20,7 @@
20
20
  },
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "@upstash/redis": "^1.35.0",
23
+ "@upstash/redis": "^1.35.1",
24
24
  "@upstash/vector": "^1.2.2"
25
25
  },
26
26
  "devDependencies": {
@@ -31,9 +31,9 @@
31
31
  "tsup": "^8.5.0",
32
32
  "typescript": "^5.8.3",
33
33
  "vitest": "^3.2.4",
34
- "@internal/lint": "0.0.22",
35
- "@internal/storage-test-utils": "0.0.18",
36
- "@mastra/core": "0.11.1"
34
+ "@internal/lint": "0.0.23",
35
+ "@internal/storage-test-utils": "0.0.19",
36
+ "@mastra/core": "0.12.0-alpha.2"
37
37
  },
38
38
  "peerDependencies": {
39
39
  "@mastra/core": ">=0.10.7-0 <0.12.0-0"
@@ -899,4 +899,74 @@ export class StoreMemoryUpstash extends MemoryStorage {
899
899
  );
900
900
  }
901
901
  }
902
+
903
+ async deleteMessages(messageIds: string[]): Promise<void> {
904
+ if (!messageIds || messageIds.length === 0) {
905
+ return;
906
+ }
907
+
908
+ try {
909
+ const threadIds = new Set<string>();
910
+ const messageKeys: string[] = [];
911
+
912
+ // Find all message keys and collect thread IDs
913
+ for (const messageId of messageIds) {
914
+ const pattern = getMessageKey('*', messageId);
915
+ const keys = await this.operations.scanKeys(pattern);
916
+
917
+ for (const key of keys) {
918
+ const message = await this.client.get<MastraMessageV2 | MastraMessageV1>(key);
919
+ if (message && message.id === messageId) {
920
+ messageKeys.push(key);
921
+ if (message.threadId) {
922
+ threadIds.add(message.threadId);
923
+ }
924
+ break;
925
+ }
926
+ }
927
+ }
928
+
929
+ if (messageKeys.length === 0) {
930
+ // none of the message ids existed
931
+ return;
932
+ }
933
+
934
+ const pipeline = this.client.pipeline();
935
+
936
+ // Delete all messages
937
+ for (const key of messageKeys) {
938
+ pipeline.del(key);
939
+ }
940
+
941
+ // Update thread timestamps
942
+ if (threadIds.size > 0) {
943
+ for (const threadId of threadIds) {
944
+ const threadKey = getKey(TABLE_THREADS, { id: threadId });
945
+ const thread = await this.client.get<StorageThreadType>(threadKey);
946
+ if (thread) {
947
+ const updatedThread = {
948
+ ...thread,
949
+ updatedAt: new Date(),
950
+ };
951
+ pipeline.set(threadKey, processRecord(TABLE_THREADS, updatedThread).processedRecord);
952
+ }
953
+ }
954
+ }
955
+
956
+ // Execute all operations
957
+ await pipeline.exec();
958
+
959
+ // TODO: Delete from vector store if semantic recall is enabled
960
+ } catch (error) {
961
+ throw new MastraError(
962
+ {
963
+ id: 'STORAGE_UPSTASH_DELETE_MESSAGES_FAILED',
964
+ domain: ErrorDomain.STORAGE,
965
+ category: ErrorCategory.THIRD_PARTY,
966
+ details: { messageIds: messageIds.join(', ') },
967
+ },
968
+ error,
969
+ );
970
+ }
971
+ }
902
972
  }
@@ -65,6 +65,7 @@ export class UpstashStore extends MastraStorage {
65
65
  resourceWorkingMemory: true,
66
66
  hasColumn: false,
67
67
  createTable: false,
68
+ deleteMessages: true,
68
69
  };
69
70
  }
70
71
 
@@ -281,6 +282,10 @@ export class UpstashStore extends MastraStorage {
281
282
  return this.stores.memory.updateMessages(args);
282
283
  }
283
284
 
285
+ async deleteMessages(messageIds: string[]): Promise<void> {
286
+ return this.stores.memory.deleteMessages(messageIds);
287
+ }
288
+
284
289
  async getResourceById({ resourceId }: { resourceId: string }): Promise<StorageResourceType | null> {
285
290
  return this.stores.memory.getResourceById({ resourceId });
286
291
  }