From 08221a0879b4a163eab6860524f255dbcb4743ae Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Mon, 17 Jul 2023 12:05:03 -0700 Subject: match vscode color theme --- continuedev/src/continuedev/libs/util/dedent.py | 25 ------------ continuedev/src/continuedev/libs/util/strings.py | 49 ++++++++++++++++++++++++ continuedev/src/continuedev/steps/chat.py | 8 ++-- continuedev/src/continuedev/steps/core/core.py | 17 ++------ 4 files changed, 55 insertions(+), 44 deletions(-) delete mode 100644 continuedev/src/continuedev/libs/util/dedent.py create mode 100644 continuedev/src/continuedev/libs/util/strings.py (limited to 'continuedev/src') diff --git a/continuedev/src/continuedev/libs/util/dedent.py b/continuedev/src/continuedev/libs/util/dedent.py deleted file mode 100644 index e59c2e97..00000000 --- a/continuedev/src/continuedev/libs/util/dedent.py +++ /dev/null @@ -1,25 +0,0 @@ -from typing import Tuple - - -def dedent_and_get_common_whitespace(s: str) -> Tuple[str, str]: - lines = s.splitlines() - if len(lines) == 0: - return "", "" - - # Longest common whitespace prefix - lcp = lines[0].split(lines[0].strip())[0] - # Iterate through the lines - for i in range(1, len(lines)): - # Empty lines are wildcards - if lines[i].strip() == "": - continue - # Iterate through the leading whitespace characters of the current line - for j in range(0, len(lcp)): - # If it doesn't have the same whitespace as lcp, then update lcp - if j >= len(lines[i]) or lcp[j] != lines[i][j]: - lcp = lcp[:j] - if lcp == "": - return s, "" - break - - return "\n".join(map(lambda x: x.lstrip(lcp), lines)), lcp diff --git a/continuedev/src/continuedev/libs/util/strings.py b/continuedev/src/continuedev/libs/util/strings.py new file mode 100644 index 00000000..f1fb8d0b --- /dev/null +++ b/continuedev/src/continuedev/libs/util/strings.py @@ -0,0 +1,49 @@ +from typing import Tuple + + +def dedent_and_get_common_whitespace(s: str) -> Tuple[str, str]: + lines = s.splitlines() + if len(lines) == 0: + return "", "" + + # Longest common whitespace prefix + lcp = lines[0].split(lines[0].strip())[0] + # Iterate through the lines + for i in range(1, len(lines)): + # Empty lines are wildcards + if lines[i].strip() == "": + continue + # Iterate through the leading whitespace characters of the current line + for j in range(0, len(lcp)): + # If it doesn't have the same whitespace as lcp, then update lcp + if j >= len(lines[i]) or lcp[j] != lines[i][j]: + lcp = lcp[:j] + if lcp == "": + return s, "" + break + + return "\n".join(map(lambda x: x.lstrip(lcp), lines)), lcp + + +def remove_quotes_and_escapes(output: str) -> str: + """ + Clean up the output of the completion API, removing unnecessary escapes and quotes + """ + output = output.strip() + + # Replace smart quotes + output = output.replace("“", '"') + output = output.replace("”", '"') + output = output.replace("‘", "'") + output = output.replace("’", "'") + + # Remove escapes + output = output.replace('\\"', '"') + output = output.replace("\\'", "'") + output = output.replace("\\n", "\n") + output = output.replace("\\t", "\t") + output = output.replace("\\\\", "\\") + if (output.startswith('"') and output.endswith('"')) or (output.startswith("'") and output.endswith("'")): + output = output[1:-1] + + return output diff --git a/continuedev/src/continuedev/steps/chat.py b/continuedev/src/continuedev/steps/chat.py index 3751dec2..7c6b42db 100644 --- a/continuedev/src/continuedev/steps/chat.py +++ b/continuedev/src/continuedev/steps/chat.py @@ -3,6 +3,7 @@ from typing import Any, Coroutine, List from pydantic import Field +from ..libs.util.strings import remove_quotes_and_escapes from .main import EditHighlightedCodeStep from .core.core import MessageStep from ..core.main import FunctionCall, Models @@ -43,11 +44,8 @@ class SimpleChatStep(Step): finally: await generator.aclose() - self.name = (await sdk.models.gpt35.complete( - f"Write a short title for the following chat message: {self.description}")).strip() - - if self.name.startswith('"') and self.name.endswith('"'): - self.name = self.name[1:-1] + self.name = remove_quotes_and_escapes(await sdk.models.gpt35.complete( + f"Write a short title for the following chat message: {self.description}")) self.chat_context.append(ChatMessage( role="assistant", diff --git a/continuedev/src/continuedev/steps/core/core.py b/continuedev/src/continuedev/steps/core/core.py index d5a7cd9a..5b9b9fd5 100644 --- a/continuedev/src/continuedev/steps/core/core.py +++ b/continuedev/src/continuedev/steps/core/core.py @@ -12,7 +12,7 @@ from ...models.filesystem import FileSystem, RangeInFile, RangeInFileWithContent from ...core.observation import Observation, TextObservation, TracebackObservation, UserInputObservation from ...core.main import ChatMessage, ContinueCustomException, Step, SequentialStep from ...libs.util.count_tokens import MAX_TOKENS_FOR_MODEL, DEFAULT_MAX_TOKENS -from ...libs.util.dedent import dedent_and_get_common_whitespace +from ...libs.util.strings import dedent_and_get_common_whitespace, remove_quotes_and_escapes import difflib @@ -157,17 +157,6 @@ class DefaultModelEditCodeStep(Step): _new_contents: str = "" _prompt_and_completion: str = "" - def _cleanup_output(self, output: str) -> str: - output = output.replace('\\"', '"') - output = output.replace("\\'", "'") - output = output.replace("\\n", "\n") - output = output.replace("\\t", "\t") - output = output.replace("\\\\", "\\") - if output.startswith('"') and output.endswith('"'): - output = output[1:-1] - - return output - async def describe(self, models: Models) -> Coroutine[str, None, None]: if self._previous_contents.strip() == self._new_contents.strip(): description = "No edits were made" @@ -183,9 +172,9 @@ class DefaultModelEditCodeStep(Step): Please give brief a description of the changes made above using markdown bullet points. Be concise:""")) name = await models.gpt3516k.complete(f"Write a very short title to describe this requested change (no quotes): '{self.user_input}'. This is the title:") - self.name = self._cleanup_output(name) + self.name = remove_quotes_and_escapes(name) - return f"{self._cleanup_output(description)}" + return f"{remove_quotes_and_escapes(description)}" async def get_prompt_parts(self, rif: RangeInFileWithContents, sdk: ContinueSDK, full_file_contents: str): # We don't know here all of the functions being passed in. -- cgit v1.2.3-70-g09d2