|
2 | 2 |
|
3 | 3 | import asyncio
|
4 | 4 | from collections.abc import AsyncIterator
|
5 |
| -from typing import Any |
| 5 | +from typing import Any, cast |
6 | 6 |
|
7 | 7 | from typing_extensions import assert_never
|
8 | 8 |
|
| 9 | +from ..agent import Agent |
9 | 10 | from ..handoffs import Handoff
|
10 | 11 | from ..run_context import RunContextWrapper, TContext
|
11 | 12 | from ..tool import FunctionTool
|
12 | 13 | from ..tool_context import ToolContext
|
13 | 14 | from .agent import RealtimeAgent
|
14 |
| -from .config import RealtimeUserInput |
| 15 | +from .config import RealtimeRunConfig, RealtimeUserInput |
15 | 16 | from .events import (
|
16 | 17 | RealtimeAgentEndEvent,
|
17 | 18 | RealtimeAgentStartEvent,
|
|
20 | 21 | RealtimeAudioInterrupted,
|
21 | 22 | RealtimeError,
|
22 | 23 | RealtimeEventInfo,
|
| 24 | + RealtimeGuardrailTripped, |
23 | 25 | RealtimeHistoryAdded,
|
24 | 26 | RealtimeHistoryUpdated,
|
25 | 27 | RealtimeRawModelEvent,
|
@@ -62,26 +64,35 @@ def __init__(
|
62 | 64 | agent: RealtimeAgent,
|
63 | 65 | context: TContext | None,
|
64 | 66 | model_config: RealtimeModelConfig | None = None,
|
| 67 | + run_config: RealtimeRunConfig | None = None, |
65 | 68 | ) -> None:
|
66 | 69 | """Initialize the session.
|
67 | 70 |
|
68 | 71 | Args:
|
69 | 72 | model: The model to use.
|
70 | 73 | agent: The current agent.
|
71 |
| - context_wrapper: The context wrapper. |
72 |
| - event_info: Event info object. |
73 |
| - history: The conversation history. |
| 74 | + context: The context object. |
74 | 75 | model_config: Model configuration.
|
| 76 | + run_config: Runtime configuration including guardrails. |
75 | 77 | """
|
76 | 78 | self._model = model
|
77 | 79 | self._current_agent = agent
|
78 | 80 | self._context_wrapper = RunContextWrapper(context)
|
79 | 81 | self._event_info = RealtimeEventInfo(context=self._context_wrapper)
|
80 | 82 | self._history: list[RealtimeItem] = []
|
81 | 83 | self._model_config = model_config or {}
|
| 84 | + self._run_config = run_config or {} |
82 | 85 | self._event_queue: asyncio.Queue[RealtimeSessionEvent] = asyncio.Queue()
|
83 | 86 | self._closed = False
|
84 | 87 |
|
| 88 | + # Guardrails state tracking |
| 89 | + self._interrupted_by_guardrail = False |
| 90 | + self._item_transcripts: dict[str, str] = {} # item_id -> accumulated transcript |
| 91 | + self._item_guardrail_run_counts: dict[str, int] = {} # item_id -> run count |
| 92 | + self._debounce_text_length = self._run_config.get("guardrails_settings", {}).get( |
| 93 | + "debounce_text_length", 100 |
| 94 | + ) |
| 95 | + |
85 | 96 | async def __aenter__(self) -> RealtimeSession:
|
86 | 97 | """Start the session by connecting to the model. After this, you will be able to stream
|
87 | 98 | events from the model and send messages and audio to the model.
|
@@ -159,8 +170,22 @@ async def on_event(self, event: RealtimeModelEvent) -> None:
|
159 | 170 | RealtimeHistoryUpdated(info=self._event_info, history=self._history)
|
160 | 171 | )
|
161 | 172 | elif event.type == "transcript_delta":
|
162 |
| - # TODO (rm) Add guardrails |
163 |
| - pass |
| 173 | + # Accumulate transcript text for guardrail debouncing per item_id |
| 174 | + item_id = event.item_id |
| 175 | + if item_id not in self._item_transcripts: |
| 176 | + self._item_transcripts[item_id] = "" |
| 177 | + self._item_guardrail_run_counts[item_id] = 0 |
| 178 | + |
| 179 | + self._item_transcripts[item_id] += event.delta |
| 180 | + |
| 181 | + # Check if we should run guardrails based on debounce threshold |
| 182 | + current_length = len(self._item_transcripts[item_id]) |
| 183 | + threshold = self._debounce_text_length |
| 184 | + next_run_threshold = (self._item_guardrail_run_counts[item_id] + 1) * threshold |
| 185 | + |
| 186 | + if current_length >= next_run_threshold: |
| 187 | + self._item_guardrail_run_counts[item_id] += 1 |
| 188 | + await self._run_output_guardrails(self._item_transcripts[item_id]) |
164 | 189 | elif event.type == "item_updated":
|
165 | 190 | is_new = not any(item.item_id == event.item.item_id for item in self._history)
|
166 | 191 | self._history = self._get_new_history(self._history, event.item)
|
@@ -189,6 +214,11 @@ async def on_event(self, event: RealtimeModelEvent) -> None:
|
189 | 214 | )
|
190 | 215 | )
|
191 | 216 | elif event.type == "turn_ended":
|
| 217 | + # Clear guardrail state for next turn |
| 218 | + self._item_transcripts.clear() |
| 219 | + self._item_guardrail_run_counts.clear() |
| 220 | + self._interrupted_by_guardrail = False |
| 221 | + |
192 | 222 | await self._put_event(
|
193 | 223 | RealtimeAgentEndEvent(
|
194 | 224 | agent=self._current_agent,
|
@@ -290,3 +320,49 @@ def _get_new_history(
|
290 | 320 |
|
291 | 321 | # Otherwise, add it to the end
|
292 | 322 | return old_history + [event]
|
| 323 | + |
| 324 | + async def _run_output_guardrails(self, text: str) -> bool: |
| 325 | + """Run output guardrails on the given text. Returns True if any guardrail was triggered.""" |
| 326 | + output_guardrails = self._run_config.get("output_guardrails", []) |
| 327 | + if not output_guardrails or self._interrupted_by_guardrail: |
| 328 | + return False |
| 329 | + |
| 330 | + triggered_results = [] |
| 331 | + |
| 332 | + for guardrail in output_guardrails: |
| 333 | + try: |
| 334 | + result = await guardrail.run( |
| 335 | + # TODO (rm) Remove this cast, it's wrong |
| 336 | + self._context_wrapper, |
| 337 | + cast(Agent[Any], self._current_agent), |
| 338 | + text, |
| 339 | + ) |
| 340 | + if result.output.tripwire_triggered: |
| 341 | + triggered_results.append(result) |
| 342 | + except Exception: |
| 343 | + # Continue with other guardrails if one fails |
| 344 | + continue |
| 345 | + |
| 346 | + if triggered_results: |
| 347 | + # Mark as interrupted to prevent multiple interrupts |
| 348 | + self._interrupted_by_guardrail = True |
| 349 | + |
| 350 | + # Emit guardrail tripped event |
| 351 | + await self._put_event( |
| 352 | + RealtimeGuardrailTripped( |
| 353 | + guardrail_results=triggered_results, |
| 354 | + message=text, |
| 355 | + info=self._event_info, |
| 356 | + ) |
| 357 | + ) |
| 358 | + |
| 359 | + # Interrupt the model |
| 360 | + await self._model.interrupt() |
| 361 | + |
| 362 | + # Send guardrail triggered message |
| 363 | + guardrail_names = [result.guardrail.get_name() for result in triggered_results] |
| 364 | + await self._model.send_message(f"guardrail triggered: {', '.join(guardrail_names)}") |
| 365 | + |
| 366 | + return True |
| 367 | + |
| 368 | + return False |
0 commit comments