
    ^h                        S SK r S SKrS SKJrJr  \R
                  " 5          \R                  " S5        S SKrSSS5         SS\S\S\	S\
S\4
S	 jjrS
 rS rS rS r SS\\\\4      S\S\
S\4S jjrg! , (       d  f       NE= f)    N)DictListignoretextmodel
max_tokenstop_proportionreturnc                    US :X  a  U $ [         R                  " X5      n[        U5      U::  a  U $ US-
  n[        X5-  5      n[        U5      XV-
  -
  n[         R                  " XSU 5      S-   [         R                  " XUS  5      -   $ )N   r   z [...] )litellmencodelenintdecode)r   r   r   r	   tokens	total_lentop_len	bot_starts           K/home/james-whalen/.local/lib/python3.13/site-packages/chatdbg/util/trim.pysandwich_tokensr   
   s     T^^E(F
6{j N	n01K9#67	NN57"34nnU9:$678	
    c                 *    [         R                  " XS9$ )Nmessages)r   token_counterr   r   s     r   _sum_messagesr      s      ::r   c                 .   ^ [        U4S jU  5       5      $ )Nc              3   R   >#    U  H  u  pU(       d  M  [        UT5      v   M     g 7f)N)r   ).0r   keptr   s      r   	<genexpr>#_sum_kept_chunks.<locals>.<genexpr>"   s$     Uf2B8PT-}Xu--fs   '')sum)chunksr   s    `r   _sum_kept_chunksr'   !   s    UfUUUr   c                     / n/ nU  H:  nUR                  SS5      U;   a  [        US   USS5      nXeS'   X5/-  nM5  XE/-  nM<     X44$ )Ntool_call_idcontenti   g      ?)getr   )r   r   tool_call_idstoolsothermr+   s          r   _extractr1   %   s`    EE55$5%a	lE3DG"iLSLESLE  <r   c                    U (       d  / $ U S   nSU;  a*  [        US   USS5      US'   U/S4/[        U SS  U5      -   $ US    Vs/ s H  o3S   PM	     nn[        U SS  X5      u  pVU/U-   S4/[        Xa5      -   $ s  snf )Nr   
tool_callsr+      F   id)r   	_chunkifyr1   )r   r   r0   	tool_callidsr.   r/   s          r   r7   r7   3   s    	A1&q|UD!D)e~	(12, >>>01,@9@e9ue$%	%(??? As   A<r   
trim_ratioc                    [         R                  " U 5      n [        R                  U   S   n[	        X2-  5      n[        R
                  " XS9U:  a  U $ [        XS9nU VVs/ s H  u  pgXg=(       d    US   S   S:H  4PM     nnn[        [        U5      5       H  nXX   u  p	U S   S   S:X  d  M  U S4XX'   M     [        [        U5      5      S	S	S
2    H5  nXX   u  p	U	(       a  M  [        XQ5      [        X5      -   U:  a  U S4XX'   M5    O   [        XQ5      U:  d   S[        XQ5       SU S35       eU V V	Vs/ s H  u  p	U	(       d  M  U   H  ofPM     M     snn	n $ s  snnf s  snn	n f )a  
Strategy:
- chunk messages:
    - single message, or
    - tool request and all the tool responses
- keep the system messages
- keep the first user message
- go most recent to oldest, keeping chunks until we are at the limit

Also, shorten tool call results along the way.
-max_input_tokensr   r   r   rolesystemuserTNr*   zNew conversation too big z vs !)copydeepcopyr   
model_costr   r   r7   ranger   r'   r   )
r   r   r:   max_tokens_for_modelr   r&   r0   bir"   s
             r   trim_messagesrH   @   s   " }}X&H"--e45GH)67JU6C6F @FFvVaq/qtF|x/0vFF 3v;A;v&(!4(FI   3v;"%V+mH.LL "4(FI  &$ 	'*4V	"#3F#B"C4
|STUV4 '-Gf"8AhAhAfGGA G@ Hs   "!E:E%E%)r4   g      ?)g      ?)rA   warningstypingr   r   catch_warningssimplefilterr   strr   floatr   r   r'   r1   r7   listrH    r   r   <module>rQ      s      (#  LO



'*
CH

&;V
@  =H4S>"=H=H =H 
	=Hw s   A88
B