o
    
۾i                  
   @   s  d dl 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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 e  e!Z"G dd dZ#dedeegef de$e dede#f
ddZ%dS )    N)Callable)ResponseFunctionToolCallResponseOutputItem)"ResponseFunctionToolCallOutputItem)McpCall)ResponseOutputMessage)ResponseOutputText)ContentResponseReasoningItem)
MCP_PREFIX)ResponseInputOutputItemResponsesRequest)CompletionOutput)ReasoningParser)TokenizerLike)
ToolParser)random_uuidc                   @   sn   e Zd ZdZdedeegef dee de	deege
f dB f
dd	Zd
edd fddZdee fddZdS )ResponsesParserzAIncremental parser over completion tokens with reasoning support.	tokenizerreasoning_parser_clsresponse_messagesrequesttool_parser_clsNc                C   sH   || _ t|| _|| _|| _||| _d | _|d ur||| _d | _d S )N)r   lennum_init_messagesr   r   reasoning_parser_instancetool_parser_instancefinish_reason)selfr   r   r   r   r    r   c/home/ubuntu/.local/lib/python3.10/site-packages/vllm/entrypoints/openai/parser/responses_parser.py__init__#   s   



zResponsesParser.__init__outputreturnc                 C   s  |j | _ | jj|j| jd\}}|r'| jtddt  g t	d|dgd g }| j
d ur\| j
j|d ur7|nd| jd}|d ur\|jr\|dd	 |jD  |j}|r\| dkr\d }|rv| jtd
dt  ddtg d|d dgd t|dkr| j| | S )N)r   	reasoningrs_reasoning_text)typetext)r'   idsummarycontent c              	   s   s<    | ]}t d t  dt  dd|jj|jjdV  qdS )fc_call_function_call	completed)r)   call_idr'   statusname	argumentsN)r   r   functionr3   r4   ).0	tool_callr   r   r    	<genexpr>Z   s    	


z*ResponsesParser.process.<locals>.<genexpr>messagemsg_r0   	assistantoutput_text)annotationsr'   r(   logprobs)r'   r)   r2   roler+   r   )r   r   extract_reasoningr(   r   r   appendr
   r   r	   r   extract_tool_callstools_calledextend
tool_callsr+   stripr   r   r   )r   r"   reasoning_contentr+   function_callstool_call_infor   r   r    process<   sb   



	
zResponsesParser.processc              
   C   s   | j | jd }g }|D ]<}t|ts|| qt|dkr#tdt|d trHtt	 t
  |d j|d j|d jdd|jd}||d< q|S )z:Given a list of sentences, construct ResponseOutput Items.Nr   z=Cannot have a FunctionToolCallOutput before FunctionToolCall.mcp_callr0   )r)   r4   r3   server_labelr'   r2   r"   )r   r   
isinstancer   rA   r   
ValueErrorr   r   r   r   r4   r3   r"   )r   r   output_messagesr9   mcp_messager   r   r    0make_response_output_items_from_parsable_context   s2   
z@ResponsesParser.make_response_output_items_from_parsable_context)__name__
__module____qualname____doc__r   r   r   listr   r   r   r!   r   rJ   r   rR   r   r   r   r    r       s"    
Cr   r   r   r   r   r#   c                 C   s   t | ||||dS )zFactory function to create a ResponsesParser with
    optional reasoning parser.

    Returns:
        ResponsesParser instance configured with the provided parser
    r   r   r   r   r   )r   rX   r   r   r    'get_responses_parser_for_simple_context   s   rY   )&loggingcollections.abcr   openai.types.responsesr   r   >openai.types.responses.response_function_tool_call_output_itemr   +openai.types.responses.response_output_itemr   .openai.types.responses.response_output_messager   +openai.types.responses.response_output_textr   .openai.types.responses.response_reasoning_itemr	   r
   vllm.entrypoints.constantsr   *vllm.entrypoints.openai.responses.protocolr   r   vllm.outputsr   $vllm.reasoning.abs_reasoning_parsersr   vllm.tokenizersr   &vllm.tool_parsers.abstract_tool_parserr   
vllm.utilsr   	getLoggerrS   loggerr   rW   rY   r   r   r   r    <module>   s8   
