+
    ~j                      a  0 t $ ^ RIHt ^ RIt^ RIt^ RIHtHt ^ RIH	t	H
t
HtHtHtHt ^ RIHtHtHtHt ^ RIHt ^ RIHt ^RIHt ^R	IHt ^R
IHtHt ^RIH t  ^RIH!t! ]	'       d   ^RI"H#t#H$t$H%t%  ! R R]&4      t' ! R R]4      t( ! R R]4      t)]R3,          t*R]+R&    ! R R]RR7      t, ! R R]RR7      t- ! R R]4      t.](]),          ]',          ]&,          t/R]+R&    ! R R ]4      t0 ! R! R"]4      t1 ! R# R$]4      t2 ! R% R&]4      t3]
].]0,          ]1,          ]2,          ]3,          ]! R'R(7      3,          t4 ! R) R*4      t5 ! R+ R,]54      t6R4R- R. llt7R5R/ R0 llt8R1 R2 lt9R# )6    )annotationsN)	GeneratorSequence)TYPE_CHECKING	AnnotatedAnyLiteral	TypeAliasoverload)	BaseModelFieldPrivateAttrTypeAdapter)	TypedDict)rtc)utils)logger)	NOT_GIVEN
NotGivenOr)is_given)_provider_format)LLMToolToolsetc                     a  ] tR t^%t$ RtR]R&   R]R&   RRRR/R	 V 3R
 lllt]R R l4       t]R R l4       t	R R lt
R R ltR R ltR R ltR R lt]R R l4       tRtV ;t# )Instructionsa  Instructions that adapt based on the user's input modality (audio vs. text).

``str(self)`` is what providers see when treating this as a plain string.
By default it equals the ``audio`` variant; after :meth:`as_modality` it
equals the chosen variant.

``_audio_variant`` and ``_text_variant`` are always preserved so
:meth:`as_modality` can be called again for a different modality (e.g.,
when the same ``ChatContext`` is reused across tool-call turns).
str_audio_variant
str | None_text_varianttextN
_representc               (    V ^8  d   QhRRRRRRRR/# )   audior   r!   r   r"   returnr    )formats   "t/Users/mitch_tango/dev/rabbit-r1-livekit/agent/.venv/lib/python3.14/site-packages/livekit/agents/llm/chat_context.py__annotate__Instructions.__annotate__4   s,      ",AK	    c               J   < \         ST `  Ye   TMT4      pWn        W$n        V# )zCreate an Instructions object.

Args:
    audio: The audio (voice) variant.
    text: The text variant.  Falls back to ``audio`` when omitted.
)super__new__r   r    )clsr%   r!   r"   instance	__class__s   &&$$ r)   r/   Instructions.__new__4   s,     7?36L
RWX"'!%r,   c                   V ^8  d   QhRR/# r$   r&   r   r'   )r(   s   "r)   r*   r+   C   s     # #s #r,   c                    V P                   # )z.The audio (voice) variant of the instructions.)r   selfs   &r)   r%   Instructions.audioB   s     """r,   c                   V ^8  d   QhRR/# r5   r'   )r(   s   "r)   r*   r+   H   s     ] ]c ]r,   c                N    V P                   e   V P                   # V P                  # )zjThe text variant of the instructions.

Falls back to the audio variant when no text variant was provided.
)r    r   r7   s   &r)   r!   Instructions.textG   s&     &*%7%7%Ct!!\I\I\\r,   c               $    V ^8  d   QhRRRRRR/# )r$   argsobjectkwargsr&   r   r'   )r(   s   "r)   r*   r+   O   s!     
 
F 
f 
 
r,   c           	        \         ;QJ d    R V 4       F  '       g   K   RM	  RM! R V 4       4      ;'       gQ    \         ;QJ d*    R VP                  4        4       F  '       g   K   RM	  RM! R VP                  4        4       4      pV'       d   \        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      p\        ;QJ d    . R V 4       F  NK  	  5M! R V 4       4      pVP                  4        UUu/ uF*  w  rgT\	        V\
        4      '       d   VP                  MTbK,  	  pppVP                  4        UUu/ uF*  w  rgT\	        V\
        4      '       d   VP                  MTbK,  	  p	ppMT;rET;r\        V P                  P                  ! V/ VB V'       g   V P                  e   V P                  P                  ! V/ V	B MR\        V 4      P                  ! V/ VB R7      # u uppi u uppi )	z9Format the instructions with the given keyword arguments.c              3  B   "   T F  p\        V\        4      x  K  	  R # 5iN
isinstancer   .0args   & r)   	<genexpr>&Instructions.format.<locals>.<genexpr>R   s     Mz#|<<   TFc              3  B   "   T F  p\        V\        4      x  K  	  R # 5irC   rD   )rG   vs   & r)   rI   rJ   R   s      U
1@AJq,''rK   c              3  j   "   T F)  p\        V\        4      '       d   VP                  MTx  K+  	  R # 5irC   )rE   r   r%   rF   s   & r)   rI   rJ   V   s(     c^bWZJsL,I,IsyysR^b   13c              3  j   "   T F)  p\        V\        4      '       d   VP                  MTx  K+  	  R # 5irC   )rE   r   r!   rF   s   & r)   rI   rJ   W   s(     a\`UX*S,*G*GchhSP\`rO   Nr%   r!   r"   )anyvaluestupleitemsrE   r   r%   r!   r(   r    r   )
r8   r>   r@   any_instructions
audio_args	text_argskrM   audio_kwargstext_kwargss
   &*,       r)   r(   Instructions.formatO   s    3MM333MMM 
 
QTQT U
17U
QTQTQT U
17U
 R
 c^bcc^bccJa\`aa\`aaIMS\\^M[TQjL99177q@^   MSLLNLZDAZ<88166a?N  K &*)J)//L**##Z@<@ $t'9'9'E 		  );{;4y''88
 	
s    0G30G9c                    V ^8  d   QhRRRR/# )r$   modalityzLiteral['audio', 'text']r&   r   r'   )r(   s   "r)   r*   r+   l   s     

 

$< 

 

r,   c                    \        V P                  V P                  VR8X  d   V P                  R7      # V P                  R7      # )zReturn a copy whose ``str`` value is the correct variant for *modality*.

Both ``_audio_variant`` and ``_text_variant`` are preserved so this can
be called again for a different modality (e.g. across tool-call turns).
r%   rQ   )r   r   r    r%   r!   )r8   r^   s   &&r)   as_modalityInstructions.as_modalityl   sE     %%##%-%8tzz
 	
 ?Cii
 	
r,   c                    V ^8  d   QhRRRR/# r$   otherr?   r&   r   r'   )r(   s   "r)   r*   r+   x   s     F FV F Fr,   c                >   \        V\        4      '       d   V P                  RJ;'       g    VP                  RJp\        V P                  VP                  ,           V'       d   V P                  VP                  ,           MR\        V 4      \        V4      ,           R7      # \        V\
        4      '       dQ   \        V P                  V,           V P                  e   V P                  V,           MR\        V 4      V,           R7      # \        R\        V4       24      h)zAConcatenate, propagating both variants and the current str value.NrQ   zCannot add Instructions and )rE   r   r    r%   r!   r   	TypeErrortype)r8   rd   has_texts   && r)   __add__Instructions.__add__x   s    e\**))5XX9L9LTX9XHjj5;;.19dii%**,tt9s5z1 
 eS!!jj5(595G5G5Sd((50Y]t9u, 
 6tE{mDEEr,   c                    V ^8  d   QhRRRR/# rc   r'   )r(   s   "r)   r*   r+      s     F Ff F Fr,   c                    \        V\        4      '       dO   \        WP                  ,           V P                  e   WP                  ,           MRV\        V 4      ,           R7      # \        R\        V4       R24      h)z@Support ``plain_str + Instructions``, propagating both variants.NrQ   zCannot add z and Instructions)rE   r   r   r%   r    rf   rg   )r8   rd   s   &&r)   __radd__Instructions.__radd__   sg    eS!!jj(595G5G5Se000Y] 3t9, 
 +d5k]2CDEEr,   c                   V ^8  d   QhRR/# r5   r'   )r(   s   "r)   r*   r+      s     . .# .r,   c                	"    R \        V 4      : R2# )zInstructions())r   r7   s   &r)   __repr__Instructions.__repr__   s    s4ym1--r,   c               $    V ^8  d   QhRRRRRR/# )r$   source_typer   handlerr&   r'   )r(   s   "r)   r*   r+      s!     
 
s 
S 
S 
r,   c           	     	   a  ^ RI Hp R V 3R llpR V 3R llpR R lpVP                  VP                  V4      VP                  V4      VP	                  VRR	7      R
7      # )r   )core_schemac                    V ^8  d   QhRRRR/# r$   rM   r   r&   r   r'   )r(   s   "r)   r*   ?Instructions.__get_pydantic_core_schema__.<locals>.__annotate__   s     	L 	Ls 	L| 	Lr,   c                   < \        V \        4      '       d   V # \        V \        4      '       d7   V P                  R 4      R8X  d!   S! V R,          V P                  R4      R7      # \	        R\        V 4      : R24      hrg   instructionsr%   r!   )r!   zCannot convert z to Instructions)rE   r   dictget
ValueErrorrg   rM   r0   s   &r)   validate_pythonBInstructions.__get_pydantic_core_schema__.<locals>.validate_python   sc    !\**!T""quuV}'F1W:AEE&M::tAwk9IJKKr,   c                    V ^8  d   QhRRRR/# rz   r'   )r(   s   "r)   r*   r{      s     	L 	LS 	L\ 	Lr,   c                   < \        V \        4      '       d7   V P                  R 4      R8X  d!   S! V R,          V P                  R4      R7      # \        R\	        V 4      : R24      hr}   )rE   r   r   r   rg   r   s   &r)   validate_json@Instructions.__get_pydantic_core_schema__.<locals>.validate_json   sQ    !T""quuV}'F1W:AEE&M::tAwk9IJKKr,   c                    V ^8  d   QhRRRR/# )r$   rM   r   r&   dict[str, Any]r'   )r(   s   "r)   r*   r{      s     	 	 	. 	r,   c                `    R RRV P                   /pV P                  e   V P                  VR&   V# )rg   r~   r%   r!   )r%   r    )rM   ds   & r)   	serialize<Instructions.__get_pydantic_core_schema__.<locals>.serialize   s0    !'!'' JA*OO&	Hr,   F)info_arg)python_schemajson_schemaserialization)pydantic_corerx   json_or_python_schema no_info_plain_validator_function$plain_serializer_function_ser_schema)r0   ru   rv   rx   r   r   r   s   f&&    r)   __get_pydantic_core_schema__)Instructions.__get_pydantic_core_schema__   sr    -	L 	L	L 	L
	 00%FFW#DD]S%JJE K  1 
 	
r,   r'   )__name__
__module____qualname____firstlineno____doc____annotations__r/   propertyr%   r!   r(   r`   ri   rm   rr   classmethodr   __static_attributes____classcell__)r2   s   @r)   r   r   %   s    	 /3NR  # # ] ]
:

F"F. 
 
r,   r   c                      ] tR t^t$ Rt]! R R7      tR]R&    ]! RR7      tR]R	&   R
]R&    Rt	R]R&    Rt
R]R&    RtR]R&    RtR]R&    ]! ]R7      tR]R&   RtR# )ImageContenta  
ImageContent is used to input images into the ChatContext on supported LLM providers / plugins.

You may need to consult your LLM provider's documentation on supported URL types.

```python
# Pass a VideoFrame directly, which will be automatically converted to a JPEG data URL internally
async for event in rtc.VideoStream(video_track):
    chat_image = ImageContent(image=event.frame)
    # this instance is now available for your ChatContext

# Encode your VideoFrame yourself for more control, and pass the result as a data URL (see EncodeOptions for more details)
from livekit.agents.utils.images import encode, EncodeOptions, ResizeOptions

image_bytes = encode(
    event.frame,
    EncodeOptions(
        format="PNG",
        resize_options=ResizeOptions(width=512, height=512, strategy="scale_aspect_fit"),
    ),
)
chat_image = ImageContent(
    image=f"data:image/png;base64,{base64.b64encode(image_bytes).decode('utf-8')}"
)

# With an external URL
chat_image = ImageContent(image="https://example.com/image.jpg")
```
c                 	.    \         P                  ! R 4      # )img_r   	shortuuidr'   r,   r)   <lambda>ImageContent.<lambda>   s    EOOF,Cr,   default_factoryr   idimage_contentdefaultzLiteral['image_content']rg   zstr | rtc.VideoFrameimageN
int | Noneinference_widthinference_heightautozLiteral['auto', 'high', 'low']inference_detailr   	mime_typezdict[Any, Any]_cacher'   )r   r   r   r   r   r   r   r   rg   r   r   r   r   r   r   r   r   r'   r,   r)   r   r      s    < $CDBD &+?%CD
"C #'OZ& $(j' 8>4=
 !Iz  )>FN>r,   r   c                  L    ] tR t^t$ ]! RR7      tR]R&   R]R&   RtR]R	&   R
tR# )AudioContentaudio_contentr   zLiteral['audio_content']rg   zlist[rtc.AudioFrame]frameNr   
transcriptr'   )	r   r   r   r   r   rg   r   r   r   r'   r,   r)   r   r      s"    %*?%CD
"C!J
!r,   r   r
   ChatRolec                  ,    ] tR t^t$ R]R&   R]R&   RtR# )MetricsMetadatar   
model_namemodel_providerr'   Nr   r   r   r   r   r   r'   r,   r)   r   r      s    Or,   r   F)totalc                      ] tR tRt$ R]R&   R]R&   R]R&    R]R&    R]R&    R]R&    R]R	&    R]R
&    R]R&    R]R&   R]R&   R]R&   RtR# )MetricsReporti  floatstarted_speaking_atstopped_speaking_attranscription_delayend_of_turn_delayon_user_turn_completed_delayllm_node_ttfttts_node_ttfbplayback_latencye2e_latencyr   llm_metadatatts_metadatastt_metadatar'   Nr   r'   r,   r)   r   r     s{    
 
 #('
 
 
  
 "!!!!!r,   r   c                     ] tR tRt$ ]! R R7      tR]R&   RtR]R&   R	]R
&   R]R&   RtR]R&   Rt	R]R&   ]! ]
R7      tR]R&   ]! R R7      tR]R&   ]! ]P                  R7      tR]R&   ]! RRR7      tR]R&   ]R R l4       tR tR# )!ChatMessagei;  c                 	.    \         P                  ! R 4      # item_r   r'   r,   r)   r   ChatMessage.<lambda><      EOOG,Dr,   r   r   r   messagezLiteral['message']rg   r   rolezlist[ChatContent]contentFboolinterruptedNzfloat | Nonetranscript_confidencer   extrac                 	    \        4       # rC   )r   r'   r,   r)   r   r   C  s    =?r,   r   metricsr   
created_atzhash is deprecated)r   
deprecatedzbytes | Nonehashc                   V ^8  d   QhRR/# )r$   r&   r   r'   )r(   s   "r)   r*   ChatMessage.__annotate__H  s     	% 	%j 	%r,   c                    V P                    Uu. uF  p\        V\        4      '       g   K  VNK  	  ppV'       g   R# RP                  V4      # u upi )zp
Returns a string of all text content in the message.

Multiple text content items will be joined by a newline.
N
)r   rE   r   join)r8   c
text_partss   &  r)   text_contentChatMessage.text_contentG  sB     "&DAAs1Caa
Dyy$$ Es
   AAr'   )r   r   r   r   r   r   r   rg   r   r   r   r   r   timer   r   r   r   r   r'   r,   r)   r   r   ;  s    $DEBE(D
(
NK*.<.!$7E>7"3JKG]Kdii8J8t8LMD,M	% 	%r,   r   ChatContentc                      ] tR tRt$ ]! R R7      tR]R&   RtR]R&   R]R	&   R]R
&   R]R&   ]! ]P                  R7      t	R]R&   ]! ]
R7      tR]R&    RtR]R&   RtR# )FunctionCalliW  c                 	.    \         P                  ! R 4      # r   r   r'   r,   r)   r   FunctionCall.<lambda>X  r   r,   r   r   r   function_callzLiteral['function_call']rg   call_id	argumentsnamer   r   r   r   Nr   group_idr'   )r   r   r   r   r   r   r   rg   r   r   r   r   r   r   r'   r,   r)   r   r   W  sc    $DEBE%4D
"4LN
Idii8J8!$7E>77HjTr,   r   c                      ] tR tRt$ ]! R R7      tR]R&   ]! RR7      tR]R	&   ]! R
R7      tR]R&   R]R&   R]R&   R]R&   ]! ]	P                  R7      t
R]R&   RtR# )FunctionCallOutputig  c                 	.    \         P                  ! R 4      # r   r   r'   r,   r)   r   FunctionCallOutput.<lambda>h  r   r,   r   r   r   function_call_outputr   zLiteral['function_call_output']rg    r   r   outputr   is_errorr   r   r'   N)r   r   r   r   r   r   r   rg   r   r   r   r   r'   r,   r)   r   r   g  sT    $DEBE,1:P,QD
)Qb!D#!LKNdii8J8r,   r   c                      ] tR tRt$ ]! R R7      tR]R&   ]! RR7      tR]R	&   R
tR]R&   R]R&   ]! ]	P                  R7      t
R]R&   RtR
# )AgentHandoffiq  c                 	.    \         P                  ! R 4      # r   r   r'   r,   r)   r   AgentHandoff.<lambda>r  r   r,   r   r   r   agent_handoffr   zLiteral['agent_handoff']rg   Nr   old_agent_idnew_agent_idr   r   r'   )r   r   r   r   r   r   r   rg   r  r   r   r   r'   r,   r)   r  r  q  sE    $DEBE%*?%CD
"C#L*#dii8J8r,   r  c                      ] tR tRt$ ]! R R7      tR]R&   ]! RR7      tR]R	&   R
tR]R&   R
t	R]R&   R
t
R]R&   ]! ]P                  R7      tR]R&   ]! ]R7      tR]R&   RtR
# )AgentConfigUpdateiy  c                 	.    \         P                  ! R 4      # r   r   r'   r,   r)   r   AgentConfigUpdate.<lambda>z  r   r,   r   r   r   agent_config_updater   zLiteral['agent_config_update']rg   NzInstructions | str | Noner~   zlist[str] | Nonetools_addedtools_removedr   r   z
list[Tool]_toolsr'   )r   r   r   r   r   r   r   rg   r~   r  r  r   r   r   listr  r   r'   r,   r)   r  r  y  si    $DEBE+09N+OD
(O.2L+2$(K!(&*M#*dii8J8$T:FJ:Ar,   r  rg   )discriminatorc                  r   ] tR tRt]3R R llt]R R l4       t]R R l4       t	]	P                  R R	 l4       t	R
 R ltR]R]R]R]R]/R R lltR R ltR R ltR R ltRRRRRRRRRRR]/R  R! lltR" R# ltRRRRRR/R$ R% lltR&R'R(R'R)R'RRR*RRR/R+ R, llt]R-R'/R. R/ ll4       t]R-R'R0R1/R2 R3 ll4       t]R-R'/R4 R5 ll4       t]R-R'/R6 R7 ll4       t]R8 R9 l4       t]R: R; l4       tR-R'/R< R= lltR> R? ltR@R/RA RB lltRC^/RD RE llt]RF RG l4       t]RH RI l4       tRJ RK ltRLtR1# )MChatContexti  c                   V ^8  d   QhRR/# )r$   rU   zNotGivenOr[list[ChatItem]]r'   )r(   s   "r)   r*   ChatContext.__annotate__  s     G G8 Gr,   c                	F    \        V4      '       d	   Wn        R # . V n        R # rC   )r   _itemsr8   rU   s   &&r)   __init__ChatContext.__init__  s    /7eBr,   c                   V ^8  d   QhRR/# )r$   r&   r  r'   )r(   s   "r)   r*   r    s      k r,   c                	    V ! . 4      # rC   r'   )r0   s   &r)   emptyChatContext.empty  s    2wr,   c                   V ^8  d   QhRR/# r$   r&   list[ChatItem]r'   )r(   s   "r)   r*   r    s      ~ r,   c                	    V P                   # rC   r  r7   s   &r)   rU   ChatContext.items  s    {{r,   c                    V ^8  d   QhRRRR/# )r$   rU   r#  r&   Noner'   )r(   s   "r)   r*   r    s      > d r,   c                	    Wn         R # rC   r%  r  s   &&r)   rU   r&    s    r,   c                   V ^8  d   QhRR/# )r$   r&   zlist[ChatMessage]r'   )r(   s   "r)   r*   r    s     O O+ Or,   c                t    V P                    Uu. uF  p\        V\        4      '       g   K  VNK  	  up# u upi )zNReturn only chat messages, ignoring function calls, outputs, and other events.)r  rE   r   )r8   items   & r)   messagesChatContext.messages  s)    !%N
40MNNNs   55r   r   r   r   r   c               8    V ^8  d   QhRRRRRRRRR	R
RRRRRR/# )r$   r   r   r   zlist[ChatContent] | strr   zNotGivenOr[str]r   zNotGivenOr[bool]r   zNotGivenOr[float]r   zNotGivenOr[MetricsReport]r   zNotGivenOr[dict[str, Any]]r&   r   r'   )r(   s   "r)   r*   r    s\     ! ! ! )	!
 ! &! &! +! *! 
!r,   c               	   / p\        V4      '       d   W8R &   \        V4      '       d   WHR&   \        V4      '       d   WXR&   \        V4      '       d   WhR&   \        V4      '       d   WxR&   \        V\        4      '       d   \        RRVRV./VB p	M\        RRVRV/VB p	\        V4      '       d0   V P	                  VR7      p
V P
                  P                  W4       V	# V P
                  P                  V	4       V	# )	r   r   r   r   r   r   r   r   r'   )r   rE   r   r   find_insertion_indexr  insertappend)r8   r   r   r   r   r   r   r   r@   r   idxs   &$$$$$$$   r)   add_messageChatContext.add_message  s     "$B<<4LK  $/=!J#-< G '9E??#7Ogs##!ItIgYI&IG!GtGWGGGJ++z+BCKKs,  KKw'r,   c                    V ^8  d   QhRRRR/# )r$   r,  zChatItem | Sequence[ChatItem]r&   r(  r'   )r(   s   "r)   r*   r    s     + +8 +T +r,   c                    \        V\        4      '       d   \        V4      MV.pV F:  pV P                  VP                  R7      pV P
                  P                  WC4       K<  	  R# )zGInsert an item or list of items into the chat context by creation time.r1  N)rE   r   r  r2  r   r  r3  )r8   r,  rU   _itemr5  s   &&   r)   r3  ChatContext.insert  sP    (x88T
tfE++u7G7G+HCKKs* r,   c                    V ^8  d   QhRRRR/# )r$   item_idr   r&   zChatItem | Noner'   )r(   s   "r)   r*   r    s     O O O Or,   c                	D   a \        V3R  lV P                   4       R4      # )c              3  L   <"   T F  qP                   S8X  g   K  Vx  K  	  R # 5irC   r   )rG   r,  r=  s   & r)   rI   (ChatContext.get_by_id.<locals>.<genexpr>  s     GjdGGw4FTTjs   $
$N)nextrU   r8   r=  s   &fr)   	get_by_idChatContext.get_by_id  s    GdjjGNNr,   c                    V ^8  d   QhRRRR/# )r$   r=  r   r&   r   r'   )r(   s   "r)   r*   r    s     Z Z3 Z: Zr,   c                	V   a \        V3R  l\        V P                  4       4       R4      # )c              3  R   <"   T F  w  rVP                   S8X  g   K  Vx  K  	  R # 5irC   r@  )rG   ir,  r=  s   &  r)   rI   *ChatContext.index_by_id.<locals>.<genexpr>  s"     R&;71tww'?QQQ&;s   '
'N)rB  	enumeraterU   rC  s   &fr)   index_by_idChatContext.index_by_id  s     Ri

&;RTXYYr,   exclude_function_callFexclude_instructionsexclude_empty_messageexclude_handoffexclude_config_updatetoolsc               4    V ^8  d   QhRRRRRRRRRRRRR	R
/# )r$   rN  r   rO  rP  rQ  rR  rS  z*NotGivenOr[Sequence[Tool | Toolset | str]]r&   r  r'   )r(   s   "r)   r*   r    sR     =" ="  $=" #	="
  $=" ="  $=" :=" 
="r,   c               	  a
aaa . p^RI Ho
HoHo R V
VVV3R lloV'       d   \	        S! V4      4      M	\	        4       pV P
                   EF  p	V'       d   V	P                  R	9   d   K  V'       d%   V	P                  R8X  d   V	P                  R
9   d   KK  V'       d&   V	P                  R8X  d   V	P                  '       g   Kx  V'       d   V	P                  R8X  d   K  V'       d   V	P                  R8X  d   K  \        V4      '       d6   V	P                  R8X  g   V	P                  R8X  d   V	P                  V9  d   K  VP                  V	4       EK  	  \        V4      # )   )FunctionToolRawFunctionToolr   c                    V ^8  d   QhRRRR/# )r$   rS  zSequence[Tool | Toolset | str]r&   zGenerator[str, None, None]r'   )r(   s   "r)   r*   &ChatContext.copy.<locals>.__annotate__  s     	 	1	'	r,   c              3    <"   V  F{  p\        V\        4      '       d   Vx  K  \        VSS34      '       d   VP                  P                  x  KM  \        VS4      '       d   S! VP                  4       R j  xL
  K{  K}  	  R #  L5irC   )rE   r   infor   rS  )rS  toolrW  rX  r   get_tool_namess   & r)   r^  (ChatContext.copy.<locals>.get_tool_names  sm      dC((J|_&EFF))..(g..-djj999   :s   A7B	:B;B	r   r   r   r  r  r   r   system	developer)tool_contextrW  rX  r   setrU   rg   r   r   r   r   r4  r  )r8   rN  rO  rP  rQ  rR  rS  rU   valid_toolsr,  rW  rX  r   r^  s   &$$$$$$   @@@@r)   copyChatContext.copy  s     HH	 	 5:c./0suJJD$ 7 *  %II*II!88$i)?499#?$6K)K YY/1TYYBX5XII[0LL= @ 5!!r,   c                    V ^8  d   QhRRRR/# )r$   	max_itemsintr&   r  r'   )r(   s   "r)   r*   r    s     ! !S ![ !r,   c                 a \        V P                  4      V8:  d   V # \        R V P                   4       R4      oV P                  V) R pV'       d,   V^ ,          P                  R9   d   VP	                  ^ 4       K3  S'       dS   \
        ;QJ d    V3R lV 4       F  '       g   K   RM	  RM! V3R lV 4       4      '       g   VP                  ^ S4       W P                  R&   V # )zTruncate the chat context to the last N items in place.

Removes leading function calls to avoid partial function outputs.
Preserves the first instruction message (system/developer) by adding it back
to the beginning.
c              3  r   "   T F-  pVP                   R 8X  g   K  VP                  R9   g   K)  Vx  K/  	  R# 5i)r   Nra  )rg   r   )rG   r,  s   & r)   rI   'ChatContext.truncate.<locals>.<genexpr>  s7      'D99	) .2ii;R.R 's   77
7Nc              3  T   <"   T F  qP                   SP                   8H  x  K  	  R # 5irC   r@  )rG   r,  r~   s   & r)   rI   rn  .  s     #U94GG|$>9s   %(TF:NNNr`  )lenr  rB  rg   poprR   r3  )r8   rj  	new_itemsr~   s   &$ @r)   truncateChatContext.truncate  s     t{{y(K KK
 
 KK
,	 IaL-- 2
 
 MM!#U9#U#U9#U U UQ-"Ar,   c          
     ,    V ^8  d   QhRRRRRRRRRR/# )r$   other_chat_ctxr  rN  r   rO  rR  r&   r'   )r(   s   "r)   r*   r  4  s<     ! !#!  $	!
 #!  $! 
!r,   c                  V P                    Uu0 uF  qUP                  kK  	  ppVP                   F  pV'       d   VP                  R9   d   K  V'       d%   VP                  R8X  d   VP                  R9   d   KJ  V'       d   VP                  R8X  d   Ke  VP                  V9  g   Kx  V P                  VP                  R7      pV P                   P                  Wu4       VP                  VP                  4       K  	  V # u upi )zkAdd messages from `other_chat_ctx` into this one, avoiding duplicates, and keep items sorted by created_at.r   r  r1  r`  ra  )	r  r   rU   rg   r   r2  r   r3  add)r8   rv  rN  rO  rR  r,  existing_idsr5  s   &&$$$   r)   mergeChatContext.merge4  s     -1KK8KDK8"((D$ 7 *  %II*II!88$6K)Kwwl*//4??/K""3-  )) ), 1 9s   Dexclude_imageTexclude_audioexclude_timestampexclude_metricsc               4    V ^8  d   QhRRRRRRRRRRRRRR	/# )
r$   r|  r   r}  r~  rN  r  rR  r&   r   r'   )r(   s   "r)   r*   r  W  sR     .
 .
 .
 	.

  .
  $.
 .
  $.
 
.
r,   c               	   . pV P                    F  pV'       d   VP                  R
9   d   K  V'       d   VP                  R8X  d   K9  VP                  R8X  d   VP                  4       pV'       d:   VP                   U	u. uF  p	\	        V	\
        4      '       d   K  V	NK  	  up	Vn        V'       d:   VP                   U	u. uF  p	\	        V	\        4      '       d   K  V	NK  	  up	Vn        VP                  V4       K  	  \        4       p
V'       d   V
P                  R4       V'       d   V
P                  R4       RV Uu. uF  pVP                  RRRV
R	7      NK  	  up/# u up	i u up	i u upi )r   r  r   r   r   rU   jsonTF)modeexclude_noneexclude_defaultsexcluder`  )rU   rg   
model_copyr   rE   r   r   r4  re  rx  
model_dump)r8   r|  r}  r~  rN  r  rR  rU   r,  r   exclude_fieldss   &$$$$$$    r)   to_dictChatContext.to_dictW  s:    !#JJD$ 7 * $6K)KyyI%( /3||#_|!:aQ]C^AA|#_DL /3||#_|!:aQ]C^AA|#_DLLL# & $'5|,y)  " "D !%%**	    "

 
	
 $`#_s   ?E#E# E(E(E-inject_dummy_user_messagec               $    V ^8  d   QhRRRRRR/# )r$   r(   z%Literal['openai', 'openai.responses']r  r   r&   z tuple[list[dict], Literal[None]]r'   )r(   s   "r)   r*   r    s(     / /5/ $(	/
 
*/r,   c               	    R # rC   r'   r8   r(   r  s   &&$r)   to_provider_formatChatContext.to_provider_format  s     ,/r,   thought_signaturesNc               (    V ^8  d   QhRRRRRRRR/# )	r$   r(   zLiteral['google']r  r   r  zdict[str, bytes] | Noner&   z;tuple[list[dict], _provider_format.google.GoogleFormatData]r'   )r(   s   "r)   r*   r    s9     J J!J $(	J
 4J 
EJr,   c               	    R # rC   r'   )r8   r(   r  r  s   &&$$r)   r  r    s	     GJr,   c               $    V ^8  d   QhRRRRRR/# )r$   r(   zLiteral['aws']r  r   r&   z9tuple[list[dict], _provider_format.aws.BedrockFormatData]r'   )r(   s   "r)   r*   r    s)     H H$HDHH	BHr,   c               	    R # rC   r'   r  s   &&$r)   r  r    s	     EHr,   c               $    V ^8  d   QhRRRRRR/# )r$   r(   zLiteral['anthropic']r  r   r&   zAtuple[list[dict], _provider_format.anthropic.AnthropicFormatData]r'   )r(   s   "r)   r*   r    s)     P P*PJNP	JPr,   c               	    R # rC   r'   r  s   &&$r)   r  r    s	     MPr,   c                    V ^8  d   QhRRRR/# )r$   r(   zLiteral['mistralai']r&   z?tuple[list[dict], _provider_format.mistralai.MistralFormatData]r'   )r(   s   "r)   r*   r    s      N N*N	HNr,   c                	    R # rC   r'   )r8   r(   s   &&r)   r  r    s	     KNr,   c               $    V ^8  d   QhRRRRRR/# )r$   r(   r   r@   r   r&   tuple[list[dict], Any]r'   )r(   s   "r)   r*   r    s    [[[[@V[r,   c                	    R # rC   r'   )r8   r(   r@   s   &&,r)   r  r    s    X[r,   c               (    V ^8  d   QhRRRRRRRR/# )	r$   r(   zVLiteral['openai', 'openai.responses', 'google', 'aws', 'anthropic', 'mistralai'] | strr  r   r@   r   r&   r  r'   )r(   s   "r)   r*   r    s:     G GG
 $(G G 
 Gr,   c                  W#R&   VR8X  d"   \         P                  P                  ! V 3/ VB # VR8X  d"   \         P                  P                  ! V 3/ VB # VR8X  d"   \         P                  P                  ! V 3/ VB # VR8X  d"   \         P
                  P                  ! V 3/ VB # VR8X  d"   \         P                  P                  ! V 3/ VB # VR8X  d    \         P                  P                  V 4      # \        RV 24      h)	a:  Convert the chat context to a provider-specific format.

If ``inject_dummy_user_message`` is ``True``, a dummy user message will be added
to the beginning or end of the chat context depending on the provider.

This is necessary because some providers expect a user message to be present for
generating a response.
r  openaizopenai.responsesgoogleaws	anthropic	mistralaizUnsupported provider format: )
r   r  to_chat_ctxto_responses_chat_ctxr  r  r  r  to_conversations_ctxr   )r8   r(   r  r@   s   &&$,r)   r  r    s      /H*+X#**66tFvFF))#**@@PPPx#**66tFvFFu_#''33DCFCC{"#--99$I&II{"#--BB4HH<VHEFFr,   c                    V ^8  d   QhRRRR/# )r$   r   r   r&   rk  r'   )r(   s   "r)   r*   r    s      % C r,   c                   \        \        \        V P                  4      4      4       F0  pV P                  V,          P                  V8:  g   K'  V^,           u # 	  ^ # )z
Returns the index to insert an item by creation time.

Iterates in reverse, assuming items are sorted by `created_at`.
Finds the position after the last item with `created_at <=` the given timestamp.
)reversedrangerp  r  r   )r8   r   rI  s   &$ r)   r2   ChatContext.find_insertion_index  sE     %DKK 012A{{1~((J61u 3 r,   allow_type_mismatchc               $    V ^8  d   QhRRRRRR/# )r$   r,  ChatItemr  r   r&   r(  r'   )r(   s   "r)   r*   r    s"     % % %4 %TX %r,   c               n   V P                  VP                  4      pVe{   V'       gc   VP                  V P                  V,          P                  8w  d7   \	        RVP                   RV P                  V,          P                   24      hWP                  V&   R# V P                  P                  V4       R# )zBUpdate an item with the same ID if it exists, otherwise append it.NzItem type mismatch: z != )rL  r   rg   r  r   r4  )r8   r,  r  r5  s   &&$ r)   _upsert_itemChatContext._upsert_item  s    tww'?&499C8H8M8M+M #7		{$t{{SVGWG\G\F]!^__#KKKKt$r,   keep_last_turnsc               $    V ^8  d   QhRRRRRR/# )r$   llm_vr   r  rk  r&   r  r'   )r(   s   "r)   r*   r    s-     } }} 	}
 
}r,   c               	  "   V^,          p\        V P                  4      pV^ 8  d   ^ p\        \        V P                  4      ^,
          RR4       FT  pV P                  V,          p\        V\        4      '       g   K.  VP
                  R9   g   KA  V^,          pWS8  g   KR  Tp M	  V # V^ 8X  d   V # V P                  RV V P                  VR r. p
V F  p\        V\        4      '       dv   VP
                  R9  d   K,  VP                  P                  R4      RJ d   KM  VP                  ;'       g    RP                  4       pV'       d   V
P                  V4       K  K  \        V\        \        34      '       g   K  V
P                  V4       K  	  V
'       g   V # . pV
 F  p\        V\        \        34      '       d1   TP                  \        V4      P                  ;'       g    R4       KO  TP                  \        VP
                  VP                  ;'       g    RP                  4       4      4       K  	  RP                  V4      P                  4       pV'       g   V # \!        4       pVP#                  R\$        P&                  ! R	4      R
7       VP#                  RRV 2R
7       . pVP)                  VR7      ;_uu_4       GRj  xL
 pV  Rj  xL
  pVP*                  '       g   K  VP*                  P,                  '       g   K<  VP                  VP*                  P,                  4       Kc   Li L`DaRRR4      GRj  xL 
  M  + GRj  xL 
 '       g   i     M; iRP                  T4      P                  4       pT'       g   T # . pT F[  p\        T\        4      '       d   TP
                  R9   d   K,  \        T\        \        34      '       d   KJ  TP                  T4       K]  	  TT n        T	'       d   T	^ ,          P0                  R,
          MTR,          P0                  R,           pT P#                  R\        RT4      TRR/R7       T P.                  P3                  T	4       T # 5i)r$   user	assistantN
is_summaryTr   r   rb  u3                  Compress older conversation history into a short, faithful summary.

                The conversation is formatted as XML. Here is how to read it:
                - <user>…</user>  — something the user said.
                - <assistant>…</assistant>  — something the assistant said.
                - <function_call name="…" call_id="…">…</function_call>  — the assistant invoked an action.
                - <function_call_output name="…" call_id="…">…</function_call_output>  — the result of that                 action. May contain <error>…</error> if it failed.

                Guidelines:
                - Distill the *information learned* from function call outputs into the summary.                 Do not mention that a tool/function was called — just preserve the knowledge gained.
                - Focus on: user goals, constraints, decisions, key facts, preferences, entities,                 and any pending or unresolved tasks.
                - Omit greetings, filler, and chit-chat.
                - Be concise.)r   r   zConversation to summarize:

)chat_ctxgư>chat_history_summaryr   r   r   r   )r  r  )rp  rU   r  rE   r   r   r   r   r   stripr4  r   r   _function_call_item_to_messageto_xmlr   r  r6  textwrapdedentchatdeltar   r  r   extend)r8   r  r  
msg_budget	split_idx	msg_countrI  r,  
head_items
tail_itemsto_summarizer!   contentsmsource_textr  chunksstreamchunksummary	preserveditcreated_at_hints   &&$                    r)   
_summarizeChatContext._summarize  s     %q(


O	>I3tzz?Q.B7zz!}dK00TYYBW5WNI .$%	 8 >K!%JY!7IJ9OJ OQD$,,99$99::>>,/47))//R668 ''- D<1C"DEE##D)  K !A!l,>?@@ >q A N N T TRTUqvv0D0D"/K/K/M NO	  ii)//1K=OO %! " 	 	
( 	4[MB 	 	

 ::x:000F% 7 7e;;;5;;#6#6#6MM%++"5"56 17v 100000
 ''&/'')K %'	B"k**rww:O/O"|-?@AAR     2<Z]%%,*R.B[B[^bBb 	 	17;&&	 	 	
 	:&s   A8Q8?Q8Q8#BQ85Q8.Q8Q8AQ8*6Q8!AQ8%A)Q8L8Q8ML<L:
L<M3M'M8Q8:L<<M=Q8M	Q8M(	M
M(	!M(	#.Q8A3Q8A2Q8c                    V ^8  d   QhRRRR/# )r$   datar   r&   r  r'   )r(   s   "r)   r*   r  b  s      ^  r,   c                	v    \        \        \        ,          4      pVP                  VR ,          4      pV ! V4      # )rU   )r   r  r  r   )r0   r  item_adapterrU   s   &&  r)   	from_dictChatContext.from_dicta  s.    "4>2,,T'];5zr,   c                   V ^8  d   QhRR/# r$   r&   r   r'   )r(   s   "r)   r*   r  h  s      $ r,   c                	    R # )Fr'   r7   s   &r)   readonlyChatContext.readonlyg  s    r,   c                    V ^8  d   QhRRRR/# )r$   rd   r  r&   r   r'   )r(   s   "r)   r*   r  k  s     ' '; '4 'r,   c                N   WJ d   R# \        V P                  4      \        VP                  4      8w  d   R# \        V P                  VP                  RR7       EF  w  r#VP                  VP                  8w  g   VP                  VP                  8w  d    R# VP                  R8X  dh   VP                  R8X  dW   VP
                  VP
                  8w  g7   VP                  VP                  8w  g   VP                  VP                  8w  d    R# K  VP                  R8X  di   VP                  R8X  dX   VP                  VP                  8w  g7   VP                  VP                  8w  g   VP                  VP                  8w  d    R# EK0  VP                  R8X  g   EKD  VP                  R8X  g   EKX  VP                  VP                  8w  gU   VP                  VP                  8w  g:   VP                  VP                  8w  g   VP                  VP                  8w  g   EK   R# 	  R# )a  
Return True if `other` has the same sequence of items with matching
essential fields (IDs, types, and payload) as this context.

Comparison rules:
  - Messages: compares the full `content` list, `role` and `interrupted`.
  - Function calls: compares `name`, `call_id`, and `arguments`.
  - Function call outputs: compares `name`, `call_id`, `output`, and `is_error`.

Does not consider timestamps or other metadata.
TF)strictr   r   r   )rp  rU   zipr   rg   r   r   r   r   r   r   r  r  )r8   rd   abs   &&  r)   is_equivalentChatContext.is_equivalentk  sr    =tzz?c%++..

EKK>>DAttqtt|qvv/vv"qvv':66QVV#q}}'EVWV_V_I_  J` ?*qvv/H66QVV#qyyAII'=PQP[P[A[  B\ 11aff@V6VFFaff$yyAII-xx188+zzQZZ/ ' ?* r,   r%  )r   r   r   r   r   r  r   r  r   rU   setterr-  r6  r3  rD  rL  rg  rs  rz  r  r   r  r2  r  r  r  r  r  r   r'   r,   r)   r  r    s^   ;D G     \\ O!
 (! )2! )2! .7! -6!F+OZ=" ',=" &+	="
 ',=" !&=" ',=" =F="~!F! ',	!
 &+! ',!F.
 #.
 #	.

 #'.
 ',.
 !&.
 ',.
` / +/	/ / J +/	J
 7;J J HKOH H PQUP P N N [ [G
 +/GB%% %}  !	}~  
  ' 'r,   r  c                  d    ] tR tRtRtRt ! R R]],          4      tR R lt	]
R R	 l4       tR
tR# )_ReadOnlyChatContexti  z@A read-only wrapper for ChatContext that prevents modifications.zttrying to modify a read-only chat context, please use .copy() and agent.update_chat_ctx() to modify the chat contextc                  Z    ] tR tRtR R lt];t;t;t;t;t	;t
t];t;t;ttR R ltRtR# )#_ReadOnlyChatContext._ImmutableListi  c               $    V ^8  d   QhRRRRRR/# )r$   r>   r   r@   r&   r(  r'   )r(   s   "r)   r*   0_ReadOnlyChatContext._ImmutableList.__annotate__  s!     	? 	?c 	?S 	?T 	?r,   c                	|    \         P                  ! \        P                  4       \	        \        P                  4      hrC   )r   errorr  	error_msgRuntimeError)r8   r>   r@   s   &*,r)   _raise_error0_ReadOnlyChatContext._ImmutableList._raise_error  s'    LL-7783==>>r,   c                   V ^8  d   QhRR/# r"  r'   )r(   s   "r)   r*   r    s     	 	. 	r,   c                	    \        V 4      # rC   )r  r7   s   &r)   rg  (_ReadOnlyChatContext._ImmutableList.copy  s    :r,   r'   N)r   r   r   r   r  r4  r  rq  removeclearsortreverse__setitem____delitem____iadd____imul__rg  r   r'   r,   r)   _ImmutableListr    sL    	?
 CONNN#NNN:FFFkFHx	 	r,   r  c                   V ^8  d   QhRR/# )r$   rU   r#  r'   )r(   s   "r)   r*   !_ReadOnlyChatContext.__annotate__  s     1 1n 1r,   c                	2    V P                  V4      V n        R # rC   )r  r  r  s   &&r)   r  _ReadOnlyChatContext.__init__  s    ))%0r,   c                   V ^8  d   QhRR/# r  r'   )r(   s   "r)   r*   r    s      $ r,   c                	    R # )Tr'   r7   s   &r)   r  _ReadOnlyChatContext.readonly  s    r,   r%  N)r   r   r   r   r   r  r  r  r  r  r   r  r   r'   r,   r)   r  r    s9    J	T 

h 
1  r,   r  c                    V ^8  d   QhRRRR/# )r$   attrsdict[str, Any] | Noner&   r   r'   )r(   s   "r)   r*   r*     s      . * r,   c           
         V '       d:   R P                  V P                  4        UUu. uF  w  rV RV R2NK  	  upp4      # R# u uppi ) z=""N)r   rU   )r  rY   rM   s   &  r)   _to_attrs_strr    s@    xx%++-@-$!A3b1-@AA As   A
c               (    V ^8  d   QhRRRRRRRR/# )r$   tag_namer   r   r   r  r  r&   r'   )r(   s   "r)   r*   r*     s6     Q QQQ !Q 		Qr,   c                    \        V4      pV'       d.   R P                  V'       d
   RV  RV R2MRV  R2VRV  R2.4      # V'       d
   RV  RV R2# RV  R2# )r   <r  >z</z />)r  r   )r  r   r  	attrs_strs   &&& r)   r  r    s{    
 e$Iyy/8!H:Qyk+(1oXJa 
 	
 1:8*Ai[,P8*C?PPr,   c                    V ^8  d   QhRRRR/# )r$   r,  z!FunctionCall | FunctionCallOutputr&   r   r'   )r(   s   "r)   r*   r*     s      
  
)J  
{  
r,   c                   \        V \        4      '       dK   \        R \        RV P                  RV P
                  RV P                  /R7      .V P                  RR/R7      # \        V \        4      '       ds   \        R\        R	V P                  '       g   V P                  M\        R
V P                  4      RV P                  RV P
                  /R7      .V P                  RR/R7      # R# )r  r   r   r   )r  is_function_callTr  r  r   r  is_function_call_outputN)rE   r   r   r  r   r   r   r   r   r  r  )r,  s   &r)   r  r    s    $%%#NN		!4<<	 %t,
 	
 
D,	-	-*'+}}}DKK&$++:V!4<<			 ,d3
 	
 
.r,   )rc  rb  r  r  rC   )NN):__conditional_annotations__
__future__r   r  r   collections.abcr   r   typingr   r   r   r	   r
   r   pydanticr   r   r   r   typing_extensionsr   livekitr   r   r   logr   typesr   r   
utils.miscr   r   llmr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  r  )r  s   @r)   <module>r'     sN   # "   / N N ? ? '    ) ! ((M
3 M
`<?9 <?~"9 " HI) I
iu 
4"IU 4"n%) %2 &4|CcIY IT9 T 9 999 9B	 B ,!33lBEVV	!E EP; 8Q& 
r,   