
    h              8       	   d dl Z d dl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 d dlZd dl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mZmZm Z m!Z!m"Z"m#Z#m$Z$m%Z% d dl& d dl'm(Z( d dl)m*Z*m+Z+ ddl,m-Z-  e       Z. e       Z/	 d<de0fdZ1	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=dee0e f   de0dee	e      dee0   dee2   dee   deee0ef      dee3   dee0   dee   dee3   dee3   dee3   dee4   d ee#   d!ee$   d"eee%      d#ee4   d$ee
d%      d&ee0   d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,ee"e-f   f4d-Z6e+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=dee0e f   de0dee	e      dee0   dee2   dee   deee0ef      dee3   dee0   dee   dee3   dee3   dee3   dee4   d ee#   d!ee$   d"eee%      d#ee4   d$ee
d%      d&ee0   d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,ee"e-f   f4d.       Z7e+	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d=dee0e f   de0dee	e      dee0   dee2   dee   deee0ef      dee3   dee0   dee   dee3   dee3   dee3   dee4   d ee#   d!ee$   d"eee%      d#ee4   d$ee
d%      d&ee0   d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   f2d/       Z8e+	 	 	 	 	 d>d0e0d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,e9fd1       Z:e+	 	 	 	 	 d>d0e0d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,ee9eeee9f   f   fd2       Z;e+	 	 	 	 	 d>d0e0d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,e"fd3       Z<e+	 	 	 	 	 d>d0e0d'eee0ef      d(eee0ef      d)eee0ef      d*eee4ejj                  f      d+ee0   d,ee"eeee"f   f   fd4       Z=e+	 	 	 	 	 	 	 	 d?d0e0d5ee0   d6ee0   dee	e0      d7e2d8e
d9   d'eee0ef      d*eee4ejj                  f      d+ee0   d,efd:       Z>e+	 	 	 	 	 	 	 	 d?d0e0d5ee0   d6ee0   dee	e0      d7e2d8e
d9   d'eee0ef      d*eee4ejj                  f      d+ee0   d,eeeeeef   f   fd;       Z?y)@    N)partial)Any	CoroutineDictIterableListLiteralOptionalUnion)request_timeout)Logging)BaseResponsesAPIConfig)BaseLLMHTTPHandler)&LiteLLMCompletionTransformationHandler)ResponsesAPIRequestUtils)	PromptObject	ReasoningResponseIncludableResponseInputParam!ResponsesAPIOptionalRequestParamsResponsesAPIResponseResponseTextConfigParam
ToolChoice	ToolParam)*)GenericLiteLLMParams)ProviderConfigManagerclient   )!BaseResponsesAPIStreamingIteratormock_responsec                     t        d,i i ddddddddd	d d
d dd dd dddddddd| g dgdgdddd dd d dddddddd diid!d"g dd#d$d%d&id'd(d&id)d*d i d+S )-Nid5resp_67ccd2bed1ec8190b14f964abc0542670bb6a6b452d3795bobjectresponse
created_atigstatus	completederrorincomplete_detailsinstructionsmax_output_tokensmodelzgpt-4.1-2025-04-14outputmessage4msg_67ccd2bf17f0819081ff3bb2cf6508e60bb6a6b452d3795b	assistantoutput_text)typetextannotations)r4   r#   r(   rolecontentparallel_tool_callsTprevious_response_id	reasoning)effortsummarystoretemperatureg      ?r5   formatr4   tool_choiceautodisabled$   cached_tokensr   W   reasoning_tokens{   )input_tokensinput_tokens_detailsoutput_tokensoutput_tokens_detailstotal_tokens)toolstop_p
truncationusageusermetadata )r   r!   s    R/var/www/Befach/backend/env/lib/python3.12/site-packages/litellm/responses/main.pymock_responses_api_responserW   )   sU      .,
I,
j,
 *,
 k	,

 T,
 !$,
 D,
  ,
 ),
 %P)' %2$1+- ,
2 "43,
4 #D5,
6 DT:7,
8 T9,
: 3;,
< X/0=,
> 6?,
@ $ ")8!(<!#*<a)@ # W,
. .    inputr.   includer,   r-   promptrS   r9   r:   r;   r>   
backgroundstreamr?   r5   rA   rN   rO   rP   )rB   rC   rR   extra_headersextra_query
extra_bodytimeoutcustom_llm_providerreturnc                 J  K   ddl m} |j                  |      \  }}g }g }|r;|j                  d      }|j	                  |       d{   }|j                  |      }|s|r||z   nd} i d|d|d|d|d	|d
|d|	d|
d|d|d|d|d|d|d|d|d|||||d|}!t        d| || |d|! d{   }"|rt        |"t              r|j                  |      r|j                  |"      }#|#r|j                  di       j                  d      }|j                  |#|       d{   }$|$rc|j                  |"|$|       }% |j                  d|%|| |"j                  d|! d{   }&t        |&t              r|j                  |&||$      }&|&S |"S 7 R7 7 p7 3w) a  
    Async version of responses API with MCP integration.
    
    When MCP tools with server_url="litellm_proxy" are provided, this function will:
    1. Get available tools from the MCP server manager
    2. Insert the tools into the messages/input
    3. Call the standard responses API
    4. If require_approval="never" and tool calls are returned, automatically execute them
    r   LiteLLM_Proxy_MCP_Handleruser_api_key_authNrZ   r,   r-   r[   rS   r9   r;   r>   r\   r]   r?   r5   rA   rO   rP   rR   r^   )r_   r`   ra   rb   )rY   r.   rN   r:   )mcp_tools_with_litellm_proxy)r&   litellm_metadata)
tool_callsrg   )r&   tool_resultsoriginal_input)follow_up_inputr.   	all_toolsresponse_id)r&   mcp_tools_fetchedrk   rT   )/litellm.responses.mcp.litellm_proxy_mcp_handlerrf   _parse_mcp_toolsget_get_mcp_tools_from_manager_transform_mcp_tools_to_openai
aresponses
isinstancer   _should_auto_execute_tools!_extract_tool_calls_from_response_execute_tool_calls_create_follow_up_input_make_follow_up_callr#   $_add_mcp_output_elements_to_response)'rY   r.   rZ   r,   r-   r[   rS   r9   r:   r;   r>   r\   r]   r?   r5   rA   rN   rO   rP   rR   r^   r_   r`   ra   rb   kwargsrf   rh   other_toolsopenai_toolsrp   rg   rn   call_paramsr&   rj   rk   rm   final_responses'                                          rV   aresponses_api_with_mcpr   \   s    P
 1J0Z0Z[`0a- + L#"JJ':;";"W"WXi"jj0OOPab 0<{{*QUI7 	. 	&	
 	H 	2 	Y 	 	j 	& 	{ 	 	{ 	 	j  	!" 	#$ # 2+, -K6   1	
  H 	%812!<<Zv<w.PPZbPc
 &

+=r B F FGZ [!:!N!NZd  yJ!N  "K  KL";"S"S%!-#( #T # (V'@'U'U ($3' (	(
 "( " n.BC%>%c%c!/*;%1 &d &N
 &%OY kD  K"sJ   AF#F	A1F#:F;A3F#.F/>F#-F!.-F#F#F#!F#c                 "  K   t               }	 t        j                         }d|d<   |,t        j                  ||j                  dd            \  }}}}t        t        fi d| d|d|d	|d
|d|d|d|d|d|	d|
d|d|d|d|d|d|d|d|d|d|d|d|d|d||}t        j                         }t        |j                  |      }|j                  d|       d{   } t        j                  |       r|  d{   }!n| }!t        |!t              r(t        j                   |!|j                  di       |       }!|!t#        d!|!       |!S 7 p7 S# t$        $ r}"t        j&                  |||"||"      d}"~"ww xY ww)#zS
    Async: Handles responses API requests by reusing the synchronous function
    Trv   Nbase_url)r.   api_baserY   r.   rZ   r,   r-   r[   rS   r9   r:   r;   r>   r\   r]   r?   r5   rA   rN   rO   rP   rR   r^   r_   r`   ra   rb   ri   responses_api_responseri   rb   z8Got an unexpected None response from the Responses API: r.   rb   original_exceptioncompletion_kwargsextra_kwargs)localsasyncioget_event_looplitellmget_llm_providerrs   r   	responsescontextvarscopy_contextrunrun_in_executoriscoroutinerw   r   r   /_update_responses_api_response_id_with_model_id
ValueError	Exceptionexception_type)#rY   r.   rZ   r,   r-   r[   rS   r9   r:   r;   r>   r\   r]   r?   r5   rA   rN   rO   rP   rR   r^   r_   r`   ra   rb   r~   
local_varsloop_funcctxfunc_with_contextinit_responser&   es#                                      rV   rv   rv      sO    F JD
%%'#| &+2+C+CjnnZ&F,(A"Aq 

 
 	

 &
 0
 
 
 !4
 "6
  
 
 "
 
 $
  !
" $#
$ %
& '
( ")
* +
, (-
. $/
0 "1
2 3
4 !47
< &&(#CGGT2"2249JKK}-**H$H h 45/__'/!',>!C$7H WX`Wabcc% L +   
$$ 3 (
 	

sN   FC!E$ /E 0E$ E"AE$ F E$ "E$ $	F-FFFc                    ddl m} t               }	 |j                  d      }|j                  dd      }|j	                  dd      du }t        di |}|j                  r0t        |j                  t              rt        |j                  	      S t        j                  |||j                  |j                  
      \  }}} }!|j                  |      rt        di |S t!        j"                  |t        j$                  |            }"|j'                  |       t)        j*                  |      }#|"t-        j.                  d|| |#|||d|S t)        j0                  ||"|#      }$|j3                  ||t5        |$      d|i|$|       t6        j/                  || |"|$||||||xs t8        ||j                  d      |"j;                  |||      |j                  di             }%t        |%t<              r(t)        j>                  |%|j                  di       |      }%|%S # t@        $ r}&t        jB                  |||&||      d}&~&ww xY w)zk
    Synchronous version of the Responses API.
    Uses the synchronous HTTP handler to make requests.
    r   re   litellm_logging_objlitellm_call_idNrv   FTrU   )r.   rb   r   api_key)rN   r.   provider)r.   rY   responses_api_requestrb   	_is_asyncr]   )r.   responses_api_provider_configresponse_api_optional_params)r.   rR   optional_paramslitellm_paramsrb   r   )r.   r]   rb   ri   )r.   rY   r   $response_api_optional_request_paramsrb   r   logging_objr^   r`   ra   r   r   fake_streamri   r   r   rT   )"rq   rf   r   rs   popr   r!   rw   strrW   r   r   r   r   _should_use_litellm_mcp_gatewayr   r   !get_provider_responses_api_configLlmProvidersupdater   )get_requested_response_api_optional_param)litellm_completion_transformation_handlerresponse_api_handler!get_optional_params_responses_apiupdate_environment_variablesdictbase_llm_http_handlerr   should_fake_streamr   r   r   r   )'rY   r.   rZ   r,   r-   r[   rS   r9   r:   r;   r>   r\   r]   r?   r5   rA   rN   rO   rP   rR   r^   r_   r`   ra   rb   r~   rf   r   r   r   r   r   dynamic_api_keydynamic_api_baser   r   responses_api_request_paramsr&   r   s'                                          rV   r   r   K  s   H Jy
17<Q1R)/4Et)LJJ|U3t;	 .77
 ''J((#-
 /,::  $$ 3#,,"**	
	
 %DD5DQ*   "CC --.AB 	& 	&! %NN 	% )0<QQ &B$7#   %FF.K-I 	% 	88 !=>!?. !4 	9 		
 )==*G1M 3)+'!.::h'5HHF@S I  $ZZ(:B?! > 
( h 45/__'/!',>!C$7H  
$$ 3 (
 	

s-   A=H AH A-H CH 	I'IIro   c           
      0  K   t               }	 t        j                         }d|d<   t        j                  |       }	|	j                  d      xs | } |	j                  d      xs |}t        t        f| |||||d|}
t        j                         }t        |j                  |
      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  d||||      d}~ww xY ww)	zz
    Async version of the DELETE Responses API

    DELETE /v1/responses/{response_id} endpoint in the responses API

    Tadelete_responsesro   ro   rb   ro   rb   r^   r_   r`   ra   Nr   )r   r   r   r   !_decode_responses_api_response_idrs   r   delete_responsesr   r   r   r   r   r   r   r   ro   r^   r_   r`   ra   rb   r~   r   r   decoded_response_idr   r   r   r   r&   r   s                   rV   r   r     sE    & J*
%%'&*"# %FF' 	
 *--m<K##$9:Q>Q 	 	
# 3'#!	
 	
 &&(#CGGT2"2249JKK}-**H  %H L +  
$$ 3 (
 	

sY   DB/C+ =C'>C+ C)C+ "D#C+ &D'C+ )C+ +	D4DDDc                    t               }	 |j                  d      }|j                  dd      }	|j                  dd      du }
t        di |}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  dt        j                  |            }|t        d|       |j                  |       |j                  dd| id|	i|       t        j                  | |||||||xs t        |
|j                  d      
      }|S # t         $ r}t        j"                  d||||      d}~ww xY w)z
    Synchronous version of the DELETE Responses API

    DELETE /v1/responses/{response_id} endpoint in the responses API

    r   r   Nr   FTr   ro   rb   2custom_llm_provider is required but passed as Noner   z&DELETE responses is not supported for r.   r   r   rb   r   
ro   rb   r   r   r   r^   r`   ra   r   r   r   rT   )r   rs   r   r   r   r   r   r   r   r   r   r   r   r   delete_response_api_handlerr   r   r   ro   r^   r_   r`   ra   rb   r~   r   r   r   r   r   r   r   r&   r   s                   rV   r   r   1  s   & JG
17<Q1R)/4Et)LJJ2E:dB	 .77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR "CC --.AB 	& )089L8MN  	&! 	88{ "? !4 	9 		
 )DD# 3*G)+'!.::h' E 
  
$$ 3 (
 	

s   D*D7 7	E EEc           
        K   t               }	 t        j                         }d|d<   t        j                  |       }	|	j                  d      xs | } |	j                  d      xs |}t        t        f| |||||d|}
t        j                         }t        |j                  |
      }|j                  d|       d{   }t        j                  |      r| d{   }n|}t        |t              r(t        j                  ||j                  di       |	      }|S 7 `7 C# t         $ r}t#        j$                  d||||
      d}~ww xY ww)ax  
    Async: Fetch a response by its ID.

    GET /v1/responses/{response_id} endpoint in the responses API

    Args:
        response_id: The ID of the response to fetch.
        custom_llm_provider: Optional provider name. If not specified, will be decoded from response_id.

    Returns:
        The response object with complete information about the stored response.
    Taget_responsesr   ro   rb   r   Nri   r   r   )r   r   r   r   r   rs   r   get_responsesr   r   r   r   r   rw   r   r   r   r   r   r   s                   rV   r   r     sp    2 J2
%%'#'  %FF' 	
 *--m<K##$9:Q>Q 	 	
# 3'#!	
 	
 &&(#CGGT2"2249JKK}-**H$H h 45/__'/!',>!C$7H
  L +  
$$ 3 (
 	

sN   EB/D" =D>D" D A D" ED"  D" "	E
+EE

Ec                 4   t               }	 |j                  d      }|j                  dd      }	|j                  dd      du }
t        di |}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  dt        j                  |            }|t        d|       |j                  |       |j                  dd| id|	i|       t        j                  | |||||||xs t        |
|j                  d      
      }t!        |t"              r(t	        j$                  ||j                  di       |      }|S # t&        $ r}t        j(                  d||||      d}~ww xY w)aq  
    Fetch a response by its ID.

    GET /v1/responses/{response_id} endpoint in the responses API

    Args:
        response_id: The ID of the response to fetch.
        custom_llm_provider: Optional provider name. If not specified, will be decoded from response_id.

    Returns:
        The response object with complete information about the stored response.
    r   r   Nr   FTr   ro   rb   r   r   z#GET responses is not supported for r   r   r   ri   r   r   rT   )r   rs   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rw   r   r   r   r   r   s                   rV   r   r     s   2 JO
17<Q1R)/4Et)LJJ/74?	 .77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR "CC --.AB 	& )056I5JK  	&! 	88{ "? !4 	9 		
 )66# 3*G)+'!.::h' 7 
 h 45/__'/!',>!C$7H  
$$ 3 (
 	

s   E"E/ /	F8FFafterbeforelimitorder)ascdescc	                 6  K   t               }
	 t        j                         }d|	d<   t        j                  |       }|j                  d      xs | } |j                  d      xs |}t        t        f| ||||||||d	|	}t        j                         }t        |j                  |      }|j                  d|       d{   }t        j                  |      r| d{   }|S |}|S 7 )7 # t        $ r}t        j                  d|||
|	      d}~ww xY ww)	z&Async: List input items for a responseTalist_input_itemsr   ro   rb   )	ro   r   r   rZ   r   r   r^   ra   rb   Nr   )r   r   r   r   r   rs   r   list_input_itemsr   r   r   r   r   r   r   r   )ro   r   r   rZ   r   r   r^   ra   rb   r~   r   r   r   r   r   r   r   r&   r   s                      rV   r   r   J  sN     J,
%%'&*"# %FF' 	
 *--m<K##$9:Q>Q 	 
#' 3
 
 &&(#CGGT2"2249JKK}-**H  %H L +  
$$ 3 (
 	

sY   DB2C.  C*C. C, C. %D&C. )D*C. ,C. .	D7DDDc	                    t               }
	 |	j                  d      }|	j                  dd      }|	j                  dd      du }t        di |	}t	        j
                  |       }|j                  d      xs | } |j                  d	      xs |}|t        d
      t        j                  dt        j                  |            }|t        d|       |
j                  |	       |j                  dd| id|i|       t        j                  | |||||||||||xs t        ||	j                  d            }|S # t         $ r}t        j"                  d|||
|	      d}~ww xY w)zList input items for a responser   r   Nr   FTr   ro   rb   r   r   z&list_input_items is not supported for r   r   )ro   rb   r   r   r   r   r   rZ   r   r   r^   ra   r   r   r   rT   )r   rs   r   r   r   r   r   r   r   r   r   r   r   r   list_responses_input_itemsr   r   r   )ro   r   r   rZ   r   r   r^   ra   rb   r~   r   r   r   r   r   r   r   r&   r   s                      rV   r   r     s    JB
17<Q1R)/4Et)LJJ2E:dB	-77 %FF' 	
 *--m<K##$9:Q>Q 	 &QRR "CC --.AB 	& )089L8MN  	&!88*K8-? 3	 	9 	
 )CC# 3*G)+'.::h' D 
"  
$$ 3 (
 	

s   D.D; ;	E#EE#)a  In a peaceful grove beneath a silver moon, a unicorn named Lumina discovered a hidden pool that reflected the stars. As she dipped her horn into the water, the pool began to shimmer, revealing a pathway to a magical realm of endless night skies. Filled with wonder, Lumina whispered a wish for all who dream to find their own hidden magic, and as she glanced back, her hoofprints sparkled like stardust.)NNNNNNNNNNNNNNNNNNNNNNN)NNNNN)NNN   r   NNN)@r   r   	functoolsr   typingr   r   r   r   r   r	   r
   r   httpxr   litellm.constantsr   *litellm.litellm_core_utils.litellm_loggingr   LiteLLMLoggingObj.litellm.llms.base_llm.responses.transformationr   *litellm.llms.custom_httpx.llm_http_handlerr   ;litellm.responses.litellm_completion_transformation.handlerr   litellm.responses.utilsr   litellm.types.llms.openair   r   r   r   r   r   r   r   r   litellm.types.responses.mainlitellm.types.routerr   litellm.utilsr   r   streaming_iteratorr    r   r   r   rW   intboolfloatTimeoutr   rv   r   DeleteResponseResultr   r   r   r   r   r   rT   rX   rV   <module>r      s      Q Q Q   - S Q I =
 
 
 + 5 7 A +, ,R,T )
 o11l 37"&'+%))-*.*.%) !%!#'.2(,+/!8< /3,0+/59)-9@(()@@ d-./@ 3-	@
  }@ \"@ tCH~&@ "$@ #3-@ 	"@ D>@ @ TN@ %@ *
+@  *%!@" HY'(#@$ E?%@& !345'@( 3-)@. DcN+/@0 $sCx.)1@2 c3h(3@4 eE5==0125@8 "#9@< !BBC=@H  37"&'+%))-*.*.%) !%!#'.2(,+/!8< /3,0+/59)-9g
(()g
g
 d-./g
 3-	g

  }g
 \"g
 tCH~&g
 "$g
 #3-g
 	"g
 D>g
 g
 TNg
 %g
 *
+g
  *%!g
" HY'(#g
$ E?%g
& !345'g
( 3-)g
. DcN+/g
0 $sCx.)1g
2 c3h(3g
4 eE5==0125g
8 "#9g
< !BBC=g
 g
T  37"&'+%))-*.*.%) !%!#'.2(,+/!8< /3,0+/59)-9a
(()a
a
 d-./a
 3-	a

  }a
 \"a
 tCH~&a
 "$a
 #3-a
 	"a
 D>a
 a
 TNa
 %a
 *
+a
  *%!a
" HY'(#a
$ E?%a
& !345'a
( 3-)a
. DcN+/a
0 $sCx.)1a
2 c3h(3a
4 eE5==0125a
8 "#9a
 a
H 
 /3,0+/59)-=
=
 DcN+	=

 $sCx.)=
 c3h(=
 eE5==012=
 "#=
 =
 =
@ 
 /3,0+/59)-Z
Z
 DcN+	Z

 $sCx.)Z
 c3h(Z
 eE5==012Z
 "#Z
 35I+I!JJKZ
 Z
z 
 /3,0+/59)-K
K
 DcN+	K

 $sCx.)K
 c3h(K
 eE5==012K
 "#K
 K
 K
\ 
 /3,0+/59)-h
h
 DcN+	h

 $sCx.)h
 c3h(h
 eE5==012h
 "#h
 35I+I!JJKh
 h
V    #'$*.259)-:
:
C=:
 SM:
 d3i 	:

 :
 =!:
 DcN+:
 eE5==012:
 "#:
 
:
 :
z    #'$*.259)-P
P
C=P
 SMP
 d3i 	P

 P
 =!P
 DcN+P
 eE5==012P
 "#P
 43T>**+P
 P
rX   