o
    i                     @   sZ  d dl Z d dlZd dlZd dlmZmZmZmZ d dlmZm	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mZmZmZmZmZmZmZmZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) d dl*m+Z+ ejeeeeef Z,eje,ef Z-de,d	e.fd
dZ/de0e! d	e"fddZ1de0e. d	e)fddZ2de3e4e0e  f d	e0e, fddZ5eG dd dZ6eG dd dZ7G dd deZ8eje%ed f Z9ejeeeedf eeedf eeedf eeedf eeedf eeed f eeed!f eeed"f eeed#f ee8edf f
 ed$d% f Z:G d&d' d'eZ;dS )(    N)Union	AnnotatedOptionalLiteral)	dataclassfield)DiscriminatorTag	BaseModel)pydantic_model_from_json)FunctionResultEntryFunctionCallEntryMessageOutputEntryAgentHandoffEntryToolExecutionEntryMessageInputEntryAgentHandoffDoneEventAgentHandoffStartedEventResponseDoneEventResponseErrorEventResponseStartedEventFunctionCallEventMessageOutputEventToolExecutionDoneEventToolExecutionStartedEventConversationEventsDataMessageOutputEventContentMessageOutputEntryContent	TextChunkMessageOutputContentChunksSSETypesInputEntriesToolFileChunkToolReferenceChunkFunctionCallEntryArgumentsget_discriminatorentryreturnc                 C   s   d}t | trFt | jtr|| j7 }|S | jD ].}t |tr$||j7 }qt |tr7|d|j d|j d7 }qt |t	rE|d|j
 d7 }q|S )zDKeep only the messages and turn content into textual representation. z	<File id=z name=>z<Reference title=)
isinstancer   contentstrr   textr"   file_id	file_namer#   title)r'   r.   chunk r3   `/var/www/html/karishye-ai-python/venv/lib/python3.10/site-packages/mistralai/extra/run/result.pyas_text1   s   

	



r5   chunksc                 C   s   t dd | D rdttt | S g }| D ]2}t|tr$t|d}t|trEt|r?t|d tr?|d  j	|j	7  _	q|
| q|
| q|S )z[Given a list of MessageOutputEventContent, recreate a normalised MessageOutputEntryContent.c                 s   s    | ]}t |tV  qd S N)r+   r-   ).0r2   r3   r3   r4   	<genexpr>F   s    z/reconstitute_message_content.<locals>.<genexpr>r)   )r.   )alljointypingcastlistr-   r+   r   lenr.   append)r6   r,   r2   r3   r3   r4   reconstitute_message_contentB   s   


rB   c                 C   s   t td| S )z-Recreates function call arguments from streamr)   )r=   r>   r$   r<   r6   r3   r3   r4   reconstitute_function_call_argsV   s   rD   received_event_trackerc                 C   s   g }t |  dd dD ]U\}}|d }t|tr<ttt |}|tt	dd |D d|j
|j|j|j|jd qt|trattt |}|t|jtd	d |D d|j
|j|jd
 q|S )z;Given a list of events, recreate the corresponding entries.c                 S   s   | d S )Nr   r3   )xr3   r3   r4   <lambda>`   s    z%reconstitue_entries.<locals>.<lambda>)keyr   c                 S      g | ]}|j qS r3   )r,   )r8   message_eventr3   r3   r4   
<listcomp>g   s    z'reconstitue_entries.<locals>.<listcomp>rC   )r,   
created_atidagent_idmodelrolec                 S   rI   r3   )	arguments)r8   function_call_eventr3   r3   r4   rK   x   s    )namerQ   rL   rM   tool_call_id)sorteditemsr+   r   r=   r>   r?   rA   r   rB   rL   rM   rN   rO   rP   r   r   rS   rD   rT   )rE   run_entriesidxeventsfirst_eventmessage_eventsfunction_call_eventsr3   r3   r4   reconstitue_entries[   sH   

r]   c                   @   s&   e Zd ZU eed< eed< eed< dS )RunFilesrM   rS   r,   N)__name__
__module____qualname__r-   __annotations__bytesr3   r3   r3   r4   r^      s   
 r^   c                   @   s   e Zd ZU ee ed< eddZee	 ed< eedZ
ee ed< eedZee	ef ed< eddZeee  ed< d	e	d
ee fddZed
ee fddZed
e	fddZed
efddZdS )	RunResultinput_entriesN)defaultconversation_id)default_factoryoutput_entriesfilesoutput_modelr/   r(   c                 C   s   | j |S r7   )rj   get)selfr/   r3   r3   r4   get_file   s   zRunResult.get_filec                 C   s   g | j | jS r7   )re   ri   rm   r3   r3   r4   entries   s   zRunResult.entriesc                 C   s$   | j stdddd | j D S )NzNo output entries were started.
c                 s   s"    | ]}|j d krt|V  qdS )zmessage.outputN)typer5   )r8   r'   r3   r3   r4   r9      s    
z+RunResult.output_as_text.<locals>.<genexpr>)ri   
ValueErrorr<   ro   r3   r3   r4   output_as_text   s
   
zRunResult.output_as_textc                 C   s&   | j d u r	tdtt| j| j S )NzNo output format was not set.)rk   rs   r   jsonloadsrt   ro   r3   r3   r4   output_as_model   s
   
zRunResult.output_as_model)r_   r`   ra   r?   r!   rb   r   rg   r   r-   ri   RunOutputEntriesdictrj   r^   rk   rr   r
   rn   property
RunEntriesrp   rt   rw   r3   r3   r3   r4   rd      s   
 	rd   c                   @   sr   e Zd ZU dZee ed< dZeed  ed< eed< eed< e	j	j
e	jjdZee	j	 ed< d	Zee ed
< dS )FunctionResultEventNrM   function.resultrr   resultrT   )tzrL   r   output_index)r_   r`   ra   rM   r   r-   rb   rr   r   datetimenowtimezoneutcrL   r   intr3   r3   r3   r4   r|      s   
 r|   r}   zagent.handoff.donezagent.handoff.startedzconversation.response.donezconversation.response.errorzconversation.response.startedzfunction.call.deltazmessage.output.deltaztool.execution.doneztool.execution.startedc                 C   s   t | ddS )Nrr   r%   )mr3   r3   r4   rG      s    rG   c                   @   s   e Zd ZU eed< eed< dS )RunResultEventseventdataN)r_   r`   ra   RunResultEventsTyperb   RunResultEventsDatar3   r3   r3   r4   r      s   
 r   )<r   ru   r=   r   r   r   r   dataclassesr   r   pydanticr   r	   r
   %mistralai.extra.utils.response_formatr   mistralai.modelsr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r#   r$   mistralai.utilsr&   rx   r{   r-   r5   r?   rB   rD   ry   r   r]   r^   rd   r|   r   r   r   r3   r3   r3   r4   <module>   sl    l

*!
