Finished code cleanup, readme is mostly done.

This commit is contained in:
2025-04-20 20:22:57 -04:00
parent 9dcd31dd04
commit 1621023958
6 changed files with 171 additions and 25 deletions

View File

@@ -21,6 +21,7 @@ import (
"strings"
)
// Command defines the autopatch command along with command line flags.
func Command() *cli.Command {
return &cli.Command{
Name: "auto-patch",
@@ -34,7 +35,7 @@ func Command() *cli.Command {
},
&cli.StringFlag{
Name: "task",
Usage: "task to perform, e.g. \"add a test for a function\"",
Usage: "task to perform, e.g. \"add a http route for a new health check endpoint\"",
Required: true,
},
&cli.BoolFlag{
@@ -45,11 +46,13 @@ func Command() *cli.Command {
}
}
// autoPatch is a struct implementing the auto patcher.
type autoPatch struct {
llm *llm.LLM
execute bool
}
// run gets executed when the command is run.
func (a *autoPatch) run(ctx context.Context, cmd *cli.Command) error {
llmRef := llm.FromContext(ctx)
a.llm = llmRef
@@ -70,6 +73,8 @@ func (a *autoPatch) run(ctx context.Context, cmd *cli.Command) error {
return nil
}
// generateGitCommit will generate the code patch, the unit test, and will stage and commit the changes with an
// appropriate commit message.
func (a *autoPatch) generateGitCommit(ctx context.Context, repoPath, prompt string) error {
var affectedFiles []string
@@ -97,6 +102,7 @@ func (a *autoPatch) generateGitCommit(ctx context.Context, repoPath, prompt stri
return nil
}
// commit stages the changed files and commits them with a commit message.
func (a *autoPatch) commit(ctx context.Context, prompt, repoPath string, files ...string) error {
gitPath := osfs.New(filepath.Join(repoPath, ".git"))
@@ -146,6 +152,7 @@ func (a *autoPatch) commit(ctx context.Context, prompt, repoPath string, files .
return nil
}
// generateCodePatch generates an appropriate code patch given a repository and prompt.
func (a *autoPatch) generateCodePatch(ctx context.Context, repoPath, prompt string) (string, string, error) {
db := database.FromContext(ctx)
cfg := config.FromContext(ctx)
@@ -209,6 +216,8 @@ func (a *autoPatch) generateCodePatch(ctx context.Context, repoPath, prompt stri
return fileName, codeBlock, err
}
// generateUnitTest passes the new code and a prompt to the LLM to generate an appropriate unit test. It will add the
// new test to the bottom of an existing test file, or generate a new one if no unit test file already exists.
func (a *autoPatch) generateUnitTest(ctx context.Context, prompt, fileName, newCode string) (string, error) {
// Check to see if a test file for this already exists.
testFileExists := false
@@ -263,6 +272,8 @@ func (a *autoPatch) generateUnitTest(ctx context.Context, prompt, fileName, newC
return testFile, nil
}
// generateCode takes a prompt and tries to extract code from it. The prompt should try to get the LLM to structure the
// code as a single yaml chunk.
func (a *autoPatch) generateCode(ctx context.Context, prompt string) (string, error) {
rsp, err := a.llm.CodePrompt(ctx, prompt)
if err != nil {
@@ -284,6 +295,7 @@ func (a *autoPatch) generateCode(ctx context.Context, prompt string) (string, er
return codeBlock, nil
}
// patchFile attempts to write the merged diff to the original file so it can be staged and committed.
func patchFile(fileName string, diffs []diffmatchpatch.Diff) error {
var buff bytes.Buffer
for _, diff := range diffs {