
    .`i#              
          d dl mZ d dlmZmZmZ d dlmZ d dl	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 d d
lmZ d dlmZ d dlmZ d dlmZ deee         z  de fdZ!dddddedz  deee         z  dee         dz  dee         dz  fdZ"dedee         dedz  fdZ#dee         dee         fdZ$dedefdZ%dee         de&e         fdZ'de(de(fd Z)dee         d!edee(eef                  dz  fd"Z*dS )#    )Any)#ChatCompletionAssistantMessageParam"ChatCompletionMessageToolCallParamChatCompletionToolMessageParam)Function)ResponseFunctionToolCallResponseOutputItem)
ToolChoice)"ResponseFunctionToolCallOutputItem)ResponseOutputMessage)ResponseReasoningItem)Tool)envs)
MCP_PREFIX)ChatCompletionMessageParam)ResponseInputOutputItemrequest_inputreturnc                 P   t          | t                    rdS | sdS | d         }t          |t                    r	|j        dv S t          |t                    r	|j        dv S t          |t
                    r1|                    dd          dvrdS |                    d          dv S dS )a  
    Determine if the last input message is a partial assistant message
    that should be continued rather than starting a new generation.

    This enables partial message completion similar to Anthropic's Messages API,
    where users can provide an incomplete assistant message and have the model
    continue from where it left off.

    A message is considered partial if:
    1. It's a ResponseOutputMessage or ResponseReasoningItem
    2. Its status is "in_progress" or "incomplete"

    Args:
        request_input: The input to the Responses API request

    Returns:
        True if the final message should be continued, False otherwise
    F)in_progress
incompletetypemessage)r   	reasoningstatus)
isinstancestrr   r   r   dictget)r   	last_items     {/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/entrypoints/openai/responses/utils.pyshould_continue_final_messager#      s    * -%% u ub!I )233 A#@@@ )233 A#@@@)T"" H==++3KKK5}}X&&*GGG5    N)request_instructionsprev_msgprev_response_outputr%   r&   r'   c                    g }| r|                     d| d           ||                    |           |A|D ]>}t          |t                    r'|j        D ]}|                     d|j        d            ?t          |t                    r|                     d|d           n$t          |          }|                    |           |S )Nsystemrolecontent	assistantuser)appendextendr   r   r,   textr   &construct_chat_messages_with_tool_call)r%   r   r&   r'   messagesoutput_itemr,   input_messagess           r"   construct_input_messagesr6   L   s    24H 
 / 	
 	
 	
 !!!'/ 		 		K+'<== *2  GOO$/'.|     -%% (MBBCCCC?NN'''Or$   itemr3   c                    t          | t                    r&| j        r| j                            t                    sdS t          |          dk    rdS |d         }|                    d          dk    r|                    d          dS t          | j        t          | j
        | j                  d	          g|d
<   |S )zMany models treat MCP calls and reasoning as a single message.
    This function checks if the last message is a reasoning message and
    the current message is a tool callNr   r   r+   r-   r   name	argumentsfunctionidr<   r   
tool_calls)r   r   r>   
startswithr   lenr    r   call_idFunctionCallToolr:   r;   )r7   r3   last_messages      r"   &_maybe_combine_reasoning_and_tool_callrE   w   s     	4122G Gz**
 t
8}}tB<L  K//[))5t 	+|%Y.   	
 	
 	
	"L r$   r5   c                     g }| D ]<}t          ||          }|||d<   |                    t          |                     =|S )zThis function wraps _construct_single_message_from_response_item
    Because some chatMessages come from multiple response items
    for example a reasoning item and a MCP tool call are two response items
    but are one chat message
    Nr   )rE   r/   ,_construct_single_message_from_response_item)r5   r3   r7   maybe_combined_messages       r"   r2   r2      s^     24H P P!Gh!W!W!-1HRLLOOHNNOOOOOr$   c           
      N   t          | t                    r@t          dt          | j        t          | j        | j                  d          g          S t          | t                    ryd}| j	        rt          d          t          | j                  dk    r| j        d	         j        }n1| j        r*t          | j                  dk    r| j        d	         j        }d|d
S t          | t                    rd| j        d	         j        dS t          | t                     rt#          d| j        | j                  S t          | t&                    rQ|                     d          dk    r8t#          d|                     d          |                     d                    S | S )Nr-   r9   r<   r=   )r+   r?    z#Encrypted content is not supported.   r   )r+   r   r*   tool)r+   r,   tool_call_idr   function_call_outputoutputrB   )r   r   r   r   rB   rC   r:   r;   r   encrypted_content
ValueErrorrA   summaryr1   r,   r   r   r   rO   r   r    )r7   reasoning_contents     r"   rG   rG      s    $011 ,
22|-!Y"&.   $  	
 
 
 	
 
D/	0	0 
! 	DBCCCt|!! $Q 4\ 	5c$,//144 $Q 4*
 
 	
 
D/	0	0 
|A+
 
 	
 
D<	=	= 
-K
 
 
 	

 
D$		 
DHHV$4$48N$N$N-HHX&&),,
 
 
 	

 Kr$   toolsc                     t                      }| D ]U}|j        dk    r.|j        t          j        v r|                    |j                   ;|                    |j                   V|S )z7
    Extracts the tool types from the given tools.
    mcp)setr   server_labelr   #VLLM_GPT_OSS_SYSTEM_TOOL_MCP_LABELSadd)rT   
tool_typesrL   s      r"   extract_tool_typesr\      so     55J & &9  D$LLLt0111NN49%%%%r$   rL   c                     d| dS )z
    Convert a flat tool schema:
        {"type": "function", "name": "...", "description": "...", "parameters": {...}}
    into:
        {"type": "function", "function": {...}}
    r<   )r   r<    )rL   s    r"   ,convert_tool_responses_to_completions_formatr_      s       r$   tool_choicec                 4    | |dk    rd }nd | D             }|S )Nnonec                 P    g | ]#}t          |                                          $S r^   )r_   
model_dump).0rL   s     r"   
<listcomp>z(construct_tool_dicts.<locals>.<listcomp>  s:     
 
 
 99J9JKK
 
 
r$   r^   )rT   r`   
tool_dictss      r"   construct_tool_dictsrh      s@     }..


 

 
 

 r$   )+typingr   openai.types.chatr   r   r   9openai.types.chat.chat_completion_message_tool_call_paramr   rC   openai.types.responsesr   r	   openai.types.responses.responser
   >openai.types.responses.response_function_tool_call_output_itemr   .openai.types.responses.response_output_messager   .openai.types.responses.response_reasoning_itemr   openai.types.responses.toolr   vllmr   vllm.entrypoints.constantsr   0vllm.entrypoints.openai.chat_completion.protocolr   *vllm.entrypoints.openai.responses.protocolr   r   listboolr#   r6   rE   r2   rG   rW   r\   r   r_   rh   r^   r$   r"   <module>rx      s                 
      P O O O O O O O 6 6 6 6 6 6      Q P P P P P P P P P P P , , , , , ,       1 1 1 1 1 1 W W W W W W N N N N N N,566,	, , , ,b (,8<<@( ( (*( 566( -.5	(
 12T9( ( ( (V
!-12L-M$&   D01	
$%   &0
!00 0 0 0fd4j SX    "
t 
 
 
 
 

:
$.
	$sCx.D 
 
 
 
 
 
r$   