-
Notifications
You must be signed in to change notification settings - Fork 0
feat(lifecycle): add on_turn_start and on_turn_end hooks to RunHooksBase (#2671) #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 3 commits
27d5dbe
ff99d90
f1cafcd
fe48c87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,7 +111,7 @@ | |
| from .tracing import Span, SpanError, agent_span, get_current_trace | ||
| from .tracing.context import TraceCtxManager, create_trace_for_run | ||
| from .tracing.span_data import AgentSpanData | ||
| from .util import _error_tracing | ||
| from .util import _coro, _error_tracing | ||
|
|
||
| DEFAULT_AGENT_RUNNER: AgentRunner = None # type: ignore | ||
| # the value is set at the end of the module | ||
|
|
@@ -968,6 +968,25 @@ def _with_reasoning_item_id_policy(result: RunResult) -> RunResult: | |
|
|
||
| logger.debug("Running agent %s (turn %s)", current_agent.name, current_turn) | ||
|
|
||
| run_hook_control, agent_hook_control = await asyncio.gather( | ||
| hooks.on_turn_start(context_wrapper, current_agent, current_turn), | ||
| ( | ||
| current_agent.hooks.on_turn_start( | ||
| context_wrapper, current_agent, current_turn | ||
| ) | ||
| if current_agent.hooks | ||
| else _coro.noop_coroutine() | ||
| ), | ||
| ) | ||
| if run_hook_control == "stop" or agent_hook_control == "stop": | ||
| logger.debug( | ||
| "Turn %s: on_turn_start hook requested stop; halting run.", | ||
| current_turn, | ||
| ) | ||
| raise MaxTurnsExceeded( | ||
| f"Run halted by on_turn_start hook at turn {current_turn}" | ||
| ) | ||
|
Comment on lines
+996
to
+1003
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Route hook-triggered stops through the existing max-turns handler. Line 1001 raises 🤖 Prompt for AI Agents |
||
|
|
||
| if session_persistence_enabled: | ||
| try: | ||
| last_saved_input_snapshot_for_rewind = ( | ||
|
|
@@ -1093,6 +1112,17 @@ def _with_reasoning_item_id_policy(result: RunResult) -> RunResult: | |
| last_saved_input_snapshot_for_rewind = None | ||
| should_run_agent_start_hooks = False | ||
|
|
||
| await asyncio.gather( | ||
| hooks.on_turn_end(context_wrapper, current_agent, current_turn), | ||
| ( | ||
| current_agent.hooks.on_turn_end( | ||
| context_wrapper, current_agent, current_turn | ||
| ) | ||
| if current_agent.hooks | ||
| else _coro.noop_coroutine() | ||
| ), | ||
| ) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't emit When Please skip this callback for 🤖 Prompt for AI Agents |
||
|
|
||
| model_responses.append(turn_result.model_response) | ||
| original_input = turn_result.original_input | ||
| # For model input, use new_step_items (filtered on handoffs). | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -820,6 +820,29 @@ async def _save_stream_items_without_count( | |
| streamed_result._event_queue.put_nowait(QueueCompleteSentinel()) | ||
| break | ||
|
|
||
| run_hook_control, agent_hook_control = await asyncio.gather( | ||
| hooks.on_turn_start(context_wrapper, current_agent, current_turn), | ||
| ( | ||
| current_agent.hooks.on_turn_start( | ||
| context_wrapper, current_agent, current_turn | ||
| ) | ||
| if current_agent.hooks | ||
| else _coro.noop_coroutine() | ||
| ), | ||
| ) | ||
| if run_hook_control == "stop" or agent_hook_control == "stop": | ||
| logger.debug( | ||
| "Turn %s: on_turn_start hook requested stop; halting run.", | ||
| current_turn, | ||
| ) | ||
| streamed_result._max_turns_handled = True | ||
| streamed_result.current_turn = current_turn | ||
| if run_state is not None: | ||
| run_state._current_turn = current_turn | ||
| run_state._current_step = None | ||
| streamed_result._event_queue.put_nowait(QueueCompleteSentinel()) | ||
| break | ||
|
Comment on lines
+836
to
+857
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Route hook-requested stops through the existing max-turns path. Lines 833-844 terminate the stream directly, but they skip the 🤖 Prompt for AI Agents |
||
|
|
||
| if current_turn == 1: | ||
| all_input_guardrails = starting_agent.input_guardrails + ( | ||
| run_config.input_guardrails or [] | ||
|
|
@@ -909,6 +932,17 @@ async def _save_stream_items_without_count( | |
| tool_use_tracker | ||
| ) | ||
|
|
||
| await asyncio.gather( | ||
| hooks.on_turn_end(context_wrapper, current_agent, current_turn), | ||
| ( | ||
| current_agent.hooks.on_turn_end( | ||
| context_wrapper, current_agent, current_turn | ||
| ) | ||
| if current_agent.hooks | ||
| else _coro.noop_coroutine() | ||
| ), | ||
| ) | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| streamed_result.raw_responses = streamed_result.raw_responses + [ | ||
| turn_result.model_response | ||
| ] | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace the en dashes in the
TurnControldocstring.Ruff flags the
–characters on Lines 17-18 as ambiguous punctuation (RUF001), so this will fail in strict linting. Use plain-here instead.🧰 Tools
🪛 Ruff (0.15.10)
[warning] 17-17: String contains ambiguous
–(EN DASH). Did you mean-(HYPHEN-MINUS)?(RUF001)
[warning] 18-18: String contains ambiguous
–(EN DASH). Did you mean-(HYPHEN-MINUS)?(RUF001)
🤖 Prompt for AI Agents