
    CjYL                        S SK Jr  S SKJrJr  S SKJrJrJr  S SK	J
r
  SSKJrJrJrJrJr  SSKJr  \(       a	  SSKJrJr  O\r\r\
" S	\S
9r\" SS9 " S S5      5       r\" SS9 " S S\\   5      5       r\" SS9 " S S\\   5      5       rg)    )annotations)	dataclassfield)TYPE_CHECKINGAnyGeneric)TypeVar   )FunctionToolLookupKeyget_function_tool_approval_keysget_function_tool_lookup_key$is_reserved_synthetic_tool_namespacetool_qualified_name)Usage)ToolApprovalItemTResponseInputItemTContext)defaultF)eqc                  j    \ rS rSr% Sr\" \S9rS\S'   \" \S9r	S\S'   \" \
S9rS\S'   S	rS
\S'   Srg	)_ApprovalRecord   zTracks approval/rejection state for a tool.

``approved`` and ``rejected`` are either booleans (permanent allow/deny)
or lists of call IDs when approval is scoped to specific tool calls.
default_factoryzbool | list[str]approvedrejectedzdict[str, str]rejection_messagesN
str | Nonesticky_rejection_message )__name__
__module____qualname____firstlineno____doc__r   listr   __annotations__r   dictr   r   __static_attributes__r        d/home/rurouni/.local/share/pipx/venvs/strix-agent/lib/python3.13/site-packages/agents/run_context.pyr   r      sD     "'t!<H<!&t!<H<).t)DD+/j/r*   r   c                     \ rS rSr% SrS\S'    \" \S9rS\S'    \" \	S9r
S\S	'   \" \S9rS
\S'   SrS\S'    \S&S j5       r\S'S j5       r\S(S j5       r\S'S j5       r\S)S j5       r\S*S j5       r\S(S j5       rS+S jrS,S jrS-S jr\S.S j5       r\S/S j5       rSSSS.           S0S jjrSS.         S1S jjrS2S3S jjr  S4       S5S  jjrSSSS.           S6S! jjrS7S" jr S8S# jr!S9S$ jr"S%r#g):RunContextWrapper*   a!  This wraps the context object that you passed to `Runner.run()`. It also contains
information about the usage of the agent run so far.

NOTE: Contexts are not passed to the LLM. They're a way to pass dependencies and data to code
you implement, like tool functions, callbacks, hooks, etc.
r   contextr   r   usagezlist[TResponseInputItem]
turn_inputzdict[str, _ApprovalRecord]
_approvalsNz
Any | None
tool_inputc                p    [        U [        5      (       a  U $ U b   [        U 5      $ g ! [         a     g f = fN)
isinstancestr	Exception)values    r+   _to_str_or_none!RunContextWrapper._to_str_or_none@   sB    eS!!L5z!   s   
( 
55c                P   U R                   nU R                  (       a  U R                  $ [        U[        5      (       a*  UR	                  S5      =(       d    UR	                  S5      nO![        USS 5      =(       d    [        USS 5      n[        R                  U5      =(       d    S$ )Nnametypeunknown_tool)raw_item	tool_namer6   r(   getgetattrr-   r:   approval_itemraw	candidates      r+   _resolve_tool_name$RunContextWrapper._resolve_tool_nameK   s{    $$"" ***c4  :3776?IVT2Pgc646PI 00;M~Mr*   c                $   U R                   n[        U R                  [        5      (       a  U R                  (       a  U R                  $ [        U[        5      (       a  UR                  S5      nO[        USS 5      n[        R                  U5      $ )N	namespace)	r@   r6   tool_namespacer7   r(   rB   rC   r-   r:   rD   s      r+   _resolve_tool_namespace)RunContextWrapper._resolve_tool_namespaceW   sn    $$m22C88]=Y=Y ///c4  ,I[$7I 00;;r*   c                    [         R                  U 5      n[         R                  U 5      n[         R                  U 5      n[	        UUUUS L S9nU(       a  US   $ [        X5      =(       d    U=(       d    S$ )N)rA   rL   tool_lookup_key!prefer_legacy_same_name_namespacer?   )r-   rH   rM   _resolve_tool_lookup_keyr   r   )rE   rA   rL   
lookup_keyapproval_keyss        r+   _resolve_approval_key'RunContextWrapper._resolve_approval_keyb   sr    %88G	*BB=Q&??N
7)&.8D.@	
  $$"9=\\n\r*   c           	         [         R                  U 5      n[        [         R                  U 5      [         R	                  U 5      [        U SS5      UUSL S9$ )zAReturn all approval keys that should mirror this approval record._allow_bare_name_aliasFN)rA   rL   allow_bare_name_aliasrP   rQ   )r-   rS   r   rH   rM   rC   )rE   rT   s     r+   _resolve_approval_keys(RunContextWrapper._resolve_approval_keysq   sV     '??N
.'::=I,DD]S")-9QSX"Y&.8D.@
 	
r*   c                d   [        U SS 5      n[        U[        5      (       a  U$ U R                  n[        U[        5      (       a  UR                  S5      nO[        USS 5      nUS:w  a  g [        R                  U 5      n[        R                  U 5      n[        XE5      (       a  g [        XE5      $ )NrP   r>   function_call)rC   r6   tupler@   r(   rB   r-   rH   rM   r   r   )rE   rG   rF   raw_typerA   rL   s         r+   rS   *RunContextWrapper._resolve_tool_lookup_key}   s    M+<dC	i''$$c4  wwvHsFD1H&%88G	*BB=Q/	JJ+IFFr*   c                   U R                   n[        U[        5      (       a  UR                  S5      n[        U[        5      (       a=  UR                  S5      S:X  a(  UR                  S5      n[        U[        5      (       a  U$ UR                  S5      =(       d    UR                  S5      nO[        USS 5      n[        U[        5      (       a=  UR                  S5      S:X  a(  UR                  S5      n[        U[        5      (       a  U$ [        USS 5      =(       d    [        USS 5      n[        R                  U5      $ )Nprovider_datar>   mcp_approval_requestidcall_id)r@   r6   r(   rB   r7   rC   r-   r:   )rE   rF   rc   rG   s       r+   _resolve_call_id"RunContextWrapper._resolve_call_id   s   $$c4  GGO4M=$//!%%f-1GG)--d3	i--$$	*;cggdmI#C$?M=$//!%%f-1GG)--d3	i--$$Y5QdD9QI 00;;r*   c                r    U R                   R                  U5      nUc  [        5       nX R                   U'   U$ r5   )r2   rB   r   )selfrA   approval_entrys      r+   _get_or_create_approval_entry/RunContextWrapper._get_or_create_approval_entry   s6    ,,Y7!,.N)7OOI&r*   c                $    U R                  X5      $ )z/Return True/False/None for the given tool call.)_get_approval_status_for_key)rj   rA   rf   s      r+   is_tool_approved"RunContextWrapper.is_tool_approved   s    00DDr*   c                   U R                   R                  U5      nU(       d  gUR                  SL a  UR                  SL a  gUR                  SL a  gUR                  SL a  g[	        UR                  [
        5      (       a  [        UR                  5      O	[        5       n[	        UR                  [
        5      (       a  [        UR                  5      O	[        5       nX$;   a  gX%;   a  gg)zAReturn True/False/None for a concrete approval key and tool call.NTF)r2   rB   r   r   r6   r&   set)rj   approval_keyrf   rk   approved_idsrejected_idss         r+   ro   .RunContextWrapper._get_approval_status_for_key   s    ,,\: ""d*~/F/F$/N""d*""d* -7~7N7NPT,U,UC''([^[` 	 -7~7N7NPT,U,UC''([^[` 	 ""r*   c                D    Uc  g U R                   R                  US 5        g r5   )r   poprecordrf   s     r+   _clear_rejection_message*RunContextWrapper._clear_rejection_message   s     ?!!%%gt4r*   c                   U R                   SL a*  XR                  ;   a  U R                  U   $ U R                  $ [        U R                   [        5      (       a*  XR                   ;   a  U R                  R                  U5      $ g )NT)r   r   r   r6   r&   rB   rz   s     r+   _get_rejection_message_for_key0RunContextWrapper._get_rejection_message_for_key   sk    ??d"3330099222foot,,OO1K,,0099r*   )rL   existing_pendingrP   c          	     l   / n[        U[        5      (       a	  U(       a  UOSnUb  U R                  U5      OSnU(       a  U R                  U5      OSn	U(       a  U R	                  U5      OSn
Ub  [        U R                  U5      5      O/ nU(       a  U	b  UR                  U	5        Uc  Ub  [        [        UUUSS95      O/ nU H  nX;  d  M
  UR                  U5        M     U(       d   U(       a  U	b  X;  a  UR                  U	5        Uc  Uc  Uc  X;  a  UR                  U5        U(       a=  U H  nX;  d  M
  UR                  U5        M     Uc  U
b  X;  a  UR                  U
5        U H@  nU R                  R                  U5      nU(       d  M'  U R                  UU5      nUc  M>  Us  $    g)z@Return a stored rejection message for a tool call if one exists.NTrA   rL   rP   include_legacy_deferred_key)r6   r7   rM   rV   rH   r&   r[   appendr   r2   rB   r   )rj   rA   rf   rL   r   rP   
candidatesexplicit_namespacepending_namespacepending_keypending_tool_namepending_keysexplicit_keysexplicit_keypending_candidaterG   rk   messages                     r+   get_rejection_message'RunContextWrapper.get_rejection_message   s    !#
(==.NVZ 	 ?O>ZD(()9:`d 	 GWd001AB\`IYD334DE_c  + ,,-=>? 	  7k* "-1L /'#5$304	  	 *L-!!,/ * !2{7N,!!+.&' (+i(%1!$6%%&78 &2 ")%1%7!!"34#I!__00;N!99.'RG" $ r*   )rejection_messagec                  U R                  U5      =(       d    SnU R                  U5      nU R                  U5      nU(       d  Uc  U4OUnU GHp  n	U R                  U	5      n
U(       d  Ucw  X:l        U(       a  / OSU
l        U(       d7  SU
l        Ub  Ub  XJR                  U'   OUb  U R                  X5        XJl        O!U
R                  R                  5         SU
l        M  U(       a  U
R
                  OU
R                  n[        U[        5      (       a  X{;   a  UR                  U5        U(       a  U
R                  OU
R
                  n[        U[        5      (       a  X|;  a  UR                  U5        U(       a  U R                  X5        GME  Uc  GMK  Ub  XJR                  U'   GM_  U R                  X5        GMs     g)z)Record an approval or rejection decision.)r?   NTF)r[   rV   rg   rl   r   r   r   r|   r   clearr6   r&   remover   )rj   rE   alwaysapprover   rU   exact_approval_keyrf   decision_keysrt   rk   oppositetargets                r+   _apply_approval_decision*RunContextWrapper._apply_approval_decision,  sz    33MBWFW!77F''6177?+-P])L!??MN*1'07"T'.3N+(49LEV99'B ,55nN>O;"55;;=>BN;29~..~?V?VH(D))g.A(07^,,^=T=TF&$''G,Ag&--nF$$0AR55g>11.J= *r*   c                &    U R                  UUSS9  g)z5Approve a tool call, optionally for all future calls.T)r   r   Nr   )rj   rE   always_approves      r+   approve_toolRunContextWrapper.approve_toolZ  s    %%! 	& 	
r*   c                (    U R                  UUSUS9  g)z4Reject a tool call, optionally for all future calls.F)r   r   r   Nr   )rj   rE   always_rejectr   s       r+   reject_toolRunContextWrapper.reject_toolb  s#     	%% /	 	& 	
r*   c          	     (   / n[        U[        5      (       a	  U(       a  UOSnUb  U R                  U5      OSnU(       a  U R                  U5      OSn	U(       a  U R	                  U5      OSn
Ub  [        U R                  U5      5      O/ nU(       a  U	b  UR                  U	5        Uc  Ub  [        [        UUUSS95      O/ nU H  nX;  d  M
  UR                  U5        M     U(       d   U(       a  U	b  X;  a  UR                  U	5        Uc  Uc  Uc  X;  a  UR                  U5        U(       a=  U H  nX;  d  M
  UR                  U5        M     Uc  U
b  X;  a  UR                  U
5        SnU H  nU R                  UU5      nUc  M    U$    U$ )zLReturn approval status, retrying with pending item's tool name if necessary.NTr   )
r6   r7   rM   rV   rH   r&   r[   r   r   ro   )rj   rA   rf   rL   r   rP   r   r   r   r   r   r   r   r   r   statusrG   s                    r+   get_approval_status%RunContextWrapper.get_approval_statusp  s    !#
(==.NVZ 	 ?O>ZD(()9:`d 	 GWd001AB\`IYD334DE_c  + ,,-=>? 	  7k* "-1L /'#5$304	  	 *L-!!,/ * !2{7N,!!+.&' (+i(%1!$6%%&78 &2 ")%1%7!!"34"#I66y'JF!	 $ r*   c           	        0 U l         UR                  5        H  u  p#[        5       nUR                  S/ 5      Ul        UR                  S/ 5      Ul        UR                  S0 5      n[        U[        5      (       aK  UR                  5        VVs0 s H(  u  pg[        U[        5      (       d  M  [        U5      U_M*     snnUl	        UR                  S5      n[        U[        5      (       a  Xl
        X@R                   U'   M     gs  snnf )z(Restore approvals from serialized state.r   r   r   r   N)r2   itemsr   rB   r   r   r6   r(   r7   r   r   )	rj   	approvalsrA   record_dictr{   r   rf   r   r   s	            r+   _rebuild_approvals$RunContextWrapper._rebuild_approvals  s    &/oo&7"I$&F)ooj"=FO)ooj"=FO!,1Er!J,d33 -?,D,D,F-,F(!'3/ *CL'),F-)
 (37Q'R$2C882J/)/OOI& '8-s   D.Dc                    [        U R                  S9nU R                  Ul        U R                  Ul        U R                  Ul        Xl        U$ )zKCreate a child context that shares approvals and usage with tool input set.r/   )r-   r/   r0   r2   r1   r3   )rj   r3   forks      r+   _fork_with_tool_input'RunContextWrapper._fork_with_tool_input  s;     6ZZ
////$r*   c                    [        U R                  S9nU R                  Ul        U R                  Ul        U R                  Ul        U$ )zJCreate a child context that shares approvals and usage without tool input.r   )r-   r/   r0   r2   r1   )rj   r   s     r+   _fork_without_tool_input*RunContextWrapper._fork_without_tool_input  s6     6ZZ
////r*   )r2   )r9   r   returnr   )rE   r   r   r7   )rE   r   r   r   )rE   r   r   ztuple[str, ...])rE   r   r   FunctionToolLookupKey | None)rA   r7   r   r   )rA   r7   rf   r7   r   bool | None)rt   r7   rf   r7   r   r   )r{   r   rf   r   r   None)r{   r   rf   r7   r   r   )rA   r7   rf   r7   rL   r   r   ToolApprovalItem | NonerP   r   r   r   )
rE   r   r   boolr   r   r   r   r   r   )F)rE   r   r   r   r   r   )FN)rE   r   r   r   r   r   r   r   )rA   r7   rf   r7   rL   r   r   r   rP   r   r   r   )r   zdict[str, dict[str, Any]]r   r   )r3   r   r   RunContextWrapper[TContext])r   r   )$r!   r"   r#   r$   r%   r'   r   r   r0   r&   r1   r(   r2   r3   staticmethodr:   rH   rM   rV   r[   rS   rg   rl   rp   ro   r|   r   r   r   r   r   r   r   r   r   r)   r    r*   r+   r-   r-   *   sI    G/E5/ ,1+FJ(F-24-HJ*H!J
!J  	N 	N < < ] ] 	
 	
 G G& < <0E> 5 5
   &*488<FF F
 #F 2F 6F 
F\ )-,K',K 	,K
 ,K &,K 
,K\
 $(,	
'
 
 &	

 

& &*488<DD D
 #D 2D 6D 
DL0&r*   r-   c                      \ rS rSrSrSrg)AgentHookContexti  z1Context passed to agent hooks (on_start, on_end).r    N)r!   r"   r#   r$   r%   r)   r    r*   r+   r   r     s    ;r*   r   N)
__future__r   dataclassesr   r   typingr   r   r   typing_extensionsr	   _tool_identityr   r   r   r   r   r0   r   r   r   r   r   r   r-   r   r    r*   r+   <module>r      s    " ( . . %  ;; :s+ e
0 
0 
0 em) m m` e<(2 < <r*   