Skip to content

Open source AI coding agent. Designed for large projects and real world tasks.

changes tui apply, drop changes logic, don't error on failed replacement

Dane Schneiderbc0bc592e097
diff --git a/cli/changes_tui/actions.go b/cli/changes_tui/actions.go
index 3e432677..d55b90a9 100644
--- a/cli/changes_tui/actions.go
+++ b/cli/changes_tui/actions.go
@@ -2,6 +2,8 @@ package changes_tui
import (
"fmt"
+ "log"
+ "plandex/lib"
"strings"
"github.com/atotto/clipboard"
@@ -10,19 +12,19 @@ import (
"github.com/plandex/plandex/shared"
)
-func (m changesUIModel) rejectChange() error {
-
- return nil
-}
+func (m *changesUIModel) rejectChange() {
+ if m.selectionInfo == nil || m.selectionInfo.currentRep == nil {
+ log.Println("can't drop change; no change is currently selected")
+ return
+ }
-func (m changesUIModel) applyAllChanges() error {
+ err := lib.DropChange(lib.CurrentPlanName, m.selectionInfo.currentPath, m.selectionInfo.currentRep)
- return nil
-}
-
-func (m changesUIModel) rejectAllChanges() error {
+ if err != nil {
+ log.Printf("error dropping change: %v", err)
+ return
+ }
- return nil
}
func (m changesUIModel) copyCurrentChange() error {
diff --git a/cli/changes_tui/model.go b/cli/changes_tui/model.go
index b6d85221..f9883ae4 100644
--- a/cli/changes_tui/model.go
+++ b/cli/changes_tui/model.go
@@ -26,6 +26,8 @@ type changesUIModel struct {
ready bool
width int
height int
+ shouldApplyAll bool
+ shouldRejectAll bool
}
type keymap = struct {
diff --git a/cli/changes_tui/run.go b/cli/changes_tui/run.go
new file mode 100644
index 00000000..13463852
--- /dev/null
+++ b/cli/changes_tui/run.go
@@ -0,0 +1,40 @@
+package changes_tui
+
+import (
+ "fmt"
+ "plandex/lib"
+
+ tea "github.com/charmbracelet/bubbletea"
+)
+
+func StartChangesUI() error {
+ initial := initialModel()
+
+ if len(initial.currentPlan.SortedPaths) == 0 {
+ fmt.Println("πŸ€·β€β™‚οΈ No changes pending")
+ return nil
+ }
+
+ m, err := tea.NewProgram(initial, tea.WithAltScreen()).Run()
+
+ if err != nil {
+ return fmt.Errorf("error running changes UI: %v", err)
+ }
+
+ if m.(changesUIModel).shouldApplyAll {
+ err := lib.ApplyPlanWithOutput(lib.CurrentPlanName, false)
+
+ if err != nil {
+ return fmt.Errorf("error applying plan: %v", err)
+ }
+ } else if m.(changesUIModel).shouldRejectAll {
+ err := lib.DropChangesWithOutput(lib.CurrentPlanName)
+
+ if err != nil {
+ return fmt.Errorf("error rejecting plan: %v", err)
+ }
+
+ }
+
+ return nil
+}
diff --git a/cli/changes_tui/start.go b/cli/changes_tui/start.go
deleted file mode 100644
index 4e26bef4..00000000
--- a/cli/changes_tui/start.go
+++ /dev/null
@@ -1,27 +0,0 @@
-package changes_tui
-
-import (
- "fmt"
-
- tea "github.com/charmbracelet/bubbletea"
-)
-
-func StartChangesUI() error {
- initial := initialModel()
-
- if len(initial.currentPlan.SortedPaths) == 0 {
- fmt.Println("πŸ€·β€β™‚οΈ No changes to apply")
- return nil
- }
-
- _, err := tea.NewProgram(initial, tea.WithAltScreen()).Run()
-
- if err != nil {
- return fmt.Errorf("error running changes UI: %v", err)
- }
-
- // fmt.Println("exited changes UI")
- // spew.Dump(m)
-
- return nil
-}
diff --git a/cli/changes_tui/update.go b/cli/changes_tui/update.go
index 06c15e82..556fb38e 100644
--- a/cli/changes_tui/update.go
+++ b/cli/changes_tui/update.go
@@ -54,10 +54,12 @@ func (m changesUIModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.rejectChange()
case bubbleKey.Matches(msg, m.keymap.applyAll):
- m.applyAllChanges()
+ m.shouldApplyAll = true
+ return m, tea.Quit
case bubbleKey.Matches(msg, m.keymap.rejectAll):
- m.rejectAllChanges()
+ m.shouldRejectAll = true
+ return m, tea.Quit
case bubbleKey.Matches(msg, m.keymap.copy):
m.copyCurrentChange()
diff --git a/cli/cmd/tell.go b/cli/cmd/tell.go
index 37755157..f7c6069c 100644
--- a/cli/cmd/tell.go
+++ b/cli/cmd/tell.go
@@ -131,26 +131,4 @@ func getEditorInstructions(editor string) string {
return "Write your prompt below, then save and exit to send it to Plandex.\n\n"
- // var instructions string
-
- // switch editor {
- // case "vim":
- // instructions = "Write your prompt below, then save and exit to send it to Plandex.\n\n" +
- // "Vim Instructions:\n" +
- // "Use the arrow keys or 'hjkl' to navigate around the text.\n" +
- // "Press 'i' to enter 'Insert' mode and start typing your prompt.\n" +
- // "To exit 'Insert' mode and go back to 'Normal' mode, press 'Esc'.\n" +
- // "In 'Normal' mode, type ':wq' followed by 'Enter' key to save, exit, and send your prompt.\n" +
- // "If you want to exit without saving, type ':q!' followed by 'Enter'."
-
- // case "nano":
- // instructions = "Write your prompt below, then save and exit to send it to Plandex.\n\n" +
- // "Nano Instructions:\n" +
- // "Use the arrow keys to navigate around the text.\n" +
- // "Type your prompt as needed.\n" +
- // "When you're ready, press 'Ctrl + O' to save the file, then 'Enter' to confirm.\n" +
- // "Finally, press 'Ctrl + X' to exit Nano and send your prompt."
- // }
-
- // return instructions
}
diff --git a/cli/lib/current_plan.go b/cli/lib/current_plan.go
index 980db88f..c8475b46 100644
--- a/cli/lib/current_plan.go
+++ b/cli/lib/current_plan.go
@@ -43,6 +43,7 @@ func GetCurrentPlanStateBeforeReplacement(id string) (*types.CurrentPlanState, e
errCh <- fmt.Errorf("error getting plan results: %v", err)
return
}
+
planResInfoCh <- planResInfo
}()
@@ -66,8 +67,6 @@ func GetCurrentPlanStateBeforeReplacement(id string) (*types.CurrentPlanState, e
continue
}
- // fmt.Printf("contextPart: %s\n", contextPart.FilePath)
-
_, hasPath := planResInfo.PlanResByPath[contextPart.FilePath]
// fmt.Printf("hasPath: %v\n", hasPath)
@@ -191,7 +190,11 @@ func GetPlanResultsInfo() (*types.PlanResultsInfo, error) {
resDirExists := !os.IsNotExist(err)
if !resDirExists {
- return nil, nil
+ return &types.PlanResultsInfo{
+ PlanResByPath: resByPath,
+ SortedPaths: paths,
+ ReplacementsByPath: replacementsByPath,
+ }, nil
}
err = filepath.Walk(ResultsSubdir, func(path string, info os.FileInfo, err error) error {
diff --git a/cli/lib/drop_changes.go b/cli/lib/drop_changes.go
index 1236f9e9..a08f7cb3 100644
--- a/cli/lib/drop_changes.go
+++ b/cli/lib/drop_changes.go
@@ -1,24 +1,22 @@
package lib
import (
- "plandex/shared"
- "time"
-)
+ "encoding/json"
+ "fmt"
+ "os"
+ "path/filepath"
-func rejectReplacement(replacement *shared.Replacement) {
- currentTime := time.Now().Format(time.RFC3339)
- replacement.RejectedAt = currentTime
-}
+ "github.com/plandex/plandex/shared"
+)
-func DropChanges(name string) error {
+func DropChangesWithOutput(name string) error {
plandexDir, _, err := FindOrCreatePlandex()
if err != nil {
return fmt.Errorf("error finding or creating plandex dir: %w", err)
}
if name == "" {
- fmt.Println("\uD83E\uDD37\u200D\u2642\uFE0F No plan specified and no current plan")
- return nil
+ return fmt.Errorf("no plan specified and no current plan")
}
rootDir := filepath.Join(plandexDir, name)
@@ -26,8 +24,7 @@ func DropChanges(name string) error {
_, err = os.Stat(rootDir)
if os.IsNotExist(err) {
- fmt.Printf("\uD83E\uDD37\u200D\u2642\uFE0F Plan with name '%s' doesn't exist\n", name)
- return nil
+ return fmt.Errorf("plan with name '%s' doesn't exist", name)
} else if err != nil {
return fmt.Errorf("error checking if plan exists: %w", err)
}
@@ -39,27 +36,57 @@ func DropChanges(name string) error {
}
planResByPath := res.PlanResByPath
+ ts := shared.StringTs()
+ numRejected := planResByPath.SetRejected(ts)
+ errCh := make(chan error, numRejected)
for _, planResults := range planResByPath {
for _, planResult := range planResults {
- for _, replacement := range planResult.Replacements {
- rejectReplacement(replacement)
+ if planResult.RejectedAt != ts {
+ continue
}
+
+ go func(planResult *shared.PlanResult) {
+ bytes, err := json.Marshal(planResult)
+ if err != nil {
+ errCh <- fmt.Errorf("error marshalling plan result: %v", err)
+ return
+ }
+
+ path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")
+
+ err = os.WriteFile(path, bytes, 0644)
+ if err != nil {
+ errCh <- fmt.Errorf("error writing plan result: %v", err)
+ return
+ }
+
+ errCh <- nil
+
+ }(planResult)
+ }
+ }
+
+ for i := 0; i < numRejected; i++ {
+ err := <-errCh
+ if err != nil {
+ return fmt.Errorf("error wrting plan result file after SetApplied: %v", err)
}
}
+ fmt.Println("πŸ—‘οΈ Pending changes dropped")
+
return nil
}
-func DropChange(name string, changeId string) error {
+func DropChange(name, path string, replacement *shared.Replacement) error {
plandexDir, _, err := FindOrCreatePlandex()
if err != nil {
return fmt.Errorf("error finding or creating plandex dir: %w", err)
}
if name == "" {
- fmt.Println("πŸ€·β€β™‚οΈ No plan specified and no current plan")
- return nil
+ return fmt.Errorf("no plan specified and no current plan")
}
rootDir := filepath.Join(plandexDir, name)
@@ -67,8 +94,7 @@ func DropChange(name string, changeId string) error {
_, err = os.Stat(rootDir)
if os.IsNotExist(err) {
- fmt.Printf("πŸ€·β€β™‚οΈ Plan with name '%s' doesn't exist\n", name)
- return nil
+ return fmt.Errorf("plan with name '%s' doesn't exist", name)
} else if err != nil {
return fmt.Errorf("error checking if plan exists: %w", err)
}
@@ -80,18 +106,30 @@ func DropChange(name string, changeId string) error {
}
planResByPath := res.PlanResByPath
-
- for _, planResults := range planResByPath {
- for _, planResult := range planResults {
- for _, replacement := range planResult.Replacements {
- if replacement.Id == changeId {
- rejectReplacement(replacement)
- return nil
- }
+ planResults := planResByPath[path]
+ ts := shared.StringTs()
+
+ for _, planResult := range planResults {
+ for _, resultReplacement := range planResult.Replacements {
+ if resultReplacement.Id == replacement.Id {
+ replacement.SetRejected(ts)
+ break
}
}
+
+ bytes, err := json.Marshal(planResult)
+ if err != nil {
+ return fmt.Errorf("error marshalling plan result: %v", err)
+ }
+
+ path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")
+
+ err = os.WriteFile(path, bytes, 0644)
+ if err != nil {
+ return fmt.Errorf("error writing plan result: %v", err)
+ }
}
- fmt.Printf("πŸ€·β€β™‚οΈ Change with ID '%s' doesn't exist in plan '%s'\n", changeId, name)
- return nil
+ return fmt.Errorf("change with ID '%s' doesn't exist in plan '%s' at path '%s'", replacement.Id, name, path)
+
}
diff --git a/cli/lib/proposal.go b/cli/lib/proposal.go
index 5cbcb355..14632909 100644
--- a/cli/lib/proposal.go
+++ b/cli/lib/proposal.go
@@ -312,6 +312,8 @@ func Propose(prompt string) error {
close(done)
closedDone = true
writeFileProgress()
+ fmt.Println()
+ fmt.Println()
}
case shared.STATE_DESCRIBING:
@@ -424,6 +426,8 @@ func Propose(prompt string) error {
close(done)
closedDone = true
writeFileProgress()
+ fmt.Println()
+ fmt.Println()
}
}
}
diff --git a/server/model/openai_models.go b/server/model/openai_models.go
index 116d4fd3..ea9377ce 100644
--- a/server/model/openai_models.go
+++ b/server/model/openai_models.go
@@ -6,7 +6,7 @@ import (
"github.com/sashabaranov/go-openai"
)
-const strongModel = openai.GPT4
+const strongModel = openai.GPT4 // gpt-4-turbo-preview has huge context and is faster, but seems much weaker than gpt-4
const mediumModel = openai.GPT4TurboPreview
const weakModel = openai.GPT3Dot5Turbo1106
diff --git a/server/model/prompts/build.go b/server/model/prompts/build.go
index cde4c24f..d42fb1cb 100644
--- a/server/model/prompts/build.go
+++ b/server/model/prompts/build.go
@@ -31,6 +31,8 @@ The 'old' text should be unique and unambiguous in the current file. It must not
The 'new' text must include the full text of the replacement without any placeholders or references to the original file. DO NOT INCLUDE text like "// ... existing code", "// ... rest of the file", or other equivalents with different wording/formatting/syntax in the 'new' text. Only include the actual code that should be inserted.
+For the 'new' text, apart from the exceptions I mentioned in the previous paragraph, do not make any additional modifications on top of the changes from the plan unless absolutely necessary to apply the change correctly. Don't add additional comments. Don't escape characters (like emoji or other characters) unless they are also escaped in the plan. Keep as closely as possible to the changes as suggested in the plan, down to specific characters and formatting.
+
You MUST call only 'writeReplacements'--don't call any other function or produce any other output.
Replacement examples below. Note: >>> and <<< indicate the start and end of an example response.
diff --git a/server/model/proposal/build.go b/server/model/proposal/build.go
index 01655a0e..da7feb6d 100644
--- a/server/model/proposal/build.go
+++ b/server/model/proposal/build.go
@@ -422,14 +422,15 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {
}
}
- handleErrorRetry(
- nil, // no error -- if we reach MAX_REPLACEMENT_RETRIES, we just ignore the error and continue
- false,
- true,
- planResult,
- )
-
- return
+ if numReplacementsRetry < MaxReplacementRetries {
+ handleErrorRetry(
+ nil, // no error -- if we reach MAX_REPLACEMENT_RETRIES, we just ignore the error and continue
+ false,
+ true,
+ planResult,
+ )
+ return
+ }
}
planTokenCount := &shared.PlanTokenCount{
diff --git a/shared/plan_result.go b/shared/plan_result.go
index bfd84ffe..65f88dd2 100644
--- a/shared/plan_result.go
+++ b/shared/plan_result.go
@@ -10,15 +10,10 @@ type PlanResult struct {
Replacements []*Replacement `json:"replacements"`
AppliedAt string `json:"appliedAt"`
RejectedAt string `json:"rejectedAt"`
- ResolvedAt string `json:"resolvedAt"`
}
type PlanResultsByPath map[string][]*PlanResult
-func (rep *Replacement) IsPending() bool {
- return !rep.Failed && rep.RejectedAt == "" && rep.ResolvedAt == ""
-}
-
func (res *PlanResult) NumPendingReplacements() int {
numPending := 0
for _, rep := range res.Replacements {
@@ -30,7 +25,7 @@ func (res *PlanResult) NumPendingReplacements() int {
}
func (res *PlanResult) IsPending() bool {
- return res.AppliedAt == "" && res.RejectedAt == "" && res.ResolvedAt == "" && res.NumPendingReplacements() > 0
+ return res.AppliedAt == "" && res.RejectedAt == "" && res.NumPendingReplacements() > 0
}
func (p PlanResultsByPath) SetApplied(ts string) {
@@ -44,24 +39,19 @@ func (p PlanResultsByPath) SetApplied(ts string) {
}
}
-func (p PlanResultsByPath) SetRejected(ts string) {
+func (p PlanResultsByPath) SetRejected(ts string) int {
+ numRejected := 0
for _, planResults := range p {
for _, planResult := range planResults {
if !planResult.IsPending() {
continue
}
planResult.RejectedAt = ts
- }
- }
-}
+ numRejected++
-func (p PlanResultsByPath) SetResolved(ts string) {
- for _, planResults := range p {
- for _, planResult := range planResults {
- if !planResult.IsPending() {
- continue
+ for _, rep := range planResult.Replacements {
+ rep.SetRejected(ts)
}
- planResult.ResolvedAt = ts
}
}
}
diff --git a/shared/replace.go b/shared/replace.go
index 9e2d474c..00fcb5f9 100644
--- a/shared/replace.go
+++ b/shared/replace.go
@@ -11,7 +11,6 @@ type Replacement struct {
Summary string `json:"summary"`
Failed bool `json:"failed"`
RejectedAt string `json:"rejectedAt"`
- ResolvedAt string `json:"resolvedAt"`
}
func ApplyReplacements(content string, replacements []*Replacement, setFailed bool) (string, bool) {
@@ -52,3 +51,11 @@ func ApplyReplacements(content string, replacements []*Replacement, setFailed bo
return updated, allSucceeded
}
+
+func (rep *Replacement) IsPending() bool {
+ return !rep.Failed && rep.RejectedAt == ""
+}
+
+func (rep *Replacement) SetRejected(ts string) {
+ rep.RejectedAt = ts
+}
diff --git a/shared/tokens.go b/shared/tokens.go
index 33fec5a8..82070b6c 100644
--- a/shared/tokens.go
+++ b/shared/tokens.go
@@ -6,9 +6,9 @@ import (
"github.com/pkoukk/tiktoken-go"
)
-var MaxTokens int = 120000
-var MaxContextTokens int = 50000
-var MaxConvoTokens int = 20000
+var MaxTokens int = 7000 //120000
+var MaxContextTokens int = 5000 //50000
+var MaxConvoTokens int = 2000 //20000
func GetNumTokens(text string) (int, error) {
tkm, err := tiktoken.EncodingForModel("gpt-4")