stream_dispatch.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Adapter-driven dispatch of structured stream events (gateway/stream_events.py).
  2. Message events flow into the consumer; tool events are formatted by the adapter (None
  3. = eat it on platforms without tool chrome) and enqueued onto the same tool-progress
  4. queue the gateway drains, so the two paths never race. Synchronous: callable from
  5. the agent's worker thread.
  6. """
  7. from __future__ import annotations
  8. import logging
  9. from typing import Any, Callable, Optional
  10. from gateway.stream_events import (
  11. Commentary, GatewayNotice, LongToolHint, MessageChunk, MessageStop, StreamEvent, ToolCallChunk,
  12. )
  13. logger = logging.getLogger("gateway.stream_events")
  14. class GatewayEventDispatcher:
  15. """Route typed stream events through an adapter onto a delivery sink.
  16. sink: the GatewayStreamConsumer, or None when streaming is disabled (message
  17. events dropped; the final still goes out normally). enqueue_tool_line: None
  18. when tool progress is disabled. tool_mode: "all"/"new"/"verbose"/"off";
  19. preview_max_len: ``tool_preview_length`` (0 = no cap in verbose).
  20. on_long_tool / on_notice: the gateway owns the "surface this here?" decision.
  21. """
  22. def __init__(
  23. self,
  24. adapter: Any,
  25. sink: Any = None,
  26. *,
  27. enqueue_tool_line: Optional[Callable[[Any], None]] = None,
  28. tool_mode: str = "all",
  29. preview_max_len: int = 40,
  30. on_long_tool: Optional[Callable[[LongToolHint], None]] = None,
  31. on_notice: Optional[Callable[[GatewayNotice], None]] = None,
  32. ) -> None:
  33. self.adapter = adapter
  34. self.sink = sink
  35. self._enqueue_tool_line = enqueue_tool_line
  36. self.tool_mode = tool_mode or "all"
  37. self.preview_max_len = preview_max_len
  38. self._on_long_tool = on_long_tool
  39. self._on_notice = on_notice
  40. self._last_tool: Optional[str] = None # "new"-mode dedup
  41. def dispatch(self, event: StreamEvent) -> None:
  42. """Route a single event. Never raises into the agent's worker thread."""
  43. try:
  44. self._dispatch(event)
  45. except Exception: # presentation must never break the agent loop
  46. logger.debug("stream-event dispatch error", exc_info=True)
  47. def _dispatch(self, event: StreamEvent) -> None:
  48. # ToolCallFinished: no chrome on completion (only "started" is rendered);
  49. # completion only drives onboarding hints (LongToolHint).
  50. if isinstance(event, (MessageChunk, MessageStop, Commentary)):
  51. if self.sink is not None:
  52. self.adapter.render_message_event(event, self.sink)
  53. elif isinstance(event, ToolCallChunk):
  54. self._dispatch_tool_call(event)
  55. elif isinstance(event, LongToolHint) and self._on_long_tool is not None:
  56. self._on_long_tool(event)
  57. elif isinstance(event, GatewayNotice) and self._on_notice is not None:
  58. self._on_notice(event)
  59. def _dispatch_tool_call(self, event: ToolCallChunk) -> None:
  60. if self.tool_mode == "off" or self._enqueue_tool_line is None:
  61. return
  62. if self.tool_mode == "new" and event.tool_name == self._last_tool:
  63. return
  64. self._last_tool = event.tool_name
  65. line = self.adapter.format_tool_event(
  66. event, mode=self.tool_mode, preview_max_len=self.preview_max_len,
  67. )
  68. if line: # None/"" == adapter chose to eat this event
  69. self._enqueue_tool_line(line)
  70. __all__ = ["GatewayEventDispatcher"]