33import difflib
44import re
55
6+ from langchain_anthropic import ChatAnthropic
67from langchain_core .prompts import ChatPromptTemplate
7- from langchain_openai import ChatOpenAI
88
99from codegen import Codebase
1010
1111from .tool_prompts import _HUMAN_PROMPT_DRAFT_EDITOR , _SYSTEM_PROMPT_DRAFT_EDITOR
12+ from .view_file import add_line_numbers
1213
1314
1415def generate_diff (original : str , modified : str ) -> str :
@@ -117,15 +118,25 @@ def semantic_edit(codebase: Codebase, filepath: str, edit_content: str, start: i
117118 original_content = file .content
118119 original_lines = original_content .split ("\n " )
119120
121+ # Check if file is too large for full edit
122+ MAX_LINES = 300
123+ if len (original_lines ) > MAX_LINES and start == 1 and end == - 1 :
124+ return {
125+ "error" : (
126+ f"File is { len (original_lines )} lines long. For files longer than { MAX_LINES } lines, "
127+ "please specify a line range using start and end parameters. "
128+ "You may need to make multiple targeted edits."
129+ ),
130+ "status" : "error" ,
131+ "line_count" : len (original_lines ),
132+ }
133+
120134 # Handle append mode
121135 if start == - 1 and end == - 1 :
122136 try :
123137 file .add_symbol_from_source (edit_content )
124138 codebase .commit ()
125139
126- # Analyze changes for append
127- new_lines = file .content .split ("\n " )
128-
129140 return {"filepath" : filepath , "content" : file .content , "diff" : generate_diff (original_content , file .content ), "status" : "success" }
130141 except Exception as e :
131142 msg = f"Failed to append content: { e !s} "
@@ -144,10 +155,10 @@ def semantic_edit(codebase: Codebase, filepath: str, edit_content: str, start: i
144155 system_message = _SYSTEM_PROMPT_DRAFT_EDITOR
145156 human_message = _HUMAN_PROMPT_DRAFT_EDITOR
146157 prompt = ChatPromptTemplate .from_messages ([system_message , human_message ])
147- llm = ChatOpenAI (
148- model = "gpt-4o " ,
158+ llm = ChatAnthropic (
159+ model = "claude-3-5-sonnet-latest " ,
149160 temperature = 0 ,
150- max_tokens = 10000 ,
161+ max_tokens = 5000 ,
151162 )
152163 chain = prompt | llm
153164 response = chain .invoke ({"original_file_section" : original_file_section , "edit_content" : edit_content })
@@ -173,4 +184,4 @@ def semantic_edit(codebase: Codebase, filepath: str, edit_content: str, start: i
173184 file .edit (new_content )
174185 codebase .commit ()
175186
176- return {"filepath" : filepath , "diff" : diff , "status" : "success" }
187+ return {"filepath" : filepath , "diff" : diff , "status" : "success" , "new_content" : add_line_numbers ( new_content ) }
0 commit comments