Commit f4f2396
feat: add RunState parameter support to Runner.run() methods
This commit integrates RunState into the Runner API, allowing runs to be
resumed from a saved state. This is the final piece needed to make
human-in-the-loop (HITL) tool approval fully functional.
**Changes:**
1. **Import NextStepInterruption** (run.py:21-32)
- Added NextStepInterruption to imports from _run_impl
- Added RunState import
2. **Updated Method Signatures** (run.py:285-444)
- Runner.run(): Added `RunState[TContext]` to input union type
- Runner.run_sync(): Added `RunState[TContext]` to input union type
- Runner.run_streamed(): Added `RunState[TContext]` to input union type
- AgentRunner.run(): Added `RunState[TContext]` to input union type
- AgentRunner.run_sync(): Added `RunState[TContext]` to input union type
- AgentRunner.run_streamed(): Added `RunState[TContext]` to input union type
3. **RunState Resumption Logic** (run.py:524-584)
- Check if input is RunState instance
- Extract state fields when resuming: current_turn, original_input,
generated_items, model_responses, context_wrapper
- Prime server conversation tracker from model_responses if resuming
- Cast context_wrapper to correct type after extraction
4. **Interruption Handling** (run.py:689-726)
- Added `interruptions=[]` to successful RunResult creation
- Added elif branch for NextStepInterruption
- Return RunResult with interruptions when tool approval needed
- Set final_output to None for interrupted runs
5. **RunResultStreaming Support** (run.py:879-918)
- Handle RunState input for streaming runs
- Added `interruptions=[]` field to RunResultStreaming creation
- Extract original_input from RunState for result
**How It Works:**
When resuming from RunState:
```python
# User approves/rejects tool calls on the state
run_state.approve(approval_item)
# Resume the run from where it left off
result = await Runner.run(agent, run_state)
```
When a tool needs approval:
1. Run pauses at tool execution
2. Returns RunResult with interruptions=[ToolApprovalItem(...)]
3. User can inspect interruptions and approve/reject
4. User resumes by passing RunResult back to Runner.run()
**Remaining Work:**
- Add `state` property to RunResult for creating RunState from results
- Add comprehensive tests
- Add documentation/examples
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>1 parent 74235e7 commit f4f2396
1 file changed
+78
-21
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| 25 | + | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
| |||
65 | 66 | | |
66 | 67 | | |
67 | 68 | | |
| 69 | + | |
68 | 70 | | |
69 | 71 | | |
70 | 72 | | |
| |||
283 | 285 | | |
284 | 286 | | |
285 | 287 | | |
286 | | - | |
| 288 | + | |
287 | 289 | | |
288 | 290 | | |
289 | 291 | | |
| |||
358 | 360 | | |
359 | 361 | | |
360 | 362 | | |
361 | | - | |
| 363 | + | |
362 | 364 | | |
363 | 365 | | |
364 | 366 | | |
| |||
431 | 433 | | |
432 | 434 | | |
433 | 435 | | |
434 | | - | |
| 436 | + | |
435 | 437 | | |
436 | 438 | | |
437 | 439 | | |
| |||
506 | 508 | | |
507 | 509 | | |
508 | 510 | | |
509 | | - | |
| 511 | + | |
510 | 512 | | |
511 | 513 | | |
512 | 514 | | |
| |||
519 | 521 | | |
520 | 522 | | |
521 | 523 | | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
522 | 545 | | |
523 | 546 | | |
524 | 547 | | |
525 | 548 | | |
526 | 549 | | |
527 | 550 | | |
528 | 551 | | |
529 | | - | |
530 | | - | |
531 | | - | |
532 | | - | |
533 | | - | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
534 | 556 | | |
| 557 | + | |
| 558 | + | |
535 | 559 | | |
536 | 560 | | |
537 | 561 | | |
| |||
541 | 565 | | |
542 | 566 | | |
543 | 567 | | |
544 | | - | |
545 | | - | |
546 | | - | |
547 | | - | |
548 | | - | |
549 | | - | |
550 | | - | |
551 | | - | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
552 | 585 | | |
553 | 586 | | |
554 | 587 | | |
| |||
666 | 699 | | |
667 | 700 | | |
668 | 701 | | |
| 702 | + | |
669 | 703 | | |
670 | 704 | | |
671 | 705 | | |
| |||
675 | 709 | | |
676 | 710 | | |
677 | 711 | | |
| 712 | + | |
| 713 | + | |
| 714 | + | |
| 715 | + | |
| 716 | + | |
| 717 | + | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
| 721 | + | |
| 722 | + | |
| 723 | + | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
678 | 728 | | |
679 | 729 | | |
680 | 730 | | |
| |||
711 | 761 | | |
712 | 762 | | |
713 | 763 | | |
714 | | - | |
| 764 | + | |
715 | 765 | | |
716 | 766 | | |
717 | 767 | | |
| |||
790 | 840 | | |
791 | 841 | | |
792 | 842 | | |
793 | | - | |
| 843 | + | |
794 | 844 | | |
795 | 845 | | |
796 | 846 | | |
| |||
824 | 874 | | |
825 | 875 | | |
826 | 876 | | |
| 877 | + | |
| 878 | + | |
| 879 | + | |
| 880 | + | |
| 881 | + | |
| 882 | + | |
827 | 883 | | |
828 | | - | |
| 884 | + | |
829 | 885 | | |
830 | 886 | | |
831 | 887 | | |
| |||
840 | 896 | | |
841 | 897 | | |
842 | 898 | | |
| 899 | + | |
843 | 900 | | |
844 | 901 | | |
845 | 902 | | |
846 | 903 | | |
847 | 904 | | |
848 | | - | |
| 905 | + | |
849 | 906 | | |
850 | 907 | | |
851 | 908 | | |
| |||
0 commit comments