| 448 | """ |
| 449 | |
| 450 | async def run( |
| 451 | self, |
| 452 | starting_agent: Agent[TContext], |
| 453 | input: str | list[TResponseInputItem] | RunState[TContext], |
| 454 | **kwargs: Unpack[RunOptions[TContext]], |
| 455 | ) -> RunResult: |
| 456 | context = kwargs.get("context") |
| 457 | max_turns = kwargs.get("max_turns", DEFAULT_MAX_TURNS) |
| 458 | hooks = cast(RunHooks[TContext], validate_run_hooks(kwargs.get("hooks"))) |
| 459 | run_config = kwargs.get("run_config") |
| 460 | error_handlers = kwargs.get("error_handlers") |
| 461 | previous_response_id = kwargs.get("previous_response_id") |
| 462 | auto_previous_response_id = kwargs.get("auto_previous_response_id", False) |
| 463 | conversation_id = kwargs.get("conversation_id") |
| 464 | session = kwargs.get("session") |
| 465 | |
| 466 | if run_config is None: |
| 467 | run_config = RunConfig() |
| 468 | |
| 469 | is_resumed_state = isinstance(input, RunState) |
| 470 | run_state: RunState[TContext] | None = None |
| 471 | starting_input = input if not is_resumed_state else None |
| 472 | original_user_input: str | list[TResponseInputItem] | None = None |
| 473 | session_input_items_for_persistence: list[TResponseInputItem] | None = ( |
| 474 | [] if (session is not None and is_resumed_state) else None |
| 475 | ) |
| 476 | # Track the most recent input batch we persisted so conversation-lock retries can rewind |
| 477 | # exactly those items (and not the full history). |
| 478 | last_saved_input_snapshot_for_rewind: list[TResponseInputItem] | None = None |
| 479 | |
| 480 | if is_resumed_state: |
| 481 | run_state = cast(RunState[TContext], input) |
| 482 | ( |
| 483 | conversation_id, |
| 484 | previous_response_id, |
| 485 | auto_previous_response_id, |
| 486 | ) = apply_resumed_conversation_settings( |
| 487 | run_state=run_state, |
| 488 | conversation_id=conversation_id, |
| 489 | previous_response_id=previous_response_id, |
| 490 | auto_previous_response_id=auto_previous_response_id, |
| 491 | ) |
| 492 | validate_session_conversation_settings( |
| 493 | session, |
| 494 | conversation_id=conversation_id, |
| 495 | previous_response_id=previous_response_id, |
| 496 | auto_previous_response_id=auto_previous_response_id, |
| 497 | ) |
| 498 | starting_input = run_state._original_input |
| 499 | original_user_input = copy_input_items(run_state._original_input) |
| 500 | prepared_input = normalize_resumed_input(original_user_input) |
| 501 | |
| 502 | context_wrapper = resolve_resumed_context( |
| 503 | run_state=run_state, |
| 504 | context=context, |
| 505 | ) |
| 506 | context = context_wrapper.context |
| 507 | |