@mastra/cloudflare 1.3.1 → 1.3.2-alpha.0
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 +9 -0
- package/dist/{chunk-ZBYNKKG6.cjs → chunk-3SMRLX2Z.cjs} +141 -6
- package/dist/chunk-3SMRLX2Z.cjs.map +1 -0
- package/dist/{chunk-NSFHQATX.js → chunk-7THHWTAZ.js} +276 -7
- package/dist/chunk-7THHWTAZ.js.map +1 -0
- package/dist/{chunk-O57GFJSB.js → chunk-HJZCD37D.js} +142 -8
- package/dist/chunk-HJZCD37D.js.map +1 -0
- package/dist/{chunk-E4MARS3A.cjs → chunk-T2WSIEXQ.cjs} +275 -5
- package/dist/chunk-T2WSIEXQ.cjs.map +1 -0
- package/dist/do/index.cjs +11 -7
- package/dist/do/index.d.ts +2 -1
- package/dist/do/index.d.ts.map +1 -1
- package/dist/do/index.js +1 -1
- package/dist/do/storage/domains/background-tasks/index.d.ts +18 -0
- package/dist/do/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/docs/SKILL.md +1 -1
- package/dist/docs/assets/SOURCE_MAP.json +20 -20
- package/dist/docs/references/reference-storage-cloudflare.md +3 -3
- package/dist/index.cjs +13 -13
- package/dist/index.js +2 -2
- package/dist/kv/index.cjs +10 -6
- package/dist/kv/index.d.ts +2 -1
- package/dist/kv/index.d.ts.map +1 -1
- package/dist/kv/index.js +1 -1
- package/dist/kv/storage/domains/background-tasks/index.d.ts +17 -0
- package/dist/kv/storage/domains/background-tasks/index.d.ts.map +1 -0
- package/dist/kv/storage/types.d.ts +1 -0
- package/dist/kv/storage/types.d.ts.map +1 -1
- package/package.json +7 -7
- package/dist/chunk-E4MARS3A.cjs.map +0 -1
- package/dist/chunk-NSFHQATX.js.map +0 -1
- package/dist/chunk-O57GFJSB.js.map +0 -1
- package/dist/chunk-ZBYNKKG6.cjs.map +0 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { MastraError, ErrorCategory, ErrorDomain } from '@mastra/core/error';
|
|
2
|
-
import { MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, filterByDateRange, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TABLE_TRACES, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
|
|
2
|
+
import { BackgroundTasksStorage, TABLE_BACKGROUND_TASKS, MemoryStorage, TABLE_MESSAGES, TABLE_THREADS, TABLE_RESOURCES, ensureDate, createStorageErrorId, normalizePerPage, calculatePagination, serializeDate, filterByDateRange, ScoresStorage, TABLE_SCORERS, WorkflowsStorage, TABLE_WORKFLOW_SNAPSHOT, MastraCompositeStore, TABLE_TRACES, transformScoreRow as transformScoreRow$1 } from '@mastra/core/storage';
|
|
3
3
|
import Cloudflare from 'cloudflare';
|
|
4
|
-
import { MessageList } from '@mastra/core/agent';
|
|
5
4
|
import { MastraBase } from '@mastra/core/base';
|
|
5
|
+
import { MessageList } from '@mastra/core/agent';
|
|
6
6
|
import { saveScorePayloadSchema } from '@mastra/core/evals';
|
|
7
7
|
|
|
8
8
|
// src/kv/index.ts
|
|
@@ -558,7 +558,131 @@ var CloudflareKVDB = class extends MastraBase {
|
|
|
558
558
|
}
|
|
559
559
|
};
|
|
560
560
|
|
|
561
|
-
// src/kv/storage/domains/
|
|
561
|
+
// src/kv/storage/domains/background-tasks/index.ts
|
|
562
|
+
function toRecord(task) {
|
|
563
|
+
return {
|
|
564
|
+
id: task.id,
|
|
565
|
+
tool_call_id: task.toolCallId,
|
|
566
|
+
tool_name: task.toolName,
|
|
567
|
+
agent_id: task.agentId,
|
|
568
|
+
thread_id: task.threadId ?? null,
|
|
569
|
+
resource_id: task.resourceId ?? null,
|
|
570
|
+
run_id: task.runId,
|
|
571
|
+
status: task.status,
|
|
572
|
+
args: task.args,
|
|
573
|
+
result: task.result ?? null,
|
|
574
|
+
error: task.error ?? null,
|
|
575
|
+
retry_count: task.retryCount,
|
|
576
|
+
max_retries: task.maxRetries,
|
|
577
|
+
timeout_ms: task.timeoutMs,
|
|
578
|
+
createdAt: task.createdAt.toISOString(),
|
|
579
|
+
startedAt: task.startedAt?.toISOString() ?? null,
|
|
580
|
+
completedAt: task.completedAt?.toISOString() ?? null
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
function fromRecord(record) {
|
|
584
|
+
return {
|
|
585
|
+
id: record.id,
|
|
586
|
+
status: record.status,
|
|
587
|
+
toolName: record.tool_name,
|
|
588
|
+
toolCallId: record.tool_call_id,
|
|
589
|
+
args: record.args ?? {},
|
|
590
|
+
agentId: record.agent_id,
|
|
591
|
+
threadId: record.thread_id ?? void 0,
|
|
592
|
+
resourceId: record.resource_id ?? void 0,
|
|
593
|
+
runId: record.run_id ?? "",
|
|
594
|
+
result: record.result ?? void 0,
|
|
595
|
+
error: record.error ?? void 0,
|
|
596
|
+
retryCount: Number(record.retry_count ?? 0),
|
|
597
|
+
maxRetries: Number(record.max_retries ?? 0),
|
|
598
|
+
timeoutMs: Number(record.timeout_ms ?? 3e5),
|
|
599
|
+
createdAt: new Date(record.createdAt),
|
|
600
|
+
startedAt: record.startedAt ? new Date(record.startedAt) : void 0,
|
|
601
|
+
completedAt: record.completedAt ? new Date(record.completedAt) : void 0
|
|
602
|
+
};
|
|
603
|
+
}
|
|
604
|
+
var BackgroundTasksStorageCloudflare = class extends BackgroundTasksStorage {
|
|
605
|
+
#db;
|
|
606
|
+
constructor(config) {
|
|
607
|
+
super();
|
|
608
|
+
this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
|
|
609
|
+
}
|
|
610
|
+
async dangerouslyClearAll() {
|
|
611
|
+
await this.#db.clearTable({ tableName: TABLE_BACKGROUND_TASKS });
|
|
612
|
+
}
|
|
613
|
+
async createTask(task) {
|
|
614
|
+
await this.#db.putKV({ tableName: TABLE_BACKGROUND_TASKS, key: task.id, value: toRecord(task) });
|
|
615
|
+
}
|
|
616
|
+
async updateTask(taskId, update) {
|
|
617
|
+
const existing = await this.getTask(taskId);
|
|
618
|
+
if (!existing) return;
|
|
619
|
+
const merged = { ...existing };
|
|
620
|
+
if ("status" in update) merged.status = update.status;
|
|
621
|
+
if ("result" in update) merged.result = update.result;
|
|
622
|
+
if ("error" in update) merged.error = update.error;
|
|
623
|
+
if ("retryCount" in update) merged.retryCount = update.retryCount;
|
|
624
|
+
if ("startedAt" in update) merged.startedAt = update.startedAt;
|
|
625
|
+
if ("completedAt" in update) merged.completedAt = update.completedAt;
|
|
626
|
+
await this.#db.putKV({ tableName: TABLE_BACKGROUND_TASKS, key: taskId, value: toRecord(merged) });
|
|
627
|
+
}
|
|
628
|
+
async getTask(taskId) {
|
|
629
|
+
const data = await this.#db.getKV(TABLE_BACKGROUND_TASKS, taskId);
|
|
630
|
+
return data ? fromRecord(data) : null;
|
|
631
|
+
}
|
|
632
|
+
async listTasks(filter) {
|
|
633
|
+
const keys = await this.#db.listKV(TABLE_BACKGROUND_TASKS);
|
|
634
|
+
if (keys.length === 0) return { tasks: [], total: 0 };
|
|
635
|
+
const records = await Promise.all(keys.map((k) => this.#db.getKV(TABLE_BACKGROUND_TASKS, k.name)));
|
|
636
|
+
let tasks = records.filter(Boolean).map((r) => fromRecord(r));
|
|
637
|
+
if (filter.status) {
|
|
638
|
+
const s = Array.isArray(filter.status) ? filter.status : [filter.status];
|
|
639
|
+
tasks = tasks.filter((t) => s.includes(t.status));
|
|
640
|
+
}
|
|
641
|
+
if (filter.agentId) tasks = tasks.filter((t) => t.agentId === filter.agentId);
|
|
642
|
+
if (filter.threadId) tasks = tasks.filter((t) => t.threadId === filter.threadId);
|
|
643
|
+
if (filter.toolName) tasks = tasks.filter((t) => t.toolName === filter.toolName);
|
|
644
|
+
if (filter.runId) tasks = tasks.filter((t) => t.runId === filter.runId);
|
|
645
|
+
const dateCol = filter.dateFilterBy ?? "createdAt";
|
|
646
|
+
if (filter.fromDate) {
|
|
647
|
+
tasks = tasks.filter((t) => {
|
|
648
|
+
const val = t[dateCol];
|
|
649
|
+
return val != null && val >= filter.fromDate;
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
if (filter.toDate) {
|
|
653
|
+
tasks = tasks.filter((t) => {
|
|
654
|
+
const val = t[dateCol];
|
|
655
|
+
return val != null && val < filter.toDate;
|
|
656
|
+
});
|
|
657
|
+
}
|
|
658
|
+
const orderBy = filter.orderBy ?? "createdAt";
|
|
659
|
+
const dir = filter.orderDirection === "desc" ? -1 : 1;
|
|
660
|
+
tasks.sort((a, b) => ((a[orderBy]?.getTime() ?? 0) - (b[orderBy]?.getTime() ?? 0)) * dir);
|
|
661
|
+
const total = tasks.length;
|
|
662
|
+
if (filter.page != null && filter.perPage != null) {
|
|
663
|
+
const start = filter.page * filter.perPage;
|
|
664
|
+
tasks = tasks.slice(start, start + filter.perPage);
|
|
665
|
+
} else if (filter.perPage != null) {
|
|
666
|
+
tasks = tasks.slice(0, filter.perPage);
|
|
667
|
+
}
|
|
668
|
+
return { tasks, total };
|
|
669
|
+
}
|
|
670
|
+
async deleteTask(taskId) {
|
|
671
|
+
await this.#db.deleteKV(TABLE_BACKGROUND_TASKS, taskId);
|
|
672
|
+
}
|
|
673
|
+
async deleteTasks(filter) {
|
|
674
|
+
const { tasks } = await this.listTasks(filter);
|
|
675
|
+
await Promise.all(tasks.map((t) => this.#db.deleteKV(TABLE_BACKGROUND_TASKS, t.id)));
|
|
676
|
+
}
|
|
677
|
+
async getRunningCount() {
|
|
678
|
+
const { total } = await this.listTasks({ status: "running" });
|
|
679
|
+
return total;
|
|
680
|
+
}
|
|
681
|
+
async getRunningCountByAgent(agentId) {
|
|
682
|
+
const { total } = await this.listTasks({ status: "running", agentId });
|
|
683
|
+
return total;
|
|
684
|
+
}
|
|
685
|
+
};
|
|
562
686
|
var MemoryStorageCloudflare = class extends MemoryStorage {
|
|
563
687
|
#db;
|
|
564
688
|
constructor(config) {
|
|
@@ -2186,7 +2310,13 @@ var CloudflareKVStorage = class extends MastraCompositeStore {
|
|
|
2186
2310
|
if (!config.bindings) {
|
|
2187
2311
|
throw new Error("KV bindings are required when using Workers Binding API");
|
|
2188
2312
|
}
|
|
2189
|
-
const requiredTables = [
|
|
2313
|
+
const requiredTables = [
|
|
2314
|
+
TABLE_THREADS,
|
|
2315
|
+
TABLE_MESSAGES,
|
|
2316
|
+
TABLE_WORKFLOW_SNAPSHOT,
|
|
2317
|
+
TABLE_SCORERS,
|
|
2318
|
+
TABLE_BACKGROUND_TASKS
|
|
2319
|
+
];
|
|
2190
2320
|
for (const table of requiredTables) {
|
|
2191
2321
|
if (!(table in config.bindings)) {
|
|
2192
2322
|
throw new Error(`Missing KV binding for table: ${table}`);
|
|
@@ -2210,6 +2340,7 @@ var CloudflareKVStorage = class extends MastraCompositeStore {
|
|
|
2210
2340
|
let workflows;
|
|
2211
2341
|
let memory;
|
|
2212
2342
|
let scores;
|
|
2343
|
+
let backgroundTasks;
|
|
2213
2344
|
if (isWorkersConfig(config)) {
|
|
2214
2345
|
this.validateWorkersConfig(config);
|
|
2215
2346
|
this.bindings = config.bindings;
|
|
@@ -2222,6 +2353,7 @@ var CloudflareKVStorage = class extends MastraCompositeStore {
|
|
|
2222
2353
|
workflows = new WorkflowsStorageCloudflare(domainConfig);
|
|
2223
2354
|
memory = new MemoryStorageCloudflare(domainConfig);
|
|
2224
2355
|
scores = new ScoresStorageCloudflare(domainConfig);
|
|
2356
|
+
backgroundTasks = new BackgroundTasksStorageCloudflare(domainConfig);
|
|
2225
2357
|
} else {
|
|
2226
2358
|
this.validateRestConfig(config);
|
|
2227
2359
|
this.accountId = config.accountId.trim();
|
|
@@ -2238,11 +2370,13 @@ var CloudflareKVStorage = class extends MastraCompositeStore {
|
|
|
2238
2370
|
workflows = new WorkflowsStorageCloudflare(domainConfig);
|
|
2239
2371
|
memory = new MemoryStorageCloudflare(domainConfig);
|
|
2240
2372
|
scores = new ScoresStorageCloudflare(domainConfig);
|
|
2373
|
+
backgroundTasks = new BackgroundTasksStorageCloudflare(domainConfig);
|
|
2241
2374
|
}
|
|
2242
2375
|
this.stores = {
|
|
2243
2376
|
workflows,
|
|
2244
2377
|
memory,
|
|
2245
|
-
scores
|
|
2378
|
+
scores,
|
|
2379
|
+
backgroundTasks
|
|
2246
2380
|
};
|
|
2247
2381
|
} catch (error) {
|
|
2248
2382
|
throw new MastraError(
|
|
@@ -2260,6 +2394,6 @@ var CloudflareKVStorage = class extends MastraCompositeStore {
|
|
|
2260
2394
|
};
|
|
2261
2395
|
var CloudflareStore = CloudflareKVStorage;
|
|
2262
2396
|
|
|
2263
|
-
export { CloudflareKVStorage, CloudflareStore, MemoryStorageCloudflare, ScoresStorageCloudflare, WorkflowsStorageCloudflare };
|
|
2264
|
-
//# sourceMappingURL=chunk-
|
|
2265
|
-
//# sourceMappingURL=chunk-
|
|
2397
|
+
export { BackgroundTasksStorageCloudflare, CloudflareKVStorage, CloudflareStore, MemoryStorageCloudflare, ScoresStorageCloudflare, WorkflowsStorageCloudflare };
|
|
2398
|
+
//# sourceMappingURL=chunk-HJZCD37D.js.map
|
|
2399
|
+
//# sourceMappingURL=chunk-HJZCD37D.js.map
|