o
    پiI                   
   @   s   d dl Z d dlmZmZ d dlmZ d dlZe eZ	zd dl
mZ W n ey7 Z zeZ
W Y dZ[ndZ[ww d dlmZmZmZmZmZ d dlmZmZmZ d dlmZ G dd	 d	eZG d
d deZG dd deZG dd deZdS )    N)ABCabstractmethod)Union)ClientSession)AuthorMessageRoleStreamStateTextContent)get_encoding#get_streamable_parser_for_assistantrender_for_completion)Toolc                   @   sX   e Zd ZedddZedee fddZedefddZ	edee
 fd	d
ZdS )ConversationContextreturnNc                 C      d S N selfoutputr   r   R/home/ubuntu/.local/lib/python3.10/site-packages/sglang/srt/entrypoints/context.pyappend_output      z!ConversationContext.append_outputc                    s   d S r   r   r   r   r   r   	call_tool    s   zConversationContext.call_toolc                 C   r   r   r   r   r   r   r   need_builtin_tool_call$   r   z*ConversationContext.need_builtin_tool_callc                 C   r   r   r   r   r   r   r   r   (   r   z)ConversationContext.render_for_completionr   N)__name__
__module____qualname__r   r   listr   r   boolr   intr   r   r   r   r   r      s    r   c                   @   sP   e Zd Zdd ZdddZdefddZdee fd	d
Z	dee
 fddZdS )SimpleContextc                 C   s
   d | _ d S r   last_outputr   r   r   r   __init__/      
zSimpleContext.__init__r   Nc                 C   s
   || _ d S r   r%   r   r   r   r   r   2   r(   zSimpleContext.append_outputc                 C   s   dS )NFr   r   r   r   r   r   5   s   z$SimpleContext.need_builtin_tool_callc                    s
   t dNzShould not be called.NotImplementedErrorr   r   r   r   r   8   s   zSimpleContext.call_toolc                 C   s   t dr)   r*   r   r   r   r   r   ;   s   z#SimpleContext.render_for_completionr   )r   r   r    r'   r   r"   r   r!   r   r   r#   r   r   r   r   r   r$   -   s    
r$   c                   @   s   e Zd Zdedeeedef f fddZddd	Z	e
defd
dZdefddZdee fddZdee fddZdedef dedee fddZdedef dedee fddZdS )HarmonyContextmessagestool_sessionsr   c                 C   s:   || _ || _t | _t|| _d| _d| _d| _d| _	d S Nr   )
	_messagesr.   r   parserlennum_init_messagesnum_prompt_tokensnum_cached_tokensnum_output_tokensnum_reasoning_tokens)r   r-   r.   r   r   r   r'   A   s   

zHarmonyContext.__init__r   Nc                 C   s   t |trEd|v rE|d }|D ]}| j| q| jj}|d }t |trDd|v r.|d | _d|v r7|d | _d|v rD|  j|d 7  _n|}| j	| d S )N
output_ids	meta_infoprompt_token_idsprompt_tokenscached_tokenscompletion_tokens)

isinstancedictr1   processr-   r4   r5   r6   r0   extend)r   r   output_token_idstoken_idoutput_msgsr9   r   r   r   r   S   s    


zHarmonyContext.append_outputc                 C   s   | j S r   )r0   r   r   r   r   r-   j   s   zHarmonyContext.messagesc                 C   s6   | j sdS | j d }|j}|d uo|dp|dS )NFbrowser.python)r-   	recipient
startswithr   last_msgrH   r   r   r   r   n   s   
z%HarmonyContext.need_builtin_tool_callc                    sp   | j sg S | j d }|j}|d ur4|dr#| | jd |I d H S |dr4| | jd |I d H S td)NrE   rF   browserrG   zNo tool call found)r-   rH   rI   call_search_toolr.   call_python_tool
ValueErrorrJ   r   r   r   r   w   s   






zHarmonyContext.call_toolc                 C   s
   t | jS r   )r   r-   r   r   r   r   r      r(   z$HarmonyContext.render_for_completiontool_sessionrK   c           	         s   t |tr|| I d H S |jdd }t|jd j}|	||I d H }|jd j}t
|d}ttj|jd}t||gtjdgS )N.   r   textrolename)authorcontentrH   )r>   r   
get_resultrH   splitorjsonloadsrY   rT   r   r
   r   r   TOOLr   	ASSISTANT)	r   rP   rK   	tool_nameargsresult
result_strrY   rX   r   r   r   rM      s   

zHarmonyContext.call_search_toolc                    sz   t |tr|| I d H S d|jd ji}|d|I d H }|jd j}t|d}ttj	dd}t
||g|jtjdgS )Ncoder   rG   rS   rU   )rX   rY   channelrH   )r>   r   rZ   rY   rT   r   r
   r   r   r^   r   re   r_   )r   rP   rK   paramrb   rc   rY   rX   r   r   r   rN      s    

zHarmonyContext.call_python_toolr   )r   r   r    r!   r?   strr   r   r'   r   propertyr-   r"   r   r   r   r#   r   rM   rN   r   r   r   r   r,   ?   s4    

	


r,   c                       sj   e Zd Z fddZedefddZdddZdefd	d
Z	defddZ
dee f fddZ  ZS )StreamingHarmonyContextc                    s8   t  j|i | d | _t | _t | _d | _d| _d S r/   )	superr'   r&   r   r1   r   encodinglast_toknum_processed_tokens)r   ra   kwargs	__class__r   r   r'      s   
z StreamingHarmonyContext.__init__r   c                 C   s   | j jS r   )r1   r-   r   r   r   r   r-      s   z StreamingHarmonyContext.messagesNc           
      C   s   t |trGd|v rG|d }|di }|d}|d ur/t||kr/|| jd  }t|| _n|}|  jt|7  _|D ]}| j| q<d S t|dksQJ d|d }|jjt	j
krd|jd u rdd|_| j|}|D ]}	| j|	 ql|d | _d S )	Nr8   r9   r=   rR   z&Tool output should be a single messager   	assistantrE   )r>   r?   getr2   rm   r1   r@   rX   rV   r   r^   rH   rk   renderrl   )
r   r   rB   r9   r=   new_token_idsrC   msgtokstokr   r   r   r      s*   
z%StreamingHarmonyContext.append_outputc                 C   s   | j jtjkS r   )r1   stater	   EXPECT_STARTr   r   r   r   is_expecting_start   s   z*StreamingHarmonyContext.is_expecting_startc                 C   s   | j | j v S r   )rl   rk   !stop_tokens_for_assistant_actionsr   r   r   r   is_assistant_action_turn   s   z0StreamingHarmonyContext.is_assistant_action_turnc                    sb   t   }d}g }|| | jkr"|||  |d8 }|| | jkst|D ]}| j| q&|S )NrE   rR   )rj   r   rl   appendreversedr1   r@   )r   rendered_tokenslast_n
to_processrw   ro   r   r   r      s   
z-StreamingHarmonyContext.render_for_completionr   )r   r   r    r'   rh   r!   r-   r   r"   rz   r|   r#   r   __classcell__r   r   ro   r   ri      s    	
'ri   )loggingabcr   r   typingr   r\   	getLoggerr   loggermcpr   ImportErroreopenai_harmonyr   r   r   r	   r
   $sglang.srt.entrypoints.harmony_utilsr   r   r   sglang.srt.entrypoints.toolr   r   r$   r,   ri   r   r   r   r   <module>   s$   
p