/ qwencoder-eval / instruct / aider / aider / coders / single_wholefile_func_coder.py
single_wholefile_func_coder.py
  1  from aider import diffs
  2  
  3  from ..dump import dump  # noqa: F401
  4  from .base_coder import Coder
  5  from .single_wholefile_func_prompts import SingleWholeFileFunctionPrompts
  6  
  7  
  8  class SingleWholeFileFunctionCoder(Coder):
  9      edit_format = "func"
 10  
 11      functions = [
 12          dict(
 13              name="write_file",
 14              description="write new content into the file",
 15              # strict=True,
 16              parameters=dict(
 17                  type="object",
 18                  properties=dict(
 19                      explanation=dict(
 20                          type="string",
 21                          description=(
 22                              "Step by step plan for the changes to be made to the code (future"
 23                              " tense, markdown format)"
 24                          ),
 25                      ),
 26                      content=dict(
 27                          type="string",
 28                          description="Content to write to the file",
 29                      ),
 30                  ),
 31                  required=["explanation", "content"],
 32                  additionalProperties=False,
 33              ),
 34          ),
 35      ]
 36  
 37      def __init__(self, *args, **kwargs):
 38          self.gpt_prompts = SingleWholeFileFunctionPrompts()
 39          super().__init__(*args, **kwargs)
 40  
 41      def update_cur_messages(self, edited):
 42          if edited:
 43              self.cur_messages += [
 44                  dict(role="assistant", content=self.gpt_prompts.redacted_edit_message)
 45              ]
 46          else:
 47              self.cur_messages += [dict(role="assistant", content=self.partial_response_content)]
 48  
 49      def render_incremental_response(self, final=False):
 50          res = ""
 51          if self.partial_response_content:
 52              res += self.partial_response_content
 53  
 54          args = self.parse_partial_args()
 55  
 56          if not args:
 57              return ""
 58  
 59          for k, v in args.items():
 60              res += "\n"
 61              res += f"{k}:\n"
 62              res += v
 63  
 64          return res
 65  
 66      def live_diffs(self, fname, content, final):
 67          lines = content.splitlines(keepends=True)
 68  
 69          # ending an existing block
 70          full_path = self.abs_root_path(fname)
 71  
 72          content = self.io.read_text(full_path)
 73          if content is None:
 74              orig_lines = []
 75          else:
 76              orig_lines = content.splitlines()
 77  
 78          show_diff = diffs.diff_partial_update(
 79              orig_lines,
 80              lines,
 81              final,
 82              fname=fname,
 83          ).splitlines()
 84  
 85          return "\n".join(show_diff)
 86  
 87      def get_edits(self):
 88          chat_files = self.get_inchat_relative_files()
 89          assert len(chat_files) == 1, chat_files
 90  
 91          args = self.parse_partial_args()
 92          if not args:
 93              return []
 94  
 95          res = chat_files[0], args["content"]
 96          dump(res)
 97          return [res]
 98  
 99      def apply_edits(self, edits):
100          for path, content in edits:
101              full_path = self.abs_root_path(path)
102              self.io.write_text(full_path, content)