@mastra/upstash 0.12.1 → 0.12.2

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.
@@ -0,0 +1,243 @@
1
+ import type { WorkflowRun, WorkflowRuns, WorkflowRunState } from '@mastra/core';
2
+ import { ErrorCategory, ErrorDomain, MastraError } from '@mastra/core/error';
3
+ import { TABLE_WORKFLOW_SNAPSHOT, WorkflowsStorage } from '@mastra/core/storage';
4
+ import type { Redis } from '@upstash/redis';
5
+ import type { StoreOperationsUpstash } from '../operations';
6
+ import { ensureDate, getKey } from '../utils';
7
+
8
+ function parseWorkflowRun(row: any): WorkflowRun {
9
+ let parsedSnapshot: WorkflowRunState | string = row.snapshot as string;
10
+ if (typeof parsedSnapshot === 'string') {
11
+ try {
12
+ parsedSnapshot = JSON.parse(row.snapshot as string) as WorkflowRunState;
13
+ } catch (e) {
14
+ // If parsing fails, return the raw snapshot string
15
+ console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
16
+ }
17
+ }
18
+
19
+ return {
20
+ workflowName: row.workflow_name,
21
+ runId: row.run_id,
22
+ snapshot: parsedSnapshot,
23
+ createdAt: ensureDate(row.createdAt)!,
24
+ updatedAt: ensureDate(row.updatedAt)!,
25
+ resourceId: row.resourceId,
26
+ };
27
+ }
28
+ export class WorkflowsUpstash extends WorkflowsStorage {
29
+ private client: Redis;
30
+ private operations: StoreOperationsUpstash;
31
+
32
+ constructor({ client, operations }: { client: Redis; operations: StoreOperationsUpstash }) {
33
+ super();
34
+ this.client = client;
35
+ this.operations = operations;
36
+ }
37
+
38
+ async persistWorkflowSnapshot(params: {
39
+ namespace: string;
40
+ workflowName: string;
41
+ runId: string;
42
+ snapshot: WorkflowRunState;
43
+ }): Promise<void> {
44
+ const { namespace = 'workflows', workflowName, runId, snapshot } = params;
45
+ try {
46
+ await this.operations.insert({
47
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
48
+ record: {
49
+ namespace,
50
+ workflow_name: workflowName,
51
+ run_id: runId,
52
+ snapshot,
53
+ createdAt: new Date(),
54
+ updatedAt: new Date(),
55
+ },
56
+ });
57
+ } catch (error) {
58
+ throw new MastraError(
59
+ {
60
+ id: 'STORAGE_UPSTASH_STORAGE_PERSIST_WORKFLOW_SNAPSHOT_FAILED',
61
+ domain: ErrorDomain.STORAGE,
62
+ category: ErrorCategory.THIRD_PARTY,
63
+ details: {
64
+ namespace,
65
+ workflowName,
66
+ runId,
67
+ },
68
+ },
69
+ error,
70
+ );
71
+ }
72
+ }
73
+
74
+ async loadWorkflowSnapshot(params: {
75
+ namespace: string;
76
+ workflowName: string;
77
+ runId: string;
78
+ }): Promise<WorkflowRunState | null> {
79
+ const { namespace = 'workflows', workflowName, runId } = params;
80
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, {
81
+ namespace,
82
+ workflow_name: workflowName,
83
+ run_id: runId,
84
+ });
85
+ try {
86
+ const data = await this.client.get<{
87
+ namespace: string;
88
+ workflow_name: string;
89
+ run_id: string;
90
+ snapshot: WorkflowRunState;
91
+ }>(key);
92
+ if (!data) return null;
93
+ return data.snapshot;
94
+ } catch (error) {
95
+ throw new MastraError(
96
+ {
97
+ id: 'STORAGE_UPSTASH_STORAGE_LOAD_WORKFLOW_SNAPSHOT_FAILED',
98
+ domain: ErrorDomain.STORAGE,
99
+ category: ErrorCategory.THIRD_PARTY,
100
+ details: {
101
+ namespace,
102
+ workflowName,
103
+ runId,
104
+ },
105
+ },
106
+ error,
107
+ );
108
+ }
109
+ }
110
+
111
+ async getWorkflowRunById({
112
+ runId,
113
+ workflowName,
114
+ }: {
115
+ runId: string;
116
+ workflowName?: string;
117
+ }): Promise<WorkflowRun | null> {
118
+ try {
119
+ const key =
120
+ getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows', workflow_name: workflowName, run_id: runId }) + '*';
121
+ const keys = await this.operations.scanKeys(key);
122
+ const workflows = await Promise.all(
123
+ keys.map(async key => {
124
+ const data = await this.client.get<{
125
+ workflow_name: string;
126
+ run_id: string;
127
+ snapshot: WorkflowRunState | string;
128
+ createdAt: string | Date;
129
+ updatedAt: string | Date;
130
+ resourceId: string;
131
+ }>(key);
132
+ return data;
133
+ }),
134
+ );
135
+ const data = workflows.find(w => w?.run_id === runId && w?.workflow_name === workflowName) as WorkflowRun | null;
136
+ if (!data) return null;
137
+ return parseWorkflowRun(data);
138
+ } catch (error) {
139
+ throw new MastraError(
140
+ {
141
+ id: 'STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUN_BY_ID_FAILED',
142
+ domain: ErrorDomain.STORAGE,
143
+ category: ErrorCategory.THIRD_PARTY,
144
+ details: {
145
+ namespace: 'workflows',
146
+ runId,
147
+ workflowName: workflowName || '',
148
+ },
149
+ },
150
+ error,
151
+ );
152
+ }
153
+ }
154
+
155
+ async getWorkflowRuns({
156
+ workflowName,
157
+ fromDate,
158
+ toDate,
159
+ limit,
160
+ offset,
161
+ resourceId,
162
+ }: {
163
+ workflowName?: string;
164
+ fromDate?: Date;
165
+ toDate?: Date;
166
+ limit?: number;
167
+ offset?: number;
168
+ resourceId?: string;
169
+ }): Promise<WorkflowRuns> {
170
+ try {
171
+ // Get all workflow keys
172
+ let pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows' }) + ':*';
173
+ if (workflowName && resourceId) {
174
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
175
+ namespace: 'workflows',
176
+ workflow_name: workflowName,
177
+ run_id: '*',
178
+ resourceId,
179
+ });
180
+ } else if (workflowName) {
181
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: 'workflows', workflow_name: workflowName }) + ':*';
182
+ } else if (resourceId) {
183
+ pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
184
+ namespace: 'workflows',
185
+ workflow_name: '*',
186
+ run_id: '*',
187
+ resourceId,
188
+ });
189
+ }
190
+ const keys = await this.operations.scanKeys(pattern);
191
+
192
+ // Check if we have any keys before using pipeline
193
+ if (keys.length === 0) {
194
+ return { runs: [], total: 0 };
195
+ }
196
+
197
+ // Use pipeline for batch fetching to improve performance
198
+ const pipeline = this.client.pipeline();
199
+ keys.forEach(key => pipeline.get(key));
200
+ const results = await pipeline.exec();
201
+
202
+ // Filter and transform results - handle undefined results
203
+ let runs = results
204
+ .map((result: any) => result as Record<string, any> | null)
205
+ .filter(
206
+ (record): record is Record<string, any> =>
207
+ record !== null && record !== undefined && typeof record === 'object' && 'workflow_name' in record,
208
+ )
209
+ // Only filter by workflowName if it was specifically requested
210
+ .filter(record => !workflowName || record.workflow_name === workflowName)
211
+ .map(w => parseWorkflowRun(w!))
212
+ .filter(w => {
213
+ if (fromDate && w.createdAt < fromDate) return false;
214
+ if (toDate && w.createdAt > toDate) return false;
215
+ return true;
216
+ })
217
+ .sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
218
+
219
+ const total = runs.length;
220
+
221
+ // Apply pagination if requested
222
+ if (limit !== undefined && offset !== undefined) {
223
+ runs = runs.slice(offset, offset + limit);
224
+ }
225
+
226
+ return { runs, total };
227
+ } catch (error) {
228
+ throw new MastraError(
229
+ {
230
+ id: 'STORAGE_UPSTASH_STORAGE_GET_WORKFLOW_RUNS_FAILED',
231
+ domain: ErrorDomain.STORAGE,
232
+ category: ErrorCategory.THIRD_PARTY,
233
+ details: {
234
+ namespace: 'workflows',
235
+ workflowName: workflowName || '',
236
+ resourceId: resourceId || '',
237
+ },
238
+ },
239
+ error,
240
+ );
241
+ }
242
+ }
243
+ }
@@ -0,0 +1,13 @@
1
+ import { createTestSuite } from '@internal/storage-test-utils';
2
+ import { vi } from 'vitest';
3
+ import { UpstashStore } from './index';
4
+
5
+ // Increase timeout for all tests in this file to 30 seconds
6
+ vi.setConfig({ testTimeout: 200_000, hookTimeout: 200_000 });
7
+
8
+ createTestSuite(
9
+ new UpstashStore({
10
+ url: 'http://localhost:8079',
11
+ token: 'test_token',
12
+ }),
13
+ );