Skip to content

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

some reorg, rm command

Dane Schneidere9193c54b014
diff --git a/cli/cmd/context.go b/cli/cmd/context.go
deleted file mode 100644
index 93a05307..00000000
--- a/cli/cmd/context.go
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
-package cmd
-
-import (
- "fmt"
-
- "github.com/spf13/cobra"
-)
-
-// contextCmd represents the context command
-var contextCmd = &cobra.Command{
- Use: "context",
- Short: "A brief description of your command",
- Long: `A longer description that spans multiple lines and likely contains examples
-and usage of using your command. For example:
-
-Cobra is a CLI library for Go that empowers applications.
-This application is a tool to generate the needed files
-to quickly create a Cobra application.`,
- Run: func(cmd *cobra.Command, args []string) {
- fmt.Println("context called")
- },
-}
-
-func init() {
- RootCmd.AddCommand(contextCmd)
-
- // Here you will define your flags and configuration settings.
-
- // Cobra supports Persistent Flags which will work for this command
- // and all subcommands, e.g.:
- // contextCmd.PersistentFlags().String("foo", "", "A help for foo")
-
- // Cobra supports local flags which will only run when this command
- // is called directly, e.g.:
- // contextCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
-}
diff --git a/cli/cmd/context_ls.go b/cli/cmd/context_ls.go
deleted file mode 100644
index 7d29e4da..00000000
--- a/cli/cmd/context_ls.go
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
-package cmd
-
-import (
- "fmt"
- "os"
- "plandex/lib"
-
- "github.com/spf13/cobra"
-)
-
-// contextLsCmd represents the ls command
-var contextLsCmd = &cobra.Command{
- Use: "ls",
- Aliases: []string{"list"},
- Short: "",
- Long: ``,
- Run: contextLs,
-}
-
-func contextLs(cmd *cobra.Command, args []string) {
- context, err := lib.GetAllContext(true)
-
- if err != nil {
- fmt.Fprintln(os.Stderr, "Error listing context:", err)
- os.Exit(1)
- }
-
- totalTokens := 0
-
- for i, part := range context {
- totalTokens += part.NumTokens
-
- if i != 0 {
- fmt.Print("\n")
- }
-
- fmt.Println("Index:", i)
- if part.FilePath != "" {
- fmt.Printf("File: %s\n", part.FilePath)
- }
-
- if part.Url != "" {
- fmt.Printf("Url: %s\n", part.Url)
- }
-
- fmt.Printf("Tokens: %d\n", part.NumTokens)
- fmt.Printf("Updated: %s\n", part.UpdatedAt)
- }
-
- fmt.Printf("\nTotal tokens: %d\n", totalTokens)
-
-}
-
-func init() {
- contextCmd.AddCommand(contextLsCmd)
-
-}
diff --git a/cli/cmd/context_rm.go b/cli/cmd/context_rm.go
deleted file mode 100644
index 5ef663a0..00000000
--- a/cli/cmd/context_rm.go
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
-package cmd
-
-import (
- "log"
- "os"
- "path/filepath"
- "plandex/lib"
-
- "github.com/spf13/cobra"
-)
-
-// contextRmCmd represents the delete command
-var contextRmCmd = &cobra.Command{
- Use: "rm [indices-or-files-or-urls...]",
- Short: "Remove context by index, filename, or url.",
- Args: cobra.MinimumNArgs(1),
- Run: contextRm,
-}
-
-func contextRm(cmd *cobra.Command, args []string) {
-
- contextStateFilePath := filepath.Join(lib.ContextSubdir, "context.json")
-
- contextStateFile, err := os.Open(contextStateFilePath)
- if err != nil {
- log.Fatalf("Error opening file: %v", err)
- }
- defer contextStateFile.Close()
-
-}
-
-func init() {
- contextCmd.AddCommand(contextRmCmd)
-
-}
diff --git a/cli/cmd/context_update.go b/cli/cmd/context_update.go
deleted file mode 100644
index c97a2b38..00000000
--- a/cli/cmd/context_update.go
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
-package cmd
-
-import (
- "fmt"
-
- "github.com/spf13/cobra"
-)
-
-// contextUpdateCmd represents the context command
-var contextUpdateCmd = &cobra.Command{
- Use: "update [file-or-urls...]",
- Short: "A brief description of your command",
- Long: `A longer description that spans multiple lines and likely contains examples
-and usage of using your command. For example:
-
-Cobra is a CLI library for Go that empowers applications.
-This application is a tool to generate the needed files
-to quickly create a Cobra application.`,
- Run: func(cmd *cobra.Command, args []string) {
- fmt.Println("context called")
- },
-}
-
-func init() {
- contextCmd.AddCommand(contextUpdateCmd)
-
- // Here you will define your flags and configuration settings.
-
- // Cobra supports Persistent Flags which will work for this command
- // and all subcommands, e.g.:
- // contextUpdateCmd.PersistentFlags().String("foo", "", "A help for foo")
-
- // Cobra supports local flags which will only run when this command
- // is called directly, e.g.:
- // contextUpdateCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
-}
diff --git a/cli/cmd/delete_plan.go b/cli/cmd/delete_plan.go
new file mode 100644
index 00000000..1c859198
--- /dev/null
+++ b/cli/cmd/delete_plan.go
@@ -0,0 +1,88 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "plandex/lib"
+
+ "github.com/fatih/color"
+ "github.com/spf13/cobra"
+)
+
+var all bool
+
+func init() {
+ rmCmd.Flags().BoolVar(&all, "all", false, "Delete all plans and clear the current plan")
+ RootCmd.AddCommand(rmCmd)
+}
+
+// rmCmd represents the rm command
+var rmCmd = &cobra.Command{
+ Use: "delete-plan [name]",
+ Aliases: []string{"del"},
+ Short: "Delete the specified plan",
+ Args: cobra.RangeArgs(0, 1),
+ Run: del,
+}
+
+func del(cmd *cobra.Command, args []string) {
+ if all {
+ delAll()
+ return
+ }
+
+ name := args[0]
+ plandexDir, _, err := lib.FindOrCreatePlandex()
+
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error:", err)
+ return
+ }
+
+ planDir := filepath.Join(plandexDir, name)
+
+ if _, err := os.Stat(planDir); os.IsNotExist(err) {
+ fmt.Fprintf(os.Stderr, "Plan %s does not exist", color.New(color.Bold).Sprint(name))
+ return
+ }
+
+ err = os.RemoveAll(planDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error deleting the plan:", err)
+ return
+ }
+
+ if lib.CurrentPlanName == name {
+ err = os.Remove(filepath.Join(plandexDir, "current_plan.json"))
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error deleting current plan:", err)
+ return
+ }
+ }
+
+ fmt.Printf("✅ Deleted plan %s\n", color.New(color.Bold).Sprint(name))
+}
+
+func delAll() {
+ plandexDir, _, err := lib.FindOrCreatePlandex()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error:", err)
+ return
+ }
+
+ err = os.RemoveAll(plandexDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error deleting all the plans:", err)
+ return
+ }
+
+ err = os.Mkdir(plandexDir, os.ModePerm)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error creating .plandex folder:", err)
+ return
+ }
+
+ fmt.Fprintln(os.Stderr, "✅ All plans have been deleted.")
+}
diff --git a/cli/cmd/load.go b/cli/cmd/load.go
index b227e447..3292a963 100644
--- a/cli/cmd/load.go
+++ b/cli/cmd/load.go
@@ -20,7 +20,7 @@ var contextLoadCmd = &cobra.Command{
Use: "load [files-or-urls...]",
Aliases: []string{"l"},
Short: "Load context from various inputs",
- Long: `Load context from a file path, a directory, a URL, text, or piped data.`,
+ Long: `Load context from a file path, a directory, a URL, a string, or piped data.`,
Args: cobra.MinimumNArgs(1),
Run: contextLoad,
}
diff --git a/cli/cmd/ls.go b/cli/cmd/ls.go
index f19d75ca..52ce69cf 100644
--- a/cli/cmd/ls.go
+++ b/cli/cmd/ls.go
@@ -3,42 +3,53 @@ package cmd
import (
"fmt"
"os"
-
"plandex/lib"
"github.com/spf13/cobra"
)
-func init() {
- RootCmd.AddCommand(lsCmd)
-}
-
-// lsCmd represents the list command
-var lsCmd = &cobra.Command{
+var contextCmd = &cobra.Command{
Use: "ls",
- Aliases: []string{"list"},
- Short: "List all available plans",
- Run: ls,
+ Aliases: []string{"ls"},
+ Short: "List everything in context",
+ Run: context,
}
-func ls(cmd *cobra.Command, args []string) {
- plandexDir, _, err := lib.FindOrCreatePlandex()
+func context(cmd *cobra.Command, args []string) {
+ context, err := lib.GetAllContext(true)
if err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
- return
+ fmt.Fprintln(os.Stderr, "Error listing context:", err)
+ os.Exit(1)
}
- plans, err := os.ReadDir(plandexDir)
- if err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
- return
- }
+ totalTokens := 0
+
+ for i, part := range context {
+ totalTokens += part.NumTokens
+
+ if i != 0 {
+ fmt.Print("\n")
+ }
- fmt.Println("Available plans:")
- for _, p := range plans {
- if p.IsDir() {
- fmt.Println("-", p.Name())
+ fmt.Println("Index:", i)
+ if part.FilePath != "" {
+ fmt.Printf("File: %s\n", part.FilePath)
}
+
+ if part.Url != "" {
+ fmt.Printf("Url: %s\n", part.Url)
+ }
+
+ fmt.Printf("Tokens: %d\n", part.NumTokens)
+ fmt.Printf("Updated: %s\n", part.UpdatedAt)
}
+
+ fmt.Printf("\nTotal tokens: %d\n", totalTokens)
+
+}
+
+func init() {
+ RootCmd.AddCommand(contextCmd)
+
}
diff --git a/cli/cmd/new.go b/cli/cmd/new.go
index 9d47820d..1e303dc8 100644
--- a/cli/cmd/new.go
+++ b/cli/cmd/new.go
@@ -1,6 +1,3 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
package cmd
import (
diff --git a/cli/cmd/next.go b/cli/cmd/next.go
index cec915e6..f59baae9 100644
--- a/cli/cmd/next.go
+++ b/cli/cmd/next.go
@@ -1,6 +1,3 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
package cmd
import (
@@ -32,4 +29,4 @@ func next(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "Prompt error:", err)
return
}
-}
\ No newline at end of file
+}
diff --git a/cli/cmd/plans.go b/cli/cmd/plans.go
new file mode 100644
index 00000000..cef2a14b
--- /dev/null
+++ b/cli/cmd/plans.go
@@ -0,0 +1,43 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ RootCmd.AddCommand(plansCmd)
+}
+
+// plansCmd represents the list command
+var plansCmd = &cobra.Command{
+ Use: "plans",
+ Short: "List all available plans",
+ Run: plans,
+}
+
+func plans(cmd *cobra.Command, args []string) {
+ plandexDir, _, err := lib.FindOrCreatePlandex()
+
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error:", err)
+ return
+ }
+
+ plans, err := os.ReadDir(plandexDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error:", err)
+ return
+ }
+
+ fmt.Println("Available plans:")
+ for _, p := range plans {
+ if p.IsDir() {
+ fmt.Println("-", p.Name())
+ }
+ }
+}
diff --git a/cli/cmd/rm.go b/cli/cmd/rm.go
index 40e5c041..4865e634 100644
--- a/cli/cmd/rm.go
+++ b/cli/cmd/rm.go
@@ -1,88 +1,128 @@
package cmd
import (
+ "encoding/json"
"fmt"
"os"
"path/filepath"
-
"plandex/lib"
+ "plandex/types"
- "github.com/fatih/color"
+ "github.com/plandex/plandex/shared"
"github.com/spf13/cobra"
)
-var all bool
-
-func init() {
- rmCmd.Flags().BoolVar(&all, "all", false, "Delete all plans and clear the current plan")
- RootCmd.AddCommand(rmCmd)
+var contextRmCmd = &cobra.Command{
+ Use: "rm",
+ Aliases: []string{"remove"},
+ Short: "Remove a specific context file",
+ Long: `This command allows the user to remove a specific context file by providing the file's index as an argument.`,
+ Args: cobra.MinimumNArgs(1),
+ Run: contextRm,
}
-// rmCmd represents the rm command
-var rmCmd = &cobra.Command{
- Use: "rm [name]",
- Aliases: []string{"delete"},
- Short: "Delete the specified plan",
- Args: cobra.RangeArgs(0, 1),
- Run: del,
-}
-
-func del(cmd *cobra.Command, args []string) {
- if all {
- delAll()
- return
- }
+func contextRm(cmd *cobra.Command, args []string) {
- name := args[0]
- plandexDir, _, err := lib.FindOrCreatePlandex()
+ context, err := lib.GetAllContext(true)
if err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
+ fmt.Fprintln(os.Stderr, "Error retrieving context:", err)
return
}
- planDir := filepath.Join(plandexDir, name)
-
- if _, err := os.Stat(planDir); os.IsNotExist(err) {
- fmt.Fprintf(os.Stderr, "Plan %s does not exist", color.New(color.Bold).Sprint(name))
- return
+ toRemovePaths := []string{}
+ toRemoveParts := []shared.ModelContextPart{}
+
+ for i, part := range context {
+ path := lib.CreateContextFileName(part.Name, part.Sha)
+ for _, id := range args {
+ if fmt.Sprintf("%d", i) == id || part.FilePath == id || part.Url == id {
+ toRemovePaths = append(toRemovePaths, path)
+ toRemoveParts = append(toRemoveParts, part)
+ break
+ } else if part.FilePath != "" {
+ // Check if id is a glob pattern
+ matched, err := filepath.Match(id, part.FilePath)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error matching glob pattern:", err)
+ return
+ }
+ if matched {
+ toRemovePaths = append(toRemovePaths, path)
+ toRemoveParts = append(toRemoveParts, part)
+ break
+ }
+ }
+ }
}
- err = os.RemoveAll(planDir)
- if err != nil {
- fmt.Fprintln(os.Stderr, "Error deleting the plan:", err)
- return
+ // 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)
+ }
}
- if lib.CurrentPlanName == name {
- err = os.Remove(filepath.Join(plandexDir, "current_plan.json"))
+ for i := 0; i < len(toRemovePaths)*2; i++ {
+ err := <-errCh
if err != nil {
- fmt.Fprintln(os.Stderr, "Error deleting current plan:", err)
+ fmt.Fprintln(os.Stderr, "Error removing context file:", err)
return
}
}
- fmt.Printf("✅ Deleted plan %s\n", color.New(color.Bold).Sprint(name))
-}
+ // update context.json with new token count
+ var contextState types.ModelContextState
-func delAll() {
- plandexDir, _, err := lib.FindOrCreatePlandex()
+ contextStateFilePath := filepath.Join(lib.ContextSubdir, "context.json")
+ bytes, err := os.ReadFile(contextStateFilePath)
if err != nil {
- fmt.Fprintln(os.Stderr, "Error:", err)
+ fmt.Fprintln(os.Stderr, "Error reading context state file:", err)
return
}
- err = os.RemoveAll(plandexDir)
+ err = json.Unmarshal(bytes, &contextState)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error unmarshalling context state:", err)
+ return
+ }
+
+ removedTokens := 0
+ totalTokens := contextState.NumTokens
+ for _, part := range toRemoveParts {
+ removedTokens += part.NumTokens
+ totalTokens -= part.NumTokens
+ }
+ contextState.NumTokens = totalTokens
+
+ bytes, err = json.MarshalIndent(contextState, "", " ")
if err != nil {
- fmt.Fprintln(os.Stderr, "Error deleting all the plans:", err)
+ fmt.Fprintln(os.Stderr, "Error marshalling context state:", err)
return
}
- err = os.Mkdir(plandexDir, os.ModePerm)
+ err = os.WriteFile(contextStateFilePath, bytes, 0644)
if err != nil {
- fmt.Fprintln(os.Stderr, "Error creating .plandex folder:", err)
+ fmt.Fprintln(os.Stderr, "Error writing context state file:", err)
return
}
- fmt.Fprintln(os.Stderr, "✅ All plans have been deleted.")
+ // output
+ if len(toRemovePaths) > 0 {
+ suffix := ""
+ if len(toRemovePaths) > 1 {
+ suffix = "s"
+ }
+ fmt.Printf("✅ Removed %d piece%s of context | removed → %d 🪙 | total → %d 🪙 \n", len(toRemovePaths), suffix, removedTokens, totalTokens)
+ } else {
+ fmt.Println("🤷‍♂️ No context removed")
+ }
+
+}
+
+func init() {
+ RootCmd.AddCommand(contextRmCmd)
}
diff --git a/cli/cmd/tell.go b/cli/cmd/tell.go
index a6de4476..a6df43d8 100644
--- a/cli/cmd/tell.go
+++ b/cli/cmd/tell.go
@@ -1,6 +1,3 @@
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
package cmd
import (
diff --git a/cli/go.mod b/cli/go.mod
index 61d60384..ee617537 100644
--- a/cli/go.mod
+++ b/cli/go.mod
@@ -3,6 +3,7 @@ module plandex
go 1.21.3
require (
+ github.com/davecgh/go-spew v1.1.1
github.com/fatih/color v1.13.0
github.com/looplab/fsm v1.0.1
github.com/spf13/cobra v1.7.0
diff --git a/cli/lib/context_fs.go b/cli/lib/context_fs.go
index 9acbbb47..045f7705 100644
--- a/cli/lib/context_fs.go
+++ b/cli/lib/context_fs.go
@@ -5,13 +5,14 @@ import (
"fmt"
"os"
"path/filepath"
+ "plandex/types"
"sync"
"github.com/plandex/plandex/shared"
)
func WriteInitialContextState(contextDir string) error {
- contextState := shared.ModelContextState{
+ contextState := types.ModelContextState{
NumTokens: 0,
}
contextStateFilePath := filepath.Join(contextDir, "context.json")
@@ -31,8 +32,8 @@ func WriteInitialContextState(contextDir string) error {
return err
}
-// createContextFileName constructs a filename based on the given name and sha.
-func createContextFileName(name, sha string) string {
+// 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
shaSubstring := sha[:8]
return fmt.Sprintf("%s.%s", name, shaSubstring)
@@ -40,10 +41,10 @@ func createContextFileName(name, sha string) string {
// writeContextPartToFile writes a single ModelContextPart to a file.
func writeContextPartToFile(part shared.ModelContextPart) error {
- metaFilename := createContextFileName(part.Name, part.Sha) + ".meta"
+ metaFilename := CreateContextFileName(part.Name, part.Sha) + ".meta"
metaPath := filepath.Join(ContextSubdir, metaFilename)
- bodyFilename := createContextFileName(part.Name, part.Sha) + ".body"
+ bodyFilename := CreateContextFileName(part.Name, part.Sha) + ".body"
bodyPath := filepath.Join(ContextSubdir, bodyFilename)
body := []byte(part.Body)
part.Body = ""
diff --git a/cli/lib/context_load.go b/cli/lib/context_load.go
index 8be27c19..4bf77e8d 100644
--- a/cli/lib/context_load.go
+++ b/cli/lib/context_load.go
@@ -13,7 +13,6 @@ import (
"plandex/types"
"strings"
"sync"
- "sync/atomic"
"time"
"github.com/briandowns/spinner"
@@ -26,7 +25,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)
s.Prefix = "📥 Loading context... "
s.Start()
- var contextState shared.ModelContextState
+ var contextState types.ModelContextState
contextStateFilePath := filepath.Join(ContextSubdir, "context.json")
// fmt.Fprintf(os.Stderr, "Loading context from %s\n", contextStateFilePath)
@@ -64,8 +63,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
totalTokens := contextState.NumTokens
var totalTokensMutex sync.Mutex
- counter := contextState.Counter
-
var contextParts []shared.ModelContextPart
var contextPartsMutex sync.Mutex
@@ -118,7 +115,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
fileName := GetFileNameWithoutExt(fileNameResp.FileName)
contextPart := shared.ModelContextPart{
- Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Name: fileName,
Body: body,
Sha: sha,
NumTokens: numTokens,
@@ -129,7 +126,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
contextParts = append(contextParts, contextPart)
contextPartsMutex.Unlock()
- atomic.AddUint32(&counter, 1)
}()
}
@@ -192,7 +188,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
fileName := GetFileNameWithoutExt(fileNameResp.FileName)
contextPart := shared.ModelContextPart{
- Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Name: fileName,
Body: body,
Sha: sha,
NumTokens: numTokens,
@@ -203,7 +199,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
contextParts = append(contextParts, contextPart)
contextPartsMutex.Unlock()
- atomic.AddUint32(&counter, 1)
}()
}
}
@@ -268,7 +263,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
}()
contextPart := shared.ModelContextPart{
- Name: fmt.Sprintf("%d-directory-layout", atomic.LoadUint32(&counter)),
+ Name: "directory layout",
Body: body,
Sha: sha,
NumTokens: numTokens,
@@ -279,8 +274,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
contextParts = append(contextParts, contextPart)
contextPartsMutex.Unlock()
- atomic.AddUint32(&counter, 1)
-
} else {
for _, path := range flattenedPaths {
wg.Add(1)
@@ -328,7 +321,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
_, fileName := filepath.Split(path)
contextPart := shared.ModelContextPart{
- Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Name: fileName,
Body: body,
FilePath: path,
Sha: sha,
@@ -340,7 +333,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
contextParts = append(contextParts, contextPart)
contextPartsMutex.Unlock()
- atomic.AddUint32(&counter, 1)
}(path)
}
@@ -396,7 +388,7 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
// }
contextPart := shared.ModelContextPart{
- Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), SanitizeAndClipURL(url, 70)),
+ Name: SanitizeAndClipURL(url, 70),
Body: body,
Sha: sha,
NumTokens: numTokens,
@@ -406,8 +398,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
contextPartsMutex.Lock()
contextParts = append(contextParts, contextPart)
contextPartsMutex.Unlock()
-
- atomic.AddUint32(&counter, 1)
}(url)
}
}
@@ -431,7 +421,6 @@ func LoadContextOrDie(params *types.LoadContextParams) (int, int) {
go func() {
defer wg.Done()
contextState.NumTokens = totalTokens
- contextState.Counter = counter
data, err := json.MarshalIndent(contextState, "", " ")
if err != nil {
diff --git a/cli/lib/stream.go b/cli/lib/stream.go
index a89c4fda..87aa7ffa 100644
--- a/cli/lib/stream.go
+++ b/cli/lib/stream.go
@@ -33,17 +33,6 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
return false, fmt.Errorf("error parsing plan chunk: %v", err)
}
- // _, sectionNum, err := shared.SplitSectionPath(chunk.Path)
- // if err != nil {
- // return false, fmt.Errorf("error parsing section number: %v", err)
- // }
- // isSection := sectionNum >= 0
-
- // fmt.Println("path: " + chunk.Path)
- // fmt.Println("isSection: " + fmt.Sprintf("%t", isSection))
- // fmt.Println("sectionNum: " + fmt.Sprintf("%d", sectionNum))
- // fmt.Println("filePath: " + filePath)
-
buffer := jsonBuffers[chunk.Path]
buffer += chunk.Content
jsonBuffers[chunk.Path] = buffer
@@ -77,9 +66,7 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
// log.Println("Parsed JSON. Streamed type: " + streamedType)
- var writeToPath string
-
- writeToPath = filepath.Join(PlanFilesDir, chunk.Path)
+ writeToPath := filepath.Join(PlanFilesDir, chunk.Path)
var content string
@@ -94,9 +81,9 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
bytes, err := os.ReadFile(writeToPath)
if err != nil {
- err = fmt.Errorf("failed to read file '%s': %v\n", writeToPath, err)
+ err = fmt.Errorf("failed to read file '%s': %v", writeToPath, err)
log.Println(err)
- return false, fmt.Errorf("failed to read file '%s': %v\n", writeToPath, err)
+ return false, fmt.Errorf("failed to read file '%s': %v", writeToPath, err)
}
content = string(bytes)
@@ -104,9 +91,9 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
bytes, err := os.ReadFile(filepath.Join(ProjectRoot, chunk.Path))
if err != nil {
- err = fmt.Errorf("failed to read file '%s': %v\n", chunk.Path, err)
+ err = fmt.Errorf("failed to read file '%s': %v", chunk.Path, err)
log.Println(err)
- return false, fmt.Errorf("failed to read file '%s': %v\n", chunk.Path, err)
+ return false, fmt.Errorf("failed to read file '%s': %v", chunk.Path, err)
}
content = string(bytes)
@@ -132,7 +119,7 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
sub := content[lastInsertedIdx:]
idx := strings.Index(sub, replacement.Old)
if idx == -1 {
- err = fmt.Errorf("failed to find replacement string '%s' in file '%s'\n", replacement.Old, chunk.Path)
+ err = fmt.Errorf("failed to find replacement string '%s' in file '%s'", replacement.Old, chunk.Path)
log.Println(err)
return false, err
}
@@ -163,20 +150,20 @@ func receiveFileToken(params *receiveFileChunkParams) (bool, error) {
err := os.MkdirAll(filepath.Dir(writeToPath), os.ModePerm)
if err != nil {
- log.Printf("failed to create directory: %s\n", err)
- return false, fmt.Errorf("failed to create directory: %s\n", err)
+ log.Printf("failed to create directory: %s", err)
+ return false, fmt.Errorf("failed to create directory: %s", err)
}
err = os.WriteFile(writeToPath, []byte(content), 0644)
if err != nil {
- log.Printf("failed to write plan file '%s': %v\n", writeToPath, err)
- return false, fmt.Errorf("failed to write plan file '%s': %v\n", writeToPath, err)
+ log.Printf("failed to write plan file '%s': %v", writeToPath, err)
+ return false, fmt.Errorf("failed to write plan file '%s': %v", writeToPath, err)
}
err = writeTokenCounts(&chunk, numStreamedTokensByPath)
if err != nil {
- return false, fmt.Errorf("failed to write token counts: %s\n", err)
+ return false, fmt.Errorf("failed to write token counts: %s", err)
}
finishedByPath[chunk.Path] = true
@@ -237,11 +224,11 @@ func loadCurrentPlanTokensByFilePath() (map[string]int, error) {
} else {
fileBytes, err := os.ReadFile(currrentPlanTokensPath)
if err != nil {
- return nil, fmt.Errorf("failed to open current plan token count file: %s\n", err)
+ return nil, fmt.Errorf("failed to open current plan token count file: %s", err)
}
err = json.Unmarshal(fileBytes, &currentPlanTokensByPath)
if err != nil {
- return nil, fmt.Errorf("failed to parse current plan token count json: %s\n", err)
+ return nil, fmt.Errorf("failed to parse current plan token count json: %s", err)
}
}
diff --git a/cli/lib/term.go b/cli/lib/term.go
index bf612888..443c0972 100644
--- a/cli/lib/term.go
+++ b/cli/lib/term.go
@@ -11,7 +11,7 @@ import (
var cmdDesc = map[string][2]string{
"new": {"", "start a new plan"},
- "load": {"l", "load files/urls or pipe data into context"},
+ "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"},
diff --git a/cli/types/types.go b/cli/types/types.go
index 77e9053c..3ef150d7 100644
--- a/cli/types/types.go
+++ b/cli/types/types.go
@@ -53,3 +53,7 @@ type PlanState struct {
type PlanSettings struct {
Name string
}
+
+type ModelContextState struct {
+ NumTokens int `json:"numTokens"`
+}
diff --git a/server/handlers/proposal.go b/server/handlers/proposal.go
index 1e6d87e5..0afe7322 100644
--- a/server/handlers/proposal.go
+++ b/server/handlers/proposal.go
@@ -126,42 +126,3 @@ func AbortProposalHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("Ok"))
}
-
-// func ReviseProposalHandler(w http.ResponseWriter, r *http.Request) {
-// log.Println("Received a request for ReviseProposalHandler")
-
-// // Read the request body
-// body, err := io.ReadAll(r.Body)
-// if err != nil {
-// log.Printf("Error reading request body: %v\n", err)
-// http.Error(w, "Error reading request body", http.StatusInternalServerError)
-// return
-// }
-// defer r.Body.Close()
-
-// // Unmarshal the request body to get the 'prompt'
-// var requestBody shared.PromptRequest
-// if err := json.Unmarshal(body, &requestBody); err != nil {
-// log.Printf("Error parsing request body: %v\n", err)
-// http.Error(w, "Error parsing request body", http.StatusBadRequest)
-// return
-// }
-
-// if requestBody.Prompt == "" {
-// log.Println("Received empty prompt field")
-// http.Error(w, "prompt field is required", http.StatusBadRequest)
-// return
-// }
-
-// modelRes, err := model.ReviseProposal(requestBody.ProposalId, requestBody.NewPrompt)
-// if err != nil {
-// log.Printf("Error revising proposal: %v\n", err)
-// http.Error(w, "Error revising proposal: "+err.Error(), http.StatusInternalServerError)
-// return
-// }
-
-// log.Println("Successfully revised proposal")
-
-// // Return the response from OpenAI to the client
-// w.Write(modelRes)
-// }
diff --git a/server/model/proposal/create.go b/server/model/proposal/create.go
index 0f5d6599..db8fa1f1 100644
--- a/server/model/proposal/create.go
+++ b/server/model/proposal/create.go
@@ -18,7 +18,7 @@ import (
"github.com/sashabaranov/go-openai"
)
-const systemMessageHead = `You are Plandex, an AI programming and system administration assistant. You and the programmer collaborate to create a 'plan' for the task at hand. A plan is a set of files with an attached context.` +
+const systemMessageHead = shared.IdentityPrompt + ` A plan is a set of files with an attached context.` +
"Your instructions:\n\n```\n" +
@@ -294,32 +294,3 @@ func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error
return nil
}
-
-// func summarizeConversationIfPossible(req *shared.PromptRequest) (*shared.PromptRequest, error) {
-// msgs := req.Conversation
-// if len(msgs) >= 2 {
-// for i := 1; i < len(msgs); i++ {
-// if msgs[i].Role == openai.ChatMessageRoleAssistant && !msgs[i].Summarized {
-// // assuming the summarize function takes a text and returns
-// // a summarization of that text
-// summary, err := model.Summarize(msgs[i].Content)
-// if err != nil {
-// return nil, err
-// }
-
-// // In case the message has multiple sentences. Concatenate the first
-// // two and add '... (summarized)' for clarity on UI.
-// sentences := strings.Split(summary, ".")
-// if len(sentences) > 2 {
-// summary = sentences[0] + ". " + sentences[1] + "... (summarized)"
-// }
-
-// msgs[i].Content = summary
-// msgs[i].Summarized = true
-// break
-// }
-// }
-// }
-// req.Conversation = msgs
-// return req, nil
-// }
diff --git a/server/model/proposal/state.go b/server/model/proposal/state.go
index ab65ef58..78a84bb6 100644
--- a/server/model/proposal/state.go
+++ b/server/model/proposal/state.go
@@ -5,6 +5,7 @@ import (
)
var (
- proposals types.SafeMap[types.Proposal] = *types.NewSafeMap[types.Proposal]()
- plans types.SafeMap[types.Plan] = *types.NewSafeMap[types.Plan]()
+ proposals types.SafeMap[types.Proposal] = *types.NewSafeMap[types.Proposal]()
+ plans types.SafeMap[types.Plan] = *types.NewSafeMap[types.Plan]()
+ convoSummaries types.SafeMap[types.ConversationSummary] = *types.NewSafeMap[types.ConversationSummary]()
)
diff --git a/server/model/summarize.go b/server/model/summarize.go
index 383edae9..d7f50ecd 100644
--- a/server/model/summarize.go
+++ b/server/model/summarize.go
@@ -77,11 +77,23 @@ func ShortSummary(text string) ([]byte, int, error) {
return byteRes, resp.Usage.CompletionTokens, 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.
+
+- 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.
+
+Output only the summary of the current state of the plan and nothing else.
+`
+
func PlanSummary(conversation []*shared.ConversationMessage) ([]byte, int, error) {
messages := []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
- Content: "You are Plandex, an AI programming and system administration assistant. You and the programmer collaborate to create a 'plan' for the task at hand.",
+ Content: shared.IdentityPrompt,
},
}
@@ -90,35 +102,14 @@ func PlanSummary(conversation []*shared.ConversationMessage) ([]byte, int, error
}
messages = append(messages, openai.ChatCompletionMessage{
- Role: openai.ChatMessageRoleUser,
- Content: `
- Based on the conversation so far, make a summary of the current state of the plan so far.
-
- - 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. Condense this information as much as possible while still being clear and retaining the meaning of the original messages.
-
- - 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 way as the original messages. Do not make new changes to the plan or to suggested code changes in your summary.
- `,
+ Role: openai.ChatMessageRoleUser,
+ Content: convoSummaryPrompt,
})
resp, err := Client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
- Model: openai.GPT4,
- Functions: []openai.FunctionDefinition{{
- Name: "summarize",
- Parameters: &jsonschema.Definition{
- Type: jsonschema.Object,
- Properties: map[string]jsonschema.Definition{
- "summary": {
- Type: jsonschema.String,
- Description: "A summary of the current state of the plan.",
- },
- },
- Required: []string{"summary"},
- },
- }},
+ Model: openai.GPT4,
Messages: messages,
},
)
diff --git a/server/types/conversation.go b/server/types/conversation.go
index 50fd2134..6299afbc 100644
--- a/server/types/conversation.go
+++ b/server/types/conversation.go
@@ -1,6 +1,6 @@
package types
-type Conversation struct {
+type ConversationSummary struct {
ProposalId string
Summary string
SummaryTokens int
diff --git a/shared/reply_test_examples/2.md b/shared/reply_test_examples/2.md
index c2bf218d..6eace5c3 100644
--- a/shared/reply_test_examples/2.md
+++ b/shared/reply_test_examples/2.md
@@ -3,9 +3,7 @@ Sure, here's how you could structure your 'context rm' command and 'context upda
**cmd/context_rm.go**
```go
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
+
package cmd
import (
@@ -59,9 +57,7 @@ func init() {
**cmd/context_update.go**
```go
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
+
package cmd
import (
diff --git a/shared/reply_test_examples/3.md b/shared/reply_test_examples/3.md
index c2bf218d..6eace5c3 100644
--- a/shared/reply_test_examples/3.md
+++ b/shared/reply_test_examples/3.md
@@ -3,9 +3,7 @@ Sure, here's how you could structure your 'context rm' command and 'context upda
**cmd/context_rm.go**
```go
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
+
package cmd
import (
@@ -59,9 +57,7 @@ func init() {
**cmd/context_update.go**
```go
-/*
-Copyright © 2023 NAME HERE <EMAIL ADDRESS>
-*/
+
package cmd
import (
diff --git a/shared/types.go b/shared/types.go
index 7028c032..5103e12f 100644
--- a/shared/types.go
+++ b/shared/types.go
@@ -49,11 +49,6 @@ type ModelContextPart struct {
}
type ModelContext []ModelContextPart
-type ModelContextState struct {
- NumTokens int `json:"numTokens"`
- Counter uint32 `json:"counter"`
-}
-
type PlanChunk struct {
Path string `json:"path"`
Content string `json:"content"`