@mastra/upstash 1.0.5 → 1.1.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,231 @@
1
1
  # @mastra/upstash
2
2
 
3
+ ## 1.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Update peer dependencies to match core package version bump (1.0.5) ([#12557](https://github.com/mastra-ai/mastra/pull/12557))
8
+
9
+ ### Patch Changes
10
+
11
+ - Add durable agents with resumable streams ([#12557](https://github.com/mastra-ai/mastra/pull/12557))
12
+
13
+ Durable agents make agent execution resilient to disconnections, crashes, and long-running operations.
14
+
15
+ ### The Problem
16
+
17
+ Standard agent streaming has two fragility points:
18
+ 1. **Connection drops** - If a client disconnects mid-stream (network blip, browser refresh, mobile app backgrounded), all subsequent events are lost. The client has no way to "catch up" on what they missed.
19
+ 2. **Long-running operations** - Agent loops with tool calls can take minutes. Holding an HTTP connection open that long is unreliable. If the server restarts or the connection times out, the work is lost.
20
+
21
+ ### The Solution
22
+
23
+ **Resumable streams** solve connection drops. Every event is cached with a sequential index. If a client disconnects at event 5, they can reconnect and request events starting from index 6. They receive cached events immediately, then continue with live events as they arrive.
24
+
25
+ **Durable execution** solves long-running operations. Instead of executing the agent loop directly in the HTTP request, execution happens in a workflow engine (built-in evented engine or Inngest). The HTTP request just subscribes to events. If the connection drops, execution continues. The client can reconnect anytime to observe progress.
26
+
27
+ ### Usage
28
+
29
+ Wrap any existing `Agent` with durability using factory functions:
30
+
31
+ ```typescript
32
+ import { Agent } from '@mastra/core/agent';
33
+ import { createDurableAgent } from '@mastra/core/agent/durable';
34
+
35
+ const agent = new Agent({
36
+ id: 'my-agent',
37
+ model: openai('gpt-4'),
38
+ instructions: 'You are helpful',
39
+ });
40
+
41
+ const durableAgent = createDurableAgent({ agent });
42
+ ```
43
+
44
+ **Factory functions for different execution strategies:**
45
+
46
+ | Factory | Execution | Use Case |
47
+ | ---------------------------------------- | ----------------------------------- | ------------------------------- |
48
+ | `createDurableAgent({ agent })` | Local, synchronous | Development, simple deployments |
49
+ | `createEventedAgent({ agent })` | Fire-and-forget via workflow engine | Long-running operations |
50
+ | `createInngestAgent({ agent, inngest })` | Inngest-powered | Production, distributed systems |
51
+
52
+ ### Resumable Streams
53
+
54
+ ```typescript
55
+ // Start streaming
56
+ const { runId, output } = await durableAgent.stream('Analyze this data...');
57
+
58
+ // Client disconnects at event 5...
59
+
60
+ // Reconnect and resume from where we left off
61
+ const { output: resumed } = await durableAgent.observe(runId, { offset: 6 });
62
+ // Receives events 6, 7, 8... from cache, then continues with live events
63
+ ```
64
+
65
+ ### PubSub and Cache
66
+
67
+ Durable agents use two infrastructure components:
68
+
69
+ | Component | Purpose | Default |
70
+ | ---------- | ----------------------------------------- | --------------------- |
71
+ | **PubSub** | Real-time event delivery during streaming | `EventEmitterPubSub` |
72
+ | **Cache** | Stores events for replay on reconnection | `InMemoryServerCache` |
73
+
74
+ When `stream()` is called, events flow through pubsub in real-time. The cache stores each event with a sequential index. When `observe()` is called, missed events replay from cache before continuing with live events.
75
+
76
+ **Configure via Mastra instance (recommended):**
77
+
78
+ ```typescript
79
+ const mastra = new Mastra({
80
+ cache: new RedisServerCache({ url: 'redis://...' }),
81
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
82
+ agents: {
83
+ // Inherits cache and pubsub from Mastra
84
+ myAgent: createDurableAgent({ agent }),
85
+ },
86
+ });
87
+ ```
88
+
89
+ **Configure per-agent (overrides Mastra):**
90
+
91
+ ```typescript
92
+ const durableAgent = createDurableAgent({
93
+ agent,
94
+ cache: new RedisServerCache({ url: 'redis://...' }),
95
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
96
+ });
97
+ ```
98
+
99
+ **Disable caching (streams won't be resumable):**
100
+
101
+ ```typescript
102
+ const durableAgent = createDurableAgent({ agent, cache: false });
103
+ ```
104
+
105
+ For single-instance deployments, the defaults work fine. For multi-instance deployments (load balancer, horizontal scaling), use Redis-backed implementations so any instance can serve reconnection requests.
106
+
107
+ ### Class Hierarchy
108
+ - `DurableAgent` extends `Agent` - base class with resumable streams
109
+ - `EventedAgent` extends `DurableAgent` - fire-and-forget execution
110
+ - `InngestAgent` extends `DurableAgent` - Inngest-powered execution
111
+
112
+ - Updated dependencies [[`920c757`](https://github.com/mastra-ai/mastra/commit/920c75799c6bd71787d86deaf654a35af4c839ca), [`d587199`](https://github.com/mastra-ai/mastra/commit/d5871993c0371bde2b0717d6b47194755baa1443), [`1fe2533`](https://github.com/mastra-ai/mastra/commit/1fe2533c4382ca6858aac7c4b63e888c2eac6541), [`f8694b6`](https://github.com/mastra-ai/mastra/commit/f8694b6fa0b7a5cde71d794c3bbef4957c55bcb8), [`f8694b6`](https://github.com/mastra-ai/mastra/commit/f8694b6fa0b7a5cde71d794c3bbef4957c55bcb8)]:
113
+ - @mastra/core@1.30.0
114
+ - @mastra/redis@1.1.0
115
+
116
+ ## 1.1.0-alpha.0
117
+
118
+ ### Minor Changes
119
+
120
+ - Update peer dependencies to match core package version bump (1.0.5) ([#12557](https://github.com/mastra-ai/mastra/pull/12557))
121
+
122
+ ### Patch Changes
123
+
124
+ - Add durable agents with resumable streams ([#12557](https://github.com/mastra-ai/mastra/pull/12557))
125
+
126
+ Durable agents make agent execution resilient to disconnections, crashes, and long-running operations.
127
+
128
+ ### The Problem
129
+
130
+ Standard agent streaming has two fragility points:
131
+ 1. **Connection drops** - If a client disconnects mid-stream (network blip, browser refresh, mobile app backgrounded), all subsequent events are lost. The client has no way to "catch up" on what they missed.
132
+ 2. **Long-running operations** - Agent loops with tool calls can take minutes. Holding an HTTP connection open that long is unreliable. If the server restarts or the connection times out, the work is lost.
133
+
134
+ ### The Solution
135
+
136
+ **Resumable streams** solve connection drops. Every event is cached with a sequential index. If a client disconnects at event 5, they can reconnect and request events starting from index 6. They receive cached events immediately, then continue with live events as they arrive.
137
+
138
+ **Durable execution** solves long-running operations. Instead of executing the agent loop directly in the HTTP request, execution happens in a workflow engine (built-in evented engine or Inngest). The HTTP request just subscribes to events. If the connection drops, execution continues. The client can reconnect anytime to observe progress.
139
+
140
+ ### Usage
141
+
142
+ Wrap any existing `Agent` with durability using factory functions:
143
+
144
+ ```typescript
145
+ import { Agent } from '@mastra/core/agent';
146
+ import { createDurableAgent } from '@mastra/core/agent/durable';
147
+
148
+ const agent = new Agent({
149
+ id: 'my-agent',
150
+ model: openai('gpt-4'),
151
+ instructions: 'You are helpful',
152
+ });
153
+
154
+ const durableAgent = createDurableAgent({ agent });
155
+ ```
156
+
157
+ **Factory functions for different execution strategies:**
158
+
159
+ | Factory | Execution | Use Case |
160
+ | ---------------------------------------- | ----------------------------------- | ------------------------------- |
161
+ | `createDurableAgent({ agent })` | Local, synchronous | Development, simple deployments |
162
+ | `createEventedAgent({ agent })` | Fire-and-forget via workflow engine | Long-running operations |
163
+ | `createInngestAgent({ agent, inngest })` | Inngest-powered | Production, distributed systems |
164
+
165
+ ### Resumable Streams
166
+
167
+ ```typescript
168
+ // Start streaming
169
+ const { runId, output } = await durableAgent.stream('Analyze this data...');
170
+
171
+ // Client disconnects at event 5...
172
+
173
+ // Reconnect and resume from where we left off
174
+ const { output: resumed } = await durableAgent.observe(runId, { offset: 6 });
175
+ // Receives events 6, 7, 8... from cache, then continues with live events
176
+ ```
177
+
178
+ ### PubSub and Cache
179
+
180
+ Durable agents use two infrastructure components:
181
+
182
+ | Component | Purpose | Default |
183
+ | ---------- | ----------------------------------------- | --------------------- |
184
+ | **PubSub** | Real-time event delivery during streaming | `EventEmitterPubSub` |
185
+ | **Cache** | Stores events for replay on reconnection | `InMemoryServerCache` |
186
+
187
+ When `stream()` is called, events flow through pubsub in real-time. The cache stores each event with a sequential index. When `observe()` is called, missed events replay from cache before continuing with live events.
188
+
189
+ **Configure via Mastra instance (recommended):**
190
+
191
+ ```typescript
192
+ const mastra = new Mastra({
193
+ cache: new RedisServerCache({ url: 'redis://...' }),
194
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
195
+ agents: {
196
+ // Inherits cache and pubsub from Mastra
197
+ myAgent: createDurableAgent({ agent }),
198
+ },
199
+ });
200
+ ```
201
+
202
+ **Configure per-agent (overrides Mastra):**
203
+
204
+ ```typescript
205
+ const durableAgent = createDurableAgent({
206
+ agent,
207
+ cache: new RedisServerCache({ url: 'redis://...' }),
208
+ pubsub: new RedisPubSub({ url: 'redis://...' }),
209
+ });
210
+ ```
211
+
212
+ **Disable caching (streams won't be resumable):**
213
+
214
+ ```typescript
215
+ const durableAgent = createDurableAgent({ agent, cache: false });
216
+ ```
217
+
218
+ For single-instance deployments, the defaults work fine. For multi-instance deployments (load balancer, horizontal scaling), use Redis-backed implementations so any instance can serve reconnection requests.
219
+
220
+ ### Class Hierarchy
221
+ - `DurableAgent` extends `Agent` - base class with resumable streams
222
+ - `EventedAgent` extends `DurableAgent` - fire-and-forget execution
223
+ - `InngestAgent` extends `DurableAgent` - Inngest-powered execution
224
+
225
+ - Updated dependencies [[`920c757`](https://github.com/mastra-ai/mastra/commit/920c75799c6bd71787d86deaf654a35af4c839ca), [`1fe2533`](https://github.com/mastra-ai/mastra/commit/1fe2533c4382ca6858aac7c4b63e888c2eac6541), [`f8694b6`](https://github.com/mastra-ai/mastra/commit/f8694b6fa0b7a5cde71d794c3bbef4957c55bcb8), [`f8694b6`](https://github.com/mastra-ai/mastra/commit/f8694b6fa0b7a5cde71d794c3bbef4957c55bcb8)]:
226
+ - @mastra/core@1.30.0-alpha.1
227
+ - @mastra/redis@1.1.0-alpha.0
228
+
3
229
  ## 1.0.5
4
230
 
5
231
  ### Patch Changes
@@ -0,0 +1,60 @@
1
+ import { RedisServerCache } from '@mastra/redis';
2
+ import { Redis } from '@upstash/redis';
3
+ /**
4
+ * Configuration for UpstashServerCache.
5
+ * Accepts either:
6
+ * 1. An existing Redis client from @upstash/redis
7
+ * 2. URL and token to create a client (requires @upstash/redis to be installed)
8
+ */
9
+ export type UpstashCacheConfig = {
10
+ client: Redis;
11
+ } | {
12
+ url: string;
13
+ token: string;
14
+ };
15
+ /**
16
+ * Options for UpstashServerCache
17
+ */
18
+ export interface UpstashServerCacheOptions {
19
+ /**
20
+ * Optional key prefix to namespace all cache keys.
21
+ * Defaults to 'mastra:cache:'.
22
+ */
23
+ keyPrefix?: string;
24
+ /**
25
+ * Default TTL in seconds for cached items.
26
+ * Defaults to 300 (5 minutes).
27
+ * Set to 0 to disable TTL (items persist until explicitly deleted).
28
+ */
29
+ ttlSeconds?: number;
30
+ }
31
+ /**
32
+ * Upstash Redis implementation of MastraServerCache.
33
+ *
34
+ * This is a convenience wrapper around RedisServerCache from @mastra/redis
35
+ * with the upstash preset pre-configured.
36
+ *
37
+ * @example With existing client
38
+ * ```typescript
39
+ * import { Redis } from '@upstash/redis';
40
+ * import { UpstashServerCache } from '@mastra/upstash';
41
+ *
42
+ * const redis = new Redis({ url: '...', token: '...' });
43
+ * const cache = new UpstashServerCache({ client: redis });
44
+ * ```
45
+ *
46
+ * @example With URL and token
47
+ * ```typescript
48
+ * import { UpstashServerCache } from '@mastra/upstash';
49
+ *
50
+ * const cache = new UpstashServerCache({
51
+ * url: process.env.UPSTASH_REDIS_REST_URL!,
52
+ * token: process.env.UPSTASH_REDIS_REST_TOKEN!,
53
+ * });
54
+ * ```
55
+ */
56
+ export declare class UpstashServerCache extends RedisServerCache {
57
+ constructor(config: UpstashCacheConfig, options?: UpstashServerCacheOptions);
58
+ }
59
+ export { RedisServerCache, upstashPreset, type RedisClient, type RedisServerCacheOptions } from '@mastra/redis';
60
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cache/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAiB,MAAM,eAAe,CAAC;AAEhE,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAEvC;;;;;GAKG;AACH,MAAM,MAAM,kBAAkB,GAAG;IAAE,MAAM,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAEpF;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACxC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAC;IAEnB;;;;OAIG;IACH,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,qBAAa,kBAAmB,SAAQ,gBAAgB;gBAC1C,MAAM,EAAE,kBAAkB,EAAE,OAAO,GAAE,yBAA8B;CAiBhF;AAGD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,KAAK,WAAW,EAAE,KAAK,uBAAuB,EAAE,MAAM,eAAe,CAAC"}
@@ -3,7 +3,7 @@ name: mastra-upstash
3
3
  description: Documentation for @mastra/upstash. Use when working with @mastra/upstash APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/upstash"
6
- version: "1.0.5"
6
+ version: "1.1.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,6 +1,15 @@
1
1
  {
2
- "version": "1.0.5",
2
+ "version": "1.1.0",
3
3
  "package": "@mastra/upstash",
4
- "exports": {},
4
+ "exports": {
5
+ "RedisServerCache": {
6
+ "types": "dist/index.d.ts",
7
+ "implementation": "dist/redis"
8
+ },
9
+ "upstashPreset": {
10
+ "types": "dist/index.d.ts",
11
+ "implementation": "dist/redis"
12
+ }
13
+ },
5
14
  "modules": {}
6
15
  }
@@ -208,7 +208,7 @@ const paragraphMemory = new Memory({
208
208
 
209
209
  ## Structured working memory
210
210
 
211
- Working memory can also be defined using a structured schema instead of a Markdown template. This allows you to specify the exact fields and types that should be tracked, using a [Zod](https://zod.dev/) schema. When using a schema, the agent will see and update working memory as a JSON object matching your schema.
211
+ Working memory can also be defined using a structured schema instead of a Markdown template. This allows you to specify the exact fields and types that should be tracked, using a [Standard JSON Schema](https://standardschema.dev/json-schema) ([Zod](https://zod.dev/), [Valibot](https://valibot.dev/), [ArkType](https://arktype.io/), etc.). When using a schema, the agent will see and update working memory as a JSON object matching your schema.
212
212
 
213
213
  **Important:** You must specify either `template` or `schema`, but not both.
214
214
 
package/dist/index.cjs CHANGED
@@ -1,7 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ var redis = require('@mastra/redis');
4
+ var redis$1 = require('@upstash/redis');
3
5
  var storage = require('@mastra/core/storage');
4
- var redis = require('@upstash/redis');
5
6
  var error = require('@mastra/core/error');
6
7
  var agent = require('@mastra/core/agent');
7
8
  var evals = require('@mastra/core/evals');
@@ -10,7 +11,23 @@ var vector = require('@mastra/core/vector');
10
11
  var vector$1 = require('@upstash/vector');
11
12
  var filter = require('@mastra/core/vector/filter');
12
13
 
13
- // src/storage/index.ts
14
+ // src/cache/index.ts
15
+ var UpstashServerCache = class extends redis.RedisServerCache {
16
+ constructor(config, options = {}) {
17
+ let client;
18
+ if ("client" in config) {
19
+ client = config.client;
20
+ } else {
21
+ client = new redis$1.Redis({ url: config.url, token: config.token });
22
+ }
23
+ const redisOptions = {
24
+ ...redis.upstashPreset,
25
+ keyPrefix: options.keyPrefix,
26
+ ttlSeconds: options.ttlSeconds
27
+ };
28
+ super({ client }, redisOptions);
29
+ }
30
+ };
14
31
  function getKey(tableName, keys) {
15
32
  const keyParts = Object.entries(keys).filter(([_, value]) => value !== void 0).map(([key, value]) => `${key}:${value}`);
16
33
  return `${tableName}:${keyParts.join(":")}`;
@@ -44,7 +61,7 @@ function resolveUpstashConfig(config) {
44
61
  if ("client" in config) {
45
62
  return config.client;
46
63
  }
47
- return new redis.Redis({
64
+ return new redis$1.Redis({
48
65
  url: config.url,
49
66
  token: config.token
50
67
  });
@@ -2153,7 +2170,7 @@ var UpstashStore = class extends storage.MastraCompositeStore {
2153
2170
  if (!config.token || typeof config.token !== "string" || config.token.trim() === "") {
2154
2171
  throw new Error("UpstashStore: token is required and cannot be empty.");
2155
2172
  }
2156
- this.redis = new redis.Redis({
2173
+ this.redis = new redis$1.Redis({
2157
2174
  url: config.url,
2158
2175
  token: config.token
2159
2176
  });
@@ -2872,10 +2889,19 @@ Example Complex Query:
2872
2889
  ]
2873
2890
  }`;
2874
2891
 
2892
+ Object.defineProperty(exports, "RedisServerCache", {
2893
+ enumerable: true,
2894
+ get: function () { return redis.RedisServerCache; }
2895
+ });
2896
+ Object.defineProperty(exports, "upstashPreset", {
2897
+ enumerable: true,
2898
+ get: function () { return redis.upstashPreset; }
2899
+ });
2875
2900
  exports.BackgroundTasksUpstash = BackgroundTasksUpstash;
2876
2901
  exports.ScoresUpstash = ScoresUpstash;
2877
2902
  exports.StoreMemoryUpstash = StoreMemoryUpstash;
2878
2903
  exports.UPSTASH_PROMPT = UPSTASH_PROMPT;
2904
+ exports.UpstashServerCache = UpstashServerCache;
2879
2905
  exports.UpstashStore = UpstashStore;
2880
2906
  exports.UpstashVector = UpstashVector;
2881
2907
  exports.WorkflowsUpstash = WorkflowsUpstash;