
    `gj5                        d Z ddlmZ ddlZddlmZmZmZ ddlm	Z	m
Z
mZmZmZmZmZmZ  ej"                  d      Z G d d      ZdgZy)	u  Adapter-driven dispatch of structured stream events to a delivery sink.

``GatewayEventDispatcher`` is the seam Tobi asked for: the agent emits typed
events (gateway/stream_events.py), and the *adapter* decides how each one is
delivered.  The dispatcher holds an adapter + the stream consumer (sink) + the
resolved per-channel presentation settings (tool-progress mode, preview length)
and routes each event through the adapter's render hooks.

Message/commentary/segment events flow into the consumer (native draft on
Telegram DMs, edit-in-place elsewhere).  Tool events are formatted by the
adapter — which may return None to *eat* the event on platforms that can't
render tool chrome — and the rendered line is enqueued onto the same tool
progress queue the gateway already drains, so the two no longer race through
independent code paths.

This module deliberately has no platform knowledge and no asyncio: it is a thin
synchronous router callable from the agent's worker thread, exactly like the
callbacks it replaces.
    )annotationsN)AnyCallableOptional)
CommentaryGatewayNoticeLongToolHintMessageChunkMessageStopStreamEventToolCallChunkToolCallFinishedzgateway.stream_eventsc                  X    e Zd ZdZ	 d	dddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d
dZddZddZy)GatewayEventDispatchera  Route typed stream events through an adapter onto a delivery sink.

    Parameters
    ----------
    adapter:
        The platform adapter.  Provides ``render_message_event`` and
        ``format_tool_event`` (BasePlatformAdapter defaults reproduce today's
        behavior; adapters may override for native rendering).
    sink:
        The GatewayStreamConsumer for assistant-text delivery.  May be None
        when streaming is disabled, in which case message events are dropped
        (the final response still goes out via the normal send path).
    enqueue_tool_line:
        Callback that places a rendered tool-progress line onto the gateway's
        progress queue (the same queue ``send_progress_messages`` drains).  May
        be None when tool progress is disabled for this channel.
    tool_mode:
        Resolved tool-progress mode for this channel ("all" / "new" / "verbose"
        / "off").
    preview_max_len:
        Resolved ``tool_preview_length`` (0 = no cap in verbose mode).
    on_long_tool / on_notice:
        Optional hooks for LongToolHint / GatewayNotice events, letting the
        gateway own the "should I surface this here?" decision.
    Nall(   )enqueue_tool_line	tool_modepreview_max_lenon_long_tool	on_noticec               |    || _         || _        || _        |xs d| _        || _        || _        || _        d | _        y )Nr   )adaptersink_enqueue_tool_liner   r   _on_long_tool
_on_notice
_last_tool)selfr   r   r   r   r   r   r   s           J/root/.hermes/venv/lib/python3.12/site-packages/gateway/stream_dispatch.py__init__zGatewayEventDispatcher.__init__C   sD     	"3"+e.)#)-    c                t    	 | j                  |       y# t        $ r t        j                  dd       Y yw xY w)zCRoute a single event.  Never raises into the agent's worker thread.zstream-event dispatch errorT)exc_infoN)	_dispatch	Exceptionloggerdebug)r   events     r    dispatchzGatewayEventDispatcher.dispatchX   s6    	GNN5! 	GLL6LF	Gs     77c                   t        |t        t        t        f      r3| j                  &| j
                  j                  || j                         y t        |t              r| j                  dk(  s| j                  y | j                  dk(  r|j                  | j                  k(  ry |j                  | _        | j
                  j                  || j                  | j                        }|r| j                  |       y t        |t              ry t        |t              r| j                   | j!                  |       y t        |t"              r| j$                  | j%                  |       y y )Noffnew)moder   )
isinstancer
   r   r   r   r   render_message_eventr   r   r   	tool_namer   format_tool_eventr   r   r	   r   r   r   )r   r)   lines      r    r%   z GatewayEventDispatcher._dispatch_   s$   elKDEyy$11%Ce]+~~&$*A*A*I~~&5??doo+M#ooDO<<11DNND<P<P 2 D ''-e-. e\*!!-""5)e]+*& ,r"   )N)r   r   r   r   r   zOptional[Callable[[Any], None]]r   strr   intr   z(Optional[Callable[[LongToolHint], None]]r   z)Optional[Callable[[GatewayNotice], None]]returnNone)r)   r   r6   r7   )__name__
__module____qualname____doc__r!   r*   r%    r"   r    r   r   (   s    : .
 >B!AE?C.. .
 ;. . . ?. =. 
.*G"r"   r   )r;   
__future__r   loggingtypingr   r   r   gateway.stream_eventsr   r   r	   r
   r   r   r   r   	getLoggerr'   r   __all__r<   r"   r    <module>rC      sS   ( #  * *	 	 	 
		2	3Y Yx $
$r"   