Commit 972d591
feat(tools): add AbortSignal support for tool runner (#848)
## Summary
- Adds `AbortSignal` support to `BetaToolRunner` for cancelling API
calls and tool execution
- Introduces `BetaToolRunContext` type passed to `tool.run()` with
`signal` and `toolUseBlock`
- Adds `setRequestOptions()` method for updating signal/headers after
runner creation
## Context
Picks up the design from #877 (which was approved but stalled), adapted
to the current codebase. Multiple users have requested the ability to
cancel tool runner operations.
The signal flows through:
1. Constructor options or `setRequestOptions()` → stored in `#options`
2. `#options` passed to API calls (`messages.create`/`messages.stream`)
3. `generateToolResponse(signal?)` → `tool.run(input, { toolUseBlock,
signal })`
Tools can check `context?.signal?.aborted` or pass the signal to
downstream operations like `fetch()`.
## Test plan
- [x] `./scripts/build` passes
- [x] `./scripts/test -- tests/lib/tools/ToolRunner.test.ts` — 5 new
tests
- [x] Signal + toolUseBlock passed to tool.run via constructor options
- [x] Undefined signal when none provided
- [x] `setRequestOptions()` with direct object
- [x] `setRequestOptions()` with mutator function
- [x] Documentation updated in helpers.md
---------
Co-authored-by: Felix Becker <felix@anthropic.com>
Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
Co-authored-by: Mike Cluck <mcluck90@gmail.com>1 parent 0b536ae commit 972d591
File tree
6 files changed
+457
-15
lines changed- src
- helpers/beta
- lib/tools
- tests/lib/tools
6 files changed
+457
-15
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
256 | 256 | | |
257 | 257 | | |
258 | 258 | | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
259 | 301 | | |
260 | 302 | | |
261 | 303 | | |
| |||
386 | 428 | | |
387 | 429 | | |
388 | 430 | | |
389 | | - | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
390 | 448 | | |
391 | | - | |
| 449 | + | |
392 | 450 | | |
393 | 451 | | |
394 | 452 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | | - | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
20 | 23 | | |
21 | 24 | | |
22 | 25 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
6 | | - | |
| 6 | + | |
7 | 7 | | |
8 | 8 | | |
9 | 9 | | |
| |||
50 | 50 | | |
51 | 51 | | |
52 | 52 | | |
53 | | - | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
54 | 57 | | |
55 | 58 | | |
56 | 59 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
35 | 41 | | |
36 | 42 | | |
37 | 43 | | |
38 | | - | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
39 | 48 | | |
40 | 49 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
152 | 152 | | |
153 | 153 | | |
154 | 154 | | |
155 | | - | |
| 155 | + | |
| 156 | + | |
156 | 157 | | |
157 | 158 | | |
158 | 159 | | |
| |||
281 | 282 | | |
282 | 283 | | |
283 | 284 | | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
284 | 319 | | |
285 | 320 | | |
286 | 321 | | |
| |||
293 | 328 | | |
294 | 329 | | |
295 | 330 | | |
296 | | - | |
| 331 | + | |
297 | 332 | | |
298 | 333 | | |
299 | 334 | | |
300 | 335 | | |
301 | | - | |
| 336 | + | |
302 | 337 | | |
303 | 338 | | |
304 | | - | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
305 | 343 | | |
306 | 344 | | |
307 | 345 | | |
308 | | - | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
309 | 350 | | |
310 | 351 | | |
311 | 352 | | |
| |||
407 | 448 | | |
408 | 449 | | |
409 | 450 | | |
| 451 | + | |
410 | 452 | | |
411 | 453 | | |
412 | 454 | | |
| |||
441 | 483 | | |
442 | 484 | | |
443 | 485 | | |
444 | | - | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
445 | 490 | | |
446 | 491 | | |
447 | 492 | | |
| |||
491 | 536 | | |
492 | 537 | | |
493 | 538 | | |
494 | | - | |
| 539 | + | |
0 commit comments