11import os
22import sys
3-
43from colorama import Fore , Style
54from prompt_toolkit import prompt
6-
75from dir_assistant .assistant .cgrag_assistant import CGRAGAssistant
8-
9-
106class GitAssistant (CGRAGAssistant ):
117 def __init__ (
128 self ,
@@ -49,17 +45,14 @@ def __init__(
4945 thinking_end_pattern ,
5046 )
5147 self .commit_to_git = commit_to_git
52-
5348 def create_prompt (self , user_input ):
5449 if not self .commit_to_git :
5550 return super ().create_prompt (user_input )
5651 else :
5752 # Ask the LLM if a diff commit is appropriate
5853 should_diff_output = self .run_one_off_completion (
5954 f"""Does the prompt below request changes to files?
60-
6155Respond only with one word: "YES" or "NO". Do not respond with additional words or characters, only "YES" or "NO".
62-
6356User prompt:
6457<---------------------------->
6558{ user_input }
@@ -75,19 +68,16 @@ def create_prompt(self, user_input):
7568 if self .should_diff :
7669 return f"""If this is the final part of this prompt, this is the actual request to respond to. All information
7770above should be considered supplementary to this request to help answer it.
78-
7971User Prompt (RESPOND TO THIS PROMPT BY CREATING A FILE WITH THE SPECIFICATIONS BELOW):
8072<---------------------------->
8173{ user_input }
8274<---------------------------->
83-
8475Given the user prompt and included file snippets above, respond with the contents of a single file that has
8576the changes the user prompt requested. Do not provide an introduction, summary, or conclusion. Do not write
8677any additional text other than the file contents. Only respond with the file's contents. Markdown and other
8778formatting is NOT ALLOWED. Add the filename of the file as the first line of the response. It is okay to
8879create a new file. Always respond with the entire contents of the new version of the file. File snippets are
8980NOT ALLOWED. Ensure white space and new lines are consistent with the original.
90-
9181Example response:
9282<---------------------------->
9383/home/user/hello_project/hello_world.py
@@ -97,7 +87,6 @@ def create_prompt(self, user_input):
9787"""
9888 else :
9989 return user_input
100-
10190 def run_post_stream_processes (self , user_input , stream_output ):
10291 if (
10392 not self .commit_to_git or not self .should_diff
@@ -120,16 +109,14 @@ def run_post_stream_processes(self, user_input, stream_output):
120109 )
121110 output_lines = stream_output .split ("\n " )
122111 changed_filepath = output_lines [0 ].strip ()
123-
124112 # Security: Validate the filepath to prevent path traversal attacks
125- if ".." in changed_filepath or os . path . isabs ( changed_filepath ) :
113+ if ".." in changed_filepath :
126114 if self .chat_mode :
127115 self .write_error_message (
128116 f"Error: Invalid file path '{ changed_filepath } '. "
129117 "Path must be relative and within the project directory."
130118 )
131119 return True # Abort the operation
132-
133120 abs_path = os .path .abspath (changed_filepath )
134121 if not abs_path .startswith (os .getcwd ()):
135122 if self .chat_mode :
@@ -138,7 +125,6 @@ def run_post_stream_processes(self, user_input, stream_output):
138125 "Attempted to write outside the project directory."
139126 )
140127 return True # Abort the operation
141-
142128 file_content_lines = []
143129 if len (output_lines ) > 1 :
144130 file_content_lines = output_lines [1 :]
@@ -176,7 +162,6 @@ def run_post_stream_processes(self, user_input, stream_output):
176162 )
177163 sys .stdout .flush ()
178164 return True
179-
180165 def stream_chat (self , user_input ):
181166 self .git_apply_error = None
182167 super ().stream_chat (user_input )
0 commit comments