Commit d7136af
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
run_state.approve(approval_item)
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 422c17d commit d7136af
1 file changed
+78
-22
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 | | |
| |||
704 | 737 | | |
705 | 738 | | |
706 | 739 | | |
| 740 | + | |
707 | 741 | | |
708 | 742 | | |
709 | 743 | | |
| |||
712 | 746 | | |
713 | 747 | | |
714 | 748 | | |
715 | | - | |
| 749 | + | |
| 750 | + | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
| 755 | + | |
| 756 | + | |
| 757 | + | |
| 758 | + | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
| 763 | + | |
| 764 | + | |
716 | 765 | | |
717 | 766 | | |
718 | 767 | | |
| |||
756 | 805 | | |
757 | 806 | | |
758 | 807 | | |
759 | | - | |
| 808 | + | |
760 | 809 | | |
761 | 810 | | |
762 | 811 | | |
| |||
835 | 884 | | |
836 | 885 | | |
837 | 886 | | |
838 | | - | |
| 887 | + | |
839 | 888 | | |
840 | 889 | | |
841 | 890 | | |
| |||
869 | 918 | | |
870 | 919 | | |
871 | 920 | | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
| 926 | + | |
872 | 927 | | |
873 | | - | |
| 928 | + | |
874 | 929 | | |
875 | 930 | | |
876 | 931 | | |
| |||
885 | 940 | | |
886 | 941 | | |
887 | 942 | | |
| 943 | + | |
888 | 944 | | |
889 | 945 | | |
890 | 946 | | |
891 | 947 | | |
892 | 948 | | |
893 | | - | |
| 949 | + | |
894 | 950 | | |
895 | 951 | | |
896 | 952 | | |
| |||
0 commit comments