
    ^h                         S SK r SSKJr  SSKJrJrJr  SSKJr  SSK	J
r
  SSKJr  SS	KJr  SS
KJr   " S S\5      r " S S5      rg)    N   )clangd_lsp_integration   )build_followup_promptbuild_initial_promptinitial_instructions)	Assistant)chatdbg_config)CommandHistory)
ChatDBGLog)build_enriched_stacktracec                   (   ^  \ rS rSrU 4S jrSrU =r$ )DBGError   c                 D   > Xl         [        TU ]	  U R                   5        g N)messagesuper__init__)selfr   	__class__s     X/home/james-whalen/.local/lib/python3.13/site-packages/chatdbg/native_util/dbg_dialog.pyr   DBGError.__init__   s    &    )r   )__name__
__module____qualname____firstlineno__r   __static_attributes____classcell__)r   s   @r   r   r      s    ' 'r   r   c                   (   \ rS rSr\" \R                  \R                  " 5       SS9rS#S jr	S r
S rS	 rS
 rS rS$S\4S jjrS rS rS rS rS rS rS rS rS rS\S\4S jrS\S\S\4S jrS\S\S\S\4S jrS rS\4S jr S  r!S! r"S"r#g)%	DBGDialog   F)log_filenameconfigcapture_streamsreturnNc                 R    Xl         [        U R                   5      U l        SU l        g )NF)_promptr   _history_unsafe_cmd)r   prompts     r   r   DBGDialog.__init__    s    &t||4 r   c                     U R                  X#5      nU R                  R                  5         [        UR	                  XB5      S   5        U R
                  (       a  U R                  S5        SU l        g g )Nr   zWarning: One or more debugger commands were blocked as potentially unsafe.
Warning: You can disable sanitizing with `config --unsafe` and try again at your own risk.F)build_promptr*   clearprintqueryr+   warn)r   	assistant	user_textis_followupr,   s        r   query_and_printDBGDialog.query_and_print%   sb    ""9:ioof0;<II y  %D	 r   c                 *   U R                  5       nU R                  5         U R                  X!S5          [        S5      R	                  5       nUS;   a  OUS;   a  U R                  X#S5        OUS:X  a  [        U R                  5        OgU R                  U5      nU R                  U5      (       a  U R                  X#S5        O-US:w  a  U R                  R                  X45        [        U5         M  UR                  5         g ! [         a     M  f = f)NFTz(ChatDBG chatting) )exitquit)chatwhyhistorytest_prompt)_make_assistantcheck_debugger_stater7   inputstripr1   r*   _run_one_command_message_is_a_bad_command_errorappendEOFErrorclose)r   r5   r4   commandresults        r   dialogDBGDialog.dialog0   s    ((*	!!#Y59 56<<>..o-((TB	)$--( "227;F;;FCC,,YF"m3 MM00Af% . 		  s)   D D .D 
9D ,D 
DDc                     g r    r   rI   s     r   rD   DBGDialog._run_one_commandO       r   c                     g r   rN   r   r   s     r   rE   )DBGDialog._message_is_a_bad_command_errorR   rQ   r   c                     g r   rN   r   s    r   rA   DBGDialog.check_debugger_stateU   rQ   r   max_entriesc                     g r   rN   )r   rX   s     r   _get_frame_summariesDBGDialog._get_frame_summariesX   rQ   r   c                 8    U R                  5       n[        U5      $ r   )_supported_functionsr   r   	functionss     r   initial_prompt_instructions%DBGDialog.initial_prompt_instructions[   s    --/	#I..r   c                 4    [        U R                  5       5      $ r   )r   rZ   rV   s    r   &_initial_prompt_enchriched_stack_trace0DBGDialog._initial_prompt_enchriched_stack_trace_   s    ()B)B)DEEr   c                     g r   rN   rV   s    r   _initial_prompt_error_message'DBGDialog._initial_prompt_error_messageb       r   c                     g)z:Anything more beyond the initial error message to include.NrN   rV   s    r   _initial_prompt_error_details'DBGDialog._initial_prompt_error_detailse   s    r   c                     g r   rN   rV   s    r   _initial_prompt_command_line&DBGDialog._initial_prompt_command_linei   rh   r   c                     g r   rN   rV   s    r   _initial_prompt_inputDBGDialog._initial_prompt_inputl   rh   r   c                     g)zZ
Return a simple backtrace to show the LLM where we are on the stack
in followup prompts.
NrN   rV   s    r   _prompt_stackDBGDialog._prompt_stacko   s    
 r   c                 ,    [        U R                  5      $ r   )strr*   rV   s    r   _prompt_historyDBGDialog._prompt_historyv   s    4==!!r   c           
      (   U(       dc  [        U R                  5       U R                  5       U R                  5       U R	                  5       U R                  5       U R                  5       US9$ [        U R                  5       U R                  5       U5      $ )N)r5   )	r   rc   rf   rj   rm   rp   rw   r   rs   )r   arg
conversings      r   r/   DBGDialog.build_prompty   s    ';;=224224113**,$$&  )$$&(:(:(<c r   rI   c                     g r   rN   rO   s     r   	llm_debugDBGDialog.llm_debug   rQ   r   filenameline_numberc                 @    SU SU 3U R                  SU SU 35      4$ )a  
{
    "name": "get_code_surrounding",
    "description": "The `get_code_surrounding` function returns the source code in the given file surrounding and including the provided line number.",
    "parameters": {
        "type": "object",
        "properties": {
            "filename": {
                "type": "string",
                "description": "The filename to read from."
            },
            "line_number": {
                "type": "integer",
                "description": "The line number to focus on. Some context before and after that line will be provided."
            }
        },
        "required": [ "filename", "line_number" ]
    }
}
zcode :rD   )r   r   r   s      r   llm_get_code_surrounding"DBGDialog.llm_get_code_surrounding   s;    * xj+/1F1FH:Q{m,2
 
 	
r   symbolc           	      L    SU SU SU 3U R                  SU SU SU 35      4$ )aA  
{
    "name": "find_definition",
    "description": "The `find_definition` function returns the source code for the definition for the given symbol at the given source line number.  Call `find_definition` on every symbol that could be linked to the issue.",
    "parameters": {
        "type": "object",
        "properties": {
            "filename": {
                "type": "string",
                "description": "The filename the symbol is from."
            },
            "line_number": {
                "type": "integer",
                "description": "The line number where the symbol is present."
            },
            "symbol": {
                "type": "string",
                "description": "The symbol to lookup."
            }
        },
        "required": [ "filename", "line_number", "symbol" ]
    }
}
zdefinition r    r   )r   r   r   r   s       r   llm_find_definitionDBGDialog.llm_find_definition   sH    2 XJa}AfX>@U@U(1[M6(;A
 
 	
r   c                     U R                   U R                  /n[        R                  " 5       (       a  XR                  /-  nU$ r   )r~   r   r   is_availabler   r^   s     r   r]   DBGDialog._supported_functions   s<    ^^T%B%BC	!..002233Ir   c           	      :   U R                  5       nU R                  5       n[        R                  " [        R
                  U R                  SS5      n[        U[        R                  [        R                  U[        R                  (       + UU R                  /S9nU$ )Nz   P   )modeldebugr_   stream	listeners)r]   r`   r
   make_printersys
__stdout__r)   r	   r   r   	no_stream_log)r   r_   instruction_promptprinterr4   s        r   r@   DBGDialog._make_assistant   s    --/	!==?
 !--cnndllESUV && &&%///		

	 r   c                     [        U5        g r   )r1   rS   s     r   r3   DBGDialog.warn   s	    gr   c                     [        U5      er   )r   rS   s     r   failDBGDialog.fail   s    wr   )r*   r)   r+   )r'   N)   )$r   r   r   r   r   r
   logto_jsonr   r   r7   rK   rD   rE   rA   intrZ   r`   rc   rf   rj   rm   rp   rs   rw   r/   rv   r~   r   r   r]   r	   r@   r3   r   r   rN   r   r   r"   r"      s    #''%%'D!
	%> /F"   
 
3 
3 
2
C 
c 
3 
SV 
: 0 r   r"   )r    r   util.promptsr   r   r   assistant.assistantr	   util.configr
   util.historyr   util.logr   stacksr   	Exceptionr   r"   rN   r   r   <module>r      s>    
 $  , ( ) ! -'y 'L  L r   