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
improving 'plans' and 'ls' commands, a bunch of improvements, fixes, and refactoring
Dane Schneider83ac966739f7
diff --git a/cli/cmd/cd.go b/cli/cmd/cd.goindex 1ad53116..f20ad2fd 100644--- a/cli/cmd/cd.go+++ b/cli/cmd/cd.go@@ -5,6 +5,7 @@ import ("os""path/filepath""plandex/lib"+ "strconv""github.com/spf13/cobra")@@ -14,40 +15,45 @@ func init() {}var cdCmd = &cobra.Command{- Use: "cd [name]",+ Use: "cd [name-or-index]",Aliases: []string{"set-plan"},- Short: "Change to a different plan",+ Short: "Change to a different plan by name or index",Args: cobra.ExactArgs(1),Run: cd,}func cd(cmd *cobra.Command, args []string) {- name := args[0]plandexDir, _, err := lib.FindOrCreatePlandex()-if err != nil {fmt.Fprintln(os.Stderr, "Error:", err)- os.Exit(1)+ return+ }++ nameOrIdx := args[0]+ var name string++ // see if it's an index+ if idx, err := strconv.Atoi(nameOrIdx); err == nil {+ plans, err := lib.GetPlans()+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ }+ plan := plans[idx]+ name = plan.Name+ } else {+ name = nameOrIdx}planDir := filepath.Join(plandexDir, name)if _, err := os.Stat(planDir); os.IsNotExist(err) {- fmt.Fprintln(os.Stderr, "Error: plan with name '"+name+"' does not exist")- os.Exit(1)- }-- currentPlanFilePath := filepath.Join(plandexDir, "current_plan.json")- currentPlanFile, err := os.OpenFile(currentPlanFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)- if err != nil {- fmt.Println(err)+ fmt.Fprintln(os.Stderr, "π€·ββοΈ Plan '"+name+"' does not exist")os.Exit(1)}- defer currentPlanFile.Close()- _, err = currentPlanFile.WriteString(fmt.Sprintf(`{"name": "%s"}`, name))+ err = lib.SetCurrentPlan(name)if err != nil {- fmt.Println(err)+ fmt.Fprintln(os.Stderr, "Error setting current plan:", err)os.Exit(1)}fmt.Fprintln(os.Stderr, "β Changed current plan to "+name)diff --git a/cli/cmd/clear.go b/cli/cmd/clear.goindex 14344c5b..ed9d9450 100644--- a/cli/cmd/clear.go+++ b/cli/cmd/clear.go@@ -1,13 +1,11 @@package cmdimport (- "encoding/json""fmt""os"- "path/filepath""plandex/lib"- "plandex/types"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -20,35 +18,49 @@ var clearCmd = &cobra.Command{func clearAllContext(cmd *cobra.Command, args []string) {// clear all files from context dir- err := os.RemoveAll(lib.ContextSubdir)+ context, err := lib.GetAllContext(true)if err != nil {- fmt.Fprintln(os.Stderr, "Error clearing context:", err)+ fmt.Fprintln(os.Stderr, "Error retrieving context:", err)return}- // create context dir- err = os.MkdirAll(lib.ContextSubdir, 0755)+ toRemovePaths := []string{}+ for _, part := range context {+ path := lib.CreateContextFileName(part.Name, part.Sha)+ toRemovePaths = append(toRemovePaths, path)+ }++ err = lib.ContextRm(toRemovePaths)+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error removing context:", err)+ return+ }++ // update plan state with new token count+ planState, err := lib.GetPlanState()if err != nil {- fmt.Fprintln(os.Stderr, "Error creating context dir:", err)+ fmt.Fprintln(os.Stderr, "Error retrieving plan state:", err)return}+ planState.ContextTokens = 0++ err = lib.SetPlanState(planState, shared.StringTs())- contextStateFilePath := filepath.Join(lib.ContextSubdir, "context.json")- contextState := types.ModelContextState{NumTokens: 0}- bytes, err := json.MarshalIndent(contextState, "", " ")if err != nil {- fmt.Fprintln(os.Stderr, "Error marshalling context state:", err)+ fmt.Fprintln(os.Stderr, "Error writing plan state:", err)return}- err = os.WriteFile(contextStateFilePath, bytes, 0644)+ msg := "Context cleared"+ err = lib.GitCommitContextUpdate(msg)+if err != nil {- fmt.Fprintln(os.Stderr, "Error writing context state file:", err)+ fmt.Fprintln(os.Stderr, "Error committing context update:", err)return}- fmt.Println("β All context cleared")+ fmt.Println("β " + msg)}diff --git a/cli/cmd/convo.go b/cli/cmd/convo.goindex cea8d9f1..194e67be 100644--- a/cli/cmd/convo.go+++ b/cli/cmd/convo.go@@ -29,7 +29,7 @@ func convo(cmd *cobra.Command, args []string) {}if len(conversation) == 0 {- fmt.Println("No conversation history.")+ fmt.Println("π€·ββοΈ No conversation history")return}@@ -61,6 +61,7 @@ func convo(cmd *cobra.Command, args []string) {fmt.Println(lib.GetDivisionLine())fmt.Println()+fmt.Println(color.New(color.Bold, color.FgCyan).Sprint(" Conversation size β") + fmt.Sprintf(" %d πͺ", totalTokens))fmt.Println()}diff --git a/cli/cmd/current.go b/cli/cmd/current.goindex 1b868f32..2e2cfb52 100644--- a/cli/cmd/current.go+++ b/cli/cmd/current.go@@ -24,4 +24,5 @@ func current(cmd *cobra.Command, args []string) {return}fmt.Println(lib.CurrentPlanName)+}diff --git a/cli/cmd/delete_plan.go b/cli/cmd/delete_plan.goindex 1c859198..91d30c29 100644--- a/cli/cmd/delete_plan.go+++ b/cli/cmd/delete_plan.go@@ -4,6 +4,7 @@ import ("fmt""os""path/filepath"+ "strconv""plandex/lib"@@ -14,15 +15,15 @@ import (var all boolfunc init() {- rmCmd.Flags().BoolVar(&all, "all", false, "Delete all plans and clear the current plan")+ rmCmd.Flags().BoolVar(&all, "all", false, "Delete all plans")RootCmd.AddCommand(rmCmd)}// rmCmd represents the rm commandvar rmCmd = &cobra.Command{- Use: "delete-plan [name]",+ Use: "delete-plan [name-or-index]",Aliases: []string{"del"},- Short: "Delete the specified plan",+ Short: "Delete a plan by name or index, or delete all plans with --all flag",Args: cobra.RangeArgs(0, 1),Run: del,}@@ -33,7 +34,6 @@ func del(cmd *cobra.Command, args []string) {return}- name := args[0]plandexDir, _, err := lib.FindOrCreatePlandex()if err != nil {@@ -41,6 +41,21 @@ func del(cmd *cobra.Command, args []string) {return}+ nameOrIdx := args[0]+ var name string++ // see if it's an index+ if idx, err := strconv.Atoi(nameOrIdx); err == nil {+ plans, err := lib.GetPlans()+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ }+ plan := plans[idx]+ name = plan.Name+ } else {+ name = nameOrIdx+ }+planDir := filepath.Join(plandexDir, name)if _, err := os.Stat(planDir); os.IsNotExist(err) {@@ -84,5 +99,5 @@ func delAll() {return}- fmt.Fprintln(os.Stderr, "β All plans have been deleted.")+ fmt.Fprintln(os.Stderr, "β Deleted all plans")}diff --git a/cli/cmd/load.go b/cli/cmd/load.goindex 3292a963..d9c971e9 100644--- a/cli/cmd/load.go+++ b/cli/cmd/load.go@@ -1,6 +1,7 @@package cmdimport (+ "fmt""plandex/lib""plandex/types"@@ -21,7 +22,6 @@ var contextLoadCmd = &cobra.Command{Aliases: []string{"l"},Short: "Load context from various inputs",Long: `Load context from a file path, a directory, a URL, a string, or piped data.`,- Args: cobra.MinimumNArgs(1),Run: contextLoad,}@@ -48,4 +48,7 @@ func contextLoad(cmd *cobra.Command, args []string) {Truncate: truncate,Resources: args,})++ fmt.Println()+ lib.PrintCmds("", "ls", "tell")}diff --git a/cli/cmd/ls.go b/cli/cmd/ls.goindex fa29087f..d7e5f855 100644--- a/cli/cmd/ls.go+++ b/cli/cmd/ls.go@@ -3,11 +3,14 @@ package cmdimport ("fmt""os"+ "plandex/format""plandex/lib""strconv"+ "time""github.com/fatih/color""github.com/olekukonko/tablewriter"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -28,47 +31,72 @@ func context(cmd *cobra.Command, args []string) {totalTokens := 0table := tablewriter.NewWriter(os.Stdout)- table.SetHeader([]string{"Index", "Type", "ID", "Tokens", "Updated"})+ table.SetHeader([]string{"#" /*"Type",*/, "Name", "πͺ", "Added", "Updated"})table.SetAutoWrapText(false)+ if len(context) == 0 {+ fmt.Println("π€·ββοΈ No context")+ fmt.Println()+ lib.PrintCmds("", "load")+ return+ }+for i, part := range context {totalTokens += part.NumTokens- var contextType string+ // var contextType stringvar id stringif part.FilePath != "" {- contextType = "file"- id = part.FilePath+ // contextType = "file"+ id = " π " + part.FilePath} else if part.Url != "" {- contextType = "url"- id = part.Url+ // contextType = "url"+ id = " π " + part.Url} else {- contextType = "text"- id = part.Name+ // contextType = "text"+ id = " βοΈ " + part.Name+ }++ addedAt, err := time.Parse(shared.TsFormat, part.AddedAt)+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error parsing time:", err)+ continue+ }++ updatedAt, err := time.Parse(shared.TsFormat, part.UpdatedAt)+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error parsing time:", err)+ continue}row := []string{strconv.Itoa(i),- contextType,+ // contextType,id,- strconv.Itoa(part.NumTokens) + " πͺ",- part.UpdatedAt,- }- if i%2 == 0 {- table.Rich(row, []tablewriter.Colors{- {tablewriter.FgHiCyanColor, tablewriter.Bold},- {tablewriter.FgHiWhiteColor},- {tablewriter.FgHiGreenColor, tablewriter.Bold},- {tablewriter.FgHiWhiteColor},- {tablewriter.FgHiWhiteColor},- })- } else {- table.Append(row)+ strconv.Itoa(part.NumTokens), //+ " πͺ",+ format.Time(addedAt),+ format.Time(updatedAt),}+ table.Rich(row, []tablewriter.Colors{+ {tablewriter.FgHiWhiteColor, tablewriter.Bold},+ // {tablewriter.FgHiWhiteColor},+ {tablewriter.FgHiGreenColor, tablewriter.Bold},+ {tablewriter.FgHiWhiteColor},+ {tablewriter.FgHiWhiteColor},+ {tablewriter.FgHiWhiteColor},+ })}table.Render()- fmt.Print(color.New(color.FgHiCyan, color.Bold).Sprintf("\nTotal β") + color.New(color.FgHiWhite, color.Bold).Sprintf(" %d πͺ\n", totalTokens))++ tokensTbl := tablewriter.NewWriter(os.Stdout)+ tokensTbl.SetAutoWrapText(false)+ tokensTbl.Append([]string{color.New(color.FgHiCyan, color.Bold).Sprintf("Total tokens β") + color.New(color.FgHiWhite, color.Bold).Sprintf(" %d πͺ", totalTokens)})++ tokensTbl.Render()++ fmt.Println()+ lib.PrintCmds("", "load", "rm", "clear")}diff --git a/cli/cmd/new.go b/cli/cmd/new.goindex 1e303dc8..8b541af2 100644--- a/cli/cmd/new.go+++ b/cli/cmd/new.go@@ -7,8 +7,10 @@ import ("path/filepath""plandex/lib"+ "plandex/types""github.com/fatih/color"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -68,6 +70,13 @@ func new(cmd *cobra.Command, args []string) {return}+ // Fixes current plan paths (CurrentPlanName, CurrentPlanRootDir, etc.)+ err = lib.LoadCurrentPlan()+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error loading current plan:", err)+ return+ }+// Create 'name' directory inside .plandexerr = os.Mkdir(rootDir, os.ModePerm)if err != nil && !os.IsExist(err) {@@ -131,9 +140,16 @@ func new(cmd *cobra.Command, args []string) {return}}- err = lib.WriteInitialContextState(contextDir)++ ts := shared.StringTs()+ initialPlanState := &types.PlanState{+ Name: "draft",+ CreatedAt: ts,+ UpdatedAt: ts,+ }+ err = lib.SetPlanState(initialPlanState, ts)if err != nil {- fmt.Fprintln(os.Stderr, "Error writing initial context state:", err)+ fmt.Fprintln(os.Stderr, "Error writing initial plan state:", err)return}@@ -160,6 +176,6 @@ func new(cmd *cobra.Command, args []string) {}fmt.Println()- lib.PrintCmds("", "load", "tell")+ lib.PrintCmds("", "load", "tell", "plans")}diff --git a/cli/cmd/plans.go b/cli/cmd/plans.goindex cef2a14b..7887ce47 100644--- a/cli/cmd/plans.go+++ b/cli/cmd/plans.go@@ -3,9 +3,15 @@ package cmdimport ("fmt""os"+ "strconv"+ "time"+ "plandex/format""plandex/lib"+ "github.com/fatih/color"+ "github.com/olekukonko/tablewriter"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -21,23 +27,69 @@ var plansCmd = &cobra.Command{}func plans(cmd *cobra.Command, args []string) {- plandexDir, _, err := lib.FindOrCreatePlandex()-+ plans, err := lib.GetPlans()if err != nil {fmt.Fprintln(os.Stderr, "Error:", err)return}- plans, err := os.ReadDir(plandexDir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error:", err)+ table := tablewriter.NewWriter(os.Stdout)+ table.SetAutoWrapText(false)+ table.SetHeader([]string{"#", "Name", "Updated", "Created", "Context", "Convo"})++ if len(plans) == 0 {+ fmt.Println("π€·ββοΈ No plans")+ fmt.Println()+ lib.PrintCmds("", "new")return}- fmt.Println("Available plans:")- for _, p := range plans {- if p.IsDir() {- fmt.Println("-", p.Name())+ for i, p := range plans {++ var name string+ if p.Name == lib.CurrentPlanName {+ name = color.New(color.Bold, color.FgGreen).Sprint(p.Name) + color.New(color.FgWhite).Sprint(" π current")+ } else {+ name = p.Name+ }++ createdAt, err := time.Parse(shared.TsFormat, p.CreatedAt)+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error parsing time:", err)+ continue}++ updatedAt, err := time.Parse(shared.TsFormat, p.UpdatedAt)+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error parsing time:", err)+ continue+ }++ row := []string{+ strconv.Itoa(i),+ name,+ format.Time(updatedAt),+ format.Time(createdAt),+ strconv.Itoa(p.ContextTokens) + " πͺ",+ strconv.Itoa(p.ConvoTokens) + " πͺ",+ }++ var style []tablewriter.Colors+ if p.Name == lib.CurrentPlanName {+ style = []tablewriter.Colors{+ {tablewriter.FgGreenColor, tablewriter.Bold},+ }+ } else {+ style = []tablewriter.Colors{+ {tablewriter.FgHiWhiteColor, tablewriter.Bold},+ {tablewriter.FgHiWhiteColor},+ }+ }++ table.Rich(row, style)+}+ table.Render()+ fmt.Println()+ lib.PrintCmds("", "new", "cd", "delete-plan")}diff --git a/cli/cmd/rm.go b/cli/cmd/rm.goindex 1f345d12..9dbcf4fc 100644--- a/cli/cmd/rm.go+++ b/cli/cmd/rm.go@@ -1,12 +1,10 @@package cmdimport (- "encoding/json""fmt""os""path/filepath""plandex/lib"- "plandex/types""github.com/plandex/plandex/shared""github.com/spf13/cobra"@@ -16,13 +14,12 @@ var contextRmCmd = &cobra.Command{Use: "rm",Aliases: []string{"remove"},Short: "Remove context",- Long: `Remove context by index, name, file path, or url.`,+ Long: `Remove context by index, name, or glob.`,Args: cobra.MinimumNArgs(1),Run: contextRm,}func contextRm(cmd *cobra.Command, args []string) {-context, err := lib.GetAllContext(true)if err != nil {@@ -56,57 +53,31 @@ func contextRm(cmd *cobra.Command, args []string) {}}- // remove files- errCh := make(chan error, len(toRemovePaths)*2)- for _, path := range toRemovePaths {- for _, ext := range []string{".meta", ".body"} {- go func(path, ext string) {- errCh <- os.Remove(filepath.Join(lib.ContextSubdir, path+ext))- }(path, ext)- }- }-- for i := 0; i < len(toRemovePaths)*2; i++ {- err := <-errCh- if err != nil {- fmt.Fprintln(os.Stderr, "Error removing context file:", err)- return- }- }-- // update context.json with new token count- var contextState types.ModelContextState-- contextStateFilePath := filepath.Join(lib.ContextSubdir, "context.json")- bytes, err := os.ReadFile(contextStateFilePath)+ err = lib.ContextRm(toRemovePaths)if err != nil {- fmt.Fprintln(os.Stderr, "Error reading context state file:", err)+ fmt.Fprintln(os.Stderr, "Error removing context:", err)return}- err = json.Unmarshal(bytes, &contextState)+ // update plan state with new token count+ planState, err := lib.GetPlanState()if err != nil {- fmt.Fprintln(os.Stderr, "Error unmarshalling context state:", err)+ fmt.Fprintln(os.Stderr, "Error retrieving plan state:", err)return}removedTokens := 0- totalTokens := contextState.NumTokens+ totalTokens := planState.ContextTokensfor _, part := range toRemoveParts {removedTokens += part.NumTokenstotalTokens -= part.NumTokens}- contextState.NumTokens = totalTokens+ planState.ContextTokens = totalTokens- bytes, err = json.MarshalIndent(contextState, "", " ")- if err != nil {- fmt.Fprintln(os.Stderr, "Error marshalling context state:", err)- return- }+ err = lib.SetPlanState(planState, shared.StringTs())- err = os.WriteFile(contextStateFilePath, bytes, 0644)if err != nil {- fmt.Fprintln(os.Stderr, "Error writing context state file:", err)+ fmt.Fprintln(os.Stderr, "Error writing plan state:", err)return}@@ -116,7 +87,16 @@ func contextRm(cmd *cobra.Command, args []string) {if len(toRemovePaths) > 1 {suffix = "s"}- fmt.Printf("β Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ \n", len(toRemovePaths), suffix, removedTokens, totalTokens)+ msg := fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ \n", len(toRemovePaths), suffix, removedTokens, totalTokens)++ err = lib.GitCommitContextUpdate(msg)++ if err != nil {+ fmt.Fprintln(os.Stderr, "Error committing context update:", err)+ return+ }++ fmt.Println("β " + msg)} else {fmt.Println("π€·ββοΈ No context removed")}diff --git a/cli/cmd/update.go b/cli/cmd/update.gonew file mode 100644index 00000000..39cf3fae--- /dev/null+++ b/cli/cmd/update.go@@ -0,0 +1,25 @@+package cmd++import (+ "github.com/spf13/cobra"+)++// updateCmd represents the update command+var updateCmd = &cobra.Command{+ Use: "update ",+ Aliases: []string{"u"},+ Short: "Update outdated context",+ Args: cobra.MaximumNArgs(1),+ Run: update,+}++func init() {+ // Add update command+ RootCmd.AddCommand(updateCmd)++}++func update(cmd *cobra.Command, args []string) {+ // Argument parsing and update logic will be implemented++}diff --git a/cli/format/file.go b/cli/format/file.gonew file mode 100644index 00000000..be5c4993--- /dev/null+++ b/cli/format/file.go@@ -0,0 +1,22 @@+package format++import (+ "path/filepath"+ "strings"+)++func GetFileNameWithoutExt(path string) string {+ name := path[:len(path)-len(filepath.Ext(path))]++ name = strings.ToLower(name)+ name = strings.ReplaceAll(name, "_", "-")+ name = strings.ReplaceAll(name, " ", "-")+ name = strings.ReplaceAll(name, ".", "-")+ name = strings.ReplaceAll(name, "/", "-")+ name = strings.ReplaceAll(name, "\\", "-")+ name = strings.ReplaceAll(name, "'", "")+ name = strings.ReplaceAll(name, "`", "")+ name = strings.ReplaceAll(name, "\"", "")++ return name+}diff --git a/cli/format/time.go b/cli/format/time.gonew file mode 100644index 00000000..a569573d--- /dev/null+++ b/cli/format/time.go@@ -0,0 +1,99 @@+// Adapted from https://raw.githubusercontent.com/dustin/go-humanize/master/times.go++package format++import (+ "fmt"+ "sort"+ "time"+)++// Seconds-based time units+const (+ Day = 24 * time.Hour+ Week = 7 * Day+ Month = 30 * Day+ Year = 12 * Month+ LongTime = 37 * Year+)++// Time formats a time into a relative string.+//+// Time(someT) -> "3 weeks ago"+func Time(then time.Time) string {+ return relTime(then.UTC(), time.Now().UTC(), "ago", "from now")+}++// A relTimeMagnitude struct contains a relative time point at which+// the relative format of time will switch to a new format string. A+// slice of these in ascending order by their "D" field is passed to+// CustomRelTime to format durations.+//+// The Format field is a string that may contain a "%s" which will be+// replaced with the appropriate signed label (e.g. "ago" or "from+// now") and a "%d" that will be replaced by the quantity.+//+// The DivBy field is the amount of time the time difference must be+// divided by in order to display correctly.+//+// e.g. if D is 2*time.Minute and you want to display "%d minutes %s"+// DivBy should be time.Minute so whatever the duration is will be+// expressed in minutes.+type relTimeMagnitude struct {+ D time.Duration+ Format string+ DivBy time.Duration+}++var defaultMagnitudes = []relTimeMagnitude{+ {time.Second, "just now", time.Second},+ {2 * time.Second, "1s %s", 1},+ {time.Minute, "%ds %s", time.Second},+ {2 * time.Minute, "1m %s", 1},+ {time.Hour, "%dm %s", time.Minute},+ {2 * time.Hour, "1h %s", 1},+ {Day, "%dh %s", time.Hour},+ {2 * Day, "1d %s", 1},+ {Week, "%dd %s", Day},+ {2 * Week, "1w %s", 1},+ {Month, "%dw %s", Week},+}++// RelTime(timeInPast, timeInFuture, "earlier", "later") -> "3 weeks earlier"+func relTime(a, b time.Time, albl, blbl string) string {+ return customRelTime(a, b, albl, blbl, defaultMagnitudes)+}++func customRelTime(a, b time.Time, albl, blbl string, magnitudes []relTimeMagnitude) string {+ lbl := albl+ diff := b.Sub(a)++ if a.After(b) {+ lbl = blbl+ diff = a.Sub(b)+ }++ // Find the largest magnitude+ largestMagnitude := magnitudes[len(magnitudes)-1].D++ // If the difference is greater than the largest magnitude, format the date in local time+ if diff >= largestMagnitude {+ return a.Local().Format("Jan 2 2006")+ }++ n := sort.Search(len(magnitudes), func(i int) bool {+ return magnitudes[i].D > diff+ })++ // If no magnitude is large enough, use the largest magnitude available+ if n >= len(magnitudes) {+ n = len(magnitudes) - 1+ }+ mag := magnitudes[n]++ if mag.DivBy == 1 {+ return fmt.Sprintf(mag.Format, lbl)+ }++ return fmt.Sprintf(mag.Format, diff/mag.DivBy, lbl)+}diff --git a/cli/go.mod b/cli/go.modindex ee617537..ec7b10fe 100644--- a/cli/go.mod+++ b/cli/go.mod@@ -38,6 +38,7 @@ require (github.com/briandowns/spinner v1.23.0github.com/charmbracelet/glamour v0.6.0github.com/charmbracelet/glow v1.5.1+ github.com/dustin/go-humanize v1.0.1github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203github.com/inconshreveable/mousetrap v1.1.0 // indirectgithub.com/mattn/go-runewidth v0.0.15 // indirectdiff --git a/cli/go.sum b/cli/go.sumindex afd9f3d1..7e644e90 100644--- a/cli/go.sum+++ b/cli/go.sum@@ -272,6 +272,8 @@ github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsqgithub.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=github.com/dustin/go-humanize v1.0.1-0.20200219035652-afde56e7acac/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=+github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=+github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203 h1:XBBHcIb256gUJtLmY22n99HaZTz+r2Z51xUPi01m3wg=github.com/eiannone/keyboard v0.0.0-20220611211555-0d226195f203/go.mod h1:E1jcSv8FaEny+OP/5k9UxZVw9YFWGj7eI4KR/iOBqCg=github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=diff --git a/cli/lib/context_fs.go b/cli/lib/context_fs.goindex 045f7705..31c486cc 100644--- a/cli/lib/context_fs.go+++ b/cli/lib/context_fs.go@@ -5,33 +5,10 @@ import ("fmt""os""path/filepath"- "plandex/types"- "sync""github.com/plandex/plandex/shared")-func WriteInitialContextState(contextDir string) error {- contextState := types.ModelContextState{- NumTokens: 0,- }- contextStateFilePath := filepath.Join(contextDir, "context.json")- contextStateFile, err := os.OpenFile(contextStateFilePath, os.O_CREATE|os.O_WRONLY, 0644)- if err != nil {- return err- }- defer contextStateFile.Close()-- contextStateFileContents, err := json.Marshal(contextState)- if err != nil {- return err- }-- _, err = contextStateFile.Write(contextStateFileContents)-- return err-}-// CreateContextFileName constructs a filename based on the given name and sha.func CreateContextFileName(name, sha string) string {// Extract the first 8 characters of the sha@@ -39,6 +16,27 @@ func CreateContextFileName(name, sha string) string {return fmt.Sprintf("%s.%s", name, shaSubstring)}+func ContextRm(paths []string) error {+ // remove files+ errCh := make(chan error, len(paths)*2)+ for _, path := range paths {+ for _, ext := range []string{".meta", ".body"} {+ go func(path, ext string) {+ errCh <- os.Remove(filepath.Join(ContextSubdir, path+ext))+ }(path, ext)+ }+ }++ for i := 0; i < len(paths)*2; i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error removing context file: %v", err)+ }+ }++ return nil+}+// writeContextPartToFile writes a single ModelContextPart to a file.func writeContextPartToFile(part shared.ModelContextPart) error {metaFilename := CreateContextFileName(part.Name, part.Sha) + ".meta"@@ -83,18 +81,27 @@ func writeContextPartToFile(part shared.ModelContextPart) error {}// Write each context part in parallel-func writeContextParts(contextParts []shared.ModelContextPart) {- var wg sync.WaitGroup+func writeContextParts(contextParts []shared.ModelContextPart) error {+ var errCh = make(chan error)for _, part := range contextParts {- wg.Add(1)go func(p shared.ModelContextPart) {- defer wg.Done()if err := writeContextPartToFile(p); err != nil {// Handling the error in the goroutine by logging. Depending on your application,// you might want a different strategy (e.g., collect errors and handle them after waiting).- fmt.Fprintf(os.Stderr, "Error writing context part to file: %v", err)+ err := fmt.Errorf("Error writing context part to file: %v", err)+ errCh <- err}+ errCh <- nil}(part)}- wg.Wait() // Wait for all goroutines to finish writing++ // Wait for all goroutines to finish+ for range contextParts {+ if err := <-errCh; err != nil {+ return err+ }+ }++ return nil+}diff --git a/cli/lib/context_get.go b/cli/lib/context_get.goindex ade94ce3..0f42b055 100644--- a/cli/lib/context_get.go+++ b/cli/lib/context_get.go@@ -5,6 +5,7 @@ import ("fmt""os""path/filepath"+ "sort""strings""github.com/plandex/plandex/shared"@@ -17,7 +18,7 @@ func GetAllContext(metaOnly bool) ([]shared.ModelContextPart, error) {return nil, err}- var contexts []shared.ModelContextPart+ var context []shared.ModelContextPartfor _, file := range files {filename := file.Name()@@ -57,9 +58,14 @@ func GetAllContext(metaOnly bool) ([]shared.ModelContextPart, error) {contextPart.Body = string(bodyContent)}- contexts = append(contexts, contextPart)+ context = append(context, contextPart)}}- return contexts, nil+ // sort by timestamp ascending+ sort.Slice(context, func(i, j int) bool {+ return context[i].AddedAt < context[j].AddedAt+ })++ return context, nil}diff --git a/cli/lib/context_load.go b/cli/lib/context_load.goindex a2023109..60a15dc1 100644--- a/cli/lib/context_load.go+++ b/cli/lib/context_load.go@@ -4,12 +4,12 @@ import ("bufio""crypto/sha256""encoding/hex"- "encoding/json""fmt""io""log""os""path/filepath"+ "plandex/format""plandex/types""strings""sync"@@ -25,10 +25,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)s.Prefix = "π₯ Loading context... "s.Start()- var contextState types.ModelContextState- contextStateFilePath := filepath.Join(ContextSubdir, "context.json")-- // fmt.Fprintf(os.Stderr, "Loading context from %s\n", contextStateFilePath)var maxTokens intif params.MaxTokens == -1 {@@ -37,30 +33,13 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {maxTokens = min(params.MaxTokens, shared.MaxTokens)}- func() {- contextStateFile, err := os.Open(contextStateFilePath)- if err != nil {- log.Fatalf("Error opening file: %v", err)- return- }-- defer contextStateFile.Close()-- data, err := io.ReadAll(contextStateFile)- if err != nil {- log.Fatalf("Error reading file: %v", err)- return- }-- err = json.Unmarshal(data, &contextState)- if err != nil {- log.Fatalf("Error unmarshalling JSON: %v", err)- return- }- }()+ planState, err := GetPlanState()+ if err != nil {+ log.Fatalf("Failed to get plan state: %v", err)+ }tokensAdded := 0- totalTokens := contextState.NumTokens+ totalTokens := planState.ContextTokensvar totalTokensMutex sync.Mutexvar contextParts []shared.ModelContextPart@@ -91,8 +70,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating input text.\n", totalTokens, maxTokens)numTokens = maxTokens - (totalTokens - numTokens)- s.Start()-// If the number of tokens is less than or equal to 0, then we can stop processing filesif numTokens <= 0 {return@@ -116,14 +93,16 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {log.Fatalf("Failed to summarize the text: %v", err)}- fileName := GetFileNameWithoutExt(fileNameResp.FileName)+ fileName := format.GetFileNameWithoutExt(fileNameResp.FileName)+ ts := shared.StringTs()contextPart := shared.ModelContextPart{Name: fileName,Body: body,Sha: sha,NumTokens: numTokens,- UpdatedAt: shared.StringTs(),+ AddedAt: ts,+ UpdatedAt: ts,}contextPartsMutex.Lock()@@ -170,8 +149,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating piped data.\n", totalTokens, maxTokens)numTokens = maxTokens - (totalTokens - numTokens)- s.Start()-if numTokens <= 0 {return}@@ -193,14 +170,16 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {log.Fatalf("Failed to summarize piped data: %v", err)}- fileName := GetFileNameWithoutExt(fileNameResp.FileName)+ fileName := format.GetFileNameWithoutExt(fileNameResp.FileName)+ ts := shared.StringTs()contextPart := shared.ModelContextPart{Name: fileName,Body: body,Sha: sha,NumTokens: numTokens,- UpdatedAt: shared.StringTs(),+ AddedAt: ts,+ UpdatedAt: ts,}contextPartsMutex.Lock()@@ -237,8 +216,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {body := strings.Join(flattenedPaths, "\n")bytes := []byte(body)- fmt.Println(body)-hash := sha256.Sum256(bytes)sha := hex.EncodeToString(hash[:])numTokens := shared.GetNumTokens(body)@@ -256,8 +233,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating directory layout.\n", totalTokens, maxTokens)numTokens = maxTokens - (totalTokens - numTokens)- s.Start()-// If the number of tokens is less than or equal to 0, then we can stop processing filesif numTokens <= 0 {return@@ -274,12 +249,14 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {}()+ ts := shared.StringTs()contextPart := shared.ModelContextPart{Name: "directory layout",Body: body,Sha: sha,NumTokens: numTokens,- UpdatedAt: shared.StringTs(),+ AddedAt: ts,+ UpdatedAt: ts,}contextPartsMutex.Lock()@@ -316,8 +293,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating the file %s.\n", totalTokens, maxTokens, path)numTokens = maxTokens - (totalTokens - numTokens)- s.Start()-// If the number of tokens is less than or equal to 0, then we can stop processing filesif numTokens <= 0 {return@@ -336,13 +311,16 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {_, fileName := filepath.Split(path)+ ts := shared.StringTs()+contextPart := shared.ModelContextPart{Name: fileName,Body: body,FilePath: path,Sha: sha,NumTokens: numTokens,- UpdatedAt: shared.StringTs(),+ AddedAt: ts,+ UpdatedAt: ts,}contextPartsMutex.Lock()@@ -383,8 +361,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {fmt.Fprintf(os.Stderr, "The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating content from URL %s.\n", totalTokens, maxTokens, url)numTokens = maxTokens - (totalTokens - numTokens)- s.Start()-// If the number of tokens is less than or equal to 0, then we can stop processing contentif numTokens <= 0 {return@@ -402,13 +378,15 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {hash := sha256.Sum256([]byte(body))sha := hex.EncodeToString(hash[:])+ ts := shared.StringTs()contextPart := shared.ModelContextPart{Name: SanitizeAndClipURL(url, 70),Url: url,Body: body,Sha: sha,NumTokens: numTokens,- UpdatedAt: shared.StringTs(),+ AddedAt: ts,+ UpdatedAt: ts,}contextPartsMutex.Lock()@@ -424,32 +402,22 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {log.Fatalln("No context loaded")}- wg = sync.WaitGroup{}-- wg.Add(1)+ errCh := make(chan error, 2)go func() {- defer wg.Done()- writeContextParts(contextParts)+ errCh <- writeContextParts(contextParts)}()- wg.Add(1)-go func() {- defer wg.Done()- contextState.NumTokens = totalTokens+ planState.ContextTokens = totalTokens+ errCh <- SetPlanState(planState, shared.StringTs())+ }()- data, err := json.MarshalIndent(contextState, "", " ")+ for i := 0; i < 2; i++ {+ err := <-errChif err != nil {- log.Fatalf("Failed to marshal context state: %v", err)+ log.Fatal(err)}- // write file- err = os.WriteFile(contextStateFilePath, data, 0644)- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to write context state to file: %v", err)- }- }()-- wg.Wait()+ }var added []stringif params.Note != "" {@@ -493,21 +461,9 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {// msg += fmt.Sprintf("\n\nTotal tokens in context: %d\n", totalTokens)- err = GitAddAndCommit(ContextSubdir, msg)-- if err != nil {- log.Fatalf("Failed to commit context: %v", err)- }-- err = GitAdd(CurrentPlanRootDir, ContextSubdir, true)- if err != nil {- log.Fatalf("failed to stage submodule changes in context dir: %s\n", err)- }-- // Commit these staged submodule changes in the root repo- err = GitCommit(CurrentPlanRootDir, msg, true)+ err = GitCommitContextUpdate(msg)if err != nil {- log.Fatalf("failed to commit submodule updates in root dir: %s\n", err)+ log.Fatalf("Failed to commit context update to git: %v", err)}elapsed := time.Since(timeStart)@@ -518,7 +474,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {s.Stop()ClearCurrentLine()- fmt.Fprintln(os.Stderr, "β "+msg)+ fmt.Println("β " + msg)return tokensAdded, totalTokens}diff --git a/cli/lib/context_update.go b/cli/lib/context_update.gonew file mode 100644index 00000000..55c21f80--- /dev/null+++ b/cli/lib/context_update.go@@ -0,0 +1 @@+package libdiff --git a/cli/lib/conversation.go b/cli/lib/conversation.goindex aa48c783..db57ab6d 100644--- a/cli/lib/conversation.go+++ b/cli/lib/conversation.go@@ -192,6 +192,9 @@ func appendConversation(params types.AppendConversationParams) error {return fmt.Errorf("failed to write conversation file: %s", err)}+ // Update plan state -- will be written to file by caller+ params.PlanState.ConvoTokens += params.PromptTokens + params.ReplyTokens+return nil}diff --git a/cli/lib/fs.go b/cli/lib/fs.goindex 0c459456..b0cfa729 100644--- a/cli/lib/fs.go+++ b/cli/lib/fs.go@@ -138,7 +138,6 @@ func FlattenPaths(fileOrDirPaths []string, params *types.LoadContextParams) []stwg.Add(1)go func(p string) {defer wg.Done()- depth := 0err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {if err != nil {@@ -150,11 +149,17 @@ func FlattenPaths(fileOrDirPaths []string, params *types.LoadContextParams) []streturn filepath.SkipDir}- if depth >= params.MaxDepth {- return filepath.SkipDir+ // calculate directory depth from p+ depth := 0+ for _, c := range info.Name() {+ if c == filepath.Separator {+ depth += 1+ }}- depth++ // Incrementing depth here+ if params.MaxDepth != -1 && depth > params.MaxDepth {+ return filepath.SkipDir+ }}resPathsChan <- pathdiff --git a/cli/lib/git.go b/cli/lib/git.goindex 6ad2fa06..faab996b 100644--- a/cli/lib/git.go+++ b/cli/lib/git.go@@ -99,33 +99,54 @@ func GitCommit(repoDir, commitMsg string, lockMutex bool) error {return nil}-func gitCommitPlanUpdate(commitMsg string) error {+func GitCommitContextUpdate(commitMsg string) error {+ err := GitAddAndCommit(ContextSubdir, commitMsg)++ if err != nil {+ return fmt.Errorf("failed to commit context: %v", err)+ }++ err = GitAdd(CurrentPlanRootDir, ContextSubdir, true)+ if err != nil {+ return fmt.Errorf("failed to stage submodule changes in context dir: %s", err)+ }++ // Commit these staged submodule changes in the root repo+ err = GitCommit(CurrentPlanRootDir, commitMsg, true)+ if err != nil {+ return fmt.Errorf("failed to commit submodule updates in root dir: %s", err)+ }++ return nil+}++func GitCommitPlanUpdate(commitMsg string) error {err := GitAddAndCommit(ConversationSubdir, commitMsg)if err != nil {- return fmt.Errorf("failed to commit files to conversation dir: %s\n", err)+ return fmt.Errorf("failed to commit files to conversation dir: %s", err)}err = GitAddAndCommit(PlanSubdir, commitMsg)if err != nil {- return fmt.Errorf("failed to commit files to plan dir: %s\n", err)+ return fmt.Errorf("failed to commit files to plan dir: %s", err)}// Stage changes in the submodules in the root repoerr = GitAdd(CurrentPlanRootDir, ConversationSubdir, true)if err != nil {- return fmt.Errorf("failed to stage submodule changes in conversation dir: %s\n", err)+ return fmt.Errorf("failed to stage submodule changes in conversation dir: %s", err)}err = GitAdd(CurrentPlanRootDir, PlanSubdir, true)if err != nil {- return fmt.Errorf("failed to stage submodule changes in plan dir: %s\n", err)+ return fmt.Errorf("failed to stage submodule changes in plan dir: %s", err)}// Commit these staged submodule changes in the root repoerr = GitCommit(CurrentPlanRootDir, commitMsg, true)if err != nil {- return fmt.Errorf("failed to commit submodule updates in root dir: %s\n", err)+ return fmt.Errorf("failed to commit submodule updates in root dir: %s", err)}return nildiff --git a/cli/lib/plans.go b/cli/lib/plans.goindex e757e4f8..71366717 100644--- a/cli/lib/plans.go+++ b/cli/lib/plans.go@@ -1,10 +1,15 @@package libimport (+ "encoding/json""fmt""os""path/filepath"+ "plandex/types""regexp"+ "sort"++ "github.com/plandex/plandex/shared")func SetCurrentPlan(name string) error {@@ -102,6 +107,17 @@ func RenameCurrentDraftPlan(name string) error {if err != nil {return fmt.Errorf("failed to load current plan: %w", err)}++ planState, err := GetPlanState()+ if err != nil {+ return fmt.Errorf("failed to get plan state: %w", err)+ }++ planState.Name = name+ err = SetPlanState(planState, shared.StringTs())+ if err != nil {+ return fmt.Errorf("failed to set plan state: %w", err)+ }}return nil@@ -142,3 +158,96 @@ func DedupPlanName(name string) (string, error) {return name, nil}++func GetPlanState() (*types.PlanState, error) {+ return GetPlanStateForPlan(CurrentPlanName)+}++func GetPlanStateForPlan(name string) (*types.PlanState, error) {+ if PlandexDir == "" {+ return nil, fmt.Errorf("PlandexDir not set")+ }++ var state types.PlanState+ planStatePath := filepath.Join(PlandexDir, name, "plan.json")+ fileBytes, err := os.ReadFile(planStatePath)+ if err != nil {+ return nil, fmt.Errorf("failed to open plan state file: %s", err)+ }+ err = json.Unmarshal(fileBytes, &state)+ if err != nil {+ return nil, fmt.Errorf("failed to parse plan state json: %s", err)+ }++ return &state, nil+}++func SetPlanState(state *types.PlanState, updatedAt string) error {+ if updatedAt != "" {+ state.UpdatedAt = updatedAt+ }++ planStatePath := filepath.Join(CurrentPlanRootDir, "plan.json")+ planStateBytes, err := json.MarshalIndent(state, "", " ")+ if err != nil {+ return fmt.Errorf("failed to marshal plan state: %s", err)+ }+ err = os.WriteFile(planStatePath, planStateBytes, 0644)+ if err != nil {+ return fmt.Errorf("failed to write plan state: %s", err)+ }+ return nil+}++func GetPlans() ([]*types.PlanState, error) {+ if PlandexDir == "" {+ return nil, fmt.Errorf("PlandexDir not set")+ }++ var plans []*types.PlanState++ planContents, err := os.ReadDir(PlandexDir)++ if err != nil {+ return nil, fmt.Errorf("failed to read plandex directory: %s", err)+ }++ planCh := make(chan *types.PlanState, len(planContents))+ errCh := make(chan error, 1)++ for _, planFileOrDir := range planContents {+ if planFileOrDir.IsDir() {+ go func(planDir os.DirEntry) {+ planName := planDir.Name()++ planState, err := GetPlanStateForPlan(planName)+ if err != nil {+ errCh <- fmt.Errorf("failed to get plan state for plan %s: %s", planName, err)+ return+ }++ planCh <- planState+ }(planFileOrDir)+ } else {+ planCh <- nil+ }+ }++ for range planContents {+ select {+ case plan := <-planCh:+ if plan != nil {+ plans = append(plans, plan)+ }+ case err := <-errCh:+ return nil, err+ }+ }++ // sort plans by UpdatedAt descending+ sort.Slice(plans, func(i, j int) bool {+ return plans[i].UpdatedAt > plans[j].UpdatedAt+ })++ return plans, nil+}diff --git a/cli/lib/proposal.go b/cli/lib/proposal.goindex 85f5fa43..0e49b4fd 100644--- a/cli/lib/proposal.go+++ b/cli/lib/proposal.go@@ -5,7 +5,7 @@ import ("encoding/json""fmt""os"- "path/filepath"+ "plandex/format""plandex/types""time"@@ -26,7 +26,7 @@ func Propose(prompt string) error {start := time.Now()s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)- s.Prefix = "π¬ Sending prompt..."+ s.Prefix = "π¬ Sending prompt... "s.Start()if CurrentPlanIsDraft() {@@ -35,7 +35,7 @@ func Propose(prompt string) error {fmt.Fprintln(os.Stderr, "\nError summarizing prompt:", err)return err}- RenameCurrentDraftPlan(GetFileNameWithoutExt(fileNameResp.FileName))+ RenameCurrentDraftPlan(format.GetFileNameWithoutExt(fileNameResp.FileName))}timestamp := shared.StringTs()@@ -50,9 +50,6 @@ func Propose(prompt string) error {var proposalId stringvar replyStarted boolvar terminalHasPendingUpdate bool- var endedReply bool-- var descJson stringvar desc *shared.PlanDescriptionvar state *fsm.FSMvar streamFinished bool@@ -66,23 +63,14 @@ func Propose(prompt string) error {var parentProposalId stringvar rootId string- var planState types.PlanState- // get plan state from [CurrentPlanRootDir]/plan.json- planStatePath := filepath.Join(CurrentPlanRootDir, "plan.json")- if _, err := os.Stat(planStatePath); os.IsNotExist(err) {- planState = types.PlanState{}- } else {- fileBytes, err := os.ReadFile(planStatePath)- if err != nil {- return fmt.Errorf("failed to open plan state file: %s", err)- }- err = json.Unmarshal(fileBytes, &planState)- if err != nil {- return fmt.Errorf("failed to parse plan state json: %s", err)- }- parentProposalId = planState.ProposalId- rootId = planState.RootId++ planState, err := GetPlanState()++ if err != nil {+ return fmt.Errorf("failed to get plan state: %s", err)}+ parentProposalId = planState.ProposalId+ rootId = planState.RootIdif rootId != "" {err = saveLatestConvoSummary(rootId)@@ -127,12 +115,12 @@ func Propose(prompt string) error {s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)s.Prefix = " "s.Start()- endedReply = true}appendConvo := func() {var totalTokens int_, _, _, totalTokens = replyTokenCounter.FinishAndRead()+err := appendConversation(types.AppendConversationParams{Timestamp: timestamp,ResponseTimestamp: desc.ResponseTimestamp,@@ -140,6 +128,7 @@ func Propose(prompt string) error {PromptTokens: promptNumTokens,Reply: reply,ReplyTokens: totalTokens,+ PlanState: planState,})if err != nil {fmt.Printf("failed to append conversation: %s", err)@@ -196,22 +185,13 @@ func Propose(prompt string) error {}// Save proposal id to [CurrentPlanRootDir]/plan.json- planState = types.PlanState{- ProposalId: proposalId,- RootId: rootId,- }- planStatePath := filepath.Join(CurrentPlanRootDir, "plan.json")- planStateBytes, err := json.Marshal(planState)+ planState.ProposalId = proposalId+ planState.RootId = rootId+ err = SetPlanState(planState, shared.StringTs())if err != nil {- onError(fmt.Errorf("failed to marshal plan state: %s", err))+ onError(fmt.Errorf("failed to update plan state: %s", err))return}- err = os.WriteFile(planStatePath, planStateBytes, 0644)- if err != nil {- onError(fmt.Errorf("failed to write plan state: %s", err))- return- }-return}} else if !replyStarted {@@ -234,10 +214,6 @@ func Propose(prompt string) error {terminalHasPendingUpdate = truecase shared.STATE_FINISHED:- if !endedReply {- endReply()- appendConvo()- }s.Stop()streamFinished = true@@ -251,15 +227,21 @@ func Propose(prompt string) error {endReply()} else {- descJson = content- err := json.Unmarshal([]byte(descJson), &desc)+ err := json.Unmarshal([]byte(content), &desc)if err != nil {onError(fmt.Errorf("error parsing plan description: %v", err))return}+ planState.Description = descappendConvo()+ err = SetPlanState(planState, shared.StringTs())+ if err != nil {+ onError(fmt.Errorf("failed to update plan state: %s", err))+ return+ }+if desc.MadePlan && (len(desc.Files) > 0) {s.Stop()fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))diff --git a/cli/lib/term.go b/cli/lib/term.goindex f25df412..db304906 100644--- a/cli/lib/term.go+++ b/cli/lib/term.go@@ -5,20 +5,29 @@ import ("os""strings"+ "github.com/eiannone/keyboard""github.com/fatih/color""golang.org/x/term")var cmdDesc = map[string][2]string{- "new": {"", "start a new plan"},- "load": {"l", "load files/directories/urls/strings or pipe data into context"},- "tell": {"t", "describe a task, ask a question, or chat"},- "diffs": {"d", "show diffs between plan and project files"},- "preview": {"p", "preview the plan in a branch"},- "apply": {"ap", "apply the plan to your project files"},- "next": {"n", "continue to next step of the plan"},- "status": {"s", "show status of the plan"},- "rewind": {"rw", "rewind the plan to a previous step or state"},+ "new": {"", "start a new plan"},+ "current": {"c", "show current plan"},+ "cd": {"", "change current plan"},+ "load": {"l", "load files/directories/urls/strings or pipe data into context"},+ "tell": {"t", "describe a task, ask a question, or chat"},+ "diffs": {"d", "show diffs between plan and project files"},+ "preview": {"p", "preview the plan in a branch"},+ "apply": {"ap", "apply the plan to your project files"},+ "next": {"n", "continue to next step of the plan"},+ "status": {"s", "show status of the plan"},+ "rewind": {"rw", "rewind the plan to a previous step or state"},+ "ls": {"", "list everything in context"},+ "rm": {"", "remove context by name, index, or glob"},+ "clear": {"", "remove all context"},+ "delete-plan": {"del", "delete plan by name or index"},+ "plans": {"", "list plans"},+ "update": {"u", "update outdated context"},}func PrintCmds(prefix string, cmds ...string) {@@ -135,3 +144,19 @@ func displayHotkeys() string {" \x1b[1m(s)\x1b[0m" + `top` + divisionLine}++func getUserInput() (rune, error) {+ if err := keyboard.Open(); err != nil {+ return 0, fmt.Errorf("failed to open keyboard: %s\n", err)+ }+ defer func() {+ _ = keyboard.Close()+ }()++ char, _, err := keyboard.GetKey()+ if err != nil {+ return 0, fmt.Errorf("failed to read keypress: %s\n", err)+ }++ return char, nil+}diff --git a/cli/lib/utils.go b/cli/lib/utils.goindex 2fede454..aed0d838 100644--- a/cli/lib/utils.go+++ b/cli/lib/utils.go@@ -1,14 +1,9 @@package libimport (- "fmt"- "path/filepath""regexp""strconv"- "strings""time"-- "github.com/eiannone/keyboard")func EnsureMinDuration(start time.Time, minDuration time.Duration) {@@ -18,38 +13,6 @@ func EnsureMinDuration(start time.Time, minDuration time.Duration) {}}-func GetFileNameWithoutExt(path string) string {- name := path[:len(path)-len(filepath.Ext(path))]-- name = strings.ToLower(name)- name = strings.ReplaceAll(name, "_", "-")- name = strings.ReplaceAll(name, " ", "-")- name = strings.ReplaceAll(name, ".", "-")- name = strings.ReplaceAll(name, "/", "-")- name = strings.ReplaceAll(name, "\\", "-")- name = strings.ReplaceAll(name, "'", "")- name = strings.ReplaceAll(name, "`", "")- name = strings.ReplaceAll(name, "\"", "")-- return name-}--func getUserInput() (rune, error) {- if err := keyboard.Open(); err != nil {- return 0, fmt.Errorf("failed to open keyboard: %s\n", err)- }- defer func() {- _ = keyboard.Close()- }()-- char, _, err := keyboard.GetKey()- if err != nil {- return 0, fmt.Errorf("failed to read keypress: %s\n", err)- }-- return char, nil-}-func cleanJsonFragment(s string) string {// replace escaped quotes with unescaped quotess = regexp.MustCompile(`\\\"`).ReplaceAllString(s, `"`)diff --git a/cli/plan.json b/cli/plan.jsonnew file mode 100644index 00000000..d79d0411--- /dev/null+++ b/cli/plan.json@@ -0,0 +1,11 @@+{+ "name": "draft",+ "proposalId": "",+ "rootId": "",+ "createdAt": "2023-11-04T09:44:29.147Z",+ "updatedAt": "2023-11-04T09:44:29.147Z",+ "description": null,+ "contextTokens": 0,+ "convoTokens": 0,+ "convoSummarizedTokens": 0+}\ No newline at end of filediff --git a/cli/types/types.go b/cli/types/types.goindex 153fd549..751a22db 100644--- a/cli/types/types.go+++ b/cli/types/types.go@@ -39,17 +39,21 @@ type AppendConversationParams struct {Reply stringPromptTokens intReplyTokens int+ PlanState *PlanState}type PlanState struct {- ProposalId string- RootId string+ Name string `json:"name"`+ ProposalId string `json:"proposalId"`+ RootId string `json:"rootId"`+ CreatedAt string `json:"createdAt"`+ UpdatedAt string `json:"updatedAt"`+ Description *shared.PlanDescription `json:"description"`+ ContextTokens int `json:"contextTokens"`+ ConvoTokens int `json:"convoTokens"`+ ConvoSummarizedTokens int `json:"convoSummarizedTokens"`}type PlanSettings struct {Name string}--type ModelContextState struct {- NumTokens int `json:"numTokens"`-}diff --git a/server/model/proposal/confirm.go b/server/model/proposal/build.gosimilarity index 96%rename from server/model/proposal/confirm.gorename to server/model/proposal/build.goindex e3a840c0..da0256aa 100644--- a/server/model/proposal/confirm.go+++ b/server/model/proposal/build.go@@ -16,151 +16,7 @@ import ("github.com/sashabaranov/go-openai/jsonschema")-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.--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 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.--1.)-If the current file is:-` + "```" + `-package main--import "fmt"--func main() {- fmt.Println("Hello, world!")-}-` + "```" + `--And the previous response was:-->>>-You can change the main.go file to print the current time instead of "Hello, world!".:--- main.go:-` + "```" + `-func main() {- fmt.Println(time.Now())-}-` + "```" + `--You'll also need to import the time package:--- main.go:-` + "```" + `-import (- "fmt"- "time"-)-` + "```" + `-<<<--Then you would call the 'writeReplacements' function like this:--writeReplacements({- replacements: [- {- old: "import \"fmt\"",- new: "import (\n\t\"fmt\"\n\t\"time\"\n)"- },- {- old: "fmt.Println(\"Hello, world!\")",- new: "fmt.Println(time.Now())"- }- }-})--2.)-If the current file is:-` + "```" + `-package helpers--func Add(a, b int) int {- return a + b-}-` + "```" + `--And the previous response was:-->>>-Add another function to the helpers.go file that subtracts two numbers:--- helpers.go:-` + "```" + `-func Subtract(a, b int) int {- return a - b-}-` + "```" + `-<<<--Then you would call the 'writeReplacements' function like this:--writeReplacements({- replacements: [- {- old: "\n}",- new: "\n}\n\nfunc Subtract(a, b int) int {\n\treturn a - b\n}"- }- ]-})--3.)-If the current file is:-` + "```" + `-package main--import "fmt"--func main() {- fmt.Println("Hello, world!")-}-` + "```" + `--And the previous response was:-->>>-You can change the main.go file to print "I love you!" in addition to "Hello, world!".:--- main.go:-` + "```" + `-func main() {- fmt.Println("Hello, world!")- fmt.Println("I love you!")-}-` + "```" + `-<<<--Then you would call the 'writeReplacements' function like this:--writeReplacements({- replacements: [- {- old: "fmt.Println(\"Hello, world!\")",- new: "fmt.Println(\"Hello, world!\")\n\tfmt.Println(\"I love you!\")"- }- ]-})--[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, fileContents map[string]string, numTokensByFile map[string]int, onStream types.OnStreamFunc) error {+func buildPlan(proposalId string, fileContents map[string]string, numTokensByFile map[string]int, onStream types.OnStreamFunc) error {goEnv := os.Getenv("GOENV")if goEnv == "test" {streamFilesLoremIpsum(onStream)@@ -441,11 +297,13 @@ func confirmProposal(proposalId string, fileContents map[string]string, numTokendelta := response.Choices[0].Deltaif delta.FunctionCall == nil {- fmt.Printf("\nStream received data not for a function call")+ fmt.Println("Stream received data not for a function call")spew.Dump(delta)continue} else {+ fmt.Printf("Stream received data for function call: %s", delta.FunctionCall.Name)+content = delta.FunctionCall.Arguments}@@ -476,3 +334,147 @@ func confirmProposal(proposalId string, fileContents map[string]string, numTokenreturn nil}++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.++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 *ABSOLUTELY MUST BE AN EXACT 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.++1.)+If the current file is:+` + "```" + `+package main++import "fmt"++func main() {+ fmt.Println("Hello, world!")+}+` + "```" + `++And the previous response was:++>>>+You can change the main.go file to print the current time instead of "Hello, world!".:++- main.go:+` + "```" + `+func main() {+ fmt.Println(time.Now())+}+` + "```" + `++You'll also need to import the time package:++- main.go:+` + "```" + `+import (+ "fmt"+ "time"+)+` + "```" + `+<<<++Then you would call the 'writeReplacements' function like this:++writeReplacements({+ replacements: [+ {+ old: "import \"fmt\"",+ new: "import (\n\t\"fmt\"\n\t\"time\"\n)"+ },+ {+ old: "fmt.Println(\"Hello, world!\")",+ new: "fmt.Println(time.Now())"+ }+ }+})++2.)+If the current file is:+` + "```" + `+package helpers++func Add(a, b int) int {+ return a + b+}+` + "```" + `++And the previous response was:++>>>+Add another function to the helpers.go file that subtracts two numbers:++- helpers.go:+` + "```" + `+func Subtract(a, b int) int {+ return a - b+}+` + "```" + `+<<<++Then you would call the 'writeReplacements' function like this:++writeReplacements({+ replacements: [+ {+ old: "\n}",+ new: "\n}\n\nfunc Subtract(a, b int) int {\n\treturn a - b\n}"+ }+ ]+})++3.)+If the current file is:+` + "```" + `+package main++import "fmt"++func main() {+ fmt.Println("Hello, world!")+}+` + "```" + `++And the previous response was:++>>>+You can change the main.go file to print "I love you!" in addition to "Hello, world!".:++- main.go:+` + "```" + `+func main() {+ fmt.Println("Hello, world!")+ fmt.Println("I love you!")+}+` + "```" + `+<<<++Then you would call the 'writeReplacements' function like this:++writeReplacements({+ replacements: [+ {+ old: "fmt.Println(\"Hello, world!\")",+ new: "fmt.Println(\"Hello, world!\")\n\tfmt.Println(\"I love you!\")"+ }+ ]+})++[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]`diff --git a/server/model/proposal/create.go b/server/model/proposal/create.goindex 488bd344..2edd1b66 100644--- a/server/model/proposal/create.go+++ b/server/model/proposal/create.go@@ -131,7 +131,7 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) errorfor _, convoMessage := range req.Conversation {conversationTokens += convoMessage.TokenstokensUpToTimestamp[convoMessage.Timestamp] = conversationTokens- fmt.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)+ // fmt.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)}fmt.Printf("Conversation tokens: %d\n", conversationTokens)@@ -197,10 +197,12 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error}}- messages = append(messages, openai.ChatCompletionMessage{+ promptMessage := openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,Content: fmt.Sprintf(promptWrapperFormatStr, req.Prompt),- })+ }++ messages = append(messages, promptMessage)// fmt.Println("\n\nMessages:")// for _, message := range messages {@@ -302,20 +304,37 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) errorconvoSummaryProcs.Set(proposal.RootId, &summaryProc)- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: proposal.Content,- })-go func() {fmt.Println("Generating plan summary for rootId:", proposal.RootId)defer func() {close(summaryCh)close(errCh)- fmt.Println("Closing summary channels for root", proposal.RootId)}()- summary, err := model.PlanSummary(messages, responseTs)++ var summaryMessages []openai.ChatCompletionMessage+ var latestSummary *shared.ConversationSummary+ if len(req.ConversationSummaries) > 0 {+ latestSummary = &req.ConversationSummaries[len(req.ConversationSummaries)-1]+ }++ if latestSummary == nil {+ for _, convoMessage := range req.Conversation {+ summaryMessages = append(summaryMessages, convoMessage.Message)+ }+ } else {+ summaryMessages = append(summaryMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: latestSummary.Summary,+ })+ }++ summaryMessages = append(summaryMessages, promptMessage, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: proposal.Content,+ })++ summary, err := model.PlanSummary(summaryMessages, responseTs)if err != nil {fmt.Printf("Error generating plan summary for root %s: %v\n", proposal.RootId, err)@@ -326,7 +345,7 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) errorreturn}- fmt.Println("Generated plan summary for root", proposal.RootId, ":", summary.Summary)+ fmt.Println("Generated plan summary for root", proposal.RootId)convoSummaries.Set(proposal.RootId, summary)summaryCh <- summary@@ -336,46 +355,38 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) errorfiles, fileContents, numTokensByFile, _ := replyInfo.FinishAndRead()+ var planDescription *shared.PlanDescription+if len(files) == 0 {- planDescription := &shared.PlanDescription{+ planDescription = &shared.PlanDescription{Files: []string{},MadePlan: false,ResponseTimestamp: responseTs,}- bytes, err := json.Marshal(planDescription)+ } else {+ planDescription, err = genPlanDescriptionJson(proposalId, ctx)if err != nil {- onError(fmt.Errorf("failed to marshal plan description: %v", err))+ onError(fmt.Errorf("failed to generate plan description json: %v", err))return}- planDescriptionJson := string(bytes)- proposals.Update(proposalId, func(proposal *types.Proposal) {- proposal.Finish(planDescription)- })-- fmt.Println(planDescriptionJson)-- onStream(planDescriptionJson, nil)- onStream(shared.STREAM_FINISHED, nil)- return+ planDescription.MadePlan = true+ planDescription.Files = files+ planDescription.ResponseTimestamp = responseTs}- planDescription, err := genPlanDescriptionJson(proposalId, ctx)- if err != nil {- onError(fmt.Errorf("failed to generate plan description json: %v", err))- return+ if summary != nil {+ planDescription.SummarizedToTimestamp = summary.LastMessageTimestamp}- planDescription.MadePlan = true- planDescription.Files = files- planDescription.ResponseTimestamp = responseTs-bytes, err := json.Marshal(planDescription)if err != nil {onError(fmt.Errorf("failed to marshal plan description: %v", err))return}planDescriptionJson := string(bytes)++ fmt.Println("Plan description json:")fmt.Println(planDescriptionJson)proposals.Update(proposalId, func(proposal *types.Proposal) {@@ -384,12 +395,16 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) erroronStream(planDescriptionJson, nil)- onStream(shared.STREAM_BUILD_PHASE, nil)- err = confirmProposal(proposalId, fileContents, numTokensByFile, onStream)- if err != nil {- onError(fmt.Errorf("failed to confirm proposal: %v", err))+ if len(files) == 0 {+ onStream(planDescriptionJson, nil)+ onStream(shared.STREAM_FINISHED, nil)+ } else {+ onStream(shared.STREAM_BUILD_PHASE, nil)+ err = buildPlan(proposalId, fileContents, numTokensByFile, onStream)+ if err != nil {+ onError(fmt.Errorf("failed to confirm proposal: %v", err))+ }}-return}diff --git a/server/model/proposal/lorem_ipsum.go b/server/model/proposal/lorem_ipsum.goindex cf5aa965..a3cb11d6 100644--- a/server/model/proposal/lorem_ipsum.go+++ b/server/model/proposal/lorem_ipsum.go@@ -63,7 +63,7 @@ func streamLoremIpsum(onStream types.OnStreamFunc) {onStream(planDescriptionJson, nil)onStream(shared.STREAM_BUILD_PHASE, nil)- confirmProposal("", map[string]string{}, map[string]int{}, onStream)+ buildPlan("", map[string]string{}, map[string]int{}, onStream)}diff --git a/server/model/summarize.go b/server/model/summarize.goindex 2a83f168..f0dfc491 100644--- a/server/model/summarize.go+++ b/server/model/summarize.go@@ -77,26 +77,6 @@ func ShortSummary(text string) ([]byte, error) {return byteRes, nil}-const convoSummaryPrompt = `-Based on the conversation so far, make a summary of the current state of the plan.--- It should begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages. If the user has sent messages like 'continue to the next step of the plan' that don't contain any new information relevant to the plan, you should omit them.--- Next, if the plan includes a statement from the assistant to the effect of "I will break this large task into subtasks" and the plan has been broken down into subtasks, include those subtasks in the summary as a numbered list. Condense these as much as possible while still being clear and retaining the meaning of each subtask. At the end of the list, state which subtask is currently being worked on (unless they are all finished, in which case state that they are all finished).--- Last, summarize the latest version of the plan and any changes you have suggested. If the some of the older changes have been overridden by newer changes, you should only include the newest changes and omit the older ones.--- If your responses include code blocks labelled with file paths, include the latest state of your modifications to each file's code and label them with file paths in the same format as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.--- If code blocks can be converted to a textual summary without losing meaning or precision, summarize them. Otherwise, leave them as code.--- If part of the plan has already been summarized, update the summary based on the latest messages while continuing the follow the above guidelines.--- The summary should condense the conversation to save tokens, but still contain enough information about the state of the plan to be actionable and apply the suggested changes.--Output only the summary of the current state of the plan and nothing else.-`-func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimestamp string) (*shared.ConversationSummary, error) {messages := []openai.ChatCompletionMessage{{@@ -112,10 +92,14 @@ func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimestaContent: convoSummaryPrompt,})+ // fmt.Println("summarizing messages:")+ // spew.Dump(messages)+resp, err := Client.CreateChatCompletion(context.Background(),openai.ChatCompletionRequest{- Model: openai.GPT4,+ Model: openai.GPT4,+ // Model: openai.GPT3Dot5Turbo16K,Messages: messages,},)@@ -127,7 +111,7 @@ func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimesta}if len(resp.Choices) == 0 {- return nil, fmt.Errorf("no response from GPT-4")+ return nil, fmt.Errorf("no response from GPT")}content := resp.Choices[0].Message.Content@@ -139,3 +123,23 @@ func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimesta}, nil}++const convoSummaryPrompt = `+Based on the conversation so far, make a summary of the current state of the plan.++- It should begin with a summary of the user's messages, with particular focus any tasks they have given you. Your summary of the tasks should reflect the latest version of each task--if they have changed over time, summarize the latest state of each task that was given and omit anything that is now obsolete. Condense this information as much as possible while still being clear and retaining the meaning of the original messages. If the user has sent messages like 'continue to the next step of the plan' that don't contain any new information relevant to the plan, you should omit them.++- Next, if the plan includes a statement from the assistant to the effect of "I will break this large task into subtasks" and the plan has been broken down into subtasks, include those subtasks in the summary as a numbered list. Condense these as much as possible while still being clear and retaining the meaning of each subtask. At the end of the list, state which subtask is currently being worked on (unless they are all finished, in which case state that they are all finished).++- Last, summarize the latest version of the plan and any changes you have suggested. If the some of the older changes have been overridden by newer changes, you should only include the newest changes and omit the older ones.++- If your responses include code blocks labelled with file paths, include the latest state of your modifications to each file's code and label them with file paths in the same format as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.++- If code blocks can be converted to a textual summary without losing meaning or precision, summarize them. Otherwise, leave them as code.++- If part of the plan has already been summarized, update the summary based on the latest messages while continuing the follow the above guidelines.++- The summary should condense the conversation to save tokens, but still contain enough information about the state of the plan to be actionable and apply the suggested changes.++Output only the summary of the current state of the plan and nothing else.+`diff --git a/shared/types.go b/shared/types.goindex b118aa32..1330a214 100644--- a/shared/types.go+++ b/shared/types.go@@ -41,14 +41,27 @@ type FileNameResponse struct {FileName string `json:"fileName"`}+type ModelContextPartType string++const (+ ContextFileType ModelContextPartType = "file"+ ContextURLType ModelContextPartType = "url"+ ContextNoteType ModelContextPartType = "note"+ ContextDirectoryType ModelContextPartType = "directory"+ ContextDirectoryTreeType ModelContextPartType = "directory tree"+ ContextPipedDataType ModelContextPartType = "piped data"+)+type ModelContextPart struct {- Name string `json:"name"`- Body string `json:"body"`- Url string `json:"url"`- FilePath string `json:"filePath"`- Sha string `json:"sha"`- NumTokens int `json:"numTokens"`- UpdatedAt string `json:"updatedAt"`+ Type ModelContextPartType `json:"type"`+ Name string `json:"name"`+ Body string `json:"body"`+ Url string `json:"url"`+ FilePath string `json:"filePath"`+ Sha string `json:"sha"`+ NumTokens int `json:"numTokens"`+ AddedAt string `json:"addedAt"`+ UpdatedAt string `json:"updatedAt"`}type ModelContext []ModelContextPart@@ -59,10 +72,11 @@ type PlanChunk struct {}type PlanDescription struct {- MadePlan bool `json:"madePlan"`- CommitMsg string `json:"commitMsg"`- Files []string `json:"files"`- ResponseTimestamp string `json:"responseTimestamp"`+ MadePlan bool `json:"madePlan"`+ CommitMsg string `json:"commitMsg"`+ Files []string `json:"files"`+ ResponseTimestamp string `json:"responseTimestamp"`+ SummarizedToTimestamp string `json:"summarizedToTimestamp"`}type ConversationSummary struct {diff --git a/shared/utils.go b/shared/utils.goindex e66fb4b4..ac3c4220 100644--- a/shared/utils.go+++ b/shared/utils.go@@ -2,6 +2,8 @@ package sharedimport "time"+const TsFormat = "2006-01-02T15:04:05.999Z"+func StringTs() string {- return time.Now().Format("2006-01-02T15:04:05.999Z")+ return time.Now().UTC().Format(TsFormat)}