@mastra/upstash 1.2.1-alpha.0 → 1.2.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
@@ -1764,6 +1764,36 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1764
1764
  async dangerouslyClearAll() {
1765
1765
  await this.#db.deleteData({ tableName: TABLE_WORKFLOW_SNAPSHOT });
1766
1766
  }
1767
+ async getWorkflowRunRecord({
1768
+ namespace,
1769
+ runId,
1770
+ resourceId,
1771
+ workflowName
1772
+ }) {
1773
+ if (workflowName) {
1774
+ const keyVariants = [
1775
+ ...resourceId ? [{ namespace, workflow_name: workflowName, run_id: runId, resourceId }] : [],
1776
+ { namespace, workflow_name: workflowName, run_id: runId }
1777
+ ];
1778
+ for (const keys2 of keyVariants) {
1779
+ const data = await this.#db.get({
1780
+ tableName: TABLE_WORKFLOW_SNAPSHOT,
1781
+ keys: keys2
1782
+ });
1783
+ if (data) return data;
1784
+ }
1785
+ }
1786
+ const key = `${getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace })}:*run_id:${runId}*`;
1787
+ const keys = await this.#db.scanKeys(key);
1788
+ const workflows = await Promise.all(
1789
+ keys.map(async (key2) => {
1790
+ return this.client.get(key2);
1791
+ })
1792
+ );
1793
+ return workflows.find(
1794
+ (w) => w?.run_id === runId && (!workflowName || w.workflow_name === workflowName) && (!resourceId || w.resourceId === resourceId)
1795
+ ) ?? null;
1796
+ }
1767
1797
  async updateWorkflowResults({
1768
1798
  workflowName,
1769
1799
  runId,
@@ -1967,18 +1997,38 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1967
1997
  async persistWorkflowSnapshot(params) {
1968
1998
  const { namespace = "workflows", workflowName, runId, resourceId, snapshot, createdAt, updatedAt } = params;
1969
1999
  try {
1970
- await this.#db.insert({
1971
- tableName: TABLE_WORKFLOW_SNAPSHOT,
1972
- record: {
1973
- namespace,
1974
- workflow_name: workflowName,
1975
- run_id: runId,
1976
- resourceId,
1977
- snapshot,
1978
- createdAt: createdAt ?? /* @__PURE__ */ new Date(),
1979
- updatedAt: updatedAt ?? /* @__PURE__ */ new Date()
1980
- }
2000
+ const now = /* @__PURE__ */ new Date();
2001
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, {
2002
+ namespace,
2003
+ workflow_name: workflowName,
2004
+ run_id: runId,
2005
+ ...resourceId ? { resourceId } : {}
1981
2006
  });
2007
+ const record = {
2008
+ namespace,
2009
+ workflow_name: workflowName,
2010
+ run_id: runId,
2011
+ resourceId,
2012
+ snapshot,
2013
+ createdAt: (createdAt ?? now).toISOString(),
2014
+ updatedAt: (updatedAt ?? now).toISOString()
2015
+ };
2016
+ await this.client.eval(
2017
+ `
2018
+ local existing = redis.call("GET", KEYS[1])
2019
+ local next = cjson.decode(ARGV[1])
2020
+ if existing then
2021
+ local current = cjson.decode(existing)
2022
+ if current["createdAt"] then
2023
+ next["createdAt"] = current["createdAt"]
2024
+ end
2025
+ end
2026
+ redis.call("SET", KEYS[1], cjson.encode(next))
2027
+ return 1
2028
+ `,
2029
+ [key],
2030
+ [JSON.stringify(record)]
2031
+ );
1982
2032
  } catch (error) {
1983
2033
  throw new MastraError(
1984
2034
  {
@@ -1997,15 +2047,10 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
1997
2047
  }
1998
2048
  async loadWorkflowSnapshot(params) {
1999
2049
  const { namespace = "workflows", workflowName, runId } = params;
2000
- const key = getKey(TABLE_WORKFLOW_SNAPSHOT, {
2001
- namespace,
2002
- workflow_name: workflowName,
2003
- run_id: runId
2004
- });
2005
2050
  try {
2006
- const data = await this.client.get(key);
2051
+ const data = await this.getWorkflowRunRecord({ namespace, runId, workflowName });
2007
2052
  if (!data) return null;
2008
- return data.snapshot;
2053
+ return typeof data.snapshot === "string" ? JSON.parse(data.snapshot) : data.snapshot;
2009
2054
  } catch (error) {
2010
2055
  throw new MastraError(
2011
2056
  {
@@ -2027,15 +2072,7 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
2027
2072
  workflowName
2028
2073
  }) {
2029
2074
  try {
2030
- const key = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName, run_id: runId }) + "*";
2031
- const keys = await this.#db.scanKeys(key);
2032
- const workflows = await Promise.all(
2033
- keys.map(async (key2) => {
2034
- const data2 = await this.client.get(key2);
2035
- return data2;
2036
- })
2037
- );
2038
- const data = workflows.find((w) => w?.run_id === runId && w?.workflow_name === workflowName);
2075
+ const data = await this.getWorkflowRunRecord({ namespace: "workflows", runId, workflowName });
2039
2076
  if (!data) return null;
2040
2077
  return this.parseWorkflowRun(data);
2041
2078
  } catch (error) {
@@ -2055,8 +2092,14 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
2055
2092
  }
2056
2093
  }
2057
2094
  async deleteWorkflowRunById({ runId, workflowName }) {
2058
- const key = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName, run_id: runId });
2059
2095
  try {
2096
+ const record = await this.getWorkflowRunRecord({ namespace: "workflows", runId, workflowName });
2097
+ const key = getKey(TABLE_WORKFLOW_SNAPSHOT, {
2098
+ namespace: "workflows",
2099
+ workflow_name: workflowName,
2100
+ run_id: runId,
2101
+ ...record?.resourceId ? { resourceId: record.resourceId } : {}
2102
+ });
2060
2103
  await this.client.del(key);
2061
2104
  } catch (error) {
2062
2105
  throw new MastraError(
@@ -2095,24 +2138,7 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
2095
2138
  new Error("page must be >= 0")
2096
2139
  );
2097
2140
  }
2098
- let pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows" }) + ":*";
2099
- if (workflowName && resourceId) {
2100
- pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
2101
- namespace: "workflows",
2102
- workflow_name: workflowName,
2103
- run_id: "*",
2104
- resourceId
2105
- });
2106
- } else if (workflowName) {
2107
- pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows", workflow_name: workflowName }) + ":*";
2108
- } else if (resourceId) {
2109
- pattern = getKey(TABLE_WORKFLOW_SNAPSHOT, {
2110
- namespace: "workflows",
2111
- workflow_name: "*",
2112
- run_id: "*",
2113
- resourceId
2114
- });
2115
- }
2141
+ const pattern = `${getKey(TABLE_WORKFLOW_SNAPSHOT, { namespace: "workflows" })}:*`;
2116
2142
  const keys = await this.#db.scanKeys(pattern);
2117
2143
  if (keys.length === 0) {
2118
2144
  return { runs: [], total: 0 };
@@ -2122,7 +2148,7 @@ var WorkflowsUpstash = class extends WorkflowsStorage {
2122
2148
  const results = await pipeline.exec();
2123
2149
  let runs = results.map((result) => result).filter(
2124
2150
  (record) => record !== null && record !== void 0 && typeof record === "object" && "workflow_name" in record
2125
- ).filter((record) => !workflowName || record.workflow_name === workflowName).map((w) => this.parseWorkflowRun(w)).filter((w) => {
2151
+ ).filter((record) => !workflowName || record.workflow_name === workflowName).filter((record) => !resourceId || record.resourceId === resourceId).map((w) => this.parseWorkflowRun(w)).filter((w) => {
2126
2152
  if (fromDate && w.createdAt < fromDate) return false;
2127
2153
  if (toDate && w.createdAt > toDate) return false;
2128
2154
  if (status) {