
    _j
%                        S r SSKJr  SSKrSSKJrJrJrJrJ	r	J
r
JrJrJrJr  SSKJrJr  SSKJr  SSKJrJrJrJr  SS	KJrJrJr  \" S
\S9r " S S\\   5      r " S S\5      rg)z!Asynchronous Research API client.    )annotationsN)
AnyAsyncGeneratorDictGenericLiteralOptionalTypeTypeVarUnionoverload)	BaseModelTypeAdapter   )AsyncResearchBaseClient)ResearchDtoResearchEventListResearchResponseDtoResearchModel)async_stream_sse_eventsis_pydantic_modelpydantic_to_json_schemaT)boundc                  "    \ rS rSrSrSS jrSrg)AsyncResearchTyped%   z6Wrapper for typed research responses in async context.c                   Xl         X l        UR                  U l        UR                  U l        UR                  U l        UR
                  U l        UR                  U l        UR                  U l        [        US5      (       a  UR                  U l	        [        US5      (       a  UR                  U l
        [        US5      (       a  UR                  U l        [        US5      (       a  UR                  U l        g g )Neventsoutputcost_dollarserror)researchparsed_outputresearch_idstatus
created_atmodelinstructionsoutput_schemahasattrr   r    r!   r"   )selfr#   r$   s      Q/home/rurouni/.local/lib/python3.13/site-packages/exa_py/research/async_client.py__init__AsyncResearchTyped.__init__(   s     *#//oo"--^^
$11%338X&&"//DK8X&&"//DK8^,, ( 5 5D8W%%!DJ &    )r!   r'   r"   r   r)   r(   r    r*   r$   r#   r%   r&   N)r#   r   r$   r   )__name__
__module____qualname____firstlineno____doc__r.   __static_attributes__ r0   r-   r   r   %   s
    @(r0   r   c                  2   \ rS rSrSr\SS.     SS jj5       r\SS.       S S jj5       r\SS.       S!S jj5       rSS	S
.       S"S jjr\    S#S j5       r\SSS.       S$S jj5       r\S	S.       S%S jj5       r\SSS.         S&S jj5       rSSS	S.         S'S jjrS	S	S.     S(S jjr\SSSS.         S)S jj5       r	\SSSS.           S*S jj5       r	SSSS	S.           S+S jjr	Sr
g	),AsyncResearchClient<   z)Asynchronous client for the Research API.zexa-research-fast)r(   c                  #    g 7fNr7   )r,   r)   r(   s      r-   createAsyncResearchClient.create?   s
         zexa-researchc                  #    g 7fr<   r7   r,   r)   r(   r*   s       r-   r=   r>   G   
      r?   c                  #    g 7fr<   r7   rA   s       r-   r=   r>   P   rB   r?   N)r(   r*   c                  #    UUS.nUb#  [        U5      (       a  [        U5      US'   OX4S'   U R                  SSUS9I Sh  vN n[        [        5      nUR                  U5      $  N$7f)zCreate a new research request.

Args:
    instructions: The research instructions.
    model: The model to use for research.
    output_schema: Optional JSON schema or Pydantic model for structured output.

Returns:
    The created research object.
)r)   r(   NoutputSchema POST)methoddata)r   r   requestr   r   validate_python)r,   r)   r(   r*   payloadresponseadapters          r-   r=   r>   Y   sv     $ )

 $ //*A-*P'*7'bgFFk*&&x00 Gs   A A)A'%A)c                   #    g 7fr<   r7   )r,   r%   s     r-   getAsyncResearchClient.gety   s
      r?   F)streamr   c                  #    g 7fr<   r7   r,   r%   rR   r   s       r-   rP   rQ      rB   r?   r   c                  #    g 7fr<   r7   rT   s       r-   rP   rQ      s
      /2r?   c                  #    g 7fr<   r7   )r,   r%   rR   r   r*   s        r-   rP   rQ      s
      !$r?   )rR   r   r*   c                 #    0 nU(       d  SUS'   U(       a  SUS'   O$SUS'   Ub  [        U5      R                  5       US'   U(       a(  U R                  SU 3SUSS	9I Sh  vN n[        U5      $ U R                  SU 3SUS
9I Sh  vN n[	        [
        5      nUR                  U5      nU(       a  [        US5      (       a  UR                  (       a   UR                  R                  (       a&  UR                  UR                  R                  5      n	O:SSKn
U
R                  UR                  R                  5      nUR                  U5      n	[        X5      $ U$  N N! [         a    Us $ f = f7f)a?  Get a research request by ID.

Args:
    research_id: The research ID.
    stream: Whether to stream events.
    events: Whether to include events in non-streaming response.
    output_schema: Optional Pydantic model for typed output validation.

Returns:
    Research object, typed research, or async event generator.
falserR   truer   N/GETT)rH   paramsrR   rH   r]   r    r   )strlowerrJ   r   r   r   rK   r+   r    parsedmodel_validatejsonloadscontentr   	Exception)r,   r%   rR   r   r*   r]   rM   rN   r#   ra   rc   parsed_datas               r-   rP   rQ      se    & &F8#)x %F8!#&v;#4#4#6x !\\K=!%t *  H +844!\\K=!% *  H "+.G..x8H8!<!<$--!.!=!=hoo>T>T!U#&*jj1H1H&I!.!=!=k!J-h??
 O3
  ! $#O$sJ   AE5!E"&E5E!	AE5BE# E5!E5#E2/E51E22E5)cursorlimitc                  #    U R                  X5      nU R                  SSUS9I Sh  vN n[        R                  " U5      $  N7f)zList research requests.

Args:
    cursor: Pagination cursor.
    limit: Maximum number of results.

Returns:
    List of research objects with pagination info.
rF   r\   r^   N)build_pagination_paramsrJ   r   rb   )r,   rh   ri   r]   rM   s        r-   listAsyncResearchClient.list   sE      --f<bvFF&55h?? Gs   &AAA  i'	 )poll_interval
timeout_msr   c                  #    g 7fr<   r7   )r,   r%   ro   rp   r   s        r-   poll_until_finished'AsyncResearchClient.poll_until_finished   s
      r?   c                  #    g 7fr<   r7   )r,   r%   ro   rp   r   r*   s         r-   rr   rs      s
      !$r?   )ro   rp   r   r*   c          	     `  #    US-  nUS-  nSn[         R                  " 5       R                  5       n	Sn
  U(       a  U R                  XUS9I Sh  vN nOU R                  XS9I Sh  vN nSn
[	        US5      (       a  UR
                  OSnUS;   a  U$  [         R                  " 5       R                  5       U	-
  U:  a  [        SU SU S35      e[         R                  " U5      I Sh  vN   M   N N! [         a(  nU
S	-  n
X:  a  [        S
U SU SU 35      e SnANSnAff = f N?7f)a  Poll until research is finished.

Args:
    research_id: The research ID.
    poll_interval: Milliseconds between polls (default 1000).
    timeout_ms: Maximum time to wait in milliseconds (default 600000).
    events: Whether to include events in the response.
    output_schema: Optional Pydantic model for typed output validation.

Returns:
    Completed research object or typed research.

Raises:
    TimeoutError: If research doesn't complete within timeout.
    RuntimeError: If polling fails too many times.
rn      r   )r   r*   NrU   r&   )	completedfailedcanceledr   zPolling failed z times in a row for research z: z	Research z did not complete within ms)
asyncioget_event_looptimerP   r+   r&   rf   RuntimeErrorTimeoutErrorsleep)r,   r%   ro   rp   r   r*   poll_interval_sectimeout_secmax_consecutive_failures
start_timeconsecutive_failuresresultr&   es                 r-   rr   rs      sp    2 *D0 4'#$ ++-224
  #'88#- $, $ F $(88K8#GGF'($ +2&(*C*C@@!M A %%',,.;kI"},Ej\QST  -- 1222;  H  $)$'C&)*B)C D((3}Bqc;  D 3s_   3D.C7 C3C7 (C5),C7 AD.,D,-D.3C7 5C7 7
D)D$D.$D))D.r7   )r)   r_   r(   r   returnr   )r)   r_   r(   +Literal['exa-research', 'exa-research-pro']r*   zDict[str, Any]r   r   )r)   r_   r(   r   r*   Type[T]r   r   )r)   r_   r(   r   r*   z0Optional[Union[Dict[str, Any], Type[BaseModel]]]r   r   )r%   r_   r   r   )r%   r_   rR   Literal[False]r   boolr   r   )r%   r_   rR   zLiteral[True]r   zOptional[bool]r   z#AsyncGenerator[ResearchEvent, None])
r%   r_   rR   r   r   r   r*   r   r   AsyncResearchTyped[T])
r%   r_   rR   r   r   r   r*   Optional[Type[BaseModel]]r   zKUnion[ResearchDto, AsyncResearchTyped, AsyncGenerator[ResearchEvent, None]])rh   zOptional[str]ri   zOptional[int]r   r   )
r%   r_   ro   intrp   r   r   r   r   r   )r%   r_   ro   r   rp   r   r   r   r*   r   r   r   )r%   r_   ro   r   rp   r   r   r   r*   r   r   z&Union[ResearchDto, AsyncResearchTyped])r1   r2   r3   r4   r5   r   r=   rP   rl   rr   r6   r7   r0   r-   r9   r9   <   sT   3
  3	  	
 
  
 >L	  ;	
 & 
  
 >L	  ;	
  
  >LJN1 1 ;	1
 H1 
1@  
 
 
 "' 	
  
   "&22 	2
 2 
-2 2 
 "'$$ 	$
 $ $ 
$ $ 3777 	7
 7 17 
U7x !%#	@ @ 	@
 
!@& 
 "  	
   
  
 " $$ 	$
 $ $ $ 
$ $ " 37<3<3 	<3
 <3 <3 1<3 
0<3 <3r0   r9   ) r5   
__future__r   r{   typingr   r   r   r   r   r	   r
   r   r   r   pydanticr   r   baser   modelsr   r   r   r   utilsr   r   r   r   r   r9   r7   r0   r-   <module>r      sf    ' "    , )   Cy!( (.|31 |3r0   