Skip to content

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

apply/preview improvements + sectionaizing

Dane Schneideredde9598373f
diff --git a/cli/cmd/apply.go b/cli/cmd/apply.go
index 64a8a609..a555bdd4 100644
--- a/cli/cmd/apply.go
+++ b/cli/cmd/apply.go
@@ -8,6 +8,7 @@ import (
"os/exec"
"path/filepath"
"plandex/lib"
+ "strings"
"github.com/spf13/cobra"
)
@@ -30,20 +31,17 @@ func apply(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
name = args[0]
+ name = strings.TrimSpace(name)
}
if err != nil {
return fmt.Errorf("Error: %v", err)
}
- if name == "current" {
+ if name == "" || name == "current" {
name = lib.CurrentPlanName
}
- if name == "" {
- log.Fatalf("No plan name provided. Use 'plandex apply current' to apply the current plan: %s\n", lib.CurrentPlanName)
- }
-
// Check git installed
if !lib.IsCommandAvailable("git") {
log.Fatalln("Error: git is required")
diff --git a/cli/cmd/preview.go b/cli/cmd/preview.go
index bcb563ae..0a617e14 100644
--- a/cli/cmd/preview.go
+++ b/cli/cmd/preview.go
@@ -1,6 +1,7 @@
package cmd
import (
+ "bytes"
"fmt"
"log"
"os"
@@ -23,29 +24,53 @@ var checkoutCmd = &cobra.Command{
Run: checkout,
}
+func getCurrentBranch() (string, error) {
+ cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
+ var out bytes.Buffer
+ cmd.Stdout = &out
+ err := cmd.Run()
+ if err != nil {
+ return "", err
+ }
+ return strings.TrimSpace(out.String()), nil
+}
+
func checkout(cmd *cobra.Command, args []string) {
if !lib.IsCommandAvailable("git") {
log.Fatalln("Error: git is required")
}
+ var name string
+
+ if len(args) > 0 {
+ name = args[0]
+ name = strings.TrimSpace(name)
+ }
+
output, err := exec.Command("git", "rev-parse", "--is-inside-work-tree").CombinedOutput()
if err != nil || strings.TrimSpace(string(output)) != "true" {
log.Fatalln("Error: please make sure you're inside of a git repository")
}
- if name == "current" {
+ if name == "" || name == "current" {
name = lib.CurrentPlanName
}
- if name == "" {
- log.Fatalf("No plan name provided. Use 'plandex preview current' to preview the current plan in a branch: %s\n", lib.CurrentPlanName)
- }
-
- branchName := "pdx_" + args[0]
- _, err = exec.Command("git", "checkout", "-b", branchName).CombinedOutput()
+ branchName := "pdx-" + name
+ currentBranch, err := getCurrentBranch()
if err != nil {
- log.Fatalln("Error: could not checkout to a new branch.")
+ log.Fatalf("Error: could not retrieve current branch: %v\n", err)
+ }
+
+ if currentBranch == branchName {
+ fmt.Printf("Already on branch %s\n", branchName)
+ } else {
+ _, err = exec.Command("git", "checkout", "-b", branchName).CombinedOutput()
+ fmt.Printf("✅ Checked out branch %s\n", branchName)
+ if err != nil {
+ log.Fatalf("Error: could not checkout to a new branch: %v\n", err)
+ }
}
err = apply(cmd, args)
@@ -54,5 +79,5 @@ func checkout(cmd *cobra.Command, args []string) {
fmt.Fprintln(os.Stderr, "Error committing plan: ", err)
return
}
- fmt.Println("Plan applied and committed successfully to branch", branchName, "!")
+ fmt.Println("✅ Applied changes")
}
diff --git a/server/model/sectionize.go b/server/model/sectionize.go
new file mode 100644
index 00000000..b5995038
--- /dev/null
+++ b/server/model/sectionize.go
@@ -0,0 +1,75 @@
+package model
+
+import (
+ "context"
+ "fmt"
+
+ "github.com/davecgh/go-spew/spew"
+ "github.com/plandex/plandex/shared"
+ "github.com/plandex/plandex/server/types"
+ "github.com/sashabaranov/go-openai"
+ "github.com/sashabaranov/go-openai/jsonschema"
+)
+
+func Sectionize(text string) ([]byte, error) {
+ resp, err := Client.CreateChatCompletion(
+ context.Background(),
+ openai.ChatCompletionRequest{
+ Model: openai.GPT3Dot5Turbo,
+ Functions: []openai.FunctionDefinition{{
+ Name: "sectionize",
+ Parameters: &jsonschema.Definition{
+ Type: jsonschema.Object,
+ Properties: map[string]jsonschema.Definition{
+ "text": {
+ Type: jsonschema.String,
+ Description: "The text that needs to be sectionized",
+ },
+ },
+ Required: []string{"text"},
+ },
+ }},
+ Messages: []openai.ChatCompletionMessage{
+ {
+ Role: openai.ChatMessageRoleSystem,
+ Content: "You are an AI that breaks up text into logical sections. For code, each function would be its own section. Sections can also contain subsections.",
+ },
+ {
+ Role: openai.ChatMessageRoleUser,
+ Content: fmt.Sprintf("Please sectionize the text below: \n %s", text),
+ },
+ },
+ },
+ )
+
+ 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 == "sectionize" {
+ fnCall := choice.Message.FunctionCall
+ byteRes = []byte(fnCall.Arguments)
+ }
+ }
+
+ if len(byteRes) == 0 {
+ return nil, fmt.Errorf("no sectionize function call found in response")
+ }
+
+ // validate the JSON response
+ var sectionizeResp types.SectionizeResponse
+ if err := json.Unmarshal(byteRes, &sectionizeResp); err != nil {
+ return nil, err
+ }
+
+ spew.Dump(sectionizeResp)
+
+ return byteRes, nil
+}
+
+ spew.Dump(sectionizeResp)
+
+ return byteRes, nil
+}
diff --git a/server/types/section.go b/server/types/section.go
new file mode 100644
index 00000000..a658a4d2
--- /dev/null
+++ b/server/types/section.go
@@ -0,0 +1,15 @@
+package types
+
+// Section represents a section in a text document
+// It can have sub-sections as well
+type Section struct {
+ Name string
+ Content string
+ Subsections []Section
+}
+
+// SectionizeResponse represents the response from the sectionize function
+// It consists of sections for the document
+type SectionizeResponse struct {
+ Sections []Section
+}