@mastra/client-js 1.30.1-alpha.2 → 1.31.0-alpha.3
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 +30 -0
- package/dist/client.d.ts +34 -50
- package/dist/client.d.ts.map +1 -1
- package/dist/docs/SKILL.md +3 -3
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-long-running-agents-schedules.md +227 -0
- package/dist/docs/references/reference-client-js-observability.md +88 -1
- package/dist/index.cjs +51 -85
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +51 -85
- package/dist/index.js.map +1 -1
- package/dist/route-types.generated.d.ts +331 -551
- package/dist/route-types.generated.d.ts.map +1 -1
- package/dist/types.d.ts +123 -91
- package/dist/types.d.ts.map +1 -1
- package/package.json +6 -6
- package/dist/docs/references/docs-long-running-agents-heartbeats.md +0 -213
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mastra/client-js",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.31.0-alpha.3",
|
|
4
4
|
"description": "The official TypeScript library for the Mastra Client API",
|
|
5
5
|
"author": "",
|
|
6
6
|
"type": "module",
|
|
@@ -39,8 +39,8 @@
|
|
|
39
39
|
"canonicalize": "^1.0.8",
|
|
40
40
|
"jose": "^6.2.1",
|
|
41
41
|
"json-schema": "^0.4.0",
|
|
42
|
-
"@mastra/
|
|
43
|
-
"@mastra/
|
|
42
|
+
"@mastra/core": "1.50.0-alpha.3",
|
|
43
|
+
"@mastra/schema-compat": "1.3.3"
|
|
44
44
|
},
|
|
45
45
|
"peerDependencies": {
|
|
46
46
|
"zod": "^3.25.0 || ^4.0.0"
|
|
@@ -56,10 +56,10 @@
|
|
|
56
56
|
"typescript": "^6.0.3",
|
|
57
57
|
"vitest": "4.1.8",
|
|
58
58
|
"zod": "^4.4.3",
|
|
59
|
-
"@internal/ai-sdk-v5": "0.0.58",
|
|
60
59
|
"@internal/ai-sdk-v4": "0.0.58",
|
|
61
|
-
"@internal/
|
|
62
|
-
"@internal/types-builder": "0.0.86"
|
|
60
|
+
"@internal/ai-sdk-v5": "0.0.58",
|
|
61
|
+
"@internal/types-builder": "0.0.86",
|
|
62
|
+
"@internal/lint": "0.0.111"
|
|
63
63
|
},
|
|
64
64
|
"engines": {
|
|
65
65
|
"node": ">=22.13.0"
|
|
@@ -1,213 +0,0 @@
|
|
|
1
|
-
> Discover all available pages from the documentation index: https://mastra.ai/llms.txt
|
|
2
|
-
|
|
3
|
-
# Heartbeats
|
|
4
|
-
|
|
5
|
-
**Added in:** `@mastra/core@1.46.0`
|
|
6
|
-
|
|
7
|
-
> **Beta:** This feature is in beta. Breaking changes may occur without a major version bump until the API is stable.
|
|
8
|
-
|
|
9
|
-
A heartbeat runs an agent on a cron schedule. On each fire, Mastra sends a prompt to the agent, either as a [signal](https://mastra.ai/docs/long-running-agents/signals) into a thread or as a threadless `agent.generate()` run. Use heartbeats for recurring agent work such as daily summaries, periodic checks, or scheduled nudges into a conversation.
|
|
10
|
-
|
|
11
|
-
Heartbeats are persisted, so they survive restarts and redeploys. Manage them at runtime through `mastra.heartbeats`, the canonical create, read, update, and delete (CRUD) surface.
|
|
12
|
-
|
|
13
|
-
## Prerequisites
|
|
14
|
-
|
|
15
|
-
Heartbeats require a [storage](https://mastra.ai/docs/memory/storage) adapter that implements the schedules domain, for example `@mastra/libsql`. Without one, `mastra.heartbeats.create()` throws.
|
|
16
|
-
|
|
17
|
-
## Quickstart
|
|
18
|
-
|
|
19
|
-
The following heartbeat runs the `pinger` agent every hour. It has no thread, so each fire is an isolated `agent.generate()` run.
|
|
20
|
-
|
|
21
|
-
```typescript
|
|
22
|
-
import { Mastra } from '@mastra/core'
|
|
23
|
-
import { Agent } from '@mastra/core/agent'
|
|
24
|
-
import { LibSQLStore } from '@mastra/libsql'
|
|
25
|
-
|
|
26
|
-
const pinger = new Agent({
|
|
27
|
-
id: 'pinger',
|
|
28
|
-
name: 'Pinger',
|
|
29
|
-
instructions: 'Report the current system status in one sentence.',
|
|
30
|
-
model: 'openai/gpt-5.5',
|
|
31
|
-
})
|
|
32
|
-
|
|
33
|
-
const mastra = new Mastra({
|
|
34
|
-
agents: { pinger },
|
|
35
|
-
storage: new LibSQLStore({ url: 'file:./mastra.db' }),
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
await mastra.heartbeats.create({
|
|
39
|
-
agentId: 'pinger',
|
|
40
|
-
cron: '0 * * * *',
|
|
41
|
-
prompt: 'Give me a status update.',
|
|
42
|
-
})
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Mastra starts the scheduler the first time a heartbeat is created, then fires the agent on the cron you specify.
|
|
46
|
-
|
|
47
|
-
## Cadence
|
|
48
|
-
|
|
49
|
-
Heartbeats fire on a cron expression. The `cron` field accepts a standard 5-, 6-, or 7-part cron expression, and it's validated when you create or update the heartbeat.
|
|
50
|
-
|
|
51
|
-
Croner nicknames also work, for example `@hourly`, `@daily`, `@weekly`, `@monthly`, and `@midnight`. For day-and-time combinations, write the cron field directly:
|
|
52
|
-
|
|
53
|
-
```typescript
|
|
54
|
-
// Every weekday at 9am
|
|
55
|
-
await mastra.heartbeats.create({
|
|
56
|
-
agentId: 'pinger',
|
|
57
|
-
cron: '0 9 * * 1-5',
|
|
58
|
-
prompt: 'Start-of-day check.',
|
|
59
|
-
})
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Set `timezone` to an IANA timezone, for example `America/New_York`, so fire times don't depend on the host's locale. When omitted, the cron resolves against the host's local timezone.
|
|
63
|
-
|
|
64
|
-
For more readable cron construction, you can use a userland builder such as [`cron-time-generator`](https://www.npmjs.com/package/cron-time-generator) and pass its output to `cron`.
|
|
65
|
-
|
|
66
|
-
## Threadless and threaded heartbeats
|
|
67
|
-
|
|
68
|
-
A heartbeat fires in one of two modes, decided by whether you pass a `threadId`.
|
|
69
|
-
|
|
70
|
-
### Threadless
|
|
71
|
-
|
|
72
|
-
Without a `threadId`, each fire is an isolated `agent.generate()` run. Nothing is written to a conversation thread. This is the simplest mode and suits status checks, reports, and other work that doesn't need conversation context.
|
|
73
|
-
|
|
74
|
-
### Threaded
|
|
75
|
-
|
|
76
|
-
With a `threadId`, the heartbeat sends a [signal](https://mastra.ai/docs/long-running-agents/signals) into that thread, so the prompt joins the agent's conversation. Threaded heartbeats require a `resourceId` alongside the `threadId`.
|
|
77
|
-
|
|
78
|
-
```typescript
|
|
79
|
-
await mastra.heartbeats.create({
|
|
80
|
-
agentId: 'pinger',
|
|
81
|
-
cron: '0 9 * * *',
|
|
82
|
-
prompt: 'Summarize anything new since yesterday.',
|
|
83
|
-
threadId: 'thread-123',
|
|
84
|
-
resourceId: 'user-456',
|
|
85
|
-
})
|
|
86
|
-
```
|
|
87
|
-
|
|
88
|
-
Threaded heartbeats accept extra fields that control how the signal behaves. They mirror the options [`agent.sendSignal`](https://mastra.ai/docs/long-running-agents/signals) accepts and stay JSON-serializable so they persist with the schedule.
|
|
89
|
-
|
|
90
|
-
- `signalType`: the [signal type](https://mastra.ai/docs/long-running-agents/signals) to send, for example `notification` or `system-reminder`. Defaults to `notification`.
|
|
91
|
-
- `tagName`: the XML tag the signal renders as. Defaults to `heartbeat`, so a fire surfaces to the agent as `<heartbeat>…</heartbeat>`.
|
|
92
|
-
- `attributes`: values rendered onto the signal's XML tag.
|
|
93
|
-
- `ifActive`: behavior when the thread is already streaming, as `{ behavior, attributes }`. `behavior` is one of `deliver`, `persist`, or `discard`.
|
|
94
|
-
- `ifIdle`: behavior when the thread is idle, as `{ behavior, attributes, streamOptions }`. `behavior` is one of `wake`, `persist`, or `discard`. `streamOptions.requestContext` is applied to the woken run.
|
|
95
|
-
|
|
96
|
-
These fields require a `threadId`. Passing them on a threadless heartbeat throws.
|
|
97
|
-
|
|
98
|
-
```typescript
|
|
99
|
-
await mastra.heartbeats.create({
|
|
100
|
-
agentId: 'pinger',
|
|
101
|
-
cron: '0 9 * * *',
|
|
102
|
-
prompt: 'Summarize anything new since yesterday.',
|
|
103
|
-
threadId: 'thread-123',
|
|
104
|
-
resourceId: 'user-456',
|
|
105
|
-
tagName: 'check-in', // renders as <check-in>…</check-in>
|
|
106
|
-
attributes: { source: 'cron' },
|
|
107
|
-
ifActive: { behavior: 'discard' }, // skip if the thread is mid-stream
|
|
108
|
-
ifIdle: {
|
|
109
|
-
behavior: 'wake', // wake the agent if the thread is idle
|
|
110
|
-
streamOptions: { requestContext: { locale: 'en-US' } },
|
|
111
|
-
},
|
|
112
|
-
})
|
|
113
|
-
```
|
|
114
|
-
|
|
115
|
-
`providerOptions` are merged into the signal payload on every fire and apply to both threaded and threadless heartbeats.
|
|
116
|
-
|
|
117
|
-
## Managing heartbeats
|
|
118
|
-
|
|
119
|
-
Use `mastra.heartbeats` for all heartbeat operations. To scope to a single agent, pass `agentId` to `create` or `list`.
|
|
120
|
-
|
|
121
|
-
```typescript
|
|
122
|
-
// Create
|
|
123
|
-
const hb = await mastra.heartbeats.create({
|
|
124
|
-
agentId: 'pinger',
|
|
125
|
-
cron: '0 * * * *',
|
|
126
|
-
prompt: 'Status check.',
|
|
127
|
-
})
|
|
128
|
-
|
|
129
|
-
// Read
|
|
130
|
-
await mastra.heartbeats.get(hb.id)
|
|
131
|
-
await mastra.heartbeats.list({ agentId: 'pinger' })
|
|
132
|
-
|
|
133
|
-
// Update — changing cron or timezone recomputes the next fire time
|
|
134
|
-
await mastra.heartbeats.update(hb.id, { cron: '*/30 * * * *' })
|
|
135
|
-
|
|
136
|
-
// Pause and resume
|
|
137
|
-
await mastra.heartbeats.pause(hb.id)
|
|
138
|
-
await mastra.heartbeats.resume(hb.id)
|
|
139
|
-
|
|
140
|
-
// Fire once now, off-schedule
|
|
141
|
-
await mastra.heartbeats.run(hb.id)
|
|
142
|
-
|
|
143
|
-
// Delete
|
|
144
|
-
await mastra.heartbeats.delete(hb.id)
|
|
145
|
-
```
|
|
146
|
-
|
|
147
|
-
A few rules worth knowing:
|
|
148
|
-
|
|
149
|
-
- `pause` and `resume` are durable and idempotent. A paused heartbeat survives restarts, and `resume` recomputes the next fire time from now rather than firing backlogged runs.
|
|
150
|
-
- `run` fires the heartbeat once immediately without affecting its schedule.
|
|
151
|
-
- `list` filters by `agentId`, `threadId`, `resourceId`, and `name`.
|
|
152
|
-
|
|
153
|
-
### Custom IDs
|
|
154
|
-
|
|
155
|
-
By default `create` generates a random `hb_<uuid>` id. Pass `id` to choose a stable one, for example when you want a predictable handle to look up, update, or delete later:
|
|
156
|
-
|
|
157
|
-
```typescript
|
|
158
|
-
await mastra.heartbeats.create({
|
|
159
|
-
id: 'nightly-summary',
|
|
160
|
-
agentId: 'pinger',
|
|
161
|
-
cron: '0 9 * * *',
|
|
162
|
-
prompt: 'Summarize anything new since yesterday.',
|
|
163
|
-
})
|
|
164
|
-
// stored as `hb_nightly-summary`
|
|
165
|
-
```
|
|
166
|
-
|
|
167
|
-
The id is normalized to `hb_<slug>`: the `hb_` prefix is added if missing and the rest is slugified. Creating a heartbeat with an id that already exists throws, so use `update` to change an existing one.
|
|
168
|
-
|
|
169
|
-
### From the client
|
|
170
|
-
|
|
171
|
-
The same operations are available from `@mastra/client-js` over the server routes, so you can manage heartbeats from a separate process or a UI.
|
|
172
|
-
|
|
173
|
-
## Lifecycle hooks
|
|
174
|
-
|
|
175
|
-
Hooks let you run code at key points in a heartbeat's lifecycle, for example to compute fire-time parameters or react to the outcome. Configure them on the `Mastra` constructor under `heartbeat`. The hooks are a single flat bundle that runs for every agent's heartbeats; each hook context carries the firing `agentId`, so branch on it when you need per-agent behavior. Hooks live at the `Mastra` level so they apply to both code-defined and stored agents.
|
|
176
|
-
|
|
177
|
-
```typescript
|
|
178
|
-
const mastra = new Mastra({
|
|
179
|
-
agents: { pinger },
|
|
180
|
-
storage: new LibSQLStore({ url: 'file:./mastra.db' }),
|
|
181
|
-
heartbeat: {
|
|
182
|
-
prepare: async ({ agentId, heartbeat, trigger }) => {
|
|
183
|
-
// Return overrides, null to skip this fire, or undefined for defaults
|
|
184
|
-
return { prompt: `Status as of ${trigger.firedAt.toISOString()}` }
|
|
185
|
-
},
|
|
186
|
-
onFinish: async ({ agentId, outcome, runId }) => {
|
|
187
|
-
// Runs on any non-error, non-abort outcome
|
|
188
|
-
},
|
|
189
|
-
onError: async ({ agentId, phase, error }) => {
|
|
190
|
-
// Runs when prepare, the signal, or the agent run threw
|
|
191
|
-
},
|
|
192
|
-
onAbort: async ({ agentId, runId }) => {
|
|
193
|
-
// Runs when the run was aborted mid-stream
|
|
194
|
-
},
|
|
195
|
-
},
|
|
196
|
-
})
|
|
197
|
-
```
|
|
198
|
-
|
|
199
|
-
The hooks are:
|
|
200
|
-
|
|
201
|
-
- `prepare`: runs before the fire. Return an object to override fire-time parameters such as `prompt` or `threadId`, `null` to skip the fire, or `undefined` to use the stored defaults.
|
|
202
|
-
- `onFinish`: runs once per trigger that reached a non-error, non-abort terminal state.
|
|
203
|
-
- `onError`: runs when `prepare`, the signal, or the agent run threw.
|
|
204
|
-
- `onAbort`: runs when the run was aborted mid-stream.
|
|
205
|
-
|
|
206
|
-
Every hook context includes `agentId` (the agent the heartbeat fired for) alongside `heartbeat` and `trigger`.
|
|
207
|
-
|
|
208
|
-
Hook exceptions are caught and logged. They never re-route the worker or trigger another hook.
|
|
209
|
-
|
|
210
|
-
## Related
|
|
211
|
-
|
|
212
|
-
- [Signals](https://mastra.ai/docs/long-running-agents/signals): the delivery mechanism behind threaded heartbeats.
|
|
213
|
-
- [Scheduled workflows](https://mastra.ai/docs/workflows/scheduled-workflows): run a workflow, rather than an agent, on a cron schedule.
|