From 37ae9a5aabacd8dc2c61235d8ba80fdf1f67acc5 Mon Sep 17 00:00:00 2001 From: gptlang <121417512+gptlang@users.noreply.github.com> Date: Thu, 8 Feb 2024 09:57:31 +0000 Subject: [PATCH] fix merge conflicts --- rplugin/python3/copilot.py | 7 ++++--- rplugin/python3/handlers/chat_handler.py | 18 +++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/rplugin/python3/copilot.py b/rplugin/python3/copilot.py index ee471b64..4a86ae26 100644 --- a/rplugin/python3/copilot.py +++ b/rplugin/python3/copilot.py @@ -127,10 +127,11 @@ def ask( ) ) for line in response.iter_lines(): - line = line.decode("utf-8").replace("data: ", "").strip() - if line.startswith("[DONE]"): + line: bytes = line + line = line.replace(b"data: ", b"") + if line.startswith(b"[DONE]"): break - elif line == "": + elif line == b"": continue try: line = json.loads(line) diff --git a/rplugin/python3/handlers/chat_handler.py b/rplugin/python3/handlers/chat_handler.py index e410d8ce..36b88570 100644 --- a/rplugin/python3/handlers/chat_handler.py +++ b/rplugin/python3/handlers/chat_handler.py @@ -203,7 +203,8 @@ def _add_chat_messages( if self.copilot.github_token is None: req = self.copilot.request_auth() self.nvim.out_write( - f"Please visit {req['verification_uri']} and enter the code {req['user_code']}\n" + f"Please visit {req['verification_uri'] + } and enter the code {req['user_code']}\n" ) current_time = time.time() wait_until = current_time + req["expires_in"] @@ -215,10 +216,21 @@ def _add_chat_messages( return self.nvim.out_write("Successfully authenticated with Copilot\n") self.copilot.authenticate() - + buffer = "" for token in self.copilot.ask( system_prompt, prompt, code, language=cast(str, file_type), model=model ): + buffer += token + # Look for the last newline in buffer + last_newline = buffer.rfind("\n") + # If there is no newline, then continue + if last_newline == -1: + continue + # If there is a newline, take everything in front of it and set that as the token + token = buffer[: last_newline + 1] + + # Remove the token and newline from the buffer + buffer = buffer.replace(token, "") self.nvim.exec_lua( 'require("CopilotChat.utils").log_info(...)', f"Token: {token}" ) @@ -232,7 +244,7 @@ def _add_chat_messages( last_line_col, last_line_row, last_line_col, - token.split("\n"), + [tok.encode('utf-8') for tok in token.split("\n")], ) def _add_end_separator(self, model: str, disable_separators: bool = False):