@mastra/cloudflare 0.1.1-alpha.3 → 0.1.1-alpha.5
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/_tsup-dts-rollup.d.cts +27 -10
- package/dist/_tsup-dts-rollup.d.ts +27 -10
- package/dist/index.cjs +182 -33
- package/dist/index.js +182 -33
- package/package.json +4 -4
|
@@ -13,10 +13,13 @@ import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
|
13
13
|
import type { TABLE_THREADS } from '@mastra/core/storage';
|
|
14
14
|
import type { TABLE_TRACES } from '@mastra/core/storage';
|
|
15
15
|
import type { TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
|
|
16
|
+
import type { WorkflowRun } from '@mastra/core/storage';
|
|
16
17
|
import type { WorkflowRuns } from '@mastra/core/storage';
|
|
17
18
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
18
19
|
import type { WorkflowRunState as WorkflowRunState_2 } from '@mastra/core';
|
|
19
20
|
|
|
21
|
+
export declare const checkWorkflowSnapshot: (snapshot: WorkflowRunState_2 | string, stepId: string, status: string) => void;
|
|
22
|
+
|
|
20
23
|
/**
|
|
21
24
|
* Configuration for Cloudflare KV using REST API
|
|
22
25
|
*/
|
|
@@ -46,10 +49,7 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
46
49
|
private getNamespaceValue;
|
|
47
50
|
private putNamespaceValue;
|
|
48
51
|
private deleteNamespaceValue;
|
|
49
|
-
listNamespaceKeys(tableName: TABLE_NAMES, options?:
|
|
50
|
-
limit?: number;
|
|
51
|
-
prefix?: string;
|
|
52
|
-
}): Promise<KVNamespaceListKey_2<unknown, string>[]>;
|
|
52
|
+
listNamespaceKeys(tableName: TABLE_NAMES, options?: ListOptions): Promise<KVNamespaceListKey_2<unknown, string>[]>;
|
|
53
53
|
private createNamespaceById;
|
|
54
54
|
private getNamespaceIdByName;
|
|
55
55
|
private createNamespace;
|
|
@@ -103,9 +103,9 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
103
103
|
clearTable({ tableName }: {
|
|
104
104
|
tableName: TABLE_NAMES;
|
|
105
105
|
}): Promise<void>;
|
|
106
|
-
insert<T extends TABLE_NAMES>({ tableName, record }: {
|
|
106
|
+
insert<T extends TABLE_NAMES>({ tableName, record, }: {
|
|
107
107
|
tableName: T;
|
|
108
|
-
record:
|
|
108
|
+
record: Record<string, any>;
|
|
109
109
|
}): Promise<void>;
|
|
110
110
|
load<R>({ tableName, keys }: {
|
|
111
111
|
tableName: TABLE_NAMES;
|
|
@@ -162,14 +162,22 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
162
162
|
}): Promise<any[]>;
|
|
163
163
|
private parseJSON;
|
|
164
164
|
getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
165
|
-
|
|
165
|
+
private parseWorkflowRun;
|
|
166
|
+
private buildWorkflowSnapshotPrefix;
|
|
167
|
+
getWorkflowRuns({ namespace, workflowName, limit, offset, resourceId, fromDate, toDate, }?: {
|
|
166
168
|
namespace?: string;
|
|
167
169
|
workflowName?: string;
|
|
168
|
-
fromDate?: Date;
|
|
169
|
-
toDate?: Date;
|
|
170
170
|
limit?: number;
|
|
171
171
|
offset?: number;
|
|
172
|
+
resourceId?: string;
|
|
173
|
+
fromDate?: Date;
|
|
174
|
+
toDate?: Date;
|
|
172
175
|
}): Promise<WorkflowRuns>;
|
|
176
|
+
getWorkflowRunById({ namespace, runId, workflowName, }: {
|
|
177
|
+
namespace: string;
|
|
178
|
+
runId: string;
|
|
179
|
+
workflowName: string;
|
|
180
|
+
}): Promise<WorkflowRun | null>;
|
|
173
181
|
close(): Promise<void>;
|
|
174
182
|
}
|
|
175
183
|
export { CloudflareStore }
|
|
@@ -222,7 +230,11 @@ export declare const createSampleTrace: (name: string, scope?: string, attribute
|
|
|
222
230
|
createdAt: string;
|
|
223
231
|
};
|
|
224
232
|
|
|
225
|
-
export declare const createSampleWorkflowSnapshot: (threadId: string) =>
|
|
233
|
+
export declare const createSampleWorkflowSnapshot: (threadId: string, status: string, createdAt?: Date) => {
|
|
234
|
+
snapshot: WorkflowRunState_2;
|
|
235
|
+
runId: string;
|
|
236
|
+
stepId: string;
|
|
237
|
+
};
|
|
226
238
|
|
|
227
239
|
/**
|
|
228
240
|
* Helper to determine if a config is using Workers bindings
|
|
@@ -243,6 +255,11 @@ export declare interface KVOperation {
|
|
|
243
255
|
metadata?: any;
|
|
244
256
|
}
|
|
245
257
|
|
|
258
|
+
export declare type ListOptions = {
|
|
259
|
+
limit?: number;
|
|
260
|
+
prefix?: string;
|
|
261
|
+
};
|
|
262
|
+
|
|
246
263
|
export declare type RecordTypes = {
|
|
247
264
|
[TABLE_THREADS]: StorageThreadType;
|
|
248
265
|
[TABLE_MESSAGES]: MessageType;
|
|
@@ -13,10 +13,13 @@ import type { TABLE_NAMES } from '@mastra/core/storage';
|
|
|
13
13
|
import type { TABLE_THREADS } from '@mastra/core/storage';
|
|
14
14
|
import type { TABLE_TRACES } from '@mastra/core/storage';
|
|
15
15
|
import type { TABLE_WORKFLOW_SNAPSHOT } from '@mastra/core/storage';
|
|
16
|
+
import type { WorkflowRun } from '@mastra/core/storage';
|
|
16
17
|
import type { WorkflowRuns } from '@mastra/core/storage';
|
|
17
18
|
import type { WorkflowRunState } from '@mastra/core/workflows';
|
|
18
19
|
import type { WorkflowRunState as WorkflowRunState_2 } from '@mastra/core';
|
|
19
20
|
|
|
21
|
+
export declare const checkWorkflowSnapshot: (snapshot: WorkflowRunState_2 | string, stepId: string, status: string) => void;
|
|
22
|
+
|
|
20
23
|
/**
|
|
21
24
|
* Configuration for Cloudflare KV using REST API
|
|
22
25
|
*/
|
|
@@ -46,10 +49,7 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
46
49
|
private getNamespaceValue;
|
|
47
50
|
private putNamespaceValue;
|
|
48
51
|
private deleteNamespaceValue;
|
|
49
|
-
listNamespaceKeys(tableName: TABLE_NAMES, options?:
|
|
50
|
-
limit?: number;
|
|
51
|
-
prefix?: string;
|
|
52
|
-
}): Promise<KVNamespaceListKey_2<unknown, string>[]>;
|
|
52
|
+
listNamespaceKeys(tableName: TABLE_NAMES, options?: ListOptions): Promise<KVNamespaceListKey_2<unknown, string>[]>;
|
|
53
53
|
private createNamespaceById;
|
|
54
54
|
private getNamespaceIdByName;
|
|
55
55
|
private createNamespace;
|
|
@@ -103,9 +103,9 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
103
103
|
clearTable({ tableName }: {
|
|
104
104
|
tableName: TABLE_NAMES;
|
|
105
105
|
}): Promise<void>;
|
|
106
|
-
insert<T extends TABLE_NAMES>({ tableName, record }: {
|
|
106
|
+
insert<T extends TABLE_NAMES>({ tableName, record, }: {
|
|
107
107
|
tableName: T;
|
|
108
|
-
record:
|
|
108
|
+
record: Record<string, any>;
|
|
109
109
|
}): Promise<void>;
|
|
110
110
|
load<R>({ tableName, keys }: {
|
|
111
111
|
tableName: TABLE_NAMES;
|
|
@@ -162,14 +162,22 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
162
162
|
}): Promise<any[]>;
|
|
163
163
|
private parseJSON;
|
|
164
164
|
getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
165
|
-
|
|
165
|
+
private parseWorkflowRun;
|
|
166
|
+
private buildWorkflowSnapshotPrefix;
|
|
167
|
+
getWorkflowRuns({ namespace, workflowName, limit, offset, resourceId, fromDate, toDate, }?: {
|
|
166
168
|
namespace?: string;
|
|
167
169
|
workflowName?: string;
|
|
168
|
-
fromDate?: Date;
|
|
169
|
-
toDate?: Date;
|
|
170
170
|
limit?: number;
|
|
171
171
|
offset?: number;
|
|
172
|
+
resourceId?: string;
|
|
173
|
+
fromDate?: Date;
|
|
174
|
+
toDate?: Date;
|
|
172
175
|
}): Promise<WorkflowRuns>;
|
|
176
|
+
getWorkflowRunById({ namespace, runId, workflowName, }: {
|
|
177
|
+
namespace: string;
|
|
178
|
+
runId: string;
|
|
179
|
+
workflowName: string;
|
|
180
|
+
}): Promise<WorkflowRun | null>;
|
|
173
181
|
close(): Promise<void>;
|
|
174
182
|
}
|
|
175
183
|
export { CloudflareStore }
|
|
@@ -222,7 +230,11 @@ export declare const createSampleTrace: (name: string, scope?: string, attribute
|
|
|
222
230
|
createdAt: string;
|
|
223
231
|
};
|
|
224
232
|
|
|
225
|
-
export declare const createSampleWorkflowSnapshot: (threadId: string) =>
|
|
233
|
+
export declare const createSampleWorkflowSnapshot: (threadId: string, status: string, createdAt?: Date) => {
|
|
234
|
+
snapshot: WorkflowRunState_2;
|
|
235
|
+
runId: string;
|
|
236
|
+
stepId: string;
|
|
237
|
+
};
|
|
226
238
|
|
|
227
239
|
/**
|
|
228
240
|
* Helper to determine if a config is using Workers bindings
|
|
@@ -243,6 +255,11 @@ export declare interface KVOperation {
|
|
|
243
255
|
metadata?: any;
|
|
244
256
|
}
|
|
245
257
|
|
|
258
|
+
export declare type ListOptions = {
|
|
259
|
+
limit?: number;
|
|
260
|
+
prefix?: string;
|
|
261
|
+
};
|
|
262
|
+
|
|
246
263
|
export declare type RecordTypes = {
|
|
247
264
|
[TABLE_THREADS]: StorageThreadType;
|
|
248
265
|
[TABLE_MESSAGES]: MessageType;
|
package/dist/index.cjs
CHANGED
|
@@ -63,7 +63,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
63
63
|
this.logger.info("Using Cloudflare KV REST API");
|
|
64
64
|
}
|
|
65
65
|
} catch (error) {
|
|
66
|
-
|
|
66
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
67
|
+
this.logger.error("Failed to initialize CloudflareStore:", { message });
|
|
67
68
|
throw error;
|
|
68
69
|
}
|
|
69
70
|
}
|
|
@@ -105,7 +106,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
105
106
|
if (error.message && error.message.includes("key not found")) {
|
|
106
107
|
return null;
|
|
107
108
|
}
|
|
108
|
-
|
|
109
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
110
|
+
this.logger.error(`Failed to get value for ${tableName} ${key}:`, { message });
|
|
109
111
|
throw error;
|
|
110
112
|
}
|
|
111
113
|
}
|
|
@@ -130,7 +132,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
130
132
|
});
|
|
131
133
|
}
|
|
132
134
|
} catch (error) {
|
|
133
|
-
|
|
135
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
136
|
+
this.logger.error(`Failed to put value for ${tableName} ${key}:`, { message });
|
|
134
137
|
throw error;
|
|
135
138
|
}
|
|
136
139
|
}
|
|
@@ -253,7 +256,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
253
256
|
}
|
|
254
257
|
return data;
|
|
255
258
|
} catch (error) {
|
|
256
|
-
|
|
259
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
260
|
+
this.logger.error("Failed to parse text:", { message, text });
|
|
257
261
|
return null;
|
|
258
262
|
}
|
|
259
263
|
}
|
|
@@ -287,9 +291,9 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
287
291
|
throw new Error(`Failed to delete KV value: ${error.message}`);
|
|
288
292
|
}
|
|
289
293
|
}
|
|
290
|
-
async listKV(tableName) {
|
|
294
|
+
async listKV(tableName, options) {
|
|
291
295
|
try {
|
|
292
|
-
return await this.listNamespaceKeys(tableName);
|
|
296
|
+
return await this.listNamespaceKeys(tableName, options);
|
|
293
297
|
} catch (error) {
|
|
294
298
|
this.logger.error(`Failed to list KV for ${tableName}:`, error);
|
|
295
299
|
throw new Error(`Failed to list KV: ${error.message}`);
|
|
@@ -362,7 +366,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
362
366
|
if (!data) return null;
|
|
363
367
|
return typeof data === "string" ? JSON.parse(data) : data;
|
|
364
368
|
} catch (error) {
|
|
365
|
-
|
|
369
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
370
|
+
this.logger.error(`Error retrieving message ${id}:`, { message });
|
|
366
371
|
return null;
|
|
367
372
|
}
|
|
368
373
|
})
|
|
@@ -395,7 +400,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
395
400
|
value: JSON.stringify(updatedOrder)
|
|
396
401
|
});
|
|
397
402
|
} catch (error) {
|
|
398
|
-
|
|
403
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
404
|
+
this.logger.error(`Error updating sorted order for key ${orderKey}:`, { message });
|
|
399
405
|
throw error;
|
|
400
406
|
} finally {
|
|
401
407
|
if (this.updateQueue.get(orderKey) === nextPromise) {
|
|
@@ -437,7 +443,11 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
437
443
|
if (!record.namespace || !record.workflow_name || !record.run_id) {
|
|
438
444
|
throw new Error("Namespace, workflow name, and run ID are required");
|
|
439
445
|
}
|
|
440
|
-
|
|
446
|
+
let key = `${prefix}${tableName}:${record.namespace}:${record.workflow_name}:${record.run_id}`;
|
|
447
|
+
if (record.resourceId) {
|
|
448
|
+
key = `${key}:${record.resourceId}`;
|
|
449
|
+
}
|
|
450
|
+
return key;
|
|
441
451
|
case storage.TABLE_TRACES:
|
|
442
452
|
if (!record.id) throw new Error("Trace ID is required");
|
|
443
453
|
return `${prefix}${tableName}:${record.id}`;
|
|
@@ -454,7 +464,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
454
464
|
const schemaKey = this.getSchemaKey(tableName);
|
|
455
465
|
return await this.getKV(tableName, schemaKey);
|
|
456
466
|
} catch (error) {
|
|
457
|
-
|
|
467
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
468
|
+
this.logger.error(`Failed to get schema for ${tableName}:`, { message });
|
|
458
469
|
return null;
|
|
459
470
|
}
|
|
460
471
|
}
|
|
@@ -499,7 +510,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
499
510
|
}
|
|
500
511
|
}
|
|
501
512
|
} catch (error) {
|
|
502
|
-
|
|
513
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
514
|
+
this.logger.error(`Error validating record against schema:`, { message, record, schema });
|
|
503
515
|
throw error;
|
|
504
516
|
}
|
|
505
517
|
}
|
|
@@ -526,7 +538,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
526
538
|
}
|
|
527
539
|
break;
|
|
528
540
|
case storage.TABLE_WORKFLOW_SNAPSHOT:
|
|
529
|
-
if (!("namespace" in recordTyped) || !("
|
|
541
|
+
if (!("namespace" in recordTyped) || !("workflow_name" in recordTyped) || !("run_id" in recordTyped)) {
|
|
530
542
|
throw new Error("Workflow record missing required fields");
|
|
531
543
|
}
|
|
532
544
|
break;
|
|
@@ -539,7 +551,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
539
551
|
throw new Error(`Unknown table type: ${tableName}`);
|
|
540
552
|
}
|
|
541
553
|
} catch (error) {
|
|
542
|
-
|
|
554
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
555
|
+
this.logger.error(`Failed to validate record for ${tableName}:`, { message, record });
|
|
543
556
|
throw error;
|
|
544
557
|
}
|
|
545
558
|
}
|
|
@@ -579,7 +592,10 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
579
592
|
await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
|
|
580
593
|
}
|
|
581
594
|
}
|
|
582
|
-
async insert({
|
|
595
|
+
async insert({
|
|
596
|
+
tableName,
|
|
597
|
+
record
|
|
598
|
+
}) {
|
|
583
599
|
try {
|
|
584
600
|
const key = this.getKey(tableName, record);
|
|
585
601
|
const processedRecord = {
|
|
@@ -591,7 +607,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
591
607
|
await this.validateRecord(processedRecord, tableName);
|
|
592
608
|
await this.putKV({ tableName, key, value: processedRecord });
|
|
593
609
|
} catch (error) {
|
|
594
|
-
|
|
610
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
611
|
+
this.logger.error(`Failed to insert record for ${tableName}:`, { message });
|
|
595
612
|
throw error;
|
|
596
613
|
}
|
|
597
614
|
}
|
|
@@ -608,7 +625,9 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
608
625
|
};
|
|
609
626
|
return processed;
|
|
610
627
|
} catch (error) {
|
|
611
|
-
this.logger.error(`Failed to load data for ${tableName}:`, {
|
|
628
|
+
this.logger.error(`Failed to load data for ${tableName}:`, {
|
|
629
|
+
error: error instanceof Error ? error.message : String(error)
|
|
630
|
+
});
|
|
612
631
|
return null;
|
|
613
632
|
}
|
|
614
633
|
}
|
|
@@ -623,7 +642,9 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
623
642
|
metadata: this.ensureMetadata(thread.metadata)
|
|
624
643
|
};
|
|
625
644
|
} catch (error) {
|
|
626
|
-
this.logger.error(`Error processing thread ${threadId}:`, {
|
|
645
|
+
this.logger.error(`Error processing thread ${threadId}:`, {
|
|
646
|
+
error: error instanceof Error ? error.message : String(error)
|
|
647
|
+
});
|
|
627
648
|
return null;
|
|
628
649
|
}
|
|
629
650
|
}
|
|
@@ -644,14 +665,16 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
644
665
|
metadata: this.ensureMetadata(thread.metadata)
|
|
645
666
|
};
|
|
646
667
|
} catch (error) {
|
|
647
|
-
|
|
668
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
669
|
+
this.logger.error(`Error processing thread from key ${keyObj.name}:`, { message });
|
|
648
670
|
return null;
|
|
649
671
|
}
|
|
650
672
|
})
|
|
651
673
|
);
|
|
652
674
|
return threads.filter((thread) => thread !== null);
|
|
653
675
|
} catch (error) {
|
|
654
|
-
|
|
676
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
677
|
+
this.logger.error(`Error getting threads for resourceId ${resourceId}:`, { message });
|
|
655
678
|
return [];
|
|
656
679
|
}
|
|
657
680
|
}
|
|
@@ -660,7 +683,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
660
683
|
await this.insert({ tableName: storage.TABLE_THREADS, record: thread });
|
|
661
684
|
return thread;
|
|
662
685
|
} catch (error) {
|
|
663
|
-
|
|
686
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
687
|
+
this.logger.error("Error saving thread:", { message });
|
|
664
688
|
throw error;
|
|
665
689
|
}
|
|
666
690
|
}
|
|
@@ -686,7 +710,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
686
710
|
await this.insert({ tableName: storage.TABLE_THREADS, record: updatedThread });
|
|
687
711
|
return updatedThread;
|
|
688
712
|
} catch (error) {
|
|
689
|
-
|
|
713
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
714
|
+
this.logger.error(`Error updating thread ${id}:`, { message });
|
|
690
715
|
throw error;
|
|
691
716
|
}
|
|
692
717
|
}
|
|
@@ -707,7 +732,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
707
732
|
this.deleteKV(storage.TABLE_THREADS, this.getKey(storage.TABLE_THREADS, { id: threadId }))
|
|
708
733
|
]);
|
|
709
734
|
} catch (error) {
|
|
710
|
-
|
|
735
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
736
|
+
this.logger.error(`Error deleting thread ${threadId}:`, { message });
|
|
711
737
|
throw error;
|
|
712
738
|
}
|
|
713
739
|
}
|
|
@@ -715,7 +741,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
715
741
|
try {
|
|
716
742
|
return this.getKey(storage.TABLE_MESSAGES, { threadId, id: messageId });
|
|
717
743
|
} catch (error) {
|
|
718
|
-
|
|
744
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
745
|
+
this.logger.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
|
|
719
746
|
throw error;
|
|
720
747
|
}
|
|
721
748
|
}
|
|
@@ -723,7 +750,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
723
750
|
try {
|
|
724
751
|
return this.getKey(storage.TABLE_MESSAGES, { threadId, id: "messages" });
|
|
725
752
|
} catch (error) {
|
|
726
|
-
|
|
753
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
754
|
+
this.logger.error(`Error getting thread messages key for thread ${threadId}:`, { message });
|
|
727
755
|
throw error;
|
|
728
756
|
}
|
|
729
757
|
}
|
|
@@ -874,10 +902,20 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
874
902
|
const { namespace, workflowName, runId, snapshot } = params;
|
|
875
903
|
const normalizedState = this.normalizeWorkflowState(snapshot);
|
|
876
904
|
this.validateWorkflowState(normalizedState);
|
|
877
|
-
|
|
878
|
-
|
|
905
|
+
await this.insert({
|
|
906
|
+
tableName: storage.TABLE_WORKFLOW_SNAPSHOT,
|
|
907
|
+
record: {
|
|
908
|
+
namespace,
|
|
909
|
+
workflow_name: workflowName,
|
|
910
|
+
run_id: runId,
|
|
911
|
+
snapshot: normalizedState,
|
|
912
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
913
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
914
|
+
}
|
|
915
|
+
});
|
|
879
916
|
} catch (error) {
|
|
880
|
-
|
|
917
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
918
|
+
this.logger.error("Error persisting workflow snapshot:", { message });
|
|
881
919
|
throw error;
|
|
882
920
|
}
|
|
883
921
|
}
|
|
@@ -888,11 +926,13 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
888
926
|
const key = this.getKey(storage.TABLE_WORKFLOW_SNAPSHOT, { namespace, workflow_name: workflowName, run_id: runId });
|
|
889
927
|
const data = await this.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
|
|
890
928
|
if (!data) return null;
|
|
891
|
-
const state = this.normalizeWorkflowState(data);
|
|
929
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
892
930
|
this.validateWorkflowState(state);
|
|
893
931
|
return state;
|
|
894
932
|
} catch (error) {
|
|
895
|
-
this.logger.error("Error loading workflow snapshot:", {
|
|
933
|
+
this.logger.error("Error loading workflow snapshot:", {
|
|
934
|
+
error: error instanceof Error ? error.message : String(error)
|
|
935
|
+
});
|
|
896
936
|
return null;
|
|
897
937
|
}
|
|
898
938
|
}
|
|
@@ -912,7 +952,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
912
952
|
})
|
|
913
953
|
);
|
|
914
954
|
} catch (error) {
|
|
915
|
-
|
|
955
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
956
|
+
this.logger.error("Error in batch insert:", { message });
|
|
916
957
|
throw error;
|
|
917
958
|
}
|
|
918
959
|
}
|
|
@@ -985,7 +1026,8 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
985
1026
|
createdAt: record.createdAt
|
|
986
1027
|
}));
|
|
987
1028
|
} catch (error) {
|
|
988
|
-
|
|
1029
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1030
|
+
this.logger.error("Failed to get traces:", { message });
|
|
989
1031
|
return [];
|
|
990
1032
|
}
|
|
991
1033
|
}
|
|
@@ -1002,8 +1044,115 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
1002
1044
|
getEvalsByAgentName(_agentName, _type) {
|
|
1003
1045
|
throw new Error("Method not implemented.");
|
|
1004
1046
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1047
|
+
parseWorkflowRun(row) {
|
|
1048
|
+
let parsedSnapshot = row.snapshot;
|
|
1049
|
+
if (typeof parsedSnapshot === "string") {
|
|
1050
|
+
try {
|
|
1051
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
1052
|
+
} catch (e) {
|
|
1053
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
return {
|
|
1057
|
+
workflowName: row.workflow_name,
|
|
1058
|
+
runId: row.run_id,
|
|
1059
|
+
snapshot: parsedSnapshot,
|
|
1060
|
+
createdAt: this.ensureDate(row.createdAt),
|
|
1061
|
+
updatedAt: this.ensureDate(row.updatedAt),
|
|
1062
|
+
resourceId: row.resourceId
|
|
1063
|
+
};
|
|
1064
|
+
}
|
|
1065
|
+
buildWorkflowSnapshotPrefix({
|
|
1066
|
+
namespace,
|
|
1067
|
+
workflowName,
|
|
1068
|
+
runId,
|
|
1069
|
+
resourceId
|
|
1070
|
+
}) {
|
|
1071
|
+
const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
|
|
1072
|
+
let key = `${prefix}${storage.TABLE_WORKFLOW_SNAPSHOT}`;
|
|
1073
|
+
if (namespace) key += `:${namespace}`;
|
|
1074
|
+
if (workflowName) key += `:${workflowName}`;
|
|
1075
|
+
if (runId) key += `:${runId}`;
|
|
1076
|
+
if (resourceId) key += `:${resourceId}`;
|
|
1077
|
+
if (!resourceId && (runId || workflowName || namespace)) key += ":";
|
|
1078
|
+
return key;
|
|
1079
|
+
}
|
|
1080
|
+
async getWorkflowRuns({
|
|
1081
|
+
namespace,
|
|
1082
|
+
workflowName,
|
|
1083
|
+
limit = 20,
|
|
1084
|
+
offset = 0,
|
|
1085
|
+
resourceId,
|
|
1086
|
+
fromDate,
|
|
1087
|
+
toDate
|
|
1088
|
+
} = {}) {
|
|
1089
|
+
try {
|
|
1090
|
+
const prefix = this.buildWorkflowSnapshotPrefix({ namespace, workflowName });
|
|
1091
|
+
const keyObjs = await this.listKV(storage.TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
1092
|
+
const runs = [];
|
|
1093
|
+
for (const { name: key } of keyObjs) {
|
|
1094
|
+
const parts = key.split(":");
|
|
1095
|
+
const idx = parts.indexOf(storage.TABLE_WORKFLOW_SNAPSHOT);
|
|
1096
|
+
if (idx === -1 || parts.length < idx + 4) continue;
|
|
1097
|
+
const ns = parts[idx + 1];
|
|
1098
|
+
const wfName = parts[idx + 2];
|
|
1099
|
+
const keyResourceId = parts.length > idx + 4 ? parts[idx + 4] : void 0;
|
|
1100
|
+
if (namespace && ns !== namespace || workflowName && wfName !== workflowName) continue;
|
|
1101
|
+
if (resourceId && keyResourceId && keyResourceId !== resourceId) continue;
|
|
1102
|
+
const data = await this.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
|
|
1103
|
+
if (!data) continue;
|
|
1104
|
+
try {
|
|
1105
|
+
if (resourceId && data.resourceId && data.resourceId !== resourceId) continue;
|
|
1106
|
+
const createdAt = this.ensureDate(data.createdAt);
|
|
1107
|
+
if (fromDate && createdAt && createdAt < fromDate) continue;
|
|
1108
|
+
if (toDate && createdAt && createdAt > toDate) continue;
|
|
1109
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
1110
|
+
this.validateWorkflowState(state);
|
|
1111
|
+
const run = this.parseWorkflowRun({ ...data, snapshot: state });
|
|
1112
|
+
runs.push(run);
|
|
1113
|
+
} catch (err) {
|
|
1114
|
+
this.logger.error("Failed to parse workflow snapshot:", { key, error: err });
|
|
1115
|
+
}
|
|
1116
|
+
}
|
|
1117
|
+
runs.sort((a, b) => {
|
|
1118
|
+
const aDate = a.createdAt ? new Date(a.createdAt).getTime() : 0;
|
|
1119
|
+
const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
|
|
1120
|
+
return bDate - aDate;
|
|
1121
|
+
});
|
|
1122
|
+
const pagedRuns = runs.slice(offset, offset + limit);
|
|
1123
|
+
return {
|
|
1124
|
+
runs: pagedRuns,
|
|
1125
|
+
total: runs.length
|
|
1126
|
+
};
|
|
1127
|
+
} catch (error) {
|
|
1128
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1129
|
+
this.logger.error("Error in getWorkflowRuns:", { message });
|
|
1130
|
+
return { runs: [], total: 0 };
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
async getWorkflowRunById({
|
|
1134
|
+
namespace,
|
|
1135
|
+
runId,
|
|
1136
|
+
workflowName
|
|
1137
|
+
}) {
|
|
1138
|
+
try {
|
|
1139
|
+
if (!runId || !workflowName || !namespace) {
|
|
1140
|
+
throw new Error("runId, workflowName, and namespace are required");
|
|
1141
|
+
}
|
|
1142
|
+
const prefix = this.buildWorkflowSnapshotPrefix({ namespace, workflowName, runId });
|
|
1143
|
+
const keyObjs = await this.listKV(storage.TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
1144
|
+
if (!keyObjs.length) return null;
|
|
1145
|
+
const key = keyObjs[0]?.name;
|
|
1146
|
+
const data = await this.getKV(storage.TABLE_WORKFLOW_SNAPSHOT, key);
|
|
1147
|
+
if (!data) return null;
|
|
1148
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
1149
|
+
this.validateWorkflowState(state);
|
|
1150
|
+
return this.parseWorkflowRun({ ...data, snapshot: state });
|
|
1151
|
+
} catch (error) {
|
|
1152
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1153
|
+
this.logger.error("Error in getWorkflowRunById:", { message });
|
|
1154
|
+
return null;
|
|
1155
|
+
}
|
|
1007
1156
|
}
|
|
1008
1157
|
async close() {
|
|
1009
1158
|
}
|
package/dist/index.js
CHANGED
|
@@ -57,7 +57,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
57
57
|
this.logger.info("Using Cloudflare KV REST API");
|
|
58
58
|
}
|
|
59
59
|
} catch (error) {
|
|
60
|
-
|
|
60
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
61
|
+
this.logger.error("Failed to initialize CloudflareStore:", { message });
|
|
61
62
|
throw error;
|
|
62
63
|
}
|
|
63
64
|
}
|
|
@@ -99,7 +100,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
99
100
|
if (error.message && error.message.includes("key not found")) {
|
|
100
101
|
return null;
|
|
101
102
|
}
|
|
102
|
-
|
|
103
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
104
|
+
this.logger.error(`Failed to get value for ${tableName} ${key}:`, { message });
|
|
103
105
|
throw error;
|
|
104
106
|
}
|
|
105
107
|
}
|
|
@@ -124,7 +126,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
124
126
|
});
|
|
125
127
|
}
|
|
126
128
|
} catch (error) {
|
|
127
|
-
|
|
129
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
130
|
+
this.logger.error(`Failed to put value for ${tableName} ${key}:`, { message });
|
|
128
131
|
throw error;
|
|
129
132
|
}
|
|
130
133
|
}
|
|
@@ -247,7 +250,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
247
250
|
}
|
|
248
251
|
return data;
|
|
249
252
|
} catch (error) {
|
|
250
|
-
|
|
253
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
254
|
+
this.logger.error("Failed to parse text:", { message, text });
|
|
251
255
|
return null;
|
|
252
256
|
}
|
|
253
257
|
}
|
|
@@ -281,9 +285,9 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
281
285
|
throw new Error(`Failed to delete KV value: ${error.message}`);
|
|
282
286
|
}
|
|
283
287
|
}
|
|
284
|
-
async listKV(tableName) {
|
|
288
|
+
async listKV(tableName, options) {
|
|
285
289
|
try {
|
|
286
|
-
return await this.listNamespaceKeys(tableName);
|
|
290
|
+
return await this.listNamespaceKeys(tableName, options);
|
|
287
291
|
} catch (error) {
|
|
288
292
|
this.logger.error(`Failed to list KV for ${tableName}:`, error);
|
|
289
293
|
throw new Error(`Failed to list KV: ${error.message}`);
|
|
@@ -356,7 +360,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
356
360
|
if (!data) return null;
|
|
357
361
|
return typeof data === "string" ? JSON.parse(data) : data;
|
|
358
362
|
} catch (error) {
|
|
359
|
-
|
|
363
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
364
|
+
this.logger.error(`Error retrieving message ${id}:`, { message });
|
|
360
365
|
return null;
|
|
361
366
|
}
|
|
362
367
|
})
|
|
@@ -389,7 +394,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
389
394
|
value: JSON.stringify(updatedOrder)
|
|
390
395
|
});
|
|
391
396
|
} catch (error) {
|
|
392
|
-
|
|
397
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
398
|
+
this.logger.error(`Error updating sorted order for key ${orderKey}:`, { message });
|
|
393
399
|
throw error;
|
|
394
400
|
} finally {
|
|
395
401
|
if (this.updateQueue.get(orderKey) === nextPromise) {
|
|
@@ -431,7 +437,11 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
431
437
|
if (!record.namespace || !record.workflow_name || !record.run_id) {
|
|
432
438
|
throw new Error("Namespace, workflow name, and run ID are required");
|
|
433
439
|
}
|
|
434
|
-
|
|
440
|
+
let key = `${prefix}${tableName}:${record.namespace}:${record.workflow_name}:${record.run_id}`;
|
|
441
|
+
if (record.resourceId) {
|
|
442
|
+
key = `${key}:${record.resourceId}`;
|
|
443
|
+
}
|
|
444
|
+
return key;
|
|
435
445
|
case TABLE_TRACES:
|
|
436
446
|
if (!record.id) throw new Error("Trace ID is required");
|
|
437
447
|
return `${prefix}${tableName}:${record.id}`;
|
|
@@ -448,7 +458,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
448
458
|
const schemaKey = this.getSchemaKey(tableName);
|
|
449
459
|
return await this.getKV(tableName, schemaKey);
|
|
450
460
|
} catch (error) {
|
|
451
|
-
|
|
461
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
462
|
+
this.logger.error(`Failed to get schema for ${tableName}:`, { message });
|
|
452
463
|
return null;
|
|
453
464
|
}
|
|
454
465
|
}
|
|
@@ -493,7 +504,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
493
504
|
}
|
|
494
505
|
}
|
|
495
506
|
} catch (error) {
|
|
496
|
-
|
|
507
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
508
|
+
this.logger.error(`Error validating record against schema:`, { message, record, schema });
|
|
497
509
|
throw error;
|
|
498
510
|
}
|
|
499
511
|
}
|
|
@@ -520,7 +532,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
520
532
|
}
|
|
521
533
|
break;
|
|
522
534
|
case TABLE_WORKFLOW_SNAPSHOT:
|
|
523
|
-
if (!("namespace" in recordTyped) || !("
|
|
535
|
+
if (!("namespace" in recordTyped) || !("workflow_name" in recordTyped) || !("run_id" in recordTyped)) {
|
|
524
536
|
throw new Error("Workflow record missing required fields");
|
|
525
537
|
}
|
|
526
538
|
break;
|
|
@@ -533,7 +545,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
533
545
|
throw new Error(`Unknown table type: ${tableName}`);
|
|
534
546
|
}
|
|
535
547
|
} catch (error) {
|
|
536
|
-
|
|
548
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
549
|
+
this.logger.error(`Failed to validate record for ${tableName}:`, { message, record });
|
|
537
550
|
throw error;
|
|
538
551
|
}
|
|
539
552
|
}
|
|
@@ -573,7 +586,10 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
573
586
|
await Promise.all(keys.map((keyObj) => this.deleteKV(tableName, keyObj.name)));
|
|
574
587
|
}
|
|
575
588
|
}
|
|
576
|
-
async insert({
|
|
589
|
+
async insert({
|
|
590
|
+
tableName,
|
|
591
|
+
record
|
|
592
|
+
}) {
|
|
577
593
|
try {
|
|
578
594
|
const key = this.getKey(tableName, record);
|
|
579
595
|
const processedRecord = {
|
|
@@ -585,7 +601,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
585
601
|
await this.validateRecord(processedRecord, tableName);
|
|
586
602
|
await this.putKV({ tableName, key, value: processedRecord });
|
|
587
603
|
} catch (error) {
|
|
588
|
-
|
|
604
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
605
|
+
this.logger.error(`Failed to insert record for ${tableName}:`, { message });
|
|
589
606
|
throw error;
|
|
590
607
|
}
|
|
591
608
|
}
|
|
@@ -602,7 +619,9 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
602
619
|
};
|
|
603
620
|
return processed;
|
|
604
621
|
} catch (error) {
|
|
605
|
-
this.logger.error(`Failed to load data for ${tableName}:`, {
|
|
622
|
+
this.logger.error(`Failed to load data for ${tableName}:`, {
|
|
623
|
+
error: error instanceof Error ? error.message : String(error)
|
|
624
|
+
});
|
|
606
625
|
return null;
|
|
607
626
|
}
|
|
608
627
|
}
|
|
@@ -617,7 +636,9 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
617
636
|
metadata: this.ensureMetadata(thread.metadata)
|
|
618
637
|
};
|
|
619
638
|
} catch (error) {
|
|
620
|
-
this.logger.error(`Error processing thread ${threadId}:`, {
|
|
639
|
+
this.logger.error(`Error processing thread ${threadId}:`, {
|
|
640
|
+
error: error instanceof Error ? error.message : String(error)
|
|
641
|
+
});
|
|
621
642
|
return null;
|
|
622
643
|
}
|
|
623
644
|
}
|
|
@@ -638,14 +659,16 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
638
659
|
metadata: this.ensureMetadata(thread.metadata)
|
|
639
660
|
};
|
|
640
661
|
} catch (error) {
|
|
641
|
-
|
|
662
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
663
|
+
this.logger.error(`Error processing thread from key ${keyObj.name}:`, { message });
|
|
642
664
|
return null;
|
|
643
665
|
}
|
|
644
666
|
})
|
|
645
667
|
);
|
|
646
668
|
return threads.filter((thread) => thread !== null);
|
|
647
669
|
} catch (error) {
|
|
648
|
-
|
|
670
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
671
|
+
this.logger.error(`Error getting threads for resourceId ${resourceId}:`, { message });
|
|
649
672
|
return [];
|
|
650
673
|
}
|
|
651
674
|
}
|
|
@@ -654,7 +677,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
654
677
|
await this.insert({ tableName: TABLE_THREADS, record: thread });
|
|
655
678
|
return thread;
|
|
656
679
|
} catch (error) {
|
|
657
|
-
|
|
680
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
681
|
+
this.logger.error("Error saving thread:", { message });
|
|
658
682
|
throw error;
|
|
659
683
|
}
|
|
660
684
|
}
|
|
@@ -680,7 +704,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
680
704
|
await this.insert({ tableName: TABLE_THREADS, record: updatedThread });
|
|
681
705
|
return updatedThread;
|
|
682
706
|
} catch (error) {
|
|
683
|
-
|
|
707
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
708
|
+
this.logger.error(`Error updating thread ${id}:`, { message });
|
|
684
709
|
throw error;
|
|
685
710
|
}
|
|
686
711
|
}
|
|
@@ -701,7 +726,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
701
726
|
this.deleteKV(TABLE_THREADS, this.getKey(TABLE_THREADS, { id: threadId }))
|
|
702
727
|
]);
|
|
703
728
|
} catch (error) {
|
|
704
|
-
|
|
729
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
730
|
+
this.logger.error(`Error deleting thread ${threadId}:`, { message });
|
|
705
731
|
throw error;
|
|
706
732
|
}
|
|
707
733
|
}
|
|
@@ -709,7 +735,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
709
735
|
try {
|
|
710
736
|
return this.getKey(TABLE_MESSAGES, { threadId, id: messageId });
|
|
711
737
|
} catch (error) {
|
|
712
|
-
|
|
738
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
739
|
+
this.logger.error(`Error getting message key for thread ${threadId} and message ${messageId}:`, { message });
|
|
713
740
|
throw error;
|
|
714
741
|
}
|
|
715
742
|
}
|
|
@@ -717,7 +744,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
717
744
|
try {
|
|
718
745
|
return this.getKey(TABLE_MESSAGES, { threadId, id: "messages" });
|
|
719
746
|
} catch (error) {
|
|
720
|
-
|
|
747
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
748
|
+
this.logger.error(`Error getting thread messages key for thread ${threadId}:`, { message });
|
|
721
749
|
throw error;
|
|
722
750
|
}
|
|
723
751
|
}
|
|
@@ -868,10 +896,20 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
868
896
|
const { namespace, workflowName, runId, snapshot } = params;
|
|
869
897
|
const normalizedState = this.normalizeWorkflowState(snapshot);
|
|
870
898
|
this.validateWorkflowState(normalizedState);
|
|
871
|
-
|
|
872
|
-
|
|
899
|
+
await this.insert({
|
|
900
|
+
tableName: TABLE_WORKFLOW_SNAPSHOT,
|
|
901
|
+
record: {
|
|
902
|
+
namespace,
|
|
903
|
+
workflow_name: workflowName,
|
|
904
|
+
run_id: runId,
|
|
905
|
+
snapshot: normalizedState,
|
|
906
|
+
createdAt: /* @__PURE__ */ new Date(),
|
|
907
|
+
updatedAt: /* @__PURE__ */ new Date()
|
|
908
|
+
}
|
|
909
|
+
});
|
|
873
910
|
} catch (error) {
|
|
874
|
-
|
|
911
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
912
|
+
this.logger.error("Error persisting workflow snapshot:", { message });
|
|
875
913
|
throw error;
|
|
876
914
|
}
|
|
877
915
|
}
|
|
@@ -882,11 +920,13 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
882
920
|
const key = this.getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace, workflow_name: workflowName, run_id: runId });
|
|
883
921
|
const data = await this.getKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
884
922
|
if (!data) return null;
|
|
885
|
-
const state = this.normalizeWorkflowState(data);
|
|
923
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
886
924
|
this.validateWorkflowState(state);
|
|
887
925
|
return state;
|
|
888
926
|
} catch (error) {
|
|
889
|
-
this.logger.error("Error loading workflow snapshot:", {
|
|
927
|
+
this.logger.error("Error loading workflow snapshot:", {
|
|
928
|
+
error: error instanceof Error ? error.message : String(error)
|
|
929
|
+
});
|
|
890
930
|
return null;
|
|
891
931
|
}
|
|
892
932
|
}
|
|
@@ -906,7 +946,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
906
946
|
})
|
|
907
947
|
);
|
|
908
948
|
} catch (error) {
|
|
909
|
-
|
|
949
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
950
|
+
this.logger.error("Error in batch insert:", { message });
|
|
910
951
|
throw error;
|
|
911
952
|
}
|
|
912
953
|
}
|
|
@@ -979,7 +1020,8 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
979
1020
|
createdAt: record.createdAt
|
|
980
1021
|
}));
|
|
981
1022
|
} catch (error) {
|
|
982
|
-
|
|
1023
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1024
|
+
this.logger.error("Failed to get traces:", { message });
|
|
983
1025
|
return [];
|
|
984
1026
|
}
|
|
985
1027
|
}
|
|
@@ -996,8 +1038,115 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
996
1038
|
getEvalsByAgentName(_agentName, _type) {
|
|
997
1039
|
throw new Error("Method not implemented.");
|
|
998
1040
|
}
|
|
999
|
-
|
|
1000
|
-
|
|
1041
|
+
parseWorkflowRun(row) {
|
|
1042
|
+
let parsedSnapshot = row.snapshot;
|
|
1043
|
+
if (typeof parsedSnapshot === "string") {
|
|
1044
|
+
try {
|
|
1045
|
+
parsedSnapshot = JSON.parse(row.snapshot);
|
|
1046
|
+
} catch (e) {
|
|
1047
|
+
console.warn(`Failed to parse snapshot for workflow ${row.workflow_name}: ${e}`);
|
|
1048
|
+
}
|
|
1049
|
+
}
|
|
1050
|
+
return {
|
|
1051
|
+
workflowName: row.workflow_name,
|
|
1052
|
+
runId: row.run_id,
|
|
1053
|
+
snapshot: parsedSnapshot,
|
|
1054
|
+
createdAt: this.ensureDate(row.createdAt),
|
|
1055
|
+
updatedAt: this.ensureDate(row.updatedAt),
|
|
1056
|
+
resourceId: row.resourceId
|
|
1057
|
+
};
|
|
1058
|
+
}
|
|
1059
|
+
buildWorkflowSnapshotPrefix({
|
|
1060
|
+
namespace,
|
|
1061
|
+
workflowName,
|
|
1062
|
+
runId,
|
|
1063
|
+
resourceId
|
|
1064
|
+
}) {
|
|
1065
|
+
const prefix = this.namespacePrefix ? `${this.namespacePrefix}:` : "";
|
|
1066
|
+
let key = `${prefix}${TABLE_WORKFLOW_SNAPSHOT}`;
|
|
1067
|
+
if (namespace) key += `:${namespace}`;
|
|
1068
|
+
if (workflowName) key += `:${workflowName}`;
|
|
1069
|
+
if (runId) key += `:${runId}`;
|
|
1070
|
+
if (resourceId) key += `:${resourceId}`;
|
|
1071
|
+
if (!resourceId && (runId || workflowName || namespace)) key += ":";
|
|
1072
|
+
return key;
|
|
1073
|
+
}
|
|
1074
|
+
async getWorkflowRuns({
|
|
1075
|
+
namespace,
|
|
1076
|
+
workflowName,
|
|
1077
|
+
limit = 20,
|
|
1078
|
+
offset = 0,
|
|
1079
|
+
resourceId,
|
|
1080
|
+
fromDate,
|
|
1081
|
+
toDate
|
|
1082
|
+
} = {}) {
|
|
1083
|
+
try {
|
|
1084
|
+
const prefix = this.buildWorkflowSnapshotPrefix({ namespace, workflowName });
|
|
1085
|
+
const keyObjs = await this.listKV(TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
1086
|
+
const runs = [];
|
|
1087
|
+
for (const { name: key } of keyObjs) {
|
|
1088
|
+
const parts = key.split(":");
|
|
1089
|
+
const idx = parts.indexOf(TABLE_WORKFLOW_SNAPSHOT);
|
|
1090
|
+
if (idx === -1 || parts.length < idx + 4) continue;
|
|
1091
|
+
const ns = parts[idx + 1];
|
|
1092
|
+
const wfName = parts[idx + 2];
|
|
1093
|
+
const keyResourceId = parts.length > idx + 4 ? parts[idx + 4] : void 0;
|
|
1094
|
+
if (namespace && ns !== namespace || workflowName && wfName !== workflowName) continue;
|
|
1095
|
+
if (resourceId && keyResourceId && keyResourceId !== resourceId) continue;
|
|
1096
|
+
const data = await this.getKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
1097
|
+
if (!data) continue;
|
|
1098
|
+
try {
|
|
1099
|
+
if (resourceId && data.resourceId && data.resourceId !== resourceId) continue;
|
|
1100
|
+
const createdAt = this.ensureDate(data.createdAt);
|
|
1101
|
+
if (fromDate && createdAt && createdAt < fromDate) continue;
|
|
1102
|
+
if (toDate && createdAt && createdAt > toDate) continue;
|
|
1103
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
1104
|
+
this.validateWorkflowState(state);
|
|
1105
|
+
const run = this.parseWorkflowRun({ ...data, snapshot: state });
|
|
1106
|
+
runs.push(run);
|
|
1107
|
+
} catch (err) {
|
|
1108
|
+
this.logger.error("Failed to parse workflow snapshot:", { key, error: err });
|
|
1109
|
+
}
|
|
1110
|
+
}
|
|
1111
|
+
runs.sort((a, b) => {
|
|
1112
|
+
const aDate = a.createdAt ? new Date(a.createdAt).getTime() : 0;
|
|
1113
|
+
const bDate = b.createdAt ? new Date(b.createdAt).getTime() : 0;
|
|
1114
|
+
return bDate - aDate;
|
|
1115
|
+
});
|
|
1116
|
+
const pagedRuns = runs.slice(offset, offset + limit);
|
|
1117
|
+
return {
|
|
1118
|
+
runs: pagedRuns,
|
|
1119
|
+
total: runs.length
|
|
1120
|
+
};
|
|
1121
|
+
} catch (error) {
|
|
1122
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1123
|
+
this.logger.error("Error in getWorkflowRuns:", { message });
|
|
1124
|
+
return { runs: [], total: 0 };
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
async getWorkflowRunById({
|
|
1128
|
+
namespace,
|
|
1129
|
+
runId,
|
|
1130
|
+
workflowName
|
|
1131
|
+
}) {
|
|
1132
|
+
try {
|
|
1133
|
+
if (!runId || !workflowName || !namespace) {
|
|
1134
|
+
throw new Error("runId, workflowName, and namespace are required");
|
|
1135
|
+
}
|
|
1136
|
+
const prefix = this.buildWorkflowSnapshotPrefix({ namespace, workflowName, runId });
|
|
1137
|
+
const keyObjs = await this.listKV(TABLE_WORKFLOW_SNAPSHOT, { prefix });
|
|
1138
|
+
if (!keyObjs.length) return null;
|
|
1139
|
+
const key = keyObjs[0]?.name;
|
|
1140
|
+
const data = await this.getKV(TABLE_WORKFLOW_SNAPSHOT, key);
|
|
1141
|
+
if (!data) return null;
|
|
1142
|
+
const state = this.normalizeWorkflowState(data.snapshot || data);
|
|
1143
|
+
this.validateWorkflowState(state);
|
|
1144
|
+
return this.parseWorkflowRun({ ...data, snapshot: state });
|
|
1145
|
+
} catch (error) {
|
|
1146
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1147
|
+
this.logger.error("Error in getWorkflowRunById:", { message });
|
|
1148
|
+
return null;
|
|
1149
|
+
}
|
|
1001
1150
|
}
|
|
1002
1151
|
async close() {
|
|
1003
1152
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/cloudflare",
|
|
3
|
-
"version": "0.1.1-alpha.
|
|
3
|
+
"version": "0.1.1-alpha.5",
|
|
4
4
|
"description": "Cloudflare provider for Mastra - includes db storage capabilities",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"files": [
|
|
@@ -24,18 +24,18 @@
|
|
|
24
24
|
"license": "MIT",
|
|
25
25
|
"dependencies": {
|
|
26
26
|
"cloudflare": "^4.1.0",
|
|
27
|
-
"@mastra/core": "^0.9.1-alpha.
|
|
27
|
+
"@mastra/core": "^0.9.1-alpha.4"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
30
|
"@cloudflare/workers-types": "^4.20250313.0",
|
|
31
|
-
"@microsoft/api-extractor": "^7.52.
|
|
31
|
+
"@microsoft/api-extractor": "^7.52.5",
|
|
32
32
|
"@types/node": "^20.17.27",
|
|
33
33
|
"dotenv": "^16.4.7",
|
|
34
34
|
"eslint": "^9.23.0",
|
|
35
35
|
"miniflare": "^4.20250310.0",
|
|
36
36
|
"tsup": "^8.4.0",
|
|
37
37
|
"typescript": "^5.8.2",
|
|
38
|
-
"vitest": "^3.
|
|
38
|
+
"vitest": "^3.1.2",
|
|
39
39
|
"@internal/lint": "0.0.2"
|
|
40
40
|
},
|
|
41
41
|
"scripts": {
|