
    .`i$                        d dl Z d dl mZmZ d dlmZ d dlZd dlmZmZ d dl	m
Z
 d dlmZ d dlmZmZ ded	ed
efdZded	ed
efdZdeded
efdZdeded
ee         fdZdeded
eeef         fdZded
efdZdeded
efdZde
ez  d
eeeeef         dz  f         fdZde
ez  d
efdZdee
ez           d
efdZdee
ez           d
efdZ d eez  ez  deeez           dz  d
eez  dz  fd!Z!dS )"    N)JSONDecodeErrorJSONDecoder)Any)FunctionToolToolChoiceFunction)Tool)Allow)"ChatCompletionNamedToolChoiceParamChatCompletionToolsParams1s2returnc                     d}t          t          |           t          |                    }t          d|          D ] }| |         ||         k    r|| |         z  }  |S )a  
    Finds a common prefix that is shared between two strings, if there is one.
    Order of arguments is NOT important.

    This function is provided as a UTILITY for extracting information from JSON
    generated by partial_json_parser, to help in ensuring that the right tokens
    are returned in streaming, so that close-quotes, close-brackets and
    close-braces are not returned prematurely.

    e.g. find_common_prefix('{"fruit": "ap"}', '{"fruit": "apple"}') ->
    '{"fruit": "ap'
     r   )minlenrange)r   r   prefix
min_lengthis        k/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/tool_parsers/utils.pyfind_common_prefixr      se     FSWWc"gg&&J1j!!  a5BqE>>beOFFM    c                    d}t          t          |           t          |                    }t          d|dz             D ]>}| |          ||          k    r(| |                                          s| |          |z   }> |S )a  
    Finds a common suffix shared between two strings, if there is one. Order of
    arguments is NOT important.
    Stops when the suffix ends OR it hits an alphanumeric character

    e.g. find_common_suffix('{"fruit": "ap"}', '{"fruit": "apple"}') -> '"}'
    r      )r   r   r   isalnum)r   r   suffixr   r   s        r   find_common_suffixr   -   s     FSWWc"gg&&J1j1n%%  qb6RVBrFNN$4$4Vf_FFMr   curroldc                 |   t          | |          }|ddd                             |ddd         dd          ddd         }t          | |          }| }t          |          r2|ddd                             |ddd         dd          ddd         }t          |          r|                    |dd          }|S )a  
    Given two strings, extract the difference in the middle between two strings
    that are known to have a common prefix and/or suffix.

    This function is provided as a UTILITY for extracting information from JSON
    generated by partial_json_parser, to help in ensuring that the right tokens
    are returned in streaming, so that close-quotes, close-brackets and
    close-braces are not returned prematurely. The order of arguments IS
    important - the new version of the partially-parsed JSON must be the first
    argument, and the secnod argument must be from the previous generation.

    What it returns, is tokens that should be streamed to the client.

    e.g. extract_intermediate_diff('{"fruit": "apple"}', '{"fruit": "ap"}')
        -> 'ple'

    Nr   r   )r   replacer   r   )r   r    r   r   diffs        r   extract_intermediate_diffr%   ?   s    $  c**F
ddd)

F44R4L"a
0
02
6Cc**FD
6{{ =DDbDz!!&2,A66ttt<
6{{ +||FB**Kr   string	substringc                 |    g }d}	 |                      ||dz             }|dk    rn|                    |           6|S )zk
    Find all (starting) indices of a substring in a given string. Useful for
    tool call extraction
    r"   Tr   )findappend)r&   r'   indicesindexs       r   find_all_indicesr-   `   sT    
 GEIuqy11B;;u	
 Nr   	input_strflagsc                     	 t          j        | |          t          |           fS # t          $ r7}d|j        v r(t                      }|                    |           cY d }~S  d }~ww xY w)Nz
Extra data)partial_json_parserloadsr   r   msgr   
raw_decode)r.   r/   edecs       r   partial_json_loadsr7   q   s~    #))U;;S^^LL   15  --C>>),,,,,,,,	s    #& 
A'+A"A'!A""A'c                 R    	 t          j        |            dS # t          $ r Y dS w xY w)NTF)jsonr2   r   )r.   s    r   is_complete_jsonr:   {   s>    
9t   uus    
&&r   sc                     | t          |          k     rL||                                          r2| dz  } | t          |          k     r||                                          2| S )Nr   )r   isspace)r   r;   s     r   consume_spacer>      sP    
c!ff**1*	Q c!ff**1*Hr   toolc                     t          | t                    r| j        | j        fS t          | t                    r| j        j        | j        j        fS t          dt          |                      )NzUnsupported tool type: )
isinstancer   name
parametersr   function	TypeErrortype)r?   s    r   _extract_tool_inforG      sl     $%% @y$/))	D2	3	3 @}!4=#;;;>$t**>>???r   c                 T    t          |           \  }}|r|ndi d}d|gd|dddgdS )	Nobject)rF   
propertiesr&   )rF   enum)rB   rC   rB   rC   )rJ   required)rG   )r?   rB   paramss      r   _get_tool_schema_from_toolrN      s^    %d++LD&GVVHB#G#GF &v66 
 
 \*  r   toolsc                     i }| D ]o}t          |          \  }}||                    di           }|                                D ]-\  }}||v r||         |k    rt          d| d          |||<   .p|S )N$defszTool definition 'z/' has multiple schemas, which is not supported.)rG   popitems
ValueError)rO   all_defsr?   _rM   defsdef_name
def_schemas           r   _get_tool_schema_defsrZ      s     +-H , ,&t,,	6>zz'2&&$(JJLL 	, 	, Hj8##(:j(H(H . . . .   ",HX	, Or   c                 X    dddd | D             dd}t          |           }|r||d<   |S )Narrayr   rI   c                 ,    g | ]}t          |          S  )rN   .0r?   s     r   
<listcomp>z/_get_json_schema_from_tools.<locals>.<listcomp>   s!    III4066IIIr   )rF   anyOf)rF   minItemsrS   rQ   )rZ   )rO   json_schemajson_schema_defss      r   _get_json_schema_from_toolsrf      s\     II5III
 
 K -U33 0/Gr   tool_choicec                    | dv s|d S t          | t                    sLt          | t                    r7| j        }d |D             }||vrt	          d| d          ||         j        S t          | t                    sVt          | t                    rA| j        j        }d |D             }||vrt	          d| d          ||         j        j        S | dk    rt          |          S d S )N)noneNc                 H    i | ]}t          |t                    |j        | S r^   )rA   r   rB   r_   s     r   
<dictcomp>z.get_json_schema_from_tools.<locals>.<dictcomp>   s+    XXXD,9W9WXDItXXXr   zTool 'z!' has not been passed in `tools`.c                 R    i | ]$}t          |t                    |j        j        |%S r^   )rA   r   rD   rB   r_   s     r   rk   z.get_json_schema_from_tools.<locals>.<dictcomp>   s@     
 
 
$ 899
M
 
 
r   rL   )	rA   strr   rB   rT   rC   r
   rD   rf   )rg   rO   	tool_nametool_maps       r   get_json_schema_from_toolsrp      s;   
 n$$t{C(( .j'/ / .  $	XXXXXH$$RiRRRSSS	"--{C(( 7j7/ / 7  (-	
 

 
 

 H$$RiRRRSSS	"+66j  *51114r   )"r9   r   r   typingr   r1   openai.types.responsesr   r   openai.types.responses.toolr    partial_json_parser.core.optionsr	   0vllm.entrypoints.openai.chat_completion.protocolr
   r   rm   r   r   r%   listintr-   tupler7   boolr:   r>   dictrG   rN   rZ   rf   rp   r^   r   r   <module>r{      s    - - - - - - - -                  - , , , , , 2 2 2 2 2 2       3 C C    .3 C C    $C c c    BS S T#Y    "# e c3h         S S S    @
)
)@
3S#X%%&@ @ @ @	T,D%D 	 	 	 	 	//0	   &//0	   "!)),NN!7784?! 	4Z$! ! ! ! ! !r   