
    i                         d Z ddlZddlmZmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ  G d de      Zy)z:PromptChannel implementation backed by WebSocket sessions.    N)AnyDictListOptional)MessageBlock)AttachmentService)SessionExecutionController)AttachmentStore)TimeoutError)PromptChannelPromptResult)get_server_loggerc                       e Zd ZdZdedededededdfd	Z	ddd
dedede
e   de
eeef      def
dZdedeeee   f   fdZdedee   dee   fdZdedededdfdZy)WebPromptChannelzFPrompt channel that mediates through the WebSocket session controller.
session_idsession_controllerwebsocket_managerattachment_serviceattachment_storereturnNc                    || _         || _        || _        || _        || _        	 t        j                         | _        y # t        $ r
 d | _        Y y w xY w)N)	r   r   r   r   r   asyncioget_running_loop_loopRuntimeError)selfr   r   r   r   r   s         K/Users/bowang/.openclaw/workspace/ChatDev/server/services/prompt_channel.py__init__zWebPromptChannel.__init__   sV     %"4!2"4 0	 113DJ 	DJ	s   ? AA)inputsmetadatanode_idtaskr   r    c                   |xs d}||d|xs i }| j                   j                  | j                  ||       | j                  |||       	 | j                   j	                  | j                        }| j                  |      \  }
}| j                  |
|      }t        |      t        |      d}t        |
||      S # t
        $ r  t        $ r/}t               }	|	j                  |d|| j                          d }~ww xY w)N )inputtask_descriptionzError waiting for human input)r!   r   )attachment_count
input_size)textblocksr    )r   set_waiting_for_inputr   _notify_human_promptwait_for_human_inputr   	Exceptionr   log_exception_extract_response_build_blockslenr   )r   r!   r"   r   r    previewpayloadhuman_responseexcloggerresponse_textattachment_idsr*   metadata_outs                 r   requestzWebPromptChannel.request%   s    ,B $
 ~2
 	55OO	
 	!!'7D9	!44II$//ZN )-(>(>~(N%~##M>B #N 3g,
 vUU  	 	&(F  &Ewcgcrcr s	s   %B= =C>*C99C>r4   c                     t        |t              r.|j                  d      xs d}|j                  d      xs g }||fS |dg fS t        |      g fS )Nr)   r$   attachments)
isinstancedictgetstr)r   r4   r8   r=   s       r   r0   z"WebPromptChannel._extract_responseM   sZ    gt$#KK/52M!++m4:K +--?r6M7|R    r)   r9   c                 (   g }|r$|j                  t        j                  |             |rA|j                  | j                  j                  | j                  || j                               |s$|j                  t        j                  d             |S )N)target_storer$   )appendr   
text_blockextendr   build_attachment_blocksr   r   )r   r)   r9   r*   s       r   r1   zWebPromptChannel._build_blocksV   s~    %'MM,11$78MM''??OO"!%!6!6 @  MM,11"56rB   r3   c                    d|||dd}| j                   rp| j                   j                         rVt        j                  | j                  j                  | j                  |      | j                         }	 |j                          y | j                  j                  | j                  |       y # t        $ r) | j                  j                  | j                  |       Y y w xY w)Nhuman_input_required)r!   r%   r&   )typedata)
r   
is_runningr   run_coroutine_threadsafer   send_messager   resultr.   send_message_sync)r   r!   r3   r"   messagefutures         r   r,   z%WebPromptChannel._notify_human_promptf   s    *" $(
 ::$**//155&&33DOOWM

FS
 ""44T__gN	  S&&88'RSs   5B- -/CC)__name__
__module____qualname____doc__rA   r	   r   r   r
   r   r   r   r   r;   tupler   r0   r   r1   r,    rB   r   r   r      s   P  7	
  . * 
0 !%-1&V &V 	&V
 &V 4S>*&V 
&VP   sDI~1F  # tCy T,EW  OC O# OS OT OrB   r   )rW   r   typingr   r   r   r   entity.messagesr   "server.services.attachment_servicer   !server.services.session_executionr	   utils.attachmentsr
   utils.exceptionsr   utils.human_promptr   r   utils.structured_loggerr   r   rY   rB   r   <module>rb      s6    @  , , ( @ H - ) : 5kO} kOrB   