From 0addbf043595f97b092c87c0f4702d6b0e205ad1 Mon Sep 17 00:00:00 2001 From: Erno Hopearuoho Date: Fri, 9 Feb 2024 09:49:51 +0200 Subject: [PATCH 1/4] move files under subdirectory --- rplugin/python3/{ => CopilotChat}/copilot.py | 0 .../python3/{copilot-plugin.py => CopilotChat/copilot_plugin.py} | 0 rplugin/python3/{ => CopilotChat}/handlers/chat_handler.py | 0 .../python3/{ => CopilotChat}/handlers/inplace_chat_handler.py | 0 rplugin/python3/{ => CopilotChat}/handlers/vsplit_chat_handler.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/core/autocmdmapper.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/core/buffer.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/core/keymapper.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/core/nvim.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/core/window.py | 0 .../{ => CopilotChat}/mypynvim/ui_components/calculator.py | 0 .../python3/{ => CopilotChat}/mypynvim/ui_components/layout.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/popup.py | 0 rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/types.py | 0 rplugin/python3/{ => CopilotChat}/prompts.py | 0 rplugin/python3/{ => CopilotChat}/typings.py | 0 rplugin/python3/{ => CopilotChat}/utilities.py | 0 17 files changed, 0 insertions(+), 0 deletions(-) rename rplugin/python3/{ => CopilotChat}/copilot.py (100%) rename rplugin/python3/{copilot-plugin.py => CopilotChat/copilot_plugin.py} (100%) rename rplugin/python3/{ => CopilotChat}/handlers/chat_handler.py (100%) rename rplugin/python3/{ => CopilotChat}/handlers/inplace_chat_handler.py (100%) rename rplugin/python3/{ => CopilotChat}/handlers/vsplit_chat_handler.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/core/autocmdmapper.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/core/buffer.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/core/keymapper.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/core/nvim.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/core/window.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/calculator.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/layout.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/popup.py (100%) rename rplugin/python3/{ => CopilotChat}/mypynvim/ui_components/types.py (100%) rename rplugin/python3/{ => CopilotChat}/prompts.py (100%) rename rplugin/python3/{ => CopilotChat}/typings.py (100%) rename rplugin/python3/{ => CopilotChat}/utilities.py (100%) diff --git a/rplugin/python3/copilot.py b/rplugin/python3/CopilotChat/copilot.py similarity index 100% rename from rplugin/python3/copilot.py rename to rplugin/python3/CopilotChat/copilot.py diff --git a/rplugin/python3/copilot-plugin.py b/rplugin/python3/CopilotChat/copilot_plugin.py similarity index 100% rename from rplugin/python3/copilot-plugin.py rename to rplugin/python3/CopilotChat/copilot_plugin.py diff --git a/rplugin/python3/handlers/chat_handler.py b/rplugin/python3/CopilotChat/handlers/chat_handler.py similarity index 100% rename from rplugin/python3/handlers/chat_handler.py rename to rplugin/python3/CopilotChat/handlers/chat_handler.py diff --git a/rplugin/python3/handlers/inplace_chat_handler.py b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py similarity index 100% rename from rplugin/python3/handlers/inplace_chat_handler.py rename to rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py diff --git a/rplugin/python3/handlers/vsplit_chat_handler.py b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py similarity index 100% rename from rplugin/python3/handlers/vsplit_chat_handler.py rename to rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py diff --git a/rplugin/python3/mypynvim/core/autocmdmapper.py b/rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py similarity index 100% rename from rplugin/python3/mypynvim/core/autocmdmapper.py rename to rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py diff --git a/rplugin/python3/mypynvim/core/buffer.py b/rplugin/python3/CopilotChat/mypynvim/core/buffer.py similarity index 100% rename from rplugin/python3/mypynvim/core/buffer.py rename to rplugin/python3/CopilotChat/mypynvim/core/buffer.py diff --git a/rplugin/python3/mypynvim/core/keymapper.py b/rplugin/python3/CopilotChat/mypynvim/core/keymapper.py similarity index 100% rename from rplugin/python3/mypynvim/core/keymapper.py rename to rplugin/python3/CopilotChat/mypynvim/core/keymapper.py diff --git a/rplugin/python3/mypynvim/core/nvim.py b/rplugin/python3/CopilotChat/mypynvim/core/nvim.py similarity index 100% rename from rplugin/python3/mypynvim/core/nvim.py rename to rplugin/python3/CopilotChat/mypynvim/core/nvim.py diff --git a/rplugin/python3/mypynvim/core/window.py b/rplugin/python3/CopilotChat/mypynvim/core/window.py similarity index 100% rename from rplugin/python3/mypynvim/core/window.py rename to rplugin/python3/CopilotChat/mypynvim/core/window.py diff --git a/rplugin/python3/mypynvim/ui_components/calculator.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py similarity index 100% rename from rplugin/python3/mypynvim/ui_components/calculator.py rename to rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py diff --git a/rplugin/python3/mypynvim/ui_components/layout.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py similarity index 100% rename from rplugin/python3/mypynvim/ui_components/layout.py rename to rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py diff --git a/rplugin/python3/mypynvim/ui_components/popup.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py similarity index 100% rename from rplugin/python3/mypynvim/ui_components/popup.py rename to rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py diff --git a/rplugin/python3/mypynvim/ui_components/types.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/types.py similarity index 100% rename from rplugin/python3/mypynvim/ui_components/types.py rename to rplugin/python3/CopilotChat/mypynvim/ui_components/types.py diff --git a/rplugin/python3/prompts.py b/rplugin/python3/CopilotChat/prompts.py similarity index 100% rename from rplugin/python3/prompts.py rename to rplugin/python3/CopilotChat/prompts.py diff --git a/rplugin/python3/typings.py b/rplugin/python3/CopilotChat/typings.py similarity index 100% rename from rplugin/python3/typings.py rename to rplugin/python3/CopilotChat/typings.py diff --git a/rplugin/python3/utilities.py b/rplugin/python3/CopilotChat/utilities.py similarity index 100% rename from rplugin/python3/utilities.py rename to rplugin/python3/CopilotChat/utilities.py From 3e906262704a2fd27e0bf6aaf6d09d288866a108 Mon Sep 17 00:00:00 2001 From: Erno Hopearuoho Date: Fri, 9 Feb 2024 09:50:31 +0200 Subject: [PATCH 2/4] change to use local relative imports --- rplugin/python3/CopilotChat/__init__.py | 1 + rplugin/python3/CopilotChat/copilot.py | 27 +++++++++---------- rplugin/python3/CopilotChat/copilot_plugin.py | 6 ++--- .../CopilotChat/handlers/chat_handler.py | 22 +++++++-------- .../handlers/inplace_chat_handler.py | 26 +++++++++--------- .../handlers/vsplit_chat_handler.py | 6 ++--- .../mypynvim/ui_components/calculator.py | 2 +- .../mypynvim/ui_components/layout.py | 2 +- .../mypynvim/ui_components/popup.py | 6 ++--- rplugin/python3/CopilotChat/utilities.py | 20 +++++++------- 10 files changed, 59 insertions(+), 59 deletions(-) create mode 100644 rplugin/python3/CopilotChat/__init__.py diff --git a/rplugin/python3/CopilotChat/__init__.py b/rplugin/python3/CopilotChat/__init__.py new file mode 100644 index 00000000..446ecc17 --- /dev/null +++ b/rplugin/python3/CopilotChat/__init__.py @@ -0,0 +1 @@ +from .copilot_plugin import CopilotPlugin as CopilotPlugin diff --git a/rplugin/python3/CopilotChat/copilot.py b/rplugin/python3/CopilotChat/copilot.py index 4a86ae26..801a099c 100644 --- a/rplugin/python3/CopilotChat/copilot.py +++ b/rplugin/python3/CopilotChat/copilot.py @@ -5,10 +5,10 @@ from typing import Dict, List import dotenv -import prompts +from .prompts import * import requests -import typings -import utilities +from .typings import * +from .utilities import * from prompt_toolkit import PromptSession from prompt_toolkit.history import InMemoryHistory @@ -20,16 +20,15 @@ "user-agent": "GithubCopilot/1.133.0", } - class Copilot: def __init__(self, token: str = None): if token is None: - token = utilities.get_cached_token() + token = get_cached_token() self.github_token = token self.token: Dict[str, any] = None - self.chat_history: List[typings.Message] = [] + self.chat_history: List[Message] = [] self.vscode_sessionid: str = None - self.machineid = utilities.random_hex() + self.machineid = random_hex() self.session = requests.Session() @@ -68,7 +67,7 @@ def poll_auth(self, device_code: str) -> bool: "accept": "application/json", } response = self.session.get(url, headers=headers).json() - utilities.cache_token(response["login"], access_token) + cache_token(response["login"], access_token) self.github_token = access_token return True return False @@ -104,8 +103,8 @@ def ask( if not system_prompt: system_prompt = prompts.COPILOT_INSTRUCTIONS url = "https://api.githubcopilot.com/chat/completions" - self.chat_history.append(typings.Message(prompt, "user")) - data = utilities.generate_request( + self.chat_history.append(Message(prompt, "user")) + data = generate_request( self.chat_history, code, language, system_prompt=system_prompt, model=model ) @@ -149,17 +148,17 @@ def ask( print("Error:", line) continue - self.chat_history.append(typings.Message(full_response, "system")) + self.chat_history.append(Message(full_response, "system")) - def _get_embeddings(self, inputs: list[typings.FileExtract]): + def _get_embeddings(self, inputs: list[FileExtract]): embeddings = [] url = "https://api.githubcopilot.com/embeddings" # If we have more than 18 files, we need to split them into multiple requests for i in range(0, len(inputs), 18): if i + 18 > len(inputs): - data = utilities.generate_embedding_request(inputs[i:]) + data = generate_embedding_request(inputs[i:]) else: - data = utilities.generate_embedding_request(inputs[i : i + 18]) + data = generate_embedding_request(inputs[i : i + 18]) response = self.session.post(url, headers=self._headers(), json=data).json() if "data" not in response: raise Exception(f"Error fetching embeddings: {response}") diff --git a/rplugin/python3/CopilotChat/copilot_plugin.py b/rplugin/python3/CopilotChat/copilot_plugin.py index ec9b4b5b..cec89d8c 100644 --- a/rplugin/python3/CopilotChat/copilot_plugin.py +++ b/rplugin/python3/CopilotChat/copilot_plugin.py @@ -1,7 +1,7 @@ import pynvim -from handlers.inplace_chat_handler import InPlaceChatHandler -from handlers.vsplit_chat_handler import VSplitChatHandler -from mypynvim.core.nvim import MyNvim +from .handlers.inplace_chat_handler import InPlaceChatHandler +from .handlers.vsplit_chat_handler import VSplitChatHandler +from .mypynvim.core.nvim import MyNvim PLUGIN_MAPPING_CMD = "CopilotChatMapping" PLUGIN_AUTOCMD_CMD = "CopilotChatAutocmd" diff --git a/rplugin/python3/CopilotChat/handlers/chat_handler.py b/rplugin/python3/CopilotChat/handlers/chat_handler.py index 2d9ade76..049fa782 100644 --- a/rplugin/python3/CopilotChat/handlers/chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/chat_handler.py @@ -2,10 +2,10 @@ from datetime import datetime from typing import Optional, cast -import prompts as system_prompts -from copilot import Copilot -from mypynvim.core.buffer import MyBuffer -from mypynvim.core.nvim import MyNvim +from ..prompts import * +from ..copilot import Copilot +from ..mypynvim.core.buffer import MyBuffer +from ..mypynvim.core.nvim import MyNvim def is_module_installed(name): @@ -66,13 +66,13 @@ def chat( # private def _construct_system_prompt(self, prompt: str): - system_prompt = system_prompts.COPILOT_INSTRUCTIONS - if prompt == system_prompts.FIX_SHORTCUT: - system_prompt = system_prompts.COPILOT_FIX - elif prompt == system_prompts.TEST_SHORTCUT: - system_prompt = system_prompts.COPILOT_TESTS - elif prompt == system_prompts.EXPLAIN_SHORTCUT: - system_prompt = system_prompts.COPILOT_EXPLAIN + system_prompt = COPILOT_INSTRUCTIONS + if prompt == FIX_SHORTCUT: + system_prompt = COPILOT_FIX + elif prompt == TEST_SHORTCUT: + system_prompt = COPILOT_TESTS + elif prompt == EXPLAIN_SHORTCUT: + system_prompt = COPILOT_EXPLAIN return system_prompt def _add_start_separator( diff --git a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py index dc5db6ac..96048527 100644 --- a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py @@ -1,9 +1,9 @@ -import prompts as system_prompts -from handlers.chat_handler import ChatHandler -from mypynvim.core.buffer import MyBuffer -from mypynvim.core.nvim import MyNvim -from mypynvim.ui_components.layout import Box, Layout -from mypynvim.ui_components.popup import PopUp +from ..prompts import * +from .chat_handler import ChatHandler +from ..mypynvim.core.buffer import MyBuffer +from ..mypynvim.core.nvim import MyNvim +from ..mypynvim.ui_components.layout import Box, Layout +from ..mypynvim.ui_components.popup import PopUp # Define constants for the models MODEL_GPT4 = "gpt-4" @@ -164,7 +164,7 @@ def _chat(self): self.filetype, self.original_code, self.copilot_popup.window.handle, - system_prompt=system_prompts.__dict__[self.system_prompt], + system_prompt=prompts.__dict__[self.system_prompt], disable_start_separator=True, disable_end_separator=True, model=self.model, @@ -203,7 +203,7 @@ def _toggle_model(self): def _toggle_system_model(self): # Create a list of all system prompts and add the current system prompt - system_prompts = [ + prompts = [ "SENIOR_DEVELOPER_PROMPT", "COPILOT_EXPLAIN", "COPILOT_TESTS", @@ -215,11 +215,11 @@ def _toggle_system_model(self): ] # Get the index of the current system prompt - current_system_prompt_index = system_prompts.index(self.system_prompt) + current_system_prompt_index = prompts.index(self.system_prompt) # Set the next system prompt - self.system_prompt = system_prompts[ - (current_system_prompt_index + 1) % len(system_prompts) + self.system_prompt = prompts[ + (current_system_prompt_index + 1) % len(prompts) ] self.copilot_popup.original_config.title = ( @@ -240,10 +240,10 @@ def _set_keymaps(self): self.prompt_popup.map("n", "", lambda cb=self._toggle_system_model: cb()) self.prompt_popup.map( - "n", "'", lambda: self._set_prompt(system_prompts.PROMPT_SIMPLE_DOCSTRING) + "n", "'", lambda: self._set_prompt(PROMPT_SIMPLE_DOCSTRING) ) self.prompt_popup.map( - "n", "s", lambda: self._set_prompt(system_prompts.PROMPT_SEPARATE) + "n", "s", lambda: self._set_prompt(PROMPT_SEPARATE) ) self.prompt_popup.map( diff --git a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py index 191fd6b2..4e4a2ae8 100644 --- a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py @@ -1,6 +1,6 @@ -from handlers.chat_handler import ChatHandler -from mypynvim.core.buffer import MyBuffer -from mypynvim.core.nvim import MyNvim +from .chat_handler import ChatHandler +from ..mypynvim.core.buffer import MyBuffer +from ..mypynvim.core.nvim import MyNvim class VSplitChatHandler(ChatHandler): diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py index 1f19d4ab..91626b16 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py @@ -3,7 +3,7 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Union -from mypynvim.core.nvim import MyNvim +from ..core.nvim import MyNvim if TYPE_CHECKING: from .popup import PopUpConfiguration diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py index dfea8c87..bf4f7e5a 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py @@ -1,7 +1,7 @@ from dataclasses import dataclass from typing import Callable, Literal, Optional, Union, cast -from mypynvim.core.nvim import MyNvim +from ..core.nvim import MyNvim from .calculator import Calculator from .popup import PopUp diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py index cab2bac9..1a6c1f31 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py @@ -5,13 +5,13 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union if TYPE_CHECKING: - from mypynvim.core.nvim import MyNvim + from ..core.nvim import MyNvim from .layout import Layout -from mypynvim.core.buffer import MyBuffer -from mypynvim.core.window import MyWindow +from ..core.buffer import MyBuffer +from ..core.window import MyWindow from .calculator import Calculator from .types import PaddingKeys, PopUpConfiguration, Relative diff --git a/rplugin/python3/CopilotChat/utilities.py b/rplugin/python3/CopilotChat/utilities.py index bc0540a7..8e028559 100644 --- a/rplugin/python3/CopilotChat/utilities.py +++ b/rplugin/python3/CopilotChat/utilities.py @@ -2,8 +2,8 @@ import os import random -import prompts -import typings +from .prompts import * +from .typings import * def random_hex(length: int = 65): @@ -11,10 +11,10 @@ def random_hex(length: int = 65): def generate_request( - chat_history: list[typings.Message], + chat_history: list[Message], code_excerpt: str, language: str = "", - system_prompt=prompts.COPILOT_INSTRUCTIONS, + system_prompt=COPILOT_INSTRUCTIONS, model="gpt-4", ): messages = [ @@ -49,7 +49,7 @@ def generate_request( } -def generate_embedding_request(inputs: list[typings.FileExtract]): +def generate_embedding_request(inputs: list[FileExtract]): return { "input": [ f"File: `{i.filepath}`\n```{i.filepath.split('.')[-1]}\n{i.code}```" @@ -97,11 +97,11 @@ def get_cached_token(): json.dumps( generate_request( [ - typings.Message("Hello, Copilot!", "user"), - typings.Message("Hello, World!", "system"), - typings.Message("How are you?", "user"), - typings.Message("I am fine, thanks.", "system"), - typings.Message("What does this code do?", "user"), + Message("Hello, Copilot!", "user"), + Message("Hello, World!", "system"), + Message("How are you?", "user"), + Message("I am fine, thanks.", "system"), + Message("What does this code do?", "user"), ], "print('Hello, World!')", "python", From 7eb6c905220dae45d72969744c8f42bb59237864 Mon Sep 17 00:00:00 2001 From: Erno Hopearuoho Date: Fri, 9 Feb 2024 20:09:13 +0200 Subject: [PATCH 3/4] fix healthcheck to support pynvim 0.4.3 and use correct python version --- lua/CopilotChat/health.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/CopilotChat/health.lua b/lua/CopilotChat/health.lua index f109065f..0dd28813 100644 --- a/lua/CopilotChat/health.lua +++ b/lua/CopilotChat/health.lua @@ -24,7 +24,7 @@ end --- Run a python command and handle potential errors ---@param command string local function run_python_command(command) - return run_command_on_executable('python3', command) + return run_command_on_executable(vim.g.python3_host_prog, command) end -- Add health check for python3 and pynvim @@ -52,7 +52,7 @@ function M.check() return end - file:write('import pynvim; print(pynvim.__version__)') + file:write('import pynvim; v = pynvim.VERSION; print("{0}.{1}.{2}".format(v.major, v.minor, v.patch))') file:close() -- Run the temporary Python script and capture the output @@ -66,7 +66,7 @@ function M.check() -- Delete the temporary Python script os.remove(temp_file) - if pynvim_version ~= '0.5.0' then + if vim.version.lt(pynvim_version, "0.4.3") then warn('pynvim version ' .. pynvim_version .. ' is not supported') else ok('pynvim version ' .. pynvim_version .. ' is supported') From 6a724677171f87102cf47b755170e46ac23c07b7 Mon Sep 17 00:00:00 2001 From: Erno Hopearuoho Date: Sun, 11 Feb 2024 15:57:08 +0200 Subject: [PATCH 4/4] Fix imports again --- rplugin/python3/CopilotChat/copilot.py | 27 ++++++++++--------- rplugin/python3/CopilotChat/copilot_plugin.py | 6 ++--- .../CopilotChat/handlers/chat_handler.py | 22 +++++++-------- .../handlers/inplace_chat_handler.py | 26 +++++++++--------- .../handlers/vsplit_chat_handler.py | 6 ++--- .../mypynvim/core/autocmdmapper.py | 2 +- .../CopilotChat/mypynvim/core/buffer.py | 2 +- .../CopilotChat/mypynvim/core/keymapper.py | 2 +- .../python3/CopilotChat/mypynvim/core/nvim.py | 8 +++--- .../CopilotChat/mypynvim/core/window.py | 4 +-- .../mypynvim/ui_components/calculator.py | 4 +-- .../mypynvim/ui_components/layout.py | 8 +++--- .../mypynvim/ui_components/popup.py | 12 ++++----- rplugin/python3/CopilotChat/utilities.py | 20 +++++++------- 14 files changed, 75 insertions(+), 74 deletions(-) diff --git a/rplugin/python3/CopilotChat/copilot.py b/rplugin/python3/CopilotChat/copilot.py index 801a099c..72db8894 100644 --- a/rplugin/python3/CopilotChat/copilot.py +++ b/rplugin/python3/CopilotChat/copilot.py @@ -5,10 +5,10 @@ from typing import Dict, List import dotenv -from .prompts import * +import CopilotChat.prompts as prompts import requests -from .typings import * -from .utilities import * +import CopilotChat.typings as typings +import CopilotChat.utilities as utilities from prompt_toolkit import PromptSession from prompt_toolkit.history import InMemoryHistory @@ -20,15 +20,16 @@ "user-agent": "GithubCopilot/1.133.0", } + class Copilot: def __init__(self, token: str = None): if token is None: - token = get_cached_token() + token = utilities.get_cached_token() self.github_token = token self.token: Dict[str, any] = None - self.chat_history: List[Message] = [] + self.chat_history: List[typings.Message] = [] self.vscode_sessionid: str = None - self.machineid = random_hex() + self.machineid = utilities.random_hex() self.session = requests.Session() @@ -67,7 +68,7 @@ def poll_auth(self, device_code: str) -> bool: "accept": "application/json", } response = self.session.get(url, headers=headers).json() - cache_token(response["login"], access_token) + utilities.cache_token(response["login"], access_token) self.github_token = access_token return True return False @@ -103,8 +104,8 @@ def ask( if not system_prompt: system_prompt = prompts.COPILOT_INSTRUCTIONS url = "https://api.githubcopilot.com/chat/completions" - self.chat_history.append(Message(prompt, "user")) - data = generate_request( + self.chat_history.append(typings.Message(prompt, "user")) + data = utilities.generate_request( self.chat_history, code, language, system_prompt=system_prompt, model=model ) @@ -148,17 +149,17 @@ def ask( print("Error:", line) continue - self.chat_history.append(Message(full_response, "system")) + self.chat_history.append(typings.Message(full_response, "system")) - def _get_embeddings(self, inputs: list[FileExtract]): + def _get_embeddings(self, inputs: list[typings.FileExtract]): embeddings = [] url = "https://api.githubcopilot.com/embeddings" # If we have more than 18 files, we need to split them into multiple requests for i in range(0, len(inputs), 18): if i + 18 > len(inputs): - data = generate_embedding_request(inputs[i:]) + data = utilities.generate_embedding_request(inputs[i:]) else: - data = generate_embedding_request(inputs[i : i + 18]) + data = utilities.generate_embedding_request(inputs[i : i + 18]) response = self.session.post(url, headers=self._headers(), json=data).json() if "data" not in response: raise Exception(f"Error fetching embeddings: {response}") diff --git a/rplugin/python3/CopilotChat/copilot_plugin.py b/rplugin/python3/CopilotChat/copilot_plugin.py index cec89d8c..ee2a07a1 100644 --- a/rplugin/python3/CopilotChat/copilot_plugin.py +++ b/rplugin/python3/CopilotChat/copilot_plugin.py @@ -1,7 +1,7 @@ import pynvim -from .handlers.inplace_chat_handler import InPlaceChatHandler -from .handlers.vsplit_chat_handler import VSplitChatHandler -from .mypynvim.core.nvim import MyNvim +from CopilotChat.handlers.inplace_chat_handler import InPlaceChatHandler +from CopilotChat.handlers.vsplit_chat_handler import VSplitChatHandler +from CopilotChat.mypynvim.core.nvim import MyNvim PLUGIN_MAPPING_CMD = "CopilotChatMapping" PLUGIN_AUTOCMD_CMD = "CopilotChatAutocmd" diff --git a/rplugin/python3/CopilotChat/handlers/chat_handler.py b/rplugin/python3/CopilotChat/handlers/chat_handler.py index 049fa782..8de10020 100644 --- a/rplugin/python3/CopilotChat/handlers/chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/chat_handler.py @@ -2,10 +2,10 @@ from datetime import datetime from typing import Optional, cast -from ..prompts import * -from ..copilot import Copilot -from ..mypynvim.core.buffer import MyBuffer -from ..mypynvim.core.nvim import MyNvim +import CopilotChat.prompts as system_prompts +from CopilotChat.copilot import Copilot +from CopilotChat.mypynvim.core.buffer import MyBuffer +from CopilotChat.mypynvim.core.nvim import MyNvim def is_module_installed(name): @@ -66,13 +66,13 @@ def chat( # private def _construct_system_prompt(self, prompt: str): - system_prompt = COPILOT_INSTRUCTIONS - if prompt == FIX_SHORTCUT: - system_prompt = COPILOT_FIX - elif prompt == TEST_SHORTCUT: - system_prompt = COPILOT_TESTS - elif prompt == EXPLAIN_SHORTCUT: - system_prompt = COPILOT_EXPLAIN + system_prompt = system_prompts.COPILOT_INSTRUCTIONS + if prompt == system_prompts.FIX_SHORTCUT: + system_prompt = system_prompts.COPILOT_FIX + elif prompt == system_prompts.TEST_SHORTCUT: + system_prompt = system_prompts.COPILOT_TESTS + elif prompt == system_prompts.EXPLAIN_SHORTCUT: + system_prompt = system_prompts.COPILOT_EXPLAIN return system_prompt def _add_start_separator( diff --git a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py index 96048527..ed938c30 100644 --- a/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/inplace_chat_handler.py @@ -1,9 +1,9 @@ -from ..prompts import * -from .chat_handler import ChatHandler -from ..mypynvim.core.buffer import MyBuffer -from ..mypynvim.core.nvim import MyNvim -from ..mypynvim.ui_components.layout import Box, Layout -from ..mypynvim.ui_components.popup import PopUp +import CopilotChat.prompts as system_prompts +from CopilotChat.handlers.chat_handler import ChatHandler +from CopilotChat.mypynvim.core.buffer import MyBuffer +from CopilotChat.mypynvim.core.nvim import MyNvim +from CopilotChat.mypynvim.ui_components.layout import Box, Layout +from CopilotChat.mypynvim.ui_components.popup import PopUp # Define constants for the models MODEL_GPT4 = "gpt-4" @@ -164,7 +164,7 @@ def _chat(self): self.filetype, self.original_code, self.copilot_popup.window.handle, - system_prompt=prompts.__dict__[self.system_prompt], + system_prompt=system_prompts.__dict__[self.system_prompt], disable_start_separator=True, disable_end_separator=True, model=self.model, @@ -203,7 +203,7 @@ def _toggle_model(self): def _toggle_system_model(self): # Create a list of all system prompts and add the current system prompt - prompts = [ + system_prompts = [ "SENIOR_DEVELOPER_PROMPT", "COPILOT_EXPLAIN", "COPILOT_TESTS", @@ -215,11 +215,11 @@ def _toggle_system_model(self): ] # Get the index of the current system prompt - current_system_prompt_index = prompts.index(self.system_prompt) + current_system_prompt_index = system_prompts.index(self.system_prompt) # Set the next system prompt - self.system_prompt = prompts[ - (current_system_prompt_index + 1) % len(prompts) + self.system_prompt = system_prompts[ + (current_system_prompt_index + 1) % len(system_prompts) ] self.copilot_popup.original_config.title = ( @@ -240,10 +240,10 @@ def _set_keymaps(self): self.prompt_popup.map("n", "", lambda cb=self._toggle_system_model: cb()) self.prompt_popup.map( - "n", "'", lambda: self._set_prompt(PROMPT_SIMPLE_DOCSTRING) + "n", "'", lambda: self._set_prompt(system_prompts.PROMPT_SIMPLE_DOCSTRING) ) self.prompt_popup.map( - "n", "s", lambda: self._set_prompt(PROMPT_SEPARATE) + "n", "s", lambda: self._set_prompt(system_prompts.PROMPT_SEPARATE) ) self.prompt_popup.map( diff --git a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py index 4e4a2ae8..6afc6add 100644 --- a/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py +++ b/rplugin/python3/CopilotChat/handlers/vsplit_chat_handler.py @@ -1,6 +1,6 @@ -from .chat_handler import ChatHandler -from ..mypynvim.core.buffer import MyBuffer -from ..mypynvim.core.nvim import MyNvim +from CopilotChat.handlers.chat_handler import ChatHandler +from CopilotChat.mypynvim.core.buffer import MyBuffer +from CopilotChat.mypynvim.core.nvim import MyNvim class VSplitChatHandler(ChatHandler): diff --git a/rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py b/rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py index 7ba731b8..3bb152fb 100644 --- a/rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py +++ b/rplugin/python3/CopilotChat/mypynvim/core/autocmdmapper.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Callable, Union if TYPE_CHECKING: - from .nvim import MyNvim + from CopilotChat.mypynvim.core.nvim import MyNvim class AutocmdMapper: diff --git a/rplugin/python3/CopilotChat/mypynvim/core/buffer.py b/rplugin/python3/CopilotChat/mypynvim/core/buffer.py index c11db7f7..983709bb 100644 --- a/rplugin/python3/CopilotChat/mypynvim/core/buffer.py +++ b/rplugin/python3/CopilotChat/mypynvim/core/buffer.py @@ -5,7 +5,7 @@ from pynvim.api import Buffer if TYPE_CHECKING: - from .nvim import MyNvim + from Copilotchat.mypynvim.core.nvim import MyNvim class MyBuffer(Buffer): diff --git a/rplugin/python3/CopilotChat/mypynvim/core/keymapper.py b/rplugin/python3/CopilotChat/mypynvim/core/keymapper.py index 4c93ee6c..2a111d4e 100644 --- a/rplugin/python3/CopilotChat/mypynvim/core/keymapper.py +++ b/rplugin/python3/CopilotChat/mypynvim/core/keymapper.py @@ -3,7 +3,7 @@ from typing import TYPE_CHECKING, Callable, Union if TYPE_CHECKING: - from .nvim import MyNvim + from CopilotChat.mypynvim.core.nvim import MyNvim class Keymapper: diff --git a/rplugin/python3/CopilotChat/mypynvim/core/nvim.py b/rplugin/python3/CopilotChat/mypynvim/core/nvim.py index 4eeba15d..8a70fdce 100644 --- a/rplugin/python3/CopilotChat/mypynvim/core/nvim.py +++ b/rplugin/python3/CopilotChat/mypynvim/core/nvim.py @@ -3,10 +3,10 @@ from pynvim import Nvim from pynvim.api.nvim import Current -from .autocmdmapper import AutocmdMapper -from .buffer import MyBuffer -from .keymapper import Keymapper -from .window import MyWindow +from CopilotChat.mypynvim.core.autocmdmapper import AutocmdMapper +from CopilotChat.mypynvim.core.buffer import MyBuffer +from CopilotChat.mypynvim.core.keymapper import Keymapper +from CopilotChat.mypynvim.core.window import MyWindow class MyNvim(Nvim): diff --git a/rplugin/python3/CopilotChat/mypynvim/core/window.py b/rplugin/python3/CopilotChat/mypynvim/core/window.py index 0aadfc4f..f5c23aa1 100644 --- a/rplugin/python3/CopilotChat/mypynvim/core/window.py +++ b/rplugin/python3/CopilotChat/mypynvim/core/window.py @@ -4,10 +4,10 @@ from pynvim.api import Window -from .buffer import MyBuffer +from CopilotChat.mypynvim.core.buffer import MyBuffer if TYPE_CHECKING: - from .nvim import MyNvim + from CopilotChat.mypynvim.core.nvim import MyNvim class MyWindow(Window): diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py index 91626b16..ac6ecaa7 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/calculator.py @@ -3,10 +3,10 @@ from dataclasses import dataclass from typing import TYPE_CHECKING, Literal, Union -from ..core.nvim import MyNvim +from CopilotChat.mypynvim.core.nvim import MyNvim if TYPE_CHECKING: - from .popup import PopUpConfiguration + from CopilotChat.mypynvim.ui_components.popup import PopUpConfiguration @dataclass diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py index bf4f7e5a..13fa1222 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/layout.py @@ -1,11 +1,11 @@ from dataclasses import dataclass from typing import Callable, Literal, Optional, Union, cast -from ..core.nvim import MyNvim +from CopilotChat.mypynvim.core.nvim import MyNvim -from .calculator import Calculator -from .popup import PopUp -from .types import PopUpConfiguration, Relative +from CopilotChat.mypynvim.ui_components.calculator import Calculator +from CopilotChat.mypynvim.ui_components.popup import PopUp +from CopilotChat.mypynvim.ui_components.types import PopUpConfiguration, Relative class Box: diff --git a/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py b/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py index 1a6c1f31..e5a63d9b 100644 --- a/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py +++ b/rplugin/python3/CopilotChat/mypynvim/ui_components/popup.py @@ -5,16 +5,16 @@ from typing import TYPE_CHECKING, Any, Callable, Dict, Optional, Union if TYPE_CHECKING: - from ..core.nvim import MyNvim + from CopilotChat.mypynvim.core.nvim import MyNvim - from .layout import Layout + from CopilotChat.mypynvim.ui_components.layout import Layout -from ..core.buffer import MyBuffer -from ..core.window import MyWindow +from CopilotChat.mypynvim.core.buffer import MyBuffer +from CopilotChat.mypynvim.core.window import MyWindow -from .calculator import Calculator -from .types import PaddingKeys, PopUpConfiguration, Relative +from CopilotChat.mypynvim.ui_components.calculator import Calculator +from CopilotChat.mypynvim.ui_components.types import PaddingKeys, PopUpConfiguration, Relative @dataclass diff --git a/rplugin/python3/CopilotChat/utilities.py b/rplugin/python3/CopilotChat/utilities.py index 8e028559..fbc418e3 100644 --- a/rplugin/python3/CopilotChat/utilities.py +++ b/rplugin/python3/CopilotChat/utilities.py @@ -2,8 +2,8 @@ import os import random -from .prompts import * -from .typings import * +import CopilotChat.prompts as prompts +import CopilotChat.typings as typings def random_hex(length: int = 65): @@ -11,10 +11,10 @@ def random_hex(length: int = 65): def generate_request( - chat_history: list[Message], + chat_history: list[typings.Message], code_excerpt: str, language: str = "", - system_prompt=COPILOT_INSTRUCTIONS, + system_prompt=prompts.COPILOT_INSTRUCTIONS, model="gpt-4", ): messages = [ @@ -49,7 +49,7 @@ def generate_request( } -def generate_embedding_request(inputs: list[FileExtract]): +def generate_embedding_request(inputs: list[typings.FileExtract]): return { "input": [ f"File: `{i.filepath}`\n```{i.filepath.split('.')[-1]}\n{i.code}```" @@ -97,11 +97,11 @@ def get_cached_token(): json.dumps( generate_request( [ - Message("Hello, Copilot!", "user"), - Message("Hello, World!", "system"), - Message("How are you?", "user"), - Message("I am fine, thanks.", "system"), - Message("What does this code do?", "user"), + typings.Message("Hello, Copilot!", "user"), + typings.Message("Hello, World!", "system"), + typings.Message("How are you?", "user"), + typings.Message("I am fine, thanks.", "system"), + typings.Message("What does this code do?", "user"), ], "print('Hello, World!')", "python",