
    .`iu                        U d dl Z d dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZm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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' d dlm!Z( d dl)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 e j2        e j3        e j4        dZ5da6h dZ7e8e9         e:d<   de8e9         de;fdZ<d Z=	 	 	 	 	 	 	 	 dFde9dz  ded         dz  de9dz  de9dz  de9dz  de9dz  de9dz  de;defd Z>d!e,ez  fd"Z?	 	 dGde9dz  d#e@ee,z           dz  defd$ZAd%e9defd&ZBd'e.d(e@e	ez           defd)ZCd*e@de@e         fd+ZDd,e@e         de@e         fd-ZEd%e9e@z  dz  de9dz  fd.ZF	 dHd/eGe9e9f         dz  de@e         fd0ZHde@e         fd1ZId2e/de@e'         fd3ZJd4e@e         de@eK         fd5ZLd6ed7e9de	fd8ZMd6ed7e9de@e	         fd9ZNd6ede@e	         fd:ZOd6ede	fd;ZPd7e9deQe9e9f         fd<ZRd6ed7e9de@e	         fd=ZSd6ede@e	         fd>ZTd?e"de@e	         fd@ZUde@eK         fdAZVde"fdBZWdCeeK         de"fdDZXdCeeK         deQe9dz  e9dz  e;f         fdEZYdS )I    N)IterableSequence)Literal)ResponseFunctionToolCallResponseOutputItemResponseOutputMessageResponseOutputTextResponseReasoningItem)
ActionFindActionOpenPageActionSearchResponseFunctionWebSearch)McpCall)Content)Tool)AuthorChannelConfigConversationDeveloperContentHarmonyEncodingNameMessageReasoningEffortRoleStreamableParserSystemContentTextContentToolDescriptionload_harmony_encoding)r   )r   )envs)ChatCompletionToolsParam)ResponseInputOutputItemResponsesRequest)random_uuid)highmediumlow>   	containercode_interpreterweb_search_previewMCP_BUILTIN_TOOLS
tool_typesreturnc                 8    |                      t                     S )zj
    Checks if the given tool types are custom tools
    (i.e. any tool other than MCP buildin tools)
    )issubsetr*   )r+   s    /home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/openai/parser/harmony_utils.pyhas_custom_toolsr0   H   s    
 ""#45555    c                  P    t           t          t          j                  a t           S N)_harmony_encodingr   r   HARMONY_GPT_OSS r1   r/   get_encodingr7   P   s      12E2UVVr1   Fmodel_identityreasoning_effort
start_datebrowser_descriptionpython_descriptioncontainer_descriptioninstructionswith_custom_toolsc                    t          j                    }| |                    |           }|3t          j        r'|j        }	|	r|	 d| n|}
|                    |
          }| |                    t          |                   }|1t          j        	                                
                    d          }|                    |          }||                    |          }||                    |          }||                    |          }|sC|j        }dt          j        fd|j        D                       }|                    |          }t%          j        t(          j        |          }|S )N
z%Y-%m-%d
commentaryc                      g | ]
}|k    |S r6   r6   ).0cinvalid_channels     r/   
<listcomp>z&get_system_message.<locals>.<listcomp>|   s#    NNN1o9M9MQ9M9M9Mr1   )r   newwith_model_identityr   (VLLM_GPT_OSS_HARMONY_SYSTEM_INSTRUCTIONSr8   with_reasoning_effortREASONING_EFFORTdatetimenowstrftimewith_conversation_start_date
with_toolschannel_configr   require_channelsvalid_channelswith_channel_configr   from_role_and_contentr   SYSTEM)r8   r9   r:   r;   r<   r=   r>   r?   sys_msg_contentcurrent_identitynew_identityrR   
new_configsys_msgrF   s                 @r/   get_system_messager]   W   s    $'))O!)==nMMD$Q*95EW11<111< 	 *==lKK#)??-.
 
 &**,,55jAA
%BB:NNO&)445HII%)445GHH()445JKK J(7&"3NNNN5NNN
 

 *==jII+DKIIGNr1   toolc                     t          | t                    r5t          j        | j        j        | j        j        | j        j                  S t          j        | j        | j        | j                  S )N)namedescription
parameters)
isinstancer    r   rH   functionr`   ra   rb   )r^   s    r/   create_tool_definitionre      ss    $011 
"#1}/
 
 
 	

 Y$?   r1   toolsc                    t          j                    }| !t          j        s|                    |           }|lg }|D ]D}|j        dv r|j        dk    r|                    |           -t          d|j         d          |r!d |D             }|                    |          }t          j
        t          j        |          }|S )N)r)   r(   r'   rd   z
tool type z not supportedc                 ,    g | ]}t          |          S r6   )re   )rD   r^   s     r/   rG   z)get_developer_message.<locals>.<listcomp>   s.     * * *15&t,,* * *r1   )r   rH   r   rJ   with_instructionstypeappend
ValueErrorwith_function_toolsr   rV   r   	DEVELOPER)r>   rf   dev_msg_contentfunction_toolsr^   function_tool_descriptionsdev_msgs          r/   get_developer_messagers      s    '*,,O(U);;LII@B 	I 	IDy   
 j((%%d++++ !Gdi!G!G!GHHH 	* *9G* * *& .AA* O +DNOLLGNr1   contentc                 @    t          j        t          j        |           S r3   )r   rV   r   USERrt   s    r/   get_user_messagerx      s    (G<<<r1   response_msgprev_responsesc                   	 t          | t                    s|                                 } d| vs| d         dk    r| d         }| d         }|dk    rdnd	t          |t                    rt	          j        |	|z             }n#	fd|D             }t	          j        ||          }|d	k    r|                    d
          }n| d         dk    r| d         }d }t          |          D ]&}t          |t                    r|j
        |k    r|} n'|t          d|           t	          j        t          j        t          j        d|j                   | d                   }n| d         dk    rI| d         }t%          |          dk    sJ t	          j        t          j        |d         d                   }n| d         dk    rnt	          j        t          j        | d                   }|                    d          }|                    d| d                    }|                    d          }nt          d| d                    |S )Nrj   messagerolert   	developerzInstructions:
 c                 B    g | ]}t          |d          z             S )textr   )r   )rD   rE   text_prefixs     r/   rG   z(parse_response_input.<locals>.<listcomp>   s-    SSSaqy)@AAASSSr1   	assistantfinalfunction_call_outputcall_idzNo call message found for 
functions.output	reasoning   r   r   function_call	argumentsrB   r`   jsonzUnknown input type: )rc   dict
model_dumpstrr   rV   from_role_and_contentswith_channelreversedr   r   rl   from_author_and_contentr   rH   r   TOOLr`   len	ASSISTANTwith_recipientwith_content_type)
ry   rz   r}   rt   msgcontentsr   call_responseprev_responser   s
            @r/   parse_response_inputr      s    lD)) 1#..00\!!\&%9Y%F%FF#y) ,0;+>+>''Bgs## 	A/kG6KLLCCSSSS7SSSH0x@@C;""7++C	f	!7	7	7y)9=%n55 	 	M=*BCC!)W44 - C'CCDDD-Jty"C}/A"C"CDD"
 
 
f		,	,y)7||q    +DNGAJv<NOO	f		0	0+DNL<UVV|,,  !Dl6.B!D!DEE##F++FV0DFFGGGJr1   	chat_msgsc                 6   g }i }| D ]Z}|                     dg           D ]A}|                     di                                d          ||                     d          <   B[| D ]%}|                    t          ||                     &t          |          }|S )zl
    Parse a list of messages from request.messages in the Chat Completion API to
    Harmony messages.
    
tool_callsrd   r`   id)getextend#parse_chat_input_to_harmony_messageauto_drop_analysis_messages)r   msgstool_id_nameschat_msg	tool_calls        r/   %parse_chat_inputs_to_harmony_messagesr      s    
 D$&M   !lB77 	 	I1:z21N1N1R1R2 2M)----..	
  R R7-PPQQQQ&t,,DKr1   r   c                    d}t          t          |           dz
  dd          D ])}| |         }|j        j        dk    r|j        dk    r|} n*g }t          |           D ],\  }}||k     r|j        dk    r|                    |           -|S )a  
    Harmony models expect the analysis messages (representing raw chain of thought) to
    be dropped after an assistant message to the final channel is produced from the
    reasoning of those messages.

    The openai-harmony library does this if the very last assistant message is to the
    final channel, but it does not handle the case where we're in longer multi-turn
    conversations and the client gave us reasoning content from previous turns of
    the conversation with multiple assistant messages to the final channel in the
    conversation.

    So, we find the index of the last assistant message to the final channel and drop
    all analysis messages that precede it, leaving only the analysis messages that
    are relevant to the current part of the conversation.
    r   r   r   analysis)ranger   authorr}   channel	enumeraterk   )r   last_assistant_final_indexir   cleaned_msgss        r/   r   r      s      "$3t99q="b))  1g:?k))ckW.D.D)*&E"$LD// ! !3)))ckZ.G.GC    r1   c                 n    t          | t                    rd                    d | D                       S | S )zm
    Extract the text parts from a chat message content field and flatten them
    into a single string.
    r   c              3      K   | ]H}t          |t                    r1|                    d           dk    0|                    dd          V  IdS )rj   r   r   N)rc   r   r   )rD   items     r/   	<genexpr>z,flatten_chat_text_content.<locals>.<genexpr>"  sg       
 
$%%
 +/((6*:*:f*D*D HHVR  *D*D*D*D
 
r1   )rc   listjoinrw   s    r/   flatten_chat_text_contentr     sO    
 '4   
ww 
 

 
 
 
 
 	

 Nr1   r   c                 "   |pi }t          | t                    s|                     d          } |                     d          }g }|                     dg           }|dk    r|rt	          |                     d                    }|rIt          j        t          j        |          }|	                    d          }|
                    |           |                     d          p|                     d	          }|rIt          j        t          j        |          }|	                    d
          }|
                    |           |D ]}	|	                    di           }
|
                    dd          }|
                    dd          pd}t          j        t          j        |          }|	                    d          }|                    d|           }|                    d          }|
                    |           |S |dk    r|                     dd          }|                    |d          }|                     dd          pd}t	          |          }t          j        t          j        t          j        d|           |          	                    d                              d          }|gS |                     d          p|                     d	          }|dk    rK|rIt          j        t          j        |          }|	                    d
          }|
                    |           |                     d          pd}|d}t          |t"                    rt%          |          g}nd |D             }|dk    rO|rM|d         j        r@t          j        ||          }|	                    d          }|
                    |           n0|dk    r*t          j        ||          }|
                    |           |S )zc
    Parse a message from request.messages in the Chat Completion API to
    Harmony messages.
    Texclude_noner}   r   r   rt   rB   r   reasoning_contentr   rd   r`   r   r   r   r   r^   tool_call_idNr   c                 V    g | ]&}t          |                    d d                    'S r   r   r   r   r   rD   rE   s     r/   rG   z7parse_chat_input_to_harmony_message.<locals>.<listcomp>{  1    IIIAKQUU62%6%6777IIIr1   r   r   )rc   r   r   r   r   r   rV   r   r   r   rk   r   r   r   r   rH   r   r   r   r   r   )r   r   r}   r   r   rt   commentary_msgr   analysis_msgcallfuncr`   r   r   r   r   s                   r/   r   r   *  s    "'RMh%% :&&D&99<<DD lB//J{z+HLL,C,CDD 	($:4>7SSN+88FFNKK'''$LL55 
:
 :
  	&"8 1 L (44Z@@LKK%%% 	 	D88J++D88FB''Db117RI/	JJC""<00C$$%8$%8%899C ''//CKK v~~||NB77  r22,,y"--3+G44 +
49&94&9&9::G  \,''^K(( 	 u ![11VX\\BU5V5V{04T^EVWW#00<<L!!! ll9%%+G'3 JW---. JIIII {xHQK,<,T8<<w''C			,T8<<CKr1   c                     t          | t                    s|                     d          } |                     d          }|                     d          }|dk    r|rg }|D ]}|                    di           }|                    dd          }|                    d	d          pd}t	          j        t          j        |          }|                    d
          }|	                    d|           }|
                    d          }|                    |           |S |dk    r|                     dd          }|                     dd          pd}	t          |	          }	t	          j        t          j        t          j        d|           |	                              d
          }|gS |                     dd          }	t          |	t"                    rt%          |	          g}
nd |	D             }
t	          j        ||
          }|gS )zm
    Parse a message from request.previous_input_messages in the Responsees API to
    Harmony messages.
    Tr   r}   r   r   rd   r`   r   r   rB   r   r   r^   rt   r   c                 V    g | ]&}t          |                    d d                    'S r   r   r   s     r/   rG   z2parse_input_to_harmony_message.<locals>.<listcomp>  r   r1   )rc   r   r   r   r   rV   r   r   r   r   r   rk   r   r   r   rH   r   r   r   r   )r   r}   r   r   r   r   r`   r   r   rt   r   s              r/   parse_input_to_harmony_messager     s   
 h%% :&&D&99<<D l++J{z  	 	D88J++D88FB''Db117RI/	JJC""<00C$$%8$%8%899C''//CKK v~~||FB'',,y"--3+G44-Jty"5t"5"566
 

,|
$
$ 	 u ll9b))G'3 JW---. JIIII

(x
8
8C5Lr1   requestc                    g }| j         r| j         D ]}t          |t                    rC|j        j        }|t
          j        k    s|t
          j        k    rD|                    |           Zt          |          }|D ]D}|j        j        }|t
          j        k    s|t
          j        k    r/|                    |           E|S r3   )
previous_input_messagesrc   OpenAIHarmonyMessager   r}   OpenAIHarmonyRolerW   rn   rk   r   )r   messagesr|   message_roleharmony_messagesharmony_msgs         r/   )construct_harmony_previous_input_messagesr     s     ,.H& 16 	1 	1G'#788 1&~2
 !$5$<<<#'8'BBB((((#A'#J#J #3 
1 
1K#.#5#:L
 %(9(@@@'+<+FFF OOK0000
1 Or1   r   c                     t          j        |           }t                                          |t          j                  }|S r3   )r   from_messagesr7   "render_conversation_for_completionr   r   )r   conversation	token_idss      r/   render_for_completionr     s:    -h77LAAdn I r1   r|   	recipientc                    t          | j                  dk    rt          d          | j        d         }	 t          j        |j                  }n%# t          j        $ r d|j         }|||d}Y nw xY w|dk    r)t          d|                    dd	           d
          }n|dk    r)t          d|                    dd	           d          }nV|dk    r>t          |                    dd	          d|                    dd	           d          }nt          d|           t          dt                       |dd          S )zDParse browser tool calls (search, open, find) into web search items.r   z-Invalid number of contents in browser messager   z'Invalid JSON args, caught and retried: )queryurlpatternzbrowser.searchzcursor:r   r   search)r   rj   zbrowser.openr   	open_page)r   rj   zbrowser.findr   find)r   r   rj   zUnknown browser action: ws_	completedweb_search_call)r   actionstatusrj   )r   rt   rl   r   loadsr   JSONDecodeErrorr   r   r   r   r   r#   )r|   r   rt   browser_calljson_retry_output_messager   s         r/   _parse_browser_tool_callr     s   
7?q  HIIIoa G

z',// 
 
 
DglDD 	" /,0
 
	
 $$$;L,,Wb99;;(
 
 
 
n	$	$7,**5"5577k
 
 
 
n	$	$ $$Y337,**5"5577
 
 
 ?I??@@@$   	   s   A A21A2c           	          |                     d          d         }g }| j        D ]D}t                      }t          |j        d| d|d|           }|                    |           E|S )z3Parse function calls into function tool call items..r   call_r   fc_)r   r   rj   r`   r   )splitrt   r#   r   r   rk   )r|   r   function_nameoutput_itemsrt   	random_idresponse_items          r/   _parse_function_callr     s    OOC((,ML? 	+ 	+MM	0l'I''  Y  
 
 
 	M****r1   c           
          g }| j         D ]O}t          dt                       g dt          |j        d          gd          }|                    |           P|S )z6Parse reasoning/analysis content into reasoning items.rs_r   reasoning_textr   rj   Nr   summaryrj   rt   r   )rt   r
   r#   ResponseReasoningTextContentr   rk   )r|   r   rt   reasoning_items       r/   _parse_reasoning_contentr  $  s}    L? 
, 
,.$[]]$$,',EUVVV 
 
 
 	N++++r1   c                     g }| j         D ]/}t          |j        g dd          }|                    |           0t	          dt                       || j        j        dd          S )z7Parse final channel messages into output message items.output_textNr   annotationsrj   logprobsmsg_r   r|   r   rt   r}   r   rj   )rt   r	   r   rk   r   r#   r   r}   )r|   r   rt   r  s       r/   _parse_final_messager  5  s    H? % %(	
 
 
 	$$$$ !+--!!^    r1   c                     d| v r7|                      d          d         }|                      d          d         }n| }| }||fS )aM  
    Parse MCP recipient into (server_label, tool_name).

    For dotted recipients like "repo_browser.list":
        - server_label: "repo_browser" (namespace/server)
        - tool_name: "list" (specific tool)

    For simple recipients like "filesystem":
        - server_label: "filesystem"
        - tool_name: "filesystem"
    r   r   r   )r   )r   server_label	tool_names      r/   _parse_mcp_recipientr  I  sR     i s++A.OOC((,		 	""r1   c           
          t          |          \  }}g }| j        D ]@}t          |j        d||dt	                       d          }|                    |           A|S )z$Parse MCP calls into MCP call items.mcp_callmcp_r   r   rj   r`   r  r   r   )r  rt   r   r   r#   rk   )r|   r   r  r  r   rt   r   s          r/   _parse_mcp_callr  ^  s}    29==L)L? 	+ 	+l%%kmm%%
 
 
 	M****r1   c                 6   | j         j        dk    rg S g }| j        }||                    d          r%|                    t          | |                     nA| j        dk    r9|                    d          r$|                    t          | |                     n|                    d          r#|                    t          |                      n|                    t          | |                     n| j        dk    r#|                    t          |                      ns| j        dk    r#|                    t          |                      nE| j        dk    r#|                    t          |                      nt          d	| j                   |S )
zG
    Parse a Harmony message into a list of output response items.
    r   Nbrowser.rB   r   )pythonbrowserr'   r   r   zUnknown channel: )r   r}   r   
startswithrk   r   r   r   r   r  r  r  rl   )r|   r   r   s      r/   parse_output_messager  o  s    ~k)) 	-/L!I
++ 	E 8) L LMMMM _,,1E1El1S1S, 4Wi H HIIII !!"DEE 	E  8 A ABBBB  C CDDDD 
J	&	&4W==>>>>	L	(	( 	4W==>>>>	G	#	#099:::: >W_>>???r1   parserc           	         | j         sg S | j        t          j        k    rg S | j        }||                    d          rg S |r| j        dv r|                    d          rHt                      }t          | j         d| d|	                    d          d         d| d	
          gS |                    d          sh|                    d          sS|                    d          s>t                      }t          |          \  }}t          | j         d||d| d	          gS | j        dk    r9t          dt                       g dt          | j         d          gd           gS | j        dk    r9t          dt                       g dt          | j         d          gd           gS | j        dk    r?t          | j         g dd           }t          dt                       |gddd          }|gS g S ) Nr  )rB   r   r   r   r   r   r   r   in_progress)r   r   rj   r`   r   r   r  r  r'   r  r  r  rB   r   r   r   r   r   r   r   r  r  r	  r   
incompleter|   r
  )current_contentcurrent_roler   r   current_recipientr  current_channelr#   r   r   r  r   r
   r  r	   r   )r  r!  ridr  r  r  	text_items          r/   parse_remaining_stater%    s   ! 	dn,,	0$):)E)Ej)Q)Q$	 V37QQQ''55 	--C($4)CMM(*0055b9"S{{(  	 	 ((22	 ++I66	 !++K88	 --C&:;L&M&M#L)$4#"!-#c||(  	 	 --!((( 0#3:J  
 
 
 

 	
 ++!((( 0#3:J  
 
 
 

 	
 ((('	
 
 
 *%kmm%% M  
 
 
	 {Ir1   c                  B    t                                                      S r3   )r7   !stop_tokens_for_assistant_actionsr6   r1   r/   %get_stop_tokens_for_assistant_actionsr(    s    >>;;===r1   c                  P    t          t                      t          j                  S )N)r}   )r   r7   r   r   r6   r1   r/   #get_streamable_parser_for_assistantr*    s    LNN@@@@r1   r   c                 V    t                      }| D ]}|                    |           |S r3   )r*  process)r   r  token_ids      r/   parse_output_into_messagesr.    s6    022F ! !x    Mr1   c                    t          |           }|j        }d}d |D             }d |D             }|j        dk    r"|j        r|                    |j                   n,|j        dk    r!|j        r|                    |j                   d                    |          }d                    |          }|pd}|pd}|||fS )a  
    Parse the output of a Harmony chat completion into reasoning and final content.
    Note that when the `openai` tool parser is used, serving_chat only uses this
    for the reasoning content and gets the final content from the tool call parser.

    When the `openai` tool parser is not enabled, or when `GptOssReasoningParser` is
    in use,this needs to return the final content without any tool calls parsed.

    Empty reasoning or final content is returned as None instead of an empty string.
    Fc                 H    g | ]}|j         d k    |j        d         j         S r   r   r   rt   r   rD   r   s     r/   rG   z%parse_chat_output.<locals>.<listcomp>  s4        #ckZ6O6OA6O6O6Or1   c                 H    g | ]}|j         d k    |j        d         j         S r1  r2  r3  s     r/   rG   z%parse_chat_output.<locals>.<listcomp>"  s4        #ckZ6O6OA6O6O6Or1   r   rA   N)r.  r   r"  r  rk   r   )r   r  output_msgsis_tool_callreasoning_textsfinal_textsr   final_contents           r/   parse_chat_outputr:    s     (	22F/KL '2  O '2  K
 ++0F+v56666		:	-	-&2H	-61222 !IIo66I $		+ 6 6M !TI!)TMm\11r1   )NNNNNNNF)NNr3   )ZrM   r   collections.abcr   r   typingr   openai.types.responsesr   r   r   r	   r
   3openai.types.responses.response_function_web_searchr   r   r   r   +openai.types.responses.response_output_itemr   .openai.types.responses.response_reasoning_itemr   r  openai.types.responses.toolr   openai_harmonyr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   vllmr   0vllm.entrypoints.openai.chat_completion.protocolr    *vllm.entrypoints.openai.responses.protocolr!   r"   
vllm.utilsr#   HIGHMEDIUMLOWrL   r4   r*   setr   __annotations__boolr0   r7   r]   re   r   rs   rx   r   r   r   r   r   r   r   r   intr   r   r   r  r  tupler  r  r  r%  r(  r*  r.  r:  r6   r1   r/   <module>rO     sj     . . . . . . . .                               @ ? ? ? ? ?      - , , , , ,                              ; : : : : : 4 4 4 4 4 4       U U U U U U        # " " " " "  $       3s8   6S 6d 6 6 6 6   "&@D!&*%)(,##) )$J)56=) d
) t	)
 d
) :) *) ) ) ) ) )X!9D!@      $:> *//047    @=c =g = = = =.).+.CCD. . . . .bT d7m    ,d7m W    @sTzD'8 S4Z     6:_ _!#s(^d2_	']_ _ _ _D-W - - - -`	
   DDM d3i    *g *# *BT * * * *Z' c dCU>V    "g $7I2J    "' .@    (#C #E#s(O # # # #*W  >P9Q    "/' /d3E.F / / / /dZ"2 Zt<N7O Z Z Z Zz>tCy > > > >A-= A A A A(3- <L    '2}'2
3:sTz4'('2 '2 '2 '2 '2 '2r1   