
    	D,je                     H   d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	 d dl
mZ d dlmZ d dlmZ d dlmZ d dlmZmZmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dl m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z* d dl+m,Z, d dl-m.Z.m/Z/ d dl0m1Z1 d dl2m3Z3 d dl4m5Z5 d dl6m7Z7 d dl8m9Z9 d dl:m,Z; d dl<m=Z= d dl>m?Z? ddl@m@Z@ ddlAmBZB ddlCmDZD dZEd  ZFd! ZG G d" d#eH          ZIe G d$ d%                      ZJ G d& d'e          ZK G d( d)          ZLd* ZMdS )+    N)defaultdict)	dataclass)datetime)StringIOPath)	Completer
CompletionThreadedCompleter)ModalCursorShapeConfig)EditingMode)	Conditionis_searching)FileHistory)KeyBindings)	InputMode)Keys)PygmentsLexer)is_dumb_terminal)CompleteStylePromptSession)Style)MarkdownLexerguess_lexer_for_filename)Token)ColorParseError)Columns)Console)MarkdownText)MarkdownStream   )dump)pipe_editor)is_image_filezAider is waiting for your inputc                     | s| S t          | t                    rX|                                 rD|                     d          s/t	          d | D                       rt          |           dv rd|  S | S )z(Ensure hex color values have a # prefix.#c              3      K   | ]}|d v V  	dS )0123456789ABCDEFabcdefN .0cs     W/home/rurouni/.local/share/uv/tools/aider-chat/lib/python3.11/site-packages/aider/io.py	<genexpr>z%ensure_hash_prefix.<locals>.<genexpr>4   s(      <<q,,<<<<<<    )      )
isinstancestrstrip
startswithalllencolors    r/   ensure_hash_prefixr<   .   s     % %++-- 8H8H8M8M <<e<<<<< 	UvAUAUu;;Lr1   c                 F     t          j                    fd            }|S )z<Decorator to restore multiline mode after function executionc                 ~    | j         }d| _         	  | g|R i ||| _         S # t          $ r  w xY w# || _         w xY w)NF)multiline_mode	Exception)selfargskwargsorig_multilinefuncs       r/   wrapperz"restore_multiline.<locals>.wrapper<   sy    ,#	14.t...v.. #1D  	 	 		 #1D0000s   $ 03 	<)	functoolswraps)rE   rF   s   ` r/   restore_multilinerI   9   s:     _T1 1 1 1 1 Nr1   c                       e Zd ZdZdS )CommandCompletionExceptionzeRaised when a command should use the normal autocompleter instead of
    command-specific completion.N)__name__
__module____qualname____doc__r+   r1   r/   rK   rK   J   s        $ $ 	Dr1   rK   c                   4    e Zd ZU dZeed<   dZeed<   ddZdS )ConfirmGroupN
preferenceT
show_groupc                 >    |t          |          dk    | _        d S d S Nr#   )r9   rS   )rA   itemss     r/   __init__zConfirmGroup.__init__V   s%    !%jj1nDOOO r1   N)	rL   rM   rN   rR   r5   __annotations__rS   boolrW   r+   r1   r/   rQ   rQ   Q   sJ         JJ- - - - - -r1   rQ   c                   *    e Zd Z	 ddZd Zd Zd ZdS )AutoCompleterNc                    || _         || _        || _        |pg | _        t	          t
                    }|D ]B}t          j                            |          }	|	|k    r||	         	                    |           C|| _
        t                      | _        || _        t                      | _        |r| j                                        | _        |D ]}| j                            |           |D ]}| j                            |           fd|D             }
|r|
                    |           |
| _        d| _        d S )Nc                 4    g | ]}t                    |z  S r+   r   )r-   	rel_fnameroots     r/   
<listcomp>z*AutoCompleter.__init__.<locals>.<listcomp>x   s$    IIId4jj9,IIIr1   F)addable_rel_fnames
rel_fnamesencodingabs_read_only_fnamesr   listospathbasenameappendfname_to_rel_fnamessetwordscommandsdictcommand_completionsget_commandscommand_namesaddextend
all_fnames	tokenized)rA   r`   rc   rb   rn   rd   re   rk   r_   fnameru   s    `         r/   rW   zAutoCompleter.__init__\   sW    #5$ $8$>B!)$//+ 	= 	=IG$$Y//E	!!#E*11)<<<#6 UU
 #'66  	>!%!;!;!=!=D+ 	& 	&IJNN9%%%%# 	& 	&IJNN9%%%%IIIIjIII
 	42333$r1   c                    | j         rd S d| _         | j        D ]}	 t          |d| j                  5 }|                                }d d d            n# 1 swxY w Y   n# t
          t          t          f$ r Y `w xY w	 t          ||          }n# t          $ r Y w xY wt          |                    |                    }| j                            d |D                        d S )NTrrd   c              3   f   K   | ],}|d          t           j        v |d         d|d          dfV  -dS )r   r#   `N)r   Name)r-   tokens     r/   r0   z)AutoCompleter.tokenize.<locals>.<genexpr>   sP        0558uzCYCYq?uQx???+CYCYCYCY r1   )rv   ru   openrd   readFileNotFoundErrorUnicodeDecodeErrorIsADirectoryErrorr   r@   rf   
get_tokensrm   update)rA   rw   fcontentlexertokenss         r/   tokenizezAutoCompleter.tokenize   sf   > 	F_ 	 	E%t}=== 'ffhhG' ' ' ' ' ' ' ' ' ' ' ' ' ' '%'9;LM   0@@    %**73344FJ  9?      	 	sF   AAAA	AA	AA98A9=B
BBc              #     K   t          |          dk    r|d                                         sh|d                                         fd| j        D             }t	          |          D ])}t          |t          |d                              V  *d S t          |          dk    s|d                                         rd S |d         }|d                                         | j                            |          \  }}	}	t          |          dk    r	|d         }n||vrd S | j                            |          }
|
r |
||          E d {V  d S || j	        vr%| j        
                    |          }|| j	        |<   n| j	        |         }|d S fd|D             }t	          |          D ])}t          |t          |d                              V  *d S )Nr#   r   c                 >    g | ]}|                               |S r+   r7   )r-   cmdpartials     r/   ra   z9AutoCompleter.get_command_completions.<locals>.<listcomp>   s*    WWW#s~~g?V?VW#WWWr1   )start_positionc                 @    g | ]}|                                 v |S r+   lower)r-   wordr   s     r/   ra   z9AutoCompleter.get_command_completions.<locals>.<listcomp>   s+    MMMtW

5L5Ld5L5L5Lr1   )r9   isspacer   rr   sortedr
   rn   matching_commandsget_raw_completionsrp   get_completions)rA   documentcomplete_eventtextrm   
candidates	candidater   matches_raw_completerr   s              @r/   get_command_completionsz%AutoCompleter.get_command_completions   s!     u::??48#3#3#5#5?Ahnn&&GWWWW);WWWJ#J// L L	 Cb	NN?KKKKKKKFu::??d2h..00?FAh)//##77<<Aw<<1!*CCF99#>> 	$}X~>>>>>>>>>Fd...66s;;J,6D$S))1#6JFMMMMzMMM

++ 	H 	HIYE"IGGGGGGG	H 	Hr1   c              #     K   |                                   |j        }|                                }|sd S |r|d                                         rd S |d         dk    r1	 |                     ||||          E d {V  d S # t
          $ r Y nw xY w| j        }|                    t          | j	                             d |D             }|d         }t          |          dk     rd S g }|D ]\  }}	|                                                    |                                          rn|                    |	t          |           |f           | j	                            |g           }
|
r+|
D ](}|                    |t          |           |f           )t          |          D ]\  }}}t!          |||          V  d S )Nr   r   /c                 F    g | ]}t          |          t          u r|n||fS r+   )typetuple)r-   r   s     r/   ra   z1AutoCompleter.get_completions.<locals>.<listcomp>   s0    [[[d4jjE11ddd|[[[r1   r2   )r   display)r   text_before_cursorsplitr   r   rK   rm   r   rl   rk   r9   r   r7   rj   getr   r
   )rA   r   r   r   rm   r   	last_wordcompletions
word_matchword_insertrc   r_   insposmatchs                  r/   r   zAutoCompleter.get_completions   s(     *

 	F 	DH$$&& 	F7c>>77.RVX]^^^^^^^^^-    Z
#d677888[[PZ[[[
"I	 y>>AF'1 	T 	T#J!!,,Y__->->?? T""K#i..*#MNNN!599*bII
 T%/ T T	#**II	+RSSSS%k22 	E 	EOCeSeDDDDDDD	E 	Es   !B 
BBrX   )rL   rM   rN   rW   r   r   r   r+   r1   r/   r\   r\   [   sb        ]a! ! ! !F  *$H $H $HL)E )E )E )E )Er1   r\   c                   t   e Zd ZdZdZdZdZdZddddddddddddddddd	d
ddej	        ddddddfdZ
d Zd Zd Zd2dZd3dZd Zd Z	 	 d4dZd Zd Zd Zd Zd5dZd Zd6dZe	 	 	 	 	 d7d             Zed8d"            Zd9d#Zd:d$Zd:d%Zddd&d'Z d( Z!d;d)Z"d* Z#d<d+Z$d, Z%d- Z&d. Z'd/ Z(d=d0Z)d1 Z*dS )>InputOutputr   NFTblueredz#FFA500defaultutf-8platform.c                 <   d | _         d| _        t                      | _        || _        || _        d| _        || _        |r||                                 | _	        n|| _	        t          j                            d          }||dk    rd}|rt          |          nd | _        |rt          |          nd | _        |rt          |	          nd | _        |rt          |
          nd | _        t          |          | _        |rt          |          nd | _        |rt          |          nd | _        |rt          |          nd | _        |rt          |          nd | _        || _        || _        || _        || _        | j        rd| _        || _        || _        | j        rm	 t;          | j                  j                            dd           n=# t@          tB          f$ r)}| "                    d|            d | _        Y d }~nd }~ww xY w|| _#        |t;          |          | _$        nd | _$        || _%        h d}||vr(tM          d| d	d
'                    |                     |dk    rd n	|dk    rdnd| _(        || _)        tU          j+                    ,                    d          }| -                    d| d           d | _.        t_                      | _/        | j/        r	d| _        d}|r| j        | j        ta          tb                    | j        d} | j        td          j3        k    rti                      | d<   | j        tk          | j                  | d<   	 tm          di | | _.        to                      | _8        nw# tr          $ r8}!to          dd          | _8        | :                    d|!            Y d }!~!n:d }!~!ww xY wto          dd          | _8        | j/        r| ;                    d           || _<        || _=        | >                                 d S )NFNO_COLOR Tparentsexist_okz.Could not create directory for input history: >   lfcrlfr   zInvalid line_endings value: z. Must be one of: , r   r   

z%Y-%m-%d %H:%M:%Sz
# aider chat started at 

)inputoutputr   editing_modecursorhistory)force_terminalno_colorz!Can't initialize prompt toolkit: z@Detected dumb terminal, disabling fancy input and pretty output.r+   )?placeholderinterruptedrl   never_promptseditingmoder?   bell_on_next_inputnotifications get_default_notification_commandnotifications_commandrg   environr   r<   user_input_colortool_output_colortool_error_colortool_warning_colorassistant_output_colorcompletion_menu_colorcompletion_menu_bg_colorcompletion_menu_current_color completion_menu_current_bg_color
code_themer   r   prettyyesinput_history_filer   parentmkdirPermissionErrorOSErrortool_warningllm_history_filechat_history_filerd   
ValueErrorjoinnewlinedry_runr   nowstrftimeappend_chat_historyprompt_sessionr   r   r   r   VIr   r   r   r   consoler@   
tool_errortool_outputfile_watcherr`   _validate_color_settings)"rA   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rd   line_endingsr   r   r   fancy_inputr   r?   r`   r   r   r   evalid_line_endingscurrent_timesession_kwargserrs"                                     r/   rW   zInputOutput.__init__   s   <    UU&,"'* 	?2:)-)N)N)P)PD&&)>D&:>>*--HNNFHN X 23C D D DTXJP!Z!34E!F!F!FVZHN X 23C D D DTXLR"\"45G"H"H"HX\&89O&P&P#RX%b%78M%N%N%N^b"<BL7888 	% BHQ<===T 	* EKT?@@@PT 	- %
; 	 DK"4" 	//T,--4::4RV:WWWW#W- / / /!!"VST"V"VWWW*.''''''/ !1(%)*;%<%<D""%)D" 777111C| C C#'99-?#@#@C C  
 !J..DDLD<P<PDDV\ 	 |~~../BCC  !Pl!P!P!PQQQ" 0 2 2  	 DKK 	e +&}55 $ 0	 N ;>11+A+C+Cx(&2,78O,P,Py)K&3&E&En&E&E#&yy K K K&edKKK IC I IJJJJJJJJK #%$GGGDL$ e  !cddd(	 	%%'''''s0    .G H	 HH	$N 
O.O  Oc                     g d}|D ]s}t          | |d          }|r^	 t          |           '# t          $ r?}| j                            d| d| d| d           t          | |d           Y d}~kd}~ww xY wtdS )z9Validate configured color strings and reset invalid ones.)	r   r   r   r   r   r   r   r   r   Nr:   z8[bold red]Warning:[/bold red] Invalid configuration for z: 'z'. z. Disabling this color.)getattr	RichStyler   r   printsetattr)rA   color_attributes	attr_namecolor_valuer   s        r/   r   z$InputOutput._validate_color_settingsv  s    

 

 

 * 	3 	3I!$	488K 	33K00000& 3 3 3L&&U%U U*5U U:;U U U   D)T222222223		3	3 	3s   .
A75A22A7c                 j   i }| j         st          j        |          S | j        r:|                    d| j                   |                    dd| j         i           g }| j        r|                    d| j                    | j        r|                    | j                   |rd	                    |          |d<   g }| j
        r|                    | j
                   | j        r|                    d| j                    |rd	                    |          |d<   t          j        |          S )Nr   zpygments.literal.stringzbold italic zbg: zcompletion-menuz"completion-menu.completion.current)r   r   	from_dictr   
setdefaultr   r   rj   r   r   r   r   )rA   
style_dictcompletion_menu_stylecompletion_menu_current_styles       r/   
_get_stylezInputOutput._get_style  sz   
{ 	/?:...  	!!"d&;<<<-/Ud>S/U/U   !#( 	P!(()Nt/L)N)NOOO% 	E!(()CDDD  	L,/HH5J,K,KJ() )+%0 	X)001VWWW- 	])001[t7Y1[1[\\\( 	?Bxx-@ @J;< z***r1   c                 6   	 t          t          |          d          5 }t          j        |                                          }|                    d          cd d d            S # 1 swxY w Y   d S # t          $ r%}|                     | d|            Y d }~d S d }~wt          $ r |                     | d           Y d S t          $ r |                     | d           Y d S t          $ r%}|                     | d|            Y d }~d S d }~ww xY w)Nrbr   : unable to read: : file not found error: is a directory: )r   r5   base64	b64encoder   decoder   r   r   r   r@   )rA   filename
image_fileencoded_stringr   r   s         r/   
read_imagezInputOutput.read_image  s   	c(mmT** 6j!'!1*//2C2C!D!D%,,W556 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6  	 	 	OOx@@3@@AAAFFFFF  	 	 	OOx???@@@FF  	 	 	OOx999:::FF 	 	 	OOx..1..///FFFFF	sR   A4 ;A'A4 'A++A4 .A+/A4 4
D>B%D!D*	D3DDc                 x   t          |          r|                     |          S 	 t          t          |          d| j                  5 }|                                cd d d            S # 1 swxY w Y   d S # t          $ r |s|                     | d           Y d S t          $ r |s|                     | d           Y d S t          $ r'}|s|                     | d|            Y d }~d S d }~wt          $ r<}|s/|                     | d|            |                     d           Y d }~d S d }~ww xY w)Nry   rz   r  r  r  r  z+Use --encoding to set the unicode encoding.)r&   r  r   r5   rd   r   r   r   r   r   UnicodeError)rA   r  silentr   r   r   s         r/   	read_textzInputOutput.read_text  s   "" 	-??8,,,	c(mmS4=AAA  Qvvxx                                     	 	 	 E8 C C CDDDFF  	 	 	 ?8 = = =>>>FF 	 	 	 F8 D Ds D DEEEFFFFF 	 	 	 O8 2 2q 2 2333 MNNNFFFFF		sS   $A8 
A+A8 +A//A8 2A/3A8 8$D9#D9	D9C00D9=1D44D9   皙?c           
         | j         rdS |}t          |          D ]}	 t          t          |          d| j        | j                  5 }|                    |           ddd           n# 1 swxY w Y    dS # t          $ rL}||dz
  k     rt          j	        |           |dz  }n| 
                    d| d| d|             Y d}~d}~wt          $ r!}| 
                    d| d	|             d}~ww xY wdS )
au  
        Writes content to a file, retrying with progressive backoff if the file is locked.

        :param filename: Path to the file to write.
        :param content: Content to write to the file.
        :param max_retries: Maximum number of retries if a file lock is encountered.
        :param initial_delay: Initial delay (in seconds) before the first retry.
        Nw)rd   r   r#      zUnable to write file z after z attempts: r  )r   ranger   r5   rd   r   writer   timesleepr   r   )	rA   r  r   max_retriesinitial_delaydelayattemptr   r   s	            r/   
write_textzInputOutput.write_text  s    < 	F[)) 	 	G#h--t}dl[[[ %_`GGG$$$% % % % % % % % % % % % % % %"   [1_,,Ju%%%QJEEOO^^^^^Y\^^    EEEE     I I IC I IJJJ	 	sH   *A8A*A8*A.	.A81A.	2A88
C7AC		C7C22C7c                     | j         r>| j        rt          | j                  nt                      } | j        j        di | d S t                       d S )Nstyler+   )r   r   ro   r   ruler  )rA   r2  s     r/   r3  zInputOutput.rule  sa    ; 	9=9NZDt45555TXTZTZEDL&&&&&&&GGGGGr1   c                     | j         rN| j         j        rD| j         j        j        j        | _        d| _        | j         j                                         d S d S d S NT)r   appcurrent_bufferr   r   r   exitrA   s    r/   interrupt_inputzInputOutput.interrupt_input  sh     	+4#6#: 	+#26EJD#D#((*****		+ 	+ 	+ 	+r1   c                                                                                          t          |          }d}|r&fd|pg D             }                     ||          }d}	|r|	|z  }	 j        r|	|rdnddz   z  }	|	dz  }	||	z  }|	 _        d}
d}                                 }t          t          ||| j	        |                    }d t                      }|                    t          j        t          d	           
          fd            }|                    d          d             }|                    d          d             }|                    d          d             }|                    dd          d             }|                    ddt                      fd            }|                    dddt                      fd            }	 |r j        }	  j        r j        pd}d  _        d _        |s@ j        r j                                          j        r j                                          fd} j                            |||dt.          j        ||d|	  	        }nt3          |          } j        rg|pd} j        r\ j                                        }| j        r j                                          j        r j                                         S S n(# t8          $ r  t:          $ r}dd l}                     tA          |                                          |!                                           Y d }~ j        r j                                          j        r j                                         dS dS d }~wtD          $ ro}                     tA          |                     Y d }~ j        r j                                          j        r j                                         dS dS d }~ww xY w j        r j                                          j        r j                                         nF#  j        r j                                          j        r j                                         w w xY w|#                    d          ru|ss|#                    d          }|d k    r
d}d }|
dz  }
nL|d         d k    r=d$                    d! |d"d          D                       }|d |z   k    r
d}|}|
dz  }
n|}
nv|}
nsV|r_|#                                rK|r&|#                    d          | d#k    rn<|
|d$z   z  }
n1|#                    d          d#k    rn|
|d$z   z  }
n|r	|
|d$z   z  }
n|}
ntK                        &                    |
           |
S )%Nr   c                 0    g | ]}t          |          S r+   )get_rel_fname)r-   rw   r`   s     r/   ra   z)InputOutput.get_input.<locals>.<listcomp>  s0     $ $ $/4eT**$ $ $r1   r
  multi> F)re   c                 8    | j                                          dS )z&Suspend currently running application.N)r6  suspend_to_backgroundevents    r/   suspend_to_bgz,InputOutput.get_input.<locals>.suspend_to_bg;  s    I++-----r1   c                  ,    t          t          d          S )NSIGTSTP)hasattrsignalr+   r1   r/   <lambda>z'InputOutput.get_input.<locals>.<lambda>A  s    	8R8R r1   )filterc                      |            dS )z!Suspend to background with ctrl-zNr+   )rC  rD  s    r/   r   z InputOutput.get_input.<locals>._A  s     M%     r1   zc-spacec                 :    | j                             d           dS )z#Ignore Ctrl when pressing space barr
  N)r7  insert_textrB  s    r/   r   z InputOutput.get_input.<locals>._F  s!      ,,S11111r1   zc-upc                 8    | j                                          dS )z!Navigate backward through historyN)r7  history_backwardrB  s    r/   r   z InputOutput.get_input.<locals>._K  s      1133333r1   zc-downc                 8    | j                                          dS )z Navigate forward through historyN)r7  history_forwardrB  s    r/   r   z InputOutput.get_input.<locals>._P  s      0022222r1   zc-xzc-ec                     | j         }|j        }t          |d          }|                    d          |_        t	          |j                  |_        dS )z1Edit current input in external editor (like Bash)md)
input_datasuffixr   N)r7  r   r%   rstripr9   cursor_position)rC  buffercurrent_textedited_texts       r/   r   z InputOutput.get_input.<locals>._U  sX     )F!;L &dKKKK &,,T22FK &)%5%5F"""r1   enterT)eagerrJ  c                     j         rPj        t          j        k    r| j        j        j        t          j        k    s| j	        
                    d           dS | j	                                         dS )zHandle Enter key pressr   N)r?   r   r   r   r6  vi_state
input_moder   
NAVIGATIONr7  rM  validate_and_handlerC  rA   s    r/   r   z InputOutput.get_input.<locals>._d  sr     " 	; KN22I&1Y5III $0066666 $88:::::r1   escapec                     j         r| j                                         dS | j                            d           dS )zHandle Alt+Enter key pressr   N)r?   r7  ra  rM  rb  s    r/   r   z InputOutput.get_input.<locals>._r  sJ     " 7$88::::: $0066666r1   c                     j         S rX   )prompt_prefix)widthline_numberis_soft_wraprA   s      r/   get_continuationz/InputOutput.get_input.<locals>.get_continuation  s    #11r1      )r   	completerreserve_space_for_menucomplete_styler2  key_bindingscomplete_while_typingprompt_continuationr   r   {c              3   B   K   | ]}|                                 |V  d S rX   )isalnumr,   s     r/   r0   z(InputOutput.get_input.<locals>.<genexpr>  s/      !I!IQYY[[!I!!I!I!I!I!I!Ir1   r#   }r   )'r3  	ring_bellrf   format_files_for_inputr?   rf  r  r   r\   rd   r   rs   r   ControlZr   r   r   r   r   r   startclipboard_watcherpromptr   MULTI_COLUMNr   process_changesstopEOFErrorr@   	tracebackr   r5   
format_excUnicodeEncodeErrorr6   r   r  
user_input)rA   r`   rc   rb   rn   re   edit_formatshowrel_read_only_fnamesrf  inpmultiline_inputr2  completer_instancekbr   r   rj  liner   r   r  strippedmultiline_tagtagrD  s   ``                       @r/   	get_inputzInputOutput.get_input  s    			 	*%%
 	Q$ $ $ $9M9SQS$ $ $  ..z;OPPD 	)[(M 	D[8ccbGCCM*!!."%9  	
 	
	. 	. 	. ]]	i0R0R&S&S	T	T	! 	! 	! 	! 
U	T	! 
				2 	2 
		2 
	4 	4 
	4 
			3 	3 
		3 
u			6 	6 
		6 
t\M	:	:	; 	; 	; 	; 
;	:	; 
'|m	D	D	7 	7 	7 	7 
E	D	7^	 *)42& '".4"G'+D$',D$* ;, 6 -335551 ; 288:::2 2 2 2 2  .55 '"4/0'4'A#%'.2,< 6 
 
DD !;;D # #:2D( #"/??AA" $ -%**,,,) 2*//11112           C)))	 4 4 6 6777rrr
 $ -%**,,,) 2*//111112 2 &   C)))rrr$ -%**,,,) 2*//111112 2 $ -%**,,,) 2*//111 $ -%**,,,) 2*//11112 zz&!! $/ $::f--s??&*O$(M2ICCa[C''''!I!IXabb\!I!I!IIIC39,,*.(+r	"C  TZZ\\   
'zz&))-A-A-AAAtd{*ZZ''3..4$;&CC  td{"}^	@ 	
sE   3CL Q. P*AN.&Q. .P*;"P%Q. %P**Q. .AR1c                 4   | j         sd S 	 t          | j                                       |           | j        r-| j        j        r#| j        j                            |           d S d S d S # t
          $ r#}|                     d|            Y d }~d S d }~ww xY w)Nz'Unable to write to input history file: )r   r   append_stringr   r   r   r   )rA   r  r   s      r/   add_to_input_historyz InputOutput.add_to_input_history  s    & 	F	O/00>>sCCC" ?t':'B ?#+99#>>>>>? ? ? ? 	O 	O 	OMMMNNNNNNNNN	Os   AA* *
B4BBc                 d    | j         sg S t          | j                   }|                                S rX   )r   r   load_history_strings)rA   fhs     r/   get_input_historyzInputOutput.get_input_history  s4    & 	I011&&(((r1   c                 :   | j         sd S t          j                                        d          }	 t	          | j                   j                            dd           t          | j         dd          5 }|                    |	                                 d| d	           |                    |d	z              d d d            d S # 1 swxY w Y   d S # t          t          f$ r2}|                     d
| j          d|            d | _         Y d }~d S d }~ww xY w)Nseconds)timespecTr   ar   rz   r
  r   z$Unable to write to llm history file r  )r   r   r   	isoformatr   r   r   r   r(  upperr   r   r   )rA   roler   	timestamplog_filer   s         r/   log_llm_historyzInputOutput.log_llm_history  s}   $ 	FLNN,,i,@@		)&''.44TD4QQQd+S7CCC /x$**,,>>>>>???w~.../ / / / / / / / / / / / / / / / / /  ) 	) 	) 	)cTEZcc^accddd$(D!!!!!!!	)s=   AC 7AC
=C 
CC CC D('DDc                     | j         r| j        rt          | j                  }nt                      } | j        j        t          |          fi | d S )Nr1  )r   r   ro   r   r  r!   )rA   r  r2  s      r/   display_user_inputzInputOutput.display_user_input  s]    ; 	40 	t4555EEFFE499.......r1   c                     |s|                      |           d}|r|                                }ndg}d| d                    |          }d| d| }|                     |d           d S )Nz####z<blank>  
r
  r   T)	linebreak)r  
splitlinesr   r   )rA   r  log_onlyprefixhists        r/   r  zInputOutput.user_input  s     	)##C((( 	>>##DD;Df$$T** 
    66666r1   c                 d    d|                                 z   dz   }|                     |           d S )Nr   r   )r6   r   )rA   r   r  s      r/   	ai_outputzInputOutput.ai_output  s3    gmmoo%.  &&&&&r1   Open URL for more info?c                 x    || j         v rdS |                     |||          rt          j        |           dS dS )z;Offer to open a URL in the browser, returns True if opened.F)subjectallow_neverT)r   confirm_ask
webbrowserr   )rA   urlr{  r  s       r/   	offer_urlzInputOutput.offer_url  sL    $$$$5FC[II 	OC   4ur1   yc                    | xj         dz  c_         |                                  ||f}|| j        v rdS |r	|j        sd }|rd}g dd}|r|s|dz  }|dz  }|r|dz  }                    d	           |                                                    d
          r	||dz   z  }n<|                                                    d          r	||dz   z  }n||d| dz   z  }|r|                                  d|v rh|                                }	t          d |	D                       fd|	D             }
d
                    |
          }|                     |d           n|                     |d           |                                 }fd}| j        du r|rdnd
n| j        du rdn|r*|j        r#|j        |                     |  d           n	 	 | j        r| j                            ||d          nt#          |          n# t$          $ r |Y niw xY ws|n`                                t'          fdD                       }|rn.dd
                               }|                     |                                           d         dk    rO|rM| j                            |           |                                 d }|                     |dd           dS |rd
k    }ndv }dk    o|d uo| }d k    o|d u}|r|r
|sd!|_        n	|rd"|_        |                                 d }|                     |dd           |S )#Nr#   FT)r   noskipr8   z (Y)es/(N)oz/(A)llz/(S)kip allz/(D)on't ask againzdon'tr  z [Yes]: nz [No]: z [z]: r   c              3   4   K   | ]}t          |          V  d S rX   )r9   )r-   r  s     r/   r0   z*InputOutput.confirm_ask.<locals>.<genexpr>T  s(       = =tT = = = = = =r1   c                 :    g | ]}|                               S r+   )ljust)r-   r  
max_lengths     r/   ra   z+InputOutput.confirm_ask.<locals>.<listcomp>U  s%    III4

: 6 6IIIr1   boldc                 8    | sdS |                                  v S r5  r   )r   valid_responsess    r/   is_valid_responsez2InputOutput.confirm_ask.<locals>.is_valid_response]  s#     t::<<?22r1   )r  )r2  rp  c              3   B   K   | ]}|                               V  d S rX   r   )r-   valid_responseress     r/   r0   z*InputOutput.confirm_ask.<locals>.<genexpr>}  s1      ``n>44S99``````r1   zPlease answer with one of: r   r   dr
  r  
blockquote)r  r  r  sr8   r  )num_user_asksrv  r   rS   rj   r   r7   r   r  maxr   r  r   rR   r  r   r{  r   r  anyr   rs   r6   r   )rA   questionr   r  explicit_yes_requiredgroupr  question_idoptionslinespadded_linespadded_subjectr2  r  gooderror_messager  is_yesis_allis_skipr  r  r  s                       @@@r/   r  zInputOutput.confirm_ask&  s    	a 	)$,,,5 	) 	E 	K666 	%( $8#}$G 	,++G""7+++==??%%c** 	4*,,HH]]__'',, 	4)++HH"3w"3"3"333H 		5w**,,  = =u = = ===
IIII5III!%<!8!8  d ;;;;  t 444!!	3 	3 	3 	3 	3
 8t.7##CCCXCC 	/u' 	/"COOx...O????/* ."188$"'27 9   $Hoo   !CE
  !Ciikk````P_`````  Zdii>X>X Z Z...1/4 iikk!n#::+:"";///nn&&....D$$TTd$KKK5  	'CZFFJ&FOT 1O:O6O*2d!2 	* *3 *#(   *#) ..""**S**  $ GGGs   4H H"!H"r   c                 `   | xj         dz  c_         |                                  |r+|                                  |                     |d           |                                 }| j        du rd}n[| j        du rd}nO	 | j        r"| j                            |dz   ||d          }nt          |dz             }n# t          $ r |}Y nw xY w|	                                 d|	                                 }| 
                    |dd	           | j        d
v r|                     |           |S )Nr#   Tr  r   Fr  r
  )r   r2  rp  r  )TF)r  rv  r   r  r   r   r{  r   r  r6   r   )rA   r  r   r  r2  r  r  s          r/   
prompt_askzInputOutput.prompt_ask  sq   a 	 	1W4000!!8tCCXCC& 0-44 3 '#.2	 5  CC  3//C    ..""22SYY[[22  $ GGG8}$$T"""
s   ?;B; ;C
	C
c                    |                                 red|v r1|                                D ]}|                     |dd|           n0|r|                                 n|}|                     |dd           t          |t                    st	          |          }|rt          |          nd }| j        r|rt          |          nt                      }	  | j        j	        |fi | d S # t          $ rj t          |t                    r|j        }t          |                              dd                              d          } | j        j	        |fi | Y d S w xY w)	Nr   T)r  r  r6   r  r1  asciireplaceerrors)r6   r  r   r4   r!   r<   r   ro   r   r  r  plainr5   encoder  )rA   messager6   r;   r  r  r2  s          r/   _tool_messagezInputOutput._tool_message  s   ==?? 	Pw#..00 a aD,,TTdZ_,````a +0<w}}W(($(OOO'4(( 	$7mmG-2<"5)))%)[FUF5!!!!	1DLw00%00000! 	1 	1 	1'4(( (!-'ll))'))DDKKGTTGDLw00%000000	1s   C. .A0E"!E"c                 ^    | xj         dz  c_         |                     ||| j                   d S rU   )num_error_outputsr  r   rA   r  r6   s      r/   r   zInputOutput.tool_error  s7    !#7E4+@AAAAAr1   c                 >    |                      ||| j                   d S rX   )r  r   r  s      r/   r   zInputOutput.tool_warning  s#    7E4+BCCCCCr1   )r  r  c                   |rBd                     |          }|                                 }|                     |dd           |rd S t          t	          t
          |                    }t                      }| j        r#| j        rt          | j                  |d<   ||d<   t          di |} | j        j        |d|i d S )Nr
  Tr  r;   reverser2  r+   )r   r6   r   rf   mapr!   ro   r   r   r<   r  r   r  )rA   r  r  messagesr  r2  s         r/   r   zInputOutput.tool_output  s     	L88H%%Djjll$D$$TTd$KKK 	FD(++,,; 	$% L!3D4J!K!Kg#E)""E""H2E22222r1   c                 ^    t          | j        | j        d          }t          |          }|S )Nr   )r2  r   inline_code_lexer)mdargs)ro   r   r   r"   )rA   r  mdStreams      r/   get_assistant_mdstreamz"InputOutput.get_assistant_mdstream  s;    -$
 
 

 "000r1   c                     |s|                      d           d S |}|| j        }|rt          || j        | j                  }nt          |pd          }| j                            |           d S )Nz>Empty response received from LLM. Check your provider account?)r2  r   z(empty response))r   r   r   r   r   r!   r   r  )rA   r  r   	show_resps       r/   assistant_outputzInputOutput.assistant_output  s     	^___F	 >[F 	< t:t  II W:(:;;I9%%%%%r1   c                     || _         dS )z:Set a one-time placeholder text for the next input prompt.N)r   )rA   r   s     r/   set_placeholderzInputOutput.set_placeholder  s    &r1   c                 $    t          |           d S rX   )r  )rA   r  s     r/   r  zInputOutput.print  s    gr1   c                     d| _         dS )zRMark that the LLM has started processing, so we should ring the bell on next inputTN)r   r9  s    r/   llm_startedzInputOutput.llm_started  s    "&r1   c                 F   ddl }|                                }|dk    r*t          j        d          rdt           dS dt           dS |d	k    rAd
D ]<}t          j        |          r&|dk    rdt           dc S |dk    rdt           dc S =dS |dk    rdt           dS dS )zDReturn a default notification command based on the operating system.r   NDarwinzterminal-notifierz+terminal-notifier -title 'Aider' -message ''z$osascript -e 'display notification "z" with title "Aider"'Linux)notify-sendzenityr  znotify-send 'Aider' 'r  zzenity --notification --text='Windowszpowershell -command "[System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms'); [System.Windows.Forms.MessageBox]::Show('z', 'Aider')")r   systemshutilwhichNOTIFICATION_MESSAGE)rA   r   r   r   s       r/   r   z,InputOutput.get_default_notification_command  s   ""X|/00 ]\EY\\\\ e8Lddd w0 X X<$$ Xm++N7KNNNNNNW@TWWWWWW4y  =Q   tr1   c                    | j         r| j        r| j        r	 t          j        | j        dd          }|j        dk    r;|j        r4|j                            dd          }|                     d|            nA# t          $ r"}|                     d|            Y d}~nd}~ww xY wt          d	d
d           d| _         dS dS dS )z3Ring the terminal bell if needed and clear the flagT)shellcapture_outputr   r   r  r  z%Failed to run notifications command: Nr   )endflushF)r   r   r   
subprocessrun
returncodestderrr  r   r@   r  )rA   result	error_msgr   s       r/   rv  zInputOutput.ring_bell@  s    " 	,t'9 	,) 0S'^2$t  F (A--&--$*M$8$8$8$S$S	))*]R[*]*]^^^  S S S%%&Qa&Q&QRRRRRRRRS d$////&+D###	, 	, 	, 	,s   A"A: :
B&B!!B&c                     | j          | _         | j         r|                     d           dS |                     d           dS )z/Toggle between normal and multiline input modeszFMultiline mode: Enabled. Enter inserts newline, Alt-Enter submits textzGMultiline mode: Disabled. Alt-Enter inserts newline, Enter submits textN)r?   r   r9  s    r/   toggle_multiline_modez!InputOutput.toggle_multiline_modeQ  sg    "&"55 	X     Y    r1   c                 >   |r|r|                                 }d|z   }|r|r|                                }|dz   }|                    d          s|dz  }| j        	 | j        j                            dd           | j                            d| j        d          5 }|                    |           d d d            d S # 1 swxY w Y   d S # t          t          f$ r9}t          d	| j         d
           t          |           d | _        Y d }~d S d }~ww xY wd S )Nr?  r  r   Tr   r  ignore)rd   r  z.Warning: Unable to write to chat history file r   )r6   rV  endswithr   r   r   r   rd   r(  r   r   r  )rA   r   r  r  r6   r   r   s          r/   r   zInputOutput.append_chat_history]  s    	 $zz||$;D 	! %{{}}&=D}}T"" 	DLD!-.&-33D43PPP+00t}U]0^^ "bcGGDMMM" " " " " " " " " " " " " " " " " "#W- . . .`tG]```aaac


)-&&&&&&&. .-s=   AC  C6C CC 
CC D!.DDc           	         | j         sxg }t          pg           D ]}|                    | d           g }t          |          D ]}|v r|                    |            d                    ||z             dz   S t	                      }t          |d          }t          pg           }fdt          |          D             }|r'g }|D ]}	t          j                            t          j                            | j	        |	                    }
|                    t          t          |
          t          |	          k     r|
n|	                     t          d          g|z   }t	                      }t          |d                              t          |                     |                                                                }|                    t          |                     |rd |D             }|}|rt          d          g|z   }t	                      }t          |d                              t          |                     |                                                                }t          |          d	k    st          |          d	k    r|                                 |                    t          |                     |                                S )
Nz (read only)r   F)filer   c                     g | ]}|v|	S r+   r+   )r-   r   r  s     r/   ra   z6InputOutput.format_files_for_input.<locals>.<listcomp>  s$    YYY1DX;X;X!;X;X;Xr1   z	Readonly:c                 ,    g | ]}t          |          S r+   r    )r-   r   s     r/   ra   z6InputOutput.format_files_for_input.<locals>.<listcomp>  s    "C"C"Cq477"C"C"Cr1   z	Editable:r#   )r   r   rj   r   r   r   rg   rh   abspathr`   r!   r9   r  r   getvaluer  )rA   rc   r  read_only_files	full_patheditable_filesr   r   ro_pathsrel_pathabs_pathfiles_with_labelread_only_outputread_only_linestext_editable_fileseditable_outputeditable_liness     `              r/   rw  z"InputOutput.format_files_for_inputr  s   { 	F O#$8$>B?? C C	&&)'A'A'ABBBBN#J// 6 6	 444%%n555599_~=>>EEve<<< !5!;<<YYYYVJ%7%7YYY 	5H+ _ _7??27<<	8+L+LMMXX1N1NXXT\ ] ]^^^^ $[ 1 12X='zz)%@@@FFwO_G`G`aaa.7799DDFFOMM'"233444 	5"C"CN"C"C"C2 $$($5$5#69L#L "***_UCCCII'RbJcJcddd!0!9!9!;!;!F!F!H!H''!++s>/B/BQ/F/FMMOOOMM'"233444   r1   )F)r"  r#  )NN)T)r  T)r  NFNF)r   N)r   TN)r   TrX   )r   )FFT)+rL   rM   rN   r  r  rz  r   r   r   EMACSrW   r   r  r  r!  r/  r3  r:  r  r  r  r  r  r  r  r  rI   r  r  r  r   r   r   r  r  r  r  r  r   rv  r  r   rw  r+   r1   r/   r   r      s       M  $%"!%&*)-%"9G( G( G( G(R3 3 34!+ !+ !+F  $   2   >  + + + "S S S Sj	O 	O 	O) ) )) ) )/ / /7 7 7 7$' ' '     #v v v vp $ $ $ $L1 1 1 1,B B B BD D D D /4% 3 3 3 3 3&  & & & &(' ' '   ' ' '     D, , ,"
 
 
. . . .*.! .! .! .! .!r1   r   c                 h    	 t           j                            | |          S # t          $ r | cY S w xY wrX   )rg   rh   relpathr   )rw   r`   s     r/   r=  r=    sA    wud+++   s   " 11)Nr  rG   rg   r  rH  r
  r)  r  collectionsr   dataclassesr   r   ior   pathlibr   prompt_toolkit.completionr	   r
   r   prompt_toolkit.cursor_shapesr   prompt_toolkit.enumsr   prompt_toolkit.filtersr   r   prompt_toolkit.historyr   prompt_toolkit.key_bindingr   #prompt_toolkit.key_binding.vi_stater   prompt_toolkit.keysr   prompt_toolkit.lexersr   prompt_toolkit.output.vt100r   prompt_toolkit.shortcutsr   r   prompt_toolkit.stylesr   pygments.lexersr   r   pygments.tokenr   
rich.colorr   rich.columnsr   rich.consoler   rich.markdownr   
rich.styler  	rich.textr!   aider.mdstreamr"   r$   editorr%   utilsr&   r  r<   rI   r@   rK   rQ   r\   r   r=  r+   r1   r/   <module>rE     s        				            # # # # # # ! ! ! ! ! !                   N N N N N N N N N N ? ? ? ? ? ? , , , , , , : : : : : : : : . . . . . . 2 2 2 2 2 2 9 9 9 9 9 9 $ $ $ $ $ $ / / / / / / 8 8 8 8 8 8 A A A A A A A A ' ' ' ' ' ' C C C C C C C C             & & & & & &                         " " " " " " ) ) ) ) ) )       ) ) ) ) ) )                         9     "	 	 	 	 	 	 	 	 - - - - - - - -HE HE HE HE HEI HE HE HEVz! z! z! z! z! z! z! z!z    r1   