@mastra/cloudflare 1.6.2 → 1.6.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.
@@ -24,8 +24,8 @@ let _mastra_core_error = require("@mastra/core/error");
24
24
  let _mastra_core_storage = require("@mastra/core/storage");
25
25
  let cloudflare = require("cloudflare");
26
26
  cloudflare = __toESM(cloudflare, 1);
27
- let _mastra_core_base = require("@mastra/core/base");
28
27
  let _mastra_core_agent = require("@mastra/core/agent");
28
+ let _mastra_core_base = require("@mastra/core/base");
29
29
  let _mastra_core_evals = require("@mastra/core/evals");
30
30
  //#region src/kv/storage/db/index.ts
31
31
  /**
@@ -371,7 +371,7 @@ var CloudflareKVDB = class extends _mastra_core_base.MastraBase {
371
371
  await this.client.kv.namespaces.values.update(namespaceId, key, {
372
372
  account_id: this.accountId,
373
373
  value: serializedValue,
374
- metadata: serializedMetadata
374
+ ...serializedMetadata ? { metadata: serializedMetadata } : {}
375
375
  });
376
376
  }
377
377
  } catch (error) {
@@ -444,18 +444,35 @@ var CloudflareKVDB = class extends _mastra_core_base.MastraBase {
444
444
  }
445
445
  async listNamespaceKeys(tableName, options) {
446
446
  try {
447
- if (this.bindings) return (await this.getBinding(tableName).list({
448
- limit: options?.limit || 1e3,
449
- prefix: options?.prefix
450
- })).keys;
451
- else {
447
+ const pageSize = options?.limit || 1e3;
448
+ const keys = [];
449
+ if (this.bindings) {
450
+ const binding = this.getBinding(tableName);
451
+ let cursor;
452
+ do {
453
+ const response = await binding.list({
454
+ limit: pageSize,
455
+ prefix: options?.prefix,
456
+ cursor
457
+ });
458
+ keys.push(...response.keys);
459
+ cursor = response.list_complete ? void 0 : response.cursor;
460
+ } while (cursor);
461
+ } else {
452
462
  const namespaceId = await this.getNamespaceId(tableName);
453
- return (await this.client.kv.namespaces.keys.list(namespaceId, {
454
- account_id: this.accountId,
455
- limit: options?.limit || 1e3,
456
- prefix: options?.prefix
457
- })).result;
463
+ let cursor;
464
+ do {
465
+ const page = await this.client.kv.namespaces.keys.list(namespaceId, {
466
+ account_id: this.accountId,
467
+ limit: pageSize,
468
+ prefix: options?.prefix,
469
+ ...cursor ? { cursor } : {}
470
+ });
471
+ keys.push(...page.result);
472
+ cursor = page.result_info?.cursor || void 0;
473
+ } while (cursor);
458
474
  }
475
+ return keys;
459
476
  } catch (error) {
460
477
  throw new _mastra_core_error.MastraError({
461
478
  id: (0, _mastra_core_storage.createStorageErrorId)("CLOUDFLARE", "LIST_NAMESPACE_KEYS", "FAILED"),
@@ -490,149 +507,6 @@ var CloudflareKVDB = class extends _mastra_core_base.MastraBase {
490
507
  }
491
508
  };
492
509
  //#endregion
493
- //#region src/kv/storage/domains/background-tasks/index.ts
494
- function toRecord(task) {
495
- return {
496
- id: task.id,
497
- tool_call_id: task.toolCallId,
498
- tool_name: task.toolName,
499
- agent_id: task.agentId,
500
- thread_id: task.threadId ?? null,
501
- resource_id: task.resourceId ?? null,
502
- run_id: task.runId,
503
- status: task.status,
504
- args: task.args,
505
- result: task.result ?? null,
506
- error: task.error ?? null,
507
- suspend_payload: task.suspendPayload ?? null,
508
- retry_count: task.retryCount,
509
- max_retries: task.maxRetries,
510
- timeout_ms: task.timeoutMs,
511
- createdAt: task.createdAt.toISOString(),
512
- startedAt: task.startedAt?.toISOString() ?? null,
513
- suspendedAt: task.suspendedAt?.toISOString() ?? null,
514
- completedAt: task.completedAt?.toISOString() ?? null
515
- };
516
- }
517
- function fromRecord(record) {
518
- return {
519
- id: record.id,
520
- status: record.status,
521
- toolName: record.tool_name,
522
- toolCallId: record.tool_call_id,
523
- args: record.args ?? {},
524
- agentId: record.agent_id,
525
- threadId: record.thread_id ?? void 0,
526
- resourceId: record.resource_id ?? void 0,
527
- runId: record.run_id ?? "",
528
- result: record.result ?? void 0,
529
- error: record.error ?? void 0,
530
- suspendPayload: record.suspend_payload ?? void 0,
531
- retryCount: Number(record.retry_count ?? 0),
532
- maxRetries: Number(record.max_retries ?? 0),
533
- timeoutMs: Number(record.timeout_ms ?? 3e5),
534
- createdAt: new Date(record.createdAt),
535
- startedAt: record.startedAt ? new Date(record.startedAt) : void 0,
536
- suspendedAt: record.suspendedAt ? new Date(record.suspendedAt) : void 0,
537
- completedAt: record.completedAt ? new Date(record.completedAt) : void 0
538
- };
539
- }
540
- var BackgroundTasksStorageCloudflare = class extends _mastra_core_storage.BackgroundTasksStorage {
541
- #db;
542
- constructor(config) {
543
- super();
544
- this.#db = new CloudflareKVDB(resolveCloudflareConfig(config));
545
- }
546
- async dangerouslyClearAll() {
547
- await this.#db.clearTable({ tableName: _mastra_core_storage.TABLE_BACKGROUND_TASKS });
548
- }
549
- async createTask(task) {
550
- await this.#db.putKV({
551
- tableName: _mastra_core_storage.TABLE_BACKGROUND_TASKS,
552
- key: task.id,
553
- value: toRecord(task)
554
- });
555
- }
556
- async updateTask(taskId, update) {
557
- const existing = await this.getTask(taskId);
558
- if (!existing) return;
559
- const merged = { ...existing };
560
- if ("status" in update) merged.status = update.status;
561
- if ("result" in update) merged.result = update.result;
562
- if ("error" in update) merged.error = update.error;
563
- if ("suspendPayload" in update) merged.suspendPayload = update.suspendPayload;
564
- if ("retryCount" in update) merged.retryCount = update.retryCount;
565
- if ("startedAt" in update) merged.startedAt = update.startedAt;
566
- if ("suspendedAt" in update) merged.suspendedAt = update.suspendedAt;
567
- if ("completedAt" in update) merged.completedAt = update.completedAt;
568
- await this.#db.putKV({
569
- tableName: _mastra_core_storage.TABLE_BACKGROUND_TASKS,
570
- key: taskId,
571
- value: toRecord(merged)
572
- });
573
- }
574
- async getTask(taskId) {
575
- const data = await this.#db.getKV(_mastra_core_storage.TABLE_BACKGROUND_TASKS, taskId);
576
- return data ? fromRecord(data) : null;
577
- }
578
- async listTasks(filter) {
579
- const keys = await this.#db.listKV(_mastra_core_storage.TABLE_BACKGROUND_TASKS);
580
- if (keys.length === 0) return {
581
- tasks: [],
582
- total: 0
583
- };
584
- let tasks = (await Promise.all(keys.map((k) => this.#db.getKV(_mastra_core_storage.TABLE_BACKGROUND_TASKS, k.name)))).filter(Boolean).map((r) => fromRecord(r));
585
- if (filter.status) {
586
- const s = Array.isArray(filter.status) ? filter.status : [filter.status];
587
- tasks = tasks.filter((t) => s.includes(t.status));
588
- }
589
- if (filter.agentId) tasks = tasks.filter((t) => t.agentId === filter.agentId);
590
- if (filter.threadId) tasks = tasks.filter((t) => t.threadId === filter.threadId);
591
- if (filter.toolName) tasks = tasks.filter((t) => t.toolName === filter.toolName);
592
- if (filter.toolCallId) tasks = tasks.filter((t) => t.toolCallId === filter.toolCallId);
593
- if (filter.runId) tasks = tasks.filter((t) => t.runId === filter.runId);
594
- const dateCol = filter.dateFilterBy ?? "createdAt";
595
- if (filter.fromDate) tasks = tasks.filter((t) => {
596
- const val = t[dateCol];
597
- return val != null && val >= filter.fromDate;
598
- });
599
- if (filter.toDate) tasks = tasks.filter((t) => {
600
- const val = t[dateCol];
601
- return val != null && val < filter.toDate;
602
- });
603
- const orderBy = filter.orderBy ?? "createdAt";
604
- const dir = filter.orderDirection === "desc" ? -1 : 1;
605
- tasks.sort((a, b) => ((a[orderBy]?.getTime() ?? 0) - (b[orderBy]?.getTime() ?? 0)) * dir);
606
- const total = tasks.length;
607
- if (filter.page != null && filter.perPage != null) {
608
- const start = filter.page * filter.perPage;
609
- tasks = tasks.slice(start, start + filter.perPage);
610
- } else if (filter.perPage != null) tasks = tasks.slice(0, filter.perPage);
611
- return {
612
- tasks,
613
- total
614
- };
615
- }
616
- async deleteTask(taskId) {
617
- await this.#db.deleteKV(_mastra_core_storage.TABLE_BACKGROUND_TASKS, taskId);
618
- }
619
- async deleteTasks(filter) {
620
- const { tasks } = await this.listTasks(filter);
621
- await Promise.all(tasks.map((t) => this.#db.deleteKV(_mastra_core_storage.TABLE_BACKGROUND_TASKS, t.id)));
622
- }
623
- async getRunningCount() {
624
- const { total } = await this.listTasks({ status: "running" });
625
- return total;
626
- }
627
- async getRunningCountByAgent(agentId) {
628
- const { total } = await this.listTasks({
629
- status: "running",
630
- agentId
631
- });
632
- return total;
633
- }
634
- };
635
- //#endregion
636
510
  //#region src/kv/storage/domains/memory/index.ts
637
511
  var MemoryStorageCloudflare = class extends _mastra_core_storage.MemoryStorage {
638
512
  supportsPartialThreadUpdate = true;
@@ -825,7 +699,8 @@ var MemoryStorageCloudflare = class extends _mastra_core_storage.MemoryStorage {
825
699
  async deleteThread({ threadId }) {
826
700
  try {
827
701
  if (!await this.getThreadById({ threadId })) throw new Error(`Thread ${threadId} not found`);
828
- const threadMessageKeys = (await this.#db.listKV(_mastra_core_storage.TABLE_MESSAGES)).filter((key) => key.name.includes(`${_mastra_core_storage.TABLE_MESSAGES}:${threadId}:`));
702
+ const prefix = this.#db.namespacePrefix ? `${this.#db.namespacePrefix}:` : "";
703
+ const threadMessageKeys = (await this.#db.listKV(_mastra_core_storage.TABLE_MESSAGES, { prefix: `${prefix}${_mastra_core_storage.TABLE_MESSAGES}:${threadId}:` })).filter((key) => key.name.includes(`${_mastra_core_storage.TABLE_MESSAGES}:${threadId}:`));
829
704
  await Promise.all([
830
705
  this.#db.deleteKV(_mastra_core_storage.TABLE_MESSAGES, this.getThreadMessagesKey(threadId)),
831
706
  ...threadMessageKeys.map((key) => this.#db.deleteKV(_mastra_core_storage.TABLE_MESSAGES, key.name)),
@@ -2106,8 +1981,7 @@ var CloudflareKVStorage = class extends _mastra_core_storage.MastraCompositeStor
2106
1981
  _mastra_core_storage.TABLE_THREADS,
2107
1982
  _mastra_core_storage.TABLE_MESSAGES,
2108
1983
  _mastra_core_storage.TABLE_WORKFLOW_SNAPSHOT,
2109
- _mastra_core_storage.TABLE_SCORERS,
2110
- _mastra_core_storage.TABLE_BACKGROUND_TASKS
1984
+ _mastra_core_storage.TABLE_SCORERS
2111
1985
  ];
2112
1986
  for (const table of requiredTables) if (!(table in config.bindings)) throw new Error(`Missing KV binding for table: ${table}`);
2113
1987
  }
@@ -2126,7 +2000,6 @@ var CloudflareKVStorage = class extends _mastra_core_storage.MastraCompositeStor
2126
2000
  let workflows;
2127
2001
  let memory;
2128
2002
  let scores;
2129
- let backgroundTasks;
2130
2003
  if (isWorkersConfig(config)) {
2131
2004
  this.validateWorkersConfig(config);
2132
2005
  this.bindings = config.bindings;
@@ -2139,7 +2012,6 @@ var CloudflareKVStorage = class extends _mastra_core_storage.MastraCompositeStor
2139
2012
  workflows = new WorkflowsStorageCloudflare(domainConfig);
2140
2013
  memory = new MemoryStorageCloudflare(domainConfig);
2141
2014
  scores = new ScoresStorageCloudflare(domainConfig);
2142
- backgroundTasks = new BackgroundTasksStorageCloudflare(domainConfig);
2143
2015
  } else {
2144
2016
  this.validateRestConfig(config);
2145
2017
  this.accountId = config.accountId.trim();
@@ -2154,13 +2026,11 @@ var CloudflareKVStorage = class extends _mastra_core_storage.MastraCompositeStor
2154
2026
  workflows = new WorkflowsStorageCloudflare(domainConfig);
2155
2027
  memory = new MemoryStorageCloudflare(domainConfig);
2156
2028
  scores = new ScoresStorageCloudflare(domainConfig);
2157
- backgroundTasks = new BackgroundTasksStorageCloudflare(domainConfig);
2158
2029
  }
2159
2030
  this.stores = {
2160
2031
  workflows,
2161
2032
  memory,
2162
- scores,
2163
- backgroundTasks
2033
+ scores
2164
2034
  };
2165
2035
  } catch (error) {
2166
2036
  throw new _mastra_core_error.MastraError({
@@ -2177,12 +2047,6 @@ var CloudflareKVStorage = class extends _mastra_core_storage.MastraCompositeStor
2177
2047
  */
2178
2048
  const CloudflareStore = CloudflareKVStorage;
2179
2049
  //#endregion
2180
- Object.defineProperty(exports, "BackgroundTasksStorageCloudflare", {
2181
- enumerable: true,
2182
- get: function() {
2183
- return BackgroundTasksStorageCloudflare;
2184
- }
2185
- });
2186
2050
  Object.defineProperty(exports, "CloudflareKVStorage", {
2187
2051
  enumerable: true,
2188
2052
  get: function() {
@@ -2214,4 +2078,4 @@ Object.defineProperty(exports, "WorkflowsStorageCloudflare", {
2214
2078
  }
2215
2079
  });
2216
2080
 
2217
- //# sourceMappingURL=kv-Dxgei0aW.cjs.map
2081
+ //# sourceMappingURL=kv-CVZicPPC.cjs.map