@mastra/redis 1.4.13 → 1.4.14

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
@@ -193,6 +193,8 @@ return 1
193
193
  const READ = `${LIB}
194
194
  return owner(1, now())
195
195
  `;
196
+ /** Single-key reads `getRunOwnerships` keeps in flight, bounding the client's pipeline. */
197
+ const READ_BATCH_SIZE = 500;
196
198
  const RAISE = `
197
199
  local r = redis.call('HMGET', KEYS[1], 'generation', 'ownerId')
198
200
  local current = tonumber(r[1])
@@ -292,6 +294,24 @@ async function releaseRunOwnership(client, fence) {
292
294
  async function getRunOwnership(client, runId) {
293
295
  return (await evalOwner(client, READ, runId, [])).record;
294
296
  }
297
+ /**
298
+ * The ownership records of `runIds`. Each run is its own single-key script call, so the reads stay
299
+ * valid on a clustered server where the keys live in different hash slots; the client pipelines
300
+ * the {@link READ_BATCH_SIZE} calls it sends at once into one round trip.
301
+ */
302
+ async function getRunOwnerships(client, runIds) {
303
+ const records = /* @__PURE__ */ new Map();
304
+ const ids = [...new Set(runIds)];
305
+ for (let start = 0; start < ids.length; start += READ_BATCH_SIZE) {
306
+ const chunk = ids.slice(start, start + READ_BATCH_SIZE);
307
+ const read = await Promise.all(chunk.map((runId) => evalOwner(client, READ, runId, [])));
308
+ chunk.forEach((runId, i) => {
309
+ const record = read[i].record;
310
+ if (record) records.set(runId, record);
311
+ });
312
+ }
313
+ return records;
314
+ }
295
315
  async function raiseRunFence(client, fence) {
296
316
  const raised = await evalScript(client, RAISE, [runClaimKey(TABLE_MEMORY_RUN_FENCES, fence.runId)], [String(fence.generation), fence.ownerId]);
297
317
  return Number(raised) === 1;
@@ -1457,6 +1477,27 @@ function parseWorkflowRun(row) {
1457
1477
  resourceId: row.resourceId
1458
1478
  };
1459
1479
  }
1480
+ /**
1481
+ * A run with its snapshot reduced to what `listWorkflowRuns({ summary: true })` returns: `status`
1482
+ * and `timestamp` when set, and `parentWorkflow` only when both of its IDs are non-empty strings.
1483
+ * A missing snapshot, or one that failed to parse and stayed a string, reduces to `{}`.
1484
+ */
1485
+ function summarizeWorkflowRun(run) {
1486
+ const { status, timestamp, parentWorkflow } = run.snapshot !== null && typeof run.snapshot === "object" ? run.snapshot : {};
1487
+ const snapshot = {};
1488
+ if (status !== void 0 && status !== null) snapshot.status = status;
1489
+ if (timestamp !== void 0 && timestamp !== null) snapshot.timestamp = timestamp;
1490
+ const workflowId = parentWorkflow?.workflowId;
1491
+ const runId = parentWorkflow?.runId;
1492
+ if (typeof workflowId === "string" && workflowId !== "" && typeof runId === "string" && runId !== "") snapshot.parentWorkflow = {
1493
+ workflowId,
1494
+ runId
1495
+ };
1496
+ return {
1497
+ ...run,
1498
+ snapshot
1499
+ };
1500
+ }
1460
1501
  function escapeGlob(value) {
1461
1502
  return value.replace(/[*?[\]\\]/g, "\\$&");
1462
1503
  }
@@ -1547,6 +1588,19 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1547
1588
  throw this.#ownershipError("GET_RUN_OWNERSHIP", runId, error);
1548
1589
  }
1549
1590
  }
1591
+ /** Reads the records with single-key script calls the client pipelines, 500 at a time. */
1592
+ async getRunOwnerships({ runIds }) {
1593
+ try {
1594
+ return await getRunOwnerships(this.client, runIds);
1595
+ } catch (error) {
1596
+ throw new MastraError({
1597
+ id: createStorageErrorId("REDIS", "GET_RUN_OWNERSHIPS", "FAILED"),
1598
+ domain: ErrorDomain.STORAGE,
1599
+ category: ErrorCategory.THIRD_PARTY,
1600
+ details: { runCount: runIds.length }
1601
+ }, error);
1602
+ }
1603
+ }
1550
1604
  async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext, state, fence }) {
1551
1605
  const resolvedFence = resolveRunFence(this, fence, runId);
1552
1606
  try {
@@ -1735,7 +1789,11 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1735
1789
  }, error);
1736
1790
  }
1737
1791
  }
1738
- async listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId, status } = {}) {
1792
+ /**
1793
+ * Lists runs newest first. With `summary`, each snapshot keeps only its status, timestamp and
1794
+ * parent run; the full snapshots are still read from Redis.
1795
+ */
1796
+ async listWorkflowRuns({ workflowName, fromDate, toDate, perPage, page, resourceId, status, summary } = {}) {
1739
1797
  try {
1740
1798
  if (page !== void 0 && page < 0) throw new MastraError({
1741
1799
  id: createStorageErrorId("REDIS", "LIST_WORKFLOW_RUNS", "INVALID_PAGE"),
@@ -1790,7 +1848,7 @@ var WorkflowsRedis = class extends WorkflowsStorage {
1790
1848
  runs = runs.slice(offset, offset + normalizedPerPage);
1791
1849
  }
1792
1850
  return {
1793
- runs,
1851
+ runs: summary ? runs.map(summarizeWorkflowRun) : runs,
1794
1852
  total
1795
1853
  };
1796
1854
  } catch (error) {