@mastra/redis 1.4.11 → 1.4.12-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/index.js CHANGED
@@ -4,7 +4,7 @@ import { MastraStorage, MemoryStorage, RunFenceConflictError, ScoresStorage, TAB
4
4
  import { saveScorePayloadSchema } from "@mastra/core/evals";
5
5
  import { createClient } from "redis";
6
6
  import { MastraServerCache } from "@mastra/core/cache";
7
- //#region src/storage/domains/utils.ts
7
+ // src/storage/domains/utils.ts
8
8
  /**
9
9
  * Generate a Redis key from table name and key parts.
10
10
  *
@@ -48,8 +48,7 @@ function processRecord(tableName, record) {
48
48
  processedRecord
49
49
  };
50
50
  }
51
- //#endregion
52
- //#region src/storage/db/index.ts
51
+ // src/storage/db/index.ts
53
52
  var RedisDB = class {
54
53
  client;
55
54
  constructor({ client }) {
@@ -129,8 +128,7 @@ var RedisDB = class {
129
128
  }
130
129
  }
131
130
  };
132
- //#endregion
133
- //#region src/storage/domains/run-fencing.ts
131
+ // src/storage/domains/run-fencing.ts
134
132
  /**
135
133
  * A claim expires after this long without a claim, renewal or release, and a
136
134
  * fence this long after it was retired. A running execution renews every few
@@ -360,8 +358,7 @@ function writeBatch(client, check) {
360
358
  async function assertRunFence(client, check) {
361
359
  if (check) await new FencedWriteBatch(client, check).exec();
362
360
  }
363
- //#endregion
364
- //#region src/storage/domains/memory/index.ts
361
+ // src/storage/domains/memory/index.ts
365
362
  var StoreMemoryRedis = class extends MemoryStorage {
366
363
  supportsPartialThreadUpdate = true;
367
364
  client;
@@ -1292,8 +1289,7 @@ function getMessageScore(message) {
1292
1289
  const index = typeof message._index === "number" ? message._index : 0;
1293
1290
  return createdAtScore * 1e3 + index;
1294
1291
  }
1295
- //#endregion
1296
- //#region src/storage/domains/scores/index.ts
1292
+ // src/storage/domains/scores/index.ts
1297
1293
  /** Returns true when a row matches the multi-tenant scope filters (or none provided). */
1298
1294
  function matchesTenancy(row, filters) {
1299
1295
  if (filters?.organizationId !== void 0 && row.organizationId !== filters.organizationId) return false;
@@ -1443,8 +1439,7 @@ var ScoresRedis = class extends ScoresStorage {
1443
1439
  };
1444
1440
  }
1445
1441
  };
1446
- //#endregion
1447
- //#region src/storage/domains/workflows/index.ts
1442
+ // src/storage/domains/workflows/index.ts
1448
1443
  function parseWorkflowRun(row) {
1449
1444
  let parsedSnapshot = row.snapshot;
1450
1445
  if (typeof parsedSnapshot === "string") try {
@@ -1812,16 +1807,14 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1812
1807
  }
1813
1808
  }
1814
1809
  };
1815
- //#endregion
1816
- //#region src/storage/utils.ts
1810
+ // src/storage/utils.ts
1817
1811
  function isClientConfig(config) {
1818
1812
  return "client" in config;
1819
1813
  }
1820
1814
  function isConnectionStringConfig(config) {
1821
1815
  return "connectionString" in config;
1822
1816
  }
1823
- //#endregion
1824
- //#region src/storage/store.ts
1817
+ // src/storage/store.ts
1825
1818
  /**
1826
1819
  * Redis storage adapter for Mastra.
1827
1820
  *
@@ -1931,8 +1924,7 @@ var RedisStore = class extends MastraStorage {
1931
1924
  return `redis://${config.host}:${config.port || 6379}/${config.db || 0}`;
1932
1925
  }
1933
1926
  };
1934
- //#endregion
1935
- //#region src/cache.ts
1927
+ // src/cache.ts
1936
1928
  const defaultSetWithExpiry = (client, key, value, seconds) => {
1937
1929
  return client.set(key, value, "EX", seconds);
1938
1930
  };
@@ -1952,11 +1944,12 @@ const defaultEvalScript = (client, script, keys, args) => {
1952
1944
  return client.eval(script, keys.length, ...keys, ...args);
1953
1945
  };
1954
1946
  /**
1955
- * Atomic "allocate index + append + refresh TTLs" used by `listPushIndexed`.
1947
+ * Atomic "allocate index + append + refresh TTL" used by `listPushIndexed`.
1956
1948
  *
1957
1949
  * KEYS[1] = list key, KEYS[2] = counter key
1958
1950
  * ARGV[1] = JSON-serialized object WITHOUT an `index` property
1959
- * ARGV[2] = TTL in seconds (0 = no expiry)
1951
+ * ARGV[2] = TTL in seconds (0 = no expiry), applied to BOTH keys
1952
+
1960
1953
  *
1961
1954
  * The index is spliced into the JSON text as the first property rather than
1962
1955
  * decoded/re-encoded with cjson, which would silently alter large integers
@@ -2067,24 +2060,41 @@ var RedisServerCache = class extends MastraServerCache {
2067
2060
  }
2068
2061
  /**
2069
2062
  * Single round-trip index allocation + list append + TTL refresh via Lua.
2070
- * This is the durable stream per-chunk hot path (issue #22477): the composed
2071
- * default costs four awaited commands (INCR, EXPIRE, RPUSH, EXPIRE).
2063
+ * This is the durable stream per-chunk hot path (issue #22477).
2072
2064
  */
2073
- async listPushIndexed(listKey, counterKey, value) {
2074
- if (this.scriptingDisabled) return super.listPushIndexed(listKey, counterKey, value);
2065
+ async listPushIndexed(listKey, counterKey, value, ttlMs) {
2066
+ const ttlSeconds = this.ttlSeconds > 0 ? Math.max(this.ttlSeconds, Math.ceil((ttlMs ?? 0) / 1e3)) : 0;
2067
+ if (this.scriptingDisabled) return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);
2075
2068
  const { index: _ignored, ...body } = value;
2076
2069
  try {
2077
- const result = await this.evalScript(this.client, LIST_PUSH_INDEXED_SCRIPT, [this.getKey(listKey), this.getKey(counterKey)], [this.serialize(body), String(this.ttlSeconds)]);
2070
+ const result = await this.evalScript(this.client, LIST_PUSH_INDEXED_SCRIPT, [this.getKey(listKey), this.getKey(counterKey)], [this.serialize(body), String(ttlSeconds)]);
2078
2071
  return Number(result);
2079
2072
  } catch (error) {
2080
2073
  if (error instanceof Error && error.message.includes("CROSSSLOT")) {
2081
2074
  this.scriptingDisabled = true;
2082
2075
  this.logger.warn("[RedisServerCache] listPushIndexed script rejected with CROSSSLOT; falling back to increment + listPush");
2083
- return super.listPushIndexed(listKey, counterKey, value);
2076
+ return this.composedPushIndexed(listKey, counterKey, value, ttlSeconds);
2084
2077
  }
2085
2078
  throw error;
2086
2079
  }
2087
2080
  }
2081
+ /**
2082
+ * `INCR` then `RPUSH`, each followed by the same `EXPIRE` the script applies.
2083
+ * Not atomic: concurrent writers can interleave, so a list position may not
2084
+ * match its index (Redis Cluster atomicity is tracked in COR-1491).
2085
+ */
2086
+ async composedPushIndexed(listKey, counterKey, value, ttlSeconds) {
2087
+ const fullCounterKey = this.getKey(counterKey);
2088
+ const index = await this.client.incr(fullCounterKey) - 1;
2089
+ if (ttlSeconds > 0) await this.client.expire(fullCounterKey, ttlSeconds);
2090
+ const fullListKey = this.getKey(listKey);
2091
+ await this.pushToList(this.client, fullListKey, this.serialize({
2092
+ ...value,
2093
+ index
2094
+ }));
2095
+ if (ttlSeconds > 0) await this.client.expire(fullListKey, ttlSeconds);
2096
+ return index;
2097
+ }
2088
2098
  };
2089
2099
  const upstashPreset = {
2090
2100
  setWithExpiry: (client, key, value, seconds) => client.set(key, value, { ex: seconds }),
@@ -2108,7 +2118,6 @@ const nodeRedisPreset = {
2108
2118
  arguments: args
2109
2119
  })
2110
2120
  };
2111
- //#endregion
2112
2121
  export { LIST_PUSH_INDEXED_SCRIPT, RedisServerCache, RedisStore, ScoresRedis, StoreMemoryRedis, WorkflowsRedis, nodeRedisPreset, upstashPreset };
2113
2122
 
2114
2123
  //# sourceMappingURL=index.js.map