Skip to content

Commit 3553b23

Browse files
committed
split _run_impl.py into run_internal/
1 parent 92d70a1 commit 3553b23

44 files changed

Lines changed: 7295 additions & 6913 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/agents/_run_impl.py

Lines changed: 0 additions & 3278 deletions
This file was deleted.

src/agents/agent.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,12 @@ async def run_agent(context: ToolContext, input: str) -> Any:
479479
from .tool_context import ToolContext
480480

481481
resolved_max_turns = max_turns if max_turns is not None else DEFAULT_MAX_TURNS
482-
nested_context = context if isinstance(context, RunContextWrapper) else context
482+
if isinstance(context, ToolContext):
483+
nested_context = context
484+
elif isinstance(context, RunContextWrapper):
485+
nested_context = context.context
486+
else:
487+
nested_context = context
483488
run_result: RunResult | RunResultStreaming
484489

485490
if on_stream is not None:

src/agents/result.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
from dataclasses import dataclass, field
88
from typing import Any, Literal, TypeVar, cast
99

10-
from ._run_impl import NextStepInterruption, ProcessedResponse, QueueCompleteSentinel
1110
from .agent import Agent
1211
from .agent_output import AgentOutputSchemaBase
1312
from .exceptions import (
@@ -26,6 +25,11 @@
2625
)
2726
from .logger import logger
2827
from .run_context import RunContextWrapper
28+
from .run_internal.run_steps import (
29+
NextStepInterruption,
30+
ProcessedResponse,
31+
QueueCompleteSentinel,
32+
)
2933
from .run_state import RunState
3034
from .stream_events import StreamEvent
3135
from .tool_guardrails import ToolInputGuardrailResult, ToolOutputGuardrailResult
@@ -306,7 +310,7 @@ class RunResultStreaming(RunResultBase):
306310
)
307311

308312
# Store the asyncio tasks that we're waiting on
309-
_run_impl_task: asyncio.Task[Any] | None = field(default=None, repr=False)
313+
run_loop_task: asyncio.Task[Any] | None = field(default=None, repr=False)
310314
_input_guardrails_task: asyncio.Task[Any] | None = field(default=None, repr=False)
311315
_output_guardrails_task: asyncio.Task[Any] | None = field(default=None, repr=False)
312316
_stored_exception: Exception | None = field(default=None, repr=False)
@@ -462,7 +466,7 @@ async def stream_events(self) -> AsyncIterator[StreamEvent]:
462466
else:
463467
# Ensure main execution completes before cleanup to avoid race conditions
464468
# with session operations
465-
await self._await_task_safely(self._run_impl_task)
469+
await self._await_task_safely(self.run_loop_task)
466470
# Safely terminate all background tasks after main execution has finished
467471
self._cleanup_tasks()
468472

@@ -504,9 +508,9 @@ def _check_errors(self):
504508
self._stored_exception = tripwire_exc
505509

506510
# Check the tasks for any exceptions
507-
if self._run_impl_task and self._run_impl_task.done():
508-
if not self._run_impl_task.cancelled():
509-
run_impl_exc = self._run_impl_task.exception()
511+
if self.run_loop_task and self.run_loop_task.done():
512+
if not self.run_loop_task.cancelled():
513+
run_impl_exc = self.run_loop_task.exception()
510514
if run_impl_exc and isinstance(run_impl_exc, Exception):
511515
if isinstance(run_impl_exc, AgentsException) and run_impl_exc.run_data is None:
512516
run_impl_exc.run_data = self._create_error_details()
@@ -532,8 +536,8 @@ def _check_errors(self):
532536
self._stored_exception = out_guard_exc
533537

534538
def _cleanup_tasks(self):
535-
if self._run_impl_task and not self._run_impl_task.done():
536-
self._run_impl_task.cancel()
539+
if self.run_loop_task and not self.run_loop_task.done():
540+
self.run_loop_task.cancel()
537541

538542
if self._input_guardrails_task and not self._input_guardrails_task.done():
539543
self._input_guardrails_task.cancel()

0 commit comments

Comments
 (0)