@mastra/cloudflare 0.0.3-alpha.0 → 0.0.3-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/_tsup-dts-rollup.d.cts +19 -1
- package/dist/_tsup-dts-rollup.d.ts +19 -1
- package/dist/index.cjs +91 -3
- package/dist/index.js +91 -3
- package/package.json +1 -1
|
@@ -151,13 +151,14 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
151
151
|
tableName: T;
|
|
152
152
|
records: Partial<RecordTypes[T]>[];
|
|
153
153
|
}): Promise<void>;
|
|
154
|
-
getTraces(
|
|
154
|
+
getTraces({ name, scope, page, perPage, attributes, }: {
|
|
155
155
|
name?: string;
|
|
156
156
|
scope?: string;
|
|
157
157
|
page: number;
|
|
158
158
|
perPage: number;
|
|
159
159
|
attributes?: Record<string, string>;
|
|
160
160
|
}): Promise<any[]>;
|
|
161
|
+
private parseJSON;
|
|
161
162
|
getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
162
163
|
getWorkflowRuns(_args?: {
|
|
163
164
|
namespace?: string;
|
|
@@ -189,6 +190,23 @@ export declare interface CloudflareWorkersConfig {
|
|
|
189
190
|
keyPrefix?: string;
|
|
190
191
|
}
|
|
191
192
|
|
|
193
|
+
export declare const createSampleTrace: (name: string, scope?: string, attributes?: Record<string, string>) => {
|
|
194
|
+
id: string;
|
|
195
|
+
parentSpanId: string;
|
|
196
|
+
traceId: string;
|
|
197
|
+
name: string;
|
|
198
|
+
scope: string | undefined;
|
|
199
|
+
kind: string;
|
|
200
|
+
status: string;
|
|
201
|
+
events: string;
|
|
202
|
+
links: string;
|
|
203
|
+
attributes: string | undefined;
|
|
204
|
+
startTime: string;
|
|
205
|
+
endTime: string;
|
|
206
|
+
other: string;
|
|
207
|
+
createdAt: string;
|
|
208
|
+
};
|
|
209
|
+
|
|
192
210
|
/**
|
|
193
211
|
* Helper to determine if a config is using Workers bindings
|
|
194
212
|
*/
|
|
@@ -151,13 +151,14 @@ declare class CloudflareStore extends MastraStorage {
|
|
|
151
151
|
tableName: T;
|
|
152
152
|
records: Partial<RecordTypes[T]>[];
|
|
153
153
|
}): Promise<void>;
|
|
154
|
-
getTraces(
|
|
154
|
+
getTraces({ name, scope, page, perPage, attributes, }: {
|
|
155
155
|
name?: string;
|
|
156
156
|
scope?: string;
|
|
157
157
|
page: number;
|
|
158
158
|
perPage: number;
|
|
159
159
|
attributes?: Record<string, string>;
|
|
160
160
|
}): Promise<any[]>;
|
|
161
|
+
private parseJSON;
|
|
161
162
|
getEvalsByAgentName(_agentName: string, _type?: 'test' | 'live'): Promise<EvalRow[]>;
|
|
162
163
|
getWorkflowRuns(_args?: {
|
|
163
164
|
namespace?: string;
|
|
@@ -189,6 +190,23 @@ export declare interface CloudflareWorkersConfig {
|
|
|
189
190
|
keyPrefix?: string;
|
|
190
191
|
}
|
|
191
192
|
|
|
193
|
+
export declare const createSampleTrace: (name: string, scope?: string, attributes?: Record<string, string>) => {
|
|
194
|
+
id: string;
|
|
195
|
+
parentSpanId: string;
|
|
196
|
+
traceId: string;
|
|
197
|
+
name: string;
|
|
198
|
+
scope: string | undefined;
|
|
199
|
+
kind: string;
|
|
200
|
+
status: string;
|
|
201
|
+
events: string;
|
|
202
|
+
links: string;
|
|
203
|
+
attributes: string | undefined;
|
|
204
|
+
startTime: string;
|
|
205
|
+
endTime: string;
|
|
206
|
+
other: string;
|
|
207
|
+
createdAt: string;
|
|
208
|
+
};
|
|
209
|
+
|
|
192
210
|
/**
|
|
193
211
|
* Helper to determine if a config is using Workers bindings
|
|
194
212
|
*/
|
package/dist/index.cjs
CHANGED
|
@@ -349,7 +349,7 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
349
349
|
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
350
350
|
const latestIds = await this.getLastN(threadMessagesKey, limit);
|
|
351
351
|
latestIds.forEach((id) => messageIds.add(id));
|
|
352
|
-
} catch
|
|
352
|
+
} catch {
|
|
353
353
|
console.log(`No message order found for thread ${threadId}, skipping latest messages`);
|
|
354
354
|
}
|
|
355
355
|
}
|
|
@@ -438,6 +438,9 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
438
438
|
throw new Error("Namespace, workflow name, and run ID are required");
|
|
439
439
|
}
|
|
440
440
|
return `${prefix}${tableName}:${record.namespace}:${record.workflow_name}:${record.run_id}`;
|
|
441
|
+
case storage.TABLE_TRACES:
|
|
442
|
+
if (!record.id) throw new Error("Trace ID is required");
|
|
443
|
+
return `${prefix}${tableName}:${record.id}`;
|
|
441
444
|
default:
|
|
442
445
|
throw new Error(`Unsupported table: ${tableName}`);
|
|
443
446
|
}
|
|
@@ -527,6 +530,11 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
527
530
|
throw new Error("Workflow record missing required fields");
|
|
528
531
|
}
|
|
529
532
|
break;
|
|
533
|
+
case storage.TABLE_TRACES:
|
|
534
|
+
if (!("id" in recordTyped)) {
|
|
535
|
+
throw new Error("Trace record missing required fields");
|
|
536
|
+
}
|
|
537
|
+
break;
|
|
530
538
|
default:
|
|
531
539
|
throw new Error(`Unknown table type: ${tableName}`);
|
|
532
540
|
}
|
|
@@ -907,8 +915,88 @@ var CloudflareStore = class extends storage.MastraStorage {
|
|
|
907
915
|
throw error;
|
|
908
916
|
}
|
|
909
917
|
}
|
|
910
|
-
getTraces(
|
|
911
|
-
|
|
918
|
+
async getTraces({
|
|
919
|
+
name,
|
|
920
|
+
scope,
|
|
921
|
+
page = 0,
|
|
922
|
+
perPage = 100,
|
|
923
|
+
attributes
|
|
924
|
+
}) {
|
|
925
|
+
try {
|
|
926
|
+
let keys;
|
|
927
|
+
if (this.bindings) {
|
|
928
|
+
keys = (await this.listKV(storage.TABLE_TRACES))?.map((k) => k.name) || [];
|
|
929
|
+
} else {
|
|
930
|
+
const namespaceId = await this.getNamespaceId(storage.TABLE_TRACES);
|
|
931
|
+
const result = await this.client.kv.namespaces.keys.list(namespaceId, {
|
|
932
|
+
prefix: "",
|
|
933
|
+
limit: 1e3,
|
|
934
|
+
account_id: this.accountId
|
|
935
|
+
});
|
|
936
|
+
keys = result.result?.map((k) => k.name) || [];
|
|
937
|
+
}
|
|
938
|
+
const traceRecords = await Promise.all(
|
|
939
|
+
keys.map(async (key) => {
|
|
940
|
+
const record = await this.getKV(storage.TABLE_TRACES, key);
|
|
941
|
+
if (!record) return null;
|
|
942
|
+
return record;
|
|
943
|
+
})
|
|
944
|
+
);
|
|
945
|
+
let filteredTraces = traceRecords.filter(
|
|
946
|
+
(record) => record !== null && typeof record === "object"
|
|
947
|
+
);
|
|
948
|
+
if (name) {
|
|
949
|
+
filteredTraces = filteredTraces.filter((record) => record.name?.toLowerCase().startsWith(name.toLowerCase()));
|
|
950
|
+
}
|
|
951
|
+
if (scope) {
|
|
952
|
+
filteredTraces = filteredTraces.filter((record) => record.scope === scope);
|
|
953
|
+
}
|
|
954
|
+
if (attributes) {
|
|
955
|
+
filteredTraces = filteredTraces.filter((record) => {
|
|
956
|
+
if (!record.attributes) return false;
|
|
957
|
+
const recordAttrs = this.parseJSON(record.attributes);
|
|
958
|
+
if (!recordAttrs) return false;
|
|
959
|
+
return Object.entries(attributes).every(([key, value]) => recordAttrs[key] === value);
|
|
960
|
+
});
|
|
961
|
+
}
|
|
962
|
+
filteredTraces.sort((a, b) => {
|
|
963
|
+
const dateA = new Date(a.createdAt).getTime();
|
|
964
|
+
const dateB = new Date(b.createdAt).getTime();
|
|
965
|
+
return dateB - dateA;
|
|
966
|
+
});
|
|
967
|
+
const start = page * perPage;
|
|
968
|
+
const end = start + perPage;
|
|
969
|
+
const paginatedTraces = filteredTraces.slice(start, end);
|
|
970
|
+
return paginatedTraces.map((record) => ({
|
|
971
|
+
id: record.id,
|
|
972
|
+
parentSpanId: record.parentSpanId,
|
|
973
|
+
traceId: record.traceId,
|
|
974
|
+
name: record.name,
|
|
975
|
+
scope: record.scope,
|
|
976
|
+
kind: record.kind,
|
|
977
|
+
status: this.parseJSON(record.status),
|
|
978
|
+
events: this.parseJSON(record.events) || [],
|
|
979
|
+
links: this.parseJSON(record.links) || [],
|
|
980
|
+
attributes: this.parseJSON(record?.attributes) || {},
|
|
981
|
+
startTime: record.startTime,
|
|
982
|
+
endTime: record.endTime,
|
|
983
|
+
other: this.parseJSON(record.other) || {},
|
|
984
|
+
createdAt: record.createdAt
|
|
985
|
+
}));
|
|
986
|
+
} catch (error) {
|
|
987
|
+
this.logger.error("Failed to get traces:", { message: error instanceof Error ? error.message : String(error) });
|
|
988
|
+
return [];
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
parseJSON(value) {
|
|
992
|
+
if (typeof value === "string") {
|
|
993
|
+
try {
|
|
994
|
+
return JSON.parse(value);
|
|
995
|
+
} catch {
|
|
996
|
+
return value;
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
return value;
|
|
912
1000
|
}
|
|
913
1001
|
getEvalsByAgentName(_agentName, _type) {
|
|
914
1002
|
throw new Error("Method not implemented.");
|
package/dist/index.js
CHANGED
|
@@ -343,7 +343,7 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
343
343
|
const threadMessagesKey = this.getThreadMessagesKey(threadId);
|
|
344
344
|
const latestIds = await this.getLastN(threadMessagesKey, limit);
|
|
345
345
|
latestIds.forEach((id) => messageIds.add(id));
|
|
346
|
-
} catch
|
|
346
|
+
} catch {
|
|
347
347
|
console.log(`No message order found for thread ${threadId}, skipping latest messages`);
|
|
348
348
|
}
|
|
349
349
|
}
|
|
@@ -432,6 +432,9 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
432
432
|
throw new Error("Namespace, workflow name, and run ID are required");
|
|
433
433
|
}
|
|
434
434
|
return `${prefix}${tableName}:${record.namespace}:${record.workflow_name}:${record.run_id}`;
|
|
435
|
+
case TABLE_TRACES:
|
|
436
|
+
if (!record.id) throw new Error("Trace ID is required");
|
|
437
|
+
return `${prefix}${tableName}:${record.id}`;
|
|
435
438
|
default:
|
|
436
439
|
throw new Error(`Unsupported table: ${tableName}`);
|
|
437
440
|
}
|
|
@@ -521,6 +524,11 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
521
524
|
throw new Error("Workflow record missing required fields");
|
|
522
525
|
}
|
|
523
526
|
break;
|
|
527
|
+
case TABLE_TRACES:
|
|
528
|
+
if (!("id" in recordTyped)) {
|
|
529
|
+
throw new Error("Trace record missing required fields");
|
|
530
|
+
}
|
|
531
|
+
break;
|
|
524
532
|
default:
|
|
525
533
|
throw new Error(`Unknown table type: ${tableName}`);
|
|
526
534
|
}
|
|
@@ -901,8 +909,88 @@ var CloudflareStore = class extends MastraStorage {
|
|
|
901
909
|
throw error;
|
|
902
910
|
}
|
|
903
911
|
}
|
|
904
|
-
getTraces(
|
|
905
|
-
|
|
912
|
+
async getTraces({
|
|
913
|
+
name,
|
|
914
|
+
scope,
|
|
915
|
+
page = 0,
|
|
916
|
+
perPage = 100,
|
|
917
|
+
attributes
|
|
918
|
+
}) {
|
|
919
|
+
try {
|
|
920
|
+
let keys;
|
|
921
|
+
if (this.bindings) {
|
|
922
|
+
keys = (await this.listKV(TABLE_TRACES))?.map((k) => k.name) || [];
|
|
923
|
+
} else {
|
|
924
|
+
const namespaceId = await this.getNamespaceId(TABLE_TRACES);
|
|
925
|
+
const result = await this.client.kv.namespaces.keys.list(namespaceId, {
|
|
926
|
+
prefix: "",
|
|
927
|
+
limit: 1e3,
|
|
928
|
+
account_id: this.accountId
|
|
929
|
+
});
|
|
930
|
+
keys = result.result?.map((k) => k.name) || [];
|
|
931
|
+
}
|
|
932
|
+
const traceRecords = await Promise.all(
|
|
933
|
+
keys.map(async (key) => {
|
|
934
|
+
const record = await this.getKV(TABLE_TRACES, key);
|
|
935
|
+
if (!record) return null;
|
|
936
|
+
return record;
|
|
937
|
+
})
|
|
938
|
+
);
|
|
939
|
+
let filteredTraces = traceRecords.filter(
|
|
940
|
+
(record) => record !== null && typeof record === "object"
|
|
941
|
+
);
|
|
942
|
+
if (name) {
|
|
943
|
+
filteredTraces = filteredTraces.filter((record) => record.name?.toLowerCase().startsWith(name.toLowerCase()));
|
|
944
|
+
}
|
|
945
|
+
if (scope) {
|
|
946
|
+
filteredTraces = filteredTraces.filter((record) => record.scope === scope);
|
|
947
|
+
}
|
|
948
|
+
if (attributes) {
|
|
949
|
+
filteredTraces = filteredTraces.filter((record) => {
|
|
950
|
+
if (!record.attributes) return false;
|
|
951
|
+
const recordAttrs = this.parseJSON(record.attributes);
|
|
952
|
+
if (!recordAttrs) return false;
|
|
953
|
+
return Object.entries(attributes).every(([key, value]) => recordAttrs[key] === value);
|
|
954
|
+
});
|
|
955
|
+
}
|
|
956
|
+
filteredTraces.sort((a, b) => {
|
|
957
|
+
const dateA = new Date(a.createdAt).getTime();
|
|
958
|
+
const dateB = new Date(b.createdAt).getTime();
|
|
959
|
+
return dateB - dateA;
|
|
960
|
+
});
|
|
961
|
+
const start = page * perPage;
|
|
962
|
+
const end = start + perPage;
|
|
963
|
+
const paginatedTraces = filteredTraces.slice(start, end);
|
|
964
|
+
return paginatedTraces.map((record) => ({
|
|
965
|
+
id: record.id,
|
|
966
|
+
parentSpanId: record.parentSpanId,
|
|
967
|
+
traceId: record.traceId,
|
|
968
|
+
name: record.name,
|
|
969
|
+
scope: record.scope,
|
|
970
|
+
kind: record.kind,
|
|
971
|
+
status: this.parseJSON(record.status),
|
|
972
|
+
events: this.parseJSON(record.events) || [],
|
|
973
|
+
links: this.parseJSON(record.links) || [],
|
|
974
|
+
attributes: this.parseJSON(record?.attributes) || {},
|
|
975
|
+
startTime: record.startTime,
|
|
976
|
+
endTime: record.endTime,
|
|
977
|
+
other: this.parseJSON(record.other) || {},
|
|
978
|
+
createdAt: record.createdAt
|
|
979
|
+
}));
|
|
980
|
+
} catch (error) {
|
|
981
|
+
this.logger.error("Failed to get traces:", { message: error instanceof Error ? error.message : String(error) });
|
|
982
|
+
return [];
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
parseJSON(value) {
|
|
986
|
+
if (typeof value === "string") {
|
|
987
|
+
try {
|
|
988
|
+
return JSON.parse(value);
|
|
989
|
+
} catch {
|
|
990
|
+
return value;
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
return value;
|
|
906
994
|
}
|
|
907
995
|
getEvalsByAgentName(_agentName, _type) {
|
|
908
996
|
throw new Error("Method not implemented.");
|