Commit 943d2d7
Messages and abort (#1345)
# Agent Abort Signal and Message Continuation
## Why
Enable users to cancel long-running agent tasks and continue
conversations across multiple `execute()` calls. Also ensures graceful
shutdown when `stagehand.close()` is called by automatically aborting
any running agent tasks.
## What Changed
### New Features (behind `experimental: true`)
#### Abort Signal Support
- Pass `signal` to `agent.execute()` to cancel execution mid-run
- Works with `AbortController` and `AbortSignal.timeout()`
- Throws `AgentAbortError` when aborted
#### Message Continuation
- `execute()` now returns `messages` in the result
- Pass previous messages to continue a conversation across calls
### New Utilities
| File | Purpose |
|---------------------------------|-------------------------------------------------------------------------------------------|
| `combineAbortSignals.ts` | Merges multiple signals (uses native
`AbortSignal.any()` on Node 20+, fallback for older) |
| `errorHandling.ts` | Consolidates abort detection logic—needed because
`close()` may cause indirect errors (e.g., null context) that should
still be treated as abort |
| `validateExperimentalFeatures.ts` | Single place for all
experimental/CUA feature validation |
### CUA Limitations
Abort signal and message continuation are not supported with CUA mode
(throws `StagehandInvalidArgumentError`). This matches existing
streaming limitation.
### Tests Added
- `agent-abort-signal.spec.ts` (7 tests)
- `agent-message-continuation.spec.ts` (4 tests)
- `agent-experimental-validation.spec.ts` (17 tests)
<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds agent abort support and conversation continuation. You can cancel
long runs, auto-abort on close, and carry messages across execute()
calls. Feature is gated behind experimental: true and has clear CUA
limitations.
- **New Features**
- Abort signal for execute() and stream() with AbortController and
AbortSignal.timeout; throws AgentAbortError; stagehand.close()
auto-aborts via an internal controller combined with any user signal.
- Message continuation: execute() returns messages and accepts previous
messages on the next call; tool calls and results are included.
- **Refactors**
- Centralized experimental/CUA validation via
validateExperimentalFeatures: CUA disallows streaming, abort signal, and
message continuation; experimental required for integrations, tools,
streaming, callbacks, signal, and messages.
- Public API updates: re-export ModelMessage; Agent types include
messages and signal; AgentAbortError exported for consistent abort
typing.
<sup>Written for commit 5276e41.
Summary will update automatically on new commits.</sup>
<!-- End of auto-generated description by cubic. -->
---------
Co-authored-by: Nick Sweeting <github@sweeting.me>1 parent fdbb58c commit 943d2d7
File tree
12 files changed
+1000
-82
lines changed- .changeset
- packages/core
- lib/v3
- agent/utils
- handlers
- tests
- types/public
- tests/public-api
12 files changed
+1000
-82
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
Lines changed: 92 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
29 | 29 | | |
30 | 30 | | |
31 | 31 | | |
| 32 | + | |
32 | 33 | | |
33 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
34 | 39 | | |
35 | 40 | | |
36 | 41 | | |
| |||
72 | 77 | | |
73 | 78 | | |
74 | 79 | | |
75 | | - | |
76 | | - | |
77 | | - | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
78 | 85 | | |
79 | 86 | | |
80 | 87 | | |
| |||
176 | 183 | | |
177 | 184 | | |
178 | 185 | | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
184 | | - | |
185 | | - | |
186 | | - | |
187 | | - | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
193 | | - | |
194 | | - | |
195 | | - | |
196 | | - | |
197 | | - | |
198 | | - | |
199 | | - | |
200 | | - | |
201 | | - | |
202 | | - | |
203 | | - | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
204 | 190 | | |
205 | 191 | | |
206 | 192 | | |
207 | 193 | | |
208 | 194 | | |
209 | 195 | | |
210 | | - | |
| 196 | + | |
211 | 197 | | |
212 | 198 | | |
| 199 | + | |
| 200 | + | |
213 | 201 | | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
214 | 232 | | |
215 | 233 | | |
216 | 234 | | |
| |||
221 | 239 | | |
222 | 240 | | |
223 | 241 | | |
| 242 | + | |
224 | 243 | | |
225 | 244 | | |
226 | | - | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
227 | 251 | | |
228 | | - | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
229 | 264 | | |
230 | 265 | | |
231 | 266 | | |
232 | 267 | | |
233 | 268 | | |
| 269 | + | |
| 270 | + | |
234 | 271 | | |
235 | 272 | | |
236 | 273 | | |
237 | 274 | | |
238 | 275 | | |
| 276 | + | |
239 | 277 | | |
240 | 278 | | |
241 | 279 | | |
| |||
244 | 282 | | |
245 | 283 | | |
246 | 284 | | |
| 285 | + | |
247 | 286 | | |
248 | 287 | | |
249 | 288 | | |
| |||
303 | 342 | | |
304 | 343 | | |
305 | 344 | | |
306 | | - | |
307 | | - | |
308 | | - | |
309 | | - | |
310 | | - | |
311 | | - | |
312 | | - | |
313 | | - | |
314 | | - | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
315 | 356 | | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
316 | 362 | | |
| 363 | + | |
317 | 364 | | |
318 | 365 | | |
319 | 366 | | |
| |||
324 | 371 | | |
325 | 372 | | |
326 | 373 | | |
327 | | - | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
328 | 380 | | |
329 | 381 | | |
330 | 382 | | |
| |||
344 | 396 | | |
345 | 397 | | |
346 | 398 | | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
347 | 406 | | |
348 | 407 | | |
349 | 408 | | |
| |||
358 | 417 | | |
359 | 418 | | |
360 | 419 | | |
| 420 | + | |
361 | 421 | | |
362 | 422 | | |
363 | 423 | | |
| |||
0 commit comments