@mastra/cloudflare-d1 1.4.5-alpha.1 → 1.4.5-alpha.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.
package/dist/index.js CHANGED
@@ -2356,6 +2356,9 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2356
2356
  super();
2357
2357
  this.#db = new D1DB(resolveD1Config(config));
2358
2358
  }
2359
+ supportsGuardedSnapshotWrites() {
2360
+ return true;
2361
+ }
2359
2362
  supportsConcurrentUpdates() {
2360
2363
  return false;
2361
2364
  }
@@ -2532,7 +2535,7 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2532
2535
  async updateWorkflowState(_args) {
2533
2536
  throw new Error("updateWorkflowState is not implemented for Cloudflare D1 storage. D1 does not support atomic read-modify-write operations needed for concurrent workflow updates.");
2534
2537
  }
2535
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
2538
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
2536
2539
  const fullTableName = this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT);
2537
2540
  const now = (/* @__PURE__ */ new Date()).toISOString();
2538
2541
  const check = this.#runFenceCheck(fence, runId, "persistWorkflowSnapshot");
@@ -2569,7 +2572,20 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2569
2572
  });
2570
2573
  const query = createSqlBuilder().insert(fullTableName, columns, values, ["workflow_name", "run_id"], updateMap, runFenceGuard(check));
2571
2574
  try {
2572
- await executeFenced(this.#db, check, query.build());
2575
+ if (expectedStatus === void 0) {
2576
+ await executeFenced(this.#db, check, query.build());
2577
+ return true;
2578
+ }
2579
+ const expectedStatuses = Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus];
2580
+ const { sql, params } = query.build();
2581
+ const statusGuard = expectedStatuses.length === 0 ? " WHERE 0" : ` WHERE (CASE WHEN json_valid(${fullTableName}.snapshot) THEN json_extract(${fullTableName}.snapshot, '$.status') END) IN (${expectedStatuses.map(() => "?").join(", ")})`;
2582
+ const rows = await this.#db.executeQuery({
2583
+ sql: `${sql}${statusGuard} RETURNING 1 AS written`,
2584
+ params: [...params, ...expectedStatuses]
2585
+ });
2586
+ const written = Array.isArray(rows) && rows.length > 0;
2587
+ if (!written && check) await assertRunFence(this.#db, check);
2588
+ return written;
2573
2589
  } catch (error) {
2574
2590
  if (isRunFenceConflictError(error)) throw error;
2575
2591
  throw new MastraError({
@@ -2584,6 +2600,26 @@ var WorkflowsStorageD1 = class WorkflowsStorageD1 extends WorkflowsStorage {
2584
2600
  }, error);
2585
2601
  }
2586
2602
  }
2603
+ async loadWorkflowRunStatus({ workflowName, runId }) {
2604
+ try {
2605
+ const row = await this.#db.executeQuery({
2606
+ sql: `SELECT CASE WHEN json_valid(snapshot) THEN json_extract(snapshot, '$.status') END AS status FROM ${this.#db.getTableName(TABLE_WORKFLOW_SNAPSHOT)} WHERE workflow_name = ? AND run_id = ?`,
2607
+ params: [workflowName, runId],
2608
+ first: true
2609
+ });
2610
+ return (row && !Array.isArray(row) ? row.status : void 0) ?? null;
2611
+ } catch (error) {
2612
+ throw new MastraError({
2613
+ id: createStorageErrorId("CLOUDFLARE_D1", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
2614
+ domain: ErrorDomain.STORAGE,
2615
+ category: ErrorCategory.THIRD_PARTY,
2616
+ details: {
2617
+ workflowName,
2618
+ runId
2619
+ }
2620
+ }, error);
2621
+ }
2622
+ }
2587
2623
  async loadWorkflowSnapshot(params) {
2588
2624
  const { workflowName, runId } = params;
2589
2625
  this.logger.debug("Loading workflow snapshot", {