Replace the usage of services functions with IDEService client

This commit is contained in:
kagami 2024-02-03 23:35:09 +08:00
parent 5f1926fcdd
commit ef1c9ef3cb
9 changed files with 32 additions and 76 deletions

View File

@ -6,7 +6,7 @@ from chat.ask_codebase.chains.smart_qa import SmartQA
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs"))
sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "libs"))
from ide_services import get_lsp_brige_port # noqa: E402
from ide_services import IDEService # noqa: E402
def query(question, lsp_brige_port):
@ -59,7 +59,7 @@ def main():
print("Usage: python index_and_query.py query [question] [port]")
sys.exit(1)
port = get_lsp_brige_port()
port = IDEService().get_lsp_brige_port()
question = sys.argv[2]
query(question, port)

View File

@ -9,7 +9,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), "..", "..", "libs"))
sys.path.append(os.path.dirname(__file__))
from chatmark import Checkbox, Form, TextEditor # noqa: E402
from ide_services.services import log_info
from ide_services import IDEService # noqa: E402
from llm_api import chat_completion_stream # noqa: E402
diff_too_large_message_en = (
@ -67,17 +67,19 @@ def read_prompt_from_file(filename):
- FileNotFoundError: If the file does not exist.
- Exception: If any other error occurs during file reading.
"""
s = IDEService()
try:
with open(filename, "r", encoding="utf-8") as file:
return file.read().strip()
except FileNotFoundError:
log_info(
s.ide_logging(
"info",
f"File {filename} not found. "
"Please make sure it exists in the same directory as the script."
"Please make sure it exists in the same directory as the script.",
)
sys.exit(1)
except Exception as e:
log_info(f"An error occurred while reading the file {filename}: {e}")
s.ide_logging("info", f"An error occurred while reading the file {filename}: {e}")
sys.exit(1)

View File

@ -1,15 +1,5 @@
from .services import (
get_lsp_brige_port,
ide_language,
ide_logging,
install_python_env,
update_slash_commands,
)
from .service import IDEService
__all__ = [
"get_lsp_brige_port",
"install_python_env",
"update_slash_commands",
"ide_language",
"ide_logging",
"IDEService",
]

View File

@ -1,9 +1,11 @@
from typing import List
import os
from functools import wraps
from .types import Location, SymbolNode
from typing import List
import requests
from .types import Location, SymbolNode
BASE_SERVER_URL = os.environ.get("DEVCHAT_IDE_SERVICE_URL", "http://localhost:3000")
@ -47,6 +49,15 @@ def rpc_method(f):
class IDEService:
"""
Client for IDE service
Usage:
client = IDEService()
res = client.ide_language()
res = client.ide_logging("info", "some message")
"""
def __init__(self):
self._result = None
@ -78,7 +89,5 @@ class IDEService:
return [SymbolNode.parse_obj(node) for node in self._result]
@rpc_method
def find_type_def_locations(
self, abspath: str, line: int, character: int
) -> List[Location]:
def find_type_def_locations(self, abspath: str, line: int, character: int) -> List[Location]:
return [Location.parse_obj(loc) for loc in self._result]

View File

@ -1,47 +0,0 @@
from typing import List
from .rpc import rpc_call
from .types import Location, SymbolNode
@rpc_call
def get_lsp_brige_port() -> str:
pass
@rpc_call
def install_python_env(command_name: str, requirements_file: str) -> str:
pass
@rpc_call
def update_slash_commands() -> bool:
pass
@rpc_call
def ide_language() -> str:
pass
@rpc_call
def ide_logging(level: str, message: str) -> bool:
"""
level: "info" | "warn" | "error" | "debug"
"""
pass
@rpc_call
def get_document_symbols(abspath: str) -> List[SymbolNode]:
pass
@rpc_call
def find_type_def_locations(abspath: str, line: int, character: int) -> List[Location]:
pass
# NOTE: for compatibility, remove this after all usages are replaced with ide_logging
def log_info(message) -> bool:
return ide_logging("info", message)

View File

@ -1,4 +1,5 @@
from typing import List
from pydantic import BaseModel

View File

@ -7,7 +7,6 @@ sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from chatmark import Checkbox, Form, TextEditor # noqa: #402
from ide_services.services import log_info # noqa: #402
class MissEditConfirmFieldException(Exception):

View File

@ -9,7 +9,7 @@ from .openai import chat_call_completion_stream
sys.path.append(os.path.join(os.path.dirname(__file__), ".."))
from chatmark import Checkbox, Form, Radio, TextEditor # noqa: #402
from ide_services.services import log_info, log_warn # noqa: #402
from ide_services import IDEService # noqa: #402
class MissToolsFieldException(Exception):
@ -191,9 +191,10 @@ def chat_tools(
# call function
functions = {tool.function_name: tool for tool in tools}
for call in response["all_calls"]:
log_info(
IDEService().ide_logging(
"info",
f"try to call function tool: {call['function_name']} "
f"with {call['parameters']}"
f"with {call['parameters']}",
)
tool = functions[call["function_name"]]
result = tool(**json.loads(call["parameters"]))

View File

@ -10,7 +10,7 @@ sys.path.append(os.path.join(os.path.dirname(__file__), "..", "libs"))
from chatmark import Checkbox, Form, Step, TextEditor # noqa: E402
from find_reference_tests import find_reference_tests
from i18n import TUILanguage, get_translation
from ide_services import ide_language # noqa: E402
from ide_services import IDEService # noqa: E402
from model import (
FuncToTest,
TokenBudgetExceededException,
@ -214,7 +214,8 @@ def main(input: str):
user_prompt = f"Help me write unit tests for the `{func_name}` function"
repo_root = os.getcwd()
ide_lang = ide_language()
ide_lang = IDEService().ide_language()
tui_lang = TUILanguage.from_str(ide_lang)
_i = get_translation(tui_lang)