
- Added error handling to main functions in multiple modules - Ensured sys.exit(1) is called when responses are not successful - Improved robustness by checking response validity before proceeding
92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
import os
|
||
import sys
|
||
|
||
from devchat.llm import chat
|
||
|
||
from lib.ide_service import IDEService
|
||
|
||
|
||
def get_selected_code():
|
||
"""
|
||
Retrieves the selected lines of code from the user's selection.
|
||
|
||
This function extracts the text selected by the user in their IDE or text editor.
|
||
If no text has been selected, it prints an error message to stderr and exits the
|
||
program with a non-zero status indicating failure.
|
||
|
||
Returns:
|
||
dict: A dictionary containing the key 'selectedText' with the selected text
|
||
as its value. If no text is selected, the program exits.
|
||
"""
|
||
selected_data = IDEService().get_selected_range().dict()
|
||
|
||
miss_selected_error = "Please select some text."
|
||
if selected_data["range"]["start"] == selected_data["range"]["end"]:
|
||
readme_path = os.path.join(os.path.dirname(__file__), "README.md")
|
||
if os.path.exists(readme_path):
|
||
with open(readme_path, "r", encoding="utf-8") as f:
|
||
readme_text = f.read()
|
||
print(readme_text)
|
||
sys.exit(0)
|
||
|
||
print(miss_selected_error, file=sys.stderr, flush=True)
|
||
sys.exit(-1)
|
||
|
||
return selected_data
|
||
|
||
|
||
def get_visible_code():
|
||
"""
|
||
Retrieves visible code from the visible_lines function.
|
||
|
||
Returns:
|
||
visible_data: The visible code retrieved from the visible_lines function.
|
||
"""
|
||
visible_data = IDEService().get_visible_range().dict()
|
||
return visible_data
|
||
|
||
|
||
EXPLAIN_PROMPT = prompt = """
|
||
Your task is:
|
||
Explain the code.
|
||
Following the task requirements, explain the selected portion of the code. \
|
||
Note that you only explain the logic of the selected code. \
|
||
The visible code only serves as the context for you to understand the code. \
|
||
Here is the relevant context \
|
||
information for your reference:
|
||
1. selected code info: {selected_text}
|
||
2. current visible code info: {visible_text}
|
||
"""
|
||
|
||
EXPLAIN_PROMPT_ZH = prompt = """
|
||
你的任务是:
|
||
使用中文解释代码。
|
||
根据任务要求,解释被选中部分的代码。注意只解释被选中的代码逻辑,\
|
||
可见代码只是作为你理解代码的 context,你可以参考的 context 有:
|
||
1. 编辑器中被选中的代码:{selected_text}
|
||
2. 当前编辑器中可见代码:{visible_text}
|
||
"""
|
||
|
||
|
||
def get_prompt():
|
||
ide_language = IDEService().ide_language()
|
||
return EXPLAIN_PROMPT_ZH if ide_language == "zh" else EXPLAIN_PROMPT
|
||
|
||
|
||
@chat(prompt=get_prompt(), stream_out=True)
|
||
# pylint: disable=unused-argument
|
||
def explain(selected_text, visible_text):
|
||
"""
|
||
call ai to explain selected code
|
||
"""
|
||
pass # pylint: disable=unnecessary-pass
|
||
|
||
|
||
def main():
|
||
result = explain(selected_text=get_selected_code(), visible_text=get_visible_code())
|
||
sys.exit(0 if result else 1)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
main()
|