Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
working on diffs, a couple minor fixes
Dane Schneider498fc3ad145f
diff --git a/cli/cmd/diffs.go b/cli/cmd/diffs.goindex 71e3f813..5d2cafa4 100644--- a/cli/cmd/diffs.go+++ b/cli/cmd/diffs.go@@ -1,9 +1,10 @@package cmdimport (- "bytes""fmt"+ "os""os/exec"+ "path/filepath""plandex/lib""github.com/spf13/cobra"@@ -16,7 +17,7 @@ func init() {var diffsCmd = &cobra.Command{Use: "diffs",Aliases: []string{"d"},- Short: "Show differences between plan and project files",+ Short: "Show differences for each individual file between plan files and project files",Run: diffs,}@@ -27,25 +28,108 @@ func diffs(cmd *cobra.Command, args []string) {}}-// Show differences in 'git diff' style between plan files and project files.func showDiffs() error {- // Check git installed+ // Check if git is installedif !lib.IsCommandAvailable("git") {return fmt.Errorf("git is required to generate diffs")}- // Run git diff command- cmd := exec.Command("git", "diff", lib.PlanFilesDir, lib.ProjectRoot)- var out bytes.Buffer- cmd.Stdout = &out- err := cmd.Run()+ // Save current branch name+ cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")+ currentBranch, err := cmd.Output()+ if err != nil {+ return fmt.Errorf("error getting current branch: %v", err)+ }++ // Create and checkout a temporary branch+ cmd = exec.Command("git", "checkout", "-b", "temp_diff_branch")+ err = cmd.Run()+ if err != nil {+ return fmt.Errorf("error creating temp branch: %v", err)+ }++ defer func() {+ // Switch back to the original branch and delete the temporary branch+ cmd = exec.Command("git", "checkout", string(currentBranch))+ err = cmd.Run()+ if err != nil {+ fmt.Println(fmt.Errorf("error switching back to original branch: %v", err))+ }++ cmd = exec.Command("git", "branch", "-D", "temp_diff_branch")+ err = cmd.Run()+ if err != nil {+ fmt.Println(fmt.Errorf("error deleting temp branch: %v", err))+ }++ }()++ // Commit any outstanding changes+ cmd = exec.Command("git", "add", ".")+ err = cmd.Run()+ if err != nil {+ return fmt.Errorf("error adding changes: %v", err)+ }+ cmd = exec.Command("git", "commit", "-m", "temp commit")+ err = cmd.Run()+ if err != nil {+ return fmt.Errorf("error committing changes: %v", err)+ }++ // Overwrite project files with plan files+ err = copyDirectory(lib.ProjectRoot, lib.PlanFilesDir)++ if err != nil {+ return fmt.Errorf("error copying files: %v", err)+ }++ // Add changes to the staging area+ cmd = exec.Command("git", "add", ".")+ err = cmd.Run()+ if err != nil {+ return fmt.Errorf("error adding changes: %v", err)+ }++ // Show the diff+ cmd = exec.Command("git", "diff", "--cached")+ cmd.Stdin = os.Stdin+ cmd.Stdout = os.Stdout+ cmd.Stderr = os.Stderr+ err = cmd.Run()if err != nil {return fmt.Errorf("error generating diff: %v", err)}- // Print output- fmt.Println(out.String())+ return nil+}++func copyDirectory(srcDir, dstDir string) error {+ entries, err := os.ReadDir(srcDir)+ if err != nil {+ return err+ }++ for _, entry := range entries {+ srcPath := filepath.Join(srcDir, entry.Name())+ dstPath := filepath.Join(dstDir, entry.Name())++ if entry.IsDir() {+ err = copyDirectory(srcPath, dstPath)+ if err != nil {+ return err+ }+ } else {+ data, err := os.ReadFile(srcPath)+ if err != nil {+ return err+ }+ err = os.WriteFile(dstPath, data, 0644)+ if err != nil {+ return err+ }+ }+ }return nil-}\ No newline at end of file+}diff --git a/server/model/proposal/confirm.go b/server/model/proposal/confirm.goindex e932b25b..b2c1c491 100644--- a/server/model/proposal/confirm.go+++ b/server/model/proposal/confirm.go@@ -15,18 +15,16 @@ import ("github.com/sashabaranov/go-openai/jsonschema")-const systemPrompt = `+const replacementSystemPrompt = `[YOUR INSTRUCTIONS]You apply changes from a plan to a given code file. You can either us the 'writeEntireFile' function to write the entire file, or the 'writeReplacements' function to write a list of replacements to apply to the file. Decide which is a more efficient way to apply the changes, and call the appropriate function.-If the current state of the file within the plan is included, apply your changes to the current state of the file.--A. If you are using the 'writeEntireFile' function, call it with the full content of the file as raw text, including any updates from the previous response. Ouput the entire updated file.+A. If you are using the 'writeEntireFile' function, call it with the full content of the file as raw text, including any updates from the previous response. Call 'writeEntireFile' with the entire updated file. Don't include any placeholders or references to the original file.B. If you are using the 'writeReplacements' function, call it with a list of replacements to apply to the file. Each replacement is an object with two properties: 'old' and 'new'. 'old' is the old text to replace, and 'new' is the new text to replace it with. You can include as many replacements as you want. You must include at least one replacement.- The 'new' text must include the full text of the replacement without any placeholders or references to the original file.-- The 'old' text must be a substring of the current file.+- The 'old' text *must be a substring* of the current state of the file.- The 'old' text must not overlap with any other 'old' text in the list of replacements.Replacement examples below. Note: >>> and <<< indicate the start and end of an example response.@@ -154,6 +152,13 @@ writeReplacements({[END INSTRUCTIONS]`+const writeFileOnlySystemPrompt = `+[YOUR INSTRUCTIONS]++You apply changes from a plan to a given code file. Use 'writeEntireFile' function to write the full content of the file as raw text, including any updates from the previous response, to the file. Call 'writeEntireFile' with the entire updated file. Don't include any placeholders or references to the original file.++[END INSTRUCTIONS]`+func confirmProposal(proposalId string, onStream types.OnStreamFunc) error {goEnv := os.Getenv("GOENV")if goEnv == "test" {@@ -196,6 +201,8 @@ func confirmProposal(proposalId string, onStream types.OnStreamFunc) error {go func(filePath string) {fmt.Println("Getting file from model: " + filePath)+ _, fileInProject := proposal.Request.ProjectPaths[filePath]+// get relevant file context (if any)var fileContext *shared.ModelContextPartfor _, part := range proposal.Request.ModelContext {@@ -217,31 +224,49 @@ func confirmProposal(proposalId string, onStream types.OnStreamFunc) error {fileMessages := []openai.ChatCompletionMessage{}if fileContext != nil || currentState != "" {- fileMessages = append(fileMessages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleSystem,- Content: systemPrompt + "\n\n" + fmt.Sprintf(fmtStr, fmtArgs...),- })+ if fileInProject {+ fileMessages = append(fileMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: replacementSystemPrompt + "\n\n" + fmt.Sprintf(fmtStr, fmtArgs...),+ })+ } else {+ fileMessages = append(fileMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: writeFileOnlySystemPrompt + "\n\n" + fmt.Sprintf(fmtStr, fmtArgs...),+ })+ }}fileMessages = append(fileMessages, openai.ChatCompletionMessage{Role: openai.ChatMessageRoleAssistant,Content: proposal.Content,- },- openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(`+ })++ if fileInProject {+ fileMessages = append(fileMessages,+ openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: fmt.Sprintf(`Based on your instructions, apply the changes from the plan to %s. You must call either the 'writeReplacements' function or the 'writeEntireFile' function, depending on which is a more efficient way to apply the changes. Don't call any other function.`, filePath),- })+ })+ } else {+ fileMessages = append(fileMessages,+ openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: fmt.Sprintf(`+ Based on your instructions, apply the changes from the plan to %s. You must call the 'writeEntireFile' function. Don't call any other function.+ `, filePath),+ })+ }fmt.Println("Calling model for file: " + filePath)for _, msg := range fileMessages {fmt.Printf("%s: %s\n", msg.Role, msg.Content)}- modelReq := openai.ChatCompletionRequest{- Model: openai.GPT4,- Functions: []openai.FunctionDefinition{{+ functions := []openai.FunctionDefinition{+ {Name: "writeEntireFile",Parameters: &jsonschema.Definition{Type: jsonschema.Object,@@ -253,7 +278,11 @@ func confirmProposal(proposalId string, onStream types.OnStreamFunc) error {},Required: []string{"content"},},- }, {+ },+ }++ if fileInProject {+ functions = append(functions, openai.FunctionDefinition{Name: "writeReplacements",Parameters: &jsonschema.Definition{Type: jsonschema.Object,@@ -279,8 +308,13 @@ func confirmProposal(proposalId string, onStream types.OnStreamFunc) error {},Required: []string{"replacements"},},- }},- Messages: fileMessages,+ })+ }++ modelReq := openai.ChatCompletionRequest{+ Model: openai.GPT4,+ Functions: functions,+ Messages: fileMessages,}stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)diff --git a/shared/types.go b/shared/types.goindex 1dc3e1b7..3ec8ad99 100644--- a/shared/types.go+++ b/shared/types.go@@ -20,6 +20,7 @@ type PromptRequest struct {Prompt string `json:"prompt"`ModelContext ModelContext `json:"modelContext"`CurrentPlan CurrentPlanFiles `json:"currentPlan"`+ ProjectPaths map[string]bool `json:"projectPaths"`Conversation []ConversationMessage `json:"conversation"`ParentProposalId string `json:"parentProposalId"`}