
    i%                         S SK r S SKrS SKrS SKrS SKJr  S SKJrJrJ	r	J
r
JrJr  S SKJr  S SKJr  S SKJr  \" S5      r " S S	5      rg)
    N)ThreadPoolExecutor)OptionalListUnionAny	CoroutineTypeVar)urljoin)ReportMetadata)ReportClientTc                      \ rS rSrSr      S(S\\   S\\   S\\   S\\   S\\   S	\\R                     4S
 jjr
\S\R                  4S j5       rS\\\\4   S\4S jrS\4S jr S)S\S\\\      S\4S jjr     S*S\S\\   S\\   S\\\\\\4   4      S\\\\\\\4   4         S\\   S\4S jjr     S*S\S\\   S\\   S\\\\\\4   4      S\\\\\\\4   4         S\\   S\4S jjr S+S\\   S\S\S\\   4S jjr S+S\\   S\S\S\\   4S jjrS \S\4S! jrS \S\4S" jrS \S\4S# jrS \S\4S$ jr S \SS4S% jr!S \SS4S& jr"S'r#g),LlamaReport   z:Client for managing reports and general report operations.Napi_key
project_idorganization_idbase_urltimeoutasync_httpx_clientc           
         U=(       d    [         R                  " SS 5      U l        U R                  (       d  [        S5      eU=(       d    [         R                  " SS5      U l        U=(       d    SU l        U=(       d    [        R                  " U R
                  S9U l        SSU R                   30U l	        X0l
        X l        U R                  R
                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                  U R                  R                   U R                  R"                  S	.	U l        ['        [)        S
[         R*                  " 5       =(       d    SS-   5      S9U l        g )NLLAMA_CLOUD_API_KEYzNo API key provided.LLAMA_CLOUD_BASE_URLzhttps://api.cloud.llamaindex.ai<   )r   AuthorizationzBearer )	r   headersr   authevent_hookscookiesmax_redirectsparams	trust_env
         )max_workers)osgetenvr   
ValueErrorr   r   httpxAsyncClient_aclientr   r   r   r   r   r   r    r!   r"   _client_paramsr   min	cpu_count_thread_pool)selfr   r   r   r   r   r   s          Z/home/james-whalen/.local/lib/python3.13/site-packages/llama_cloud_services/report/base.py__init__LlamaReport.__init__   sF    H")),A4"H||344  
BII"$E%
 }" +Ue.?.?.U wt||n5
  /$}},,}},,..MM&&==44}},,!]]88mm**00

 /B!41 9:
    returnc                 ~    U R                   c%  [        R                  " S0 U R                  D6U l         U R                   $ )N )r,   r*   r+   r-   )r1   s    r2   aclientLlamaReport.aclient=   s1    == !--D0C0CDDM}}r5   coroc                    ^ U R                   nSU l         S[        4U4S jjnU R                  R                  U5      R	                  5       nX l         U$ )z=Run coroutine in a separate thread to avoid event loop issuesNr6   c                  V   > S[         4U4S jjn [        R                  " U " 5       5      $ )Nr6   c                  $   >#    T I S h  vN $  N7fNr8   )r;   s   r2   wrapped_coro=LlamaReport._run_sync.<locals>.run_coro.<locals>.wrapped_coroK   s     !z!zs   )r   asynciorun)r@   r;   s    r2   run_coro'LlamaReport._run_sync.<locals>.run_coroJ   s"    " " ;;|~..r5   )r,   r   r0   submitresult)r1   r;   original_clientrD   rG   s    `   r2   	_run_syncLlamaReport._run_syncC   sN     --	/! 	/ ""))(3::< (r5   c                 L  #    U R                   R                  [        [        U R                  5      S5      U R
                  S9I S h  vN nUR                  5         UR                  5       nU Vs/ s H  o3R                  S5      (       d  M  UPM     nnUS   S   $  NUs  snf 7f)Nz/api/v1/projectsr   
is_defaultr   id)r9   getr
   strr   r   raise_for_statusjson)r1   responseprojectspdefault_projects        r2   _get_default_project LlamaReport._get_default_projectW   s     ))C&(:;T\\ * 
 
 	!!#==?&.Fh%%2E1hFq!$''

 Gs*   AB$B(B$0BBB$B$endpointextra_paramsc                   #    [        [        U R                  5      U5      nU R                  (       d  U R	                  5       I Sh  vN U l        / nU R
                  (       a  UR                  SU R
                   35        U R                  (       a  UR                  SU R                   35        U(       a&  UR                  U Vs/ s H	  oUc  M  UPM     sn5        U(       a  USSR                  U5      -   -  nU$  Ns  snf 7f)z9Helper method to build URLs with common query parameters.Nzorganization_id=zproject_id=?&)	r
   rP   r   r   rW   r   appendextendjoin)r1   rY   rZ   urlquery_paramsrU   s         r2   
_build_urlLlamaReport._build_url`   s      c$--((3$($=$=$??DO"243G3G2H IJ??+doo-> ?@L JLqL JK3,///C
 @ !Ks+   ADC=A?DC?C?(D?Dnametemplate_instructionstemplate_texttemplate_fileinput_filesexisting_retriever_idc                    #    U R                  S5      I Sh  vN n/ nSU0n	U(       a  X)S'   U(       a  X9S'   U(       a  [        U5      U	S'   / n
U(       a]  [        U[        5      (       a2  UR                  [	        US5      5        U
R                  SUS	   45        OU
R                  SUS
   45        U(       ag  U Ha  n[        U[        5      (       a3  UR                  [	        US5      5        U
R                  SUS	   45        MK  U
R                  SUS
   45        Mc     U R
                  R                  XpR                  XS9I Sh  vN n UR                  5         UR                  5       S   n[        XU 5      U H  nUR                  5         M     $  GN NP! [        R                   a>  n[        SUR                  R                    SUR                  R"                   35      eSnAff = f! U H  nUR                  5         M     f = f7f)z#Create a new report asynchronously./api/v1/reports/Nre   rf   rg   rj   rbrh   r$   files)r   dataro   rN   zFailed to create report: z
Error Code: )rc   rP   
isinstancer^   openr9   postr   rQ   rR   r   closer*   HTTPStatusErrorr)   rS   textstatus_code)r1   re   rf   rg   rh   ri   rj   ra   
open_filesrp   ro   frS   	report_id	open_filees                   r2   acreate_reportLlamaReport.acreate_reportv   s     OO$677.0
~ ,A()$1! ,/0E,FD()=?---!!$}d";<oz"~>?o}Q/?@A a%%%%d1dm4LL':b>!:;LL'1Q41 ! **D + 
 

	"%%' -I	6 (	! (O 86
 $$ 	+AJJOO+<N1::KaKaJbc 	
 (	! (sR   HFD0HF	H.F <HHG-/9G((G--G0 0HHc                 H    U R                  U R                  UUUUUUS95      $ )zCreate a new report.)re   rf   rg   rh   ri   rj   )rI   r}   )r1   re   rf   rg   rh   ri   rj   s          r2   create_reportLlamaReport.create_report   s:     ~~&;++'&;   	
 		
r5   statelimitoffsetc                   #    / nU(       a  UR                  SU 35        U(       a  UR                  SU 35        U(       a  UR                  SU 35        U R                  SUS9I Sh  vN nU R                  R                  XPR                  S9I Sh  vN nUR                  5         UR                  5       nUS    Vs/ s H  n[        US	   US
   U 5      PM     sn$  Nv NMs  snf 7f)z List all reports asynchronously.zstate=zlimit=zoffset=z/api/v1/reports/list)rZ   NrL   report_responsesrz   re   )r^   rc   r9   rO   r   rQ   rR   r   )	r1   r   r   r   r!   ra   rS   rp   rs	            r2   alist_reportsLlamaReport.alist_reports   s      MMF5'*+MMF5'*+MMGF8,-OO" $ 
 

 ))#||)DD!!#}} ,-
- ;6D9-
 	


 E
s6   A'C))C **C)C"+C) C$C)"C)$C)c                 D    U R                  U R                  XU5      5      $ )z(Synchronous wrapper for listing reports.)rI   r   )r1   r   r   r   s       r2   list_reportsLlamaReport.list_reports   s      ~~d00vFGGr5   rz   c                   #    U R                  SU 35      I Sh  vN nU R                  R                  X R                  S9I Sh  vN nUR	                  5         UR                  5       n[        US   US   U 5      $  Nb N97f)z9Get a Report instance for working with a specific report.rl   NrL   rz   re   )rc   r9   rO   r   rQ   rR   r   )r1   rz   ra   rS   rp   s        r2   aget_reportLlamaReport.aget_report   st     OO&6yk$BCC))#||)DD!!#}}D-tF|TBB DDs!   BA=*BA?8B?Bc                 B    U R                  U R                  U5      5      $ )z)Synchronous wrapper for getting a report.)rI   r   r1   rz   s     r2   
get_reportLlamaReport.get_report   s    ~~d..y9::r5   c                    #    U R                  SU S35      I Sh  vN nU R                  R                  X R                  S9I Sh  vN nUR	                  5         [        S0 UR                  5       D6$  NX N/7f)ai  Get metadata for a specific report asynchronously.

Returns:
    dict containing:
    - id: Report ID
    - name: Report name
    - state: Current report state
    - report_metadata: Additional metadata
    - template_file: Name of template file if used
    - template_instructions: Template instructions if provided
    - input_files: List of input file names
rl   z	/metadataNrL   r8   )rc   r9   rO   r   rQ   r   rR   r1   rz   ra   rS   s       r2   aget_report_metadata LlamaReport.aget_report_metadata   si      OO&6yk$KLL))#||)DD!!#000	 MDs!   A8A4*A8A6.A86A8c                 B    U R                  U R                  U5      5      $ )z0Synchronous wrapper for getting report metadata.)rI   r   r   s     r2   get_report_metadataLlamaReport.get_report_metadata   s    ~~d77	BCCr5   c                    #    U R                  SU 35      I Sh  vN nU R                  R                  X R                  S9I Sh  vN nUR	                  5         g N@ N7f)z(Delete a specific report asynchronously.rl   NrL   )rc   r9   deleter   rQ   r   s       r2   adelete_reportLlamaReport.adelete_report  sQ     OO&6yk$BCC,,S,,,GG!!# DGs!   AA*AAAAc                 B    U R                  U R                  U5      5      $ )z*Synchronous wrapper for deleting a report.)rI   r   r   s     r2   delete_reportLlamaReport.delete_report  s    ~~d11)<==r5   )	r,   r-   r0   r   r   r   r   r   r   )NNNNNNr?   )NNNNN)Nd   r   )$__name__
__module____qualname____firstlineno____doc__r   rP   intr*   r+   r3   propertyr9   r   r   r   rI   rW   r   rc   r   tuplebytesr   r}   r   r   r   r   r   r   r   r   r   r   __static_attributes__r8   r5   r2   r   r      s   D "&$()-"&!%:>)
#)
 SM)
 "#	)

 3-)
 #)
 %U%6%67)
V **  
iS!4  ((C ( BF+3DI+>	2 04'+AEEI/32"2"  (}2"  }	2"
  c5e+<&< =>2" d5eCJ.?)?#@AB2"  (}2" 
2"n 04'+AEEI/3

  (}
  }	

  c5e+<&< =>
 d5eCJ.?)?#@AB
  (}
 

, LM
c]
25
EH
	l	
6 LMHc]H25HEHH	l	HC3 C< C;C ;L ;1C 1N 1&DS D^ D$c $d $>s >t >r5   r   )rB   r*   r'   ioconcurrent.futuresr   typingr   r   r   r   r   r	   urllib.parser
   llama_cloud.typesr   "llama_cloud_services.report.reportr   r   r   r8   r5   r2   <module>r      s8      	 	 1 A A   , ;CL~> ~>r5   