notification_presentation.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. """Turn-local presentation of diagnostic-only wakes; execution and controls stay live."""
  2. from contextlib import contextmanager
  3. from contextvars import ContextVar
  4. _muted_surface: ContextVar[str | None] = ContextVar("muted_notification_surface", default=None)
  5. # These are text/media UI events, not approval/clarify/connection requests or outcomes.
  6. _FREEFORM_EVENTS = frozenset({
  7. "message.start", "message.delta", "message.interim", "message.complete",
  8. "reasoning.delta", "thinking.delta", "status.update", "notification.show",
  9. "tool.start", "tool.complete", "tool.generating", "error", "reaction",
  10. })
  11. _PRESENTATION_CALLBACKS = (
  12. "stream_delta_callback", "interim_assistant_callback", "reasoning_callback",
  13. "tool_progress_callback",
  14. "tool_start_callback", "tool_complete_callback", "tool_gen_callback", "reaction_callback",
  15. )
  16. def event_presentation_muted(event: str, session_id: str) -> bool:
  17. return _muted_surface.get() == session_id and event in _FREEFORM_EVENTS
  18. def diagnostic_process_event(event: dict) -> bool:
  19. """Early failure/monitor diagnostics, not the explicitly requested final result."""
  20. return bool(event.get("task_failure_notice")) or event.get("type") in {
  21. "watch_disabled", "watch_overflow_tripped", "watch_overflow_released",
  22. }
  23. def notification_config_snapshot():
  24. """Read the owning effective config once per turn (the loader already returns a fresh copy)."""
  25. from gateway.warning_notifications import effective_user_config
  26. return effective_user_config()
  27. @contextmanager
  28. def notification_policy_snapshot(agent, platform, config):
  29. """Bind one foreground policy for callbacks, including worker threads. ``config`` is read-only."""
  30. missing = object()
  31. saved = {key: getattr(agent, key, missing)
  32. for key in ("_notification_config", "_notification_platform")}
  33. try:
  34. agent._notification_config = config
  35. agent._notification_platform = platform
  36. yield
  37. finally:
  38. for key, value in saved.items():
  39. if value is missing:
  40. delattr(agent, key)
  41. else:
  42. setattr(agent, key, value)
  43. @contextmanager
  44. def notification_turn(agent, *, muted: bool, session_id: str = ""):
  45. """Freeze the current turn's presentation without touching prompts or tool schemas."""
  46. if not muted:
  47. yield
  48. return
  49. missing = object()
  50. keys = (*_PRESENTATION_CALLBACKS, "suppress_status_output", "_mute_notification_reply")
  51. saved = {key: getattr(agent, key, missing) for key in keys}
  52. token = _muted_surface.set(session_id)
  53. try:
  54. for key in _PRESENTATION_CALLBACKS:
  55. setattr(agent, key, None)
  56. agent.suppress_status_output = True
  57. agent._mute_notification_reply = True
  58. yield
  59. finally:
  60. for key, value in saved.items():
  61. if value is missing:
  62. delattr(agent, key)
  63. else:
  64. setattr(agent, key, value)
  65. _muted_surface.reset(token)