chore: Add autoedit parameter to diff_apply method
This commit is contained in:
parent
8830931562
commit
7f0b440e42
@ -117,7 +117,7 @@ class IDEService:
|
|||||||
return self._result
|
return self._result
|
||||||
|
|
||||||
@rpc_method
|
@rpc_method
|
||||||
def diff_apply(self, filepath, content) -> bool:
|
def diff_apply(self, filepath, content, autoedit: bool = False) -> bool:
|
||||||
"""
|
"""
|
||||||
Applies a given diff to a file.
|
Applies a given diff to a file.
|
||||||
|
|
||||||
|
@ -10,7 +10,7 @@ def run_code(code: str):
|
|||||||
|
|
||||||
|
|
||||||
@rpc_call
|
@rpc_call
|
||||||
def diff_apply(filepath, content):
|
def diff_apply(filepath, content, autoedit=False):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@ -3,10 +3,29 @@ import re
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from devchat.llm import chat
|
from devchat.llm import chat
|
||||||
|
from devchat.memory import FixSizeChatMemory
|
||||||
|
|
||||||
from lib.ide_service import IDEService
|
from lib.ide_service import IDEService
|
||||||
|
|
||||||
|
|
||||||
|
def extract_edits_block(text):
|
||||||
|
"""
|
||||||
|
Extracts the first Markdown code block from the given text without the language specifier.
|
||||||
|
|
||||||
|
:param text: A string containing Markdown text
|
||||||
|
:return: The content of the first Markdown code block, or None if not found
|
||||||
|
"""
|
||||||
|
index = text.find("```edits")
|
||||||
|
if index == -1:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
start = index + len("```edits")
|
||||||
|
end = text.find("```", start)
|
||||||
|
if end == -1:
|
||||||
|
return None
|
||||||
|
else:
|
||||||
|
return text[start:end]
|
||||||
|
|
||||||
def extract_markdown_block(text):
|
def extract_markdown_block(text):
|
||||||
"""
|
"""
|
||||||
Extracts the first Markdown code block from the given text without the language specifier.
|
Extracts the first Markdown code block from the given text without the language specifier.
|
||||||
@ -14,6 +33,10 @@ def extract_markdown_block(text):
|
|||||||
:param text: A string containing Markdown text
|
:param text: A string containing Markdown text
|
||||||
:return: The content of the first Markdown code block, or None if not found
|
:return: The content of the first Markdown code block, or None if not found
|
||||||
"""
|
"""
|
||||||
|
edit_code = extract_edits_block(text)
|
||||||
|
if edit_code:
|
||||||
|
return edit_code
|
||||||
|
|
||||||
pattern = r"```(?:\w+)?\s*\n(.*?)\n```"
|
pattern = r"```(?:\w+)?\s*\n(.*?)\n```"
|
||||||
match = re.search(pattern, text, re.DOTALL)
|
match = re.search(pattern, text, re.DOTALL)
|
||||||
|
|
||||||
@ -55,23 +78,112 @@ def input_issue_descriptions(file_path, issue_line_num):
|
|||||||
|
|
||||||
|
|
||||||
# step 3 : call llm to generate fix solutions
|
# step 3 : call llm to generate fix solutions
|
||||||
PROMPT = """
|
SYSTEM_ROLE_DIFF= """
|
||||||
You are a code refactoring assistant.
|
You are a code refactoring assistant. Your task is to refactor the user's code to fix lint diagnostics. You will be provided with a code snippet and a list of diagnostics. Your response should include two parts:
|
||||||
This is my code file:
|
|
||||||
{file_content}
|
|
||||||
|
|
||||||
There is a issue in the following code:
|
1. An explanation of the reason for the diagnostics and how to fix them.
|
||||||
{issue_line_code}
|
2. The edited code snippet with the diagnostics fixed, using markdown format for clarity.
|
||||||
{issue_description}
|
|
||||||
|
|
||||||
Here is the rule description:
|
The markdown block for edits should look like this:
|
||||||
{rule_description}
|
|
||||||
|
|
||||||
Please provide me refactor code to fix this issue.
|
```edits
|
||||||
|
def hello():
|
||||||
|
print("Call hello():")
|
||||||
|
+ print("hello")
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
- hello(20)
|
||||||
|
+ hello()
|
||||||
|
```
|
||||||
|
Or like this, if a variable is not defined:
|
||||||
|
|
||||||
|
```edits
|
||||||
|
...
|
||||||
|
+ cur_file = __file__
|
||||||
|
print(cur_file)
|
||||||
|
```
|
||||||
|
Please note the following important points:
|
||||||
|
|
||||||
|
1. The new code should maintain the correct indentation. The "+ " sign is followed by two spaces for indentation, which should be included in the edited code.
|
||||||
|
2. In addition to outputting key editing information, sufficient context (i.e., key information before and after editing) should also be provided to help locate the specific position of the edited line.
|
||||||
|
3. Don't output all file lines, if some lines are unchanged, please use "..." to indicate the ignored lines.
|
||||||
|
4. Use "+ " and "- " at start of the line to indicate the addition and deletion of lines.
|
||||||
|
|
||||||
|
Here are some examples of incorrect responses:
|
||||||
|
|
||||||
|
Incorrect example 1, where the indentation is not correct:
|
||||||
|
|
||||||
|
```edits
|
||||||
|
def hello():
|
||||||
|
print("Call hello():")
|
||||||
|
+ print("hello")
|
||||||
|
```
|
||||||
|
In this case, if the "+ " sign and the extra space are removed, the print("hello") statement will lack the necessary two spaces for correct indentation.
|
||||||
|
|
||||||
|
Incorrect example 2, where no other code lines are provided:
|
||||||
|
|
||||||
|
```edits
|
||||||
|
+ print("hello")
|
||||||
|
```
|
||||||
|
This is an incorrect example because without additional context, it's unclear where the new print("hello") statement should be inserted.
|
||||||
|
"""
|
||||||
|
|
||||||
|
SYSTEM_ROLE_CODEBLOCK = """
|
||||||
|
你是一个重构工程师,你需要根据错误描述,对代码进行问题修正,只需要关注描述的问题,不需要关注代码中的其他问题。
|
||||||
|
|
||||||
|
输出的修正代码中,如果修改了多个代码段,中间没有修改的代码段,请使用...表示。
|
||||||
|
每一个被修改的代码段,应该包含前后至少3行未修改的代码,作为修改代码段的边界表示。
|
||||||
|
|
||||||
|
输出一个代码块中,例如:
|
||||||
|
```edits
|
||||||
|
def hello():
|
||||||
|
msg = "hello"
|
||||||
|
print(msg)
|
||||||
|
|
||||||
|
...
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
hello()
|
||||||
|
```
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
@chat(prompt=PROMPT, stream_out=True)
|
LLM_MODEL = os.environ.get("LLM_MODEL", "gpt-3.5-turbo-1106")
|
||||||
|
if LLM_MODEL in ["qwen2-72b-instruct", "qwen-long", "qwen-turbo", "Yi-34B-Chat", "deepseek-coder", "xinghuo-3.5"]:
|
||||||
|
SYSTEM_ROLE = SYSTEM_ROLE_CODEBLOCK
|
||||||
|
else:
|
||||||
|
SYSTEM_ROLE = SYSTEM_ROLE_DIFF
|
||||||
|
MESSAGES_A = [
|
||||||
|
{
|
||||||
|
"role": "system",
|
||||||
|
"content": SYSTEM_ROLE,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
# step 3 : call llm to generate fix solutions
|
||||||
|
PROMPT = """
|
||||||
|
|
||||||
|
Here is the code file:
|
||||||
|
|
||||||
|
{file_content}
|
||||||
|
|
||||||
|
There is an issue in the following code:
|
||||||
|
|
||||||
|
{issue_line_code}
|
||||||
|
|
||||||
|
{issue_description}
|
||||||
|
|
||||||
|
Here is the rule description:
|
||||||
|
|
||||||
|
{rule_description}
|
||||||
|
|
||||||
|
Please focus only on the error described in the prompt. Other errors in the code should be disregarded.
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
memory = FixSizeChatMemory(max_size=20, messages=MESSAGES_A)
|
||||||
|
@chat(prompt=PROMPT, stream_out=True, memory=memory)
|
||||||
def call_llm_to_generate_fix_solutions(
|
def call_llm_to_generate_fix_solutions(
|
||||||
file_content, issue_line_code, issue_description, rule_description
|
file_content, issue_line_code, issue_description, rule_description
|
||||||
):
|
):
|
||||||
@ -117,16 +229,6 @@ def get_rule_description(issue_description):
|
|||||||
return issue_description
|
return issue_description
|
||||||
|
|
||||||
|
|
||||||
# step 4 : apply fix solutions to code
|
|
||||||
def apply_fix_solutions_to_code():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
# step 0: try parse user input
|
|
||||||
def try_parse_user_input():
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("start fix issue ...\n\n")
|
print("start fix issue ...\n\n")
|
||||||
file_path, issue_line, issue_line_num = get_selected_code()
|
file_path, issue_line, issue_line_num = get_selected_code()
|
||||||
@ -144,7 +246,7 @@ def main():
|
|||||||
print("make llm prompt ...\n\n")
|
print("make llm prompt ...\n\n")
|
||||||
current_file_content = get_current_file_content(file_path, issue_line_num)
|
current_file_content = get_current_file_content(file_path, issue_line_num)
|
||||||
rule_description = get_rule_description(issue_description)
|
rule_description = get_rule_description(issue_description)
|
||||||
print("--->>:", rule_description)
|
#print("Rule description:\n\n", rule_description, end="\n\n")
|
||||||
|
|
||||||
print("call llm to fix issue ...\n\n")
|
print("call llm to fix issue ...\n\n")
|
||||||
fix_solutions = call_llm_to_generate_fix_solutions(
|
fix_solutions = call_llm_to_generate_fix_solutions(
|
||||||
@ -156,6 +258,13 @@ def main():
|
|||||||
if not fix_solutions:
|
if not fix_solutions:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
|
print("\n\n", flush=True)
|
||||||
|
|
||||||
|
edits_code = extract_markdown_block(fix_solutions)
|
||||||
|
if not edits_code:
|
||||||
|
sys.exit(0)
|
||||||
|
IDEService().diff_apply("", edits_code, True)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user