
    i                         d Z ddlZddlZddlZddlmZ ddlmZmZm	Z	 ddl
mZmZmZ ddlmZmZ ddlmZmZ  G d	 d
      Zy)z/Human input coordination for workflow sessions.    N)Future)AnyDictOptional)ValidationErrorTimeoutErrorWorkflowCancelledError)LogTypeget_server_logger   )SessionStatusWorkflowSessionStorec                   |    e Zd ZdZdeddfdZdededeeef   ddfd	Z	dded
e
defdZdededdfdZdeddfdZy)SessionExecutionControllerz5Handles blocking wait/provide cycles for human input.storereturnNc                 N    || _         t        j                  t              | _        y )N)r   logging	getLogger__name__logger)selfr   s     N/Users/bowang/.openclaw/workspace/ChatDev/server/services/session_execution.py__init__z#SessionExecutionController.__init__   s    
''1    
session_idnode_id
input_datac                    | j                   j                  |      }|st        dd|i      d|_        ||_        ||_        t        j                  |_        t               |_
        d |_        | j                  j                  d||       y )NSession not foundr   detailsTz'Session %s waiting for input at node %s)r   get_sessionr   waiting_for_inputcurrent_node_idpending_input_datar   WAITING_FOR_INPUTstatusr   human_input_futurehuman_input_valuer   info)r   r   r   r   sessions        r   set_waiting_for_inputz0SessionExecutionController.set_waiting_for_input   s{    **((4!"5j?YZZ$(!")%/"&88%+X"$(!BJPWXr   timeoutc                 J   | j                   j                  |      }|s;t               }|j                  d|t        j
                         t        dd|i      |j                  }|j                  r|Ft               }|j                  d|t        j
                         t        d||j                  d      t        j                         }d	}	 	 |j                  j                         rt        d
|      t        j                         |z
  }||z
  }	|	dk  rt        j                  j                         t!        ||	      }
	 |j#                  |
      }t               }d}t%        |t&              rt)        |j+                  d      xs d      }n|t)        t-        |            }|j/                  dt        j
                  ||       |d|_        d |_        d |_        d |_        S # t        j                  j                  $ r Y 8w xY w# t        j                  j                  $ rX | j4                  j                  d|       t               }|j                  dt        j
                  ||       t7        dd|      w xY w# d|_        d |_        d |_        d |_        w xY w)Nz1Session %s not found when waiting for human input)log_typer    r   r!   z#Session %s is not waiting for input Session is not waiting for inputr   r$   g      ?zWorkflow execution cancelled)workflow_idr   )r.   text zHuman input receivedr0   r   input_lengthFzSession %s human input timeoutzHuman input timeout)r0   r   timeout_durationzInput timeoutwait_for_human_input)	operationr8   )r   r#   r   warningr
   WORKFLOWr   r)   r$   timecancel_eventis_setr	   
concurrentfuturesr   minresult
isinstancedictlengetstrr+   r%   r&   r   CustomTimeoutError)r   r   r.   r,   r   future
start_timepoll_intervalelapsed	remaining	wait_timerC   r7   s                r   r9   z/SessionExecutionController.wait_for_human_input"   s   **((4&(FNNCZZaZjZj   ""5j?YZZ#*#=#=((FN&(FNN5zGL\L\   "2'1HaHab 
 YY[
*	.''..001O]ghh))+
2#g-	>$,,99;;y9	#]]9]=F.0F#$L!&$/'*6::f+=+C'D+'*3v;'7KK.!(!1!1#-%1	    " ).G%&*G#)-G&)-G&! "))66 !!.. 		rKK @*M&(FNN% ))%!(	   %_@Vipqq		r ).G%&*G#)-G&)-G&s9   A3H BG( (H	H H		H A5JJ J"
user_inputc                 R   | j                   j                  |      }|s.t               }|j                  d|       t	        d||d ud      |j
                  }|j                  r|6t               }|j                  d|       t	        d||j                  d      |j                  |       d|_        d	}t        |t              rt        |j                  d
      xs d      }n|t        t        |            }t               }|j                  dt        j                  ||       y )Nz/Session %s not found when providing human inputr    )r   input_providedr!   z7Session %s is not waiting for input when providing datar1   r2   Fr   r4   r5   zHuman input providedr6   )r   r#   r   r;   r   r)   r$   
set_resultrD   rE   rF   rG   rH   r+   r
   r<   )r   r   rP   r,   r   rJ   lengths          r   provide_human_inputz.SessionExecutionController.provide_human_inputd   s   **((4&(FNNLjY!#JZdlpZp-q  $+#=#=((FN&(FNNTV`a!2'1HaHab 
 	*%$)!j$'/526F#Z)F"$"%%!	 	 	
r   c                 v   | j                   j                  |      }|sy |j                  }|r |j                         s|j	                          |j
                  }|r |j                         s|j	                          d|_        d |_        d |_        d |_        d |_	        | j                  j                  d|       y )NFz,Session %s cleaned from execution controller)r   r#   r)   donecancelinput_promiser$   r%   r&   r*   r   r+   )r   r   r,   rJ   promises        r   cleanup_sessionz*SessionExecutionController.cleanup_session   s    **((4#*#=#=&++-MMO''7<<>NN$)!"&%)"%)"$(!GTr   )g      @)r   
__module____qualname____doc__r   r   rH   r   r   r-   floatr9   rU   r[    r   r   r   r      s    ?22 2t 2
Y 
Yc 
YtTWY\T\~ 
Ybf 
Y@.s @.U @.PS @.D
c 
s 
t 
BU# U$ Ur   r   )r^   concurrent.futuresr@   r   r=   r   typingr   r   r   utils.exceptionsr   r   rI   r	   utils.structured_loggerr
   r   session_storer   r   r   r`   r   r   <module>rf      s4    5    % & & h h > >EU EUr   