Skip to content

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

initial commit--kinda works

Dane Schneider6c1363b3fc87
diff --git a/.envkey b/.envkey
new file mode 100644
index 00000000..72e10bad
--- /dev/null
+++ b/.envkey
@@ -0,0 +1 @@
+{"orgId":"7ec853d6-13f9-4758-a511-5bb1abaabae0","appId":"784e5009-ec25-4a80-9095-582bc829980d"}
\ No newline at end of file
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 00000000..8d3e62bb
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,3 @@
+.plandex/
+plandex
+plandex-server
diff --git a/cli/cmd/apply.go b/cli/cmd/apply.go
new file mode 100644
index 00000000..e3d25adc
--- /dev/null
+++ b/cli/cmd/apply.go
@@ -0,0 +1,132 @@
+package cmd
+
+import (
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ RootCmd.AddCommand(applyCmd)
+}
+
+var applyCmd = &cobra.Command{
+ Use: "apply [name]",
+ Short: "Apply a plan to the project",
+ Args: cobra.ExactArgs(1),
+ Run: apply,
+}
+
+func apply(cmd *cobra.Command, args []string) {
+ plandexDir, _, err := lib.FindOrCreatePlandex()
+ name := args[0]
+
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error:", err)
+ return
+ }
+
+ if name == "" {
+ name = lib.CurrentPlanName
+ }
+
+ if name == "" {
+ log.Fatalln("No plan name provided and no current plan is set.")
+ }
+
+ // Check git installed
+ if !lib.IsCommandAvailable("git") {
+ log.Fatalln("Error: git is required")
+ }
+
+ rootDir := filepath.Join(plandexDir, name)
+
+ if _, err := os.Stat(rootDir); os.IsNotExist(err) {
+ fmt.Fprintln(os.Stderr, "Error: plan with name '"+name+"' does not exist")
+ return
+ }
+
+ copiedAny := false
+ // Enumerate all paths in [planDir]/files
+ filesDir := filepath.Join(rootDir, "plan", "files")
+ err = filepath.Walk(filesDir, func(srcPath string, info os.FileInfo, err error) error {
+ if err != nil {
+ return err
+ }
+ if info.IsDir() {
+ return nil
+ }
+
+ // Compute relative path
+ relPath, err := filepath.Rel(filesDir, srcPath)
+ if err != nil {
+ return err
+ }
+
+ // Compute destination path
+ dstPath := filepath.Join(".", relPath)
+
+ // Copy the file
+ err = copyFile(srcPath, dstPath)
+ if err != nil {
+ return fmt.Errorf("failed to copy %s to %s: %w", srcPath, dstPath, err)
+ }
+ copiedAny = true
+ return nil
+ })
+
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error processing files:", err)
+ return
+ }
+
+ didExec := false
+ // If it exists, run [planDir]/exec.sh with cwd set to current process cwd
+ execPath := filepath.Join(rootDir, "exec.sh")
+ if _, err := os.Stat(execPath); err == nil {
+ // exec.sh exists, run it
+ cmd := exec.Command(execPath)
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+ cmd.Dir = "." // Current process CWD
+
+ err = cmd.Run()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error executing plan:", err)
+ return
+ }
+ didExec = true
+ }
+
+ if copiedAny || didExec {
+ fmt.Println("Plan applied successfully!")
+ } else {
+ fmt.Fprintf(os.Stderr, "This plan has no changes to apply.")
+ os.Exit(1)
+ }
+
+}
+
+// Utility function to copy files
+func copyFile(src, dst string) error {
+ srcFile, err := os.Open(src)
+ if err != nil {
+ return err
+ }
+ defer srcFile.Close()
+
+ dstFile, err := os.Create(dst)
+ if err != nil {
+ return err
+ }
+ defer dstFile.Close()
+
+ _, err = io.Copy(dstFile, srcFile)
+ return err
+}
diff --git a/cli/cmd/cd.go b/cli/cmd/cd.go
new file mode 100644
index 00000000..1ad53116
--- /dev/null
+++ b/cli/cmd/cd.go
@@ -0,0 +1,54 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ RootCmd.AddCommand(cdCmd)
+}
+
+var cdCmd = &cobra.Command{
+ Use: "cd [name]",
+ Aliases: []string{"set-plan"},
+ Short: "Change to a different plan",
+ 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)
+ }
+
+ 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)
+ os.Exit(1)
+ }
+ defer currentPlanFile.Close()
+
+ _, err = currentPlanFile.WriteString(fmt.Sprintf(`{"name": "%s"}`, name))
+ if err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+ fmt.Fprintln(os.Stderr, "✅ Changed current plan to "+name)
+}
diff --git a/cli/cmd/chat.go b/cli/cmd/chat.go
new file mode 100644
index 00000000..6a0c2a10
--- /dev/null
+++ b/cli/cmd/chat.go
@@ -0,0 +1,44 @@
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+// chatCmd represents the prompt command
+var chatCmd = &cobra.Command{
+ Use: "chat [prompt]",
+ Short: "",
+ Long: ``,
+ Run: chat,
+}
+
+func chat(cmd *cobra.Command, args []string) {
+ p := args[0]
+
+ err := lib.Prompt(p, true)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Prompt error:", err)
+ return
+ }
+}
+
+func init() {
+ RootCmd.AddCommand(chatCmd)
+
+ // Here you will define your flags and configuration settings.
+
+ // Cobra supports Persistent Flags which will work for this command
+ // and all subcommands, e.g.:
+ // chatCmd.PersistentFlags().String("foo", "", "A help for foo")
+
+ // Cobra supports local flags which will only run when this command
+ // is called directly, e.g.:
+ // chatCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}
diff --git a/cli/cmd/context.go b/cli/cmd/context.go
new file mode 100644
index 00000000..93a05307
--- /dev/null
+++ b/cli/cmd/context.go
@@ -0,0 +1,39 @@
+/*
+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
new file mode 100644
index 00000000..096cdbc6
--- /dev/null
+++ b/cli/cmd/context_ls.go
@@ -0,0 +1,56 @@
+/*
+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)
+ }
+
+ for i, part := range context {
+ if i != 0 {
+ fmt.Print("\n\n")
+ }
+
+ fmt.Printf("Name: %s\n", part.Name)
+
+ 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("Summary:\n%s", part.Summary)
+ }
+}
+
+func init() {
+ contextCmd.AddCommand(contextLsCmd)
+
+}
diff --git a/cli/cmd/context_rm.go b/cli/cmd/context_rm.go
new file mode 100644
index 00000000..31919474
--- /dev/null
+++ b/cli/cmd/context_rm.go
@@ -0,0 +1,26 @@
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "github.com/spf13/cobra"
+)
+
+// contextRmCmd represents the delete command
+var contextRmCmd = &cobra.Command{
+ Use: "rm",
+ Aliases: []string{"delete"},
+ Short: "",
+ Long: ``,
+ Run: contextRm,
+}
+
+func contextRm(cmd *cobra.Command, args []string) {
+
+}
+
+func init() {
+ contextCmd.AddCommand(contextRmCmd)
+
+}
diff --git a/cli/cmd/context_update.go b/cli/cmd/context_update.go
new file mode 100644
index 00000000..c97a2b38
--- /dev/null
+++ b/cli/cmd/context_update.go
@@ -0,0 +1,39 @@
+/*
+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/current.go b/cli/cmd/current.go
new file mode 100644
index 00000000..02c475a9
--- /dev/null
+++ b/cli/cmd/current.go
@@ -0,0 +1,22 @@
+package cmd
+
+import (
+ "fmt"
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+var currentCmd = &cobra.Command{
+ Use: "current",
+ Short: "Get the current plan",
+ Run: current,
+}
+
+func init() {
+ RootCmd.AddCommand(currentCmd)
+}
+
+func current(cmd *cobra.Command, args []string) {
+ fmt.Println(lib.CurrentPlanName)
+}
diff --git a/cli/cmd/load.go b/cli/cmd/load.go
new file mode 100644
index 00000000..0277718d
--- /dev/null
+++ b/cli/cmd/load.go
@@ -0,0 +1,38 @@
+package cmd
+
+import (
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+var note string
+
+var contextLoadCmd = &cobra.Command{
+ Use: "load [files-or-urls...]",
+ Short: "Load context from various inputs",
+ Long: `Load context from a file path, a directory, a URL, text, or piped data.`,
+ Args: cobra.RangeArgs(0, 1),
+ Run: contextLoad,
+}
+
+func init() {
+ addSharedContextFlags(contextLoadCmd)
+ contextLoadCmd.Flags().StringVarP(&note, "note", "n", "", "Add a note to the context")
+
+ // can be called via plandex load or plandex context load
+ RootCmd.AddCommand(contextLoadCmd)
+ contextCmd.AddCommand(contextLoadCmd)
+}
+
+func contextLoad(cmd *cobra.Command, args []string) {
+ lib.LoadContextOrDie(&lib.LoadContextParams{
+ Note: note,
+ MaxTokens: maxTokens,
+ Recursive: recursive,
+ MaxDepth: maxDepth,
+ NamesOnly: namesOnly,
+ Truncate: truncate,
+ Resources: args,
+ })
+}
diff --git a/cli/cmd/log.go b/cli/cmd/log.go
new file mode 100644
index 00000000..1d619dd0
--- /dev/null
+++ b/cli/cmd/log.go
@@ -0,0 +1 @@
+package cmd
diff --git a/cli/cmd/ls.go b/cli/cmd/ls.go
new file mode 100644
index 00000000..f19d75ca
--- /dev/null
+++ b/cli/cmd/ls.go
@@ -0,0 +1,44 @@
+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{
+ Use: "ls",
+ Aliases: []string{"list"},
+ Short: "List all available plans",
+ Run: ls,
+}
+
+func ls(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/new.go b/cli/cmd/new.go
new file mode 100644
index 00000000..7ea4f0e4
--- /dev/null
+++ b/cli/cmd/new.go
@@ -0,0 +1,210 @@
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "path/filepath"
+
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+var context []string
+var name string
+
+// newCmd represents the new command
+var newCmd = &cobra.Command{
+ Use: "new [prompt]",
+ Short: "",
+ Long: ``,
+ Args: cobra.RangeArgs(0, 1),
+ Run: new,
+}
+
+func init() {
+ addSharedContextFlags(newCmd)
+ newCmd.Flags().StringSliceVarP(&context, "context", "c", []string{}, "Context to load into the new plan (file paths or urls)")
+ newCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the new plan")
+ RootCmd.AddCommand(newCmd)
+}
+
+// const minActionDelay = 1000 * time.Millisecond
+
+func new(cmd *cobra.Command, args []string) {
+ var prompt string
+ if len(args) > 0 {
+ prompt = args[0]
+ }
+
+ if name == "" && prompt == "" {
+ log.Fatalln("Error: either prompt or --name/-n flag must be provided")
+ }
+
+ if name == "" {
+ summaryResp, err := lib.ApiSummarize(prompt)
+ if err != nil {
+ log.Fatalf("Failed to get a name for the prompt: %v", err)
+ }
+ name = lib.GetFileNameWithoutExt(summaryResp.FileName)
+ }
+
+ // Check git installed
+ if !lib.IsCommandAvailable("git") {
+ log.Fatalln("Error: git is required")
+ }
+ // fmt.Fprintln(os.Stderr, "✅ Confirmed git is installed")
+
+ // Search recursively upward for a .plandex directory
+ plandexDir, newd, err := lib.FindOrCreatePlandex()
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error finding or creating .plandex dir:", err)
+ return
+ }
+ if newd {
+ fmt.Fprintln(os.Stderr, "✅ Created .plandex directory at "+plandexDir)
+ } else {
+ fmt.Fprintln(os.Stderr, "✅ Found .plandex directory at "+plandexDir)
+ }
+
+ // If 'name' directory already exists, tack on an integer to differentiate
+ rootDir := filepath.Join(plandexDir, name)
+ _, err = os.Stat(rootDir)
+ exists := !os.IsNotExist(err)
+
+ postfix := 1
+ for exists {
+ postfix += 1
+ nameWithPostfix := fmt.Sprintf("%s.%d", name, postfix)
+ rootDir = filepath.Join(plandexDir, nameWithPostfix)
+ _, err = os.Stat(rootDir)
+ exists = !os.IsNotExist(err)
+ }
+
+ // Create current plan json file with current plan name
+ planFilePath := filepath.Join(plandexDir, "current_plan.json")
+ planFile, err := os.OpenFile(planFilePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error creating current_plan.json:", err)
+ return
+ }
+ defer planFile.Close()
+ _, err = planFile.WriteString(fmt.Sprintf(`{"name": "%s"}`, name))
+
+ // Create 'name' directory inside .plandex
+ err = os.Mkdir(rootDir, os.ModePerm)
+ if err != nil && !os.IsExist(err) {
+ fmt.Fprintln(os.Stderr, "Error creating plan dir:", err)
+ return
+ }
+ fmt.Fprintln(os.Stderr, "✅ Created plan at "+rootDir)
+
+ // Create context subdirectory
+ contextDir := filepath.Join(rootDir, "context")
+ err = os.Mkdir(contextDir, os.ModePerm)
+ if err != nil && !os.IsExist(err) {
+ fmt.Fprintln(os.Stderr, "Error creating context subdir:", err)
+ return
+ }
+ // fmt.Fprintln(os.Stderr, "✅ Created context directory at "+contextDir)
+
+ // Create plan subdirectory
+ planDir := filepath.Join(rootDir, "plan")
+ err = os.Mkdir(planDir, os.ModePerm)
+ if err != nil && !os.IsExist(err) {
+ fmt.Fprintln(os.Stderr, "Error creating plan subdir:", err)
+ return
+ }
+ fmt.Fprintln(os.Stderr, "✅ Created plan directory at "+planDir)
+
+ // Create conversation subdirectory
+ conversationDir := filepath.Join(rootDir, "conversation")
+ err = os.Mkdir(conversationDir, os.ModePerm)
+ if err != nil && !os.IsExist(err) {
+ fmt.Fprintln(os.Stderr, "Error creating conversation subdir:", err)
+ return
+ }
+ fmt.Fprintln(os.Stderr, "✅ Created conversation directory at "+conversationDir)
+
+ // Init empty git repositories in context, plan, and conversation directories
+ // init git repo in root plan dir
+ // fmt.Println("Initializing git repo in " + rootDir)
+ err = lib.InitGitRepo(rootDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error initializing git repo:", err)
+ return
+ }
+ fmt.Println("Adding git submodules to " + rootDir)
+
+ initAndAddSubmodule := func(dir string) {
+
+ // Initialize the Git repo in the directory
+ fmt.Println("Initializing git repo in " + dir)
+ err = lib.InitGitRepo(dir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error initializing git repo:", err)
+ return
+ }
+
+ // Add the directory as a submodule to the root repo
+ fmt.Println("Adding git submodule " + dir + " to " + rootDir)
+ err = lib.AddGitSubmodule(rootDir, dir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error adding git submodules:", err)
+ return
+ }
+ }
+ err = lib.WriteInitialContextState(contextDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error writing initial context state:", err)
+ return
+ }
+
+ fmt.Println("Wrote initial context state to " + contextDir)
+
+ initAndAddSubmodule(contextDir)
+ initAndAddSubmodule(planDir)
+ initAndAddSubmodule(conversationDir)
+
+ // After initializing and adding all submodules, ensure they're checked out correctly in the rootDir repo
+ // fmt.Println("Updating and initializing submodules in " + rootDir)
+ err = lib.UpdateAndInitSubmodules(rootDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error updating submodules:", err)
+ return
+ }
+
+ fmt.Fprintln(os.Stderr, "✅ Initialized context and plan git repositories")
+
+ err = lib.LoadCurrentPlan()
+
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error setting current plan:", err)
+ return
+ }
+
+ if len(context) > 0 {
+ lib.LoadContextOrDie(&lib.LoadContextParams{
+ Note: note,
+ MaxTokens: maxTokens,
+ Recursive: recursive,
+ MaxDepth: maxDepth,
+ NamesOnly: namesOnly,
+ Truncate: truncate,
+ Resources: context,
+ })
+ }
+
+ if prompt != "" {
+ err = lib.Prompt(prompt, false)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Prompt error:", err)
+ return
+ }
+ }
+
+}
diff --git a/cli/cmd/revert.go b/cli/cmd/revert.go
new file mode 100644
index 00000000..5445d56e
--- /dev/null
+++ b/cli/cmd/revert.go
@@ -0,0 +1,21 @@
+package cmd
+
+import (
+ "github.com/spf13/cobra"
+)
+
+var revertCmd = &cobra.Command{
+ Use: "revert [commit-sha]",
+ Short: "Revert plan to a previous commit",
+ Long: `Revert plan to a previous commit`,
+ Args: cobra.ExactArgs(1),
+ Run: revert,
+}
+
+func init() {
+ RootCmd.AddCommand(revertCmd)
+}
+
+func revert(cmd *cobra.Command, args []string) {
+
+}
diff --git a/cli/cmd/revise.go b/cli/cmd/revise.go
new file mode 100644
index 00000000..2c5111e9
--- /dev/null
+++ b/cli/cmd/revise.go
@@ -0,0 +1,44 @@
+/*
+Copyright © 2023 NAME HERE <EMAIL ADDRESS>
+*/
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+// reviseCmd represents the prompt command
+var reviseCmd = &cobra.Command{
+ Use: "revise [prompt]",
+ Short: "",
+ Long: ``,
+ Run: revise,
+}
+
+func revise(cmd *cobra.Command, args []string) {
+ p := args[0]
+
+ err := lib.Prompt(p, false)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Prompt error:", err)
+ return
+ }
+}
+
+func init() {
+ RootCmd.AddCommand(reviseCmd)
+
+ // Here you will define your flags and configuration settings.
+
+ // Cobra supports Persistent Flags which will work for this command
+ // and all subcommands, e.g.:
+ // reviseCmd.PersistentFlags().String("foo", "", "A help for foo")
+
+ // Cobra supports local flags which will only run when this command
+ // is called directly, e.g.:
+ // reviseCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
+}
diff --git a/cli/cmd/rm.go b/cli/cmd/rm.go
new file mode 100644
index 00000000..68c543e0
--- /dev/null
+++ b/cli/cmd/rm.go
@@ -0,0 +1,79 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+
+ "plandex/lib"
+
+ "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: "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
+ }
+
+ 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.Fprintln(os.Stderr, "Error: plan with name '"+name+"' does not exist")
+ return
+ }
+
+ err = os.RemoveAll(planDir)
+ if err != nil {
+ fmt.Fprintln(os.Stderr, "Error deleting the plan:", err)
+ return
+ }
+
+ fmt.Fprintln(os.Stderr, "✅ Plan '"+name+"' has been deleted.")
+}
+
+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/root.go b/cli/cmd/root.go
new file mode 100644
index 00000000..cb8788cc
--- /dev/null
+++ b/cli/cmd/root.go
@@ -0,0 +1,30 @@
+package cmd
+
+import (
+ "fmt"
+ "os"
+
+ "github.com/spf13/cobra"
+)
+
+// RootCmd represents the base command when called without any subcommands
+var RootCmd = &cobra.Command{
+ Use: `plandex [command] [flags]`,
+ Short: "Plandex CLI: generate, iterate, and execute code with AI",
+ Run: func(cmd *cobra.Command, args []string) {
+ run(cmd, args)
+ },
+}
+
+// Execute adds all child commands to the root command and sets flags appropriately.
+// This is called by main.main(). It only needs to happen once to the rootCmd.
+func Execute() {
+ if err := RootCmd.Execute(); err != nil {
+ fmt.Println(err)
+ os.Exit(1)
+ }
+}
+
+func run(cmd *cobra.Command, args []string) {
+
+}
diff --git a/cli/cmd/shared_context_flags.go b/cli/cmd/shared_context_flags.go
new file mode 100644
index 00000000..24739fc7
--- /dev/null
+++ b/cli/cmd/shared_context_flags.go
@@ -0,0 +1,24 @@
+package cmd
+
+import (
+ "plandex/lib"
+
+ "github.com/spf13/cobra"
+)
+
+var (
+ recursive bool
+ maxDepth int16
+ namesOnly bool
+ truncate bool
+ maxTokens uint32 = lib.MaxTokens
+)
+
+func addSharedContextFlags(cmd *cobra.Command) {
+
+ cmd.Flags().BoolVarP(&recursive, "recursive", "r", false, "Search directories recursively")
+ cmd.Flags().Int16VarP(&maxDepth, "max-depth", "d", -1, "Maximum depth for recursive directory search (-1 means no limit)")
+ cmd.Flags().BoolVar(&namesOnly, "names-only", false, "Only process file names")
+ cmd.Flags().BoolVar(&truncate, "truncate", false, "Truncate contents if tokens exceed maximum, otherwise abort")
+ cmd.Flags().Uint32Var(&maxTokens, "max-tokens", lib.MaxTokens, "Maximum limit for number of tokens")
+}
diff --git a/cli/cmd/status.go b/cli/cmd/status.go
new file mode 100644
index 00000000..1d619dd0
--- /dev/null
+++ b/cli/cmd/status.go
@@ -0,0 +1 @@
+package cmd
diff --git a/cli/go.mod b/cli/go.mod
new file mode 100644
index 00000000..a710f840
--- /dev/null
+++ b/cli/go.mod
@@ -0,0 +1,29 @@
+module plandex
+
+go 1.21.0
+
+require github.com/spf13/cobra v1.7.0
+
+require (
+ github.com/andybalholm/cascadia v1.3.1 // indirect
+ github.com/dlclark/regexp2 v1.10.0 // indirect
+ github.com/fatih/color v1.7.0 // indirect
+ github.com/google/uuid v1.3.0 // indirect
+ github.com/mattn/go-colorable v0.1.2 // indirect
+ github.com/mattn/go-isatty v0.0.8 // indirect
+ golang.org/x/net v0.7.0 // indirect
+ golang.org/x/sys v0.5.0 // indirect
+ golang.org/x/term v0.5.0 // indirect
+)
+
+require (
+ github.com/PuerkitoBio/goquery v1.8.1
+ github.com/briandowns/spinner v1.23.0
+ github.com/inconshreveable/mousetrap v1.1.0 // indirect
+ github.com/pkoukk/tiktoken-go v0.1.5
+ github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000
+ github.com/sashabaranov/go-openai v1.15.1
+ github.com/spf13/pflag v1.0.5 // indirect
+)
+
+replace github.com/plandex/plandex/shared => ../shared
diff --git a/cli/go.sum b/cli/go.sum
new file mode 100644
index 00000000..a8bf5fd3
--- /dev/null
+++ b/cli/go.sum
@@ -0,0 +1,67 @@
+github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAcwmWM=
+github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
+github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
+github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
+github.com/briandowns/spinner v1.23.0 h1:alDF2guRWqa/FOZZYWjlMIx2L6H0wyewPxo/CH4Pt2A=
+github.com/briandowns/spinner v1.23.0/go.mod h1:rPG4gmXeN3wQV/TsAY4w8lPdIM6RX3yqeBQJSrbXjuE=
+github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
+github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=
+github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
+github.com/fatih/color v1.7.0 h1:DkWD4oS2D8LGGgTQ6IvwJJXSL5Vp2ffcQg58nFV38Ys=
+github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
+github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
+github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
+github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
+github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
+github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
+github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
+github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
+github.com/pkoukk/tiktoken-go v0.1.5 h1:hAlT4dCf6Uk50x8E7HQrddhH3EWMKUN+LArExQQsQx4=
+github.com/pkoukk/tiktoken-go v0.1.5/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=
+github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
+github.com/sashabaranov/go-openai v1.15.1 h1:BAV5LCVEzvZ3rN/Lh5NRVs2z6AahPt/jn5s2/cEEG0M=
+github.com/sashabaranov/go-openai v1.15.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
+github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
+github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
+github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
+github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
+github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
+golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
+golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
+golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
+golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
+golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
+golang.org/x/net v0.0.0-20210916014120-12bc252f5db8/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
+golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
+golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
+golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
+golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
+golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
+golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4=
+golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
+golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
+golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/term v0.1.0 h1:g6Z6vPFA9dYBAF7DWcH6sCcOntplXsDKcliusYijMlw=
+golang.org/x/term v0.1.0/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
+golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY=
+golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
+golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
+golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
+golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
+golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
+golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/cli/lib/api.go b/cli/lib/api.go
new file mode 100644
index 00000000..99637a4a
--- /dev/null
+++ b/cli/lib/api.go
@@ -0,0 +1,161 @@
+package lib
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net/http"
+
+ "github.com/plandex/plandex/shared"
+ openai "github.com/sashabaranov/go-openai"
+)
+
+const apiHost = "http://localhost:8088"
+
+func ApiPrompt(prompt string, chatOnly bool) (*shared.PromptResponse, error) {
+ serverUrl := apiHost + "/prompt"
+
+ // Channels to receive data and errors
+ contextChan := make(chan shared.ModelContext, 1) // Buffered channels to prevent deadlock
+ contextErrChan := make(chan error, 1)
+
+ conversationChan := make(chan []openai.ChatCompletionMessage, 1)
+ conversationErrChan := make(chan error, 1)
+
+ planChan := make(chan shared.CurrentPlanFiles, 1)
+ planErrChan := make(chan error, 1)
+
+ // Goroutine for loading context
+ go func() {
+ context, err := GetAllContext(false)
+ if err != nil {
+ fmt.Println("Error loading context:" + err.Error())
+ contextErrChan <- err
+ return
+ }
+ contextChan <- context
+ }()
+
+ // Goroutine for loading conversation
+ go func() {
+ conversation, err := loadConversation()
+ if err != nil {
+ fmt.Println("Error loading conversation")
+ conversationErrChan <- err
+ return
+ }
+ conversationChan <- conversation
+ }()
+
+ // Goroutine for loading plan
+ go func() {
+ plan, err := getCurrentPlanFiles()
+ if err != nil {
+ fmt.Println("Error loading plan")
+ planErrChan <- err
+ return
+ }
+ planChan <- plan
+ }()
+
+ var context shared.ModelContext
+ var currentPlan shared.CurrentPlanFiles
+ var conversation []openai.ChatCompletionMessage
+ var err error
+
+ // Using select to receive from either data or error channel for context
+ select {
+ case context = <-contextChan:
+ case err = <-contextErrChan:
+ return nil, err
+ }
+
+ // Using select to receive from either data or error channel for conversation
+ select {
+ case conversation = <-conversationChan:
+ case err = <-conversationErrChan:
+ return nil, err
+ }
+
+ // Using select to receive from either data or error channel for plan
+ select {
+ case plan := <-planChan:
+ currentPlan = plan
+ case err = <-planErrChan:
+ return nil, err
+ }
+
+ payload := shared.PromptRequest{
+ Prompt: prompt,
+ ModelContext: context,
+ Conversation: conversation,
+ CurrentPlan: currentPlan,
+ ChatOnly: chatOnly,
+ }
+
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return nil, err
+ }
+
+ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ // Check the HTTP status code
+ if resp.StatusCode >= 400 {
+ // Read the error message from the body
+ errorBody, _ := io.ReadAll(resp.Body)
+ return nil, fmt.Errorf("server returned an error %d: %s", resp.StatusCode, string(errorBody))
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ var parsed shared.PromptResponse
+ err = json.Unmarshal(body, &parsed)
+ if err != nil {
+ return nil, err
+ }
+
+ return &parsed, nil
+}
+
+func ApiSummarize(text string) (*shared.SummarizeResponse, error) {
+ serverUrl := apiHost + "/summarize"
+
+ payload := shared.SummarizeRequest{
+ Text: text,
+ }
+ jsonData, err := json.Marshal(payload)
+ if err != nil {
+ return nil, err
+ }
+
+ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+
+ fmt.Println("ApiSummarize response body:")
+ fmt.Println(string(body))
+
+ var summarized shared.SummarizeResponse
+ err = json.Unmarshal(body, &summarized)
+ if err != nil {
+ return nil, err
+ }
+
+ return &summarized, nil
+}
diff --git a/cli/lib/context.go b/cli/lib/context.go
new file mode 100644
index 00000000..1a239ddd
--- /dev/null
+++ b/cli/lib/context.go
@@ -0,0 +1,638 @@
+package lib
+
+import (
+ "bufio"
+ "crypto/sha256"
+ "encoding/hex"
+ "encoding/json"
+ "fmt"
+ "io"
+ "log"
+ "os"
+ "path/filepath"
+ "strings"
+ "sync"
+ "sync/atomic"
+
+ "github.com/plandex/plandex/shared"
+)
+
+type LoadContextParams struct {
+ Note string
+ MaxTokens uint32
+ Recursive bool
+ MaxDepth int16
+ NamesOnly bool
+ Truncate bool
+ Resources []string
+}
+
+func WriteInitialContextState(contextDir string) error {
+ fmt.Println("Creating initial context state...")
+ contextState := shared.ModelContextState{
+ NumTokens: 0,
+ ActiveTokens: 0,
+ ChatFlexPct: 25,
+ PlanFlexPct: 50,
+ }
+ 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
+}
+
+func LoadContextOrDie(params *LoadContextParams) {
+ var contextState shared.ModelContextState
+ contextStateFilePath := filepath.Join(ContextSubdir, "context.json")
+
+ fmt.Fprintf(os.Stderr, "Loading context from %s\n", contextStateFilePath)
+
+ 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
+ }
+ }()
+
+ totalTokens := contextState.NumTokens
+ var totalTokensMutex sync.Mutex
+
+ counter := contextState.Counter
+
+ var contextParts []shared.ModelContextPart
+ var contextPartsMutex sync.Mutex
+
+ wg := sync.WaitGroup{}
+
+ if params.Note != "" {
+ wg.Add(1)
+
+ go func() {
+ defer wg.Done()
+
+ body := params.Note
+ numTokens := GetNumTokens(body)
+
+ totalTokensMutex.Lock()
+ func() {
+ defer totalTokensMutex.Unlock()
+
+ totalTokens += numTokens
+ if totalTokens > params.MaxTokens {
+ if params.Truncate {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating input text.", totalTokens, params.MaxTokens)
+ numTokens = params.MaxTokens - (totalTokens - numTokens)
+
+ // If the number of tokens is less than or equal to 0, then we can stop processing files
+ if numTokens <= 0 {
+ return
+ }
+
+ body = body[:numTokens]
+ totalTokens = params.MaxTokens
+
+ } else {
+ log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, params.MaxTokens)
+ }
+
+ }
+ }()
+
+ hash := sha256.Sum256([]byte(body))
+ sha := hex.EncodeToString(hash[:])
+
+ summaryResp, err := ApiSummarize(body)
+ if err != nil {
+ log.Fatalf("Failed to summarize the text: %v", err)
+ }
+
+ fileName := GetFileNameWithoutExt(summaryResp.FileName)
+
+ contextPart := shared.ModelContextPart{
+ Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Summary: summaryResp.Summary,
+ Body: body,
+ Sha: sha,
+ NumTokens: numTokens,
+ UpdatedAt: StringTs(),
+ }
+
+ contextPartsMutex.Lock()
+ contextParts = append(contextParts, contextPart)
+ contextPartsMutex.Unlock()
+
+ atomic.AddUint32(&counter, 1)
+ }()
+
+ }
+
+ hasPipeData := false
+ fileInfo, err := os.Stdin.Stat()
+ if err != nil {
+ log.Fatalf("Failed to stat stdin: %v", err)
+ }
+ if fileInfo.Mode()&os.ModeNamedPipe != 0 {
+ reader := bufio.NewReader(os.Stdin)
+ pipedData, err := io.ReadAll(reader)
+ if err != nil {
+ log.Fatalf("Failed to read piped data: %v", err)
+ }
+
+ if len(pipedData) > 0 {
+ wg.Add(1)
+
+ hasPipeData = true
+
+ go func() {
+ defer wg.Done()
+
+ body := string(pipedData)
+ numTokens := GetNumTokens(body)
+
+ totalTokensMutex.Lock()
+ func() {
+ defer totalTokensMutex.Unlock()
+
+ totalTokens += numTokens
+ if totalTokens > params.MaxTokens {
+ if params.Truncate {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating piped data.", totalTokens, params.MaxTokens)
+ numTokens = params.MaxTokens - (totalTokens - numTokens)
+
+ if numTokens <= 0 {
+ return
+ }
+
+ body = body[:numTokens]
+ totalTokens = params.MaxTokens
+
+ } else {
+ log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, params.MaxTokens)
+ }
+ }
+ }()
+
+ hash := sha256.Sum256([]byte(body))
+ sha := hex.EncodeToString(hash[:])
+
+ summaryResp, err := ApiSummarize(body)
+ if err != nil {
+ log.Fatalf("Failed to summarize piped data: %v", err)
+ }
+
+ fileName := GetFileNameWithoutExt(summaryResp.FileName)
+
+ contextPart := shared.ModelContextPart{
+ Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Summary: summaryResp.Summary,
+ Body: body,
+ Sha: sha,
+ NumTokens: numTokens,
+ UpdatedAt: StringTs(),
+ }
+
+ contextPartsMutex.Lock()
+ contextParts = append(contextParts, contextPart)
+ contextPartsMutex.Unlock()
+
+ atomic.AddUint32(&counter, 1)
+ }()
+ }
+ }
+
+ var inputUrls []string
+ var inputFilePaths []string
+
+ if len(params.Resources) > 0 {
+ for _, resource := range params.Resources {
+ // so far resources are either files or urls
+ if IsValidURL(resource) {
+ inputUrls = append(inputUrls, resource)
+ } else {
+ inputFilePaths = append(inputFilePaths, resource)
+ }
+ }
+ }
+
+ if len(inputFilePaths) > 0 {
+ wg.Add(1)
+
+ go func() {
+ defer wg.Done()
+
+ flattenedPaths := flattenPaths(inputFilePaths, params, 0)
+
+ if params.NamesOnly {
+ body := strings.Join(flattenedPaths, "\n")
+ bytes := []byte(body)
+ hash := sha256.Sum256(bytes)
+ sha := hex.EncodeToString(hash[:])
+ numTokens := GetNumTokens(body)
+
+ totalTokensMutex.Lock()
+ func() {
+ defer totalTokensMutex.Unlock()
+ totalTokens += numTokens
+ if totalTokens > params.MaxTokens {
+
+ if params.Truncate {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating filenames.", totalTokens, params.MaxTokens)
+ numTokens = params.MaxTokens - (totalTokens - numTokens)
+
+ // If the number of tokens is less than or equal to 0, then we can stop processing files
+ if numTokens <= 0 {
+ return
+ }
+
+ body = body[:numTokens]
+ totalTokens = params.MaxTokens
+
+ } else {
+ log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, params.MaxTokens)
+ }
+
+ }
+
+ }()
+
+ contextPart := shared.ModelContextPart{
+ Name: fmt.Sprintf("%d-filenames", atomic.LoadUint32(&counter)),
+ Summary: "filenames",
+ Body: body,
+ Sha: sha,
+ NumTokens: numTokens,
+ UpdatedAt: StringTs(),
+ }
+
+ contextPartsMutex.Lock()
+ contextParts = append(contextParts, contextPart)
+ contextPartsMutex.Unlock()
+
+ atomic.AddUint32(&counter, 1)
+
+ } else {
+ for _, path := range flattenedPaths {
+ wg.Add(1)
+
+ go func(path string) {
+ defer wg.Done()
+
+ fileContent, err := os.ReadFile(path)
+ if err != nil {
+ log.Fatalf("Failed to read the file %s: %v", path, err)
+ }
+
+ body := string(fileContent)
+ hash := sha256.Sum256(fileContent)
+ sha := hex.EncodeToString(hash[:])
+ numTokens := GetNumTokens(body)
+
+ totalTokensMutex.Lock()
+ func() {
+ defer totalTokensMutex.Unlock()
+ totalTokens += numTokens
+ if totalTokens > params.MaxTokens {
+
+ if params.Truncate {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating the file %s.", totalTokens, params.MaxTokens, path)
+ numTokens = params.MaxTokens - (totalTokens - numTokens)
+
+ // If the number of tokens is less than or equal to 0, then we can stop processing files
+ if numTokens <= 0 {
+ return
+ }
+
+ body = body[:numTokens]
+ totalTokens = params.MaxTokens
+
+ } else {
+ log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, params.MaxTokens)
+ }
+
+ }
+
+ }()
+
+ summaryResp, err := ApiSummarize(body)
+ if err != nil {
+ log.Fatalf("Failed to summarize the file %s: %v", path, err)
+ }
+
+ // get just the filename
+ _, fileName := filepath.Split(path)
+
+ contextPart := shared.ModelContextPart{
+ Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), fileName),
+ Summary: summaryResp.Summary,
+ Body: body,
+ FilePath: path,
+ Sha: sha,
+ NumTokens: numTokens,
+ UpdatedAt: StringTs(),
+ }
+
+ contextPartsMutex.Lock()
+ contextParts = append(contextParts, contextPart)
+ contextPartsMutex.Unlock()
+
+ atomic.AddUint32(&counter, 1)
+ }(path)
+
+ }
+ }
+ }()
+
+ }
+
+ if len(inputUrls) > 0 {
+ for _, url := range inputUrls {
+ wg.Add(1)
+
+ go func(url string) {
+ defer wg.Done()
+
+ body, err := FetchURLContent(url)
+ if err != nil {
+ log.Fatalf("Failed to fetch content from URL %s: %v", url, err)
+ }
+
+ numTokens := GetNumTokens(body)
+
+ totalTokensMutex.Lock()
+ func() {
+ defer totalTokensMutex.Unlock()
+ totalTokens += numTokens
+ if totalTokens > params.MaxTokens {
+ if params.Truncate {
+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d). Truncating content from URL %s.", totalTokens, params.MaxTokens, url)
+ numTokens = params.MaxTokens - (totalTokens - numTokens)
+
+ // If the number of tokens is less than or equal to 0, then we can stop processing content
+ if numTokens <= 0 {
+ return
+ }
+
+ body = body[:numTokens]
+ totalTokens = params.MaxTokens
+
+ } else {
+ log.Fatalf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, params.MaxTokens)
+ }
+ }
+ }()
+
+ hash := sha256.Sum256([]byte(body))
+ sha := hex.EncodeToString(hash[:])
+
+ summaryResp, err := ApiSummarize(body)
+ if err != nil {
+ log.Fatalf("Failed to summarize content from URL %s: %v", url, err)
+ }
+
+ contextPart := shared.ModelContextPart{
+ Name: fmt.Sprintf("%d.%s", atomic.LoadUint32(&counter), SanitizeAndClipURL(url, 70)),
+ Summary: summaryResp.Summary,
+ Body: body,
+ Sha: sha,
+ NumTokens: numTokens,
+ UpdatedAt: StringTs(),
+ }
+
+ contextPartsMutex.Lock()
+ contextParts = append(contextParts, contextPart)
+ contextPartsMutex.Unlock()
+
+ atomic.AddUint32(&counter, 1)
+ }(url)
+ }
+ }
+
+ wg.Wait()
+
+ if len(contextParts) == 0 {
+ log.Fatalln("No context loaded")
+ }
+
+ wg = sync.WaitGroup{}
+
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ writeContextParts(contextParts)
+ }()
+
+ wg.Add(1)
+
+ go func() {
+ defer wg.Done()
+ contextState.NumTokens = totalTokens
+ contextState.Counter = counter
+
+ data, err := json.MarshalIndent(contextState, "", " ")
+ if err != nil {
+ log.Fatalf("Failed to marshal context state: %v", 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 []string
+ if params.Note != "" {
+ added = append(added, "a note")
+ }
+ if hasPipeData {
+ added = append(added, "piped data")
+ }
+ if len(inputFilePaths) > 0 {
+ added = append(added, fmt.Sprintf("%d files", len(inputFilePaths)))
+ }
+ if len(inputUrls) > 0 {
+ added = append(added, fmt.Sprintf("%d urls", len(inputUrls)))
+ }
+
+ msg := "Loaded"
+ if len(added) <= 2 {
+ msg += strings.Join(added, " and ")
+ } else {
+ for i, add := range added {
+ if i == len(added)-1 {
+ msg += ", and " + add
+ } else {
+ msg += ", " + add
+ }
+ }
+ }
+ msg += " into context"
+
+ err = GitAddAndCommit(ContextSubdir, msg)
+
+ if err != nil {
+ return
+ }
+
+ err = GitAdd(CurrentPlanRootDir, ContextSubdir, true)
+ if err != nil {
+ fmt.Printf("failed to stage submodule changes in context dir: %s\n", err)
+ return
+ }
+
+ // Commit these staged submodule changes in the root repo
+ err = GitCommit(CurrentPlanRootDir, msg, true)
+ if err != nil {
+ fmt.Printf("failed to commit submodule updates in root dir: %s\n", err)
+ }
+
+ fmt.Fprint(os.Stderr, msg)
+}
+
+func GetAllContext(metaOnly bool) ([]shared.ModelContextPart, error) {
+ files, err := os.ReadDir(ContextSubdir)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to read context directory: %v", err)
+ return nil, err
+ }
+
+ var contexts []shared.ModelContextPart
+ for _, file := range files {
+ filename := file.Name()
+
+ if filename == ".git" || filename == "context.json" {
+ continue
+ }
+
+ // Only process .meta files and then look for their corresponding .body files
+ if strings.HasSuffix(filename, ".meta") {
+ fmt.Fprintf(os.Stderr, "Reading meta context file %s\n", filename)
+
+ metaContent, err := os.ReadFile(filepath.Join(ContextSubdir, filename))
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to read meta file %s: %v", filename, err)
+ return nil, err
+ }
+
+ var contextPart shared.ModelContextPart
+ if err := json.Unmarshal(metaContent, &contextPart); err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to unmarshal JSON from file %s: %v", filename, err)
+ return nil, err
+ }
+
+ if !metaOnly {
+ // get the body filename by replacing the .meta suffix with .body
+ bodyFilename := strings.TrimSuffix(filename, ".meta") + ".body"
+ bodyPath := filepath.Join(ContextSubdir, bodyFilename)
+
+ fmt.Fprintf(os.Stderr, "Reading body context file %s\n", bodyPath)
+
+ bodyContent, err := os.ReadFile(bodyPath)
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Failed to read body file %s: %v", bodyPath, err)
+ return nil, err
+ }
+
+ contextPart.Body = string(bodyContent)
+ }
+
+ contexts = append(contexts, contextPart)
+ }
+ }
+
+ return contexts, nil
+}
+
+// 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)
+}
+
+// writeContextPartToFile writes a single ModelContextPart to a file.
+func writeContextPartToFile(part shared.ModelContextPart) error {
+ metaFilename := createContextFileName(part.Name, part.Sha) + ".meta"
+ metaPath := filepath.Join(ContextSubdir, metaFilename)
+
+ bodyFilename := createContextFileName(part.Name, part.Sha) + ".body"
+ bodyPath := filepath.Join(ContextSubdir, bodyFilename)
+ body := []byte(part.Body)
+ part.Body = ""
+
+ // Convert the ModelContextPart to JSON
+ data, err := json.MarshalIndent(part, "", " ")
+ if err != nil {
+ return fmt.Errorf("failed to marshal context part: %v", err)
+ }
+
+ // Open or create a bodyFile for writing
+ bodyFile, err := os.OpenFile(bodyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ return fmt.Errorf("failed to open file %s for writing: %v", bodyPath, err)
+ }
+ defer bodyFile.Close()
+
+ // Write the body to the file
+ if _, err = bodyFile.Write(body); err != nil {
+ return fmt.Errorf("failed to write data to file %s: %v", bodyPath, err)
+ }
+
+ // Open or create a metaFile for writing
+ metaFile, err := os.OpenFile(metaPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
+ if err != nil {
+ return fmt.Errorf("failed to open file %s for writing: %v", metaPath, err)
+ }
+ defer metaFile.Close()
+
+ // Write the JSON data to the file
+ if _, err = metaFile.Write(data); err != nil {
+ return fmt.Errorf("failed to write data to file %s: %v", metaPath, err)
+ }
+
+ return nil
+}
+
+// Write each context part in parallel
+func writeContextParts(contextParts []shared.ModelContextPart) {
+ var wg sync.WaitGroup
+ 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)
+ }
+ }(part)
+ }
+ wg.Wait() // Wait for all goroutines to finish writing
+}
diff --git a/cli/lib/conversation.go b/cli/lib/conversation.go
new file mode 100644
index 00000000..e23c3377
--- /dev/null
+++ b/cli/lib/conversation.go
@@ -0,0 +1,77 @@
+package lib
+
+import (
+ "bufio"
+ "os"
+ "strings"
+
+ openai "github.com/sashabaranov/go-openai"
+)
+
+func loadConversation() ([]openai.ChatCompletionMessage, error) {
+ var messages []openai.ChatCompletionMessage
+
+ files, err := os.ReadDir(ConversationSubdir)
+ if err != nil {
+ return nil, err
+ }
+
+ for _, file := range files {
+ // Ensure we are only reading files, not directories
+ if !file.IsDir() {
+ filePath := ConversationSubdir + "/" + file.Name()
+ file, err := os.Open(filePath)
+ if err != nil {
+ return nil, err
+ }
+
+ scanner := bufio.NewScanner(file)
+ var currentRole string
+ var contentBuffer []string
+
+ for scanner.Scan() {
+ line := scanner.Text()
+
+ // Check if the line starts with user or response indicator
+ if strings.HasPrefix(line, "@@@!>user|") {
+ if currentRole != "" {
+ // Save the previous message before starting a new one
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ })
+ contentBuffer = []string{}
+ }
+ currentRole = openai.ChatMessageRoleUser
+ continue
+ } else if strings.HasPrefix(line, "@@@!>response|") {
+ if currentRole != "" {
+ // Save the previous message before starting a new one
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ })
+ contentBuffer = []string{}
+ }
+ currentRole = openai.ChatMessageRoleAssistant
+ continue
+ }
+
+ // Add content to the buffer
+ contentBuffer = append(contentBuffer, line)
+ }
+
+ // Add the last message in the file
+ if currentRole != "" && len(contentBuffer) > 0 {
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: currentRole,
+ Content: strings.Join(contentBuffer, "\n"),
+ })
+ }
+
+ file.Close()
+ }
+ }
+
+ return messages, nil
+}
diff --git a/cli/lib/fs.go b/cli/lib/fs.go
new file mode 100644
index 00000000..fe233eeb
--- /dev/null
+++ b/cli/lib/fs.go
@@ -0,0 +1,198 @@
+package lib
+
+import (
+ "encoding/json"
+ "fmt"
+ "log"
+ "os"
+ "path/filepath"
+ "sync"
+
+ "github.com/plandex/plandex/shared"
+)
+
+var Cwd string
+var PlandexDir string
+var HomePlandexDir string
+var CacheDir string
+
+var CurrentPlanName string
+var CurrentPlanRootDir string
+var PlanSubdir string
+var ConversationSubdir string
+var ContextSubdir string
+
+func init() {
+ var err error
+ Cwd, err = os.Getwd()
+ if err != nil {
+ panic(err)
+ }
+
+ wg := sync.WaitGroup{}
+
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+
+ home, err := os.UserHomeDir()
+ if err != nil {
+ panic(err)
+ }
+ HomePlandexDir = filepath.Join(home, ".plandex")
+ CacheDir = filepath.Join(HomePlandexDir, "cache")
+ err = os.MkdirAll(filepath.Join(CacheDir, "tiktoken"), os.ModePerm)
+ if err != nil {
+ panic(err)
+ }
+ err = os.Setenv("TIKTOKEN_CACHE_DIR", CacheDir)
+ if err != nil {
+ panic(err)
+ }
+ }()
+
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+
+ PlandexDir = FindPlandex()
+ err = LoadCurrentPlan()
+ if err != nil {
+ panic(err)
+ }
+ }()
+
+ wg.Wait()
+}
+
+func LoadCurrentPlan() error {
+ // Construct the path to the current_plan.json file
+ path := filepath.Join(PlandexDir, "current_plan.json")
+
+ // Check if the file exists
+ if _, err := os.Stat(path); os.IsNotExist(err) {
+ return nil
+ }
+
+ // Read the contents of the file
+ fileBytes, err := os.ReadFile(path)
+ if err != nil {
+ return fmt.Errorf("error reading current_plan.json: %v", err)
+ }
+
+ // Unmarshal the JSON data into the shared.PlanSettings type
+ var planSettings shared.PlanSettings
+ err = json.Unmarshal(fileBytes, &planSettings)
+ if err != nil {
+ return fmt.Errorf("error unmarshalling current_plan.json: %v", err)
+ }
+
+ CurrentPlanName = planSettings.Name
+ CurrentPlanRootDir = filepath.Join(PlandexDir, CurrentPlanName)
+ PlanSubdir = filepath.Join(CurrentPlanRootDir, "plan")
+ ConversationSubdir = filepath.Join(CurrentPlanRootDir, "conversation")
+ ContextSubdir = filepath.Join(CurrentPlanRootDir, "context")
+
+ return nil
+}
+
+func FindOrCreatePlandex() (string, bool, error) {
+ var err error
+
+ // Determine the directory path
+ dir := filepath.Join(Cwd, ".plandex")
+
+ // Check if the directory already exists
+ _, err = os.Stat(dir)
+ if err != nil {
+ if os.IsNotExist(err) {
+ // If not found, create in the current directory
+ err = os.Mkdir(dir, os.ModePerm)
+ if err != nil {
+ return "", false, err
+ }
+ PlandexDir = dir
+ return dir, true, nil
+ }
+ return "", false, err
+ }
+
+ return dir, false, nil
+}
+
+func FindPlandex() string {
+ searchPath := Cwd
+ for searchPath != "/" {
+ dir := filepath.Join(searchPath, ".plandex")
+ if _, err := os.Stat(dir); !os.IsNotExist(err) {
+ return dir
+ }
+ searchPath = filepath.Dir(searchPath)
+ }
+
+ return ""
+}
+
+func CwdIsPlan() bool {
+ // check if parent directory of cwd is '.plandex'
+ parentDir := filepath.Dir(Cwd)
+ return parentDir == PlandexDir
+}
+
+func flattenPaths(fileOrDirPaths []string, params *LoadContextParams, depth int16) []string {
+ var wg sync.WaitGroup
+ resPathsChan := make(chan string, len(fileOrDirPaths))
+
+ for _, path := range fileOrDirPaths {
+ fileInfo, err := os.Stat(path)
+ if err != nil {
+ log.Fatalf("Failed to read the file %s: %v", path, err)
+ }
+
+ if fileInfo.IsDir() {
+ if !params.Recursive {
+ log.Fatalf("The path %s is a directory. Please use the --recursive / -r flag to load the directory recursively.", path)
+ }
+ wg.Add(1)
+ go func(p string) {
+ defer wg.Done()
+ err := filepath.Walk(p, func(path string, info os.FileInfo, err error) error {
+ if err != nil {
+ log.Fatalf("Failed to read the file %s: %v", path, err)
+ }
+
+ if info.IsDir() {
+ if depth < params.MaxDepth {
+ for _, subPath := range flattenPaths([]string{path}, params, depth+1) {
+ resPathsChan <- subPath
+ }
+ } else if params.NamesOnly {
+ resPathsChan <- path
+ }
+ } else {
+ resPathsChan <- path
+ }
+
+ return nil
+ })
+ if err != nil {
+ log.Fatalf("Failed to process directory %s: %v", p, err)
+ }
+ }(path)
+ } else {
+ resPathsChan <- path
+ }
+ }
+
+ go func() {
+ wg.Wait()
+ close(resPathsChan)
+ }()
+
+ var resPaths []string
+ for p := range resPathsChan {
+ resPaths = append(resPaths, p)
+ }
+
+ return resPaths
+}
diff --git a/cli/lib/git.go b/cli/lib/git.go
new file mode 100644
index 00000000..71e51819
--- /dev/null
+++ b/cli/lib/git.go
@@ -0,0 +1,100 @@
+package lib
+
+import (
+ "fmt"
+ "os/exec"
+ "path/filepath"
+ "strings"
+ "sync"
+)
+
+var gitMutex sync.Mutex
+
+func InitGitRepo(dir string) error {
+ gitMutex.Lock()
+ defer gitMutex.Unlock()
+
+ res, err := exec.Command("git", "init", dir).CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error initializing git repository for dir: %s, err: %v, output: %s", dir, err, string(res))
+ }
+
+ return GitAddAndCommit(dir, "Initial commit")
+}
+
+func AddGitSubmodule(rootDir, submoduleDir string) error {
+ gitMutex.Lock()
+ defer gitMutex.Unlock()
+
+ // Calculate the relative path from rootDir to submoduleDir
+ relPath, err := filepath.Rel(rootDir, submoduleDir)
+ if err != nil {
+ return fmt.Errorf("error computing relative path from %s to %s: %v", rootDir, submoduleDir, err)
+ }
+
+ // Ensure relative path starts with ./ or ../
+ if !strings.HasPrefix(relPath, "./") && !strings.HasPrefix(relPath, "../") {
+ relPath = "./" + relPath
+ }
+
+ res, err := exec.Command("git", "-C", rootDir, "submodule", "add", relPath).CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error adding git submodule for dir: %s, err: %v, output: %s", submoduleDir, err, string(res))
+ }
+
+ return nil
+}
+
+func UpdateAndInitSubmodules(rootDir string) error {
+ gitMutex.Lock()
+ defer gitMutex.Unlock()
+
+ res, err := exec.Command("git", "-C", rootDir, "submodule", "update", "--init", "--recursive").CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error updating git submodules for dir: %s, err: %v, output: %s", rootDir, err, string(res))
+ }
+
+ return nil
+}
+
+func GitAddAndCommit(dir, message string) error {
+ err := GitAdd(dir, ".", false)
+ if err != nil {
+ return fmt.Errorf("error adding files to git repository for dir: %s, err: %v", dir, err)
+ }
+
+ err = GitCommit(dir, message, false)
+ if err != nil {
+ return fmt.Errorf("error committing files to git repository for dir: %s, err: %v", dir, err)
+ }
+
+ return nil
+}
+
+func GitAdd(repoDir, path string, lockMutex bool) error {
+ if lockMutex {
+ gitMutex.Lock()
+ defer gitMutex.Unlock()
+ }
+
+ res, err := exec.Command("git", "-C", repoDir, "add", path).CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error adding files to git repository for dir: %s, err: %v, output: %s", repoDir, err, string(res))
+ }
+
+ return nil
+}
+
+func GitCommit(repoDir, commitMsg string, lockMutex bool) error {
+ if lockMutex {
+ gitMutex.Lock()
+ defer gitMutex.Unlock()
+ }
+
+ res, err := exec.Command("git", "-C", repoDir, "commit", "-m", commitMsg, "--allow-empty").CombinedOutput()
+ if err != nil {
+ return fmt.Errorf("error committing files to git repository for dir: %s, err: %v, output: %s", repoDir, err, string(res))
+ }
+
+ return nil
+}
diff --git a/cli/lib/os.go b/cli/lib/os.go
new file mode 100644
index 00000000..bc1d9f98
--- /dev/null
+++ b/cli/lib/os.go
@@ -0,0 +1,11 @@
+package lib
+
+import "os/exec"
+
+func IsCommandAvailable(name string) bool {
+ cmd := exec.Command(name, "--version")
+ if err := cmd.Run(); err != nil {
+ return false
+ }
+ return true
+}
diff --git a/cli/lib/plan.go b/cli/lib/plan.go
new file mode 100644
index 00000000..2b155c81
--- /dev/null
+++ b/cli/lib/plan.go
@@ -0,0 +1,94 @@
+package lib
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "sync"
+
+ "github.com/plandex/plandex/shared"
+)
+
+type safePlanFiles struct {
+ mux sync.Mutex
+ data map[string]string
+}
+
+func (s *safePlanFiles) Add(key string, value string) {
+ s.mux.Lock()
+ s.data[key] = value
+ s.mux.Unlock()
+}
+
+var planFiles safePlanFiles
+
+func worker(filesDir string, srcPath string, info os.FileInfo, err error, errCh chan<- error) {
+ if err != nil {
+ errCh <- err
+ return
+ }
+ if info.IsDir() {
+ return
+ }
+
+ // Compute relative path
+ relPath, err := filepath.Rel(filesDir, srcPath)
+ if err != nil {
+ errCh <- err
+ return
+ }
+
+ // Read file content
+ content, err := os.ReadFile(srcPath)
+ if err != nil {
+ errCh <- err
+ return
+ }
+
+ // Add file content to planFiles with relative path as key
+ planFiles.Add(relPath, string(content))
+}
+
+func getCurrentPlanFiles() (shared.CurrentPlanFiles, error) {
+ planFiles.data = make(map[string]string)
+ filesDir := filepath.Join(CurrentPlanRootDir, "plan", "files")
+
+ // Check if filesDir exists
+ _, err := os.Stat(filesDir)
+ exists := !os.IsNotExist(err)
+
+ if exists {
+ errCh := make(chan error)
+ // Enumerate all paths in [planDir]/files
+ err = filepath.Walk(filesDir, func(srcPath string, info os.FileInfo, err error) error {
+ go worker(filesDir, srcPath, info, err, errCh)
+ return nil
+ })
+
+ close(errCh)
+ for err := range errCh {
+ if err != nil {
+ fmt.Fprintf(os.Stderr, "Error processing files: %v", err)
+ return shared.CurrentPlanFiles{}, err
+ }
+ }
+ }
+
+ var execContent string
+ var execExists bool
+ execPath := filepath.Join(CurrentPlanRootDir, "exec.sh")
+ _, err = os.Stat(execPath)
+ execExists = !os.IsNotExist(err)
+
+ if execExists {
+ // exec.sh exists
+ // Read file content and set it to planFiles["exec"]
+ content, err := os.ReadFile(execPath)
+ if err != nil {
+ return shared.CurrentPlanFiles{}, err
+ }
+ execContent = string(content)
+ }
+
+ return shared.CurrentPlanFiles{Files: planFiles.data, Exec: execContent}, nil
+}
diff --git a/cli/lib/prompt.go b/cli/lib/prompt.go
new file mode 100644
index 00000000..688354d2
--- /dev/null
+++ b/cli/lib/prompt.go
@@ -0,0 +1,115 @@
+package lib
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "time"
+
+ "github.com/briandowns/spinner"
+)
+
+func Prompt(prompt string, chatOnly bool) error {
+ s := spinner.New(spinner.CharSets[14], 100*time.Millisecond) // Choose spinner style
+ defer s.Stop()
+ s.Start()
+ fmt.Fprintln(os.Stderr, "Sending prompt...")
+
+ timestamp := StringTs()
+ response, err := ApiPrompt(prompt, chatOnly)
+ if err != nil {
+ return fmt.Errorf("failed to send prompt to server: %s\n", err)
+ }
+
+ fmt.Fprintln(os.Stderr, response)
+
+ // Create or append to conversation file
+ responseTimestamp := StringTs()
+ conversationFilePath := filepath.Join(ConversationSubdir, fmt.Sprintf("%s.md", timestamp))
+ userHeader := fmt.Sprintf("@@@!>user|%s\n\n", timestamp)
+ responseHeader := fmt.Sprintf("@@@!>response|%s\n\n", responseTimestamp)
+ conversationFileContents := fmt.Sprintf("%s%s\n\n%s%s", userHeader, prompt, responseHeader, response.Reply)
+ conversationFile, err := os.OpenFile(conversationFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ return fmt.Errorf("failed to write conversation file: %s\n", err)
+ }
+ defer conversationFile.Close()
+
+ _, err = conversationFile.WriteString(conversationFileContents)
+ if err != nil {
+ return fmt.Errorf("failed to write conversation file: %s\n", err)
+ }
+
+ if len(response.Files) > 0 {
+ filesDir := filepath.Join(PlanSubdir, "files")
+ err = os.MkdirAll(filesDir, os.ModePerm)
+
+ if err != nil {
+ return fmt.Errorf("failed to create files directory: %s\n", err)
+ }
+
+ for path, contents := range response.Files {
+ filePath := filepath.Join(filesDir, path)
+ dir := filepath.Dir(filePath)
+
+ err = os.MkdirAll(dir, os.ModePerm)
+ if err != nil {
+ return fmt.Errorf("failed to create directory: %s\n", err)
+ }
+
+ err = os.WriteFile(filePath, []byte(contents), 0644)
+ if err != nil {
+ return fmt.Errorf("failed to write file: %s\n", err)
+ }
+ }
+ }
+
+ if response.Exec != "" {
+ // write to exec.sh file
+ execFilePath := filepath.Join(PlanSubdir, "exec.sh")
+ execFile, err := os.OpenFile(execFilePath, os.O_CREATE|os.O_WRONLY, 0644)
+ if err != nil {
+ return fmt.Errorf("failed to write exec file: %s\n", err)
+ }
+ defer execFile.Close()
+
+ _, err = execFile.WriteString(response.Exec)
+ if err != nil {
+ return fmt.Errorf("failed to write exec file: %s\n", err)
+ }
+
+ err = os.Chmod(execFilePath, 0755)
+ if err != nil {
+ return fmt.Errorf("failed to make exec file executable: %s\n", err)
+ }
+ }
+
+ err = GitAddAndCommit(ConversationSubdir, response.CommitMsg)
+ if err != nil {
+ return fmt.Errorf("failed to commit files to conversation dir: %s\n", err)
+ }
+
+ err = GitAddAndCommit(PlanSubdir, response.CommitMsg)
+ if err != nil {
+ return fmt.Errorf("failed to commit files to plan dir: %s\n", err)
+ }
+
+ // Stage changes in the submodules in the root repo
+ err = GitAdd(CurrentPlanRootDir, ConversationSubdir, true)
+ if err != nil {
+ return fmt.Errorf("failed to stage submodule changes in conversation dir: %s\n", err)
+ }
+
+ err = GitAdd(CurrentPlanRootDir, PlanSubdir, true)
+ if err != nil {
+ return fmt.Errorf("failed to stage submodule changes in plan dir: %s\n", err)
+ }
+
+ // Commit these staged submodule changes in the root repo
+ err = GitCommit(CurrentPlanRootDir, response.CommitMsg, true)
+ if err != nil {
+ return fmt.Errorf("failed to commit submodule updates in root dir: %s\n", err)
+ }
+
+ return nil
+}
diff --git a/cli/lib/tokens.go b/cli/lib/tokens.go
new file mode 100644
index 00000000..356f06a1
--- /dev/null
+++ b/cli/lib/tokens.go
@@ -0,0 +1,21 @@
+package lib
+
+import (
+ "fmt"
+ "log"
+
+ "github.com/pkoukk/tiktoken-go"
+)
+
+const MaxTokens uint32 = 8000
+
+func GetNumTokens(text string) (numTokens uint32) {
+ tkm, err := tiktoken.EncodingForModel("gpt-4")
+ if err != nil {
+ err = fmt.Errorf("encoding for model: %v", err)
+ log.Println(err)
+ return
+ }
+
+ return uint32(len(tkm.Encode(text, nil, nil)))
+}
diff --git a/cli/lib/url.go b/cli/lib/url.go
new file mode 100644
index 00000000..1936e965
--- /dev/null
+++ b/cli/lib/url.go
@@ -0,0 +1,88 @@
+package lib
+
+import (
+ "errors"
+ "io"
+ "log"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+
+ "github.com/PuerkitoBio/goquery"
+)
+
+const (
+ // Constants for fetchURLContent function
+ maxRedirections = 10
+ httpTimeout = 30 * time.Second
+ maxContentSizeInMB = 10
+)
+
+func FetchURLContent(url string) (string, error) {
+ client := &http.Client{
+ Timeout: httpTimeout,
+ CheckRedirect: func(req *http.Request, via []*http.Request) error {
+ if len(via) >= maxRedirections {
+ return errors.New("stopped after too many redirects")
+ }
+ return nil
+ },
+ }
+
+ resp, err := client.Get(url)
+ if err != nil {
+ return "", err
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode < 200 || resp.StatusCode >= 300 {
+ return "", errors.New("non-2xx HTTP response status: " + resp.Status)
+ }
+
+ // Limit the response reader to a maximum amount
+ limitedReader := io.LimitReader(resp.Body, maxContentSizeInMB*1024*1024)
+
+ content, err := io.ReadAll(limitedReader)
+ if err != nil {
+ return "", err
+ }
+
+ contentType := resp.Header.Get("Content-Type")
+ if strings.Contains(contentType, "text/html") {
+ return ExtractTextualContent(string(content)), nil
+ } else {
+ return string(content), nil
+ }
+}
+
+func ExtractTextualContent(htmlContent string) string {
+ r := strings.NewReader(htmlContent)
+ doc, err := goquery.NewDocumentFromReader(r)
+ if err != nil {
+ log.Fatalf("Failed to parse HTML: %v", err)
+ return ""
+ }
+
+ return doc.Text()
+}
+
+func SanitizeAndClipURL(url string, maxLength int) string {
+ // Replace common invalid filename characters. You can extend this list as needed.
+ sanitized := strings.ReplaceAll(url, ":", "_")
+ sanitized = strings.ReplaceAll(sanitized, "/", "_")
+ sanitized = strings.ReplaceAll(sanitized, "?", "_")
+ sanitized = strings.ReplaceAll(sanitized, "&", "_")
+ sanitized = strings.ReplaceAll(sanitized, "=", "_")
+ sanitized = strings.ReplaceAll(sanitized, "#", "_")
+ sanitized = strings.ReplaceAll(sanitized, "%", "_")
+ sanitized = strings.ReplaceAll(sanitized, "*", "_")
+ sanitized = strings.ReplaceAll(sanitized, " ", "_")
+
+ return sanitized[:maxLength]
+}
+
+func IsValidURL(str string) bool {
+ u, err := url.Parse(str)
+ return err == nil && u.Scheme != "" && u.Host != ""
+}
diff --git a/cli/lib/utils.go b/cli/lib/utils.go
new file mode 100644
index 00000000..34a3b92a
--- /dev/null
+++ b/cli/lib/utils.go
@@ -0,0 +1,34 @@
+package lib
+
+import (
+ "path/filepath"
+ "strings"
+ "time"
+)
+
+func StringTs() string {
+ return time.Now().Format("2006-01-02T15:04:05.999Z")
+}
+
+func EnsureMinDuration(start time.Time, minDuration time.Duration) {
+ elapsed := time.Since(start)
+ if elapsed < minDuration {
+ time.Sleep(minDuration - elapsed)
+ }
+}
+
+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/main.go b/cli/main.go
new file mode 100644
index 00000000..20c81f13
--- /dev/null
+++ b/cli/main.go
@@ -0,0 +1,9 @@
+package main
+
+import (
+ "plandex/cmd"
+)
+
+func main() {
+ cmd.Execute()
+}
diff --git a/server/go.mod b/server/go.mod
new file mode 100644
index 00000000..f93b0571
--- /dev/null
+++ b/server/go.mod
@@ -0,0 +1,12 @@
+module plandex-server
+
+go 1.21.0
+
+require (
+ github.com/davecgh/go-spew v1.1.1
+ github.com/gorilla/mux v1.8.0
+ github.com/plandex/plandex/shared v0.0.0-00010101000000-000000000000
+ github.com/sashabaranov/go-openai v1.15.1
+)
+
+replace github.com/plandex/plandex/shared => ../shared
diff --git a/server/go.sum b/server/go.sum
new file mode 100644
index 00000000..31c778d0
--- /dev/null
+++ b/server/go.sum
@@ -0,0 +1,6 @@
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
+github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
+github.com/sashabaranov/go-openai v1.15.1 h1:BAV5LCVEzvZ3rN/Lh5NRVs2z6AahPt/jn5s2/cEEG0M=
+github.com/sashabaranov/go-openai v1.15.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
diff --git a/server/handlers/prompt.go b/server/handlers/prompt.go
new file mode 100644
index 00000000..05ccf9f1
--- /dev/null
+++ b/server/handlers/prompt.go
@@ -0,0 +1,54 @@
+package handlers
+
+import (
+ "encoding/json"
+ "fmt"
+ "io"
+ "log"
+ "net/http"
+ "plandex-server/model"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func PromptHandler(w http.ResponseWriter, r *http.Request) {
+ log.Println("Received a request for PromptHandler")
+
+ // 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.Prompt(requestBody)
+ if err != nil {
+ log.Printf("Error sending prompt to model: %v\n", err)
+ http.Error(w, "Error sending prompt to model: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ log.Println("Successfully processed prompt request")
+
+ fmt.Println("Final response from model:")
+ fmt.Println(string(modelRes))
+
+ // Return the response from OpenAI to the client
+ w.Write(modelRes)
+}
diff --git a/server/handlers/summarize.go b/server/handlers/summarize.go
new file mode 100644
index 00000000..d137c21d
--- /dev/null
+++ b/server/handlers/summarize.go
@@ -0,0 +1,49 @@
+package handlers
+
+import (
+ "encoding/json"
+ "io"
+ "log"
+ "net/http"
+ "plandex-server/model"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func SummarizeHandler(w http.ResponseWriter, r *http.Request) {
+ log.Println("Received a request for SummarizeHandler")
+
+ // 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.SummarizeRequest
+ 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.Text == "" {
+ log.Println("Received empty text field")
+ http.Error(w, "text field is required", http.StatusBadRequest)
+ return
+ }
+
+ modelResp, err := model.Summarize(requestBody.Text)
+ if err != nil {
+ log.Printf("Error summarizing text: %v\n", err)
+ http.Error(w, "Error summarizing text: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ log.Println("Successfully processed summarize request")
+ // Return the response from OpenAI to the client
+ w.Write(modelResp)
+}
diff --git a/server/main.go b/server/main.go
new file mode 100644
index 00000000..6dd6cbea
--- /dev/null
+++ b/server/main.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "os"
+
+ openai "github.com/sashabaranov/go-openai"
+
+ "github.com/gorilla/mux"
+
+ "plandex-server/handlers"
+)
+
+var client *openai.Client
+
+func main() {
+ r := mux.NewRouter()
+
+ r.HandleFunc("/prompt", handlers.PromptHandler).Methods("POST")
+ r.HandleFunc("/summarize", handlers.SummarizeHandler).Methods("POST")
+
+ // Get port from the environment variable or default to 8088
+ port := os.Getenv("PORT")
+ if port == "" {
+ port = "8088"
+ }
+
+ log.Printf("Plandex server is running on :%s", port)
+ log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), r))
+}
diff --git a/server/model/client.go b/server/model/client.go
new file mode 100644
index 00000000..14707070
--- /dev/null
+++ b/server/model/client.go
@@ -0,0 +1,13 @@
+package model
+
+import (
+ "os"
+
+ "github.com/sashabaranov/go-openai"
+)
+
+var client *openai.Client
+
+func init() {
+ client = openai.NewClient(os.Getenv("OPENAI_API_KEY"))
+}
diff --git a/server/model/context.go b/server/model/context.go
new file mode 100644
index 00000000..ec4f2776
--- /dev/null
+++ b/server/model/context.go
@@ -0,0 +1,38 @@
+package model
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/plandex/plandex/shared"
+)
+
+func formatModelContext(context shared.ModelContext) string {
+ var contextMessages []string
+ for _, part := range context {
+ var message string
+ if part.FilePath != "" {
+ message = fmt.Sprintf("\n\n-file: %s\n\n```%s```", part.FilePath, part.Body)
+ } else if part.Url != "" {
+ message = fmt.Sprintf("\n\n-url: %s\n\n```%s```", part.Url, part.Body)
+ } else {
+ message = fmt.Sprintf("\n\n-content%s:\n\n```%s```", part.Name, part.Body)
+ }
+
+ contextMessages = append(contextMessages, message)
+ }
+ return strings.Join(contextMessages, "\n")
+}
+
+func formatCurrentPlan(plan shared.CurrentPlanFiles) string {
+ var planMessages []string
+ for path, content := range plan.Files {
+ planMessages = append(planMessages, fmt.Sprintf("\n\n-file: %s\n\n```%s```", path, content))
+ }
+
+ if plan.Exec != "" {
+ planMessages = append(planMessages, fmt.Sprintf("\n\n-exec: %s", plan.Exec))
+ }
+
+ return strings.Join(planMessages, "\n")
+}
diff --git a/server/model/prompt.go b/server/model/prompt.go
new file mode 100644
index 00000000..d3a763ff
--- /dev/null
+++ b/server/model/prompt.go
@@ -0,0 +1,389 @@
+package model
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "strings"
+
+ "github.com/plandex/plandex/shared"
+ "github.com/sashabaranov/go-openai"
+ "github.com/sashabaranov/go-openai/jsonschema"
+)
+
+type fileResult struct {
+ filePath string
+ content string
+ isExec bool
+}
+
+type firstParseResponse struct {
+ Reply string `json:"reply"`
+ CommitMsg string `json:"commitMsg"`
+ Files []string `json:"files"`
+}
+
+type parseFileResponse struct {
+ Content string `json:"content"`
+}
+
+func Prompt(req shared.PromptRequest) ([]byte, error) {
+ contextText := formatModelContext(req.ModelContext)
+
+ systemMessageText := `
+ You are Plandex, an AI programming and system administration assistant.
+ You help programmers with tasks, especially those that involve multiple files and shell commands. You offer a structured, versioned, and iterative approach to AI-driven development.
+ You and the programmer collaborate to create a 'plan' for the task at hand. A plan is a set of files and an 'exec' script with an attached context.
+ Based on user-provided context, please create a plan for the task. When suggesting changes that would modify files from the context or create new files, always precede them with the file path.
+ Context from the user:` + contextText + `
+ Current state of the plan:` + formatCurrentPlan(req.CurrentPlan)
+
+ systemMessage := openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleSystem,
+ Content: systemMessageText,
+ }
+
+ messages := []openai.ChatCompletionMessage{
+ systemMessage,
+ }
+
+ if len(req.Conversation) > 0 {
+ messages = append(messages, req.Conversation...)
+ }
+
+ messages = append(messages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleUser,
+ Content: req.Prompt,
+ })
+
+ for _, message := range messages {
+ fmt.Printf("%s: %s\n", message.Role, message.Content)
+ }
+
+ initialResp, err := client.CreateChatCompletion(
+ context.Background(),
+ openai.ChatCompletionRequest{
+ Model: openai.GPT4,
+ Messages: messages,
+ },
+ )
+
+ if err != nil {
+ fmt.Printf("Error while creating chat completion: %v\n", err)
+ return nil, err
+ }
+
+ initialStrRes := initialResp.Choices[0].Message.Content
+ fmt.Println("Initial response from model:")
+ fmt.Println(initialStrRes)
+
+ if req.ChatOnly {
+ chatResp := shared.PromptResponse{
+ Reply: initialStrRes,
+ }
+ chatByteRes, err := json.Marshal(chatResp)
+ if err != nil {
+ fmt.Printf("Error marshalling chat response: %v\n", err)
+ return nil, err
+ }
+ return chatByteRes, nil
+ }
+
+ firstParseResp, err := client.CreateChatCompletion(
+ context.Background(),
+ openai.ChatCompletionRequest{
+ Model: openai.GPT3Dot5Turbo,
+ Functions: []openai.FunctionDefinition{{
+ Name: "parse",
+ Parameters: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "reply": {
+ Type: jsonschema.String,
+ Description: "Your previous response without code or commands.",
+ },
+ "commitMsg": {
+ Type: jsonschema.String,
+ Description: "A brief commit message for the changes proposed in your previous response.",
+ },
+ "files": {
+ Type: jsonschema.Array,
+ Description: "An array of file paths from your previous response that should be created or updated.",
+ Items: &jsonschema.Definition{
+ Type: jsonschema.String,
+ },
+ },
+ },
+ Required: []string{"reply", "commitMsg", "files"},
+ },
+ }},
+ Messages: []openai.ChatCompletionMessage{
+ {
+ Role: openai.ChatMessageRoleSystem,
+ Content: "You are an AI parser...",
+ },
+ {
+ Role: openai.ChatMessageRoleAssistant,
+ Content: initialStrRes,
+ },
+ },
+ },
+ )
+
+ if err != nil {
+ fmt.Printf("Error during first parse completion: %v\n", err)
+ return nil, err
+ }
+
+ var firstParseStrRes string
+ for _, choice := range firstParseResp.Choices {
+ if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "parse" {
+ fnCall := choice.Message.FunctionCall
+ firstParseStrRes = fnCall.Arguments
+ }
+ }
+
+ if firstParseStrRes == "" {
+ return nil, fmt.Errorf("no parse function call found in response")
+ }
+
+ firstParseByteRes := []byte(firstParseStrRes)
+ var firstParsePromptResp firstParseResponse
+ err = json.Unmarshal(firstParseByteRes, &firstParsePromptResp)
+ if err != nil {
+ fmt.Printf("Error unmarshalling first parse response: %v\n", err)
+ fmt.Printf("JSON causing the error: %s\n", firstParseByteRes)
+ return nil, err
+ }
+
+ finalResp := shared.PromptResponse{
+ Reply: firstParsePromptResp.Reply,
+ CommitMsg: firstParsePromptResp.CommitMsg,
+ Files: map[string]string{},
+ }
+
+ // Channel to receive results from goroutines
+ results := make(chan fileResult, len(firstParsePromptResp.Files))
+
+ // Create a cancellable context
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel() // Make sure all resources are cleaned up
+
+ // Error channel to catch the first occurring error
+ errChan := make(chan error, 1)
+
+ // Channel to signal completion of goroutines
+ done := make(chan bool, len(firstParsePromptResp.Files))
+
+ for _, filePath := range firstParsePromptResp.Files {
+ go func(filePath string) {
+ // Listen for context cancellation
+ select {
+ case <-ctx.Done():
+ done <- true
+ return
+ default:
+ }
+
+ fmt.Println("Getting file from model: " + filePath)
+
+ // get relevant file context (if any)
+ var fileContext *shared.ModelContextPart
+ for _, part := range req.ModelContext {
+ if part.FilePath == filePath {
+ fileContext = &part
+ break
+ }
+ }
+
+ fileMessages := []openai.ChatCompletionMessage{}
+ if fileContext != nil {
+ fileMessages = append(fileMessages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleSystem,
+ Content: fmt.Sprintf("Original file %s:\n```\n%s\n```", filePath, fileContext.Body),
+ })
+ }
+
+ fileMessages = append(fileMessages, openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleAssistant,
+ Content: initialStrRes,
+ },
+ openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleUser,
+ Content: fmt.Sprintf(`
+ Based on your previous response, call the 'writeFile' function with the full content of the file %s as raw text, including any updates. You must include the entire file and not leave anything out, even if it is already present in the original file. Do not include any placeholders or references to the original file. Output the updated entire file. Only call the 'writeFile' function in your reponse. Don't call any other function.
+ `, filePath),
+ })
+
+ fileResp, err := client.CreateChatCompletion(
+ ctx,
+ openai.ChatCompletionRequest{
+ Model: openai.GPT4,
+ // Temperature: 0,
+ Functions: []openai.FunctionDefinition{{
+ Name: "writeFile",
+ Parameters: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "content": {
+ Type: jsonschema.String,
+ Description: "The full content of the file, including any updates from the previous response, as raw text",
+ },
+ },
+ Required: []string{"content"},
+ },
+ }},
+ Messages: fileMessages,
+ },
+ )
+
+ if err != nil {
+ fmt.Println("Error getting file from model: " + filePath + ": " + err.Error())
+
+ // Send error to error channel and cancel the context
+ select {
+ case errChan <- err:
+ default:
+ }
+ cancel()
+ }
+
+ var fileResStr string
+ for _, choice := range fileResp.Choices {
+ if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "writeFile" {
+ fnCall := choice.Message.FunctionCall
+ fileResStr = fnCall.Arguments
+ break
+ }
+ }
+
+ if fileResStr == "" {
+ fmt.Println("No response from model for file " + filePath)
+ } else {
+ var fileRes parseFileResponse
+ if err := json.Unmarshal([]byte(fileResStr), &fileRes); err != nil {
+ // Send error to error channel and cancel the context
+ select {
+ case errChan <- err:
+ default:
+ }
+ cancel()
+ }
+
+ fmt.Println("File response from model for file " + filePath + ":")
+ fmt.Println(fileRes.Content)
+
+ // Send result to channel
+ results <- fileResult{
+ filePath: filePath,
+ content: fileRes.Content,
+ }
+ }
+
+ done <- true
+
+ }(filePath)
+ }
+
+ go func() {
+ // Listen for context cancellation
+ select {
+ case <-ctx.Done():
+ done <- true
+ return
+ default:
+ }
+
+ fmt.Println("Getting exec.sh from model: ")
+
+ execResp, err := client.CreateChatCompletion(
+ ctx,
+ openai.ChatCompletionRequest{
+ Model: openai.GPT4,
+ Messages: []openai.ChatCompletionMessage{
+ {
+ Role: openai.ChatMessageRoleAssistant,
+ Content: initialStrRes,
+ },
+ {
+ Role: openai.ChatMessageRoleUser,
+ Content: fmt.Sprintf(`
+ Output a shell script that includes any commands from the previous message that should be executed after any relevant files are created and/or updated.
+
+ If no commands should be executed, output only 'no exec commands'. Otherwise, output the script as raw text and nothing else.
+ `),
+ },
+ },
+ },
+ )
+
+ if err != nil {
+ fmt.Println("Error getting shell script from model: " + err.Error())
+
+ // Send error to error channel and cancel the context
+ select {
+ case errChan <- err:
+ default:
+ }
+ cancel()
+ }
+
+ if err == nil && len(execResp.Choices) > 0 {
+ content := execResp.Choices[0].Message.Content
+
+ fmt.Println("Exec res from model:")
+ fmt.Println(content)
+
+ // Check if content includes phrase 'No commands'
+ if strings.Contains(strings.ToLower(content), "no exec commands") {
+ fmt.Println("No commands to execute")
+ done <- true
+ return
+ }
+
+ // Send result to channel
+ results <- fileResult{
+ content: content,
+ isExec: true,
+ }
+ } else {
+ fmt.Println("No response from model for shell script")
+ }
+
+ done <- true
+ }()
+
+ // Wait for all goroutines to finish or an error to occur
+ for i := 0; i < len(firstParsePromptResp.Files)+1; i++ {
+ select {
+ case <-done:
+ case err := <-errChan:
+ return nil, err
+ }
+ }
+ close(results) // Close the results channel after all routines are done
+
+ // Process the results
+ for result := range results {
+ fmt.Println("File response from model for file " + result.filePath + ":")
+ fmt.Println(result.content)
+
+ content := strings.TrimPrefix(result.content, "```")
+ content = strings.TrimSuffix(content, "```")
+
+ if result.isExec {
+ finalResp.Exec = content
+ } else {
+ finalResp.Files[result.filePath] = content
+ }
+ }
+
+ // convert final response to JSON
+ finalByteRes, err := json.Marshal(finalResp)
+
+ if err != nil {
+ return nil, err
+ }
+
+ return finalByteRes, nil
+}
diff --git a/server/model/summarize.go b/server/model/summarize.go
new file mode 100644
index 00000000..3adfda96
--- /dev/null
+++ b/server/model/summarize.go
@@ -0,0 +1,86 @@
+package model
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+
+ "github.com/davecgh/go-spew/spew"
+ "github.com/plandex/plandex/shared"
+ "github.com/sashabaranov/go-openai"
+ "github.com/sashabaranov/go-openai/jsonschema"
+)
+
+func Summarize(text string) ([]byte, error) {
+ resp, err := client.CreateChatCompletion(
+ context.Background(),
+ openai.ChatCompletionRequest{
+ Model: openai.GPT3Dot5Turbo,
+ Functions: []openai.FunctionDefinition{{
+ Name: "summarize",
+ Parameters: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "name": {
+ Type: jsonschema.String,
+ Description: "A short name for the text based on the content.",
+ },
+ "summary": {
+ Type: jsonschema.String,
+ Description: "A concise summary of the text.",
+ },
+ "fileName": {
+ Type: jsonschema.String,
+ Description: "A short file name for the text based on the content. Use dashes as word separators. No spaces or special characters. 5 words max.",
+ },
+ },
+ Required: []string{"name", "summary", "fileName"},
+ },
+ }},
+ Messages: []openai.ChatCompletionMessage{
+ {
+ Role: openai.ChatMessageRoleSystem,
+ Content: "You are an AI summarizer that summarizes text, including programs, documentation, websites, and more. Most text will be related to software development.",
+ },
+ {
+ Role: openai.ChatMessageRoleUser,
+ Content: (`
+ Please summarize the text below using the 'summarize' function. Only call the 'summarize' function in your reponse. Don't call any other function.
+
+ Text:
+
+ ` + text),
+ },
+ },
+ },
+ )
+
+ fmt.Println("Summarize response:")
+ spew.Dump(resp)
+
+ if err != nil {
+ return nil, err
+ }
+
+ var byteRes []byte
+ for _, choice := range resp.Choices {
+ if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "summarize" {
+ fnCall := choice.Message.FunctionCall
+ byteRes = []byte(fnCall.Arguments)
+ }
+ }
+
+ if len(byteRes) == 0 {
+ return nil, fmt.Errorf("no summarize function call found in response")
+ }
+
+ // validate the JSON response
+ var summarizeResp shared.SummarizeResponse
+ if err := json.Unmarshal(byteRes, &summarizeResp); err != nil {
+ return nil, err
+ }
+
+ spew.Dump(summarizeResp)
+
+ return byteRes, nil
+}
diff --git a/shared/go.mod b/shared/go.mod
new file mode 100644
index 00000000..f935bc71
--- /dev/null
+++ b/shared/go.mod
@@ -0,0 +1,5 @@
+module shared
+
+go 1.21.0
+
+require github.com/sashabaranov/go-openai v1.15.1
diff --git a/shared/go.sum b/shared/go.sum
new file mode 100644
index 00000000..dc20bd21
--- /dev/null
+++ b/shared/go.sum
@@ -0,0 +1,2 @@
+github.com/sashabaranov/go-openai v1.15.1 h1:BAV5LCVEzvZ3rN/Lh5NRVs2z6AahPt/jn5s2/cEEG0M=
+github.com/sashabaranov/go-openai v1.15.1/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
diff --git a/shared/types.go b/shared/types.go
new file mode 100644
index 00000000..387880c1
--- /dev/null
+++ b/shared/types.go
@@ -0,0 +1,78 @@
+package shared
+
+import (
+ openai "github.com/sashabaranov/go-openai"
+)
+
+type CurrentPlanFiles struct {
+ Files map[string]string `json:"files"`
+ Exec string `json:"exec"`
+}
+
+type PromptRequest struct {
+ Prompt string `json:"prompt"`
+ ModelContext ModelContext `json:"modelContext"`
+ Conversation []openai.ChatCompletionMessage
+ CurrentPlan CurrentPlanFiles `json:"currentPlan"`
+ ChatOnly bool
+}
+
+type SummarizeRequest struct {
+ Text string `json:"text"`
+}
+
+type SummarizeResponse struct {
+ Name string `json:"name"`
+ Summary string `json:"summary"`
+ FileName string `json:"fileName"`
+}
+
+type PromptResponse struct {
+ Reply string `json:"reply"`
+ CommitMsg string `json:"commitMsg"`
+ Files map[string]string `json:"files"`
+ Exec string `json:"exec"`
+}
+
+type ModelContextPart struct {
+ Name string `json:"name"`
+ Summary string `json:"summary"`
+ Body string `json:"body"`
+ Url string `json:"url"`
+ FilePath string `json:"filePath"`
+ Sha string `json:"sha"`
+ NumTokens uint32 `json:"numTokens"`
+ UpdatedAt string `json:"updatedAt"`
+}
+type ModelContext []ModelContextPart
+
+type ModelContextState struct {
+ NumTokens uint32 `json:"numTokens"`
+ Counter uint32 `json:"counter"`
+ ActiveTokens uint32 `json:"activeTokens"`
+ ChatFlexPct uint8 `json:"chatFlexPct"`
+ PlanFlexPct uint8 `json:"planFlexPct"`
+}
+
+type PlanSettings struct {
+ Name string `json:"name"`
+}
+
+// type Plan struct {
+// Id string
+// Name string
+// Summary string
+// Creator string
+// CreatedAt string
+// UpdatedAt string
+
+// ContextVersionNum uint32
+// ContextSha string
+// ContextUpdatedAt string
+// ContextUpdatedBy string
+
+// RevisionNum uint32
+// RevisionSha string
+// RevisedAt string
+// RevisedBy string
+// }