@mastra/upstash 1.0.0-beta.7 → 1.0.0-beta.9
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 +234 -0
- package/dist/index.cjs +260 -355
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +253 -351
- package/dist/index.js.map +1 -1
- package/dist/storage/db/index.d.ts +47 -0
- package/dist/storage/db/index.d.ts.map +1 -0
- package/dist/storage/domains/memory/index.d.ts +4 -7
- package/dist/storage/domains/memory/index.d.ts.map +1 -1
- package/dist/storage/domains/scores/index.d.ts +4 -7
- package/dist/storage/domains/scores/index.d.ts.map +1 -1
- package/dist/storage/domains/utils.d.ts +0 -2
- package/dist/storage/domains/utils.d.ts.map +1 -1
- package/dist/storage/domains/workflows/index.d.ts +10 -11
- package/dist/storage/domains/workflows/index.d.ts.map +1 -1
- package/dist/storage/index.d.ts +58 -167
- package/dist/storage/index.d.ts.map +1 -1
- package/package.json +4 -4
- package/dist/storage/domains/operations/index.d.ts +0 -40
- package/dist/storage/domains/operations/index.d.ts.map +0 -1
package/dist/index.cjs
CHANGED
|
@@ -11,13 +11,6 @@ var vector$1 = require('@upstash/vector');
|
|
|
11
11
|
var filter = require('@mastra/core/vector/filter');
|
|
12
12
|
|
|
13
13
|
// src/storage/index.ts
|
|
14
|
-
function ensureDate(value) {
|
|
15
|
-
if (!value) return null;
|
|
16
|
-
if (value instanceof Date) return value;
|
|
17
|
-
if (typeof value === "string") return new Date(value);
|
|
18
|
-
if (typeof value === "number") return new Date(value);
|
|
19
|
-
return null;
|
|
20
|
-
}
|
|
21
14
|
function getKey(tableName, keys) {
|
|
22
15
|
const keyParts = Object.entries(keys).filter(([_, value]) => value !== void 0).map(([key, value]) => `${key}:${value}`);
|
|
23
16
|
return `${tableName}:${keyParts.join(":")}`;
|
|
@@ -46,6 +39,107 @@ function processRecord(tableName, record) {
|
|
|
46
39
|
return { key, processedRecord };
|
|
47
40
|
}
|
|
48
41
|
|
|
42
|
+
// src/storage/db/index.ts
|
|
43
|
+
function resolveUpstashConfig(config) {
|
|
44
|
+
if ("client" in config) {
|
|
45
|
+
return config.client;
|
|
46
|
+
}
|
|
47
|
+
return new redis.Redis({
|
|
48
|
+
url: config.url,
|
|
49
|
+
token: config.token
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
var UpstashDB = class {
|
|
53
|
+
client;
|
|
54
|
+
constructor({ client }) {
|
|
55
|
+
this.client = client;
|
|
56
|
+
}
|
|
57
|
+
async insert({ tableName, record }) {
|
|
58
|
+
const { key, processedRecord } = processRecord(tableName, record);
|
|
59
|
+
try {
|
|
60
|
+
await this.client.set(key, processedRecord);
|
|
61
|
+
} catch (error$1) {
|
|
62
|
+
throw new error.MastraError(
|
|
63
|
+
{
|
|
64
|
+
id: storage.createStorageErrorId("UPSTASH", "INSERT", "FAILED"),
|
|
65
|
+
domain: error.ErrorDomain.STORAGE,
|
|
66
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
67
|
+
details: {
|
|
68
|
+
tableName
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
error$1
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
async get({ tableName, keys }) {
|
|
76
|
+
const key = getKey(tableName, keys);
|
|
77
|
+
try {
|
|
78
|
+
const data = await this.client.get(key);
|
|
79
|
+
return data || null;
|
|
80
|
+
} catch (error$1) {
|
|
81
|
+
throw new error.MastraError(
|
|
82
|
+
{
|
|
83
|
+
id: storage.createStorageErrorId("UPSTASH", "LOAD", "FAILED"),
|
|
84
|
+
domain: error.ErrorDomain.STORAGE,
|
|
85
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
86
|
+
details: {
|
|
87
|
+
tableName
|
|
88
|
+
}
|
|
89
|
+
},
|
|
90
|
+
error$1
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
async scanAndDelete(pattern, batchSize = 1e4) {
|
|
95
|
+
let cursor = "0";
|
|
96
|
+
let totalDeleted = 0;
|
|
97
|
+
do {
|
|
98
|
+
const [nextCursor, keys] = await this.client.scan(cursor, {
|
|
99
|
+
match: pattern,
|
|
100
|
+
count: batchSize
|
|
101
|
+
});
|
|
102
|
+
if (keys.length > 0) {
|
|
103
|
+
await this.client.del(...keys);
|
|
104
|
+
totalDeleted += keys.length;
|
|
105
|
+
}
|
|
106
|
+
cursor = nextCursor;
|
|
107
|
+
} while (cursor !== "0");
|
|
108
|
+
return totalDeleted;
|
|
109
|
+
}
|
|
110
|
+
async scanKeys(pattern, batchSize = 1e4) {
|
|
111
|
+
let cursor = "0";
|
|
112
|
+
let keys = [];
|
|
113
|
+
do {
|
|
114
|
+
const [nextCursor, batch] = await this.client.scan(cursor, {
|
|
115
|
+
match: pattern,
|
|
116
|
+
count: batchSize
|
|
117
|
+
});
|
|
118
|
+
keys.push(...batch);
|
|
119
|
+
cursor = nextCursor;
|
|
120
|
+
} while (cursor !== "0");
|
|
121
|
+
return keys;
|
|
122
|
+
}
|
|
123
|
+
async deleteData({ tableName }) {
|
|
124
|
+
const pattern = `${tableName}:*`;
|
|
125
|
+
try {
|
|
126
|
+
await this.scanAndDelete(pattern);
|
|
127
|
+
} catch (error$1) {
|
|
128
|
+
throw new error.MastraError(
|
|
129
|
+
{
|
|
130
|
+
id: storage.createStorageErrorId("UPSTASH", "CLEAR_TABLE", "FAILED"),
|
|
131
|
+
domain: error.ErrorDomain.STORAGE,
|
|
132
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
133
|
+
details: {
|
|
134
|
+
tableName
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
error$1
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
};
|
|
142
|
+
|
|
49
143
|
// src/storage/domains/memory/index.ts
|
|
50
144
|
function getThreadMessagesKey(threadId) {
|
|
51
145
|
return `thread:${threadId}:messages`;
|
|
@@ -59,23 +153,29 @@ function getMessageIndexKey(messageId) {
|
|
|
59
153
|
}
|
|
60
154
|
var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
61
155
|
client;
|
|
62
|
-
|
|
63
|
-
constructor(
|
|
156
|
+
#db;
|
|
157
|
+
constructor(config) {
|
|
64
158
|
super();
|
|
159
|
+
const client = resolveUpstashConfig(config);
|
|
65
160
|
this.client = client;
|
|
66
|
-
this
|
|
161
|
+
this.#db = new UpstashDB({ client });
|
|
162
|
+
}
|
|
163
|
+
async dangerouslyClearAll() {
|
|
164
|
+
await this.#db.deleteData({ tableName: storage.TABLE_THREADS });
|
|
165
|
+
await this.#db.deleteData({ tableName: storage.TABLE_MESSAGES });
|
|
166
|
+
await this.#db.deleteData({ tableName: storage.TABLE_RESOURCES });
|
|
67
167
|
}
|
|
68
168
|
async getThreadById({ threadId }) {
|
|
69
169
|
try {
|
|
70
|
-
const thread = await this.
|
|
170
|
+
const thread = await this.#db.get({
|
|
71
171
|
tableName: storage.TABLE_THREADS,
|
|
72
172
|
keys: { id: threadId }
|
|
73
173
|
});
|
|
74
174
|
if (!thread) return null;
|
|
75
175
|
return {
|
|
76
176
|
...thread,
|
|
77
|
-
createdAt: ensureDate(thread.createdAt),
|
|
78
|
-
updatedAt: ensureDate(thread.updatedAt),
|
|
177
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
178
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
79
179
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
|
|
80
180
|
};
|
|
81
181
|
} catch (error$1) {
|
|
@@ -111,7 +211,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
111
211
|
try {
|
|
112
212
|
let allThreads = [];
|
|
113
213
|
const pattern = `${storage.TABLE_THREADS}:*`;
|
|
114
|
-
const keys = await this.
|
|
214
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
115
215
|
const pipeline = this.client.pipeline();
|
|
116
216
|
keys.forEach((key) => pipeline.get(key));
|
|
117
217
|
const results = await pipeline.exec();
|
|
@@ -120,8 +220,8 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
120
220
|
if (thread && thread.resourceId === resourceId) {
|
|
121
221
|
allThreads.push({
|
|
122
222
|
...thread,
|
|
123
|
-
createdAt: ensureDate(thread.createdAt),
|
|
124
|
-
updatedAt: ensureDate(thread.updatedAt),
|
|
223
|
+
createdAt: storage.ensureDate(thread.createdAt),
|
|
224
|
+
updatedAt: storage.ensureDate(thread.updatedAt),
|
|
125
225
|
metadata: typeof thread.metadata === "string" ? JSON.parse(thread.metadata) : thread.metadata
|
|
126
226
|
});
|
|
127
227
|
}
|
|
@@ -165,7 +265,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
165
265
|
}
|
|
166
266
|
async saveThread({ thread }) {
|
|
167
267
|
try {
|
|
168
|
-
await this.
|
|
268
|
+
await this.#db.insert({
|
|
169
269
|
tableName: storage.TABLE_THREADS,
|
|
170
270
|
record: thread
|
|
171
271
|
});
|
|
@@ -243,7 +343,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
243
343
|
pipeline.del(messageKey);
|
|
244
344
|
}
|
|
245
345
|
await pipeline.exec();
|
|
246
|
-
await this.
|
|
346
|
+
await this.#db.scanAndDelete(getMessageKey(threadId, "*"));
|
|
247
347
|
} catch (error$1) {
|
|
248
348
|
throw new error.MastraError(
|
|
249
349
|
{
|
|
@@ -308,7 +408,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
308
408
|
const createdAtScore = new Date(message.createdAt).getTime();
|
|
309
409
|
const score = message._index !== void 0 ? message._index : createdAtScore;
|
|
310
410
|
const existingKeyPattern = getMessageKey("*", message.id);
|
|
311
|
-
const keys = await this.
|
|
411
|
+
const keys = await this.#db.scanKeys(existingKeyPattern);
|
|
312
412
|
if (keys.length > 0) {
|
|
313
413
|
const pipeline2 = this.client.pipeline();
|
|
314
414
|
keys.forEach((key2) => pipeline2.get(key2));
|
|
@@ -363,7 +463,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
363
463
|
return indexedThreadId;
|
|
364
464
|
}
|
|
365
465
|
const existingKeyPattern = getMessageKey("*", messageId);
|
|
366
|
-
const keys = await this.
|
|
466
|
+
const keys = await this.#db.scanKeys(existingKeyPattern);
|
|
367
467
|
if (keys.length === 0) return null;
|
|
368
468
|
const messageData = await this.client.get(keys[0]);
|
|
369
469
|
if (!messageData) return null;
|
|
@@ -704,7 +804,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
704
804
|
const messageIdToKey = {};
|
|
705
805
|
for (const messageId of messageIds) {
|
|
706
806
|
const pattern = getMessageKey("*", messageId);
|
|
707
|
-
const keys = await this.
|
|
807
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
708
808
|
for (const key of keys) {
|
|
709
809
|
const message = await this.client.get(key);
|
|
710
810
|
if (message && message.id === messageId) {
|
|
@@ -833,7 +933,7 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
833
933
|
}
|
|
834
934
|
for (const messageId of unindexedMessageIds) {
|
|
835
935
|
const pattern = getMessageKey("*", messageId);
|
|
836
|
-
const keys = await this.
|
|
936
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
837
937
|
for (const key of keys) {
|
|
838
938
|
const message = await this.client.get(key);
|
|
839
939
|
if (message && message.id === messageId) {
|
|
@@ -894,156 +994,24 @@ var StoreMemoryUpstash = class extends storage.MemoryStorage {
|
|
|
894
994
|
});
|
|
895
995
|
}
|
|
896
996
|
};
|
|
897
|
-
var StoreOperationsUpstash = class extends storage.StoreOperations {
|
|
898
|
-
client;
|
|
899
|
-
constructor({ client }) {
|
|
900
|
-
super();
|
|
901
|
-
this.client = client;
|
|
902
|
-
}
|
|
903
|
-
async createTable({
|
|
904
|
-
tableName: _tableName,
|
|
905
|
-
schema: _schema
|
|
906
|
-
}) {
|
|
907
|
-
}
|
|
908
|
-
async alterTable({
|
|
909
|
-
tableName: _tableName,
|
|
910
|
-
schema: _schema,
|
|
911
|
-
ifNotExists: _ifNotExists
|
|
912
|
-
}) {
|
|
913
|
-
}
|
|
914
|
-
async clearTable({ tableName }) {
|
|
915
|
-
const pattern = `${tableName}:*`;
|
|
916
|
-
try {
|
|
917
|
-
await this.scanAndDelete(pattern);
|
|
918
|
-
} catch (error$1) {
|
|
919
|
-
throw new error.MastraError(
|
|
920
|
-
{
|
|
921
|
-
id: storage.createStorageErrorId("UPSTASH", "CLEAR_TABLE", "FAILED"),
|
|
922
|
-
domain: error.ErrorDomain.STORAGE,
|
|
923
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
924
|
-
details: {
|
|
925
|
-
tableName
|
|
926
|
-
}
|
|
927
|
-
},
|
|
928
|
-
error$1
|
|
929
|
-
);
|
|
930
|
-
}
|
|
931
|
-
}
|
|
932
|
-
async dropTable({ tableName }) {
|
|
933
|
-
return this.clearTable({ tableName });
|
|
934
|
-
}
|
|
935
|
-
async insert({ tableName, record }) {
|
|
936
|
-
const { key, processedRecord } = processRecord(tableName, record);
|
|
937
|
-
try {
|
|
938
|
-
await this.client.set(key, processedRecord);
|
|
939
|
-
} catch (error$1) {
|
|
940
|
-
throw new error.MastraError(
|
|
941
|
-
{
|
|
942
|
-
id: storage.createStorageErrorId("UPSTASH", "INSERT", "FAILED"),
|
|
943
|
-
domain: error.ErrorDomain.STORAGE,
|
|
944
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
945
|
-
details: {
|
|
946
|
-
tableName
|
|
947
|
-
}
|
|
948
|
-
},
|
|
949
|
-
error$1
|
|
950
|
-
);
|
|
951
|
-
}
|
|
952
|
-
}
|
|
953
|
-
async batchInsert(input) {
|
|
954
|
-
const { tableName, records } = input;
|
|
955
|
-
if (!records.length) return;
|
|
956
|
-
const batchSize = 1e3;
|
|
957
|
-
try {
|
|
958
|
-
for (let i = 0; i < records.length; i += batchSize) {
|
|
959
|
-
const batch = records.slice(i, i + batchSize);
|
|
960
|
-
const pipeline = this.client.pipeline();
|
|
961
|
-
for (const record of batch) {
|
|
962
|
-
const { key, processedRecord } = processRecord(tableName, record);
|
|
963
|
-
pipeline.set(key, processedRecord);
|
|
964
|
-
}
|
|
965
|
-
await pipeline.exec();
|
|
966
|
-
}
|
|
967
|
-
} catch (error$1) {
|
|
968
|
-
throw new error.MastraError(
|
|
969
|
-
{
|
|
970
|
-
id: storage.createStorageErrorId("UPSTASH", "BATCH_INSERT", "FAILED"),
|
|
971
|
-
domain: error.ErrorDomain.STORAGE,
|
|
972
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
973
|
-
details: {
|
|
974
|
-
tableName
|
|
975
|
-
}
|
|
976
|
-
},
|
|
977
|
-
error$1
|
|
978
|
-
);
|
|
979
|
-
}
|
|
980
|
-
}
|
|
981
|
-
async load({ tableName, keys }) {
|
|
982
|
-
const key = getKey(tableName, keys);
|
|
983
|
-
try {
|
|
984
|
-
const data = await this.client.get(key);
|
|
985
|
-
return data || null;
|
|
986
|
-
} catch (error$1) {
|
|
987
|
-
throw new error.MastraError(
|
|
988
|
-
{
|
|
989
|
-
id: storage.createStorageErrorId("UPSTASH", "LOAD", "FAILED"),
|
|
990
|
-
domain: error.ErrorDomain.STORAGE,
|
|
991
|
-
category: error.ErrorCategory.THIRD_PARTY,
|
|
992
|
-
details: {
|
|
993
|
-
tableName
|
|
994
|
-
}
|
|
995
|
-
},
|
|
996
|
-
error$1
|
|
997
|
-
);
|
|
998
|
-
}
|
|
999
|
-
}
|
|
1000
|
-
async hasColumn(_tableName, _column) {
|
|
1001
|
-
return true;
|
|
1002
|
-
}
|
|
1003
|
-
async scanKeys(pattern, batchSize = 1e4) {
|
|
1004
|
-
let cursor = "0";
|
|
1005
|
-
let keys = [];
|
|
1006
|
-
do {
|
|
1007
|
-
const [nextCursor, batch] = await this.client.scan(cursor, {
|
|
1008
|
-
match: pattern,
|
|
1009
|
-
count: batchSize
|
|
1010
|
-
});
|
|
1011
|
-
keys.push(...batch);
|
|
1012
|
-
cursor = nextCursor;
|
|
1013
|
-
} while (cursor !== "0");
|
|
1014
|
-
return keys;
|
|
1015
|
-
}
|
|
1016
|
-
async scanAndDelete(pattern, batchSize = 1e4) {
|
|
1017
|
-
let cursor = "0";
|
|
1018
|
-
let totalDeleted = 0;
|
|
1019
|
-
do {
|
|
1020
|
-
const [nextCursor, keys] = await this.client.scan(cursor, {
|
|
1021
|
-
match: pattern,
|
|
1022
|
-
count: batchSize
|
|
1023
|
-
});
|
|
1024
|
-
if (keys.length > 0) {
|
|
1025
|
-
await this.client.del(...keys);
|
|
1026
|
-
totalDeleted += keys.length;
|
|
1027
|
-
}
|
|
1028
|
-
cursor = nextCursor;
|
|
1029
|
-
} while (cursor !== "0");
|
|
1030
|
-
return totalDeleted;
|
|
1031
|
-
}
|
|
1032
|
-
};
|
|
1033
997
|
function transformScoreRow(row) {
|
|
1034
998
|
return storage.transformScoreRow(row);
|
|
1035
999
|
}
|
|
1036
1000
|
var ScoresUpstash = class extends storage.ScoresStorage {
|
|
1037
1001
|
client;
|
|
1038
|
-
|
|
1039
|
-
constructor(
|
|
1002
|
+
#db;
|
|
1003
|
+
constructor(config) {
|
|
1040
1004
|
super();
|
|
1005
|
+
const client = resolveUpstashConfig(config);
|
|
1041
1006
|
this.client = client;
|
|
1042
|
-
this
|
|
1007
|
+
this.#db = new UpstashDB({ client });
|
|
1008
|
+
}
|
|
1009
|
+
async dangerouslyClearAll() {
|
|
1010
|
+
await this.#db.deleteData({ tableName: storage.TABLE_SCORERS });
|
|
1043
1011
|
}
|
|
1044
1012
|
async getScoreById({ id }) {
|
|
1045
1013
|
try {
|
|
1046
|
-
const data = await this.
|
|
1014
|
+
const data = await this.#db.get({
|
|
1047
1015
|
tableName: storage.TABLE_SCORERS,
|
|
1048
1016
|
keys: { id }
|
|
1049
1017
|
});
|
|
@@ -1071,7 +1039,7 @@ var ScoresUpstash = class extends storage.ScoresStorage {
|
|
|
1071
1039
|
pagination = { page: 0, perPage: 20 }
|
|
1072
1040
|
}) {
|
|
1073
1041
|
const pattern = `${storage.TABLE_SCORERS}:*`;
|
|
1074
|
-
const keys = await this.
|
|
1042
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
1075
1043
|
const { page, perPage: perPageInput } = pagination;
|
|
1076
1044
|
if (keys.length === 0) {
|
|
1077
1045
|
return {
|
|
@@ -1127,7 +1095,7 @@ var ScoresUpstash = class extends storage.ScoresStorage {
|
|
|
1127
1095
|
domain: error.ErrorDomain.STORAGE,
|
|
1128
1096
|
category: error.ErrorCategory.USER,
|
|
1129
1097
|
details: {
|
|
1130
|
-
scorer: score.scorer?.id ?? "unknown",
|
|
1098
|
+
scorer: typeof score.scorer?.id === "string" ? score.scorer.id : String(score.scorer?.id ?? "unknown"),
|
|
1131
1099
|
entityId: score.entityId ?? "unknown",
|
|
1132
1100
|
entityType: score.entityType ?? "unknown",
|
|
1133
1101
|
traceId: score.traceId ?? "",
|
|
@@ -1168,7 +1136,7 @@ var ScoresUpstash = class extends storage.ScoresStorage {
|
|
|
1168
1136
|
pagination = { page: 0, perPage: 20 }
|
|
1169
1137
|
}) {
|
|
1170
1138
|
const pattern = `${storage.TABLE_SCORERS}:*`;
|
|
1171
|
-
const keys = await this.
|
|
1139
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
1172
1140
|
const { page, perPage: perPageInput } = pagination;
|
|
1173
1141
|
if (keys.length === 0) {
|
|
1174
1142
|
return {
|
|
@@ -1212,7 +1180,7 @@ var ScoresUpstash = class extends storage.ScoresStorage {
|
|
|
1212
1180
|
pagination = { page: 0, perPage: 20 }
|
|
1213
1181
|
}) {
|
|
1214
1182
|
const pattern = `${storage.TABLE_SCORERS}:*`;
|
|
1215
|
-
const keys = await this.
|
|
1183
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
1216
1184
|
const { page, perPage: perPageInput } = pagination;
|
|
1217
1185
|
if (keys.length === 0) {
|
|
1218
1186
|
return {
|
|
@@ -1261,7 +1229,7 @@ var ScoresUpstash = class extends storage.ScoresStorage {
|
|
|
1261
1229
|
pagination = { page: 0, perPage: 20 }
|
|
1262
1230
|
}) {
|
|
1263
1231
|
const pattern = `${storage.TABLE_SCORERS}:*`;
|
|
1264
|
-
const keys = await this.
|
|
1232
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
1265
1233
|
const { page, perPage: perPageInput } = pagination;
|
|
1266
1234
|
if (keys.length === 0) {
|
|
1267
1235
|
return {
|
|
@@ -1318,39 +1286,116 @@ function parseWorkflowRun(row) {
|
|
|
1318
1286
|
workflowName: row.workflow_name,
|
|
1319
1287
|
runId: row.run_id,
|
|
1320
1288
|
snapshot: parsedSnapshot,
|
|
1321
|
-
createdAt: ensureDate(row.createdAt),
|
|
1322
|
-
updatedAt: ensureDate(row.updatedAt),
|
|
1289
|
+
createdAt: storage.ensureDate(row.createdAt),
|
|
1290
|
+
updatedAt: storage.ensureDate(row.updatedAt),
|
|
1323
1291
|
resourceId: row.resourceId
|
|
1324
1292
|
};
|
|
1325
1293
|
}
|
|
1326
1294
|
var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
1327
1295
|
client;
|
|
1328
|
-
|
|
1329
|
-
constructor(
|
|
1296
|
+
#db;
|
|
1297
|
+
constructor(config) {
|
|
1330
1298
|
super();
|
|
1299
|
+
const client = resolveUpstashConfig(config);
|
|
1331
1300
|
this.client = client;
|
|
1332
|
-
this
|
|
1333
|
-
}
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1301
|
+
this.#db = new UpstashDB({ client });
|
|
1302
|
+
}
|
|
1303
|
+
async dangerouslyClearAll() {
|
|
1304
|
+
await this.#db.deleteData({ tableName: storage.TABLE_WORKFLOW_SNAPSHOT });
|
|
1305
|
+
}
|
|
1306
|
+
async updateWorkflowResults({
|
|
1307
|
+
workflowName,
|
|
1308
|
+
runId,
|
|
1309
|
+
stepId,
|
|
1310
|
+
result,
|
|
1311
|
+
requestContext
|
|
1340
1312
|
}) {
|
|
1341
|
-
|
|
1313
|
+
try {
|
|
1314
|
+
const existingSnapshot = await this.loadWorkflowSnapshot({
|
|
1315
|
+
namespace: "workflows",
|
|
1316
|
+
workflowName,
|
|
1317
|
+
runId
|
|
1318
|
+
});
|
|
1319
|
+
let snapshot;
|
|
1320
|
+
if (!existingSnapshot) {
|
|
1321
|
+
snapshot = {
|
|
1322
|
+
context: {},
|
|
1323
|
+
activePaths: [],
|
|
1324
|
+
timestamp: Date.now(),
|
|
1325
|
+
suspendedPaths: {},
|
|
1326
|
+
activeStepsPath: {},
|
|
1327
|
+
resumeLabels: {},
|
|
1328
|
+
serializedStepGraph: [],
|
|
1329
|
+
status: "pending",
|
|
1330
|
+
value: {},
|
|
1331
|
+
waitingPaths: {},
|
|
1332
|
+
runId,
|
|
1333
|
+
requestContext: {}
|
|
1334
|
+
};
|
|
1335
|
+
} else {
|
|
1336
|
+
snapshot = existingSnapshot;
|
|
1337
|
+
}
|
|
1338
|
+
snapshot.context[stepId] = result;
|
|
1339
|
+
snapshot.requestContext = { ...snapshot.requestContext, ...requestContext };
|
|
1340
|
+
await this.persistWorkflowSnapshot({
|
|
1341
|
+
namespace: "workflows",
|
|
1342
|
+
workflowName,
|
|
1343
|
+
runId,
|
|
1344
|
+
snapshot
|
|
1345
|
+
});
|
|
1346
|
+
return snapshot.context;
|
|
1347
|
+
} catch (error$1) {
|
|
1348
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
1349
|
+
throw new error.MastraError(
|
|
1350
|
+
{
|
|
1351
|
+
id: storage.createStorageErrorId("UPSTASH", "UPDATE_WORKFLOW_RESULTS", "FAILED"),
|
|
1352
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1353
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1354
|
+
details: { workflowName, runId, stepId }
|
|
1355
|
+
},
|
|
1356
|
+
error$1
|
|
1357
|
+
);
|
|
1358
|
+
}
|
|
1342
1359
|
}
|
|
1343
|
-
updateWorkflowState({
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1360
|
+
async updateWorkflowState({
|
|
1361
|
+
workflowName,
|
|
1362
|
+
runId,
|
|
1363
|
+
opts
|
|
1347
1364
|
}) {
|
|
1348
|
-
|
|
1365
|
+
try {
|
|
1366
|
+
const existingSnapshot = await this.loadWorkflowSnapshot({
|
|
1367
|
+
namespace: "workflows",
|
|
1368
|
+
workflowName,
|
|
1369
|
+
runId
|
|
1370
|
+
});
|
|
1371
|
+
if (!existingSnapshot || !existingSnapshot.context) {
|
|
1372
|
+
return void 0;
|
|
1373
|
+
}
|
|
1374
|
+
const updatedSnapshot = { ...existingSnapshot, ...opts };
|
|
1375
|
+
await this.persistWorkflowSnapshot({
|
|
1376
|
+
namespace: "workflows",
|
|
1377
|
+
workflowName,
|
|
1378
|
+
runId,
|
|
1379
|
+
snapshot: updatedSnapshot
|
|
1380
|
+
});
|
|
1381
|
+
return updatedSnapshot;
|
|
1382
|
+
} catch (error$1) {
|
|
1383
|
+
if (error$1 instanceof error.MastraError) throw error$1;
|
|
1384
|
+
throw new error.MastraError(
|
|
1385
|
+
{
|
|
1386
|
+
id: storage.createStorageErrorId("UPSTASH", "UPDATE_WORKFLOW_STATE", "FAILED"),
|
|
1387
|
+
domain: error.ErrorDomain.STORAGE,
|
|
1388
|
+
category: error.ErrorCategory.THIRD_PARTY,
|
|
1389
|
+
details: { workflowName, runId }
|
|
1390
|
+
},
|
|
1391
|
+
error$1
|
|
1392
|
+
);
|
|
1393
|
+
}
|
|
1349
1394
|
}
|
|
1350
1395
|
async persistWorkflowSnapshot(params) {
|
|
1351
|
-
const { namespace = "workflows", workflowName, runId, resourceId, snapshot } = params;
|
|
1396
|
+
const { namespace = "workflows", workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
|
|
1352
1397
|
try {
|
|
1353
|
-
await this.
|
|
1398
|
+
await this.#db.insert({
|
|
1354
1399
|
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
1355
1400
|
record: {
|
|
1356
1401
|
namespace,
|
|
@@ -1358,8 +1403,8 @@ var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
|
1358
1403
|
run_id: runId,
|
|
1359
1404
|
resourceId,
|
|
1360
1405
|
snapshot,
|
|
1361
|
-
createdAt: /* @__PURE__ */ new Date(),
|
|
1362
|
-
updatedAt: /* @__PURE__ */ new Date()
|
|
1406
|
+
createdAt: createdAt ?? /* @__PURE__ */ new Date(),
|
|
1407
|
+
updatedAt: updatedAt ?? /* @__PURE__ */ new Date()
|
|
1363
1408
|
}
|
|
1364
1409
|
});
|
|
1365
1410
|
} catch (error$1) {
|
|
@@ -1411,7 +1456,7 @@ var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
|
1411
1456
|
}) {
|
|
1412
1457
|
try {
|
|
1413
1458
|
const key = getKey(storage.TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName, run_id: runId }) + "*";
|
|
1414
|
-
const keys = await this.
|
|
1459
|
+
const keys = await this.#db.scanKeys(key);
|
|
1415
1460
|
const workflows = await Promise.all(
|
|
1416
1461
|
keys.map(async (key2) => {
|
|
1417
1462
|
const data2 = await this.client.get(key2);
|
|
@@ -1465,7 +1510,7 @@ var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
|
1465
1510
|
page,
|
|
1466
1511
|
resourceId,
|
|
1467
1512
|
status
|
|
1468
|
-
}) {
|
|
1513
|
+
} = {}) {
|
|
1469
1514
|
try {
|
|
1470
1515
|
if (page !== void 0 && page < 0) {
|
|
1471
1516
|
throw new error.MastraError(
|
|
@@ -1496,7 +1541,7 @@ var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
|
1496
1541
|
resourceId
|
|
1497
1542
|
});
|
|
1498
1543
|
}
|
|
1499
|
-
const keys = await this.
|
|
1544
|
+
const keys = await this.#db.scanKeys(pattern);
|
|
1500
1545
|
if (keys.length === 0) {
|
|
1501
1546
|
return { runs: [], total: 0 };
|
|
1502
1547
|
}
|
|
@@ -1549,182 +1594,39 @@ var WorkflowsUpstash = class extends storage.WorkflowsStorage {
|
|
|
1549
1594
|
};
|
|
1550
1595
|
|
|
1551
1596
|
// src/storage/index.ts
|
|
1597
|
+
var isClientConfig = (config) => {
|
|
1598
|
+
return "client" in config;
|
|
1599
|
+
};
|
|
1552
1600
|
var UpstashStore = class extends storage.MastraStorage {
|
|
1553
1601
|
redis;
|
|
1554
1602
|
stores;
|
|
1555
1603
|
constructor(config) {
|
|
1556
1604
|
super({ id: config.id, name: "Upstash", disableInit: config.disableInit });
|
|
1557
|
-
|
|
1558
|
-
|
|
1559
|
-
|
|
1560
|
-
|
|
1561
|
-
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1605
|
+
if (isClientConfig(config)) {
|
|
1606
|
+
this.redis = config.client;
|
|
1607
|
+
} else {
|
|
1608
|
+
if (!config.url || typeof config.url !== "string" || config.url.trim() === "") {
|
|
1609
|
+
throw new Error("UpstashStore: url is required and cannot be empty.");
|
|
1610
|
+
}
|
|
1611
|
+
if (!config.token || typeof config.token !== "string" || config.token.trim() === "") {
|
|
1612
|
+
throw new Error("UpstashStore: token is required and cannot be empty.");
|
|
1613
|
+
}
|
|
1614
|
+
this.redis = new redis.Redis({
|
|
1615
|
+
url: config.url,
|
|
1616
|
+
token: config.token
|
|
1617
|
+
});
|
|
1618
|
+
}
|
|
1619
|
+
const scores = new ScoresUpstash({ client: this.redis });
|
|
1620
|
+
const workflows = new WorkflowsUpstash({ client: this.redis });
|
|
1621
|
+
const memory = new StoreMemoryUpstash({ client: this.redis });
|
|
1565
1622
|
this.stores = {
|
|
1566
|
-
operations,
|
|
1567
1623
|
scores,
|
|
1568
1624
|
workflows,
|
|
1569
1625
|
memory
|
|
1570
1626
|
};
|
|
1571
1627
|
}
|
|
1572
|
-
get supports() {
|
|
1573
|
-
return {
|
|
1574
|
-
selectByIncludeResourceScope: true,
|
|
1575
|
-
resourceWorkingMemory: true,
|
|
1576
|
-
hasColumn: false,
|
|
1577
|
-
createTable: false,
|
|
1578
|
-
deleteMessages: true,
|
|
1579
|
-
listScoresBySpan: true
|
|
1580
|
-
};
|
|
1581
|
-
}
|
|
1582
|
-
async createTable({
|
|
1583
|
-
tableName,
|
|
1584
|
-
schema
|
|
1585
|
-
}) {
|
|
1586
|
-
return this.stores.operations.createTable({ tableName, schema });
|
|
1587
|
-
}
|
|
1588
|
-
/**
|
|
1589
|
-
* No-op: This backend is schemaless and does not require schema changes.
|
|
1590
|
-
* @param tableName Name of the table
|
|
1591
|
-
* @param schema Schema of the table
|
|
1592
|
-
* @param ifNotExists Array of column names to add if they don't exist
|
|
1593
|
-
*/
|
|
1594
|
-
async alterTable(args) {
|
|
1595
|
-
return this.stores.operations.alterTable(args);
|
|
1596
|
-
}
|
|
1597
|
-
async clearTable({ tableName }) {
|
|
1598
|
-
return this.stores.operations.clearTable({ tableName });
|
|
1599
|
-
}
|
|
1600
|
-
async dropTable({ tableName }) {
|
|
1601
|
-
return this.stores.operations.dropTable({ tableName });
|
|
1602
|
-
}
|
|
1603
|
-
async insert({ tableName, record }) {
|
|
1604
|
-
return this.stores.operations.insert({ tableName, record });
|
|
1605
|
-
}
|
|
1606
|
-
async batchInsert(input) {
|
|
1607
|
-
return this.stores.operations.batchInsert(input);
|
|
1608
|
-
}
|
|
1609
|
-
async load({ tableName, keys }) {
|
|
1610
|
-
return this.stores.operations.load({ tableName, keys });
|
|
1611
|
-
}
|
|
1612
|
-
async getThreadById({ threadId }) {
|
|
1613
|
-
return this.stores.memory.getThreadById({ threadId });
|
|
1614
|
-
}
|
|
1615
|
-
async saveThread({ thread }) {
|
|
1616
|
-
return this.stores.memory.saveThread({ thread });
|
|
1617
|
-
}
|
|
1618
|
-
async updateThread({
|
|
1619
|
-
id,
|
|
1620
|
-
title,
|
|
1621
|
-
metadata
|
|
1622
|
-
}) {
|
|
1623
|
-
return this.stores.memory.updateThread({ id, title, metadata });
|
|
1624
|
-
}
|
|
1625
|
-
async deleteThread({ threadId }) {
|
|
1626
|
-
return this.stores.memory.deleteThread({ threadId });
|
|
1627
|
-
}
|
|
1628
|
-
async saveMessages(args) {
|
|
1629
|
-
return this.stores.memory.saveMessages(args);
|
|
1630
|
-
}
|
|
1631
|
-
async listMessagesById({ messageIds }) {
|
|
1632
|
-
return this.stores.memory.listMessagesById({ messageIds });
|
|
1633
|
-
}
|
|
1634
|
-
async updateWorkflowResults({
|
|
1635
|
-
workflowName,
|
|
1636
|
-
runId,
|
|
1637
|
-
stepId,
|
|
1638
|
-
result,
|
|
1639
|
-
requestContext
|
|
1640
|
-
}) {
|
|
1641
|
-
return this.stores.workflows.updateWorkflowResults({ workflowName, runId, stepId, result, requestContext });
|
|
1642
|
-
}
|
|
1643
|
-
async updateWorkflowState({
|
|
1644
|
-
workflowName,
|
|
1645
|
-
runId,
|
|
1646
|
-
opts
|
|
1647
|
-
}) {
|
|
1648
|
-
return this.stores.workflows.updateWorkflowState({ workflowName, runId, opts });
|
|
1649
|
-
}
|
|
1650
|
-
async persistWorkflowSnapshot(params) {
|
|
1651
|
-
return this.stores.workflows.persistWorkflowSnapshot(params);
|
|
1652
|
-
}
|
|
1653
|
-
async loadWorkflowSnapshot(params) {
|
|
1654
|
-
return this.stores.workflows.loadWorkflowSnapshot(params);
|
|
1655
|
-
}
|
|
1656
|
-
async deleteWorkflowRunById({ runId, workflowName }) {
|
|
1657
|
-
return this.stores.workflows.deleteWorkflowRunById({ runId, workflowName });
|
|
1658
|
-
}
|
|
1659
|
-
async listWorkflowRuns(args = {}) {
|
|
1660
|
-
return this.stores.workflows.listWorkflowRuns(args);
|
|
1661
|
-
}
|
|
1662
|
-
async getWorkflowRunById({
|
|
1663
|
-
runId,
|
|
1664
|
-
workflowName
|
|
1665
|
-
}) {
|
|
1666
|
-
return this.stores.workflows.getWorkflowRunById({ runId, workflowName });
|
|
1667
|
-
}
|
|
1668
1628
|
async close() {
|
|
1669
1629
|
}
|
|
1670
|
-
async updateMessages(args) {
|
|
1671
|
-
return this.stores.memory.updateMessages(args);
|
|
1672
|
-
}
|
|
1673
|
-
async deleteMessages(messageIds) {
|
|
1674
|
-
return this.stores.memory.deleteMessages(messageIds);
|
|
1675
|
-
}
|
|
1676
|
-
async getResourceById({ resourceId }) {
|
|
1677
|
-
return this.stores.memory.getResourceById({ resourceId });
|
|
1678
|
-
}
|
|
1679
|
-
async saveResource({ resource }) {
|
|
1680
|
-
return this.stores.memory.saveResource({ resource });
|
|
1681
|
-
}
|
|
1682
|
-
async updateResource({
|
|
1683
|
-
resourceId,
|
|
1684
|
-
workingMemory,
|
|
1685
|
-
metadata
|
|
1686
|
-
}) {
|
|
1687
|
-
return this.stores.memory.updateResource({ resourceId, workingMemory, metadata });
|
|
1688
|
-
}
|
|
1689
|
-
async getScoreById({ id: _id }) {
|
|
1690
|
-
return this.stores.scores.getScoreById({ id: _id });
|
|
1691
|
-
}
|
|
1692
|
-
async saveScore(score) {
|
|
1693
|
-
return this.stores.scores.saveScore(score);
|
|
1694
|
-
}
|
|
1695
|
-
async listScoresByRunId({
|
|
1696
|
-
runId,
|
|
1697
|
-
pagination
|
|
1698
|
-
}) {
|
|
1699
|
-
return this.stores.scores.listScoresByRunId({ runId, pagination });
|
|
1700
|
-
}
|
|
1701
|
-
async listScoresByEntityId({
|
|
1702
|
-
entityId,
|
|
1703
|
-
entityType,
|
|
1704
|
-
pagination
|
|
1705
|
-
}) {
|
|
1706
|
-
return this.stores.scores.listScoresByEntityId({
|
|
1707
|
-
entityId,
|
|
1708
|
-
entityType,
|
|
1709
|
-
pagination
|
|
1710
|
-
});
|
|
1711
|
-
}
|
|
1712
|
-
async listScoresByScorerId({
|
|
1713
|
-
scorerId,
|
|
1714
|
-
pagination,
|
|
1715
|
-
entityId,
|
|
1716
|
-
entityType,
|
|
1717
|
-
source
|
|
1718
|
-
}) {
|
|
1719
|
-
return this.stores.scores.listScoresByScorerId({ scorerId, pagination, entityId, entityType, source });
|
|
1720
|
-
}
|
|
1721
|
-
async listScoresBySpan({
|
|
1722
|
-
traceId,
|
|
1723
|
-
spanId,
|
|
1724
|
-
pagination
|
|
1725
|
-
}) {
|
|
1726
|
-
return this.stores.scores.listScoresBySpan({ traceId, spanId, pagination });
|
|
1727
|
-
}
|
|
1728
1630
|
};
|
|
1729
1631
|
var UpstashFilterTranslator = class extends filter.BaseFilterTranslator {
|
|
1730
1632
|
getSupportedOperators() {
|
|
@@ -2417,8 +2319,11 @@ Example Complex Query:
|
|
|
2417
2319
|
]
|
|
2418
2320
|
}`;
|
|
2419
2321
|
|
|
2322
|
+
exports.ScoresUpstash = ScoresUpstash;
|
|
2323
|
+
exports.StoreMemoryUpstash = StoreMemoryUpstash;
|
|
2420
2324
|
exports.UPSTASH_PROMPT = UPSTASH_PROMPT;
|
|
2421
2325
|
exports.UpstashStore = UpstashStore;
|
|
2422
2326
|
exports.UpstashVector = UpstashVector;
|
|
2327
|
+
exports.WorkflowsUpstash = WorkflowsUpstash;
|
|
2423
2328
|
//# sourceMappingURL=index.cjs.map
|
|
2424
2329
|
//# sourceMappingURL=index.cjs.map
|