o
    +if                     @  s  d Z ddlmZ ddlZddlZddlmZmZmZ ddl	m	Z	 ddl
mZmZmZmZmZmZ ddlmZmZ zddlmZmZmZmZ W n ey[   ddlmZmZmZmZ Y nw dd	lmZ dd
lmZmZ ddl m!Z! e"e#Z$dddZ%G dd de!Z&dS )zSchemas for the LangSmith API.    )annotationsN)FutureThreadPoolExecutorwait)datetime)AnyCallableDictListOptionalcast)UUIDuuid4)FieldPrivateAttrroot_validator	validator)utils)ID_TYPEClient)RunBasereturnr   c                   C  s
   t ddS )z3Ensure a thread pool exists in the current context.   )max_workers)r    r   r   L/var/www/html/corbot_env/lib/python3.10/site-packages/langsmith/run_trees.py_make_thread_pool   s   
r   c                   @  s  e Zd ZU dZded< eedZded< eej	dZ
ded< ed	d
dZded< eeddhidZded< edd ddZded< ed	ddZded< eedZded< eed
dZded< eed
dZd ed!< eedZd"ed#< ed$d%d&Zded'< ed$d(d&Zded)< G d*d+ d+Zed!d
d,dcd0d1Zedd
d,ddd3d4Zed
d,ded7d8Zed9d,ded:d;Z d	d	d	d<dfdDdEZ!d	d	d	d	d	d	d	d	d	d	dF
dgdOdPZ"dhdVdWZ#dXdY Z$didjd]d^Z%dkd_d`Z&dldadbZ'd	S )mRunTreez1Run Schema with back-references for posting runs.strname)default_factoryr   idr   
start_timeNT)defaultexcludezOptional[RunTree]
parent_run__all__parent_run_id)r    r$   zList[RunTree]
child_runsc                   C  s   t  S N)r   get_tracer_projectr   r   r   r   <lambda>-   s    zRunTree.<lambda>project_name)r    aliassession_name
project_id)r#   r-   Optional[UUID]
session_idr	   extrar   clientr   executorzList[Future]_futures z!The order of the run in the tree.)r#   descriptiondotted_orderzThe trace id of the run.trace_idc                   @  s   e Zd ZdZdZdS )zRunTree.ConfigTN)__name__
__module____qualname__arbitrary_types_allowedallow_population_by_field_namer   r   r   r   Config<   s    r?   )prevOptional[ThreadPoolExecutor]r   c                 C  s    |du rt  S |jrtd|S )zEnsure the executor is running.NzExecutor has been shutdown.)r   	_shutdown
ValueErrorclsrA   r   r   r   validate_executor@   s
   zRunTree.validate_executorOptional[Client]c                 C  s   |du rt  S |S )zEnsure the client is specified.N)r   rE   r   r   r   validate_clientI   s   zRunTree.validate_clientvaluesdictc                 C  s   d|vrd|d i|d< | ddur|d j|d< d|vr#t |d< d|vr:d|v r3|d j|d< n|d |d< nt|d  tt|di  |S )	zAssign name to the run.
serializedr   r%   Nr'   r!   r9   r2   )getr!   r   r9   printr   rK   
setdefault)rF   rJ   r   r   r   infer_defaultsP   s   
zRunTree.infer_defaultsFc                 C  sb   | d}|r| r|S |d dt|d  }|d r+|d jd | |d< |S ||d< |S )Nr8   r"   z%Y%m%dT%H%M%S%fZr!   r%   .)rM   stripstrftimer   r8   )rF   rJ   current_dotted_orderr   r   r   ensure_dotted_orderc   s   
zRunTree.ensure_dotted_order)outputserrorend_timerV   Optional[Dict]rW   Optional[str]rX   Optional[datetime]Nonec                C  s2   |pt  | _|dur|| _|dur|| _dS dS )z/Set the end time of the run and all child runs.N)r   utcnowrX   rV   rW   )selfrV   rW   rX   r   r   r   ends   s   
zRunTree.end)
run_idrL   inputsrV   rW   reference_example_idr"   rX   tagsr2   run_typer`   Optional[ID_TYPE]rL   ra   rb   rc   Optional[List[str]]c       
         C  s   |pd|i}t di d|d|pt d|d|pi d|pi d|d|d|d	|	p.t d
|
d|pSi d| d| jd| jd| jd|}| j| |S d| d| jd| jd| jd|}| j| |S )z Add a child run to the run tree.r   r!   rL   ra   rV   rW   rd   rb   r"   rX   r2   r%   r.   r3   r4   rc   Nr   )	r   r   r   r]   r.   r3   r4   r(   append)r^   r   rd   r`   rL   ra   rV   rW   rb   r"   rX   rc   r2   serialized_runr   r   r   create_child   sl   
	
zRunTree.create_childfuncr   argsr   kwargsc              
   O  s8   z||i |W S  t y } zt| |d }~ww r)   )	Exceptionlogger	exception)r^   rk   rl   rm   er   r   r   _execute   s   
zRunTree._executec                 C  sd   z	| j dhddW S  ty1   | j h ddd}| jr#| j |d< | jr-| j |d< | Y S w )Nr(   T)r$   exclude_none>   ra   rV   r(   ra   rV   )rK   	TypeErrorra   copyrV   )r^   	self_dictr   r   r   _get_dicts_safe   s   zRunTree._get_dicts_safeexclude_child_runsboolr   c                 C  sf   |   }| j| jj| j| jjfi | |s.t	dt
 | jD ]}| j|jdd q!| jd S )z,Post the run tree to the API asynchronously.z\Posting with exclude_child_runs=False is deprecated and will be removed in a future version.F)rx   )rw   r5   rg   r4   submitrr   r3   
create_runwarningswarnDeprecationWarningr(   post)r^   rx   rm   	child_runr   r   r   r      s"   

zRunTree.postc                 C  sN   | j | jj| j| jj| j| jr| j	 nd| j
| j| j| jd | j d S )z5Patch the run tree to the API in a background thread.N)r`   rV   rW   r'   rb   rX   rz   )r5   rg   r4   r{   rr   r3   
update_runr!   rV   ru   rW   r'   rb   rX   )r^   r   r   r   patch   s   
zRunTree.patchc                 C  s*   | j }t| j  |D ]}| j | q
dS )z"Wait for all _futures to complete.N)r5   r   remove)r^   futuresfuturer   r   r   r      s
   
zRunTree.wait)rA   rB   r   r   )rA   rH   r   r   )rJ   rK   r   rK   )rV   rY   rW   rZ   rX   r[   r   r\   )r   r   rd   r   r`   re   rL   rY   ra   rY   rV   rY   rW   rZ   rb   r0   r"   r[   rX   r[   rc   rf   r2   rY   r   r   )rk   r   rl   r   rm   r   r   r   )T)rx   ry   r   r   )r   r   )r   r\   )(r:   r;   r<   __doc____annotations__r   r   r!   r   r]   r"   r%   listr(   r.   r1   rK   r2   r   r3   r   r4   r   r5   r8   r9   r?   r   rG   rI   r   rP   rU   r_   rj   rr   rw   r   r   r   r   r   r   r   r   !   sl   
 


'
r   )r   r   )'r   
__future__r   loggingr}   concurrent.futuresr   r   r   r   typingr   r   r	   r
   r   r   uuidr   r   pydantic.v1r   r   r   r   ImportErrorpydantic	langsmithr   langsmith.clientr   r   langsmith.schemasr   	getLoggerr:   ro   r   r   r   r   r   r   <module>   s&     

