Commit 6f344a1
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 <[email protected]>1 parent 16673c0 commit 6f344a1
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 | | |
| |||
296 | 298 | | |
297 | 299 | | |
298 | 300 | | |
299 | | - | |
| 301 | + | |
300 | 302 | | |
301 | 303 | | |
302 | 304 | | |
| |||
371 | 373 | | |
372 | 374 | | |
373 | 375 | | |
374 | | - | |
| 376 | + | |
375 | 377 | | |
376 | 378 | | |
377 | 379 | | |
| |||
444 | 446 | | |
445 | 447 | | |
446 | 448 | | |
447 | | - | |
| 449 | + | |
448 | 450 | | |
449 | 451 | | |
450 | 452 | | |
| |||
519 | 521 | | |
520 | 522 | | |
521 | 523 | | |
522 | | - | |
| 524 | + | |
523 | 525 | | |
524 | 526 | | |
525 | 527 | | |
| |||
532 | 534 | | |
533 | 535 | | |
534 | 536 | | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
535 | 558 | | |
536 | 559 | | |
537 | 560 | | |
538 | 561 | | |
539 | 562 | | |
540 | 563 | | |
541 | 564 | | |
542 | | - | |
543 | | - | |
544 | | - | |
545 | | - | |
546 | | - | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
547 | 569 | | |
| 570 | + | |
| 571 | + | |
548 | 572 | | |
549 | 573 | | |
550 | 574 | | |
| |||
554 | 578 | | |
555 | 579 | | |
556 | 580 | | |
557 | | - | |
558 | | - | |
559 | | - | |
560 | | - | |
561 | | - | |
562 | | - | |
563 | | - | |
564 | | - | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
565 | 598 | | |
566 | 599 | | |
567 | 600 | | |
| |||
679 | 712 | | |
680 | 713 | | |
681 | 714 | | |
| 715 | + | |
682 | 716 | | |
683 | 717 | | |
684 | 718 | | |
| |||
688 | 722 | | |
689 | 723 | | |
690 | 724 | | |
| 725 | + | |
| 726 | + | |
| 727 | + | |
| 728 | + | |
| 729 | + | |
| 730 | + | |
| 731 | + | |
| 732 | + | |
| 733 | + | |
| 734 | + | |
| 735 | + | |
| 736 | + | |
| 737 | + | |
| 738 | + | |
| 739 | + | |
| 740 | + | |
691 | 741 | | |
692 | 742 | | |
693 | 743 | | |
| |||
724 | 774 | | |
725 | 775 | | |
726 | 776 | | |
727 | | - | |
| 777 | + | |
728 | 778 | | |
729 | 779 | | |
730 | 780 | | |
| |||
803 | 853 | | |
804 | 854 | | |
805 | 855 | | |
806 | | - | |
| 856 | + | |
807 | 857 | | |
808 | 858 | | |
809 | 859 | | |
| |||
837 | 887 | | |
838 | 888 | | |
839 | 889 | | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
840 | 896 | | |
841 | | - | |
| 897 | + | |
842 | 898 | | |
843 | 899 | | |
844 | 900 | | |
| |||
853 | 909 | | |
854 | 910 | | |
855 | 911 | | |
| 912 | + | |
856 | 913 | | |
857 | 914 | | |
858 | 915 | | |
859 | 916 | | |
860 | 917 | | |
861 | | - | |
| 918 | + | |
862 | 919 | | |
863 | 920 | | |
864 | 921 | | |
| |||
0 commit comments