@mastra/mcp-docs-server 1.1.42 → 1.1.43-alpha.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/.docs/docs/agents/acp.md +21 -158
- package/.docs/docs/agents/processors.md +143 -0
- package/.docs/docs/agents/signals.md +229 -9
- package/.docs/docs/build-with-ai/skills.md +3 -3
- package/.docs/docs/editor/overview.md +11 -10
- package/.docs/docs/getting-started/build-with-ai.md +37 -0
- package/.docs/docs/mastra-platform/observability.md +4 -4
- package/.docs/docs/memory/multi-user-threads.md +1 -1
- package/.docs/docs/memory/working-memory.md +27 -0
- package/.docs/docs/observability/metrics/querying.md +1 -1
- package/.docs/docs/server/pubsub.md +124 -0
- package/.docs/docs/studio/auth.md +20 -0
- package/.docs/reference/acp/acp-agent.md +228 -0
- package/.docs/reference/acp/create-acp-tool.md +131 -0
- package/.docs/reference/agents/agent.md +51 -1
- package/.docs/reference/agents/durable-agent.md +239 -0
- package/.docs/reference/cli/mastra.md +5 -5
- package/.docs/reference/client-js/agents.md +41 -7
- package/.docs/reference/index.md +9 -0
- package/.docs/reference/processors/response-cache.md +2 -2
- package/.docs/reference/pubsub/base.md +168 -0
- package/.docs/reference/pubsub/caching-pubsub.md +102 -0
- package/.docs/reference/pubsub/event-emitter.md +72 -0
- package/.docs/reference/pubsub/google-cloud-pubsub.md +94 -0
- package/.docs/reference/pubsub/redis-streams.md +108 -0
- package/.docs/reference/pubsub/unix-socket-pubsub.md +52 -0
- package/.docs/reference/storage/libsql.md +6 -0
- package/.docs/reference/storage/mongodb.md +3 -0
- package/.docs/reference/storage/postgresql.md +3 -0
- package/CHANGELOG.md +8 -0
- package/package.json +5 -5
- package/.docs/docs/agents/response-caching.md +0 -150
|
@@ -603,10 +603,10 @@ It accepts [common flags](#common-flags).
|
|
|
603
603
|
Calls a Mastra runtime server with JSON input and JSON output. Use it for local development servers, deployed Mastra platform projects, self-hosted Mastra servers, or hosted Mastra Platform Observability APIs.
|
|
604
604
|
|
|
605
605
|
```bash
|
|
606
|
-
mastra api agent list
|
|
606
|
+
mastra api agent list
|
|
607
607
|
mastra api agent run weather-agent '{"messages":"What is the weather in London?"}'
|
|
608
608
|
mastra api tool execute get-weather '{"location":"San Francisco"}'
|
|
609
|
-
mastra api trace list '{"page":0,"perPage":20}'
|
|
609
|
+
mastra api trace list '{"page":0,"perPage":20}'
|
|
610
610
|
```
|
|
611
611
|
|
|
612
612
|
Use `mastra api <resource> <action> --help` to see examples for a command.
|
|
@@ -717,7 +717,7 @@ mastra api trace list '{"page":0,"perPage":20}'
|
|
|
717
717
|
Routes that support filters accept them in the same JSON input. For example, observability trace listing supports pagination and route-supported filters:
|
|
718
718
|
|
|
719
719
|
```bash
|
|
720
|
-
mastra api trace list '{"page":0,"perPage":20,"filters":{"spanType":"agent"}}'
|
|
720
|
+
mastra api trace list '{"page":0,"perPage":20,"filters":{"spanType":"agent"}}'
|
|
721
721
|
```
|
|
722
722
|
|
|
723
723
|
### Get command-specific help
|
|
@@ -973,8 +973,8 @@ Lists observability traces. Pass optional JSON input for route-supported filters
|
|
|
973
973
|
|
|
974
974
|
```bash
|
|
975
975
|
mastra api trace list [input]
|
|
976
|
-
mastra api trace list '{"page":0,"perPage":20}'
|
|
977
|
-
mastra api trace list '{"page":0,"perPage":20}' --verbose
|
|
976
|
+
mastra api trace list '{"page":0,"perPage":20}'
|
|
977
|
+
mastra api trace list '{"page":0,"perPage":20}' --verbose
|
|
978
978
|
```
|
|
979
979
|
|
|
980
980
|
`trace list` returns lightweight root span records by default so you can page through traces without fetching large input, output, attributes, or metadata payloads. Pass `--verbose` to fetch the full root span records.
|
|
@@ -151,17 +151,51 @@ for await (const part of uiMessageStream) {
|
|
|
151
151
|
}
|
|
152
152
|
```
|
|
153
153
|
|
|
154
|
+
### `sendMessage()`
|
|
155
|
+
|
|
156
|
+
Send user-authored input to an active agent run or idle memory thread. Use this with `subscribeToThread()` so the client can render the stream that wakes from, or receives, the message.
|
|
157
|
+
|
|
158
|
+
```typescript
|
|
159
|
+
const agent = mastraClient.getAgent('support-agent')
|
|
160
|
+
|
|
161
|
+
const result = await agent.sendMessage({
|
|
162
|
+
message: {
|
|
163
|
+
contents: 'Also consider the customer note I just added.',
|
|
164
|
+
attributes: { sentFrom: 'web' },
|
|
165
|
+
},
|
|
166
|
+
resourceId: 'user-123',
|
|
167
|
+
threadId: 'thread-abc',
|
|
168
|
+
})
|
|
169
|
+
|
|
170
|
+
console.log(result.runId)
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
`message` accepts a string, an array of text/file parts, or an object with `contents`, `attributes`, `metadata`, and `providerOptions`.
|
|
174
|
+
|
|
175
|
+
### `queueMessage()`
|
|
176
|
+
|
|
177
|
+
Queue user-authored input for the next thread turn. If the thread is active, Mastra starts a new run after the current run completes. If the thread is idle, Mastra starts a run immediately.
|
|
178
|
+
|
|
179
|
+
```typescript
|
|
180
|
+
await agent.queueMessage({
|
|
181
|
+
message: 'Also check whether the tests need updates.',
|
|
182
|
+
resourceId: 'user-123',
|
|
183
|
+
threadId: 'thread-abc',
|
|
184
|
+
})
|
|
185
|
+
```
|
|
186
|
+
|
|
154
187
|
### `sendSignal()`
|
|
155
188
|
|
|
156
|
-
Send a signal to an active agent run or memory thread. Use this
|
|
189
|
+
Send a lower-level signal to an active agent run or memory thread. Use this for system-generated context such as reactive reminders or notification-shaped context that doesn't need inbox storage. For durable notification records, use the server-side [`Agent.sendNotificationSignal()`](https://mastra.ai/reference/agents/agent) API. For user-authored input, prefer `sendMessage()` or `queueMessage()`.
|
|
157
190
|
|
|
158
191
|
```typescript
|
|
159
192
|
const agent = mastraClient.getAgent('support-agent')
|
|
160
193
|
|
|
161
194
|
const result = await agent.sendSignal({
|
|
162
195
|
signal: {
|
|
163
|
-
type: '
|
|
164
|
-
|
|
196
|
+
type: 'reactive',
|
|
197
|
+
tagName: 'system-reminder',
|
|
198
|
+
contents: 'Also consider the latest customer note.',
|
|
165
199
|
},
|
|
166
200
|
resourceId: 'user-123',
|
|
167
201
|
threadId: 'thread-abc',
|
|
@@ -174,7 +208,7 @@ Use `ifActive.behavior` and `ifIdle.behavior` to control whether Mastra delivers
|
|
|
174
208
|
|
|
175
209
|
```typescript
|
|
176
210
|
await agent.sendSignal({
|
|
177
|
-
signal: { type: '
|
|
211
|
+
signal: { type: 'reactive', tagName: 'system-reminder', contents: 'Store this for later.' },
|
|
178
212
|
resourceId: 'user-123',
|
|
179
213
|
threadId: 'thread-abc',
|
|
180
214
|
ifIdle: {
|
|
@@ -187,7 +221,7 @@ Pass `ifIdle.streamOptions` when the idle wake-up stream needs options such as m
|
|
|
187
221
|
|
|
188
222
|
```typescript
|
|
189
223
|
await agent.sendSignal({
|
|
190
|
-
signal: { type: '
|
|
224
|
+
signal: { type: 'reactive', tagName: 'system-reminder', contents: 'Start from this signal.' },
|
|
191
225
|
resourceId: 'user-123',
|
|
192
226
|
threadId: 'thread-abc',
|
|
193
227
|
ifIdle: {
|
|
@@ -201,7 +235,7 @@ await agent.sendSignal({
|
|
|
201
235
|
|
|
202
236
|
Returns `{ accepted: true, runId: string }`.
|
|
203
237
|
|
|
204
|
-
**signal** (`{ type: 'user
|
|
238
|
+
**signal** (`{ type: 'user' | 'reactive' | 'notification' | string; tagName?: string; contents: string | Array<TextPart | FilePart>; attributes?: Record<string, JSONValue>; metadata?: Record<string, unknown>; providerOptions?: ProviderMetadata }`): Lower-level signal payload. Use \`type\` for the semantic signal category and \`tagName\` for the XML tag shown to the model. \`providerOptions\` is attached to the resulting prompt turn and persisted on the stored signal message.
|
|
205
239
|
|
|
206
240
|
**runId** (`string`): Run ID to target directly.
|
|
207
241
|
|
|
@@ -217,7 +251,7 @@ Returns `{ accepted: true, runId: string }`.
|
|
|
217
251
|
|
|
218
252
|
### `subscribeToThread()`
|
|
219
253
|
|
|
220
|
-
Subscribe to raw stream chunks for a memory thread. Use this to render output from a thread that may be started or continued by `sendSignal()
|
|
254
|
+
Subscribe to raw stream chunks for a memory thread. Use this to render output from a thread that may be started or continued by `sendMessage()`, `queueMessage()`, `sendSignal()`, or server-side notification dispatch.
|
|
221
255
|
|
|
222
256
|
```typescript
|
|
223
257
|
const agent = mastraClient.getAgent('support-agent')
|
package/.docs/reference/index.md
CHANGED
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
The Reference section provides documentation of Mastra's API, including parameters, types and usage examples.
|
|
4
4
|
|
|
5
|
+
- [AcpAgent](https://mastra.ai/reference/acp/acp-agent)
|
|
6
|
+
- [createACPTool()](https://mastra.ai/reference/acp/create-acp-tool)
|
|
5
7
|
- [Agent Class](https://mastra.ai/reference/agents/agent)
|
|
6
8
|
- [Channels](https://mastra.ai/reference/agents/channels)
|
|
9
|
+
- [DurableAgent](https://mastra.ai/reference/agents/durable-agent)
|
|
7
10
|
- [.generate()](https://mastra.ai/reference/agents/generate)
|
|
8
11
|
- [.generateLegacy()](https://mastra.ai/reference/agents/generateLegacy)
|
|
9
12
|
- [.getDefaultGenerateOptionsLegacy()](https://mastra.ai/reference/agents/getDefaultGenerateOptions)
|
|
@@ -193,6 +196,12 @@ The Reference section provides documentation of Mastra's API, including paramete
|
|
|
193
196
|
- [ToolSearchProcessor](https://mastra.ai/reference/processors/tool-search-processor)
|
|
194
197
|
- [UnicodeNormalizer](https://mastra.ai/reference/processors/unicode-normalizer)
|
|
195
198
|
- [WorkingMemory](https://mastra.ai/reference/processors/working-memory-processor)
|
|
199
|
+
- [CachingPubSub](https://mastra.ai/reference/pubsub/caching-pubsub)
|
|
200
|
+
- [EventEmitterPubSub](https://mastra.ai/reference/pubsub/event-emitter)
|
|
201
|
+
- [GoogleCloudPubSub](https://mastra.ai/reference/pubsub/google-cloud-pubsub)
|
|
202
|
+
- [PubSub](https://mastra.ai/reference/pubsub/base)
|
|
203
|
+
- [RedisStreamsPubSub](https://mastra.ai/reference/pubsub/redis-streams)
|
|
204
|
+
- [UnixSocketPubSub](https://mastra.ai/reference/pubsub/unix-socket-pubsub)
|
|
196
205
|
- [DatabaseConfig](https://mastra.ai/reference/rag/database-config)
|
|
197
206
|
- [Embed](https://mastra.ai/reference/rag/embeddings)
|
|
198
207
|
- [ExtractParams](https://mastra.ai/reference/rag/extract-params)
|
|
@@ -34,7 +34,7 @@ await agent.generate('What is the capital of France?', {
|
|
|
34
34
|
})
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
See [Response caching](https://mastra.ai/docs/agents/
|
|
37
|
+
See [Response caching](https://mastra.ai/docs/agents/processors) for the conceptual overview, scoping rules, and recommended deployment patterns.
|
|
38
38
|
|
|
39
39
|
## Constructor parameters
|
|
40
40
|
|
|
@@ -109,6 +109,6 @@ The argument passed to a `key` function (constructor or per-call). All fields co
|
|
|
109
109
|
|
|
110
110
|
## Related
|
|
111
111
|
|
|
112
|
-
- [Response caching](https://mastra.ai/docs/agents/
|
|
112
|
+
- [Response caching](https://mastra.ai/docs/agents/processors)
|
|
113
113
|
- [Processors](https://mastra.ai/docs/agents/processors)
|
|
114
114
|
- [Processor interface](https://mastra.ai/reference/processors/processor-interface)
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# PubSub
|
|
2
|
+
|
|
3
|
+
`PubSub` is the abstract base class for Mastra's event system. It defines the contract that every pub/sub backend implements, so the rest of Mastra can publish and subscribe to events without knowing which transport is in use.
|
|
4
|
+
|
|
5
|
+
Mastra uses pub/sub internally for workflow event processing, streaming, and cross-component communication. Most applications use the default [`EventEmitterPubSub`](https://mastra.ai/reference/pubsub/event-emitter) and never construct a `PubSub` directly. Implement this class only when you need a custom transport.
|
|
6
|
+
|
|
7
|
+
For built-in implementations, see [`EventEmitterPubSub`](https://mastra.ai/reference/pubsub/event-emitter), [`UnixSocketPubSub`](https://mastra.ai/reference/pubsub/unix-socket-pubsub), [`CachingPubSub`](https://mastra.ai/reference/pubsub/caching-pubsub), [`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams), and [`GoogleCloudPubSub`](https://mastra.ai/reference/pubsub/google-cloud-pubsub).
|
|
8
|
+
|
|
9
|
+
## Usage example
|
|
10
|
+
|
|
11
|
+
Extend `PubSub` and implement the four abstract methods to add a custom backend.
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { PubSub } from '@mastra/core/events'
|
|
15
|
+
import type { Event, EventCallback, SubscribeOptions } from '@mastra/core/events'
|
|
16
|
+
|
|
17
|
+
export class CustomPubSub extends PubSub {
|
|
18
|
+
async publish(topic: string, event: Omit<Event, 'id' | 'createdAt'>): Promise<void> {
|
|
19
|
+
// Deliver the event to subscribers of `topic`.
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async subscribe(topic: string, cb: EventCallback, options?: SubscribeOptions): Promise<void> {
|
|
23
|
+
// Register `cb` to receive events published to `topic`.
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async unsubscribe(topic: string, cb: EventCallback): Promise<void> {
|
|
27
|
+
// Remove a previously registered callback.
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async flush(): Promise<void> {
|
|
31
|
+
// Wait for any in-flight deliveries to settle.
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Pass the instance to the [Mastra](https://mastra.ai/reference/core/mastra-class) constructor:
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { Mastra } from '@mastra/core'
|
|
40
|
+
import { CustomPubSub } from './pubsub'
|
|
41
|
+
|
|
42
|
+
export const mastra = new Mastra({
|
|
43
|
+
pubsub: new CustomPubSub(),
|
|
44
|
+
})
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Delivery modes
|
|
48
|
+
|
|
49
|
+
A `PubSub` declares which delivery modes it supports through the `supportedModes` property. Mastra reads this to decide whether to run a long-lived worker that pulls events.
|
|
50
|
+
|
|
51
|
+
| Mode | Description |
|
|
52
|
+
| ------ | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
53
|
+
| `pull` | Consumers actively read from the broker, for example Redis Streams `XREADGROUP`. Mastra runs an orchestration worker to read. |
|
|
54
|
+
| `push` | Events arrive without the consumer asking, either in-process or through an HTTP endpoint. No read loop is required. |
|
|
55
|
+
|
|
56
|
+
The default is `['pull']` so that custom implementations keep today's behavior unless they opt in to push delivery.
|
|
57
|
+
|
|
58
|
+
## Methods
|
|
59
|
+
|
|
60
|
+
### Core methods
|
|
61
|
+
|
|
62
|
+
#### `publish(topic, event)`
|
|
63
|
+
|
|
64
|
+
Publishes an event to a topic. The `id` and `createdAt` fields are assigned by the implementation.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
await pubsub.publish('my-topic', {
|
|
68
|
+
type: 'example',
|
|
69
|
+
data: { value: 1 },
|
|
70
|
+
runId: 'run-123',
|
|
71
|
+
})
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
#### `subscribe(topic, cb, options?)`
|
|
75
|
+
|
|
76
|
+
Registers a callback to receive events published to a topic. When `options.group` is set, subscribers in the same group compete for messages and each event is delivered to one member. Without a group, every subscriber receives every event.
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
await pubsub.subscribe('my-topic', (event, ack, nack) => {
|
|
80
|
+
console.log(event)
|
|
81
|
+
})
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### `unsubscribe(topic, cb)`
|
|
85
|
+
|
|
86
|
+
Removes a previously registered callback from a topic.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
await pubsub.unsubscribe('my-topic', callback)
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
#### `flush()`
|
|
93
|
+
|
|
94
|
+
Waits for any in-flight deliveries to settle. Call this before shutdown to avoid dropping events.
|
|
95
|
+
|
|
96
|
+
```typescript
|
|
97
|
+
await pubsub.flush()
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
### Replay methods
|
|
101
|
+
|
|
102
|
+
These methods support resuming a stream after a disconnect. The default implementations fall back to a regular `subscribe`, so backends without history support behave as live-only. [`CachingPubSub`](https://mastra.ai/reference/pubsub/caching-pubsub) overrides them to replay cached events.
|
|
103
|
+
|
|
104
|
+
#### `getHistory(topic, offset?)`
|
|
105
|
+
|
|
106
|
+
Returns cached events for a topic, starting at `offset`. Returns an empty array when the backend has no history.
|
|
107
|
+
|
|
108
|
+
```typescript
|
|
109
|
+
const events = await pubsub.getHistory('my-topic', 0)
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Returns: `Promise<Event[]>`
|
|
113
|
+
|
|
114
|
+
#### `subscribeWithReplay(topic, cb)`
|
|
115
|
+
|
|
116
|
+
Replays cached events, then subscribes to live events.
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
await pubsub.subscribeWithReplay('my-topic', event => {
|
|
120
|
+
console.log(event)
|
|
121
|
+
})
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
#### `subscribeFromOffset(topic, offset, cb)`
|
|
125
|
+
|
|
126
|
+
Replays cached events starting at a known position, then subscribes to live events. This is more efficient than a full replay when the client knows its last position.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
await pubsub.subscribeFromOffset('my-topic', 42, event => {
|
|
130
|
+
console.log(event)
|
|
131
|
+
})
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
## Properties
|
|
135
|
+
|
|
136
|
+
**supportedModes** (`ReadonlyArray<"pull" | "push">`): Delivery modes the implementation supports. Defaults to \`\["pull"]\`.
|
|
137
|
+
|
|
138
|
+
## Types
|
|
139
|
+
|
|
140
|
+
### `Event`
|
|
141
|
+
|
|
142
|
+
**type** (`string`): Event type identifier.
|
|
143
|
+
|
|
144
|
+
**id** (`string`): Unique event ID, assigned by the implementation on publish.
|
|
145
|
+
|
|
146
|
+
**data** (`any`): Event payload.
|
|
147
|
+
|
|
148
|
+
**runId** (`string`): Run the event belongs to.
|
|
149
|
+
|
|
150
|
+
**createdAt** (`Date`): Timestamp assigned by the implementation on publish.
|
|
151
|
+
|
|
152
|
+
**index** (`number`): Sequential position used to resume from a specific offset.
|
|
153
|
+
|
|
154
|
+
**deliveryAttempt** (`number`): Number of times the event has been delivered. Starts at 1. Defaults to 1 when the backend does not track redelivery.
|
|
155
|
+
|
|
156
|
+
### `SubscribeOptions`
|
|
157
|
+
|
|
158
|
+
**group** (`string`): When set, subscribers with the same group compete for messages and each event is delivered to one member. When omitted, every subscriber receives every event.
|
|
159
|
+
|
|
160
|
+
### `EventCallback`
|
|
161
|
+
|
|
162
|
+
The callback signature for subscribers: `(event: Event, ack?: () => Promise<void>, nack?: () => Promise<void>) => void`.
|
|
163
|
+
|
|
164
|
+
**event** (`Event`): The delivered event.
|
|
165
|
+
|
|
166
|
+
**ack** (`() => Promise<void>`): Acknowledge successful processing. The event is removed from the queue.
|
|
167
|
+
|
|
168
|
+
**nack** (`() => Promise<void>`): Negative acknowledge. The event is requeued for redelivery after a delay.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# CachingPubSub
|
|
2
|
+
|
|
3
|
+
`CachingPubSub` wraps any [`PubSub`](https://mastra.ai/reference/pubsub/base) implementation and adds event caching and replay. It records every published event per topic in a cache, so a subscriber that connects late or reconnects after a disconnect can replay the events it missed before continuing with live events.
|
|
4
|
+
|
|
5
|
+
Use it to build resumable streams on top of a transport that does not keep history, such as [`EventEmitterPubSub`](https://mastra.ai/reference/pubsub/event-emitter). Transports that already persist events, such as [`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams), do not need this wrapper.
|
|
6
|
+
|
|
7
|
+
## Usage example
|
|
8
|
+
|
|
9
|
+
Wrap an inner pub/sub and provide a server cache to store events.
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { Mastra } from '@mastra/core'
|
|
13
|
+
import { CachingPubSub, EventEmitterPubSub } from '@mastra/core/events'
|
|
14
|
+
import { InMemoryServerCache } from '@mastra/core/cache'
|
|
15
|
+
|
|
16
|
+
const cache = new InMemoryServerCache()
|
|
17
|
+
const pubsub = new CachingPubSub(new EventEmitterPubSub(), cache)
|
|
18
|
+
|
|
19
|
+
export const mastra = new Mastra({
|
|
20
|
+
pubsub,
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
The `withCaching` helper returns the same instance and reads better when wrapping inline:
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { withCaching, EventEmitterPubSub } from '@mastra/core/events'
|
|
28
|
+
import { InMemoryServerCache } from '@mastra/core/cache'
|
|
29
|
+
|
|
30
|
+
const pubsub = withCaching(new EventEmitterPubSub(), new InMemoryServerCache())
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Constructor parameters
|
|
34
|
+
|
|
35
|
+
**inner** (`PubSub`): The pub/sub implementation to wrap. All publishes and live subscriptions pass through to this instance.
|
|
36
|
+
|
|
37
|
+
**cache** (`MastraServerCache`): Cache used to store events per topic for replay.
|
|
38
|
+
|
|
39
|
+
**options** (`CachingPubSubOptions`): Optional configuration.
|
|
40
|
+
|
|
41
|
+
## Methods
|
|
42
|
+
|
|
43
|
+
`CachingPubSub` implements the [`PubSub`](https://mastra.ai/reference/pubsub/base) contract. It overrides the replay methods to read cached events. The methods below describe the caching behavior.
|
|
44
|
+
|
|
45
|
+
### `publish(topic, event)`
|
|
46
|
+
|
|
47
|
+
Caches the event with a sequential index, then publishes it to the inner pub/sub.
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
await pubsub.publish('my-topic', {
|
|
51
|
+
type: 'example',
|
|
52
|
+
data: { value: 1 },
|
|
53
|
+
runId: 'run-123',
|
|
54
|
+
})
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### `subscribeWithReplay(topic, cb)`
|
|
58
|
+
|
|
59
|
+
Replays all cached events for the topic, then subscribes to live events.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
await pubsub.subscribeWithReplay('my-topic', event => {
|
|
63
|
+
console.log(event)
|
|
64
|
+
})
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
### `subscribeFromOffset(topic, offset, cb)`
|
|
68
|
+
|
|
69
|
+
Replays cached events starting at `offset`, then subscribes to live events. Use this when the client knows its last position, to avoid replaying the whole history.
|
|
70
|
+
|
|
71
|
+
```typescript
|
|
72
|
+
await pubsub.subscribeFromOffset('my-topic', 42, event => {
|
|
73
|
+
console.log(event)
|
|
74
|
+
})
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### `getHistory(topic, offset?)`
|
|
78
|
+
|
|
79
|
+
Returns cached events for the topic, starting at `offset`.
|
|
80
|
+
|
|
81
|
+
```typescript
|
|
82
|
+
const events = await pubsub.getHistory('my-topic', 0)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
Returns: `Promise<Event[]>`
|
|
86
|
+
|
|
87
|
+
## Functions
|
|
88
|
+
|
|
89
|
+
### `withCaching(pubsub, cache, options?)`
|
|
90
|
+
|
|
91
|
+
Convenience wrapper that constructs a `CachingPubSub`. Accepts the same arguments as the constructor and returns the new instance.
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { withCaching, EventEmitterPubSub } from '@mastra/core/events'
|
|
95
|
+
import { InMemoryServerCache } from '@mastra/core/cache'
|
|
96
|
+
|
|
97
|
+
const pubsub = withCaching(new EventEmitterPubSub(), new InMemoryServerCache(), {
|
|
98
|
+
keyPrefix: 'events:',
|
|
99
|
+
})
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Returns: `CachingPubSub`
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# EventEmitterPubSub
|
|
2
|
+
|
|
3
|
+
`EventEmitterPubSub` is the default [`PubSub`](https://mastra.ai/reference/pubsub/base) implementation. It delivers events in-process using a Node.js [`EventEmitter`](https://nodejs.org/api/events.html#class-eventemitter), so it works without any external service.
|
|
4
|
+
|
|
5
|
+
Use it for single-process applications. For delivery across processes on one host, see [`UnixSocketPubSub`](https://mastra.ai/reference/pubsub/unix-socket-pubsub). For distributed delivery, see [`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams) or [`GoogleCloudPubSub`](https://mastra.ai/reference/pubsub/google-cloud-pubsub).
|
|
6
|
+
|
|
7
|
+
Because it is in-process, events are not persisted and are not shared with other processes. Wrap it in [`CachingPubSub`](https://mastra.ai/reference/pubsub/caching-pubsub) when you need replay for resumable streams.
|
|
8
|
+
|
|
9
|
+
## Usage example
|
|
10
|
+
|
|
11
|
+
`EventEmitterPubSub` is used automatically when you do not configure a `pubsub` option, so most applications never construct it directly. Create one explicitly only when you want to configure or share it.
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { Mastra } from '@mastra/core'
|
|
15
|
+
import { EventEmitterPubSub } from '@mastra/core/events'
|
|
16
|
+
|
|
17
|
+
export const mastra = new Mastra({
|
|
18
|
+
pubsub: new EventEmitterPubSub(),
|
|
19
|
+
})
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
To share an emitter with other parts of your application, pass an existing `EventEmitter`:
|
|
23
|
+
|
|
24
|
+
```typescript
|
|
25
|
+
import EventEmitter from 'node:events'
|
|
26
|
+
import { EventEmitterPubSub } from '@mastra/core/events'
|
|
27
|
+
|
|
28
|
+
const emitter = new EventEmitter()
|
|
29
|
+
const pubsub = new EventEmitterPubSub(emitter)
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## Constructor parameters
|
|
33
|
+
|
|
34
|
+
**existingEmitter** (`EventEmitter`): An existing Node.js EventEmitter to use for delivery. When omitted, a new EventEmitter is created.
|
|
35
|
+
|
|
36
|
+
## Properties
|
|
37
|
+
|
|
38
|
+
**supportedModes** (`ReadonlyArray<"pull" | "push">`): Returns \`\["pull", "push"]\`. The emitter can serve a pull-style worker or push events directly to listeners.
|
|
39
|
+
|
|
40
|
+
## Methods
|
|
41
|
+
|
|
42
|
+
`EventEmitterPubSub` implements the [`PubSub`](https://mastra.ai/reference/pubsub/base) contract. The methods below have behavior specific to this implementation.
|
|
43
|
+
|
|
44
|
+
### `subscribe(topic, cb, options?)`
|
|
45
|
+
|
|
46
|
+
Registers a callback for a topic. Without `options.group`, every subscriber receives every event. With a group, events are distributed round-robin across members of that group.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
await pubsub.subscribe('workflow.events', (event, ack, nack) => {
|
|
50
|
+
console.log(event)
|
|
51
|
+
})
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### `flush()`
|
|
55
|
+
|
|
56
|
+
Waits for any pending redeliveries from `nack` to fire before resolving.
|
|
57
|
+
|
|
58
|
+
```typescript
|
|
59
|
+
await pubsub.flush()
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### `close()`
|
|
63
|
+
|
|
64
|
+
Removes all listeners and cancels pending redeliveries. Call this during graceful shutdown.
|
|
65
|
+
|
|
66
|
+
```typescript
|
|
67
|
+
await pubsub.close()
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
## Redelivery
|
|
71
|
+
|
|
72
|
+
When a grouped subscriber calls `nack`, the event is redelivered to the group after a short delay, and its `deliveryAttempt` count increases. Calling `ack` clears the tracking for that event. Fan-out subscribers receive no-op `ack` and `nack` functions, since each event reaches every subscriber once.
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# GoogleCloudPubSub
|
|
2
|
+
|
|
3
|
+
`GoogleCloudPubSub` is a [`PubSub`](https://mastra.ai/reference/pubsub/base) implementation backed by [Google Cloud Pub/Sub](https://cloud.google.com/pubsub/docs). It delivers events across processes and hosts using Google Cloud topics and subscriptions, with ordered delivery and message acknowledgment.
|
|
4
|
+
|
|
5
|
+
Use it for distributed deployments on Google Cloud. For single-process delivery, use [`EventEmitterPubSub`](https://mastra.ai/reference/pubsub/event-emitter). For Redis, use [`RedisStreamsPubSub`](https://mastra.ai/reference/pubsub/redis-streams).
|
|
6
|
+
|
|
7
|
+
Each topic maps to a Google Cloud topic. Subscriptions with a group share a named subscription, so members compete for events. Subscriptions without a group create a per-instance subscription, so every instance receives every event.
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
**npm**:
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
npm install @mastra/google-cloud-pubsub
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
**pnpm**:
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
pnpm add @mastra/google-cloud-pubsub
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
**Yarn**:
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
yarn add @mastra/google-cloud-pubsub
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
**Bun**:
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
bun add @mastra/google-cloud-pubsub
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## Usage example
|
|
36
|
+
|
|
37
|
+
Pass a Google Cloud client configuration, such as a project ID.
|
|
38
|
+
|
|
39
|
+
```typescript
|
|
40
|
+
import { Mastra } from '@mastra/core'
|
|
41
|
+
import { GoogleCloudPubSub } from '@mastra/google-cloud-pubsub'
|
|
42
|
+
|
|
43
|
+
export const mastra = new Mastra({
|
|
44
|
+
pubsub: new GoogleCloudPubSub({
|
|
45
|
+
projectId: 'my-project',
|
|
46
|
+
}),
|
|
47
|
+
})
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Constructor parameters
|
|
51
|
+
|
|
52
|
+
**config** (`ClientConfig`): Configuration for the Google Cloud Pub/Sub client, including credentials and project ID. See the \`@google-cloud/pubsub\` client documentation for all fields.
|
|
53
|
+
|
|
54
|
+
## Methods
|
|
55
|
+
|
|
56
|
+
`GoogleCloudPubSub` implements the [`PubSub`](https://mastra.ai/reference/pubsub/base) contract. The methods below are specific to this implementation.
|
|
57
|
+
|
|
58
|
+
### `init(topicName, group?)`
|
|
59
|
+
|
|
60
|
+
Creates the topic and a subscription if they do not already exist, and returns the subscription. `subscribe` calls this internally, so you rarely call it directly.
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
await pubsub.init('workflow.events')
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
### `subscribe(topic, cb, options?)`
|
|
67
|
+
|
|
68
|
+
Subscribes to a topic. With `options.group`, members of the group share a subscription and compete for events. Without a group, the instance receives every event through its own subscription.
|
|
69
|
+
|
|
70
|
+
```typescript
|
|
71
|
+
await pubsub.subscribe('workflow.events', (event, ack, nack) => {
|
|
72
|
+
console.log(event)
|
|
73
|
+
})
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
### `flush()`
|
|
77
|
+
|
|
78
|
+
Waits for pending acknowledgments to complete.
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
await pubsub.flush()
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### `destroy(topicName)`
|
|
85
|
+
|
|
86
|
+
Removes the subscription and topic for a given topic name. Use this to clean up Google Cloud resources.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
await pubsub.destroy('workflow.events')
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
## Acknowledgment
|
|
93
|
+
|
|
94
|
+
Each delivered event includes `ack` and `nack` functions. Call `ack` after successful processing to remove the event from the subscription. When neither is called, Google Cloud redelivers the event after its acknowledgment deadline expires.
|