Skip to content

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

Enhance the REPL with multi-line input (\multi), partial buffers (\send), and file execution (\run), plus improved state-driven Chat vs. Tell mode and better context handling. Unify code block partial tag parsing, ensuring unambiguous references and updates. Introduce large context fallback options for planner, coder, and context loader, removing the old ai_models.go in favor of flexible plan settings. Update _apply.sh prompts to clarify usage, add “press enter to continue” in diffs, and streamline auto-context logic for both chat/tell modes, refining plan building around missing file references.

Dane Schneider89332edd152b
diff --git a/app/cli/cmd/chat.go b/app/cli/cmd/chat.go
index 71d99346..0936d71b 100644
--- a/app/cli/cmd/chat.go
+++ b/app/cli/cmd/chat.go
@@ -12,7 +12,7 @@ import (
var chatCmd = &cobra.Command{
Use: "chat [prompt]",
- Aliases: []string{"ch"},
+ Aliases: []string{"c"},
Short: "Chat without making changes",
// Long: ``,
Args: cobra.RangeArgs(0, 1),
diff --git a/app/cli/cmd/current.go b/app/cli/cmd/current.go
index 0e3067bc..9bd67aa8 100644
--- a/app/cli/cmd/current.go
+++ b/app/cli/cmd/current.go
@@ -52,7 +52,6 @@ func current(cmd *cobra.Command, args []string) {
table := lib.GetCurrentPlanTable(plan, currentBranchesByPlanId, nil)
fmt.Println(table)
- fmt.Println()
term.PrintCmds("", "tell", "ls", "plans")
}
diff --git a/app/cli/cmd/diffs.go b/app/cli/cmd/diffs.go
index dbcb4ffe..6a05aec7 100644
--- a/app/cli/cmd/diffs.go
+++ b/app/cli/cmd/diffs.go
@@ -108,9 +108,10 @@ func diffs(cmd *cobra.Command, args []string) {
ui.OpenURL("Showing diffs in your default browser...", fmt.Sprintf("http://localhost:%d", port))
fmt.Println()
- color.New(color.Bold).Println("ctrl+c to exit")
+ color.New(color.Bold).Println("Press enter to continue")
- select {} // wait forever
+ // Wait for enter key
+ fmt.Scanln()
} else {
if plainTextOutput {
fmt.Println(diffs)
diff --git a/app/cli/cmd/models.go b/app/cli/cmd/models.go
index c6e1f889..eaed8588 100644
--- a/app/cli/cmd/models.go
+++ b/app/cli/cmd/models.go
@@ -447,18 +447,48 @@ func renderSettings(settings *shared.PlanSettings) {
fmt.Sprintf("%.1f", config.TopP),
fmt.Sprintf("%d 🪙", config.GetReservedOutputTokens()),
})
+
+ // Add large context fallback if present
+ if config.LargeContextFallback != nil {
+ table.Append([]string{
+ "└─ large-" + role,
+ string(config.LargeContextFallback.BaseModelConfig.Provider),
+ config.LargeContextFallback.BaseModelConfig.ModelName,
+ fmt.Sprintf("%.1f", config.LargeContextFallback.Temperature),
+ fmt.Sprintf("%.1f", config.LargeContextFallback.TopP),
+ fmt.Sprintf("%d 🪙", config.LargeContextFallback.GetReservedOutputTokens()),
+ })
+ }
+ }
+
+ // Handle planner separately since it has a different fallback structure
+ table.Append([]string{
+ string(shared.ModelRolePlanner),
+ string(modelPack.Planner.BaseModelConfig.Provider),
+ modelPack.Planner.BaseModelConfig.ModelName,
+ fmt.Sprintf("%.1f", modelPack.Planner.Temperature),
+ fmt.Sprintf("%.1f", modelPack.Planner.TopP),
+ fmt.Sprintf("%d 🪙", modelPack.Planner.GetReservedOutputTokens()),
+ })
+ if modelPack.Planner.PlannerLargeContextFallback != nil {
+ table.Append([]string{
+ "└─ large-planner",
+ string(modelPack.Planner.PlannerLargeContextFallback.BaseModelConfig.Provider),
+ modelPack.Planner.PlannerLargeContextFallback.BaseModelConfig.ModelName,
+ fmt.Sprintf("%.1f", modelPack.Planner.PlannerLargeContextFallback.Temperature),
+ fmt.Sprintf("%.1f", modelPack.Planner.PlannerLargeContextFallback.TopP),
+ fmt.Sprintf("%d 🪙", modelPack.Planner.PlannerLargeContextFallback.GetReservedOutputTokens()),
+ })
}
- addModelRow(string(shared.ModelRolePlanner), modelPack.Planner.ModelRoleConfig)
- addModelRow(string(shared.ModelRoleCoder), modelPack.GetCoder())
addModelRow(string(shared.ModelRoleContextLoader), modelPack.GetContextLoader())
+ addModelRow(string(shared.ModelRoleCoder), modelPack.GetCoder())
addModelRow(string(shared.ModelRolePlanSummary), modelPack.PlanSummary)
addModelRow(string(shared.ModelRoleBuilder), modelPack.Builder)
addModelRow(string(shared.ModelRoleWholeFileBuilder), modelPack.GetWholeFileBuilder())
addModelRow(string(shared.ModelRoleName), modelPack.Namer)
addModelRow(string(shared.ModelRoleCommitMsg), modelPack.CommitMsg)
addModelRow(string(shared.ModelRoleExecStatus), modelPack.ExecStatus)
- addModelRow(string(shared.ModelRoleWholeFileBuilder), modelPack.GetWholeFileBuilder())
table.Render()
fmt.Println()
@@ -468,8 +498,8 @@ func renderSettings(settings *shared.PlanSettings) {
table.SetAutoWrapText(false)
table.SetHeader([]string{"Max Tokens", "Max Convo Tokens"})
table.Append([]string{
- fmt.Sprintf("%d", modelPack.Planner.BaseModelConfig.MaxTokens),
- fmt.Sprintf("%d", modelPack.Planner.MaxConvoTokens),
+ fmt.Sprintf("%d", modelPack.Planner.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens),
+ fmt.Sprintf("%d", modelPack.Planner.GetFinalLargeContextFallback().MaxConvoTokens),
})
table.Render()
fmt.Println()
diff --git a/app/cli/cmd/new.go b/app/cli/cmd/new.go
index a92a6a17..eb80b3ab 100644
--- a/app/cli/cmd/new.go
+++ b/app/cli/cmd/new.go
@@ -112,7 +112,7 @@ func new(cmd *cobra.Command, args []string) {
var cmds []string
if term.IsRepl {
- return
+ cmds = []string{"config", "plans", "cd", "models"}
} else {
cmds = []string{"tell", "chat", "config"}
}
diff --git a/app/cli/cmd/repl.go b/app/cli/cmd/repl.go
index 114121cc..b9ecfe89 100644
--- a/app/cli/cmd/repl.go
+++ b/app/cli/cmd/repl.go
@@ -3,7 +3,7 @@ package cmd
import (
"fmt"
"os"
- "os/exec"
+ "path/filepath"
"plandex/api"
"plandex/auth"
"plandex/fs"
@@ -11,6 +11,7 @@ import (
"plandex/term"
"plandex/types"
"plandex/version"
+ "regexp"
"sort"
"strings"
"unicode"
@@ -24,31 +25,15 @@ import (
pstrings "github.com/plandex-ai/go-prompt/strings"
)
-var cliSuggestions []prompt.Suggest
-var projectPaths *types.ProjectPaths
-
-type replMode string
-
-const (
- replModeTell replMode = "tell"
- replModeChat replMode = "chat"
-)
-
-type ReplState struct {
- mode replMode
- isMulti bool
-}
-
var replCmd = &cobra.Command{
Use: "repl",
Short: "Start interactive Plandex REPL",
Run: runRepl,
}
-var replState ReplState = ReplState{
- mode: replModeTell,
- isMulti: false,
-}
+var cliSuggestions []prompt.Suggest
+var projectPaths *types.ProjectPaths
+var currentPrompt *prompt.Prompt
func init() {
RootCmd.AddCommand(replCmd)
@@ -58,12 +43,23 @@ func init() {
for _, config := range term.CliCommands {
if config.Repl {
- cliSuggestions = append(cliSuggestions, prompt.Suggest{Text: "\\" + config.Cmd, Description: config.Desc})
+ desc := config.Desc
+ if config.Alias != "" {
+ desc = fmt.Sprintf("(\\%s) %s", config.Alias, desc)
+ }
+ cliSuggestions = append(cliSuggestions, prompt.Suggest{Text: "\\" + config.Cmd, Description: desc})
}
}
}
func runRepl(cmd *cobra.Command, args []string) {
+ term.SetIsRepl(true)
+ auth.MustResolveAuthWithOrg()
+ lib.MustResolveOrCreateProject()
+
+ term.StartSpinner("")
+ lib.LoadState()
+
chatFlag, err := cmd.Flags().GetBool("chat")
if err != nil {
term.OutputErrorAndExit("Error getting chat flag: %v", err)
@@ -79,104 +75,120 @@ func runRepl(cmd *cobra.Command, args []string) {
}
if chatFlag {
- replState.mode = replModeChat
+ lib.CurrentReplState.Mode = lib.ReplModeChat
+ lib.WriteState()
} else if tellFlag {
- replState.mode = replModeTell
+ lib.CurrentReplState.Mode = lib.ReplModeTell
+ lib.WriteState()
}
- term.StartSpinner("")
-
- term.SetIsRepl(true)
-
- auth.MustResolveAuthWithOrg()
- lib.MustResolveOrCreateProject()
-
+ afterNew := false
if lib.CurrentPlanId == "" {
+ os.Setenv("PLANDEX_DISABLE_SUGGESTIONS", "1")
newCmd.Run(newCmd, []string{})
+ os.Setenv("PLANDEX_DISABLE_SUGGESTIONS", "")
+ afterNew = true
}
projectPaths, err = fs.GetProjectPaths(fs.Cwd)
if err != nil {
color.New(term.ColorHiRed).Printf("Error getting project paths: %v\n", err)
}
-
- replWelcome(false)
+ replWelcome(afterNew, false)
var p *prompt.Prompt
p = prompt.New(
func(in string) { executor(in, p) },
- prompt.WithPrefix("👉 "),
+ prompt.WithPrefixCallback(func() string {
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ return "⚡️ "
+ } else if lib.CurrentReplState.Mode == lib.ReplModeChat {
+ return "💬 "
+ }
+ return "👉 "
+ }),
prompt.WithTitle("Plandex "+version.Version),
prompt.WithSelectedSuggestionBGColor(prompt.LightGray),
prompt.WithSuggestionBGColor(prompt.DarkGray),
prompt.WithCompletionOnDown(),
prompt.WithCompleter(completer),
prompt.WithExecuteOnEnterCallback(executeOnEnter),
+ prompt.WithHistory(lib.GetHistory()),
)
+ currentPrompt = p
p.Run()
}
func getSuggestions() []prompt.Suggest {
suggestions := []prompt.Suggest{}
- if replState.isMulti {
+ if lib.CurrentReplState.IsMulti {
suggestions = append(suggestions, []prompt.Suggest{
- {Text: "\\send", Description: "Send the current prompt"},
- {Text: "\\multi", Description: "Turn multi-line mode off"},
- {Text: "\\quit", Description: "Exit the REPL"},
+ {Text: "\\send", Description: "(\\s) Send the current prompt"},
+ {Text: "\\multi", Description: "(\\m) Turn multi-line mode off"},
+ {Text: "\\quit", Description: "(\\q) Exit the REPL"},
}...)
}
- if replState.mode == replModeTell {
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
suggestions = append(suggestions, []prompt.Suggest{
- {Text: "\\chat", Description: "Switch to 'chat' mode to have a conversation without making changes"},
+ {Text: "\\chat", Description: "(\\ch) Switch to 'chat' mode to have a conversation without making changes"},
}...)
- } else if replState.mode == replModeChat {
+ } else if lib.CurrentReplState.Mode == lib.ReplModeChat {
suggestions = append(suggestions, []prompt.Suggest{
- {Text: "\\tell", Description: "Switch to 'tell' mode for implementation"},
+ {Text: "\\tell", Description: "(\\t) Switch to 'tell' mode for implementation"},
}...)
}
- if !replState.isMulti {
+ if !lib.CurrentReplState.IsMulti {
suggestions = append(suggestions, []prompt.Suggest{
- {Text: "\\multi", Description: "Turn multi-line mode on"},
- {Text: "\\quit", Description: "Exit the REPL"},
+ {Text: "\\multi", Description: "(\\m) Turn multi-line mode on"},
+ {Text: "\\run", Description: "(\\r) Run a file through tell/chat based on current mode"},
+ {Text: "\\quit", Description: "(\\q) Exit the REPL"},
}...)
}
// Add help command suggestion
- suggestions = append(suggestions, prompt.Suggest{Text: "\\help", Description: "REPL info and list of commands"})
+ suggestions = append(suggestions, prompt.Suggest{Text: "\\help", Description: "(\\h) REPL info and list of commands"})
suggestions = append(suggestions, cliSuggestions...)
for path := range projectPaths.ActivePaths {
if path == "." {
continue
}
+
+ isDir := projectPaths.ActiveDirs[path]
+
+ if isDir {
+ path += "/"
+ }
+
suggestions = append(suggestions, prompt.Suggest{Text: "@" + path})
+
+ loadArgs := path
+ if isDir {
+ loadArgs += " -r"
+ }
+ suggestions = append(suggestions, prompt.Suggest{Text: "\\load " + loadArgs})
+
+ if filepath.Ext(path) == ".md" || filepath.Ext(path) == ".txt" {
+ suggestions = append(suggestions, prompt.Suggest{Text: "\\run " + path})
+ }
}
return suggestions
}
func executeOnEnter(p *prompt.Prompt, indentSize int) (int, bool) {
- if replState.isMulti {
- input := p.Buffer().Text()
- input = strings.TrimSpace(input)
- lines := strings.Split(input, "\n")
- lastLine := lines[len(lines)-1]
- lastLine = strings.TrimSpace(lastLine)
- lastLineWords := strings.Fields(lastLine)
- lastLineLastWord := ""
- if len(lastLineWords) > 0 {
- lastLineLastWord = lastLineWords[len(lastLineWords)-1]
- }
+ input := p.Buffer().Text()
+ cmd, _ := parseCommand(input)
- if strings.HasPrefix(lastLineLastWord, "\\") ||
- strings.HasPrefix(lastLineLastWord, "@") { // @file
- return 0, true
- }
+ if cmd != "" {
+ return 0, true
+ }
+ if lib.CurrentReplState.IsMulti {
return 0, false
}
@@ -184,6 +196,8 @@ func executeOnEnter(p *prompt.Prompt, indentSize int) (int, bool) {
}
func executor(in string, p *prompt.Prompt) {
+ defer lib.WriteHistory(in)
+
in = strings.TrimSpace(in)
lines := strings.Split(in, "\n")
lastLine := lines[len(lines)-1]
@@ -194,6 +208,21 @@ func executor(in string, p *prompt.Prompt) {
return
}
+ // Handle plandex/pdx command prefix
+ if strings.HasPrefix(lastLine, "plandex ") || strings.HasPrefix(lastLine, "pdx ") {
+ fmt.Println()
+ parts := strings.Fields(lastLine)
+ if len(parts) > 1 {
+ args := parts[1:] // Skip the "plandex" or "pdx" command
+ _, err := lib.ExecPlandexCommand(args)
+ if err != nil {
+ color.New(term.ColorHiRed).Printf("Error executing command: %v\n", err)
+ }
+ }
+ fmt.Println()
+ return
+ }
+
// Find the last \ or @ in the last line
lastBackslashIndex := strings.LastIndex(lastLine, "\\")
lastAtIndex := strings.LastIndex(lastLine, "@")
@@ -203,36 +232,43 @@ func executor(in string, p *prompt.Prompt) {
preservedBuffer = strings.Join(lines[:len(lines)-1], "\n") + "\n"
}
+ suggestions, _, _ := completer(prompt.Document{Text: in})
+
// Handle file references
if lastAtIndex != -1 && lastAtIndex > lastBackslashIndex {
paths := strings.Split(lastLine, "@")
numPaths := len(paths)
+
filteredPaths := []string{}
+
for i, path := range paths {
p := strings.TrimSpace(path)
- if p == "" {
- continue
- } else if i == 0 {
+ if i == 0 {
+ // text before the @
preservedBuffer += p + " "
+ continue
}
- if !projectPaths.ActivePaths[p] && len(latestFilteredSuggestions) > 0 && i == numPaths-1 {
- p = strings.Replace(latestFilteredSuggestions[0].Text, "@", "", 1)
+ if (p == "" || !projectPaths.ActivePaths[p]) && len(suggestions) > 0 && i == numPaths-1 {
+ p = strings.Replace(suggestions[0].Text, "@", "", 1)
filteredPaths = append(filteredPaths, p)
} else if projectPaths.ActivePaths[p] {
filteredPaths = append(filteredPaths, p)
}
}
- args := []string{"load"}
- args = append(args, filteredPaths...)
- args = append(args, "-r")
- execPlandexCommand(args)
- fmt.Println()
- if preservedBuffer != "" {
- p.InsertTextMoveCursor(preservedBuffer, true)
+ if len(filteredPaths) > 0 {
+ args := []string{"load"}
+ args = append(args, filteredPaths...)
+ args = append(args, "-r")
+ fmt.Println()
+ lib.ExecPlandexCommand(args)
+ fmt.Println()
+ if preservedBuffer != "" {
+ p.InsertTextMoveCursor(preservedBuffer, true)
+ }
+ return
}
- return
}
// Handle commands
@@ -249,10 +285,11 @@ func executor(in string, p *prompt.Prompt) {
// Handle built-in REPL commands
switch {
- case cmd == "quit" || strings.HasPrefix("quit", cmd):
+ case cmd == "quit" || cmd == lib.ReplCmdAliases["quit"]:
+ lib.WriteHistory(in)
os.Exit(0)
- case cmd == "help" || strings.HasPrefix("help", cmd):
+ case cmd == "help" || cmd == lib.ReplCmdAliases["help"]:
if lastBackslashIndex > 0 {
preservedBuffer += lastLine[:lastBackslashIndex]
}
@@ -263,31 +300,23 @@ func executor(in string, p *prompt.Prompt) {
}
return
- case cmd == "multi" || strings.HasPrefix("multi", cmd):
+ case cmd == "multi" || cmd == lib.ReplCmdAliases["multi"]:
if lastBackslashIndex > 0 {
preservedBuffer += lastLine[:lastBackslashIndex]
}
fmt.Println()
- if replState.isMulti {
- replState.isMulti = false
- color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 🙅‍♂️ Multi-line mode is disabled ")
- fmt.Printf("%s for multi-line editing mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
- fmt.Printf("%s to send prompt\n", color.New(term.ColorHiCyan, color.Bold).Sprint("enter"))
- } else {
- replState.isMulti = true
- color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 📖 Multi-line mode is enabled ")
- fmt.Printf("%s to exit multi-line mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
- fmt.Printf("%s to send prompt\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\send"))
- fmt.Printf("%s for line breaks\n", color.New(term.ColorHiCyan, color.Bold).Sprint("enter"))
- }
+ lib.CurrentReplState.IsMulti = !lib.CurrentReplState.IsMulti
+ showMultiLineMode()
+ lib.WriteState()
fmt.Println()
if preservedBuffer != "" {
p.InsertTextMoveCursor(preservedBuffer, true)
}
return
- case cmd == "send" || strings.HasPrefix("send", cmd):
- input = strings.TrimSuffix(input, "\\send")
+ case cmd == "send" || cmd == lib.ReplCmdAliases["send"]:
+ split := strings.Split(input, "\\send")
+ input = strings.TrimSpace(split[0])
input = strings.TrimSpace(input)
if input == "" {
fmt.Println()
@@ -296,26 +325,38 @@ func executor(in string, p *prompt.Prompt) {
return
}
- case cmd == "tell" || strings.HasPrefix("tell", cmd):
+ case cmd == "tell" || cmd == lib.ReplCmdAliases["tell"]:
if lastBackslashIndex > 0 {
preservedBuffer += lastLine[:lastBackslashIndex]
}
- replState.mode = replModeTell
- fmt.Println()
- color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 💻 Tell mode is enabled ")
- fmt.Println()
+ lib.CurrentReplState.Mode = lib.ReplModeTell
+ lib.WriteState()
+ showReplMode()
if preservedBuffer != "" {
p.InsertTextMoveCursor(preservedBuffer, true)
}
return
- case cmd == "chat" || strings.HasPrefix("chat", cmd):
+ case cmd == "chat" || cmd == lib.ReplCmdAliases["chat"]:
+ if lastBackslashIndex > 0 {
+ preservedBuffer += lastLine[:lastBackslashIndex]
+ }
+ lib.CurrentReplState.Mode = lib.ReplModeChat
+ lib.WriteState()
+ showReplMode()
+ if preservedBuffer != "" {
+ p.InsertTextMoveCursor(preservedBuffer, true)
+ }
+ return
+
+ case cmd == "run" || cmd == lib.ReplCmdAliases["run"]:
if lastBackslashIndex > 0 {
preservedBuffer += lastLine[:lastBackslashIndex]
}
- replState.mode = replModeChat
fmt.Println()
- color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 💬 Chat mode is enabled ")
+ if err := handleRunCommand(args); err != nil {
+ color.New(term.ColorHiRed).Printf("Run command failed: %v\n", err)
+ }
fmt.Println()
if preservedBuffer != "" {
p.InsertTextMoveCursor(preservedBuffer, true)
@@ -324,49 +365,105 @@ func executor(in string, p *prompt.Prompt) {
default:
// Check CLI commands
+ var matchedCmd string
+
for _, config := range term.CliCommands {
- if (cmd == config.Cmd || (config.Alias != "" && cmd == config.Alias) || strings.HasPrefix(config.Cmd, cmd)) && config.Repl {
- if lastBackslashIndex > 0 {
- preservedBuffer += lastLine[:lastBackslashIndex]
- }
- fmt.Println()
- execArgs := []string{config.Cmd}
- execArgs = append(execArgs, args...)
- err := execPlandexCommand(execArgs)
- if err != nil {
- color.New(term.ColorHiRed).Printf("Error executing command: %v\n", err)
- }
- fmt.Println()
- if preservedBuffer != "" {
- p.InsertTextMoveCursor(preservedBuffer, true)
+ if (cmd == config.Cmd || (config.Alias != "" && cmd == config.Alias)) && config.Repl {
+ matchedCmd = config.Cmd
+ break
+ }
+ }
+
+ if matchedCmd == "" {
+ for _, config := range term.CliCommands {
+ if strings.HasPrefix(config.Cmd, cmd) && config.Repl {
+ matchedCmd = config.Cmd
+ break
}
- return
}
}
+
+ if matchedCmd != "" {
+ // fmt.Println("> plandex " + config.Cmd)
+ if lastBackslashIndex > 0 {
+ preservedBuffer += lastLine[:lastBackslashIndex]
+ }
+ fmt.Println()
+ execArgs := []string{matchedCmd}
+ execArgs = append(execArgs, args...)
+ _, err := lib.ExecPlandexCommand(execArgs)
+ if err != nil {
+ color.New(term.ColorHiRed).Printf("Error executing command: %v\n", err)
+ }
+ fmt.Println()
+ if preservedBuffer != "" {
+ p.InsertTextMoveCursor(preservedBuffer, true)
+ }
+ return
+ }
}
}
// Handle non-command input based on mode
- if replState.mode == replModeTell {
+ var output string
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ fmt.Println()
args := []string{"tell", input}
- err := execPlandexCommand(args)
+ var err error
+ output, err = lib.ExecPlandexCommand(args)
if err != nil {
color.New(term.ColorHiRed).Printf("Error executing tell: %v\n", err)
}
- } else if replState.mode == replModeChat {
+ } else if lib.CurrentReplState.Mode == lib.ReplModeChat {
+ fmt.Println()
args := []string{"chat", input}
- err := execPlandexCommand(args)
+ var err error
+ output, err = lib.ExecPlandexCommand(args)
if err != nil {
color.New(term.ColorHiRed).Printf("Error executing chat: %v\n", err)
}
- }
+ rx := regexp.MustCompile(`(?i)switch(ing)? to (tell|implementation) mode`)
+
+ if rx.MatchString(output) {
+ res, err := term.ConfirmYesNo("Switch to tell mode for implementation?")
+ if err != nil {
+ color.New(term.ColorHiRed).Printf("Error confirming yes/no: %v\n", err)
+ }
+ if res {
+ lib.CurrentReplState.Mode = lib.ReplModeTell
+ lib.WriteState()
+ fmt.Println()
+ color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" ⚡️ Tell mode is enabled ")
+ fmt.Println()
+ fmt.Println("Now that you're in tell mode, you can either begin the implementation based on the conversation so far, or you can send another prompt to begin the implementation with additional information or instructions.")
+ fmt.Println()
+ beginImplOpt := "Begin implementation"
+ anotherPromptOpt := "Send another prompt"
+ sel, err := term.SelectFromList("What would you like to do?", []string{beginImplOpt, anotherPromptOpt})
+ if err != nil {
+ color.New(term.ColorHiRed).Printf("Error selecting from list: %v\n", err)
+ }
+ if sel == beginImplOpt {
+ fmt.Println()
+ args := []string{"tell", "Go ahead with the implementation based on what we've discussed so far."}
+ _, err := lib.ExecPlandexCommand(args)
+ if err != nil {
+ color.New(term.ColorHiRed).Printf("Error executing tell: %v\n", err)
+ }
+ }
+ }
+ }
+ }
fmt.Println()
}
-var latestFilteredSuggestions []prompt.Suggest
-
func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstrings.RuneNumber) {
+ // Don't show suggestions if we're navigating history
+ if currentPrompt.IsNavigatingHistory() {
+ return []prompt.Suggest{}, 0, 0
+ }
+
endIndex := in.CurrentRuneIndex()
lines := strings.Split(in.Text, "\n")
@@ -374,7 +471,6 @@ func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstri
// Don't show suggestions if we're not on the last line
if currentLineNum < len(lines)-1 {
- latestFilteredSuggestions = []prompt.Suggest{}
return []prompt.Suggest{}, 0, 0
}
@@ -383,6 +479,27 @@ func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstri
lastLine = lines[len(lines)-2]
}
+ // Handle plandex/pdx command prefix
+ if strings.HasPrefix(lastLine, "plandex ") || strings.HasPrefix(lastLine, "pdx ") {
+ parts := strings.Fields(lastLine)
+ var prefix string
+ if len(parts) > 1 {
+ prefix = parts[len(parts)-1]
+ }
+ startIndex := endIndex - pstrings.RuneNumber(len(prefix))
+
+ suggestions := []prompt.Suggest{}
+ for _, config := range term.CliCommands {
+ suggestions = append(suggestions, prompt.Suggest{
+ Text: config.Cmd,
+ Description: config.Desc,
+ })
+ }
+
+ filtered := prompt.FilterFuzzy(suggestions, prefix, true)
+ return filtered, startIndex, endIndex
+ }
+
// Find the last valid \ or @ in the current line
lastBackslashIndex := -1
lastAtIndex := -1
@@ -415,7 +532,6 @@ func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstri
var startIndex pstrings.RuneNumber
if lastBackslashIndex == -1 && lastAtIndex == -1 {
- latestFilteredSuggestions = []prompt.Suggest{}
return []prompt.Suggest{}, 0, 0
}
@@ -432,38 +548,38 @@ func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstri
// Verify this is at the end of the line (allowing for trailing spaces)
if !strings.HasSuffix(strings.TrimSpace(lastLine), strings.TrimSpace(w)) {
- latestFilteredSuggestions = []prompt.Suggest{}
return []prompt.Suggest{}, 0, 0
}
+ wTrimmed := strings.TrimSpace(strings.TrimPrefix(w, "\\"))
+ parts := strings.Split(wTrimmed, " ")
+ wCmd := parts[0]
+
// For commands, verify it starts with an actual command
if strings.HasPrefix(w, "\\") {
isValidCommand := false
- wTrimmed := strings.TrimSpace(strings.TrimPrefix(w, "\\"))
for _, config := range term.CliCommands {
- if strings.HasPrefix(config.Cmd, wTrimmed) ||
- (config.Alias != "" && strings.HasPrefix(config.Alias, wTrimmed)) {
+ if strings.HasPrefix(config.Cmd, wCmd) ||
+ (config.Alias != "" && strings.HasPrefix(config.Alias, wCmd)) {
isValidCommand = true
break
}
}
// Also check built-in REPL commands
- if strings.HasPrefix("quit", wTrimmed) ||
- strings.HasPrefix("multi", wTrimmed) ||
- strings.HasPrefix("tell", wTrimmed) ||
- strings.HasPrefix("chat", wTrimmed) ||
- strings.HasPrefix("send", wTrimmed) {
+ if strings.HasPrefix("quit", wCmd) ||
+ strings.HasPrefix("multi", wCmd) ||
+ strings.HasPrefix("tell", wCmd) ||
+ strings.HasPrefix("chat", wCmd) ||
+ strings.HasPrefix("send", wCmd) ||
+ strings.HasPrefix("run", wCmd) {
isValidCommand = true
}
- if !isValidCommand && wTrimmed != "" {
- latestFilteredSuggestions = []prompt.Suggest{}
+ if !isValidCommand && wCmd != "" {
return []prompt.Suggest{}, 0, 0
}
}
fuzzySuggestions := prompt.FilterFuzzy(getSuggestions(), w, true)
-
- // Rest of the existing sorting logic
prefixMatches := prompt.FilterHasPrefix(getSuggestions(), w, true)
if strings.TrimSpace(w) != "\\" {
@@ -497,36 +613,46 @@ func completer(in prompt.Document) ([]prompt.Suggest, pstrings.RuneNumber, pstri
fuzzySuggestions = append(prefixMatches, nonPrefixFuzzy...)
}
- latestFilteredSuggestions = fuzzySuggestions
- return fuzzySuggestions, startIndex, endIndex
-}
+ var aliasMatch string
-// execPlandexCommand spawns the same binary, wiring std streams directly so you
-// don't have to capture output. Any os.Exit calls in the child won't kill your REPL.
-func execPlandexCommand(args []string) error {
- // Subprocess runs the same binary (os.Args[0]) with your desired args.
- cmd := exec.Command(os.Args[0], args...)
- cmd.Env = append(os.Environ(), "PLANDEX_REPL=1")
-
- // Connect the child's standard streams to the parent.
- cmd.Stdin = os.Stdin
- cmd.Stdout = os.Stdout
- cmd.Stderr = os.Stderr
-
- // Run it. If the child calls os.Exit(1), only that child is killed,
- // and Run() will return an error here. Your REPL stays alive.
- err := cmd.Run()
- if err != nil {
- if _, ok := err.(*exec.ExitError); ok {
- return nil
+ if lib.ReplCmdAliases[wTrimmed] != "" {
+ aliasMatch = "\\" + lib.ReplCmdAliases[wTrimmed]
+ } else {
+ for _, s := range term.CliCommands {
+ if s.Alias == wTrimmed {
+ aliasMatch = "\\" + s.Cmd
+ break
+ }
}
- // If it's *not* an exit error, it might be a startup error (like file not found).
- return err
}
- return nil
+
+ if aliasMatch != "" {
+ // put the suggestion with the alias match at the beginning
+ var matched prompt.Suggest
+ found := false
+ for _, s := range fuzzySuggestions {
+ if s.Text == aliasMatch {
+ matched = s
+ found = true
+ break
+ }
+ }
+ if found {
+ newSuggestions := []prompt.Suggest{}
+ newSuggestions = append(newSuggestions, matched)
+ for _, s := range fuzzySuggestions {
+ if s.Text != aliasMatch {
+ newSuggestions = append(newSuggestions, s)
+ }
+ }
+ fuzzySuggestions = newSuggestions
+ }
+ }
+
+ return fuzzySuggestions, startIndex, endIndex
}
-func replWelcome(isHelp bool) {
+func replWelcome(afterNew, isHelp bool) {
// print REPL welcome message and basic info
errCh := make(chan error, 2)
var plan *shared.Plan
@@ -559,8 +685,11 @@ func replWelcome(isHelp bool) {
term.StopSpinner()
- fmt.Println()
- color.New(color.FgHiWhite, color.BgBlue, color.Bold).Print(" 👋 Welcome to the Plandex REPL ")
+ if !afterNew {
+ fmt.Println()
+ }
+
+ color.New(color.FgHiWhite, color.BgBlue, color.Bold).Print(" 👋 Welcome to Plandex ")
versionStr := version.Version
if versionStr != "development" {
@@ -576,9 +705,9 @@ func replWelcome(isHelp bool) {
table.SetHeader([]string{"Current Plan", "Branch", "REPL Mode", "Auto Mode", "Context"})
var replModeIcon string
- if replState.mode == replModeTell {
- replModeIcon = "💻"
- } else if replState.mode == replModeChat {
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ replModeIcon = "⚡️"
+ } else if lib.CurrentReplState.Mode == lib.ReplModeChat {
replModeIcon = "💬"
}
@@ -592,7 +721,7 @@ func replWelcome(isHelp bool) {
table.Append([]string{
color.New(term.ColorHiGreen, color.Bold).Sprint(plan.Name),
lib.CurrentBranch,
- strings.Title(string(replState.mode)) + " " + replModeIcon,
+ strings.Title(string(lib.CurrentReplState.Mode)) + " " + replModeIcon,
shared.AutoModeLabels[config.AutoMode],
contextMode,
})
@@ -603,41 +732,221 @@ func replWelcome(isHelp bool) {
color.New(color.FgHiWhite).Printf("%s for commands\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\"))
color.New(color.FgHiWhite).Printf("%s for loading files into context\n", color.New(term.ColorHiCyan, color.Bold).Sprint("@"))
+ color.New(color.FgHiWhite).Printf("%s (\\h) for help\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\help"))
+ color.New(color.FgHiWhite).Printf("%s (\\q) to exit\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\quit"))
- if replState.mode == replModeTell {
- color.New(color.FgHiWhite).Printf("%s for chat mode → chat without writing code\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\chat"))
- } else {
- color.New(color.FgHiWhite).Printf("%s for tell mode → implement tasks\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\tell"))
+ showReplMode()
+ showMultiLineMode()
+ fmt.Println()
+
+ if !isHelp {
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ color.New(color.FgHiWhite, color.BgBlue, color.Bold).Println(" Describe a coding task 👇 ")
+ } else {
+ color.New(color.FgHiWhite, color.BgBlue, color.Bold).Println(" Ask a question or chat 👇 ")
+ }
+
+ fmt.Println()
}
+}
+
+func replHelp() {
+ replWelcome(false, true)
+ term.PrintHelpAllCommands()
+}
- helpFn := func() {
- color.New(color.FgHiWhite).Printf("%s for help\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\help"))
+func showReplMode() {
+ fmt.Println()
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" ⚡️ Tell mode is enabled ")
+ color.New(color.FgHiWhite).Printf("%s (\\ch) switch to chat mode to chat without writing code or making changes\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\chat"))
+ } else if lib.CurrentReplState.Mode == lib.ReplModeChat {
+ color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 💬 Chat mode is enabled ")
+ color.New(color.FgHiWhite).Printf("%s (\\t) switch to tell mode to start writing code and implementing tasks\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\tell"))
}
- if replState.isMulti {
- color.New(color.FgHiWhite).Printf("%s to exit multi-line mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
- fmt.Printf("%s to send prompt\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\send"))
- helpFn()
+ fmt.Println()
+}
+
+func showMultiLineMode() {
+ if lib.CurrentReplState.IsMulti {
+ color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 🔢 Multi-line mode is enabled ")
+ fmt.Printf("%s to exit multi-line mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
fmt.Printf("%s for line breaks\n", color.New(term.ColorHiCyan, color.Bold).Sprint("enter"))
+ fmt.Printf("%s to send prompt\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\send"))
} else {
- color.New(color.FgHiWhite).Printf("%s for multi-line editing mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
- helpFn()
+ color.New(color.BgMagenta, color.FgHiWhite, color.Bold).Println(" 1️⃣ Multi-line mode is disabled ")
+ fmt.Printf("%s for multi-line editing mode\n", color.New(term.ColorHiCyan, color.Bold).Sprint("\\multi"))
fmt.Printf("%s to send prompt\n", color.New(term.ColorHiCyan, color.Bold).Sprint("enter"))
}
+}
- fmt.Println()
+func parseCommand(in string) (string, string) {
+ in = strings.TrimSpace(in)
+ lines := strings.Split(in, "\n")
+ lastLine := lines[len(lines)-1]
+ lastLine = strings.TrimSpace(lastLine)
- if !isHelp {
- if replState.mode == replModeTell {
- color.New(color.FgHiWhite, color.BgBlue, color.Bold).Println(" 💻 Describe a coding task ")
- } else {
- color.New(color.FgHiWhite, color.BgBlue, color.Bold).Println(" 💬 Ask a question or chat ")
+ input := strings.TrimSpace(in)
+ if input == "" {
+ return "", ""
+ }
+
+ // Handle plandex/pdx command prefix
+ if strings.HasPrefix(lastLine, "plandex ") || strings.HasPrefix(lastLine, "pdx ") {
+ return lastLine, lastLine
+ }
+
+ // Find the last \ or @ in the last line
+ lastBackslashIndex := strings.LastIndex(lastLine, "\\")
+ lastAtIndex := strings.LastIndex(lastLine, "@")
+
+ suggestions, _, _ := completer(prompt.Document{Text: in})
+
+ // Handle file references
+ if lastAtIndex != -1 && lastAtIndex > lastBackslashIndex {
+ paths := strings.Split(lastLine, "@")
+ split2 := strings.SplitN(lastLine, "@", 2)
+ numPaths := len(paths)
+
+ filteredPaths := []string{}
+
+ for i, path := range paths {
+ p := strings.TrimSpace(path)
+ if i == 0 {
+ // text before the @
+ continue
+ }
+
+ if (p == "" || !projectPaths.ActivePaths[p]) && len(suggestions) > 0 && i == numPaths-1 {
+ p = strings.Replace(suggestions[0].Text, "@", "", 1)
+ filteredPaths = append(filteredPaths, p)
+ } else if projectPaths.ActivePaths[p] {
+ filteredPaths = append(filteredPaths, p)
+ }
}
- fmt.Println()
+ if len(filteredPaths) > 0 {
+ res := ""
+ for _, p := range filteredPaths {
+ res += "@" + p + " "
+ }
+ return res, split2[1]
+ }
+ }
+
+ // Handle commands
+ if lastBackslashIndex != -1 {
+ cmdString := strings.TrimSpace(lastLine[lastBackslashIndex+1:])
+ if cmdString == "" {
+ return "", ""
+ }
+
+ // Split into command and args
+ parts := strings.Fields(cmdString)
+ cmd := parts[0]
+ args := parts[1:]
+
+ // Handle built-in REPL commands
+ switch cmd {
+ case "quit", lib.ReplCmdAliases["quit"]:
+ return "\\quit", "\\" + cmdString
+
+ case "help", lib.ReplCmdAliases["help"]:
+ return "\\help", "\\" + cmdString
+
+ case "multi", lib.ReplCmdAliases["multi"]:
+ return "\\multi", "\\" + cmdString
+
+ case "send", lib.ReplCmdAliases["send"]:
+ return "\\send", "\\" + cmdString
+
+ case "tell", lib.ReplCmdAliases["tell"]:
+ return "\\tell", "\\" + cmdString
+
+ case "chat", lib.ReplCmdAliases["chat"]:
+ return "\\chat", "\\" + cmdString
+
+ case "run", lib.ReplCmdAliases["run"]:
+ return "\\run", "\\" + cmdString
+
+ default:
+ // Check CLI commands
+ var matchedCmd string
+
+ for _, config := range term.CliCommands {
+ if (cmd == config.Cmd || (config.Alias != "" && cmd == config.Alias)) && config.Repl {
+ matchedCmd = config.Cmd
+ break
+ }
+ }
+
+ if matchedCmd == "" {
+ for _, config := range term.CliCommands {
+ if strings.HasPrefix(config.Cmd, cmd) && config.Repl {
+ matchedCmd = config.Cmd
+ break
+ }
+ }
+ }
+
+ if matchedCmd != "" {
+ res := matchedCmd
+ if len(args) > 0 {
+ res += " " + strings.Join(args, " ")
+ }
+ return res, "\\" + cmdString
+ }
+ }
}
+
+ return "", ""
}
-func replHelp() {
- replWelcome(true)
- term.PrintHelpAllCommands()
+func isFileInProjectPaths(filePath string) bool {
+ // Convert to absolute path
+ absPath, err := filepath.Abs(filePath)
+ if err != nil {
+ return false
+ }
+
+ // Check if file is within any project path
+ for path := range projectPaths.ActivePaths {
+ projectAbs, err := filepath.Abs(path)
+ if err != nil {
+ continue
+ }
+ if strings.HasPrefix(absPath, projectAbs) {
+ return true
+ }
+ }
+ return false
+}
+
+func handleRunCommand(args []string) error {
+ if len(args) != 1 {
+ return fmt.Errorf("run command requires exactly one file path argument")
+ }
+
+ filePath := args[0]
+
+ // Check if file exists
+ if _, err := os.Stat(filePath); os.IsNotExist(err) {
+ return fmt.Errorf("file does not exist: %s", filePath)
+ }
+
+ // Build command based on current mode
+ var cmdArgs []string
+ if lib.CurrentReplState.Mode == lib.ReplModeTell {
+ cmdArgs = []string{"tell", "-f", filePath}
+ } else {
+ cmdArgs = []string{"chat", "-f", filePath}
+ }
+
+ // Execute the command
+ _, err := lib.ExecPlandexCommand(cmdArgs)
+ if err != nil {
+ return fmt.Errorf("error executing command: %v", err)
+ }
+
+ return nil
}
diff --git a/app/cli/fs/fs.go b/app/cli/fs/fs.go
index 46ef38b1..da418e0d 100644
--- a/app/cli/fs/fs.go
+++ b/app/cli/fs/fs.go
@@ -372,6 +372,8 @@ func GetPaths(baseDir, currentDir string) (*types.ProjectPaths, error) {
return &types.ProjectPaths{
ActivePaths: activePaths,
AllPaths: allPaths,
+ ActiveDirs: activeDirs,
+ AllDirs: allDirs,
PlandexIgnored: ignored,
IgnoredPaths: ignoredPaths,
}, nil
diff --git a/app/cli/go.mod b/app/cli/go.mod
index c2867876..fb7d78d8 100644
--- a/app/cli/go.mod
+++ b/app/cli/go.mod
@@ -5,7 +5,7 @@ go 1.23.3
require (
github.com/atotto/clipboard v0.1.4
github.com/charmbracelet/lipgloss v0.10.0
- github.com/fatih/color v1.16.0
+ github.com/fatih/color v1.18.0
github.com/muesli/reflow v0.3.0
github.com/muesli/termenv v0.15.2
github.com/olekukonko/tablewriter v0.0.5
@@ -46,7 +46,7 @@ require (
golang.org/x/exp v0.0.0-20230626212559-97b1e661b5df // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sync v0.9.0 // indirect
- golang.org/x/sys v0.19.0 // indirect
+ golang.org/x/sys v0.25.0 // indirect
)
require (
@@ -73,3 +73,5 @@ require (
)
replace github.com/plandex/plandex/shared => ../shared
+
+replace github.com/plandex-ai/go-prompt => ../../../go-prompt
diff --git a/app/cli/go.sum b/app/cli/go.sum
index b7a3d589..bb9daa50 100644
--- a/app/cli/go.sum
+++ b/app/cli/go.sum
@@ -308,8 +308,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
-github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
-github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
+github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
+github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/frankban/quicktest v1.14.3/go.mod h1:mgiwOwqx65TmIk1wJ6Q7wvnVMocbUorkibMOrVTHZps=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
@@ -602,8 +602,6 @@ github.com/pkg/term v1.2.0-beta.2 h1:L3y/h2jkuBVFdWiJvNfYfKmzcCnILw7mJWm2JQuMppw
github.com/pkg/term v1.2.0-beta.2/go.mod h1:E25nymQcrSllhX42Ok8MRm1+hyBdHY0dCeiKZ9jpNGw=
github.com/pkoukk/tiktoken-go v0.1.7 h1:qOBHXX4PHtvIvmOtyg1EeKlwFRiMKAcoMp4Q+bLQDmw=
github.com/pkoukk/tiktoken-go v0.1.7/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=
-github.com/plandex-ai/go-prompt v0.0.0-20250130142100-3d8d7ab8d071 h1:0ATy028FeyfsoiMH0hG0F0xzX1Mp1pyBowgKGeEY4Dw=
-github.com/plandex-ai/go-prompt v0.0.0-20250130142100-3d8d7ab8d071/go.mod h1:SqEsJfsIr0GYUyLatvezDOBe6XsCw64E7v33QzeH5PM=
github.com/plandex-ai/survey/v2 v2.3.7 h1:u1o6bflbaBpW8i8krm+91Z2cOcvZcMVS+AjV+rgR8Rk=
github.com/plandex-ai/survey/v2 v2.3.7/go.mod h1:RiBOKRDB5fOQrOzsiAPAN57hYqFKPkCxgSK7twcDOys=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
@@ -990,8 +988,8 @@ golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
-golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
-golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
+golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
+golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
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.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
diff --git a/app/cli/lib/apply.go b/app/cli/lib/apply.go
index 0749767f..44b44bc8 100644
--- a/app/cli/lib/apply.go
+++ b/app/cli/lib/apply.go
@@ -2,6 +2,7 @@ package lib
import (
"bufio"
+ "context"
"fmt"
"log"
"os"
@@ -271,7 +272,7 @@ func handleApplyScript(
execApplyScript(flags, toApply, onErr, toRollback, onExecFail, attempt, onSuccess)
} else {
if toRollback != nil && toRollback.HasChanges() {
- res, err := term.SelectFromList("Skipping execution. Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
+ res, err := term.SelectFromList("Skipping execution. Apply file changes or roll back?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
if err != nil {
onErr("failed to get rollback confirmation user input: %s", err)
@@ -379,31 +380,44 @@ func execApplyScript(
onErr("failed to start command: %s", err)
}
- // Handle SIGINT and SIGTERM -- delete _apply.sh and kill process
+ // Create a context that we can cancel
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+
+ // Handle SIGINT and SIGTERM
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
go func() {
- sig := <-sigChan
- os.Remove(dstPath)
+ select {
+ case sig := <-sigChan:
+ os.Remove(dstPath)
- if toRollback != nil && toRollback.HasChanges() {
- color.New(term.ColorHiRed, color.Bold).Println("🚨 Execution interrupted")
- res, err := term.SelectFromList("Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
- if err != nil {
- onErr("failed to get rollback confirmation user input: %s", err)
- }
+ if toRollback != nil && toRollback.HasChanges() {
+ color.New(term.ColorHiYellow, color.Bold).Println("⚠️ Execution interrupted")
+ res, err := term.SelectFromList("Apply file changes or roll back?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
+ if err != nil {
+ onErr("failed to get rollback confirmation user input: %s", err)
+ }
- if res == string(types.ApplyRollbackOptionRollback) {
- Rollback(toRollback, true)
- } else {
- onSuccess()
+ if res == string(types.ApplyRollbackOptionRollback) {
+ Rollback(toRollback, true)
+ } else {
+ onSuccess()
+ }
}
- }
- execCmd.Process.Signal(sig)
+ execCmd.Process.Signal(sig)
+ case <-ctx.Done():
+ // Exit the goroutine when context is cancelled
+ return
+ }
+ }()
+ defer func() {
+ cancel() // Cancel context to clean up goroutine
+ signal.Stop(sigChan) // Stop catching signals
+ close(sigChan) // Close the channel
}()
- defer signal.Stop(sigChan)
// Read and display output in real-time
scanner := bufio.NewScanner(pipe)
diff --git a/app/cli/lib/repl.go b/app/cli/lib/repl.go
new file mode 100644
index 00000000..6b09dc39
--- /dev/null
+++ b/app/cli/lib/repl.go
@@ -0,0 +1,180 @@
+package lib
+
+import (
+ "encoding/json"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "plandex/fs"
+ "plandex/term"
+)
+
+var ReplSettingsDir string
+
+type ReplMode string
+
+const (
+ ReplModeTell ReplMode = "tell"
+ ReplModeChat ReplMode = "chat"
+)
+
+type ReplState struct {
+ Mode ReplMode
+ IsMulti bool
+}
+
+var CurrentReplState = ReplState{
+ Mode: ReplModeChat,
+ IsMulti: false,
+}
+
+type ReplSettings struct {
+ State ReplState
+ History []string
+}
+
+var ReplCmdAliases = map[string]string{
+ "chat": "ch",
+ "tell": "t",
+ "multi": "m",
+ "quit": "q",
+ "help": "h",
+ "run": "r",
+ "send": "s",
+}
+
+func init() {
+ ReplSettingsDir = filepath.Join(fs.HomePlandexDir, "repl_settings")
+}
+
+func EnsureReplSettingsFile() {
+ if err := os.MkdirAll(ReplSettingsDir, os.ModePerm); err != nil {
+ term.OutputErrorAndExit("Error creating repl history directory: %v", err)
+ }
+
+ settingsFile := filepath.Join(ReplSettingsDir, CurrentProjectId+".json")
+ if _, err := os.Stat(settingsFile); os.IsNotExist(err) {
+ file, err := os.Create(settingsFile)
+ if err != nil {
+ term.OutputErrorAndExit("Error creating history file: %v", err)
+ }
+ defer file.Close()
+
+ // Write empty settings object
+ var settings ReplSettings
+
+ data, err := json.Marshal(settings)
+ if err != nil {
+ term.OutputErrorAndExit("Error converting settings to JSON: %v", err)
+ }
+
+ if _, err := file.Write(data); err != nil {
+ term.OutputErrorAndExit("Error writing to history file: %v", err)
+ }
+ }
+}
+
+func writeSettings(settings *ReplSettings) {
+ settingsFile := filepath.Join(ReplSettingsDir, CurrentProjectId+".json")
+ data, err := json.Marshal(settings)
+ if err != nil {
+ term.OutputErrorAndExit("Error converting settings to JSON: %v", err)
+ }
+
+ if err := os.WriteFile(settingsFile, data, 0644); err != nil {
+ term.OutputErrorAndExit("Error writing settings file: %v", err)
+ }
+}
+
+func getSettings() *ReplSettings {
+ EnsureReplSettingsFile()
+
+ settingsFile := filepath.Join(ReplSettingsDir, CurrentProjectId+".json")
+
+ // Read existing settings
+ data, err := os.ReadFile(settingsFile)
+ if err != nil {
+ term.OutputErrorAndExit("Error reading history file: %v", err)
+ }
+
+ // Parse JSON
+ var settings ReplSettings
+ if err := json.Unmarshal(data, &settings); err != nil {
+ term.OutputErrorAndExit("Error parsing history file: %v", err)
+ }
+
+ return &settings
+}
+
+func LoadState() {
+ settings := getSettings()
+
+ if settings.State.Mode != "" {
+ CurrentReplState = settings.State
+ } else {
+ // Write default state
+ WriteState()
+ }
+}
+
+func WriteState() {
+ settings := getSettings()
+ settings.State = CurrentReplState
+ writeSettings(settings)
+}
+
+func WriteHistory(input string) {
+ settings := getSettings()
+ // Add new input
+ settings.History = append(settings.History, input)
+ writeSettings(settings)
+}
+
+func GetHistory() []string {
+ settings := getSettings()
+ return settings.History
+}
+
+// ExecPlandexCommand spawns the same binary, wiring std streams directly so you
+// don't have to capture output. Any os.Exit calls in the child won't kill your REPL.
+func ExecPlandexCommand(args []string) (string, error) {
+ // Create temp file
+ tmpFile, err := os.CreateTemp("", "plandex-output-*")
+ if err != nil {
+ return "", err
+ }
+ tmpPath := tmpFile.Name()
+ tmpFile.Close()
+ defer os.Remove(tmpPath)
+
+ // Set env vars
+ env := append(os.Environ(),
+ "PLANDEX_REPL=1",
+ "PLANDEX_REPL_OUTPUT_FILE="+tmpPath,
+ )
+
+ // Run command
+ cmd := exec.Command(os.Args[0], args...)
+ cmd.Env = env
+
+ // Connect stdin directly
+ cmd.Stdin = os.Stdin
+ // Use MultiWriter to send output to both buffer and os.Stdout
+ cmd.Stdout = os.Stdout
+ cmd.Stderr = os.Stderr
+
+ err = cmd.Run()
+ if err != nil {
+ if _, ok := err.(*exec.ExitError); ok {
+ return "", nil
+ }
+ return "", err
+ }
+
+ // Read output from temp file
+ output, err := os.ReadFile(tmpPath)
+ if err != nil {
+ return "", err
+ }
+ return string(output), nil
+}
diff --git a/app/cli/plan_exec/apply_exec.go b/app/cli/plan_exec/apply_exec.go
index 8812d631..3e640438 100644
--- a/app/cli/plan_exec/apply_exec.go
+++ b/app/cli/plan_exec/apply_exec.go
@@ -64,7 +64,7 @@ func GetOnApplyExecFail(flags lib.ApplyFlags) types.OnApplyExecFailFn {
lib.MustApplyPlanAttempt(lib.CurrentPlanId, lib.CurrentBranch, flags, onExecFail, attempt+1)
} else {
- res, err := term.SelectFromList("Still apply other changes or roll back all changes?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
+ res, err := term.SelectFromList("Still apply file changes or roll back?", []string{string(types.ApplyRollbackOptionKeep), string(types.ApplyRollbackOptionRollback)})
if err != nil {
onErr("failed to get rollback confirmation user input: %s", err)
diff --git a/app/cli/stream_tui/run.go b/app/cli/stream_tui/run.go
index c5ecec61..110c9630 100644
--- a/app/cli/stream_tui/run.go
+++ b/app/cli/stream_tui/run.go
@@ -92,6 +92,13 @@ func StartStreamUI(prompt string, buildOnly bool) error {
os.Exit(0)
}
+ if os.Getenv("PLANDEX_REPL") != "" && os.Getenv("PLANDEX_REPL_OUTPUT_FILE") != "" {
+ // write output to file
+ err := os.WriteFile(os.Getenv("PLANDEX_REPL_OUTPUT_FILE"), []byte(mod.reply), 0644)
+ if err != nil {
+ log.Println("stream UI - error writing output to repl temp file: ", err)
+ }
+ }
return nil
}
diff --git a/app/cli/stream_tui/update.go b/app/cli/stream_tui/update.go
index e46b64be..05a534c7 100644
--- a/app/cli/stream_tui/update.go
+++ b/app/cli/stream_tui/update.go
@@ -492,7 +492,10 @@ func (m *streamUIModel) selectedMissingFileOpt() (tea.Model, tea.Cmd) {
func (m *streamUIModel) checkMissingFile(msg *shared.StreamMessage) (tea.Model, tea.Cmd) {
if msg.MissingFilePath != "" {
+ log.Println("checkMissingFile - received missing file message | path:", msg.MissingFilePath)
+
if msg.MissingFileAutoContext {
+ log.Println("checkMissingFile - received missing file message | auto context")
m.processing = true
m.autoLoadedMissingFile = true
@@ -510,6 +513,7 @@ func (m *streamUIModel) checkMissingFile(msg *shared.StreamMessage) (tea.Model,
}
content := string(bytes)
+ log.Println("checkMissingFile - calling RespondMissingFile")
apiErr := api.Client.RespondMissingFile(lib.CurrentPlanId, lib.CurrentBranch, shared.RespondMissingFileRequest{
Choice: shared.RespondMissingFileChoiceLoad,
FilePath: msg.MissingFilePath,
@@ -522,6 +526,8 @@ func (m *streamUIModel) checkMissingFile(msg *shared.StreamMessage) (tea.Model,
return tea.Quit
}
+ log.Println("checkMissingFile - RespondMissingFile success")
+
return nil
},
)
@@ -530,6 +536,7 @@ func (m *streamUIModel) checkMissingFile(msg *shared.StreamMessage) (tea.Model,
m.promptingMissingFile = true
m.missingFilePath = msg.MissingFilePath
+ log.Println("checkMissingFile - reading file")
bytes, err := os.ReadFile(m.missingFilePath)
if err != nil {
log.Println("failed to read file:", err)
@@ -538,6 +545,7 @@ func (m *streamUIModel) checkMissingFile(msg *shared.StreamMessage) (tea.Model,
}
m.missingFileContent = string(bytes)
+ log.Println("checkMissingFile - estimating tokens")
numTokens := shared.GetNumTokensEstimate(m.missingFileContent)
m.missingFileTokens = numTokens
}
diff --git a/app/cli/types/apply.go b/app/cli/types/apply.go
index 2ac3ddeb..bda2622f 100644
--- a/app/cli/types/apply.go
+++ b/app/cli/types/apply.go
@@ -7,8 +7,8 @@ import (
type ApplyRollbackOption string
const (
- ApplyRollbackOptionKeep ApplyRollbackOption = "Still apply other changes"
- ApplyRollbackOptionRollback ApplyRollbackOption = "Roll back all changes"
+ ApplyRollbackOptionKeep ApplyRollbackOption = "Apply file changes"
+ ApplyRollbackOptionRollback ApplyRollbackOption = "Roll back file changes"
)
type OnApplyExecFailFn func(status int, output string, attempt int, toRollback *ApplyRollbackPlan, onErr OnErrFn, onSuccess func())
diff --git a/app/cli/types/fs.go b/app/cli/types/fs.go
index e7fc8e08..7b1b4c2b 100644
--- a/app/cli/types/fs.go
+++ b/app/cli/types/fs.go
@@ -5,6 +5,8 @@ import ignore "github.com/sabhiram/go-gitignore"
type ProjectPaths struct {
ActivePaths map[string]bool
AllPaths map[string]bool
+ ActiveDirs map[string]bool
+ AllDirs map[string]bool
PlandexIgnored *ignore.GitIgnore
IgnoredPaths map[string]string
}
diff --git a/app/server/db/context_helpers.go b/app/server/db/context_helpers.go
index 0f6f5ada..857defec 100644
--- a/app/server/db/context_helpers.go
+++ b/app/server/db/context_helpers.go
@@ -609,8 +609,11 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextRespon
}, nil, nil
}
- dbContextsCh := make(chan *Context)
- errCh := make(chan error)
+ var dbContexts []*Context
+ var apiContexts []*shared.Context
+ var mu sync.Mutex
+
+ errCh := make(chan error, len(paramsByTempId))
for tempId, loadParams := range paramsByTempId {
go func(tempId string, loadParams *shared.LoadContextParams) {
@@ -645,27 +648,25 @@ func LoadContexts(ctx Ctx, params LoadContextsParams) (*shared.LoadContextRespon
err := StoreContext(&context, params.CachedMapsByPath != nil)
if err != nil {
- errCh <- err
+ errCh <- fmt.Errorf("error storing context: %v", err)
return
}
- dbContextsCh <- &context
+ mu.Lock()
+ dbContexts = append(dbContexts, &context)
+ apiContext := context.ToApi()
+ apiContext.Body = ""
+ apiContexts = append(apiContexts, apiContext)
+ mu.Unlock()
+ errCh <- nil
}(tempId, loadParams)
}
- var dbContexts []*Context
- var apiContexts []*shared.Context
-
- for i := 0; i < len(*req); i++ {
- select {
- case err := <-errCh:
+ for i := 0; i < len(paramsByTempId); i++ {
+ err := <-errCh
+ if err != nil {
return nil, nil, fmt.Errorf("error storing context: %v", err)
- case dbContext := <-dbContextsCh:
- dbContexts = append(dbContexts, dbContext)
- apiContext := dbContext.ToApi()
- apiContext.Body = ""
- apiContexts = append(apiContexts, apiContext)
}
}
@@ -786,11 +787,10 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
numMaps := 0
var mu sync.Mutex
- errCh := make(chan error)
+ errCh := make(chan error, len(*req))
for id, params := range *req {
go func(id string, params *shared.UpdateContextParams) {
-
var context *Context
if _, ok := contextsById[id]; ok {
context = contextsById[id]
@@ -802,7 +802,6 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
errCh <- fmt.Errorf("error getting context: %v", err)
return
}
-
// log.Println("Got context", context.Id, "numTokens", context.NumTokens)
}
@@ -886,7 +885,7 @@ func UpdateContexts(params UpdateContextsParams) (*shared.UpdateContextResponse,
}
}
- errCh = make(chan error)
+ errCh = make(chan error, len(*req))
for id, params := range *req {
go func(id string, params *shared.UpdateContextParams) {
@@ -1049,8 +1048,7 @@ func invalidateConflictedResults(params invalidateConflictedResultsParams) error
// log.Println("invalidateConflictedResults - Conflicted paths:", conflictPaths)
if len(conflictPaths) > 0 {
- errCh := make(chan error)
- numRoutines := 0
+ toUpdateDescs := []*ConvoMessageDescription{}
for _, desc := range descriptions {
if !desc.DidBuild || desc.AppliedAt != nil {
@@ -1063,23 +1061,25 @@ func invalidateConflictedResults(params invalidateConflictedResultsParams) error
desc.BuildPathsInvalidated = make(map[string]bool)
}
desc.BuildPathsInvalidated[op.Path] = true
+ toUpdateDescs = append(toUpdateDescs, desc)
+ }
+ }
+ }
- // log.Printf("Invalidating build for path: %s, desc: %s\n", path, desc.Id)
-
- go func(desc *ConvoMessageDescription) {
- err := StoreDescription(desc)
-
- if err != nil {
- errCh <- fmt.Errorf("error storing description: %v", err)
- return
- }
+ numRoutines := len(toUpdateDescs) + 1
+ errCh := make(chan error, numRoutines)
- errCh <- nil
- }(desc)
+ for _, desc := range toUpdateDescs {
+ go func(desc *ConvoMessageDescription) {
+ err := StoreDescription(desc)
- numRoutines++
+ if err != nil {
+ errCh <- fmt.Errorf("error storing description: %v", err)
+ return
}
- }
+
+ errCh <- nil
+ }(desc)
}
go func() {
@@ -1092,7 +1092,6 @@ func invalidateConflictedResults(params invalidateConflictedResultsParams) error
errCh <- nil
}()
- numRoutines++
for i := 0; i < numRoutines; i++ {
err := <-errCh
diff --git a/app/server/db/locks.go b/app/server/db/locks.go
index f5c6f212..e679b086 100644
--- a/app/server/db/locks.go
+++ b/app/server/db/locks.go
@@ -6,6 +6,8 @@ import (
"fmt"
"log"
"math/rand"
+ "runtime/debug"
+ "strings"
"time"
"github.com/lib/pq"
@@ -39,6 +41,13 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
log.Printf("locking repo. orgId: %s | planId: %s | scope: %s | branch %s | numRetry %d \n", params.OrgId, params.PlanId, params.Scope, params.Branch, numRetry)
// spew.Dump(params)
+ stack := debug.Stack()
+ // Format truncated stack excluding runtime frames
+ stackTrace := formatStackTrace(stack)
+
+ log.Println()
+ log.Printf("LOCK_ATTEMPT | params: %+v | retry: %d | stack:\n%s", params, numRetry, stackTrace)
+
orgId := params.OrgId
userId := params.UserId
planId := params.PlanId
@@ -74,7 +83,7 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
}
}()
- query := "SELECT id, org_id, user_id, plan_id, plan_build_id, scope, branch, created_at FROM repo_locks WHERE plan_id = $1 FOR UPDATE"
+ query := "SELECT id, org_id, user_id, plan_id, plan_build_id, scope, branch, last_heartbeat_at, created_at FROM repo_locks WHERE plan_id = $1 FOR UPDATE"
queryArgs := []interface{}{planId}
var locks []*repoLock
@@ -99,7 +108,7 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
now := time.Now()
for rows.Next() {
var lock repoLock
- if err := rows.Scan(&lock.Id, &lock.OrgId, &lock.UserId, &lock.PlanId, &lock.PlanBuildId, &lock.Scope, &lock.Branch, &lock.CreatedAt); err != nil {
+ if err := rows.Scan(&lock.Id, &lock.OrgId, &lock.UserId, &lock.PlanId, &lock.PlanBuildId, &lock.Scope, &lock.Branch, &lock.LastHeartbeatAt, &lock.CreatedAt); err != nil {
return fmt.Errorf("error scanning repo lock: %v", err)
}
@@ -112,6 +121,8 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
}
if len(expiredLockIds) > 0 {
+ log.Printf("deleting expired locks: %v", expiredLockIds)
+
query := "DELETE FROM repo_locks WHERE id = ANY($1)"
_, err := tx.Exec(query, pq.Array(expiredLockIds))
if err != nil {
@@ -261,6 +272,10 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
return "", fmt.Errorf("error committing transaction: %v", err)
}
+ log.Println()
+ log.Printf("LOCK_ACQUIRED | id: %s | params: %+v | stack:\n%s", newLock.Id, params, stackTrace)
+ log.Println()
+
// Start a goroutine to keep the lock alive
go func() {
numErrors := 0
@@ -313,7 +328,11 @@ func lockRepo(params LockRepoParams, numRetry int) (string, error) {
}
func DeleteRepoLock(id string) error {
- log.Println("deleting repo lock:", id)
+ stack := debug.Stack()
+ stackTrace := formatStackTrace(stack)
+
+ log.Println()
+ log.Printf("LOCK_DELETE_ATTEMPT | id: %s | stack:\n%s", id, stackTrace)
query := "DELETE FROM repo_locks WHERE id = $1"
_, err := Conn.Exec(query, id)
@@ -321,7 +340,16 @@ func DeleteRepoLock(id string) error {
return fmt.Errorf("error removing lock: %v", err)
}
- log.Println("repo lock deleted successfully:", id)
+ log.Printf("LOCK_DELETED | id: %s", id)
+ log.Println()
return nil
}
+
+func formatStackTrace(stack []byte) string {
+ lines := strings.Split(string(stack), "\n")
+ // Take first 5 meaningful lines of stack trace
+ // Skip runtime frames (first 7 lines) and limit to next 5 lines
+ relevantLines := lines[7:min(len(lines), 12)]
+ return strings.Join(relevantLines, "\n")
+}
diff --git a/app/server/db/plan_helpers.go b/app/server/db/plan_helpers.go
index 44093dc7..21f0246e 100644
--- a/app/server/db/plan_helpers.go
+++ b/app/server/db/plan_helpers.go
@@ -6,6 +6,7 @@ import (
"log"
"os"
"path/filepath"
+ "strconv"
"time"
"github.com/google/uuid"
@@ -268,6 +269,17 @@ func StoreDescription(description *ConvoMessageDescription) error {
return fmt.Errorf("error creating convo message descriptions dir: %v", err)
}
+ for _, op := range description.Operations {
+ if op.Content != "" {
+ quoted := strconv.Quote(op.Content)
+ op.Content = quoted[1 : len(quoted)-1]
+ }
+ if op.Description != "" {
+ quoted := strconv.Quote(op.Description)
+ op.Description = quoted[1 : len(quoted)-1]
+ }
+ }
+
now := time.Now()
if description.Id == "" {
diff --git a/app/server/handlers/context_helper.go b/app/server/handlers/context_helper.go
index 9fc0512b..66af16e7 100644
--- a/app/server/handlers/context_helper.go
+++ b/app/server/handlers/context_helper.go
@@ -94,6 +94,7 @@ func loadContexts(
if err != nil {
errCh <- fmt.Errorf("error generating name for piped data: %v", err)
+ return
}
context.Name = name
@@ -107,6 +108,7 @@ func loadContexts(
if err != nil {
errCh <- fmt.Errorf("error generating name for note: %v", err)
+ return
}
context.Name = name
diff --git a/app/server/model/client.go b/app/server/model/client.go
index f2455af9..e4b2b4b2 100644
--- a/app/server/model/client.go
+++ b/app/server/model/client.go
@@ -1,6 +1,7 @@
package model
import (
+ "bufio"
"bytes"
"context"
"encoding/json"
@@ -11,6 +12,7 @@ import (
"reflect"
"regexp"
"strings"
+ "sync"
"time"
"github.com/plandex/plandex/shared"
@@ -73,31 +75,83 @@ type OpenAIPrediction struct {
Content string `json:"content"`
}
+type OpenRouterProviderConfig struct {
+ Order []string `json:"order"`
+ AllowFallbacks bool `json:"allow_fallbacks"`
+}
+
type ExtendedChatCompletionRequest struct {
*openai.ChatCompletionRequest
- Prediction OpenAIPrediction `json:"prediction,omitempty"`
+ Prediction *OpenAIPrediction `json:"prediction,omitempty"`
+ Provider *OpenRouterProviderConfig `json:"provider,omitempty"`
+}
+
+// ExtendedChatCompletionStream can wrap either a native OpenAI stream or our custom implementation
+type ExtendedChatCompletionStream struct {
+ openaiStream *openai.ChatCompletionStream
+ customReader *StreamReader[openai.ChatCompletionStreamResponse]
+ ctx context.Context
+}
+
+// StreamReader handles the SSE stream reading
+type StreamReader[T any] struct {
+ reader *bufio.Reader
+ response *http.Response
+ emptyMessagesLimit int
+ errAccumulator *ErrorAccumulator
+ unmarshaler *JSONUnmarshaler
+}
+
+// ErrorAccumulator keeps track of errors during streaming
+type ErrorAccumulator struct {
+ errors []error
+ mu sync.Mutex
}
+// JSONUnmarshaler handles JSON unmarshaling for stream responses
+type JSONUnmarshaler struct{}
+
func CreateChatCompletionStreamWithRetries(
clients map[string]ClientInfo,
modelConfig *shared.ModelRoleConfig,
ctx context.Context,
- req openai.ChatCompletionRequest,
-) (*openai.ChatCompletionStream, error) {
+ req interface{},
+) (*ExtendedChatCompletionStream, error) {
client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]
-
if !ok {
return nil, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)
}
- resolveReq(&req, modelConfig)
+ var baseReq *openai.ChatCompletionRequest
+ var finalReq *ExtendedChatCompletionRequest
+
+ switch typedReq := req.(type) {
+ case openai.ChatCompletionRequest:
+ baseReq = &typedReq
+ finalReq = &ExtendedChatCompletionRequest{
+ ChatCompletionRequest: baseReq,
+ }
+
+ case ExtendedChatCompletionRequest:
+ baseReq = typedReq.ChatCompletionRequest
+ finalReq = &typedReq
- for _, msg := range req.Messages {
- log.Println("Message role:", msg.Role)
+ default:
+ return nil, fmt.Errorf("invalid request type: %T", req)
}
- return withRetries(ctx, func() (*openai.ChatCompletionStream, error) {
- return client.Client.CreateChatCompletionStream(ctx, req)
+ resolveReq(baseReq, modelConfig)
+
+ providerOrder := getOpenRouterProviderOrder(modelConfig)
+ if len(providerOrder) > 0 {
+ finalReq.Provider = &OpenRouterProviderConfig{
+ Order: providerOrder,
+ AllowFallbacks: modelConfig.BaseModelConfig.OpenRouterAllowFallbacks,
+ }
+ }
+
+ return withRetries(ctx, func() (*ExtendedChatCompletionStream, error) {
+ return createChatCompletionStreamExtended(client, modelConfig.BaseModelConfig.BaseUrl, ctx, *finalReq)
})
}
@@ -108,23 +162,31 @@ func CreateChatCompletionWithRetries[T openai.ChatCompletionRequest | ExtendedCh
req T,
) (openai.ChatCompletionResponse, error) {
client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]
-
if !ok {
return openai.ChatCompletionResponse{}, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)
}
+
var baseReq *openai.ChatCompletionRequest
- var fn func() (openai.ChatCompletionResponse, error)
+ var finalReq *ExtendedChatCompletionRequest
if normalReq, ok := any(req).(openai.ChatCompletionRequest); ok {
baseReq = &normalReq
- fn = func() (openai.ChatCompletionResponse, error) {
- return client.Client.CreateChatCompletion(ctx, normalReq)
+
+ finalReq = &ExtendedChatCompletionRequest{
+ ChatCompletionRequest: baseReq,
+ }
+
+ providerOrder := getOpenRouterProviderOrder(modelConfig)
+ if len(providerOrder) > 0 {
+ finalReq.Provider = &OpenRouterProviderConfig{
+ Order: providerOrder,
+ AllowFallbacks: modelConfig.BaseModelConfig.OpenRouterAllowFallbacks,
+ }
}
+
} else if extendedReq, ok := any(req).(ExtendedChatCompletionRequest); ok {
baseReq = extendedReq.ChatCompletionRequest
- fn = func() (openai.ChatCompletionResponse, error) {
- return createChatCompletionExtended(clients, modelConfig, ctx, extendedReq)
- }
+ finalReq = &extendedReq
} else {
log.Println("Invalid request type")
log.Println("Request type:", reflect.TypeOf(req))
@@ -133,22 +195,18 @@ func CreateChatCompletionWithRetries[T openai.ChatCompletionRequest | ExtendedCh
resolveReq(baseReq, modelConfig)
- return withRetries(ctx, fn)
+ return withRetries(ctx, func() (openai.ChatCompletionResponse, error) {
+ return createChatCompletionExtended(client, modelConfig.BaseModelConfig.BaseUrl, ctx, *finalReq)
+ })
}
func createChatCompletionExtended(
- clients map[string]ClientInfo,
- modelConfig *shared.ModelRoleConfig,
+ client ClientInfo,
+ baseUrl string,
ctx context.Context,
extendedReq ExtendedChatCompletionRequest,
) (openai.ChatCompletionResponse, error) {
- client, ok := clients[modelConfig.BaseModelConfig.ApiKeyEnvVar]
-
- if !ok {
- return openai.ChatCompletionResponse{}, fmt.Errorf("client not found for api key env var: %s", modelConfig.BaseModelConfig.ApiKeyEnvVar)
- }
-
- req, err := http.NewRequestWithContext(ctx, "POST", modelConfig.BaseModelConfig.BaseUrl+"/chat/completions", nil)
+ req, err := http.NewRequestWithContext(ctx, "POST", baseUrl+"/chat/completions", nil)
if err != nil {
return openai.ChatCompletionResponse{}, err
}
@@ -179,7 +237,7 @@ func createChatCompletionExtended(
if err != nil {
return openai.ChatCompletionResponse{}, err
}
- log.Println("Response body:", string(body))
+ // log.Println("Response body:", string(body))
var response openai.ChatCompletionResponse
err = json.Unmarshal(body, &response)
@@ -190,6 +248,158 @@ func createChatCompletionExtended(
return response, nil
}
+func createChatCompletionStreamExtended(
+ client ClientInfo,
+ baseUrl string,
+ ctx context.Context,
+ extendedReq ExtendedChatCompletionRequest,
+) (*ExtendedChatCompletionStream, error) {
+ // Marshal the request body to JSON
+ jsonBody, err := json.Marshal(extendedReq)
+ if err != nil {
+ return nil, fmt.Errorf("error marshaling request: %w", err)
+ }
+
+ // Create new request
+ req, err := http.NewRequestWithContext(ctx, "POST", baseUrl+"/chat/completions", bytes.NewReader(jsonBody))
+ if err != nil {
+ return nil, fmt.Errorf("error creating request: %w", err)
+ }
+
+ // Set required headers for streaming
+ req.Header.Set("Content-Type", "application/json")
+ req.Header.Set("Accept", "text/event-stream")
+ req.Header.Set("Cache-Control", "no-cache")
+ req.Header.Set("Connection", "keep-alive")
+ req.Header.Set("Authorization", "Bearer "+client.ApiKey)
+ if client.OrgId != "" {
+ req.Header.Set("OpenAI-Organization", client.OrgId)
+ }
+
+ // Send the request
+ resp, err := httpClient.Do(req) //nolint:bodyclose // body is closed in stream.Close()
+ if err != nil {
+ return nil, fmt.Errorf("error making request: %w", err)
+ }
+
+ if resp.StatusCode < http.StatusOK || resp.StatusCode >= http.StatusBadRequest {
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("error reading error response: %w", err)
+ }
+ return nil, fmt.Errorf("streaming request failed: status code: %d, body: %s", resp.StatusCode, string(body))
+ }
+
+ reader := &StreamReader[openai.ChatCompletionStreamResponse]{
+ reader: bufio.NewReader(resp.Body),
+ response: resp,
+ emptyMessagesLimit: 30,
+ errAccumulator: NewErrorAccumulator(),
+ unmarshaler: &JSONUnmarshaler{},
+ }
+
+ return &ExtendedChatCompletionStream{
+ customReader: reader,
+ ctx: ctx,
+ }, nil
+}
+
+func NewErrorAccumulator() *ErrorAccumulator {
+ return &ErrorAccumulator{
+ errors: make([]error, 0),
+ }
+}
+
+func (ea *ErrorAccumulator) Add(err error) {
+ ea.mu.Lock()
+ defer ea.mu.Unlock()
+ ea.errors = append(ea.errors, err)
+}
+
+func (ea *ErrorAccumulator) GetErrors() []error {
+ ea.mu.Lock()
+ defer ea.mu.Unlock()
+ return ea.errors
+}
+
+func (ju *JSONUnmarshaler) Unmarshal(data []byte, v interface{}) error {
+ return json.Unmarshal(data, v)
+}
+
+// Recv reads from the stream
+func (stream *StreamReader[T]) Recv() (*T, error) {
+ for {
+ line, err := stream.reader.ReadString('\n')
+ if err != nil {
+ return nil, err
+ }
+
+ // Trim any whitespace
+ line = strings.TrimSpace(line)
+
+ // Skip empty lines
+ if line == "" {
+ continue
+ }
+
+ // Check for data prefix
+ if !strings.HasPrefix(line, "data: ") {
+ continue
+ }
+
+ // Extract the data
+ data := strings.TrimPrefix(line, "data: ")
+
+ // Check for stream completion
+ if data == "[DONE]" {
+ return nil, io.EOF
+ }
+
+ // Parse the response
+ var response T
+ err = stream.unmarshaler.Unmarshal([]byte(data), &response)
+ if err != nil {
+ stream.errAccumulator.Add(err)
+ continue
+ }
+
+ return &response, nil
+ }
+}
+
+func (stream *StreamReader[T]) Close() error {
+ if stream.response != nil {
+ return stream.response.Body.Close()
+ }
+ return nil
+}
+
+// Recv returns the next message in the stream
+func (stream *ExtendedChatCompletionStream) Recv() (*openai.ChatCompletionStreamResponse, error) {
+ select {
+ case <-stream.ctx.Done():
+ return nil, stream.ctx.Err()
+ default:
+ if stream.openaiStream != nil {
+ response, err := stream.openaiStream.Recv()
+ if err != nil {
+ return nil, err
+ }
+ return &response, nil
+ }
+ return stream.customReader.Recv()
+ }
+}
+
+// Close the response body
+func (stream *ExtendedChatCompletionStream) Close() error {
+ if stream.openaiStream != nil {
+ return stream.openaiStream.Close()
+ }
+ return stream.customReader.Close()
+}
+
func isNonRetriableErr(err error) bool {
errStr := err.Error()
@@ -268,6 +478,20 @@ func resolveReq(req *openai.ChatCompletionRequest, modelConfig *shared.ModelRole
log.Println("Resolved request")
}
+// route directly to first-party providers on openrouter for the main models
+// seems to be much faster this way currently
+func getOpenRouterProviderOrder(modelConfig *shared.ModelRoleConfig) []string {
+ var providerOrder []string
+ if modelConfig.BaseModelConfig.Provider == shared.ModelProviderOpenRouter {
+ if len(modelConfig.BaseModelConfig.PreferredOpenRouterProviders) > 0 {
+ for _, provider := range modelConfig.BaseModelConfig.PreferredOpenRouterProviders {
+ providerOrder = append(providerOrder, string(provider))
+ }
+ }
+ }
+ return providerOrder
+}
+
func withRetries[T any](
ctx context.Context,
operation func() (T, error),
diff --git a/app/server/model/name.go b/app/server/model/name.go
index 19248d19..1b3d5c55 100644
--- a/app/server/model/name.go
+++ b/app/server/model/name.go
@@ -39,7 +39,7 @@ func GenPlanName(
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
@@ -162,7 +162,7 @@ func GenPipedDataName(
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
@@ -293,7 +293,7 @@ func GenNoteName(
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
diff --git a/app/server/model/parse/subtasks.go b/app/server/model/parse/subtasks.go
index 619f8931..7ebf1bd7 100644
--- a/app/server/model/parse/subtasks.go
+++ b/app/server/model/parse/subtasks.go
@@ -2,7 +2,7 @@ package parse
import (
"plandex-server/db"
- "strconv"
+ "regexp"
"strings"
)
@@ -20,7 +20,6 @@ func ParseSubtasks(replyContent string) []*db.Subtask {
var subtasks []*db.Subtask
var currentTask *db.Subtask
var descLines []string
- num := 1
for _, line := range lines {
line = strings.TrimSpace(line)
@@ -28,9 +27,8 @@ func ParseSubtasks(replyContent string) []*db.Subtask {
continue
}
- // Check for next task number prefix
- prefix := strconv.Itoa(num) + ". "
- if strings.HasPrefix(line, prefix) {
+ // Check for any number followed by a period and space
+ if matched, _ := regexp.MatchString(`^\d+\.\s`, line); matched {
// Save previous task if exists
if currentTask != nil {
currentTask.Description = strings.Join(descLines, "\n")
@@ -38,12 +36,14 @@ func ParseSubtasks(replyContent string) []*db.Subtask {
}
// Start new task
- title := strings.TrimPrefix(line, prefix)
- currentTask = &db.Subtask{
- Title: title,
+ parts := strings.SplitN(line, ". ", 2)
+ if len(parts) == 2 {
+ title := parts[1]
+ currentTask = &db.Subtask{
+ Title: title,
+ }
+ descLines = nil
}
- descLines = nil
- num++
continue
}
diff --git a/app/server/model/parse/subtasks_test.go b/app/server/model/parse/subtasks_test.go
index a4c8ff06..a3a66945 100644
--- a/app/server/model/parse/subtasks_test.go
+++ b/app/server/model/parse/subtasks_test.go
@@ -85,6 +85,22 @@ Uses: ` + "`main.go`",
},
},
},
+ {
+ name: "single task from pong",
+ input: "### Tasks" + `
+
+9. Update Makefile to include Homebrew-specific include and library search paths
+- Modify CFLAGS in Makefile to add -I/opt/homebrew/include
+- Modify LDFLAGS in Makefile to add -L/opt/homebrew/lib
+Uses: ` + "`Makefile`" + `, ` + "`_apply.sh`",
+ expected: []*db.Subtask{
+ {
+ Title: "Update Makefile to include Homebrew-specific include and library search paths",
+ Description: "Modify CFLAGS in Makefile to add -I/opt/homebrew/include\nModify LDFLAGS in Makefile to add -L/opt/homebrew/lib",
+ UsesFiles: []string{"Makefile", "_apply.sh"},
+ },
+ },
+ },
}
for _, tt := range tests {
diff --git a/app/server/model/plan/build_structured_edits.go b/app/server/model/plan/build_structured_edits.go
index 9acc2742..76db1088 100644
--- a/app/server/model/plan/build_structured_edits.go
+++ b/app/server/model/plan/build_structured_edits.go
@@ -93,7 +93,7 @@ func (fileState *activeBuildStreamFileState) buildStructuredEdits() {
log.Printf("buildStructuredEdits - %s - initial isValid: %t\n", filePath, isValid)
- var buildStage BuildStage = BuildStageValidateAndCorrect
+ var buildStage BuildStage = BuildStageInitial
for !isValid && int(buildStage) <= int(BuildStageValidateAndCorrect) {
buildStage = BuildStage(int(buildStage) + 1)
diff --git a/app/server/model/plan/build_whole_file.go b/app/server/model/plan/build_whole_file.go
index ba09795b..355b762a 100644
--- a/app/server/model/plan/build_whole_file.go
+++ b/app/server/model/plan/build_whole_file.go
@@ -76,7 +76,7 @@ func (fileState *activeBuildStreamFileState) buildWholeFileFallback(proposedCont
if config.BaseModelConfig.PredictedOutputEnabled {
extendedReq := &model.ExtendedChatCompletionRequest{
ChatCompletionRequest: modelReq,
- Prediction: model.OpenAIPrediction{
+ Prediction: &model.OpenAIPrediction{
Type: "content",
Content: `
## Comments
diff --git a/app/server/model/plan/commit_msg.go b/app/server/model/plan/commit_msg.go
index 73d27b03..f46f4e78 100644
--- a/app/server/model/plan/commit_msg.go
+++ b/app/server/model/plan/commit_msg.go
@@ -49,7 +49,7 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
@@ -197,7 +197,7 @@ func GenCommitMsgForPendingResults(auth *types.ServerAuth, plan *db.Plan, client
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
diff --git a/app/server/model/plan/exec_status.go b/app/server/model/plan/exec_status.go
index d3b004a5..44ad4005 100644
--- a/app/server/model/plan/exec_status.go
+++ b/app/server/model/plan/exec_status.go
@@ -57,7 +57,7 @@ func (state *activeTellStreamState) execStatusShouldContinue(message string, ctx
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
diff --git a/app/server/model/plan/tell_context.go b/app/server/model/plan/tell_context.go
index f2aa46f0..8e92580f 100644
--- a/app/server/model/plan/tell_context.go
+++ b/app/server/model/plan/tell_context.go
@@ -145,7 +145,11 @@ func (state *activeTellStreamState) checkAutoLoadContext() []string {
return nil
}
- if !(state.isContextStage || state.isPlanningStage) {
+ if state.req.IsChatOnly && state.iteration > 0 {
+ return nil
+ }
+
+ if !(state.isContextStage || state.isPlanningStage || state.req.IsChatOnly) {
return nil
}
@@ -165,6 +169,8 @@ func (state *activeTellStreamState) checkAutoLoadContext() []string {
}
}
+ log.Printf("Tell plan - checkAutoLoadContext - files: %v\n", files)
+
return files
}
diff --git a/app/server/model/plan/tell_exec.go b/app/server/model/plan/tell_exec.go
index 62bb41c5..8169beb8 100644
--- a/app/server/model/plan/tell_exec.go
+++ b/app/server/model/plan/tell_exec.go
@@ -63,6 +63,7 @@ type execTellPlanParams struct {
shouldLoadFollowUpContext bool
didLoadFollowUpContext bool
didMakeFollowUpPlan bool
+ didLoadChatOnlyContext bool
}
func execTellPlan(params execTellPlanParams) {
@@ -187,7 +188,7 @@ func execTellPlan(params execTellPlanParams) {
return
}
- sysPrompt, err := state.getTellSysPrompt(autoContextEnabled, smartContextEnabled, modelContextText)
+ sysPrompt, err := state.getTellSysPrompt(autoContextEnabled, smartContextEnabled, req.IsChatOnly && params.didLoadChatOnlyContext, modelContextText)
if err != nil {
log.Printf("Error getting tell sys prompt: %v\n", err)
active.StreamDoneCh <- &shared.ApiError{
@@ -215,10 +216,14 @@ func execTellPlan(params execTellPlanParams) {
var applyScriptSummary string
if req.ExecEnabled {
- applyScriptSummary = prompts.ApplyScriptPromptSummary
+ if isPlanningStage {
+ applyScriptSummary = prompts.ApplyScriptPlanningPromptSummary
+ } else {
+ applyScriptSummary = prompts.ApplyScriptImplementationPromptSummary
+ }
}
- promptMessage, ok := state.resolvePromptMessage(isPlanningStage, isContextStage, applyScriptSummary)
+ promptMessage, ok := state.resolvePromptMessage(isPlanningStage, isContextStage, req.IsChatOnly && params.didLoadChatOnlyContext, applyScriptSummary)
if !ok {
return
}
@@ -265,13 +270,31 @@ func execTellPlan(params execTellPlanParams) {
// log.Printf("%s: %s\n", message.Role, message.Content)
// }
+ requestTokens := shared.GetMessagesTokenEstimate(state.messages...) + imageContextTokens + shared.TokensPerRequest
+ state.totalRequestTokens = requestTokens
+
+ stop := []string{"<PlandexFinish/>"}
+ var modelConfig shared.ModelRoleConfig
+ if isPlanningStage {
+ plannerConfig := state.settings.ModelPack.Planner.GetRoleForTokens(requestTokens)
+ modelConfig = plannerConfig.ModelRoleConfig
+ if isContextStage {
+ log.Println("Tell plan - isContextStage - setting modelConfig to context loader")
+ modelConfig = state.settings.ModelPack.GetContextLoader().GetRoleForTokens(requestTokens)
+ }
+ } else if isImplementationStage {
+ modelConfig = state.settings.ModelPack.GetCoder().GetRoleForTokens(requestTokens)
+ }
+
+ log.Println("totalRequestTokens:", requestTokens)
+
_, apiErr := hooks.ExecHook(hooks.WillSendModelRequest, hooks.HookParams{
Auth: auth,
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
- InputTokens: state.totalRequestTokens,
- OutputTokens: state.settings.ModelPack.Planner.GetReservedOutputTokens(),
- ModelName: state.settings.ModelPack.Planner.BaseModelConfig.ModelName,
+ InputTokens: requestTokens,
+ OutputTokens: modelConfig.GetReservedOutputTokens(),
+ ModelName: modelConfig.BaseModelConfig.ModelName,
},
})
if apiErr != nil {
@@ -279,23 +302,6 @@ func execTellPlan(params execTellPlanParams) {
return
}
- var stop []string
- var modelConfig shared.ModelRoleConfig
- if isPlanningStage {
- stop = []string{"<EndPlandexTasks/>"}
- modelConfig = state.settings.ModelPack.Planner.ModelRoleConfig
- if isFollowUp {
- log.Println("Tell plan - isFollowUp - setting stop to <PlandexDecideContext/>")
- stop = append(stop, "<PlandexDecideContext/>")
- } else if isContextStage {
- log.Println("Tell plan - isContextStage - setting modelConfig to context loader")
- modelConfig = state.settings.ModelPack.GetContextLoader()
- }
- } else if isImplementationStage {
- stop = []string{"<PlandexSubtaskDone/>"}
- modelConfig = state.settings.ModelPack.GetCoder()
- }
-
// log.Println("Stop:", stop)
// spew.Dump(state.messages)
diff --git a/app/server/model/plan/tell_missing_file.go b/app/server/model/plan/tell_missing_file.go
index 776508d3..e286e452 100644
--- a/app/server/model/plan/tell_missing_file.go
+++ b/app/server/model/plan/tell_missing_file.go
@@ -69,9 +69,6 @@ func (state *activeTellStreamState) handleMissingFileResponse(applyScriptSummary
skipPrompt := prompts.GetSkipMissingFilePrompt(res.CurrentFilePath)
prompt := prompts.GetWrappedPrompt(skipPrompt, req.OsDetails, applyScriptSummary, isPlanningStage, isFollowUp) + "\n\n" + skipPrompt // repetition of skip prompt to improve instruction following
- skipPromptTokens := shared.GetNumTokensEstimate(skipPrompt)
- state.totalRequestTokens += skipPromptTokens
-
state.messages = append(state.messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: prompt,
@@ -81,9 +78,6 @@ func (state *activeTellStreamState) handleMissingFileResponse(applyScriptSummary
missingPrompt := prompts.GetMissingFileContinueGeneratingPrompt(res.CurrentFilePath)
prompt := prompts.GetWrappedPrompt(missingPrompt, req.OsDetails, applyScriptSummary, isPlanningStage, isFollowUp) + "\n\n" + missingPrompt // repetition of missing prompt to improve instruction following
- promptTokens := shared.GetNumTokensEstimate(prompt)
- state.totalRequestTokens += promptTokens
-
state.messages = append(state.messages, openai.ChatCompletionMessage{
Role: openai.ChatMessageRoleUser,
Content: prompt,
diff --git a/app/server/model/plan/tell_prompt_message.go b/app/server/model/plan/tell_prompt_message.go
index 5a9bf006..b1bc80bc 100644
--- a/app/server/model/plan/tell_prompt_message.go
+++ b/app/server/model/plan/tell_prompt_message.go
@@ -9,7 +9,7 @@ import (
"github.com/sashabaranov/go-openai"
)
-func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isContextStage bool, applyScriptSummary string) (*openai.ChatCompletionMessage, bool) {
+func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isContextStage, didLoadChatOnlyContext bool, applyScriptSummary string) (*openai.ChatCompletionMessage, bool) {
req := state.req
iteration := state.iteration
active := state.activePlan
@@ -17,6 +17,8 @@ func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isCont
var promptMessage *openai.ChatCompletionMessage
+ var lastMessage *openai.ChatCompletionMessage
+
if req.IsUserContinue {
if len(state.messages) == 0 {
active.StreamDoneCh <- &shared.ApiError{
@@ -28,15 +30,49 @@ func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isCont
}
// if the user is continuing the plan, we need to check whether the previous message was a user message or assistant message
- lastMessage := state.messages[len(state.messages)-1]
+ lastMessage = &state.messages[len(state.messages)-1]
log.Println("User is continuing plan. Last message role:", lastMessage.Role)
- // log.Println("User is continuing plan. Last message:\n\n", lastMessage.Content)
+ }
+
+ if req.IsChatOnly {
+ var prompt string
+ if req.IsUserContinue {
+ if lastMessage.Role == openai.ChatMessageRoleUser {
+ log.Println("User is continuing plan in chat only mode. Last message was user message. Using last user message as prompt")
+ prompt = lastMessage.Content
+ state.userPrompt = prompt
+ state.messages = state.messages[:len(state.messages)-1]
+ } else {
+ log.Println("User is continuing plan in chat only mode. Last message was assistant message. Using user continue prompt")
+ prompt = prompts.UserContinuePrompt
+ }
+ } else {
+ prompt = req.Prompt
+ }
+
+ wrapped := prompts.GetWrappedChatOnlyPrompt(prompts.ChatUserPromptParams{
+ CreatePromptParams: prompts.CreatePromptParams{
+ AutoContext: req.AutoContext,
+ ExecMode: req.ExecEnabled,
+ LastResponseLoadedContext: didLoadChatOnlyContext,
+ },
+ Prompt: prompt,
+ OsDetails: req.OsDetails,
+ })
+
+ promptMessage = &openai.ChatCompletionMessage{
+ Role: openai.ChatMessageRoleUser,
+ Content: wrapped,
+ }
+ state.messages = append(state.messages, *promptMessage)
+ } else if req.IsUserContinue {
+ // log.Println("User is continuing plan. Last message:\n\n", lastMessage.Content)
if lastMessage.Role == openai.ChatMessageRoleUser {
// if last message was a user message, we want to remove it from the messages array and then use that last message as the prompt so we can continue from where the user left off
- log.Println("User is continuing plan. Last message was user message. Using last user message as prompt")
+ log.Println("User is continuing plan in tell mode. Last message was user message. Using last user message as prompt")
state.messages = state.messages[:len(state.messages)-1]
promptMessage = &openai.ChatCompletionMessage{
@@ -48,7 +84,7 @@ func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isCont
} else {
// if the last message was an assistant message, we'll use the user continue prompt
- log.Println("User is continuing plan. Last message was assistant message. Using user continue prompt")
+ log.Println("User is continuing plan in tell mode. Last message was assistant message. Using user continue prompt")
// otherwise we'll use the continue prompt
promptMessage = &openai.ChatCompletionMessage{
@@ -61,13 +97,10 @@ func (state *activeTellStreamState) resolvePromptMessage(isPlanningStage, isCont
} else {
var prompt string
if iteration == 0 {
- if req.IsChatOnly {
- prompt = req.Prompt + prompts.ChatOnlyPrompt
- } else if req.IsUserDebug {
+ if req.IsUserDebug {
prompt = req.Prompt + prompts.DebugPrompt
} else if req.IsApplyDebug {
prompt = req.Prompt + prompts.ApplyDebugPrompt
- state.totalRequestTokens += prompts.ApplyDebugPromptTokens
} else {
prompt = req.Prompt
}
diff --git a/app/server/model/plan/tell_stream_finish.go b/app/server/model/plan/tell_stream_finish.go
index b60377ca..309f179d 100644
--- a/app/server/model/plan/tell_stream_finish.go
+++ b/app/server/model/plan/tell_stream_finish.go
@@ -81,6 +81,7 @@ func (state *activeTellStreamState) handleStreamFinished() handleStreamFinishedR
subtaskFinished: subtaskFinished,
hasNewSubtasks: hasNewSubtasks,
followUpNeedsContextStage: followUpNeedsContextStage,
+ autoLoadContextFiles: autoLoadContextFiles,
})
if storeOnFinishedResult.shouldContinueMainLoop || storeOnFinishedResult.shouldReturn {
return storeOnFinishedResult.handleStreamFinishedResult
@@ -176,6 +177,7 @@ func (state *activeTellStreamState) handleStreamFinished() handleStreamFinishedR
if followUpNeedsContextStage ||
(len(autoLoadContextFiles) > 0 && !hasNewSubtasks) ||
+ (len(autoLoadContextFiles) > 0 && req.IsChatOnly) ||
(req.AutoContinue && shouldContinue && iteration < MaxAutoContinueIterations &&
!(len(state.subtasks) > 0 && allSubtasksFinished)) {
log.Println("Auto continue plan")
@@ -189,6 +191,7 @@ func (state *activeTellStreamState) handleStreamFinished() handleStreamFinishedR
iteration: iteration + 1,
shouldLoadFollowUpContext: followUpNeedsContextStage,
didMakeFollowUpPlan: state.isFollowUp && !followUpNeedsContextStage && hasNewSubtasks,
+ didLoadChatOnlyContext: len(autoLoadContextFiles) > 0 && req.IsChatOnly,
})
} else {
var buildFinished bool
@@ -356,6 +359,7 @@ type storeOnFinishedParams struct {
subtaskFinished bool
hasNewSubtasks bool
followUpNeedsContextStage bool
+ autoLoadContextFiles []string
}
type storeOnFinishedResult struct {
@@ -369,7 +373,7 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParams
subtaskFinished := params.subtaskFinished
hasNewSubtasks := params.hasNewSubtasks
followUpNeedsContextStage := params.followUpNeedsContextStage
-
+ autoLoadContextFiles := params.autoLoadContextFiles
currentOrgId := state.currentOrgId
currentUserId := state.currentUserId
planId := state.plan.Id
@@ -440,7 +444,7 @@ func (state *activeTellStreamState) storeOnFinished(params storeOnFinishedParams
replyType = shared.ReplyTypeImplementation
} else if hasNewSubtasks {
replyType = shared.ReplyTypeMadePlan
- } else if state.isContextStage {
+ } else if len(autoLoadContextFiles) > 0 {
replyType = shared.ReplyTypeLoadedContext
} else if followUpNeedsContextStage {
replyType = shared.ReplyTypeContextAssessment
diff --git a/app/server/model/plan/tell_stream_main.go b/app/server/model/plan/tell_stream_main.go
index db4eae86..fff64d06 100644
--- a/app/server/model/plan/tell_stream_main.go
+++ b/app/server/model/plan/tell_stream_main.go
@@ -7,10 +7,9 @@ import (
"time"
"github.com/plandex/plandex/shared"
- "github.com/sashabaranov/go-openai"
)
-func (state *activeTellStreamState) listenStream(stream *openai.ChatCompletionStream) {
+func (state *activeTellStreamState) listenStream(stream *model.ExtendedChatCompletionStream) {
defer stream.Close()
plan := state.plan
diff --git a/app/server/model/plan/tell_stream_processor.go b/app/server/model/plan/tell_stream_processor.go
index f492cc6e..fa638dc5 100644
--- a/app/server/model/plan/tell_stream_processor.go
+++ b/app/server/model/plan/tell_stream_processor.go
@@ -17,7 +17,19 @@ import (
const verboseLogging = false
-var openingTagRegex = regexp.MustCompile(`<PlandexBlock\s+lang="(.+?)".*?>`)
+var openingTagRegex = regexp.MustCompile(`<PlandexBlock\s+lang="(.+?)"\s+path="(.+?)".*?>`)
+
+// matches any prefix of the opening tag
+var partialOpeningTagRegex = regexp.MustCompile(
+ `^` +
+ // 1) Nested optional groups for "PlandexBlock":
+ `P(?:l(?:a(?:n(?:d(?:e(?:x(?:B(?:l(?:o(?:c(?:k)?)?)?)?)?)?)?)?)?)?)?` +
+ // 2) Optional lang="..." (with optional final quote):
+ `(?:\s+lang="[^"]*(?:")?)?` +
+ // 3) Optional path="..." (with optional final quote):
+ `(?:\s+path="[^"]*(?:")?)?` +
+ `$`,
+)
type processChunkResult struct {
shouldReturn bool
@@ -92,7 +104,8 @@ func (state *activeTellStreamState) processChunk(choice openai.ChatCompletionStr
if currentFile != "" &&
!req.IsChatOnly &&
active.ContextsByPath[currentFile] == nil &&
- req.ProjectPaths[currentFile] && !active.AllowOverwritePaths[currentFile] {
+ req.ProjectPaths[currentFile] &&
+ !active.AllowOverwritePaths[currentFile] {
return state.handleMissingFile(bufferOrStreamRes.content, currentFile, bufferOrStreamRes.blockLang)
}
@@ -185,8 +198,34 @@ func (processor *chunkProcessor) bufferOrStream(content string, parserRes *types
}
if awaitingTag {
+ if verboseLogging {
+ log.Println("awaitingTag")
+ }
if processor.awaitingBlockOpeningTag {
- if parserRes.MaybeFilePath == "" && parserRes.CurrentFilePath == "" {
+ if verboseLogging {
+ log.Println("processor.awaitingBlockOpeningTag")
+ }
+ split := strings.Split(content, "<")
+ var matchedPrefix bool
+ if len(split) > 1 {
+ last := split[len(split)-1]
+ if verboseLogging {
+ log.Printf("last: %s\n", last)
+ }
+ if partialOpeningTagRegex.MatchString(last) {
+ if verboseLogging {
+ log.Println("partialOpeningTagRegex.MatchString(last)")
+ }
+ shouldStream = false
+ matchedPrefix = true
+ } else {
+ if verboseLogging {
+ log.Println("partialOpeningTagRegex.MatchString(last) is false")
+ }
+ }
+ }
+
+ if !matchedPrefix && parserRes.MaybeFilePath == "" && parserRes.CurrentFilePath == "" {
// wasn't really a file path / code block
processor.awaitingBlockOpeningTag = false
shouldStream = true
@@ -248,6 +287,14 @@ func (processor *chunkProcessor) bufferOrStream(content string, parserRes *types
if parserRes.MaybeFilePath != "" && parserRes.CurrentFilePath == "" {
processor.awaitingBlockOpeningTag = true
+ } else {
+ split := strings.Split(content, "<")
+ if len(split) > 1 {
+ last := split[len(split)-1]
+ if partialOpeningTagRegex.MatchString(last) {
+ processor.awaitingBlockOpeningTag = true
+ }
+ }
}
if parserRes.CurrentFilePath != "" {
@@ -381,7 +428,6 @@ func (processor *chunkProcessor) bufferOrStream(content string, parserRes *types
}
func (state *activeTellStreamState) handleNewOperations(parserRes *types.ReplyParserRes) {
-
processor := state.chunkProcessor
plan := state.plan
planId := plan.Id
@@ -515,6 +561,8 @@ func (state *activeTellStreamState) handleMissingFile(content, currentFile, bloc
Type: shared.StreamMessageReply,
ReplyChunk: chunkToStream,
})
+ active.FlushStreamBuffer()
+ time.Sleep(20 * time.Millisecond)
}
log.Printf("Prompting user for missing file: %s\n", currentFile)
@@ -585,14 +633,15 @@ func getCroppedChunk(uncropped, cropped, chunk string) string {
}
func replaceCodeBlockOpeningTag(content string, replaceWithFn func(lang string) string) (bool, string) {
- // check for opening tag matching <PlandexBlock lang="...">
+ // check for opening tag matching <PlandexBlock lang="..." path="...">
match := openingTagRegex.FindStringSubmatch(content)
if match != nil {
- // Found complete opening tag with lang attribute
+ // Found complete opening tag with lang and path attributes
lang := match[1] // Extract the language from the first capture group
return true, strings.Replace(content, match[0], replaceWithFn(lang), 1)
} else if strings.Contains(content, "<PlandexBlock>") {
+ // This is a fallback case that should probably be removed since we now require both attributes
return true, strings.Replace(content, "<PlandexBlock>", replaceWithFn(""), 1)
}
diff --git a/app/server/model/plan/tell_stream_processor_test.go b/app/server/model/plan/tell_stream_processor_test.go
index ba272162..635cccac 100644
--- a/app/server/model/plan/tell_stream_processor_test.go
+++ b/app/server/model/plan/tell_stream_processor_test.go
@@ -7,6 +7,7 @@ import (
func TestBufferOrStream(t *testing.T) {
tests := []struct {
+ only bool
name string
initialState *chunkProcessor
chunk string
@@ -60,7 +61,7 @@ func TestBufferOrStream(t *testing.T) {
name: "converts opening tag",
initialState: &chunkProcessor{
fileOpen: true,
- contentBuffer: `<PlandexBlock lang="go">` + "\n",
+ contentBuffer: `<PlandexBlock lang="go" path="main.go">` + "\n",
awaitingBlockOpeningTag: true,
},
chunk: `package`,
@@ -85,7 +86,7 @@ func TestBufferOrStream(t *testing.T) {
contentBuffer: "",
awaitingBlockOpeningTag: false,
},
- chunk: `<PlandexBlock lang="go">` + "\npackage",
+ chunk: `<PlandexBlock lang="go" path="main.go">` + "\npackage",
maybeFilePath: "",
currentFilePath: "main.go",
want: bufferOrStreamResult{
@@ -305,9 +306,80 @@ func TestBufferOrStream(t *testing.T) {
awaitingOpClosingTag: false,
},
},
+ {
+ name: "buffers for partial opening tag with no file path label",
+ initialState: &chunkProcessor{},
+ chunk: "something<Pland",
+ want: bufferOrStreamResult{
+ shouldStream: false,
+ },
+ wantState: &chunkProcessor{
+ awaitingBlockOpeningTag: true,
+ },
+ },
+ {
+ name: "continues buffering partial opening tag with no file path label",
+ initialState: &chunkProcessor{
+ awaitingBlockOpeningTag: true,
+ contentBuffer: "something<Pland",
+ },
+ chunk: "exBlock lang=\"go\" path=\"main",
+ want: bufferOrStreamResult{
+ shouldStream: false,
+ },
+ wantState: &chunkProcessor{
+ awaitingBlockOpeningTag: true,
+ contentBuffer: "something<PlandexBlock lang=\"go\" path=\"main",
+ },
+ },
+ {
+ name: "replaces opening tag with no file path label when it completes",
+ initialState: &chunkProcessor{
+ awaitingBlockOpeningTag: true,
+ contentBuffer: "something\n<Pland",
+ fileOpen: true,
+ },
+ currentFilePath: "main.go",
+ chunk: "exBlock lang=\"go\" path=\"main.go\">\npackage",
+ want: bufferOrStreamResult{
+ shouldStream: true,
+ content: "something\n```go\npackage",
+ },
+ wantState: &chunkProcessor{
+ awaitingBlockOpeningTag: false,
+ fileOpen: true,
+ },
+ },
+ {
+ name: "replaces full opening tag without file path label",
+ initialState: &chunkProcessor{
+ fileOpen: true,
+ },
+ currentFilePath: "main.go",
+ chunk: "something\n<PlandexBlock lang=\"go\" path=\"main.go\">\npackage",
+ want: bufferOrStreamResult{
+ shouldStream: true,
+ content: "something\n```go\npackage",
+ },
+ wantState: &chunkProcessor{
+ awaitingBlockOpeningTag: false,
+ fileOpen: true,
+ },
+ },
}
- for _, tt := range tests {
+ only := map[int]bool{}
+ for i, tt := range tests {
+ if tt.only {
+ only[i] = true
+ }
+ }
+
+ for i, tt := range tests {
+ if len(only) > 0 && !only[i] {
+ continue
+ }
+
t.Run(tt.name, func(t *testing.T) {
processor := tt.initialState
diff --git a/app/server/model/plan/tell_subtasks.go b/app/server/model/plan/tell_subtasks.go
index 403b63fd..b05f31ef 100644
--- a/app/server/model/plan/tell_subtasks.go
+++ b/app/server/model/plan/tell_subtasks.go
@@ -74,6 +74,7 @@ func (state *activeTellStreamState) checkNewSubtasks() bool {
subtasks := parse.ParseSubtasks(content)
if len(subtasks) == 0 {
+ log.Println("No new subtasks found")
return false
}
diff --git a/app/server/model/plan/tell_summary.go b/app/server/model/plan/tell_summary.go
index dc2a3d55..b7052b9e 100644
--- a/app/server/model/plan/tell_summary.go
+++ b/app/server/model/plan/tell_summary.go
@@ -179,8 +179,6 @@ func (state *activeTellStreamState) addConversationMessages() bool {
}
}
- state.totalRequestTokens = tokensBeforeConvo + conversationTokens
-
return true
}
diff --git a/app/server/model/plan/tell_sys_prompt.go b/app/server/model/plan/tell_sys_prompt.go
index 056dd45f..10f6e181 100644
--- a/app/server/model/plan/tell_sys_prompt.go
+++ b/app/server/model/plan/tell_sys_prompt.go
@@ -6,7 +6,7 @@ import (
"plandex-server/model/prompts"
)
-func (state *activeTellStreamState) getTellSysPrompt(autoContextEnabled, smartContextEnabled bool, modelContextText string) (string, error) {
+func (state *activeTellStreamState) getTellSysPrompt(autoContextEnabled, smartContextEnabled, didLoadChatOnlyContext bool, modelContextText string) (string, error) {
req := state.req
isPlanningStage := state.isPlanningStage
isContextStage := state.isContextStage
@@ -15,36 +15,45 @@ func (state *activeTellStreamState) getTellSysPrompt(autoContextEnabled, smartCo
var sysCreate string
- if isPlanningStage {
- log.Println("isPlanningStage")
- if autoContextEnabled && isContextStage {
- sysCreate = prompts.AutoContextPreamble
- } else if autoContextEnabled || smartContextEnabled {
- sysCreate = prompts.SysPlanningAutoContext
+ if req.IsChatOnly {
+ sysCreate = prompts.GetChatSysPrompt(prompts.CreatePromptParams{
+ ExecMode: req.ExecEnabled,
+ AutoContext: autoContextEnabled,
+ LastResponseLoadedContext: didLoadChatOnlyContext,
+ })
+ } else {
+
+ if isPlanningStage {
+ log.Println("isPlanningStage")
+ if autoContextEnabled && isContextStage {
+ sysCreate = prompts.AutoContextTellPreamble
+ } else if autoContextEnabled || smartContextEnabled {
+ sysCreate = prompts.SysPlanningAutoContext
- if isFollowUp {
- sysCreate = prompts.FollowUpPlanClassifierPrompt + "\n\n" + sysCreate
+ if isFollowUp {
+ sysCreate = prompts.FollowUpPlanClassifierPrompt + "\n\n" + sysCreate
+ }
+ } else {
+ sysCreate = prompts.SysPlanningBasic
}
} else {
- sysCreate = prompts.SysPlanningBasic
- }
- } else {
- log.Println("isImplementationStage")
- if state.currentSubtask == nil {
- return "", fmt.Errorf("no current subtask during implementation stage")
+ log.Println("isImplementationStage")
+ if state.currentSubtask == nil {
+ return "", fmt.Errorf("no current subtask during implementation stage")
+ }
+ sysCreate = prompts.GetImplementationPrompt(state.currentSubtask.Title)
}
- sysCreate = prompts.GetImplementationPrompt(state.currentSubtask.Title)
- }
- if !isContextStage {
- if req.ExecEnabled {
- if isPlanningStage {
- sysCreate += prompts.ApplyScriptPlanningPrompt
+ if !isContextStage {
+ if req.ExecEnabled {
+ if isPlanningStage {
+ sysCreate += prompts.ApplyScriptPlanningPrompt
+ } else {
+ sysCreate += prompts.ApplyScriptImplementationPrompt
+ }
} else {
- sysCreate += prompts.ApplyScriptImplementationPrompt
+ sysCreate += prompts.NoApplyScriptPrompt
}
- } else {
- sysCreate += prompts.NoApplyScriptPrompt
}
}
@@ -52,18 +61,19 @@ func (state *activeTellStreamState) getTellSysPrompt(autoContextEnabled, smartCo
sysCreate += modelContextText
- if len(active.SkippedPaths) > 0 {
- skippedPrompt := prompts.SkippedPathsPrompt
- for skippedPath := range active.SkippedPaths {
- skippedPrompt += fmt.Sprintf("- %s\n", skippedPath)
+ if !req.IsChatOnly {
+ if len(active.SkippedPaths) > 0 {
+ skippedPrompt := prompts.SkippedPathsPrompt
+ for skippedPath := range active.SkippedPaths {
+ skippedPrompt += fmt.Sprintf("- %s\n", skippedPath)
+ }
+ sysCreate += skippedPrompt
}
}
if len(state.subtasks) > 0 {
subtasksPrompt := state.formatSubtasks()
-
// log.Println("subtasksPrompt:\n", subtasksPrompt)
-
sysCreate += subtasksPrompt
}
diff --git a/app/server/model/prompts/apply_exec.go b/app/server/model/prompts/apply_exec.go
index ed58bc3c..cc760e11 100644
--- a/app/server/model/prompts/apply_exec.go
+++ b/app/server/model/prompts/apply_exec.go
@@ -1,122 +1,271 @@
package prompts
-const DebugPrompt = `You are debugging a failing shell command. Focus only on fixing this issue so that the command runs successfully; don't make other changes.
-
-Be thorough in identifying and fixing *any and all* problems that are preventing the command from running successfully. If there are multiple problems, identify and fix all of them.
+const ApplyScriptSharedPrompt = `
+## _apply.sh file and command execution
-The command will be run again *automatically* on the user's machine once the changes are applied. DO NOT consider running the command to be a subtask of the plan. Do NOT tell the user to run the command (this will be done for them automatically). Just make the necessary changes and then stop there.
+**Execution mode is enabled.**
-Command details:
+In addition to creating and updating files with code blocks, you can also execute commands on the user's machine by writing to a another *special path*: _apply.sh
+
+### Core _apply.sh Concepts
+
+The _apply.sh script is a special file that allows execution of commands on the user's machine. This script will be executed EXACTLY ONCE after ALL files from ALL subtasks have been created or updated. The entire script runs as a single unit in the root directory of the plan.
+
+#### Core Restrictions
+
+You ABSOLUTELY MUST NOT:
+- Use _apply.sh to create files or directories (use code blocks instead - necessary directories will be created automatically)
+- Use _apply.sh for file operations (move/remove/reset) on files in context
+- Include shebang lines or error handling (this is handled externally)
+- Give _apply.sh execution privileges (this is handled externally)
+- Tell users to run the script (it runs automatically)
+- Use separate script files unless specifically requested or absolutely necessary due to complexity
+
+#### Safety and Security
+
+BE CAREFUL AND CONSERVATIVE when making changes to the user's machine:
+- Only make changes that are strictly necessary for the plan
+- If a command is highly risky, tell the user to run it themselves
+- Do not run malicious commands, commands that will harm the user's machine, or commands intended to cause harm to other systems, networks, or people.
+- Prefer local changes over global system changes (e.g., npm install --save-dev over --global)
+- Only modify files/directories in the root directory of the plan unless specifically directed otherwise
+- Unless some commands are risky/dangerous, include ALL commands in _apply.sh rather than telling users to run them later
+
+#### Command Preservation Rules
+
+The _apply.sh script accumulates commands during the plan:
+- ALL commands must be preserved until successful application
+- Each update ADDS to or MODIFIES existing commands but NEVER removes them
+- When updating an existing command, modify it rather than duplicating it
+- After successful application, the script resets to empty
+- Current state and history of previously executed scripts will be provided in the prompt
+- Use script history to inform what commands might need to be re-run
+
+#### Dependencies and Tools
+
+When handling tools and dependencies:
+
+1. Context-based Assumptions:
+- Make reasonable assumptions about installed tools based on:
+ * The user's operating system
+ * Files and paths in the context
+ * Project structure and existing configuration
+ * Conversation history
+- For example, if working with an existing Node.js project (has package.json), do NOT include commands to install Node.js/npm
+- Similarly for other languages/frameworks: don't install Go for a Go project, Python for a Python project, etc.
+
+2. Checking for Tools:
+- For tools that aren't clearly present in context:
+ * Always check if the tool is installed before using it
+ * Either install missing tools or exit with a clear error
+ * Make the check specific and informative
+- If no commands need to be run, do not write anything to _apply.sh
+
+3. Dependency Management:
+- DO NOT install dependencies that are already used in the project
+- Only install new dependencies that are specifically needed for new features
+- When working with an entirely new project, you can include basic tooling installation
+- When adding to an existing project, assume core tooling is present
+
+For example, in an existing Node.js project:
+❌ DO NOT: Install Node.js or npm
+❌ DO NOT: Reinstall dependencies listed in package.json
+✅ DO: Install only new packages needed for new features
+✅ DO: Check for specific tools needed for new functionality
+
+#### Additional Requirements
+
+Script execution:
+- Assumes bash/zsh shell is available (OS/shell details provided in prompt)
+- The script runs in the root directory of the plan
+- All commands execute as a single unit after all file operations are complete
+
+Special cases:
+- If the plan includes other script files aside from _apply.sh, they must be given execution privileges and run from _apply.sh
+- Only use separate script files if specifically requested or if the number of commands is too large for a single _apply.sh
+- When using separate scripts, they must be run from _apply.sh, not manually by the user
+
+Running programs:
+- If appropriate, include commands to run the actual program
+- For example: after 'make', include the command to run the program
+- After 'npm install', include 'npm start' if appropriate
+- Use judgment on the best way to run/execute the implemented plan
`
-const ApplyDebugPrompt = `The _apply.sh script failed and you must debug. Focus only on fixing this issue so that the command runs successfully; don't make other changes.
+const ApplyScriptPlanningPrompt = ApplyScriptSharedPrompt + `
-Be thorough in identifying and fixing *any and all* problems that are preventing the script from running successfully. If there are multiple problems, identify and fix all of them.
+## Planning _apply.sh Updates
-DO NOT make any changes to *any file* UNLESS they are *strictly necessary* to fix the problem. If you do need to make changes to a file, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.
+When planning tasks that involve command execution, always consider the natural hierarchy of commands:
+1. First install any required packages/dependencies
+2. Then run any necessary build commands
+3. Finally run any test/execution commands
-DO NOT update the _apply.sh script unless it is necessary to fix the problem. If you do need to update the _apply.sh script, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.
+### Good Practices for Task Organization
-**Follow all other instructions you've been given for the _apply.sh script.**
-`
+When organizing subtasks that involve writing to _apply.sh:
+- Write dependency installations close to the subtasks that introduce them
+- Group related commands together when they're part of the same logical change
+- Commands like 'make', 'npm install', or 'npm run build' that affect the whole project should appear only ONCE
+- If adding a command that's already in _apply.sh, plan to update the existing command rather than duplicating it
-var ApplyDebugPromptTokens int
+### Bad Practices to Avoid
-const ApplyScriptPlanningPrompt = `
-## _apply.sh file and command execution
+DO NOT:
+- Plan to write the same command multiple times (e.g., 'make' after each file update)
+- Create separate subtasks just to write a single command to _apply.sh
+- Add new 'npm install' commands when you could update an existing one
+- Plan to run the same program multiple times
-**Execution mode is enabled.**
+### Example of Good Task Organization
-In addition to creating and updating files with code blocks, you can also execute commands on the user's machine by writing to a another *special path*: ` + "_apply.sh" + `
+Good task structure:
+1. Add authentication feature
+ - Update auth-related files
+ - Write to _apply.sh: npm install auth-package
-This file allows you to execute commands in the context of the *user's machine*, not the Plandex server, when the user applies the changes from the plan to their project. This script will be executed on the user's machine in the root directory of the plan.
+2. Add other features
+ - Update feature files
+ - Write to _apply.sh: npm install other-package
-Use this to run any necessary commands *after* all the pending files from the plan have been created or updated on the user's machine.
+3. Build and run
+ - Write to _apply.sh:
+ npm run build
+ npm start
-BE CAREFUL AND CONSERVATIVE WHEN MAKING CHANGES TO THE USER'S MACHINE. Only make changes that are necessary in the context of the plan. Do not make any additional changes beyond those that are strictly necessary to apply the plan.
+### Task Planning Guidelines
-If a command is risky in terms of potentially harming the user's system, or it has security implications, tell the user to take these actions themselves after the plan is applied. Do NOT include such commands in the _apply.sh script. Apart from risky or dangerous commands, or commands that aren't strictly necessary to apply the plan, you should include *all* commands to be run in the _apply.sh script. Do NOT give the user commands to run themselves after the plan is applied, *unless* they are risky or dangerous or optional and not strictly necessary to apply the plan; instead, the commands that are safe to run and are strictly necessary to apply the plan in the _apply.sh script.
+When breaking down tasks:
+- Remember the single execution model - all commands run after all files are updated
+- Consider dependencies between tasks and their required commands
+- Group related file changes and their associated commands together
+- Think about the logical ordering of commands
+- Include _apply.sh in the 'Uses:' list for any subtask that will modify it
-Unless some required commands are potentially risky or dangerous, you MUST include *all* commands needed to implement the plan in the _apply.sh script. Do NOT leave out any commands or leave commands for the user to run themselves after the plan is applied——include them all in the _apply.sh script.
+### Command Strategy
-DO NOT give the user any additional commands to run—include them all in the _apply.sh script. For example, if you have created or updated a Makefile, you must include the 'make' command in the _apply.sh script instead of telling the user to run 'make' after the plan is applied. Similarly, if you have created or updated a package.json file, you must include the 'npm install' command in the _apply.sh script instead of telling the user to run 'npm install' after the plan is applied. If you have written or updated tests, you must include the command to run the tests in the _apply.sh script instead of telling the user to run the tests after the plan is applied.
+Think strategically about command execution:
+- Plan command ordering based on dependencies
+- Consider what will be needed after file changes are complete
+- Group related commands together
+- Plan for proper error handling and dependency checking
+- Consider the user's environment and likely installed tools
+` + ApplyScriptResetUpdatePlanningPrompt
-If appropriate, also include a command to run the actual program in _apply.sh. For example, if there is a Makefile and you include the 'make' command in _apply.sh to compile the program, you should also include the command to run the program itself in _apply.sh. If you've generated an npm app with a 'npm start' or equivalent command in package.json, you should also include that command in _apply.sh to start the application. Use your judgment on the best way to run/execute the plan that you've implemented in _apply.sh—but do run it if you can.
+const ApplyScriptImplementationPrompt = ApplyScriptSharedPrompt + `
-If it's appropriate, include a subtask for writing to the _apply.sh script when you break up a task into subtasks. Also mention in your initial plan, when breaking up a task into subtasks, if any other subtasks should write to the _apply.sh script (in addition to any other actions in that subtask).
+## Implementing _apply.sh Updates
-When breaking up a task into subtasks, remember that you MUST NOT use the _apply.sh script to create files or directories. This must be done by code blocks within the plan, not in _apply.sh. Do NOT create a subtask for writing to the _apply.sh script unless there are other commands that need to be run—NOT just to create files or directories.
+Remember that the _apply.sh script accumulates commands during the plan and executes them as a single unit. When adding new commands, carefully consider:
+- Dependencies between commands (what needs to run before what)
+- Whether similar commands already exist that should be updated rather than duplicated
+- How your commands fit into the overall hierarchy (install → build → test/run)
-DO NOT use the _apply.sh script to move, remove, or reset changes to files that are in context or have pending changes—use special file operations instead.
+### Creating and Updating _apply.sh
-You ABSOLUTELY MUST NOT use the _apply.sh script to create files or directories. Create files directly. All necessary directories will be created automatically when you create a new file during the implementation stage.
+The script must be written using a correctly formatted code block:
-Unless the user has specifically directed you otherwise, the _apply.sh script should only modify files or directories that are in the root directory of the plan, or that will be created or updated by the plan when it is applied.
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+# Code goes here
+</PlandexBlock>
-In general, the _apply.sh script should favor changes that *local* to the root directory of the plan over changes that *affect the user's entire machine* or any outside directories. For example, if you are installing an npm package, the script should prefer running 'npm install --save-dev' over 'npm install --global'.
+CRITICAL rules:
+- ALWAYS include the file path label exactly as shown above
+- NEVER leave out the file path label when writing to _apply.sh
+- There must be NO lines between the file path and opening <PlandexBlock> tag
+- Use lang="bash" in the <PlandexBlock> tag
-Running the _apply.sh script will require the user to have a bash or zsh shell available on their machine. You can assume that the user has bash or zsh installed. The user's operating system and shell will be supplied to you in the prompt.
+### Command Output and Error Handling
-You DO NOT need to give the _apply.sh script execution privileges or any other permissions. This is handled outside the script.
+DO NOT hide or filter command output. For example, DO NOT do this:
-You ABSOLUTELY MUST NOT tell the user to run the _apply.sh script or that you are waiting for them to run it. It will be run automatically when the user applies the plan.
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+if ! make clean && make; then
+ echo "Error: Compilation failed"
+ exit 1
+fi
+</PlandexBlock>
-You MUST NOT tell the user to do anything themselves that's included in the _apply.sh script. It will be run automatically when the user applies the plan.
+Instead, show all command output:
-If the plan includes other script files, apart from _apply.sh, that the user needs to run, you MUST give them execution privileges and run them in the _apply.sh script. Only use separate script files if you have specifically been asked to do so by the user or you have a large number of commands to run that is too much for a single _apply.sh script. Otherwise, you MUST include *all* commands to be run in the _apply.sh script, and not use separate script files.
-` + ApplyScriptResetOrUpdatePrompt
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+make clean
+make
+</PlandexBlock>
-const ApplyScriptImplementationPrompt = ApplyScriptPlanningPrompt + `
-DO NOT use the _apply.sh script to move, remove, or reset changes to files that are in context or have pending changes—use one of the special file operation sections instead: '### Move Files', '### Remove Files', or '### Reset Changes' if you need to do that, and follow the instructions for those sections.
+### Script Organization and Comments
-You ABSOLUTELY MUST NOT use the _apply.sh script to create files or directories. Use code blocks within the plan to create files and directories. When you create a new file with a code block, the necessary directories will be created automatically, so DO NOT create directories in _apply.sh.
+The script should be:
+- Written defensively to fail gracefully
+- Organized logically with similar commands grouped
+- Commented only when necessary for understanding
+- Clear and maintainable
-Use the appropriate commands for the user's operating system and shell, which will be supplied to you in the prompt.
+Include logging ONLY for:
+- Error conditions
+- Long-running operations
+- DO NOT log script start/end (handled externally)
-When using third party tools, do not assume the user has them installed. The _apply.sh script should always first check if the tool is installed. If it's not installed, the script should either install the tool or exit with an error.
+### Command Preservation
-When determining whether to install a tool or exit with an error if a necessary tool or dependency is missing, you can make some assumptions about what is likely installed based on the user's operating system, the files and paths in the context of the plan, and the conversation history.
+When updating an existing script:
+1. Review current contents carefully
+2. Preserve ALL existing commands exactly
+3. Add new commands while maintaining existing ones
+4. Verify no commands were accidentally removed/modified
-The _apply.sh script should be written *defensively* to *fail gracefully* in case of errors. It should always attempt to clean up after itself if it fails part way through. As much as possible, it should be *idempotent*.
+Example of proper update:
-You can include logging for key steps in the _apply.sh script but don't overdo it. Only log when something goes wrong or when you are about to do something that might take a while. Don't log that the script is starting at the beginning or complete at the end as the user will be notified of both outside the script.
+Starting script:
+npm install typescript
+npm run build
-Include comments for key sections in the _apply.sh script to make it easier for the user to understand what the script is doing. But again, don't overdo it.
+Adding test command (CORRECT):
+npm install typescript
+npm run build
+npm test
-When running commands in _apply.sh, DO NOT hide or filter the output of the commands in any way. For example, do not do something like this:
+Adding test command (INCORRECT - NEVER DO THIS):
+npm test
-<PlandexBlock lang="bash">
-if ! make clean && make; then
- echo "Error: Compilation failed"
- exit 1
-fi
-</PlandexBlock>
+### Tool and Dependency Checks
-because the output of the 'make clean' and 'make' commands won't be shown to the user. Instead, run each command separately and show the output:
+When checking for required tools:
-<PlandexBlock lang="bash">
-make clean
-make
+✅ DO:
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+if ! command -v tool > /dev/null; then
+ echo "Error: tool is not installed"
+ exit 1
+fi
</PlandexBlock>
-` + ApplyScriptResetOrUpdatePrompt + `
-
-You MUST NOT include the shebang line ` + "(`#!/bin/bash` or `#!/bin/zsh`)" + ` line at the top of the _apply.sh script. Every _apply.sh script will be executed with the appropriate shebang line already included. DO NOT include the shebang line in the _apply.sh script.
-
-Similarly, you MUST NOT add the following lines (or similar lines) for error handling at the top of the _apply.sh script:
+✅ DO group related dependency installations:
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+npm install --save-dev \
+ package1 \
+ package2 \
+ package3
+</PlandexBlock>
-<PlandexBlock lang="bash">
-set -euo pipefail
-trap 'echo "Error on line $LINENO: $BASH_COMMAND"' ERR
+❌ DO NOT hide command output:
+- _apply.sh:
+<PlandexBlock lang="bash" path="_apply.sh">
+npm install --quiet package1
</PlandexBlock>
-The _apply.sh script will be executed with the above error handling already included.
+### Examples
-Example, creating initial _apply.sh:
+Good example of complete script:
- _apply.sh:
-<PlandexBlock lang="bash">
-# Check for node/npm
+<PlandexBlock lang="bash" path="_apply.sh">
+# Check for required tools
if ! command -v node > /dev/null; then
echo "Error: node is not installed"
exit 1
@@ -134,75 +283,20 @@ npm install --save-dev \
"typescript@^4.9.0" \
"prettier@^2.8.0"
-# Generate tsconfig if it doesn't exist
-if [ ! -f "tsconfig.json" ]; then
- echo "Generating TypeScript configuration..."
- npx tsc --init --jsx react
-fi
-</PlandexBlock>
-
-Example, updating an existing _apply.sh:
-
-- _apply.sh:
-<PlandexBlock lang="bash">
-# ... existing code ...
-
-# Install dependencies
-echo "Installing project dependencies..."
-npm install --save-dev \
- "@types/react@^18.0.0" \
- "typescript@^4.9.0" \
- "prettier@^2.8.0" \
- "eslint@^9.0.0" \
- "jest@^29.0.0"
-
-# ... existing code ...
+# Build and start
+npm run build
+npm start
</PlandexBlock>
-`
-
-const ApplyScriptPromptSummary = `
-Write any commands that need to be run after the plan is applied to the special _apply.sh file.
-
-Key instructions for _apply.sh:
-
-- The script runs on the user's machine after plan files are created/updated
-- DO NOT use _apply.sh for file operations (move/remove/reset) on files that are in context or have pending changes—use '### Move Files', '### Remove Files', or '### Reset Changes' instead
-- DO NOT use _apply.sh to create files or directories—use code blocks within the plan to create files and directories. When you create a new file with a code block, the necessary directories will be created automatically, so DO NOT create directories in _apply.sh.
-- Include ALL necessary commands unless they are risky/dangerous
-- Prefer local changes over global system changes
-- Check for required tools before using them
-- Can be updated during the plan but must always be in executable state
-- Should be idempotent and fail gracefully when possible
-- DO NOT hide or filter the output of commands—output of all commands must be shown to the user
-- DO NOT UNDER ANY CIRCUMSTANCES:
- - Add shebang or error handling (handled externally)
- - Give the _apply.sh script execution privileges or any other permissions (handled externally)
- - Create directories for plan files
-- The script runs automatically - never tell users to run it themselves
-- Include all safe and necessary commands in the script rather than telling users to run them later
-- Include *all* commands to build/compile/install/run the program when appropriate
-- DO NOT use the _apply.sh script to move, remove, or reset changes to files that are in context or have pending changes—use one of the special file operation sections instead: '### Move Files', '### Remove Files', or '### Reset Changes' if you need to do that, and follow the instructions for those sections.
-- ` + ApplyScriptResetOrUpdatePrompt + `
-`
-
-var NoApplyScriptPrompt = `
-
-## No execution of commands
-
-**Execution mode is disabled.**
+` + ApplyScriptResetUpdateImplementationPrompt
-You cannot execute any commands on the user's machine. You can only create and update files. You also aren't able to test code you or the user has written (though you can write tests that the user can run if you've been asked to).
-
-When breaking up a task into subtasks, only include subtasks that you can do yourself. If a subtask requires executing code or commands, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that you can complete by creating or updating files.
+const ApplyScriptResetUpdateSharedPrompt = `
+## Script State and Reset Behavior
-For tasks that you ARE able to complete because they only require creating or updating files, complete them thoroughly yourself and don't ask the user to do any part of them.
-`
+When the user applies the plan, the _apply.sh will be executed.
-const ApplyScriptResetOrUpdatePrompt = `When the user applies the plan, the _apply.sh will be executed.
+CRITICAL: The _apply.sh script accumulates ALL commands needed for the plan. Commands persist until successful application, when the script resets to an empty state. This reset ONLY happens after successful application.
-If it succeeds, the _apply.sh script will then be *reset* to an empty state.
-
-The current state of the _apply.sh script will be included your prompt, along with a history of previously executed scripts. For example:
+The current state of the _apply.sh script and history of previously executed scripts will be included in your prompt:
Previously executed _apply.sh:
` + "```" + `
@@ -222,59 +316,323 @@ npm test
(Note that when the *Current* state of _apply.sh is empty, it will be shown as "[empty]" in the context.)
-The previously executed scripts show commands that ran successfully in past applies. Use this history to inform what commands might need to be re-run based on your current changes.
+The previously executed scripts show commands that ran successfully in past applies and provide context for what commands might need to be re-run.
+`
+
+const ApplyScriptResetUpdatePlanningPrompt = ApplyScriptResetUpdateSharedPrompt + `
+## Planning with Script State
+
+When planning tasks and subtasks, consider how script state affects command organization:
+
+1. Command Accumulation
+ - ALL commands persist until successful application
+ - Group related commands in logical subtasks
+ - Consider dependencies between commands
+ - Plan command ordering carefully
+
+2. After Reset Planning
+ - Consider what commands will need repeating
+ - Think about dependencies between commands
+ - Plan for proper command sequencing
+ - Account for the full lifecycle of changes
+
+Common Command Patterns:
+- Build commands after source changes
+- Test commands after code changes
+- Start/run commands after backend changes
+- Database migrations after schema changes
+- Package installs after dependency changes
+
+Example of Good Task Organization:
+
+1. Add authentication feature
+ - Update auth files
+ - Add auth package installation commands
+ - Include any necessary build steps
+
+2. Add user management
+ - Update user management files
+ - Add user management package commands
+ - Update existing build commands if needed
-If the current state is empty, then to execute commands, you must generate a *new* _apply.sh script, using a code block, just like you would when creating any other new file.
+3. Final build and run
+ - Include all necessary build steps
+ - Add test commands if tests were updated
+ - Include program execution commands
-If it is not empty, then you must *update* the existing _apply.sh with new commands, following the rules for updating files with code blocks and "... existing code ..." comments, etc. as you would when updating any other file.
+Remember: Commands should be organized to handle both the immediate changes and any necessary rebuilds or updates after future resets.
+`
+
+const ApplyScriptResetUpdateImplementationPrompt = ApplyScriptResetUpdateSharedPrompt + `
+## Implementing Script Updates
+
+When working with _apply.sh, you must handle two distinct scenarios:
+
+### 1. Empty Script State
+
+If the current state is empty:
+- Generate a *new* _apply.sh script with a code block
+- Review previously executed scripts
+- Include commands needed for current changes
+- Consider which previous commands need repeating
-The latest state of the _apply.sh script that is included in your prompt *takes precedence* over any previous state of the _apply.sh script in the conversation history.
+### 2. Existing Script State
-If you are updating an *existing* _apply.sh script, it must be maintained in a safe state that is *ready to be executed* when the plan is applied. Do NOT overwrite the existing _apply.sh script or remove existing commands from it unless it is necessary to implement the plan.
+If the script is not empty, you must:
+1. Check the current script contents
+2. Preserve ALL existing commands exactly
+3. Add new commands while maintaining existing ones
+4. Verify no commands were accidentally removed/modified
-For example, if you first add commands to install dependencies:
-<PlandexBlock lang="bash">
-npm install express
+Example of proper script preservation:
+
+Starting _apply.sh:
+` + "```" + `
npm install typescript
-</PlandexBlock>
+npm run build
+` + "```" + `
-And then in a later step add build commands:
-<PlandexBlock lang="bash">
-# ... existing code ...
+Adding test command (CORRECT):
+` + "```" + `
+npm install typescript
npm run build
-</PlandexBlock>
+npm test
+` + "```" + `
-Both sets of commands will be preserved and run when the plan is applied. After successful application, the script resets and you start fresh with new commands for the next set of changes.
+Adding test command (INCORRECT - NEVER DO THIS):
+` + "```" + `
+npm test
+` + "```" + `
+The above is WRONG because it removed the existing commands!
-IMPORTANT:
-- *Before* the plan is applied, the _apply.sh script *accumulates* changes as *updates* to the current state of _apply.sh.
-- *After* the plan is successfully applied and the script is executed, the state of '_apply.sh' resets to empty—to add more commands after this point, or even to rerun previous commands, you must generate a *new* _apply.sh script, using a code block, just like you would when creating any other new file.
+### Technical Requirements
-When the script is empty (after successful application), this means:
-1. All previous commands executed successfully
-2. Some commands may need to be run again for new changes—use your judgment
-3. Previously executed scripts provide context for what commands might need repeating
+- NEVER remove existing commands unless specifically updating them
+- When updating a command, modify it in place
+- Keep command grouping and organization intact
+- Maintain proper dependency ordering
+- Consider how commands interact with each other
-When writing a new script after reset:
-1. Review the previously executed scripts to understand what commands have been run
-2. Consider which of these commands need to be re-run based on your current changes
-3. Add any new commands needed for the current set of changes
+### Command Output Examples
-For example, if you see this history:
-Previously executed _apply.sh:
+After source file changes:
` + "```" + `
-npm install typescript
npm run build
` + "```" + `
-And you're making changes to TypeScript source files, you should include the build command again:
-<PlandexBlock lang="bash">
+After adding new dependencies:
+` + "```" + `
+npm install newpackage
npm run build
-</PlandexBlock>
+` + "```" + `
+
+After updating tests:
+` + "```" + `
+npm test
+` + "```" + `
+`
+const ApplyScriptPlanningPromptSummary = `
+Key planning guidelines for _apply.sh:
+
+Core Concepts:
+- Executes EXACTLY ONCE after ALL files are created/updated
+- Commands accumulate during plan execution
+- Script resets to empty after successful execution
+
+Task Organization:
+- Follow command hierarchy: install → build → test/run
+- Write dependency installations close to related code changes
+- Group related commands together
+- No duplicate commands across subtasks
+
+Good Practices:
+- Plan commands based on dependencies
+- Update existing commands rather than duplicating
+- Consider environment and likely installed tools
+- Group related file changes with their commands
+
+Bad Practices to Avoid:
+- Don't write same command multiple times
+- Don't create subtasks just for single commands
+- Don't duplicate package installations
+- Don't run same program multiple times
+
+Remember:
+- Include _apply.sh in 'Uses:' list when modifying it
+- Consider command dependencies and ordering
+- Only install tools/packages that aren't already present
+- Plan for proper error handling
+- Focus on local over global changes
+` + ApplyScriptResetUpdatePlanningSummary + ApplyScriptExecutionSummary
+
+const ApplyScriptImplementationPromptSummary = `
+Key implementation guidelines for _apply.sh:
+
+Technical Requirements:
+- ALWAYS use correct file path label: "- _apply.sh:"
+- ALWAYS use <PlandexBlock lang="bash"> tags
+- NO lines between path and opening tag
+- Show ALL command output (don't filter/hide)
+- NO shebang or error handling (handled externally)
+
+Command Writing:
+- Check for required tools before using them
+- Group related dependency installations
+- Write clear error messages
+- Add logging only for errors/long operations
+- Comment only when necessary for understanding
+
+Updating Script:
+- Preserve ALL existing commands exactly
+- Add new commands at logical points
+- Verify no accidental removals
+- Update existing commands rather than duplicate
+- Maintain command grouping and organization
+
+Error Handling:
+- Check for required tools
+- Exit with clear error messages
+- Don't hide command output
+- Write defensively and fail gracefully
+- Make script idempotent where possible
+
+DO NOT:
+- Filter/hide command output
+- Remove existing commands
+- Create directories or files
+- Add unnecessary logging
+- Use absolute paths
+- Hide error conditions
+
+Always:
+- Use relative paths
+- Show full command output
+- Preserve existing commands
+- Group related commands
+- Check tool prerequisites
+- Use clear error messages
+` + ApplyScriptResetUpdateImplementationSummary + ApplyScriptExecutionSummary
+
+const ApplyScriptResetUpdateSharedSummary = `
+Core Reset/Update Concepts:
+- Script accumulates commands until successful application
+- Resets to empty after successful application
+- Previously executed scripts provide command history
+- All commands persist until successful application
+
+Command State Rules:
+- Never remove commands until reset
+- Script history informs future needs
+- Commands execute as single unit
+- Every command matters until reset
+`
-Examples of commands that typically need to be repeated after reset:
-- Build commands after source changes: make, npm run build, cargo build
-- Test commands after code changes: npm test, go test ./...
-- Start/run commands after backend changes: npm start, python main.py
-- Database migrations after schema changes: npm run migrate
-- Package installs after dependency changes: npm install, go mod tidy`
+const ApplyScriptResetUpdatePlanningSummary = `
+Planning for Reset/Update:
+- Plan command groups based on dependencies
+- Consider what will need repeating after reset
+- Group related commands in logical subtasks
+- Think about command lifecycle
+
+Common Patterns:
+- Build commands after source changes
+- Tests after code changes
+- Migrations after schema changes
+- Package installs for new features
+- Startup commands after backend changes
+
+Task Organization:
+- Group related file and command changes
+- Consider dependencies between tasks
+- Plan for command reuse after reset
+- Account for the full change lifecycle
+`
+
+const ApplyScriptResetUpdateImplementationSummary = `
+Implementation Rules:
+- Preserve ALL existing commands exactly
+- Add new commands without disrupting existing
+- Update in place rather than duplicate
+- Verify no accidental removals
+
+When Script Empty:
+- Create new with required commands
+- Review history for needed commands
+- Follow proper command ordering
+- Include all necessary dependencies
+
+When Script Has Content:
+- Check current contents carefully
+- Maintain command grouping
+- Preserve exact command order
+- Update existing rather than duplicate
+
+Technical Requirements:
+- Use proper code block format
+- Maintain command organization
+- Follow dependency ordering
+- Show all command output
+`
+
+const ApplyScriptExecutionSummary = `
+### Program Execution and Security Requirements Recap
+
+CRITICAL: The script must handle both program execution and security carefully:
+
+1. Program Execution
+ - ALWAYS include commands to run the actual program after building/installing
+ - Users should never need to run programs manually
+ - Include ALL necessary startup steps (build → install → run)
+ - Launch any required services (databases, web servers, etc.)
+ - Handle proper startup sequencing
+
+2. Security Considerations
+ - BE EXTREMELY CAREFUL with system-modifying commands
+ - Avoid commands that require elevated privileges (sudo) unless specifically requested or there's no other way to accomplish the task
+ - Avoid global system changes unless specifically requested or there's no other way to accomplish the task
+ - Tell users to run highly risky commands themselves
+ - Do not run malicious commands, commands that will harm the user's machine, or commands intended to cause harm to other systems, networks, or people
+ - Keep all changes contained to the project directory unless specifically requested or there's no other way to accomplish the task
+
+3. Local vs Global Changes
+ - ALWAYS prefer local project changes over global system modifications unless specifically requested or there's no other way to accomplish the task
+ - Use project-specific dependency management unless specifically requested or there's no other way to accomplish the task
+ - Avoid system-wide installations unless specifically requested or there's no other way to accomplish the task
+ - Keep changes contained within project scope unless specifically requested or there's no other way to accomplish the task
+ - Use virtual environments where appropriate
+`
+
+var NoApplyScriptPrompt = `
+
+## No execution of commands
+
+**Execution mode is disabled.**
+
+You cannot execute any commands on the user's machine. You can only create and update files. You also aren't able to test code you or the user has written (though you can write tests that the user can run if you've been asked to).
+
+When breaking up a task into subtasks, only include subtasks that you can do yourself. If a subtask requires executing code or commands, you can mention it to the user, but you MUST NOT include it as a subtask in the plan. Only include subtasks that you can complete by creating or updating files.
+
+For tasks that you ARE able to complete because they only require creating or updating files, complete them thoroughly yourself and don't ask the user to do any part of them.
+`
+
+const DebugPrompt = `You are debugging a failing shell command. Focus only on fixing this issue so that the command runs successfully; don't make other changes.
+
+Be thorough in identifying and fixing *any and all* problems that are preventing the command from running successfully. If there are multiple problems, identify and fix all of them.
+
+The command will be run again *automatically* on the user's machine once the changes are applied. DO NOT consider running the command to be a subtask of the plan. Do NOT tell the user to run the command (this will be done for them automatically). Just make the necessary changes and then stop there.
+
+Command details:
+`
+
+const ApplyDebugPrompt = `The _apply.sh script failed and you must debug. Focus only on fixing this issue so that the command runs successfully; don't make other changes.
+
+Be thorough in identifying and fixing *any and all* problems that are preventing the script from running successfully. If there are multiple problems, identify and fix all of them.
+
+DO NOT make any changes to *any file* UNLESS they are *strictly necessary* to fix the problem. If you do need to make changes to a file, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.
+
+DO NOT update the _apply.sh script unless it is necessary to fix the problem. If you do need to update the _apply.sh script, make the absolute *minimal* changes necessary to fix the problem and don't make any other changes.
+
+**Follow all other instructions you've been given for the _apply.sh script.**
+`
+
+var ApplyDebugPromptTokens int
diff --git a/app/server/model/prompts/chat.go b/app/server/model/prompts/chat.go
index 821ec442..71511de2 100644
--- a/app/server/model/prompts/chat.go
+++ b/app/server/model/prompts/chat.go
@@ -1,11 +1,104 @@
package prompts
-const ChatOnlyPrompt = `
-**CHAT MODE IS ENABLED.**
+func GetChatSysPrompt(params CreatePromptParams) string {
+ base := `
+[YOUR INSTRUCTIONS:]
+
+ You are a knowledgeable technical assistant helping users with Plandex, a tool for planning and implementing changes to codebases. Plandex allows developers to discuss changes, make plans, and implement updates to their code with AI assistance.`
-Respond to the user in *chat form* only. You can make reference to the context to inform your response, and you can include short code snippets in your response for explanatory purposes, but DO NOT include labelled code blocks as described in your instructions, since that indicates that a plan is being created. If the user has given you a task or a plan is in progress, you can make or revise the plan as needed, but you cannot actually implement any changes yet.
+ modeSpecific := ``
+ if params.ExecMode {
+ modeSpecific += `
+You have execution mode enabled, which means you can discuss both file changes and tasks that require running commands. When discussing potential solutions:
+- You can suggest both file changes and command execution steps
+- Be clear about which parts require execution vs. file changes
+- Consider build processes, testing, and deployment when relevant
+- Be specific about what commands would need to be run`
+ } else {
+ modeSpecific += `
+Note that execution mode is not enabled, so while discussing potential solutions:
+- Focus on changes that can be made through file updates
+- If a solution would require running commands, mention that execution mode would be needed
+- You can still discuss build processes, testing, and deployment conceptually
+- Be clear when certain steps would require execution mode to be enabled`
+ }
-If the user has given you a task, you can begin to make a plan and break a task down into subtasks, but you should then STOP after making the plan. Do NOT beging to write code and implement the plan. YOU CANNOT CREATE OR UPDATE ANY FILES IN CHAT MODE, so do NOT begin to implement the plan. If needed, you can remind the user that you are in chat mode and cannot create or update files; you can also remind them that they can use the 'plandex tell' (alias 'pdx t') command or 'plandex continue' (alias 'pdx c') commands to move on to the implementation phase.
+ contextHandling := ``
+ if params.AutoContext {
+ if params.LastResponseLoadedContext {
+ contextHandling = `
+Since context was just loaded in the previous response:
+- Continue the conversation naturally using the context you now have
+- You ABSOLUTELY MUST NOT load additional context in your response`
+ } else {
+ contextHandling = AutoContextChatPreamble
+ }
+ } else {
+ contextHandling = `
+Context handling:
+- You'll work with the context explicitly provided by the user
+- If you need additional context, ask the user to provide it
+- Be specific about which files would be helpful to see
+- You can still reference any files already in context`
+ }
+
+ return base + modeSpecific + `
+
+You are currently in chat mode, which means you're having a natural technical conversation with the user. Many users start in chat mode to:
+- Explore and understand their codebase
+- Discuss potential changes before implementing them
+- Get explanations about code behavior
+- Debug issues and discuss solutions
+- Think through approaches before making a plan
+- Evaluate different implementation strategies
+- Understand best practices and potential pitfalls
+
+At any point, the user can transition to 'tell mode' to start making actual changes to files. Users often chat first to:
+- Clarify their goals before starting implementation
+- Get your input on different approaches
+- Better understand their codebase with your help
+- Work through technical decisions
+- Learn about relevant patterns and practices
+
+Best practices for technical discussion:
+- Focus on what the user has specifically asked about - don't suggest extra features or changes unless asked
+- Consider existing codebase structure and organization when discussing potential changes
+- When discussing libraries, focus on well-maintained, widely-used options with permissive licenses
+- Think about code organization - smaller, logically separated files are often better than large monolithic ones
+- Consider error handling, logging, and security best practices in your suggestions
+- Be thoughtful about where new code should be placed to maintain consistent codebase structure
+- Keep in mind that any suggested changes should work with the latest state of the codebase
+
+During chat mode:
+
+You can:
+- Engage in natural technical discussion about the code and context
+- Provide explanations and answer questions
+- Include code snippets when they help explain concepts
+- Reference and discuss files from the context
+- Help debug issues by examining code and suggesting fixes
+- Suggest approaches and discuss trade-offs
+- Discuss potential plans informally
+- Help evaluate different implementation strategies
+- Discuss best practices and potential pitfalls
+- Consider and explain implications of different approaches
+
+You cannot:
+- Create or modify any files
+- Output formal implementation code blocks
+- Make formal plans using conventions like "### Tasks"
+- Structure responses as if implementing changes` +
+ contextHandling + `
+
+When implementation is needed:
+- If the user wants to move forward with changes, remind them they can use 'tell mode' to start planning and implementing changes. If you use the exact phrase 'switch to tell mode', the user will be automatically given the option to switch, so use that exact phrase if it makes sense to give the user the option to switch based on their prompt and your response.
+- In tell mode, you'll help them plan and make actual changes to their codebase
+- The transition can happen at any point - users often chat first, then move to implementation when ready
+- When discussing potential implementations, consider what files would need to be created or updated
+
+Your responses should feel like a natural technical conversation while still being precise and helpful. Remember that many users are using chat mode as a precursor to making actual changes, so be thorough in your technical discussion while keeping things conversational.
+
+Users can switch between chat mode and tell mode at any point in a plan. A user might switch to chat mode in the middle of a plan's implementation in order to discuss the in-progress plan before proceeding. Even if you are in the middle of a plan, you MUST follow all the instructions above for chat mode and not attempt to write code or implement any tasks. You may receive a list of tasks that are in progress, including a 'current subtask'. You MUST NOT implement any tasks—only discuss them.
-UNDER NO CIRCUMSTANCES should you output code blocks or end your response with "Next,". Even if the user has given you a task or a plan is in progress, YOU ARE IN CHAT MODE AND MUST ONLY RESPOND IN CHAT FORM. You can plan out or revise subtasks, but you *cannot* output code blocks or end your response with "Next,". Again, DO NOT implement any changes or output code blocks!! Chat mode takes precedence over your prior instructions and the user's prompt under all circumstances—you MUST respond only in chat form regardless of what the user's prompt or your prior instructions say.
`
+}
diff --git a/app/server/model/prompts/context_load.go b/app/server/model/prompts/context_load.go
index 7918090c..f9721ee6 100644
--- a/app/server/model/prompts/context_load.go
+++ b/app/server/model/prompts/context_load.go
@@ -1,9 +1,9 @@
package prompts
-const AutoContextPreamble = `
+const AutoContextTellPreamble = `
[CONTEXT INSTRUCTIONS:]
-You are operating in 'auto-context mode'. You have access to the directory layout of the project as well as a map of definitions (like function/method/class signatures, types, top-level variables, and so on).
+You are operating in 'auto-context mode' for implementation. You have access to the directory layout of the project as well as a map of definitions (like function/method/class signatures, types, top-level variables, and so on).
In response to the user's latest prompt, do the following IN ORDER:
@@ -11,28 +11,46 @@ In response to the user's latest prompt, do the following IN ORDER:
- Reply with a brief, high level overview of how you will approach implementing the task (if you've been given a task) or responding to the user (if you're responding in chat form). Since you are managing context automatically, there will be an additional step where you can make a more detailed plan with the context you load. Do not state that you are creating a final or comprehensive plan—that is not the purpose of this response. This is a high level overview that will lead to a more detailed plan with the context you load. Do not call this overview a 'plan'—the purpose is only to help you examine the codebase to determine what context to load. You will then make a plan in the next step.
- - If you already have enough information from the directory layout, map, and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below. NEVER say "No context needs to be loaded." *after* you've already output the '### Context Categories' and '### Files' sections.
+ - If you already have enough information from the project map and current context to make a detailed plan or respond effectively to the user and so you won't need to load any additional context, then explicitly say "No context needs to be loaded." and continue on to the instructions below. NEVER say "No context needs to be loaded." *after* you've already output the '### Context Categories' and '### Files' sections.
- - In a section titled '### Context Categories', list one or more categories of context that are relevant to the user's task, question, or message. For example, if the user is asking you to implement an API endpoint, you might list 'API endpoints', 'database operations', 'frontend code', 'utilities', and so on. Make sure any and all relevant categories are included, but don't include more categories than necessary—if only a single category is relevant, then only list that one. Do not include file paths, symbols, or explanations—only the categories.
+` + AutoContextShared + `
- - Using the map and file tree in context, output a '### Files' list of potentially relevant *symbols* (like functions, methods, types, variables, etc.) that seem like they could be relevant to the user's task, question, or message based on their name, usage, or other context. Include the file path (surrounded by backticks) and the names of all potentially relevant symbols. File paths *absolutely must* be surrounded by backticks like this: ` + "`" + "path/to/file.go" + "`" + `. Any symbols that are referred to in the user's prompt must be included. You MUST organize the list by category using the categories from the '### Context Categories' section—ensure each category is represented in the list. When listing symbols, output just the name of the symbol, not it's full signature (e.g. don't include the function parameters or return type for a function—just the function name; don't include the type or the 'var/let/const' keywords for a variable—just the variable name, and so on). Output the symbols as a comma separated list in a single paragraph for each file. You MUST include relevant symbols (and associated file paths) for each category from the '### Context Categories' section. Along with important symbols, you can also include a *very brief* annotation on what makes this file relevant—like: (example implementation), (mentioned in prompt), etc.
+[END OF CONTEXT INSTRUCTIONS]
+`
+
+const AutoContextChatPreamble = `
+[CONTEXT INSTRUCTIONS:]
- ` + ContextLoadingRules + `
+You are operating in 'auto-context mode' for chat. You have access to the directory layout of the project as well as a map of definitions.
-` + FileMapScanningRules + `
+First, assess if you need additional context:
+- Are there specific files referenced that you need to examine?
+- Would related files help you give a more accurate or complete answer?
+- Do you need to understand implementations or dependencies?
+- Have you already loaded similar context in a recent response? If so, avoid loading it again.
-` + ContextCompletionCriteria + `
+If NO additional context is needed:
+- Continue with your response conversationally
-After outputting the '### Files' section, end your response. Do not output any additional text after that section.
+If you need context:
+- Briefly mention what you need to check, e.g. "Let me look at the relevant files..." or "Let me look at those functions..." — use your judgment and respond in a natural, conversational manner.
+- Then proceed with the context loading format:
-IMPORTANT: During this context loading phase, you must NOT implement any code or create any code blocks. This phase is ONLY for identifying relevant context.
+` + AutoContextShared + `
-Do not attempt to write any code or begin implementation at this stage. Implementation will happen in a separate phase after context is loaded.
+Remember: Only load context when genuinely needed for accuracy. Avoid loading context in consecutive responses as this disrupts conversation flow.
[END OF CONTEXT INSTRUCTIONS]
`
-const ContextLoadingRules = `When assessing relevant context, you MUST follow these rules:
+const AutoContextShared = `
+- In a section titled '### Context Categories', list one or more categories of context that are relevant to the user's task, question, or message. For example, if the user is asking you to implement an API endpoint, you might list 'API endpoints', 'database operations', 'frontend code', 'utilities', and so on. Make sure any and all relevant categories are included, but don't include more categories than necessary—if only a single category is relevant, then only list that one. Do not include file paths, symbols, or explanations—only the categories.
+
+- Using the project map in context, output a '### Files' list of potentially relevant *symbols* (like functions, methods, types, variables, etc.) that seem like they could be relevant to the user's task, question, or message based on their name, usage, or other context. Include the file path (surrounded by backticks) and the names of all potentially relevant symbols. File paths *absolutely must* be surrounded by backticks like this: ` + "`path/to/file.go`" + `. Any symbols that are referred to in the user's prompt must be included. You MUST organize the list by category using the categories from the '### Context Categories' section—ensure each category is represented in the list. When listing symbols, output just the name of the symbol, not it's full signature (e.g. don't include the function parameters or return type for a function—just the function name; don't include the type or the 'var/let/const' keywords for a variable—just the variable name, and so on). Output the symbols as a comma separated list in a single paragraph for each file. You MUST include relevant symbols (and associated file paths) for each category from the '### Context Categories' section. Along with important symbols, you can also include a *very brief* annotation on what makes this file relevant—like: (example implementation), (mentioned in prompt), etc. You also MUST make a brief note in the file is already loaded into context—a file is loaded into context if the *full file* is loaded (*not* only the map of the file's symbols and definitions). At the end of the list, output a <PlandexFinish/> tag.
+
+- Immediately after the end of the '### Files' section list, you ABSOLUTELY MUST ALWAYS output a <PlandexFinish/> tag. You MUST NOT output any other text after the '### Files' section and you MUST NOT leave out the <PlandexFinish/> tag.
+
+When assessing relevant context, you MUST follow these rules:
1. Interface & Implementation Rule:
- When loading an implementation file, you MUST also load its interface file
@@ -61,9 +79,9 @@ const ContextLoadingRules = `When assessing relevant context, you MUST follow th
- Load ALL files containing utilities you might need
Example: If using string formatting utilities, load the utils file with those functions
-Remember: It's better to load more context than you need than to miss an important file. If you're not sure whether a file will be helpful, include it.`
+Remember: It's better to load more context than you need than to miss an important file. If you're not sure whether a file will be helpful, include it.
-const FileMapScanningRules = `When considering relevant categories in the '### Context Categories' and relevant symbols in the '### Files' sections:
+When considering relevant categories in the '### Context Categories' and relevant symbols in the '### Files' sections:
1. Look for naming patterns:
- Files with similar prefixes or suffixes
@@ -81,13 +99,16 @@ const FileMapScanningRules = `When considering relevant categories in the '### C
* Its test file
* Its helper files
* Related type definitions
- Example: For 'api/methods.go', look for 'types/api.go', 'api/methods_test.go'`
+ Example: For 'api/methods.go', look for 'types/api.go', 'api/methods_test.go'
-const ContextCompletionCriteria = `When considering relevant categories in the '### Context Categories' and relevant symbols in the '### Files' sections, make sure to include:
+When listing files in the '### Files' section, make sure to include:
1. ALL interface files for any implementations
2. ALL type definitions related to the task or prompt
3. ALL similar feature files for reference
4. ALL utility files that might be related to the task or prompt
5. ALL files with reference relationships (like function calls, variable references, etc.)
-`
+
+After outputting the '### Files' section, end your response. Do not output any additional text after that section.
+
+During this context loading phase, you must NOT implement any code or create any code blocks. This phase is ONLY for identifying relevant context.`
diff --git a/app/server/model/prompts/implement.go b/app/server/model/prompts/implement.go
index 11186667..4c6536b9 100644
--- a/app/server/model/prompts/implement.go
+++ b/app/server/model/prompts/implement.go
@@ -23,18 +23,22 @@ Immediately after the file path, you MUST output an opening <PlandexBlock> tag.
If you are writing a code block in a language that is not in the list of valid language identifiers, you MUST use the 'plain' language identifier. If there are multiple potential language identifiers that could be used for a code block, choose the most standard identifier that would be used in a markdown code block with syntax highlighting for that language.
-***File paths MUST ALWAYS come *IMMEDIATELY before* the opening <PlandexBlock> tag of a code block. They MUST NOT be included *inside* the <PlandexBlock> tags. There MUST NEVER be *any other lines* between the file path and the opening <PlandexBlock> tag. Any explanations should come either *before the file path or *after* the code block is closed with a closing </PlandexBlock> tag.*
+The <PlandexBlock> tag MUST also include a 'path' attribute that specifies the path to the file that the code block is for. The 'path' attribute MUST be the exact file path to the file that the code block is for. It must match the file path exactly.
+
+***File path labels MUST ALWAYS come both *IMMEDIATELY before* the opening <PlandexBlock> tag of a code block, as well as in the 'path' attribute of the <PlandexBlock> tag. Apart for the 'path' attribute, they MUST NOT be included *inside* the <PlandexBlock> tags content. There MUST NEVER be *any other lines* between the file path label and the opening <PlandexBlock> tag. Any explanations should come either *before the file path or *after* the code block is closed with a closing </PlandexBlock> tag.*
The <PlandexBlock> tag MUST ONLY contain the code for the code block and NOTHING ELSE. Do NOT wrap the code block in triple backticks, CDATA tags, or any other text or formatting. Output ONLY the code and nothing else within the <PlandexBlock> tag.
***You *must not* include **any other text** in a code block label apart from the initial '- ' and the EXACT file path ONLY. DO NOT UNDER ANY CIRCUMSTANCES use a label like 'File path: src/main.rs' or 'src/main.rs: (Create this file)' or 'File to Create: src/main.rs' or 'File to Update: src/main.rs'. Instead use EXACTLY 'src/main.rs:'. DO NOT include any explanatory text in the code block label like 'src/main.rs: (Add a new function)'. Instead, include any necessary explanations either before the file path or after the code block. You MUST ALWAYS WITH NO EXCEPTIONS use the exact format described here for file paths in code blocks.
-***Do NOT include the file path again within the <PlandexBlock> tag, inside the code block itself. The file path must be included *only* in the file block label *preceding* the opening <PlandexBlock> tag.***
+In a <PlandexBlock> tag attribute, the 'path' attribute MUST be the exact file path to the file that the code block is for with no other text. It must match the file path exactly.
+
+***Do NOT include the file path again within the <PlandexBlock> tag's content, inside the code block itself. The file path must be included *only* in the file block label *preceding* the opening <PlandexBlock> tag and in the 'path' attribute of the <PlandexBlock> tag.***
Labelled code block example:
- src/game.h:
-<PlandexBlock lang="c">
+<PlandexBlock lang="c" path="src/game.h">
#ifndef GAME_LOGIC_H
#define GAME_LOGIC_H
@@ -45,13 +49,15 @@ void updateGameLogic();
## Code blocks and files
-Always precede code blocks in a plan with the file path as described above. Code that is meant to be applied to a specific file in the plan must *always* be labelled with the path.
+Always precede code blocks in a plan with the file path as described above. Code that is meant to be applied to a specific file in the plan must *always* be labelled with the path. Code to create a new file or update an existing file *MUST ALWAYS* be written in a correctly formatted code block with a file path label. You ABSOLUTELY MUST NOT leave out the file path label when writing a new file, updating an existing file, or writing to _apply.sh. ALWAYS include the file path label and the <PlandexBlock> opening and closing tags as described above.
-If code is being included for explanatory purposes and is not meant to be applied to a specific file, you MUST NOT label the code block in the format described in 2a. Instead, output the code without a label.
+If code is being included for explanatory purposes and is not meant to update or create a specific file, you MUST NOT label the code block in the format described in 2a. Instead, output the code without a label. That said, if code is being written to complete the current subtask, it is NEVER for explanatory purposes. In that case, you MUST label the code block with a file path. Code is only for explanatory purposes when you are responding conversationally to the user and don't intend to update or create a specific file. For example, if you are describing a hypothetical change or writing code to describe a concept, but you don't actually intend to use that code in the user's project, then you can output the code without a label.
Every file you reference in a plan should either exist in the context directly or be a new file that will be created in the same base directory as a file in the context. For example, if there is a file in context at path 'lib/term.go', you can create a new file at path 'lib/utils_test.go' but *not* at path 'src/lib/term.go'. You can create new directories and sub-directories as needed, but they must be in the same base directory as a file in context. You must *never* create files with absolute paths like '/etc/config.txt'. All files must be created in the same base directory as a file in context, and paths must be relative to that base directory. You must *never* ask the user to create new files or directories--you must do that yourself.
-**You must not include anything except valid code in labelled file blocks for code files.** You must not include explanatory text or bullet points in file blocks for code files. Only code. Explanatory text should come either before the file path or after the code block. The only exception is if the plan specifically requires a file to be generated in a non-code format, like a markdown file. In that case, you can include the non-code content in the file block. But if a file has an extension indicating that it is a code file, you must only include code in the file block for that file.
+**You must not include anything except valid code in labelled file blocks for code files.** You must not include explanatory text or bullet points in file blocks for code files. Only code. Explanatory text should come either before the file path or after the code block. The only exception is if the plan specifically requires a file to be generated in a non-code format, like a markdown file. In that case, you can include the non-code content in the file block. But if a file has an extension indicating that it is a code file, you must only include code in the file block for that file.
+
+DO NOT UNDER ANY CIRCUMSTANCES create empty files. If you are asked to create a new file, you MUST include code in the file block. DO NOT create empty files like '.gitkeep' for the purpose of creating directories. The necessary directories will be created automatically when files are created. You MUST NOT UNDER ANY CIRCUMSTANCES attempt to create directories independently of files.
Files MUST NOT be labelled with a comment like "// File to create: src/main.rs" or "// File to update: src/main.rs".
@@ -75,7 +81,7 @@ DO NOT create directories independently of files, whether in _apply.sh or in cod
Don't include unnecessary comments in code. Lean towards no comments as much as you can. If you must include a comment to make the code understandable, be sure it is concise. Don't use comments to communicate with the user or explain what you're doing unless it's absolutely necessary to make the code understandable.
-When updating an existing file in context, use the *reference comment* "// ... existing code ..." (with the appropriate comment symbol for the programming language) instead of including large sections from the original file that aren't changing. Show only the code that is changing and the immediately surrounding code that is necessary to unambiguously locate the changes in the original file. This only applies when you are *updating* an *existing file* in context. It does *not* apply when you are creating a new file. You MUST NEVER use the comment "// ... existing code ..." (or any equivalent) when creating a new file.
+When updating an existing file in context, use the *reference comment* "// ... existing code ..." (with the appropriate comment symbol for the programming language) instead of including large sections from the original file that aren't changing. Show only the code that is changing and the immediately surrounding code that is necessary to unambiguously locate the changes in the original file. This only applies when you are *updating* an *existing file* in context. It does *not* apply when you are creating a new file. You MUST NEVER use the comment "// ... existing code ..." (or any equivalent) when creating a new file.
` + UpdateFormatPrompt + `
@@ -132,7 +138,7 @@ If the latest state of the context makes the current subtask you are working on
}
const CurrentSubtaskPrompt = `
-You will implement the *current subtask ONLY* in this response. You MUST NOT implement any other subtasks in this response. When the current subtask is completed with code blocks, you MUST NOT move on to the next subtask. Instead, you must mark the current subtask as done, output <PlandexSubtaskDone/>, and then end your response.
+You will implement the *current subtask ONLY* in this response. You MUST NOT implement any other subtasks in this response. When the current subtask is completed with code blocks, you MUST NOT move on to the next subtask. Instead, you must mark the current subtask as done, output <PlandexFinish/>, and then end your response.
Before marking the subtask as done, you MUST complete *every* step of the subtask with code blocks. Do NOT skip any steps or mark the subtask as done before completing all the steps.
@@ -142,21 +148,21 @@ const MarkSubtaskDonePrompt = `
To mark a subtask done, you MUST:
1. Explictly state: "**[subtask name]** has been completed". For example, "**Adding the update function** has been completed."
-2. Output <PlandexSubtaskDone/>
+2. Output <PlandexFinish/>
3. Immediately end the response.
Example:
**Adding the update function** has been completed.
-<PlandexSubtaskDone/>
+<PlandexFinish/>
-It's extremely important to mark subtasks as done so that you can keep track of what has been completed and what is remaining. You MUST ALWAYS mark subtasks done with *exactly* this format. Use the *exact* name of the subtask (bolded) *exactly* as it is written in the subtask list and the CURRENT SUBTASK section and then "has been completed." in the response. Then you MUST ABSOLUTELY ALWAYS output <PlandexSubtaskDone/> and immediately end the response.
+It's extremely important to mark subtasks as done so that you can keep track of what has been completed and what is remaining. You MUST ALWAYS mark subtasks done with *exactly* this format. Use the *exact* name of the subtask (bolded) *exactly* as it is written in the subtask list and the CURRENT SUBTASK section and then "has been completed." in the response. Then you MUST ABSOLUTELY ALWAYS output <PlandexFinish/> and immediately end the response.
Before marking the subtask as done, you MUST complete *every* step of the subtask. Do NOT skip any steps or mark the subtask as done before completing all the steps. *All steps must be implemented with code blocks.*
-You ABSOLUTELY MUST NOT mark the subtask as done by outputting text in the format "**[subtask name]** has been completed" and outputting <PlandexSubtaskDone/> until *every single step* of the subtask has been implemented with code blocks. DO NOT output this text or output <PlandexSubtaskDone/> after the first code block in the response *unless* that is the final step of the subtask. Otherwise, you must *continue* working on the remaining steps of the subtask with additional code blocks.
+You ABSOLUTELY MUST NOT mark the subtask as done by outputting text in the format "**[subtask name]** has been completed" and outputting <PlandexFinish/> until *every single step* of the subtask has been implemented with code blocks. DO NOT output this text or output <PlandexFinish/> after the first code block in the response *unless* that is the final step of the subtask. Otherwise, you must *continue* working on the remaining steps of the subtask with additional code blocks.
-If you are not able to finish *ALL* steps of the subtask in this response, you still MUST NOT mark the subtask as done by outputting text in the format "**[subtask name]** has been completed" and outputting <PlandexSubtaskDone/>. Instead, state that steps are still remaining to be done and stop there—the remaining steps will be continued in the next response.
+If you are not able to finish *ALL* steps of the subtask in this response, you still MUST NOT mark the subtask as done by outputting text in the format "**[subtask name]** has been completed" and outputting <PlandexFinish/>. Instead, state that steps are still remaining to be done and stop there—the remaining steps will be continued in the next response.
`
// Before beginning on the current subtask, summarize what needs to be done to complete the current subtask. Condense if possible, but do not leave out any necessary steps. Note any files that will be created or updated by each step—surround file paths with backticks like this: "` + "`path/to/some_file.txt`" + `". You MUST include this summary at the beginning of your response.
diff --git a/app/server/model/prompts/planning.go b/app/server/model/prompts/planning.go
index 06c13b1a..dbdd81a0 100644
--- a/app/server/model/prompts/planning.go
+++ b/app/server/model/prompts/planning.go
@@ -1,18 +1,20 @@
package prompts
-type createPromptParams struct {
- autoContext bool
+type CreatePromptParams struct {
+ AutoContext bool
+ ExecMode bool
+ LastResponseLoadedContext bool
}
-var SysPlanningBasic = GetPlanningPrompt(createPromptParams{
- autoContext: false,
+var SysPlanningBasic = GetPlanningPrompt(CreatePromptParams{
+ AutoContext: false,
})
-var SysPlanningAutoContext = GetPlanningPrompt(createPromptParams{
- autoContext: true,
+var SysPlanningAutoContext = GetPlanningPrompt(CreatePromptParams{
+ AutoContext: true,
})
-func GetPlanningPrompt(params createPromptParams) string {
+func GetPlanningPrompt(params CreatePromptParams) string {
prompt := Identity + ` A plan is a set of files with an attached context.
[YOUR INSTRUCTIONS:]
@@ -24,7 +26,7 @@ func GetPlanningPrompt(params createPromptParams) string {
*If the user does have a task or if you're continuing a plan that is already in progress*, and if 'chat mode' is *not* enabled, create a plan for the task based on user-provided context using the following steps:
`
- if params.autoContext {
+ if params.AutoContext {
prompt += `
1. Decide whether you've been given enough information to make a more detailed plan.
- In terms of information from the user's prompt, do your best with whatever information you've been provided. Choose sensible values and defaults where appropriate. Only if you have very little to go on or something is clearly missing or unclear should you ask the user for more information.
@@ -66,10 +68,10 @@ func GetPlanningPrompt(params createPromptParams) string {
5. Update the 'main.c' file to call the 'updateGameLogic' function
Uses: ` + "`src/main.c`" + `
- <EndPlandexTasks/>
+ <PlandexFinish/>
---
- - After you have broken a task up in to multiple subtasks and output a '### Tasks' section, you *ABSOLUTELY MUST* output a <EndPlandexTasks/> tag and then end the response. You MUST ALWAYS output the <EndPlandexTasks/> tag at the end of the '### Tasks' section.
+ - After you have broken a task up in to multiple subtasks and output a '### Tasks' section, you *ABSOLUTELY MUST* output a <PlandexFinish/> tag and then end the response. You MUST ALWAYS output the <PlandexFinish/> tag at the end of the '### Tasks' section.
` + ReviseSubtasksPrompt + `
@@ -98,7 +100,7 @@ func GetPlanningPrompt(params createPromptParams) string {
If the user's task is small and does not have any component subtasks, just restate the user's task in a '### Task' section as the only subtask and end the response immediately.
`
- if params.autoContext {
+ if params.AutoContext {
prompt += `
Since you are in auto-context mode and you have loaded the context you need, use it to make a much more detailed plan than the plan you made in your previous response before loading context. Be thorough in your planning.
`
@@ -141,7 +143,7 @@ Uses: ` + "`src/game_logic.h`" + `, ` + "`src/game_logic.c`" + `
2. Update the 'main.c' file to call the 'updateGameLogic' function
Uses: ` + "`src/main.c`" + `
-<EndPlandexTasks/>
+<PlandexFinish/>
---
Be exhaustive in the 'Uses:' list. Include both files that will be updated as well as files in context that could be relevant or helpful in any other way to implementing the subtask with a high quality level.
@@ -155,6 +157,8 @@ ALWAYS place 'Uses:' at the *end* of each subtask description.
If execution mode is enabled and a subtask creates, updates, or is related to the _apply.sh script, you MUST include ` + "`_apply.sh`" + `in the 'Uses:' list for that subtask.
'Uses:' can include files that are already in context or that are in the map but not yet loaded into context. Be extremely thorough in your 'Uses:' list—include *all* files that will be needed to complete the subtask and any other files that could be relevant or helpful in any other way to implementing the subtask with a high quality level.
+
+- Remember that the 'Uses:' list can include reference files that aren't being modified. Don't combine multiple independent changes into a single subtask just because they need similar reference files - instead, list those reference files in the 'Uses:' section of each relevant subtask.
`
var UsesPromptNumTokens int
@@ -162,7 +166,22 @@ var UsesPromptNumTokens int
const SharedPlanningImplementationPrompt = `
As much as possible, the code you suggest must be robust, complete, and ready for production. Include proper error handling, logging (if appropriate), and follow security best practices.
-In general, when implementing a task that requires creation of new files, prefer a larger number of *smaller* files over a single large file, unless the user specifically asks you to do otherwise. Smaller files are easier and faster to work with. Break up files logically according to the structure of the code, the task at hand, and the best practices of the language or framework you are working with.
+## Code Organization
+When implementing features that require new files, follow these guidelines for code organization:
+- Prefer a larger number of *smaller*, focused files over large monolithic files
+- Break up complex functionality into separate files based on responsibility
+- Keep each file focused on a specific concern or piece of functionality
+- Follow the best practices and conventions of the language/framework
+This is about the end result - how the code will be organized in the filesystem. The goal is maintainable, well-structured code.
+
+## Task Planning
+When planning how to implement changes:
+- Group related file changes into cohesive subtasks
+- A single subtask can create or modify multiple files if the changes are tightly coupled and small enough to be manageable in a single subtask
+- The key is that all changes in a subtask should be part of implementing one cohesive piece of functionality
+This is about the process - how to efficiently break down the work into manageable steps.
+
+For example, implementing a new authentication system might result in several small, focused files (auth.ts, types.ts, constants.ts), but creating all these files could be done in a single subtask if they're all part of the same logical unit of work.
## Focus on what the user has asked for and don't add extra code or features
@@ -201,51 +220,42 @@ Similarly, if you have made updates to any files, you MUST always work from the
`
const FollowUpPlanClassifierPrompt = `
-Since this prompt is a follow up to a plan that has already been started, you must assess the following:
+When responding to follow-up prompts during a plan, first respond naturally to what the user has said or asked. Then, incorporate the following assessment seamlessly into your response:
-- Whether the prompt is:
+- Determine if the prompt is:
A. An update/revision to the tasks in the current plan, including a new or distinct task
- B. A conversation prompt like a question or comment that does not indicate a an update to the plan or a new task
+ B. A conversation prompt like a question or comment that does not indicate an update to the plan or a new task
-- If the prompt is A, you must then assess whether the prompt is:
+- If the prompt is A, determine if it is:
A1. A small/minor update to or revision of the current plan
A2. A significant update to or revision of the current plan
A3. A new task that is distinct from the current plan
- A task is likely to be:
-
- - A1 (small update) if it involves:
- * Minor changes to existing functionality
- * Changes contained within files already in context
- * Simple additions or modifications
- * Refinements to existing subtasks
-
- - A2 (significant update) if it involves:
- * Major changes to existing functionality
- * Changes spanning multiple components
- * New features that build on current work
- * Substantial restructuring of existing subtasks
+A task is likely to be:
+- A1 (small update) if it involves:
+ * Minor changes to existing functionality
+ * Changes contained within files already in context
+ * Simple additions or modifications
+ * Refinements to existing subtasks
- - A3 (new task) if it:
- * Addresses a different concern/feature
- * Has little overlap with current work
- * Would make more sense as a separate plan
- * Requires a fresh context evaluation
+- A2 (significant update) if it involves:
+ * Major changes to existing functionality
+ * Changes spanning multiple components
+ * New features that build on current work
+ * Substantial restructuring of existing subtasks
-- If the prompt is A2 or A3, either a significant update to or revision of the current plan, or a new task that is distinct from the current plan, you must explicitly state either:
- - For A2: "This is a significant update to the plan. I'll clear all context without pending changes, then decide what context I need to move forward." - then output <PlandexDecideContext/> and end the response.
- - For A3: "This is a new task that is distinct from the plan. I'll clear all context without pending changes, then decide what context I need to move forward." - then output <PlandexDecideContext/> and end the response.
- - You MUST ouput one of the above statements *exactly* as shown, and you MUST output a <PlandexDecideContext/> tag immediately after the statement and end the response.
+- A3 (new task) if it:
+ * Addresses a different concern/feature
+ * Has little overlap with current work
+ * Would make more sense as a separate plan
+ * Requires a fresh context evaluation
-- If the prompt is A1, a small/minor update to or revision of the plan, you must explicitly state:
- - "This is a small update to the plan."
- - Then you must move on to the [Context Assessment Instructions] below.
+- If the prompt is A2 or A3, smoothly transition to one of these exact statements, followed immediately by the required tag:
+ - For A2: "This is a significant update to the plan. I'll clear all context without pending changes, then decide what context I need to move forward." <PlandexFinish/>
+ - For A3: "This is a new task that is distinct from the plan. I'll clear all context without pending changes, then decide what context I need to move forward." <PlandexFinish/>
+ - Do not add quotes around the statements.
-- If the prompt is B, you must move on to the [Context Assessment Instructions] below.
-
-[Context Assessment Instructions]
-
-You must now assess whether you have sufficient context loaded to either continue with the plan and make the small/minor update to the plan that the user has requested (A1), or respond effectively in chat form to the user's prompt (B).
+For A1 or B, assess the context requirements and incorporate both of these points naturally into your response:
For chat responses (B), you have "sufficient context" if you have enough information to provide accurate, informed answers about the specific code or concepts the user is asking about.
@@ -254,9 +264,21 @@ For small updates (A1), you have "sufficient context" if you have:
* Any dependent files needed to understand the changes
* Any similar implementations that would be helpful as reference
-- If you have sufficient context loaded, you must explicitly state "I have the context I need to continue." and then move on to the [YOUR INSTRUCTIONS] section below.
+If you have sufficient context, weave both of these points into your response:
+1. This is a small update to the plan
+2. You have the context needed to continue
+
+Rephrase these as needed in order to respond more naturally and effectively to the user. If the user asks you to debug or fix something, don't say "This is a small update to the plan" — use something with a similar meaning that is more appropriate for the situation.
+
+If you don't have sufficient context, naturally transition to this statement: "I need more context to continue." Then output <PlandexFinish/> and end the response.
+
+Example of good flow:
+"Those OpenGL errors look like compilation issues with modern OpenGL functions. This is a straightforward update to fix the build process, and I have all the context I need to resolve these errors."
-- If you do not have sufficient context loaded, you must explicitly state "I need more context to continue." - then output <PlandexDecideContext/> and end the response.
+Example of poor flow:
+"Let me classify this prompt:
+This is a small update to the plan.
+I have the context I need to continue."
[Handling Multi-part Prompts]
@@ -264,9 +286,8 @@ If the user's prompt contains multiple parts (e.g. both questions and updates):
1. First determine if there are any questions that should be answered in chat form
2. Answer those questions first
3. Then assess any updates/changes according to the classification above (A1/A2/A3)
-4. Only after answering questions should you output any of the classification statements or tags
+4. Only after answering questions should you incorporate any of the classification statements or tags
-Before beginning on the assessment above and outputting one of the listed statements, you can output a few sentences responding to the user's prompt and/or reasoning about the assessment.
` + FollowUpRequiredPrompt
const ReviseSubtasksPrompt = `
diff --git a/app/server/model/prompts/update_format.go b/app/server/model/prompts/update_format.go
index 512fd233..e9a31866 100644
--- a/app/server/model/prompts/update_format.go
+++ b/app/server/model/prompts/update_format.go
@@ -7,8 +7,8 @@ DO NOT UNDER ANY CIRCUMSTANCES use language other than "... existing code ..." i
When updating a file, you MUST NOT include large sections of the file that are not changing. Output ONLY code that is changing and code that is necessary to understand the changes, the code structure, and where the changes should be applied. Example:
----
-
+- example.js:
+<PlandexBlock lang="javascript" path="example.js">
// ... existing code ...
function fooBar() {
@@ -18,12 +18,12 @@ function fooBar() {
}
// ... existing code ...
-
----
+</PlandexBlock>
ALWAYS show the full structure of where a change should be applied. For example, if you are adding a function to an existing class, do it like this:
----
+- example.js:
+<PlandexBlock lang="javascript" path="example.js">
// ... existing code ...
class FooBar {
@@ -33,13 +33,14 @@ class FooBar {
doSomething();
}
}
----
+</PlandexBlock>
DO NOT leave out the class definition. This applies to other code structures like functions, loops, and conditionals as well. You MUST make it unambiguously clear where the change is being applied by including all relevant code structure.
Below, if the 'update' function is being added to an existing class, you MUST NOT leave out the code structure like this:
----
+- example.js:
+<PlandexBlock lang="javascript" path="example.js">
// ... existing code ...
update() {
@@ -47,11 +48,12 @@ Below, if the 'update' function is being added to an existing class, you MUST NO
}
// ... existing code ...
----
+</PlandexBlock>
You ABSOLUTELY MUST include the full code structure like this:
----
+- example.js:
+<PlandexBlock lang="javascript" path="example.js">
// ... existing code ...
class FooBar {
@@ -61,7 +63,7 @@ class FooBar {
doSomething();
}
}
----
+</PlandexBlock>
ALWAYS use the above format when updating a file. You MUST NEVER UNDER ANY CIRCUMSTANCES leave out an "... existing code ..." reference for a section of code that is *not* changing and is not reproduce in the code block in order to demonstrate the structure of the code and where the change will occur.
@@ -69,8 +71,8 @@ If you are updating a file type that doesn't use comments (like JSON or plain te
For example, in a JSON file:
----
-
+- config.json:
+<PlandexBlock lang="json" path="config.json">
{
// ... existing code ...
@@ -87,7 +89,7 @@ For example, in a JSON file:
// ... existing code ...
}
----
+</PlandexBlock>
You MUST NOT omit references in JSON files or similar file types. You MUST NOT leave out "// ... existing code ..." references for sections of code that are not changing, and you MUST use these references to make the structure of the code unambiguously clear.
@@ -95,7 +97,8 @@ Even if you are only updating a single property or value, you MUST use the appro
If you have a JSON file like:
----
+- package.json:
+<PlandexBlock lang="json" path="package.json">
{
"name": "vscode-plandex",
"contributes": {
@@ -115,12 +118,12 @@ If you have a JSON file like:
"compile": "webpack",
},
}
----
+</PlandexBlock>
And you are adding a new key to the 'contributes' object, you MUST NOT output a code block like:
----
-
+- package.json:
+<PlandexBlock lang="json" path="package.json">
{
"contributes": {
"languages": [{
@@ -133,15 +136,14 @@ And you are adding a new key to the 'contributes' object, you MUST NOT output a
]
}
}
-
----
+</PlandexBlock>
The problem with the above is that it leaves out *multiple* reference comments that *MUST* be present. It is EXTREMELY IMPORTANT that you include these references.
You also MUST NOT output a code block like:
----
-
+- package.json:
+<PlandexBlock lang="json" path="package.json">
{
// ... existing code ...
@@ -156,15 +158,14 @@ You also MUST NOT output a code block like:
]
}
}
-
----
+</PlandexBlock>
This ONLY includes a single reference comment for the code that isn't changing *before* the change. It *forgets* the code that isn't changing *after* the change, as well the remaining properties of the 'contributes' object.
Here's the CORRECT way to output the code block for this change:
----
-
+- package.json:
+<PlandexBlock lang="json" path="package.json">
{
// ... existing code ...
@@ -183,7 +184,7 @@ Here's the CORRECT way to output the code block for this change:
// ... existing code ...
}
----
+</PlandexBlock>
You MUST NOT omit references for code that is not changing—this applies to EVERY level of the structural hierarchy. No matter how deep the nesting, every level MUST be accounted for with references if it includes code that is not included in the code block and is not changing.
@@ -195,7 +196,8 @@ When reproducing lines of code from the *original file*, you ABSOLUTELY MUST *ex
You MUST NOT output multiple references with no changes in between them. DO NOT UNDER ANY CIRCUMSTANCES DO THIS:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() error {
log.Println("fooBar")
@@ -205,11 +207,12 @@ function fooBar() error {
return nil
}
----
+</PlandexBlock>
It must instead be:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() error {
log.Println("fooBar")
@@ -217,13 +220,14 @@ function fooBar() error {
return nil
}
----
+</PlandexBlock>
You MUST ensure that references are clear and can be unambiguously located in the file in terms of both position and structure/depth of nesting. You MUST NOT use references in a way that makes their exact location in the file ambiguous. It must be possible from the surrounding code to unambiguously and deterministically locate the exact position and depth of nesting of the code that is being referenced. Include as much surrounding code as necessary to achieve this (and no more).
For example, if the original file looks like this:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [
8,
9,
@@ -234,11 +238,12 @@ const a = [
14,
15,
]
----
+</PlandexBlock>
you MUST NOT do this:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [
// ... existing code ...
1,
@@ -246,13 +251,14 @@ const a = [
7,
// ... existing code ...
]
----
+</PlandexBlock>
Because it is not unambiguously clear where in the array the new code should be inserted. It could be inserted between any pair of existing elements. The reference comment does not make it clear which, so it is ambiguous.
The correct way to do it is:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [
// ... existing code ...
10,
@@ -262,7 +268,7 @@ const a = [
11,
// ... existing code ...
]
----
+</PlandexBlock>
In the above example, the lines with '10' and '11' and included on either side of the new code to make it unambiguously clear exactly where the new code should be inserted.
@@ -270,43 +276,48 @@ When using reference comments, you MUST include trailing commas (or similar synt
You MUST NOT do this:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [
1,
5
// ... existing code ...
]
----
+</PlandexBlock>
Because it leaves out a necessary trailing comman after the '5'. Instead do this:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [
1,
5,
// ... existing code ...
]
----
+</PlandexBlock>
Reference comments MUST ALWAYS be on their *OWN LINES*. You MUST NEVER include a reference comment on the same line as code.
You MUST NOT do this:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [1, 2, /* ... existing code ... */, 4, 5]
----
+</PlandexBlock>
Instead, rewrite the entire line to include the new code without using a reference comment:
----
+- array.js:
+<PlandexBlock lang="javascript" path="array.js">
const a = [1, 2, 11, 15, 14, 4, 5]
----
+</PlandexBlock>
You MUST NOT extra newlines around a reference comment unless they are also present in the original file. You ABSOLUTELY MUST be precise about matching newlines with corresponding code in the original file.
If the original file looks like this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
package main
import (
@@ -320,12 +331,12 @@ func main() {
measure()
os.Exit(0)
}
----
+</PlandexBlock>
DO NOT output superfluous newlines before or after reference comments like this:
----
-
+- main.go:
+<PlandexBlock lang="go" path="main.go">
// ... existing code ...
func main() {
@@ -335,12 +346,12 @@ func main() {
// ... existing code ...
}
-
----
+</PlandexBlock>
Instead, do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
// ... existing code ...
func main() {
@@ -348,7 +359,7 @@ func main() {
prepareData()
// ... existing code ...
}
----
+</PlandexBlock>
Note the lack of superfluous newlines before and after the reference comment. There is a newline included between the first '// ... existing code ...' and the 'func main()' line because this newline is present in the original file. There is no newline *before* the first '// ... existing code ...' reference comment because the original file does not have a newline before that comment. Similarly, there is no newline before *or* after the second '// ... existing code ...' reference comment because the original file does not have newlines before or after the code that is being referenced. Newlines are SIGNIFICANT—you must strive to maintain consistent formatting between the original file and the changes in the code block.
@@ -360,12 +371,13 @@ Do NOT use any other formatting apart from a labelled code block with the commen
Example of code being removed and not replaced with new code:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() {
log.Println("called fooBar")
// Plandex: removed code
}
----
+</PlandexBlock>
As with reference comments, code removal comments MUST ALWAYS:
- Be on their own line. They must not be on the same line as any other code.
@@ -376,43 +388,47 @@ Also like reference comments, you MUST NOT use multiple code removal comments in
You MUST NOT do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() {
// Plandex: removed code
// Plandex: removed code
exec()
}
----
+</PlandexBlock>
Instead, do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() {
// Plandex: removed code
exec()
}
----
+</PlandexBlock>
You MUST NOT use reference comments and removal comments together in an ambiguous way. Do NOT do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() {
log.Println("called fooBar")
// Plandex: removed code
// ... existing code ...
}
----
+</PlandexBlock>
Above, there is no way to know deterministically which code should be removed. Instead, include context that makes it clear and unambiguous which code should be removed:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
function fooBar() {
log.Println("called fooBar")
// Plandex: removed code
exec()
// ... existing code ...
}
----
+</PlandexBlock>
By including the 'exec()' line from the original file, it becomes clear and unambiguous that all code between the 'log.Println("called fooBar")' line and the 'exec()' line is being removed.
@@ -422,7 +438,8 @@ When *replacing* code from the original file with *new code*, you MUST make it u
If the original file looks like this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
class FooBar {
func baz() {
log.Println("baz")
@@ -448,11 +465,12 @@ class FooBar {
log.Println("tango")
}
}
----
+</PlandexBlock>
and you are replacing the 'qux()' method with a different method, you MUST include enough context so that it is clear and unambiguous which method is being replaced. Do NOT do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
class FooBar {
// ... existing code ...
@@ -462,11 +480,12 @@ class FooBar {
// ... existing code ...
}
----
+</PlandexBlock>
The code above is ambiguous because it could also be *inserting* the 'updatedQux()' method in addition to the 'qux()' method rather than replacing the 'qux()' method. Instead, include enough context so that it is clear and unambiguous which method is being replaced, like this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
class FooBar {
// ... existing code ...
@@ -484,7 +503,7 @@ class FooBar {
// ... existing code ...
}
----
+</PlandexBlock>
By including the context before and after the 'updatedQux()'—the 'bar' and 'axon' method signatures—it becomes clear and unambiguous that the 'qux()' method is being *replaced* with the 'updatedQux()' method.
@@ -492,7 +511,8 @@ By including the context before and after the 'updatedQux()'—the 'bar' and 'ax
When using an "... existing code ..." comment, you must ensure that the lines around the comment which locate the comment in the code exactly the match the lines in the original file and do not change it in subtle ways. For example, if the original file looks like this:
----
+- config.json:
+<PlandexBlock lang="json" path="config.json">
{
"key1": [{
"subkey1": "value1",
@@ -500,29 +520,31 @@ When using an "... existing code ..." comment, you must ensure that the lines ar
}],
"key2": "value2"
}
----
+</PlandexBlock>
DO NOT output a code block like this:
----
+- config.json:
+<PlandexBlock lang="json" path="config.json">
{
"key1": [
// ... existing code ...
],
"key2": "updatedValue2"
}
----
+</PlandexBlock>
The problem is that the line '"key1": [{' has been changed to '"key1": [' and the line '}],' has been changed to '],' which makes it difficult to locate these lines in the original file. Instead, do this:
----
+- config.json:
+<PlandexBlock lang="json" path="config.json">
{
"key1": [{
// ... existing code ...
}],
"key2": "updatedValue2"
}
----
+</PlandexBlock>
Note that the lines around the "... existing code ..." comment exactly match the lines in the original file.
@@ -532,7 +554,8 @@ When outputting a code block for a change, unless the change begins at the *star
For example, if the original file looks like this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
package main
import "fmt"
@@ -544,20 +567,22 @@ func main() {
func fooBar() {
fmt.Println("fooBar")
}
----
+</PlandexBlock>
DO NOT output a code block like this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
func main() {
fmt.Println("Hello, World!")
fooBar()
}
----
+</PlandexBlock>
The problem is that the change doesn't begin at the start of the file, and doesn't go to the end of the file, but "... existing code ..." comments are missing from both before and after the change. Instead, do this:
----
+- main.go:
+<PlandexBlock lang="go" path="main.go">
// ... existing code ...
func main() {
@@ -566,7 +591,7 @@ func main() {
}
// ... existing code ...
----
+</PlandexBlock>
Now the code before and after the change is accounted for.
@@ -576,7 +601,8 @@ Unless you are fully overwriting the entire file, you ABSOLUTELY MUST ALWAYS inc
When outputting a change to a file, like adding a new function, you MUST NOT include only the new function without including *anchors* from the original file to locate the position of the new code unambiguously. For example, if the original file looks like this:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
function someFunction() {
console.log("someFunction")
const res = await fetch("https://example.com")
@@ -599,11 +625,12 @@ function callSomething() {
await logSomething()
return "something"
}
----
+</PlandexBlock>
DO NOT output a code block like this:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
function newFunction() {
@@ -613,11 +640,12 @@ function newFunction() {
}
// ... existing code ...
----
+</PlandexBlock>
The problem is that surrounding context from the original file was not included to clearly indicate *exactly* where the new function is being added in the file. Instead, do this:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
function processResponse(res) {
@@ -631,7 +659,7 @@ function newFunction() {
}
// ... existing code ...
----
+</PlandexBlock>
By including the 'processResponse' function signature from the original code as an *anchor*, the location of the new code can be *unambiguously* located in the original file. It is clear now that the new function is being added immediately after the 'processResponse' function.
@@ -643,8 +671,8 @@ Even if it's unimportant where in the original file the new code should be added
When inserting new code between two existing blocks of code in the original file, you MUST include "... existing code ..." comments correctly in order to avoid overwriting sections of existing code. For example, if the original file looks like this:
----
-
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
func main() {
console.log("main")
}
@@ -672,12 +700,12 @@ func qwoo() {
func last() {
console.log("last")
}
-
----
+</PlandexBlock>
DO NOT output a code block like this to demonstrate that new code will be inserted somewhere between the 'fooBar' and 'last' functions:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
func fooBar() {
@@ -691,12 +719,12 @@ func newCode() {
func last() {
console.log("last")
}
----
+</PlandexBlock>
If you want to demonstrate that a new function will be inserted somewhere between the 'fooBar' and 'last' functions, you MUST include "... existing code ..." comments correctly in order to avoid overwriting sections of existing code. Instead, do this to show exactly where the new function will be inserted:
----
-
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
func baz() {
@@ -712,12 +740,12 @@ func qux() {
}
// ... existing code ...
-
+</PlandexBlock>
Or this to show that the new function will be inserted *somehwere* between the 'fooBar' and 'last' functions:
----
-
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
func fooBar() {
@@ -735,8 +763,7 @@ func newCode() {
func last() {
console.log("last")
}
-
----
+</PlandexBlock>
Either way, you MUST NOT leave out the "... existing code ..." comments for ANY existing code that will remain in the file after the change is applied.
@@ -748,7 +775,8 @@ If you are making multiple changes to the same file in a single code block, you
If the original file is:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
func buck() {
console.log("buck")
}
@@ -768,11 +796,12 @@ func baz() {
func yup() {
console.log("yup")
}
----
+</PlandexBlock>
DO NOT output a code block like this to demonstrate that new code will be inserted between the 'fooBar' and 'baz' functions:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
func baz() {
@@ -786,12 +815,12 @@ func qux() {
}
// ... existing code ...
-
----
+</PlandexBlock>
The problem is that the order of the 'baz' and 'qux' functions has been changed in the proposed changes unnecessarily. Instead, do this:
----
+- main.js:
+<PlandexBlock lang="javascript" path="main.js">
// ... existing code ...
func qux() {
@@ -805,7 +834,13 @@ func baz() {
}
// ... existing code ...
----
+</PlandexBlock>
+
+Now the order of the 'baz' and 'qux' functions is preserved exactly as it is in the original file.
+
+*
+
+When writing an "... existing code ..." comment, you MUST use the correct comment symbol for the programming language. For example, if you are writing a plan in Python, Ruby, or Bash, you MUST use '# ... existing code ...' instead of '// ... existing code ...'. If you're writing HTML, you MUST use '<!-- ... existing code ... -->'. If you're writing jsx, tsx, svelte, or another language where the correct comment symbol(s) depend on where in the code you are, use the appropriate comment symbol(s) for where that comment is placed in the file. If you're in a javascript block of a jsx file, use '// ... existing code ...'. If you're in a markup block of a jsx file, use '{/* ... existing code ... */}'.
Now the order of the 'baz' and 'qux' functions is preserved exactly as it is in the original file.
@@ -820,7 +855,8 @@ Here are some important examples of INCORRECT vs CORRECT file updates:
Example 1 - Adding a new route:
❌ INCORRECT - Replacing instead of inserting:
-<PlandexBlock lang="go">
+- src/main.go:
+<PlandexBlock lang="go" path="src/main.go">
// ... existing code ...
r.HandleFunc(prefix+"/api/users", handlers.ListUsersHandler).Methods("GET")
@@ -832,7 +868,8 @@ r.HandleFunc(prefix+"/api/config", handlers.GetConfigHandler).Methods("GET")
This is wrong because it doesn't show enough context to know what surrounding routes were preserved.
✅ CORRECT - Proper insertion with context:
-<PlandexBlock lang="go">
+- src/main.go:
+<PlandexBlock lang="go" path="src/main.go">
// ... existing code ...
r.HandleFunc(prefix+"/api/users", handlers.ListUsersHandler).Methods("GET")
@@ -849,7 +886,8 @@ r.HandleFunc(prefix+"/api/status", handlers.GetStatusHandler).Methods("GET")
Example 2 - Adding a method to a class:
❌ INCORRECT - Ambiguous insertion:
-<PlandexBlock lang="go">
+- src/main.go:
+<PlandexBlock lang="go" path="src/main.go">
class UserService {
// ... existing code ...
@@ -863,7 +901,8 @@ class UserService {
This is wrong because it doesn't show where exactly the new method should go.
✅ CORRECT - Clear insertion point:
-<PlandexBlock lang="go">
+- src/main.go:
+<PlandexBlock lang="go" path="src/main.go">
class UserService {
// ... existing code ...
@@ -886,7 +925,8 @@ class UserService {
Example 3 - Adding a configuration section:
❌ INCORRECT - Lost context:
-<PlandexBlock lang="json">
+- src/config.json:
+<PlandexBlock lang="json" path="src/config.json">
{
"database": {
"host": "localhost",
@@ -901,7 +941,8 @@ Example 3 - Adding a configuration section:
This is wrong because it dropped existing configuration sections.
✅ CORRECT - Preserved context:
-<PlandexBlock lang="json">
+- src/config.json:
+<PlandexBlock lang="json" path="src/config.json">
{
// ... existing code ...
diff --git a/app/server/model/prompts/user_prompt.go b/app/server/model/prompts/user_prompt.go
index 197205f2..b471bbaf 100644
--- a/app/server/model/prompts/user_prompt.go
+++ b/app/server/model/prompts/user_prompt.go
@@ -29,7 +29,7 @@ Do NOT include tests or documentation in the subtasks unless the user has specif
` + CombineSubtasksPrompt + `
-At the end of the '### Tasks' section, you ABSOLUTELY MUST ALWAYS include a <EndPlandexTasks/> tag, then end the response.
+At the end of the '### Tasks' section, you ABSOLUTELY MUST ALWAYS include a <PlandexFinish/> tag, then end the response.
Example:
@@ -39,7 +39,7 @@ Example:
2. Write a basic test for the 'main' function
-<EndPlandexTasks/>
+<PlandexFinish/>
IMPORTANT: During this planning phase, you must NOT implement any code or create any code blocks. Your only task is to break down the work into subtasks. Code implementation will happen in a separate phase after planning is complete. The planning phase is ONLY for breaking the work into subtasks.
@@ -54,7 +54,9 @@ You MUST NOT include any other text in a code block label apart from the initial
Always use an opening <PlandexBlock> tag to start a code block and a closing </PlandexBlock> tag to end a code block.
-The <PlandexBlock> tag MUST ONLY contain the code for the code block and NOTHING ELSE. Do NOT wrap the code block in triple backticks, CDATA tags, or any other text or formatting. Output ONLY the code and nothing else within the <PlandexBlock> tag.
+The <PlandexBlock> tag content MUST ONLY contain the code for the code block and NOTHING ELSE. Do NOT wrap the code block in triple backticks, CDATA tags, or any other text or formatting. Output ONLY the code and nothing else within the <PlandexBlock> tag.
+
+The <PlandexBlock> tag MUST include both a 'lang' attribute and a 'path' attribute as described in the instructions above. It must not include any other attributes.
You MUST follow the instructions you've been given on how to update code in code blocks:
@@ -170,4 +172,195 @@ const CombineSubtasksPrompt = `
- When using bullet points to break up a subtask into multiple steps, make a note of any files that will be created or updated by each step—surround file paths with backticks like this: "` + "`path/to/some_file.txt`" + `". All paths mentioned in the bullet points of the subtask must be included in the 'Uses: ' list for the subtask.
- Do NOT break up file operations of the same type (e.g. moving files, removing files, resetting pending changes) into multiple subtasks. Group them all into a *single* subtask.
+
+- Keep subtasks focused and manageable. While it's fine to group closely related changes (like small updates to a few tightly coupled files) into a single subtask, prefer breaking work into smaller, more focused subtasks when the changes are more substantial or independent. If a subtask involves many files or multiple distinct changes, consider whether it would be clearer and more maintainable to break it into multiple subtasks.
+
+Here are examples of good and poor task division:
+
+Example 1 - Poor (tasks too small and fragmented):
+1. Create the product.js file
+Uses: ` + "`src/models/product.js`" + `
+
+2. Add the product schema
+Uses: ` + "`src/models/product.js`" + `
+
+3. Add the validate() method
+Uses: ` + "`src/models/product.js`" + `
+
+4. Add the save() method
+Uses: ` + "`src/models/product.js`" + `
+
+Better:
+1. Create product model with core functionality
+- Create product.js with schema definition
+- Add validate() and save() methods
+Uses: ` + "`src/models/product.js`" + `
+
+Example 2 - Poor (task too large with unrelated changes):
+1. Implement user profile features
+- Add user avatar upload
+- Add profile settings page
+- Implement friend requests
+- Add user search
+- Create notification system
+Uses: ` + "`src/components/Profile.tsx`" + `, ` + "`src/components/Avatar.tsx`" + `, ` + "`src/components/Settings.tsx`" + `, ` + "`src/services/friends.ts`" + `, ` + "`src/services/search.ts`" + `, ` + "`src/services/notifications.ts`" + `
+
+Better:
+1. Implement user avatar upload functionality
+- Add avatar component with upload UI
+- Add avatar upload service
+Uses: ` + "`src/components/Avatar.tsx`" + `, ` + "`src/services/avatar.ts`" + `
+
+2. Create profile settings page
+- Add settings form components
+- Implement save/load settings
+Uses: ` + "`src/components/Settings.tsx`" + `, ` + "`src/services/settings.ts`" + `
+
+3. Add friend request system
+Uses: ` + "`src/services/friends.ts`" + `, ` + "`src/components/Profile.tsx`" + `
+
+Example 3 - Good (related changes properly grouped):
+1. Update error handling in authentication flow
+- Add error handling to login function
+- Add corresponding error states in auth context
+- Update error display in login form
+Uses: ` + "`src/auth/login.ts`" + `, ` + "`src/context/auth.tsx`" + `, ` + "`src/components/LoginForm.tsx`" + `
+
+Example 4 - Good (tightly coupled file updates):
+1. Rename UserType enum to AccountType
+- Update enum definition
+- Update all imports and usages
+Uses: ` + "`src/types/user.ts`" + `, ` + "`src/auth/account.ts`" + `, ` + "`src/components/UserProfile.tsx`" + `
+
+Notice in these examples:
+- Tasks that are too granular waste responses on tiny changes
+- Tasks that are too large mix unrelated changes and become hard to implement
+- Good tasks group related changes that make sense to implement together
+- Multiple files can be included when the changes are tightly coupled
+- Bullet points describe steps in a cohesive change, not separate features
`
+
+type ChatUserPromptParams struct {
+ CreatePromptParams
+ Prompt string
+ OsDetails string
+}
+
+func GetWrappedChatOnlyPrompt(params ChatUserPromptParams) string {
+ // Base wrapper that's always included
+ baseWrapper := "# The user's latest prompt:\n```\n%s\n```\n\n" + `Please respond according to the 'Your instructions' section above.
+
+The current UTC timestamp is: %s
+
+User's operating system details:
+%s`
+
+ // Build additional instructions based on parameter combinations
+ var additionalInstructions string
+
+ // Context handling - different for each autoContext + lastResponseLoadedContext combination
+ if params.AutoContext {
+ if params.LastResponseLoadedContext {
+ additionalInstructions += `
+
+Since you just loaded context in your previous response:
+- Focus on using that context in your explanation
+- Keep the conversation flowing naturally
+- You ABSOLUTELY MUST NOT load additional context unless the user asks about something completely different
+- Maintain conversational flow over seeking more context`
+ } else {
+ additionalInstructions += `
+
+When handling context:
+- Load context only when needed for accuracy
+- Make the context loading feel natural and conversational
+- If you need to check files, briefly mention what you're looking at
+- Once you've loaded context, use it thoroughly in your response`
+ }
+ } else {
+ additionalInstructions += `
+
+When discussing code:
+- Work with the context explicitly provided
+- If you need additional context, ask the user specifically what files would help
+- Make full use of any context you already have
+- Be clear when you need more information to provide a complete answer`
+ }
+
+ // Execution mode handling
+ if params.ExecMode {
+ additionalInstructions += `
+
+Regarding execution capabilities:
+- You can discuss both file changes and command execution
+- Be specific about what commands would need to be run
+- Consider build processes, testing, and deployment
+- Distinguish between file changes and execution steps`
+ } else {
+ additionalInstructions += `
+
+Remember about execution mode:
+- Focus on changes that can be made through file updates
+- Mention when something would require execution mode
+- You can discuss build/test/deploy conceptually
+- Be clear when certain steps would need execution mode enabled`
+ }
+
+ // Always include these key reminders
+ // Always include these key reminders
+ additionalInstructions += `
+Keep in mind:
+- Stay conversational while being technically precise
+- Reference and explain code when helpful, but don't output formal implementation blocks
+- Focus on what's specifically asked - don't suggest extra features
+- Consider existing codebase structure in your explanations
+- When discussing libraries, focus on well-maintained, widely-used options
+- If the user wants to implement changes, remind them about 'tell mode'
+- Use error handling, logging, and security best practices in your suggestions
+- Be thoughtful about code organization and structure
+- Consider implications of suggested changes on the existing codebase
+
+Remember you're in chat mode:
+- Engage in natural technical discussion about code and context
+- Help users understand their codebase and plan potential changes
+- Provide explanations and answer questions thoroughly
+- Include code snippets only when they help explain concepts
+- Help debug issues by examining and explaining code
+- Suggest approaches and discuss trade-offs
+- Help evaluate different implementation strategies
+- Consider and explain implications of different approaches
+- Stay focused on understanding and planning rather than implementation
+
+You cannot:
+- Create or modify any files
+- Output formal implementation code blocks
+- Make plans using "### Tasks" sections
+- Structure responses as if implementing changes
+- Load context multiple times in consecutive responses
+- Switch to implementation mode without user request
+
+Even if a plan is in progress:
+- Stay in discussion mode, don't attempt to implement anything
+- You can discuss the current tasks and progress
+- You can provide explanations and suggestions
+- You can help debug issues or clarify approach
+- But you must not output any implementation code
+- Return to implementation only when user switches back to tell mode
+
+Remember that users often:
+- Switch between chat and tell mode during implementation
+- Use chat mode to understand before implementing
+- Need detailed technical discussion to plan effectively
+- Want to explore options before committing to changes
+- May need to debug or understand issues mid-implementation
+- You may receive a list of tasks that are in progress, including a 'current subtask'. You MUST NOT implement any tasks—only discuss them.
+`
+
+ promptWrapperFormatStr := baseWrapper + additionalInstructions
+
+ ts := time.Now().Format(time.RFC3339)
+ return fmt.Sprintf(promptWrapperFormatStr,
+ params.Prompt,
+ ts,
+ params.OsDetails)
+}
diff --git a/app/server/model/summarize.go b/app/server/model/summarize.go
index 9ca09e6c..4ab760ed 100644
--- a/app/server/model/summarize.go
+++ b/app/server/model/summarize.go
@@ -52,7 +52,7 @@ func PlanSummary(clients map[string]ClientInfo, config shared.ModelRoleConfig, p
Plan: params.Plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
diff --git a/app/server/types/reply.go b/app/server/types/reply.go
index 98afc760..5aeeed81 100644
--- a/app/server/types/reply.go
+++ b/app/server/types/reply.go
@@ -3,6 +3,7 @@ package types
import (
"log"
"os"
+ "regexp"
"strconv"
"strings"
@@ -154,43 +155,51 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
prevFullLineTrimmed := strings.TrimSpace(prevFullLine)
- if r.maybeFilePath != "" && !r.isInMoveBlock && !r.isInRemoveBlock && !r.isInResetBlock {
- if verboseLogging {
- log.Println("Maybe file path is:", r.maybeFilePath) // Logging the maybeFilePath
+ setCurrentFile := func(path string, noLabel bool) {
+ r.currentFilePath = path
+ r.currentFileOperation = &shared.Operation{
+ Type: shared.OperationTypeFile,
+ Path: path,
}
- if strings.HasPrefix(prevFullLineTrimmed, "<PlandexBlock") {
+ r.maybeFilePath = ""
+ r.currentFileLines = []string{}
+
+ var fileDescription string
+ skipNumLines := 4
+ if noLabel {
+ skipNumLines = 2
+ }
+ if len(r.currentDescriptionLines) > skipNumLines {
+ fileDescription = strings.TrimSpace(strings.Join(r.currentDescriptionLines[0:len(r.currentDescriptionLines)-skipNumLines], "\n"))
if verboseLogging {
- log.Println("Found opening tag--confirming file path...") // Logging the confirmed file path
+ log.Println("File description:", fileDescription)
}
-
- r.currentFilePath = r.maybeFilePath
- r.currentFileOperation = &shared.Operation{
- Type: shared.OperationTypeFile,
- Path: r.maybeFilePath,
+ if fileDescription != "" {
+ r.currentFileOperation.Description = fileDescription
}
- r.maybeFilePath = ""
- r.currentFileLines = []string{}
+ } else {
+ r.currentFileOperation.Description = ""
+ }
- var fileDescription string
- if len(r.currentDescriptionLines) > 4 {
- fileDescription = strings.TrimSpace(strings.Join(r.currentDescriptionLines[0:len(r.currentDescriptionLines)-4], "\n"))
- if verboseLogging {
- log.Println("File description:", fileDescription)
- }
- if fileDescription != "" {
- r.currentFileOperation.Description = fileDescription
- }
- } else {
- r.currentFileOperation.Description = ""
- }
+ r.currentDescriptionLines = []string{""}
+ r.currentDescriptionLineIdx = 0
+
+ if verboseLogging {
+ log.Println("Confirmed file path:", r.currentFilePath) // Logging the confirmed file path
+ }
- r.currentDescriptionLines = []string{""}
- r.currentDescriptionLineIdx = 0
+ }
+ if r.maybeFilePath != "" && !r.isInMoveBlock && !r.isInRemoveBlock && !r.isInResetBlock {
+ if verboseLogging {
+ log.Println("Maybe file path is:", r.maybeFilePath) // Logging the maybeFilePath
+ }
+ if strings.HasPrefix(prevFullLineTrimmed, "<PlandexBlock") {
if verboseLogging {
- log.Println("Confirmed file path:", r.currentFilePath) // Logging the confirmed file path
+ log.Println("Found opening tag--confirming file path...") // Logging the confirmed file path
}
+ setCurrentFile(r.maybeFilePath, false)
return
} else if prevFullLineTrimmed != "" {
// turns out previous maybeFilePath was not a file path since there's a non-empty line before finding opening ticks
@@ -208,6 +217,16 @@ func (r *ReplyParser) AddChunk(chunk string, addToTotal bool) {
log.Println("Current file path is empty--checking for possible file path...")
}
+ if LineHasXmlPath(prevFullLineTrimmed) {
+ if verboseLogging {
+ log.Println("Line has XML-style PlandexBlock tag")
+ }
+ path := extractFilePath(prevFullLineTrimmed)
+ if path != "" {
+ setCurrentFile(path, true)
+ }
+ }
+
var gotPath string
if LineMaybeHasFilePath(prevFullLineTrimmed) {
gotPath = extractFilePath(prevFullLineTrimmed)
@@ -367,6 +386,10 @@ func (r *ReplyParserRes) FileOperationBlockOpen() bool {
return r.IsInMoveBlock || r.IsInRemoveBlock || r.IsInResetBlock
}
+func LineHasXmlPath(line string) bool {
+ return strings.HasPrefix(line, "<PlandexBlock") && strings.Contains(line, `path="`)
+}
+
func LineMaybeHasFilePath(line string) bool {
couldBe := (strings.HasPrefix(line, "-")) || strings.HasPrefix(line, "-file:") || strings.HasPrefix(line, "- file:") || (strings.HasPrefix(line, "**") && strings.HasSuffix(line, "**")) || (strings.HasPrefix(line, "#") && strings.HasSuffix(line, ":"))
@@ -384,7 +407,18 @@ func LineMaybeHasFilePath(line string) bool {
return couldBe
}
+var re = regexp.MustCompile(`path="([^"]+)"`)
+
func extractFilePath(line string) string {
+ // Handle XML-style PlandexBlock tag
+ if strings.HasPrefix(line, "<PlandexBlock") {
+ match := re.FindStringSubmatch(line)
+ if len(match) > 1 {
+ return match[1]
+ }
+ return ""
+ }
+
p := strings.ReplaceAll(line, "**", "")
p = strings.ReplaceAll(p, "`", "")
p = strings.ReplaceAll(p, "'", "")
diff --git a/app/server/types/reply_test.go b/app/server/types/reply_test.go
index 02fc7a28..f0532878 100644
--- a/app/server/types/reply_test.go
+++ b/app/server/types/reply_test.go
@@ -128,6 +128,7 @@ var examples = []TestExample{
},
},
{
+ Only: true,
Operations: []shared.Operation{
{
Type: shared.OperationTypeFile,
diff --git a/app/server/types/reply_test_examples/1.md b/app/server/types/reply_test_examples/1.md
index 61ea06e3..cd1e1e78 100644
--- a/app/server/types/reply_test_examples/1.md
+++ b/app/server/types/reply_test_examples/1.md
@@ -9,7 +9,7 @@ The code changes are as follows:
- file: cmd/apply.go
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/apply.go">
package cmd
// ... other imports ...
@@ -55,7 +55,7 @@ func apply(cmd *cobra.Command, args []string) error {
- file: cmd/checkout.go
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/checkout.go">
package cmd
// ... other imports ...
diff --git a/app/server/types/reply_test_examples/10.md b/app/server/types/reply_test_examples/10.md
index b18c1ccf..8e34ac8e 100644
--- a/app/server/types/reply_test_examples/10.md
+++ b/app/server/types/reply_test_examples/10.md
@@ -4,8 +4,7 @@ First, let's update the prompt in describe.go:
**Updating `server/model/prompts/describe.go`:** I'll replace the function definition and update the system prompt to use XML output instead of JSON.
-- server/model/prompts/describe.go:
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="server/model/prompts/describe.go">
package prompts
import (
@@ -49,8 +48,7 @@ Now let's update the commit message handling in commit_msg.go:
**Updating `server/model/plan/commit_msg.go`:** I'll update the genPlanDescription method to handle XML output instead of JSON.
-- server/model/plan/commit_msg.go:
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="server/model/plan/commit_msg.go">
// ... existing code ...
func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescription, error) {
@@ -76,7 +74,7 @@ func (state *activeTellStreamState) genPlanDescription() (*db.ConvoMessageDescri
Plan: plan,
WillSendModelRequestParams: &hooks.WillSendModelRequestParams{
InputTokens: numTokens,
- OutputTokens: shared.AvailableModelsByName[config.BaseModelConfig.ModelName].DefaultReservedOutputTokens,
+ OutputTokens: config.GetReservedOutputTokens(),
ModelName: config.BaseModelConfig.ModelName,
},
})
diff --git a/app/server/types/reply_test_examples/2.md b/app/server/types/reply_test_examples/2.md
index 3a3d7e97..c6bc7cf0 100644
--- a/app/server/types/reply_test_examples/2.md
+++ b/app/server/types/reply_test_examples/2.md
@@ -2,7 +2,7 @@ Sure, here's how you could structure your 'context rm' command and 'context upda
**cmd/context_rm.go**
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/context_rm.go">
package cmd
import (
@@ -55,7 +55,7 @@ func init() {
**cmd/context_update.go**
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/context_update.go">
package cmd
import (
diff --git a/app/server/types/reply_test_examples/3.md b/app/server/types/reply_test_examples/3.md
index 3a3d7e97..c6bc7cf0 100644
--- a/app/server/types/reply_test_examples/3.md
+++ b/app/server/types/reply_test_examples/3.md
@@ -2,7 +2,7 @@ Sure, here's how you could structure your 'context rm' command and 'context upda
**cmd/context_rm.go**
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/context_rm.go">
package cmd
import (
@@ -55,7 +55,7 @@ func init() {
**cmd/context_update.go**
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="cmd/context_update.go">
package cmd
import (
diff --git a/app/server/types/reply_test_examples/4.md b/app/server/types/reply_test_examples/4.md
index 15ea4ff6..2d3e6fdf 100644
--- a/app/server/types/reply_test_examples/4.md
+++ b/app/server/types/reply_test_examples/4.md
@@ -9,7 +9,7 @@ In `server/types/` directory, create a new `section.go` file.
- server/types/section.go
-<PlandexBlock lang="go">
+<PlandexBlock lang="go" path="types/section.go">
package types
type Section struct {
diff --git a/app/shared/ai_models.go b/app/shared/ai_models.go
deleted file mode 100644
index 46d20045..00000000
--- a/app/shared/ai_models.go
+++ /dev/null
@@ -1,430 +0,0 @@
-package shared
-
-import (
- "github.com/sashabaranov/go-openai"
-)
-
-const OpenAIEnvVar = "OPENAI_API_KEY"
-const OpenAIV1BaseUrl = "https://api.openai.com/v1"
-
-var fullCompatibility = ModelCompatibility{
- HasImageSupport: true,
-}
-
-var AvailableModels = []*AvailableModel{
- {
- Description: "OpenAI's latest gpt-4o model",
- DefaultMaxConvoTokens: 10000,
- DefaultReservedOutputTokens: 16384,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenAI,
- ModelName: openai.GPT4o,
- MaxTokens: 128000,
- ApiKeyEnvVar: OpenAIEnvVar,
- ModelCompatibility: fullCompatibility,
- BaseUrl: OpenAIV1BaseUrl,
- PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
- PredictedOutputEnabled: true,
- },
- },
- {
- Description: "OpenAI's latest gpt-4o-mini model",
- DefaultMaxConvoTokens: 10000,
- DefaultReservedOutputTokens: 16384,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenAI,
- ModelName: "gpt-4o-mini",
- MaxTokens: 128000,
- ApiKeyEnvVar: OpenAIEnvVar,
- ModelCompatibility: fullCompatibility,
- BaseUrl: OpenAIV1BaseUrl,
- PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
- PredictedOutputEnabled: true,
- },
- },
- {
- Description: "OpenAI's latest o1-mini model",
- DefaultMaxConvoTokens: 10000,
- DefaultReservedOutputTokens: 65536,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenAI,
- ModelName: "o1-mini",
- MaxTokens: 128000,
- ApiKeyEnvVar: OpenAIEnvVar,
- ModelCompatibility: fullCompatibility,
- BaseUrl: OpenAIV1BaseUrl,
- PreferredModelOutputFormat: ModelOutputFormatXml,
- SystemPromptDisabled: true,
- RoleParamsDisabled: true,
- },
- },
- {
- Description: "OpenAI's latest o1 model",
- DefaultMaxConvoTokens: 15000,
- DefaultReservedOutputTokens: 100000,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenAI,
- ModelName: "o1",
- MaxTokens: 200000,
- ApiKeyEnvVar: OpenAIEnvVar,
- ModelCompatibility: fullCompatibility,
- BaseUrl: OpenAIV1BaseUrl,
- PreferredModelOutputFormat: ModelOutputFormatXml,
- SystemPromptDisabled: true,
- RoleParamsDisabled: true,
- },
- },
- {
- Description: "Anthropic Claude 3.5 Sonnet via OpenRouter",
- DefaultMaxConvoTokens: 15000,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "anthropic/claude-3.5-sonnet",
- MaxTokens: 200000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: fullCompatibility,
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
- {
- Description: "Anthropic Claude 3.5 Haiku via OpenRouter",
- DefaultMaxConvoTokens: 15000,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "anthropic/claude-3.5-haiku",
- MaxTokens: 200000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: fullCompatibility,
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
- {
- Description: "Google Gemini Pro 1.5 via OpenRouter",
- DefaultMaxConvoTokens: 100000,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "google/gemini-pro-1.5",
- MaxTokens: 2000000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: fullCompatibility,
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
- {
- Description: "Google Gemini Flash 1.5 via OpenRouter",
- DefaultMaxConvoTokens: 75000,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "google/gemini-flash-1.5",
- MaxTokens: 1000000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: fullCompatibility,
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
- {
- Description: "Google Gemini Flash 2.0 Experimental via OpenRouter",
- DefaultMaxConvoTokens: 75000,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "google/gemini-2.0-flash-exp:free",
- MaxTokens: 1000000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: fullCompatibility,
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
-
- {
- Description: "DeepSeek V3 via OpenRouter",
- DefaultMaxConvoTokens: 7500,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "deepseek/deepseek-chat",
- MaxTokens: 64000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: ModelCompatibility{
- HasImageSupport: false,
- },
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
-
- {
- Description: "DeepSeek R1 via OpenRouter",
- DefaultMaxConvoTokens: 7500,
- DefaultReservedOutputTokens: 8192,
- BaseModelConfig: BaseModelConfig{
- Provider: ModelProviderOpenRouter,
- ModelName: "deepseek/deepseek-r1",
- MaxTokens: 64000,
- ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
- ModelCompatibility: ModelCompatibility{
- HasImageSupport: false,
- },
- BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
- PreferredModelOutputFormat: ModelOutputFormatXml,
- },
- },
-}
-
-var AvailableModelsByName = map[string]*AvailableModel{}
-
-var OpenRouterClaude3Dot5SonnetGPT4oModelPack ModelPack
-var OpenRouterClaude3Dot5SonnetModelPack ModelPack
-var Gpt4oLatestModelPack ModelPack
-var GoogleGeminiModelPack ModelPack
-
-var BuiltInModelPacks = []*ModelPack{
- &Gpt4oLatestModelPack,
- &OpenRouterClaude3Dot5SonnetModelPack,
- &OpenRouterClaude3Dot5SonnetGPT4oModelPack,
- &GoogleGeminiModelPack,
-}
-
-var DefaultModelPack *ModelPack = &OpenRouterClaude3Dot5SonnetGPT4oModelPack
-
-func getPlannerModelConfig(name string) PlannerModelConfig {
- return PlannerModelConfig{
- MaxConvoTokens: AvailableModelsByName[name].DefaultMaxConvoTokens,
- }
-}
-
-var DefaultConfigByRole = map[ModelRole]ModelRoleConfig{
- ModelRolePlanner: {
- Temperature: 0.3,
- TopP: 0.3,
- },
- ModelRoleCoder: {
- Temperature: 0.3,
- TopP: 0.3,
- },
- ModelRoleContextLoader: {
- Temperature: 0.3,
- TopP: 0.3,
- },
- ModelRolePlanSummary: {
- Temperature: 0.2,
- TopP: 0.2,
- },
- ModelRoleBuilder: {
- Temperature: 0.1,
- TopP: 0.1,
- },
- ModelRoleWholeFileBuilder: {
- Temperature: 0.1,
- TopP: 0.1,
- },
- ModelRoleName: {
- Temperature: 0.8,
- TopP: 0.5,
- },
- ModelRoleCommitMsg: {
- Temperature: 0.8,
- TopP: 0.5,
- },
- ModelRoleExecStatus: {
- Temperature: 0.1,
- TopP: 0.1,
- },
-}
-
-var RequiredCompatibilityByRole = map[ModelRole]ModelCompatibility{
- ModelRolePlanner: {},
- ModelRolePlanSummary: {},
- ModelRoleBuilder: {},
- ModelRoleName: {},
- ModelRoleCommitMsg: {},
- ModelRoleExecStatus: {},
-}
-
-func init() {
- for _, model := range AvailableModels {
- AvailableModelsByName[model.ModelName] = model
- }
-
- Gpt4oLatestModelPack = ModelPack{
- Name: "gpt-4o-latest",
- Description: "Uses OpenAI's latest gpt-4o model for heavy lifting, and latest version of gpt-4o-mini for lighter tasks.",
- Planner: PlannerRoleConfig{
- ModelRoleConfig: ModelRoleConfig{
- Role: ModelRolePlanner,
- BaseModelConfig: AvailableModelsByName[openai.GPT4o].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanner].TopP,
- },
- PlannerModelConfig: getPlannerModelConfig(openai.GPT4o),
- },
- PlanSummary: ModelRoleConfig{
- Role: ModelRolePlanSummary,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,
- },
- Builder: ModelRoleConfig{
- Role: ModelRoleBuilder,
- BaseModelConfig: AvailableModelsByName[openai.GPT4o].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,
- },
- Namer: ModelRoleConfig{
- Role: ModelRoleName,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleName].Temperature,
- TopP: DefaultConfigByRole[ModelRoleName].TopP,
- },
- CommitMsg: ModelRoleConfig{
- Role: ModelRoleCommitMsg,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleCommitMsg].Temperature,
- TopP: DefaultConfigByRole[ModelRoleCommitMsg].TopP,
- },
- ExecStatus: ModelRoleConfig{
- Role: ModelRoleExecStatus,
- BaseModelConfig: AvailableModelsByName[openai.GPT4o].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleExecStatus].Temperature,
- TopP: DefaultConfigByRole[ModelRoleExecStatus].TopP,
- },
- }
-
- OpenRouterClaude3Dot5SonnetGPT4oModelPack = ModelPack{
- Name: "plandex-default-pack",
- Description: "Uses Anthropic's Claude 3.5 Sonnet model (via OpenRouter) for planning, coding, and diff-based edits, OpenAI gpt-4o for whole-file edits, and gpt-4o-mini for lighter tasks.",
- Planner: PlannerRoleConfig{
- ModelRoleConfig: ModelRoleConfig{
- Role: ModelRolePlanner,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanner].TopP,
- },
- PlannerModelConfig: getPlannerModelConfig("anthropic/claude-3.5-sonnet"),
- },
- // Planner: PlannerRoleConfig{
- // ModelRoleConfig: ModelRoleConfig{
- // Role: ModelRolePlanner,
- // BaseModelConfig: AvailableModelsByName["google/gemini-pro-1.5"].BaseModelConfig,
- // Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,
- // TopP: DefaultConfigByRole[ModelRolePlanner].TopP,
- // },
- // PlannerModelConfig: getPlannerModelConfig("google/gemini-pro-1.5"),
- // },
- Coder: &ModelRoleConfig{
- Role: ModelRoleCoder,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleCoder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleCoder].TopP,
- },
- ContextLoader: &ModelRoleConfig{
- Role: ModelRoleContextLoader,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleContextLoader].Temperature,
- TopP: DefaultConfigByRole[ModelRoleContextLoader].TopP,
- },
- PlanSummary: ModelRoleConfig{
- Role: ModelRolePlanSummary,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,
- },
- Builder: ModelRoleConfig{
- Role: ModelRoleBuilder,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,
- },
- WholeFileBuilder: &ModelRoleConfig{
- Role: ModelRoleWholeFileBuilder,
- BaseModelConfig: AvailableModelsByName[openai.GPT4o].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleWholeFileBuilder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleWholeFileBuilder].TopP,
- },
- Namer: ModelRoleConfig{
- Role: ModelRoleName,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleName].Temperature,
- TopP: DefaultConfigByRole[ModelRoleName].TopP,
- },
- CommitMsg: ModelRoleConfig{
- Role: ModelRoleCommitMsg,
- BaseModelConfig: AvailableModelsByName["gpt-4o-mini"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleCommitMsg].Temperature,
- TopP: DefaultConfigByRole[ModelRoleCommitMsg].TopP,
- },
- ExecStatus: ModelRoleConfig{
- Role: ModelRoleExecStatus,
- BaseModelConfig: AvailableModelsByName[openai.GPT4o].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleExecStatus].Temperature,
- TopP: DefaultConfigByRole[ModelRoleExecStatus].TopP,
- },
- }
-
- OpenRouterClaude3Dot5SonnetModelPack = ModelPack{
- Name: "anthropic-claude-3.5-sonnet",
- Description: "Uses Anthropic's Claude 3.5 Sonnet model (via OpenRouter) for planning, builds, and auto-continue, and summarization, and Claude 3 Haiku for lighter tasks.",
- Planner: PlannerRoleConfig{
- ModelRoleConfig: ModelRoleConfig{
- Role: ModelRolePlanner,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanner].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanner].TopP,
- },
- PlannerModelConfig: getPlannerModelConfig("anthropic/claude-3.5-sonnet"),
- },
- PlanSummary: ModelRoleConfig{
- Role: ModelRolePlanSummary,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-haiku"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRolePlanSummary].Temperature,
- TopP: DefaultConfigByRole[ModelRolePlanSummary].TopP,
- },
- Builder: ModelRoleConfig{
- Role: ModelRoleBuilder,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,
- },
- Namer: ModelRoleConfig{
- Role: ModelRoleName,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-haiku"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleName].Temperature,
- TopP: DefaultConfigByRole[ModelRoleName].TopP,
- },
- CommitMsg: ModelRoleConfig{
- Role: ModelRoleCommitMsg,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-haiku"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleName].Temperature,
- TopP: DefaultConfigByRole[ModelRoleName].TopP,
- },
- ExecStatus: ModelRoleConfig{
- Role: ModelRoleExecStatus,
- BaseModelConfig: AvailableModelsByName["anthropic/claude-3.5-sonnet"].BaseModelConfig,
- Temperature: DefaultConfigByRole[ModelRoleBuilder].Temperature,
- TopP: DefaultConfigByRole[ModelRoleBuilder].TopP,
- },
- }
-
-}
-
-func FilterCompatibleModels(models []*AvailableModel, role ModelRole) []*AvailableModel {
- // required := RequiredCompatibilityByRole[role]
- var compatibleModels []*AvailableModel
-
- for _, model := range models {
- // no compatibility checks are needed in v2, but keeping this here in case compatibility checks are needed in the future
-
- compatibleModels = append(compatibleModels, model)
- }
-
- return compatibleModels
-}
diff --git a/app/shared/ai_models_available.go b/app/shared/ai_models_available.go
new file mode 100644
index 00000000..dcf78896
--- /dev/null
+++ b/app/shared/ai_models_available.go
@@ -0,0 +1,367 @@
+package shared
+
+import (
+ "github.com/sashabaranov/go-openai"
+)
+
+var AvailableModels = []*AvailableModel{
+ // Direct OpenAI models
+ {
+ Description: "OpenAI gpt-4o",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 16384,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenAI,
+ ModelName: openai.GPT4o,
+ MaxTokens: 128000,
+ ApiKeyEnvVar: OpenAIEnvVar,
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: OpenAIV1BaseUrl,
+ PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
+ PredictedOutputEnabled: true,
+ },
+ },
+ {
+ Description: "OpenAI gpt-4o-mini",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 16384,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenAI,
+ ModelName: "gpt-4o-mini",
+ MaxTokens: 128000,
+ ApiKeyEnvVar: OpenAIEnvVar,
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: OpenAIV1BaseUrl,
+ PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
+ PredictedOutputEnabled: true,
+ },
+ },
+ {
+ Description: "OpenAI o1-mini",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 65536,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenAI,
+ ModelName: "o1-mini",
+ MaxTokens: 128000,
+ ApiKeyEnvVar: OpenAIEnvVar,
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: OpenAIV1BaseUrl,
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ SystemPromptDisabled: true,
+ RoleParamsDisabled: true,
+ },
+ },
+ {
+ Description: "OpenAI o3-mini",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 100000,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenAI,
+ ModelName: "o3-mini",
+ MaxTokens: 200000,
+ ApiKeyEnvVar: OpenAIEnvVar,
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: OpenAIV1BaseUrl,
+ PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
+ RoleParamsDisabled: true,
+ },
+ },
+ {
+ Description: "OpenAI o1",
+ DefaultMaxConvoTokens: 15000,
+ DefaultReservedOutputTokens: 100000,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenAI,
+ ModelName: "o1",
+ MaxTokens: 200000,
+ ApiKeyEnvVar: OpenAIEnvVar,
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: OpenAIV1BaseUrl,
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ SystemPromptDisabled: true,
+ RoleParamsDisabled: true,
+ },
+ },
+
+ // OpenRouter models
+ {
+ Description: "Anthropic Claude 3.5 Sonnet via OpenRouter",
+ DefaultMaxConvoTokens: 15000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "anthropic/claude-3.5-sonnet",
+ MaxTokens: 200000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyAnthropic],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyAnthropic],
+ },
+ },
+ {
+ Description: "Anthropic Claude 3.5 Haiku via OpenRouter",
+ DefaultMaxConvoTokens: 15000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "anthropic/claude-3.5-haiku",
+ MaxTokens: 200000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyAnthropic],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyAnthropic],
+ },
+ },
+ {
+ Description: "Google Gemini Pro 1.5 via OpenRouter",
+ DefaultMaxConvoTokens: 100000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "google/gemini-pro-1.5",
+ MaxTokens: 2000000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyGoogle],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyGoogle],
+ },
+ },
+ {
+ Description: "Google Gemini Flash 1.5 via OpenRouter",
+ DefaultMaxConvoTokens: 75000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "google/gemini-flash-1.5",
+ MaxTokens: 1000000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyGoogle],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyGoogle],
+ },
+ },
+ {
+ Description: "Google Gemini Flash 2.0 Experimental via OpenRouter",
+ DefaultMaxConvoTokens: 75000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "google/gemini-2.0-flash-exp:free",
+ MaxTokens: 1000000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyGoogle],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyGoogle],
+ },
+ },
+
+ {
+ Description: "DeepSeek V3 via OpenRouter",
+ DefaultMaxConvoTokens: 7500,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "deepseek/deepseek-chat",
+ MaxTokens: 64000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: ModelCompatibility{
+ HasImageSupport: false,
+ },
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyDeepSeek],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyDeepSeek],
+ },
+ },
+
+ {
+ Description: "DeepSeek R1 via OpenRouter",
+ DefaultMaxConvoTokens: 7500,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "deepseek/deepseek-r1",
+ MaxTokens: 64000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: ModelCompatibility{
+ HasImageSupport: false,
+ },
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyDeepSeek],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyDeepSeek],
+ },
+ },
+
+ {
+ Description: "DeepSeek R1 Distill Llama 70B via OpenRouter/DeepInfra",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 131072,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "deepseek/deepseek-r1-distill-llama-70b",
+ MaxTokens: 131072,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: ModelCompatibility{
+ HasImageSupport: false,
+ },
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: []OpenRouterProvider{OpenRouterProviderDeepInfra},
+ OpenRouterAllowFallbacks: false,
+ },
+ },
+
+ {
+ Description: "DeepSeek R1 Distill Qwen 32B via OpenRouter/Fireworks",
+ DefaultMaxConvoTokens: 12500,
+ DefaultReservedOutputTokens: 81920,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "deepseek/deepseek-r1-distill-qwen-32b",
+ MaxTokens: 163840,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: ModelCompatibility{
+ HasImageSupport: false,
+ },
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: []OpenRouterProvider{OpenRouterProviderFireworks},
+ OpenRouterAllowFallbacks: false,
+ },
+ },
+
+ {
+ Description: "Qwen 2.5 Coder 32B via OpenRouter/Hyperbolic",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 8192,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "qwen/qwen-2.5-coder-32b-instruct",
+ MaxTokens: 128000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatXml,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyQwen],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyQwen],
+ },
+ },
+
+ // OpenAI models via OpenRouter
+ {
+ Description: "OpenAI gpt-4o via OpenRouter",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 16384,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "openai/gpt-4o",
+ MaxTokens: 128000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
+ PredictedOutputEnabled: true,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyOpenAI],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyOpenAI],
+ },
+ },
+ {
+ Description: "OpenAI gpt-4o-mini via OpenRouter",
+ DefaultMaxConvoTokens: 10000,
+ DefaultReservedOutputTokens: 16384,
+ BaseModelConfig: BaseModelConfig{
+ Provider: ModelProviderOpenRouter,
+ ModelName: "openai/gpt-4o-mini",
+ MaxTokens: 128000,
+ ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ ModelCompatibility: fullCompatibility,
+ BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ PreferredModelOutputFormat: ModelOutputFormatToolCallJson,
+ PredictedOutputEnabled: true,
+ PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyOpenAI],
+ OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyOpenAI],
+ },
+ },
+
+ // These only have OpenAI as a provider for now and o1/o3-mini both require adding OpenAI API keys in OpenRouter; better to use the direct OpenAI models until this changes
+ // {
+ // Description: "OpenAI o1-mini via OpenRouter",
+ // DefaultMaxConvoTokens: 10000,
+ // DefaultReservedOutputTokens: 65536,
+ // BaseModelConfig: BaseModelConfig{
+ // Provider: ModelProviderOpenRouter,
+ // ModelName: "openai/o1-mini",
+ // MaxTokens: 128000,
+ // ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ // ModelCompatibility: fullCompatibility,
+ // BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ // PreferredModelOutputFormat: ModelOutputFormatXml,
+ // SystemPromptDisabled: true,
+ // RoleParamsDisabled: true,
+ // PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyOpenAI],
+ // OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyOpenAI],
+ // },
+ // },
+ // {
+ // Description: "OpenAI o3-mini via OpenRouter",
+ // DefaultMaxConvoTokens: 10000,
+ // DefaultReservedOutputTokens: 100000,
+ // BaseModelConfig: BaseModelConfig{
+ // Provider: ModelProviderOpenRouter,
+ // ModelName: "openai/o3-mini",
+ // MaxTokens: 200000,
+ // ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ // ModelCompatibility: fullCompatibility,
+ // BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ // PreferredModelOutputFormat: ModelOutputFormatXml,
+ // SystemPromptDisabled: true,
+ // RoleParamsDisabled: true,
+ // PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyOpenAI],
+ // OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyOpenAI],
+ // },
+ // },
+ // {
+ // Description: "OpenAI o1 via OpenRouter",
+ // DefaultMaxConvoTokens: 15000,
+ // DefaultReservedOutputTokens: 100000,
+ // BaseModelConfig: BaseModelConfig{
+ // Provider: ModelProviderOpenRouter,
+ // ModelName: "openai/o1",
+ // MaxTokens: 200000,
+ // ApiKeyEnvVar: ApiKeyByProvider[ModelProviderOpenRouter],
+ // ModelCompatibility: fullCompatibility,
+ // BaseUrl: BaseUrlByProvider[ModelProviderOpenRouter],
+ // PreferredModelOutputFormat: ModelOutputFormatXml,
+ // SystemPromptDisabled: true,
+ // RoleParamsDisabled: true,
+ // PreferredOpenRouterProviders: DefaultOpenRouterProvidersByFamily[OpenRouterFamilyOpenAI],
+ // OpenRouterAllowFallbacks: DefaultOpenRouterAllowFallbacksByFamily[OpenRouterFamilyOpenAI],
+ // },
+ // },
+}
+
+var AvailableModelsByComposite = map[string]*AvailableModel{}
+
+func init() {
+ for _, model := range AvailableModels {
+ compositeKey := string(model.Provider) + "/" + model.ModelName
+ AvailableModelsByComposite[compositeKey] = model
+ }
+}
+
+func GetAvailableModel(provider ModelProvider, modelName string) *AvailableModel {
+ compositeKey := string(provider) + "/" + modelName
+ return AvailableModelsByComposite[compositeKey]
+}
diff --git a/app/shared/ai_models_compatibility.go b/app/shared/ai_models_compatibility.go
new file mode 100644
index 00000000..32f43bdd
--- /dev/null
+++ b/app/shared/ai_models_compatibility.go
@@ -0,0 +1,27 @@
+package shared
+
+var fullCompatibility = ModelCompatibility{
+ HasImageSupport: true,
+}
+
+var RequiredCompatibilityByRole = map[ModelRole]ModelCompatibility{
+ ModelRolePlanner: {},
+ ModelRolePlanSummary: {},
+ ModelRoleBuilder: {},
+ ModelRoleName: {},
+ ModelRoleCommitMsg: {},
+ ModelRoleExecStatus: {},
+}
+
+func FilterCompatibleModels(models []*AvailableModel, role ModelRole) []*AvailableModel {
+ // required := RequiredCompatibilityByRole[role]
+ var compatibleModels []*AvailableModel
+
+ for _, model := range models {
+ // no compatibility checks are needed in v2, but keeping this here in case compatibility checks are needed in the future
+
+ compatibleModels = append(compatibleModels, model)
+ }
+
+ return compatibleModels
+}
diff --git a/app/shared/ai_models_config.go b/app/shared/ai_models_config.go
new file mode 100644
index 00000000..74b69da0
--- /dev/null
+++ b/app/shared/ai_models_config.go
@@ -0,0 +1,46 @@
+package shared
+
+func getPlannerModelConfig(provider ModelProvider, modelName string) PlannerModelConfig {
+ return PlannerModelConfig{
+ MaxConvoTokens: GetAvailableModel(provider, modelName).DefaultMaxConvoTokens,
+ }
+}
+
+var DefaultConfigByRole = map[ModelRole]ModelRoleConfig{
+ ModelRolePlanner: {
+ Temperature: 0.3,
+ TopP: 0.3,
+ },
+ ModelRoleCoder: {
+ Temperature: 0.3,
+ TopP: 0.3,
+ },
+ ModelRoleContextLoader: {
+ Temperature: 0.3,
+ TopP: 0.3,
+ },
+ ModelRolePlanSummary: {
+ Temperature: 0.2,
+ TopP: 0.2,
+ },
+ ModelRoleBuilder: {
+ Temperature: 0.1,
+ TopP: 0.1,
+ },
+ ModelRoleWholeFileBuilder: {
+ Temperature: 0.1,
+ TopP: 0.1,
+ },
+ ModelRoleName: {
+ Temperature: 0.8,
+ TopP: 0.5,
+ },
+ ModelRoleCommitMsg: {
+ Temperature: 0.8,
+ TopP: 0.5,
+ },
+ ModelRoleExecStatus: {
+ Temperature: 0.1,
+ TopP: 0.1,
+ },
+}
diff --git a/app/shared/ai_models_data_models.go b/app/shared/ai_models_data_models.go
new file mode 100644
index 00000000..e64fcd1a
--- /dev/null
+++ b/app/shared/ai_models_data_models.go
@@ -0,0 +1,259 @@
+package shared
+
+import (
+ "database/sql/driver"
+ "encoding/json"
+ "fmt"
+ "time"
+)
+
+type ModelCompatibility struct {
+ HasImageSupport bool `json:"hasImageSupport"`
+}
+
+type ModelOutputFormat string
+
+const (
+ ModelOutputFormatToolCallJson ModelOutputFormat = "tool-call-json"
+ ModelOutputFormatXml ModelOutputFormat = "xml"
+)
+
+type BaseModelConfig struct {
+ Provider ModelProvider `json:"provider"`
+ CustomProvider *string `json:"customProvider,omitempty"`
+ BaseUrl string `json:"baseUrl"`
+ ModelName string `json:"modelName"`
+ MaxTokens int `json:"maxTokens"`
+ ApiKeyEnvVar string `json:"apiKeyEnvVar"`
+ PreferredModelOutputFormat ModelOutputFormat `json:"preferredModelOutputFormat"`
+ PreferredOpenRouterProviders []OpenRouterProvider `json:"preferredOpenRouterProviders"`
+ OpenRouterAllowFallbacks bool `json:"openRouterAllowFallbacks"`
+ SystemPromptDisabled bool `json:"systemPromptDisabled"`
+ RoleParamsDisabled bool `json:"roleParamsDisabled"`
+ PredictedOutputEnabled bool `json:"predictedOutputEnabled"`
+
+ ModelCompatibility
+}
+
+type AvailableModel struct {
+ Id string `json:"id"`
+ BaseModelConfig
+ Description string `json:"description"`
+ DefaultMaxConvoTokens int `json:"defaultMaxConvoTokens"`
+ DefaultReservedOutputTokens int `json:"defaultReservedOutputTokens"`
+ CreatedAt time.Time `json:"createdAt"`
+ UpdatedAt time.Time `json:"updatedAt"`
+}
+
+type PlannerModelConfig struct {
+ MaxConvoTokens int `json:"maxConvoTokens"`
+}
+
+type ModelRoleConfig struct {
+ Role ModelRole `json:"role"`
+ BaseModelConfig BaseModelConfig `json:"baseModelConfig"`
+ Temperature float32 `json:"temperature"`
+ TopP float32 `json:"topP"`
+ ReservedOutputTokens int `json:"reservedOutputTokens"`
+
+ LargeContextFallback *ModelRoleConfig `json:"largeContextFallback"`
+ // ErrorFallback *ModelRoleConfig `json:"errorFallback"`
+}
+
+func (m ModelRoleConfig) GetReservedOutputTokens() int {
+ if m.ReservedOutputTokens > 0 {
+ return m.ReservedOutputTokens
+ }
+ return GetAvailableModel(m.BaseModelConfig.Provider, m.BaseModelConfig.ModelName).DefaultReservedOutputTokens
+}
+
+func (m *ModelRoleConfig) Scan(src interface{}) error {
+ if src == nil {
+ return nil
+ }
+ switch s := src.(type) {
+ case []byte:
+ return json.Unmarshal(s, m)
+ case string:
+ return json.Unmarshal([]byte(s), m)
+ default:
+ return fmt.Errorf("unsupported data type: %T", src)
+ }
+}
+
+func (m ModelRoleConfig) Value() (driver.Value, error) {
+ return json.Marshal(m)
+}
+
+const maxFallbackDepth = 10 // max fallback depth for large context fallback - should never be reached in real scenarios, but protects against infinite loops in case of circular references etc.
+
+func (m ModelRoleConfig) GetFinalLargeContextFallback() ModelRoleConfig {
+ var currentConfig ModelRoleConfig = m
+ var n int = 0
+
+ for {
+ if currentConfig.LargeContextFallback == nil {
+ return currentConfig
+ } else {
+ currentConfig = *currentConfig.LargeContextFallback
+ }
+ n++
+ if n > maxFallbackDepth {
+ break
+ }
+ }
+
+ return currentConfig
+}
+
+// note that if the token number exeeds all the fallback models, it will return the last fallback model
+func (m ModelRoleConfig) GetRoleForTokens(tokens int) ModelRoleConfig {
+ var currentConfig ModelRoleConfig = m
+ var n int = 0
+ for {
+ if currentConfig.BaseModelConfig.MaxTokens >= tokens {
+ return currentConfig
+ }
+
+ if currentConfig.LargeContextFallback == nil {
+ return currentConfig
+ } else {
+ currentConfig = *currentConfig.LargeContextFallback
+ }
+ n++
+ if n > maxFallbackDepth {
+ break
+ }
+ }
+ return currentConfig
+}
+
+type PlannerRoleConfig struct {
+ ModelRoleConfig
+ PlannerModelConfig
+ PlannerLargeContextFallback *PlannerRoleConfig `json:"plannerLargeContextFallback"`
+ // PlannerErrorFallback *PlannerRoleConfig `json:"plannerErrorFallback"`
+}
+
+func (p PlannerRoleConfig) GetFinalLargeContextFallback() PlannerRoleConfig {
+ var currentConfig PlannerRoleConfig = p
+ var n int = 0
+ for {
+ if currentConfig.PlannerLargeContextFallback == nil {
+ return currentConfig
+ } else {
+ currentConfig = *currentConfig.PlannerLargeContextFallback
+ }
+ n++
+ if n > maxFallbackDepth {
+ break
+ }
+ }
+
+ return currentConfig
+}
+
+func (p PlannerRoleConfig) GetRoleForTokens(tokens int) PlannerRoleConfig {
+ var currentConfig PlannerRoleConfig = p
+ var n int = 0
+ for {
+ if currentConfig.BaseModelConfig.MaxTokens >= tokens {
+ return currentConfig
+ }
+
+ if currentConfig.PlannerLargeContextFallback == nil {
+ return currentConfig
+ } else {
+ currentConfig = *currentConfig.PlannerLargeContextFallback
+ }
+ n++
+ if n > maxFallbackDepth {
+ break
+ }
+ }
+ return currentConfig
+}
+
+func (p *PlannerRoleConfig) Scan(src interface{}) error {
+ if src == nil {
+ return nil
+ }
+ switch s := src.(type) {
+ case []byte:
+ return json.Unmarshal(s, p)
+ case string:
+ return json.Unmarshal([]byte(s), p)
+ default:
+ return fmt.Errorf("unsupported data type: %T", src)
+ }
+}
+
+func (p PlannerRoleConfig) Value() (driver.Value, error) {
+ return json.Marshal(p)
+}
+
+type ModelPack struct {
+ Id string `json:"id"`
+ Name string `json:"name"`
+ Description string `json:"description"`
+ Planner PlannerRoleConfig `json:"planner"`
+ Coder *ModelRoleConfig `json:"coder"`
+ PlanSummary ModelRoleConfig `json:"planSummary"`
+ Builder ModelRoleConfig `json:"builder"`
+ WholeFileBuilder *ModelRoleConfig `json:"wholeFileBuilder"` // optional, defaults to builder model — access via GetWholeFileBuilder()
+ Namer ModelRoleConfig `json:"namer"`
+ CommitMsg ModelRoleConfig `json:"commitMsg"`
+ ExecStatus ModelRoleConfig `json:"execStatus"`
+ ContextLoader *ModelRoleConfig `json:"contextLoader"`
+}
+
+func (m *ModelPack) GetCoder() ModelRoleConfig {
+ if m.Coder == nil {
+ return m.Planner.ModelRoleConfig
+ }
+ return *m.Coder
+}
+
+func (m *ModelPack) GetWholeFileBuilder() ModelRoleConfig {
+ if m.WholeFileBuilder == nil {
+ return m.Builder
+ }
+ return *m.WholeFileBuilder
+}
+
+func (m *ModelPack) GetContextLoader() ModelRoleConfig {
+ if m.ContextLoader == nil {
+ return m.Planner.ModelRoleConfig
+ }
+ return *m.ContextLoader
+}
+
+type ModelOverrides struct {
+ MaxConvoTokens *int `json:"maxConvoTokens"`
+ MaxTokens *int `json:"maxContextTokens"`
+ ReservedOutputTokens *int `json:"maxOutputTokens"`
+}
+
+type PlanSettings struct {
+ ModelOverrides ModelOverrides `json:"modelOverrides"`
+ ModelPack *ModelPack `json:"modelPack"`
+ UpdatedAt time.Time `json:"updatedAt"`
+}
+
+func (p *PlanSettings) Scan(src interface{}) error {
+ if src == nil {
+ return nil
+ }
+ switch s := src.(type) {
+ case []byte:
+ return json.Unmarshal(s, p)
+ case string:
+ return json.Unmarshal([]byte(s), p)
+ default:
+ return fmt.Errorf("unsupported data type: %T", src)
+ }
+}
+
+func (p PlanSettings) Value() (driver.Value, error) {
+ return json.Marshal(p)
+}
diff --git a/app/shared/ai_models_openrouter.go b/app/shared/ai_models_openrouter.go
new file mode 100644
index 00000000..8925b49d
--- /dev/null
+++ b/app/shared/ai_models_openrouter.go
@@ -0,0 +1,38 @@
+package shared
+
+type OpenRouterFamily string
+
+const (
+ OpenRouterFamilyAnthropic OpenRouterFamily = "anthropic"
+ OpenRouterFamilyGoogle OpenRouterFamily = "google"
+ OpenRouterFamilyOpenAI OpenRouterFamily = "openai"
+ OpenRouterFamilyQwen OpenRouterFamily = "qwen"
+ OpenRouterFamilyDeepSeek OpenRouterFamily = "deepseek"
+)
+
+type OpenRouterProvider string
+
+const (
+ OpenRouterProviderAnthropic OpenRouterProvider = "Anthropic"
+ OpenRouterProviderGoogle OpenRouterProvider = "Google Vertex"
+ OpenRouterProviderOpenAI OpenRouterProvider = "OpenAI"
+ OpenRouterProviderDeepSeek OpenRouterProvider = "DeepSeek"
+ OpenRouterProviderQwen OpenRouterProvider = "Hyperbolic"
+ OpenRouterProviderDeepInfra OpenRouterProvider = "DeepInfra"
+ OpenRouterProviderFireworks OpenRouterProvider = "Fireworks"
+)
+
+var DefaultOpenRouterProvidersByFamily = map[OpenRouterFamily][]OpenRouterProvider{
+ OpenRouterFamilyAnthropic: {OpenRouterProviderAnthropic},
+ OpenRouterFamilyGoogle: {OpenRouterProviderGoogle},
+ OpenRouterFamilyOpenAI: {OpenRouterProviderOpenAI},
+ OpenRouterFamilyQwen: {OpenRouterProviderQwen},
+ OpenRouterFamilyDeepSeek: {OpenRouterProviderDeepSeek},
+}
+
+// open source models don't have fallbacks enabled for now because pricing and context limits aren't predictable across providers
+var DefaultOpenRouterAllowFallbacksByFamily = map[OpenRouterFamily]bool{
+ OpenRouterFamilyAnthropic: true,
+ OpenRouterFamilyGoogle: true,
+ OpenRouterFamilyOpenAI: true,
+}
diff --git a/app/shared/ai_models_packs.go b/app/shared/ai_models_packs.go
new file mode 100644
index 00000000..524de5b0
--- /dev/null
+++ b/app/shared/ai_models_packs.go
@@ -0,0 +1,133 @@
+package shared
+
+var PlandexDefaultModelPack ModelPack
+var ClaudeModelPack ModelPack
+var O3MiniModelPack ModelPack
+
+//TODO: built-in deepseek/qwen model pack(s)
+
+// So far Gemini isn't able to follow instructions reliably enough for the 'coder' role to use it for a full model pack
+
+var BuiltInModelPacks = []*ModelPack{
+ &PlandexDefaultModelPack,
+ &ClaudeModelPack,
+ &O3MiniModelPack,
+}
+
+var DefaultModelPack *ModelPack = &PlandexDefaultModelPack
+
+func init() {
+ PlandexDefaultModelPack = ModelPack{
+ Name: "plandex-default-pack",
+ Description: "A mix of models from Anthropic, OpenAI, and Google. Supports up to 2M context.",
+ Planner: PlannerRoleConfig{
+ ModelRoleConfig: *claude35sonnet(ModelRolePlanner, nil),
+ PlannerModelConfig: getPlannerModelConfig(ModelProviderOpenRouter, "anthropic/claude-3.5-sonnet"),
+ PlannerLargeContextFallback: &PlannerRoleConfig{
+ ModelRoleConfig: *gemini15pro(ModelRolePlanner, nil),
+ PlannerModelConfig: getPlannerModelConfig(ModelProviderOpenRouter, "google/gemini-pro-1.5"),
+ },
+ },
+ Coder: claude35sonnet(ModelRoleCoder, nil),
+ ContextLoader: claude35sonnet(ModelRoleContextLoader, &modelConfigFallbacks{
+ largeContextFallback: gemini15pro(ModelRoleContextLoader, nil),
+ }),
+ PlanSummary: *openaio3mini(ModelRolePlanSummary, nil),
+ Builder: *openaio3mini(ModelRoleBuilder, nil),
+ WholeFileBuilder: openaio3mini(ModelRoleWholeFileBuilder, nil),
+ Namer: *openai4omini(ModelRoleName, nil),
+ CommitMsg: *openai4omini(ModelRoleCommitMsg, nil),
+ ExecStatus: *openaio3mini(ModelRoleExecStatus, nil),
+ }
+
+ O3MiniModelPack = ModelPack{
+ Name: "o3-mini-pack",
+ Description: "OpenAI blend. Supports up to 200k context. Uses OpenAI's o3-mini model for heavy lifting, GPT-4o Mini for lighter tasks.",
+ Planner: PlannerRoleConfig{
+ ModelRoleConfig: *openaio3mini(ModelRolePlanner, nil),
+ PlannerModelConfig: getPlannerModelConfig(ModelProviderOpenAI, "o3-mini"),
+ },
+ PlanSummary: *openaio3mini(ModelRolePlanSummary, nil),
+ Builder: *openaio3mini(ModelRoleBuilder, nil),
+ WholeFileBuilder: openaio3mini(ModelRoleWholeFileBuilder, nil),
+ Namer: *openai4omini(ModelRoleName, nil),
+ CommitMsg: *openai4omini(ModelRoleCommitMsg, nil),
+ ExecStatus: *openai4o(ModelRoleExecStatus, nil),
+ }
+
+ ClaudeModelPack = ModelPack{
+ Name: "claude-3-5-sonnet-pack",
+ Description: "Anthropic blend. Supports up to 200k context. Uses Claude 3.5 Sonnet for heavy lifting, Claude 3 Haiku for lighter tasks.",
+ Planner: PlannerRoleConfig{
+ ModelRoleConfig: *claude35sonnet(ModelRolePlanner, nil),
+ PlannerModelConfig: getPlannerModelConfig(ModelProviderOpenRouter, "anthropic/claude-3.5-sonnet"),
+ },
+ PlanSummary: *claude35haiku(ModelRolePlanSummary, nil),
+ Builder: *claude35sonnet(ModelRoleBuilder, nil),
+ Namer: *claude35haiku(ModelRoleName, nil),
+ CommitMsg: *claude35haiku(ModelRoleCommitMsg, nil),
+ ExecStatus: *claude35sonnet(ModelRoleExecStatus, nil),
+ }
+}
+
+type modelConfigFallbacks struct {
+ largeContextFallback *ModelRoleConfig
+ errorFallback *ModelRoleConfig
+}
+
+func getModelConfig(role ModelRole, provider ModelProvider, modelName string, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ if fallbacks == nil {
+ fallbacks = &modelConfigFallbacks{}
+ }
+
+ return &ModelRoleConfig{
+ Role: role,
+ BaseModelConfig: GetAvailableModel(provider, modelName).BaseModelConfig,
+ Temperature: DefaultConfigByRole[role].Temperature,
+ TopP: DefaultConfigByRole[role].TopP,
+
+ LargeContextFallback: fallbacks.largeContextFallback,
+ // ErrorFallback: fallbacks.errorFallback,
+ }
+}
+
+// allow OpenAI calls to first try OpenAI directly, then fallback to OpenRouter (which can route to Azure if OpenAI is down)
+func getOpenAIModelConfig(role ModelRole, modelName string, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ var largeContextFallback *ModelRoleConfig
+ if fallbacks != nil {
+ largeContextFallback = fallbacks.largeContextFallback
+ }
+
+ return getModelConfig(role, ModelProviderOpenAI, modelName, &modelConfigFallbacks{
+ largeContextFallback: largeContextFallback,
+ errorFallback: getModelConfig(role, ModelProviderOpenRouter, "openai/gpt-4o", nil),
+ })
+}
+
+func claude35sonnet(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getModelConfig(role, ModelProviderOpenRouter, "anthropic/claude-3.5-sonnet", fallbacks)
+}
+
+func claude35haiku(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getModelConfig(role, ModelProviderOpenRouter, "anthropic/claude-3.5-haiku", fallbacks)
+}
+
+func gemini15pro(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getModelConfig(role, ModelProviderOpenRouter, "google/gemini-pro-1.5", fallbacks)
+}
+
+func openai4o(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getOpenAIModelConfig(role, "gpt-4o", fallbacks)
+}
+
+func openai4omini(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getOpenAIModelConfig(role, "gpt-4o-mini", fallbacks)
+}
+
+func openaio1mini(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getOpenAIModelConfig(role, "o1-mini", fallbacks)
+}
+
+func openaio3mini(role ModelRole, fallbacks *modelConfigFallbacks) *ModelRoleConfig {
+ return getOpenAIModelConfig(role, "o3-mini", fallbacks)
+}
diff --git a/app/shared/ai_models_providers.go b/app/shared/ai_models_providers.go
new file mode 100644
index 00000000..8102c6e2
--- /dev/null
+++ b/app/shared/ai_models_providers.go
@@ -0,0 +1,32 @@
+package shared
+
+const OpenAIEnvVar = "OPENAI_API_KEY"
+const OpenAIV1BaseUrl = "https://api.openai.com/v1"
+
+const OpenRouterApiKeyEnvVar = "OPENROUTER_API_KEY"
+const OpenRouterBaseUrl = "https://openrouter.ai/api/v1"
+
+type ModelProvider string
+
+const (
+ ModelProviderOpenRouter ModelProvider = "openrouter"
+ ModelProviderOpenAI ModelProvider = "openai"
+ ModelProviderCustom ModelProvider = "custom"
+)
+
+var AllModelProviders = []string{
+ string(ModelProviderOpenAI),
+ string(ModelProviderOpenRouter),
+ // string(ModelProviderTogether),
+ string(ModelProviderCustom),
+}
+
+var BaseUrlByProvider = map[ModelProvider]string{
+ ModelProviderOpenAI: OpenAIV1BaseUrl,
+ ModelProviderOpenRouter: OpenRouterBaseUrl,
+}
+
+var ApiKeyByProvider = map[ModelProvider]string{
+ ModelProviderOpenAI: OpenAIEnvVar,
+ ModelProviderOpenRouter: OpenRouterApiKeyEnvVar,
+}
diff --git a/app/shared/ai_models_roles.go b/app/shared/ai_models_roles.go
new file mode 100644
index 00000000..f8c4e967
--- /dev/null
+++ b/app/shared/ai_models_roles.go
@@ -0,0 +1,29 @@
+package shared
+
+type ModelRole string
+
+const (
+ ModelRolePlanner ModelRole = "planner"
+ ModelRoleCoder ModelRole = "coder"
+ ModelRoleContextLoader ModelRole = "context-loader"
+ ModelRolePlanSummary ModelRole = "summarizer"
+ ModelRoleBuilder ModelRole = "builder"
+ ModelRoleWholeFileBuilder ModelRole = "whole-file-builder"
+ ModelRoleName ModelRole = "names"
+ ModelRoleCommitMsg ModelRole = "commit-messages"
+ ModelRoleExecStatus ModelRole = "auto-continue"
+)
+
+var AllModelRoles = []ModelRole{ModelRolePlanner, ModelRoleCoder, ModelRoleContextLoader, ModelRolePlanSummary, ModelRoleBuilder, ModelRoleWholeFileBuilder, ModelRoleName, ModelRoleCommitMsg, ModelRoleExecStatus}
+
+var ModelRoleDescriptions = map[ModelRole]string{
+ ModelRolePlanner: "replies to prompts and makes plans",
+ ModelRoleCoder: "writes code to implement a plan",
+ ModelRolePlanSummary: "summarizes conversations exceeding max-convo-tokens",
+ ModelRoleBuilder: "builds a plan into file diffs",
+ ModelRoleWholeFileBuilder: "builds a plan into file diffs by writing the entire file",
+ ModelRoleName: "names plans",
+ ModelRoleCommitMsg: "writes commit messages",
+ ModelRoleExecStatus: "determines whether to auto-continue",
+ ModelRoleContextLoader: "decides what context to load using codebase map",
+}
diff --git a/app/shared/data_models.go b/app/shared/data_models.go
index 9fd7f4d6..611b844e 100644
--- a/app/shared/data_models.go
+++ b/app/shared/data_models.go
@@ -1,9 +1,6 @@
package shared
import (
- "database/sql/driver"
- "encoding/json"
- "fmt"
"time"
"github.com/sashabaranov/go-openai"
@@ -271,169 +268,6 @@ type OrgRole struct {
Description string `json:"description"`
}
-type ModelCompatibility struct {
- HasImageSupport bool `json:"hasImageSupport"`
-}
-
-type ModelOutputFormat string
-
-const (
- ModelOutputFormatToolCallJson ModelOutputFormat = "tool-call-json"
- ModelOutputFormatXml ModelOutputFormat = "xml"
-)
-
-type BaseModelConfig struct {
- Provider ModelProvider `json:"provider"`
- CustomProvider *string `json:"customProvider,omitempty"`
- BaseUrl string `json:"baseUrl"`
- ModelName string `json:"modelName"`
- MaxTokens int `json:"maxTokens"`
- ApiKeyEnvVar string `json:"apiKeyEnvVar"`
- PreferredModelOutputFormat ModelOutputFormat `json:"preferredModelOutputFormat"`
-
- SystemPromptDisabled bool `json:"systemPromptDisabled"`
- RoleParamsDisabled bool `json:"roleParamsDisabled"`
- PredictedOutputEnabled bool `json:"predictedOutputEnabled"`
-
- ModelCompatibility
-}
-
-type AvailableModel struct {
- Id string `json:"id"`
- BaseModelConfig
- Description string `json:"description"`
- DefaultMaxConvoTokens int `json:"defaultMaxConvoTokens"`
- DefaultReservedOutputTokens int `json:"defaultReservedOutputTokens"`
- CreatedAt time.Time `json:"createdAt"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-type PlannerModelConfig struct {
- MaxConvoTokens int `json:"maxConvoTokens"`
-}
-
-type ModelRoleConfig struct {
- Role ModelRole `json:"role"`
- BaseModelConfig BaseModelConfig `json:"baseModelConfig"`
- Temperature float32 `json:"temperature"`
- TopP float32 `json:"topP"`
- ReservedOutputTokens int `json:"reservedOutputTokens"`
-}
-
-func (m *ModelRoleConfig) GetReservedOutputTokens() int {
- if m.ReservedOutputTokens > 0 {
- return m.ReservedOutputTokens
- }
- return AvailableModelsByName[m.BaseModelConfig.ModelName].DefaultReservedOutputTokens
-}
-
-func (m *ModelRoleConfig) Scan(src interface{}) error {
- if src == nil {
- return nil
- }
- switch s := src.(type) {
- case []byte:
- return json.Unmarshal(s, m)
- case string:
- return json.Unmarshal([]byte(s), m)
- default:
- return fmt.Errorf("unsupported data type: %T", src)
- }
-}
-
-func (m ModelRoleConfig) Value() (driver.Value, error) {
- return json.Marshal(m)
-}
-
-type PlannerRoleConfig struct {
- ModelRoleConfig
- PlannerModelConfig
-}
-
-func (p *PlannerRoleConfig) Scan(src interface{}) error {
- if src == nil {
- return nil
- }
- switch s := src.(type) {
- case []byte:
- return json.Unmarshal(s, p)
- case string:
- return json.Unmarshal([]byte(s), p)
- default:
- return fmt.Errorf("unsupported data type: %T", src)
- }
-}
-
-func (p PlannerRoleConfig) Value() (driver.Value, error) {
- return json.Marshal(p)
-}
-
-type ModelPack struct {
- Id string `json:"id"`
- Name string `json:"name"`
- Description string `json:"description"`
- Planner PlannerRoleConfig `json:"planner"`
- Coder *ModelRoleConfig `json:"coder"`
- PlanSummary ModelRoleConfig `json:"planSummary"`
- Builder ModelRoleConfig `json:"builder"`
- WholeFileBuilder *ModelRoleConfig `json:"wholeFileBuilder"` // optional, defaults to builder model — access via GetWholeFileBuilder()
- Namer ModelRoleConfig `json:"namer"`
- CommitMsg ModelRoleConfig `json:"commitMsg"`
- ExecStatus ModelRoleConfig `json:"execStatus"`
- ContextLoader *ModelRoleConfig `json:"contextLoader"`
-}
-
-func (m *ModelPack) GetCoder() ModelRoleConfig {
- if m.Coder == nil {
- return m.Planner.ModelRoleConfig
- }
- return *m.Coder
-}
-
-func (m *ModelPack) GetWholeFileBuilder() ModelRoleConfig {
- if m.WholeFileBuilder == nil {
- return m.Builder
- }
- return *m.WholeFileBuilder
-}
-
-func (m *ModelPack) GetContextLoader() ModelRoleConfig {
- if m.ContextLoader == nil {
- return m.Planner.ModelRoleConfig
- }
- return *m.ContextLoader
-}
-
-type ModelOverrides struct {
- MaxConvoTokens *int `json:"maxConvoTokens"`
- MaxTokens *int `json:"maxContextTokens"`
- ReservedOutputTokens *int `json:"maxOutputTokens"`
-}
-
-type PlanSettings struct {
- ModelOverrides ModelOverrides `json:"modelOverrides"`
- ModelPack *ModelPack `json:"modelPack"`
- UpdatedAt time.Time `json:"updatedAt"`
-}
-
-func (p *PlanSettings) Scan(src interface{}) error {
- if src == nil {
- return nil
- }
- switch s := src.(type) {
- case []byte:
- return json.Unmarshal(s, p)
- case string:
- return json.Unmarshal([]byte(s), p)
- default:
- return fmt.Errorf("unsupported data type: %T", src)
- }
-}
-
-func (p PlanSettings) Value() (driver.Value, error) {
- return json.Marshal(p)
-}
-
type CloudBillingFields struct {
CreditsBalance decimal.Decimal `json:"creditsBalance"`
MonthlyGrant decimal.Decimal `json:"monthlyGrant"`
diff --git a/app/shared/go.mod b/app/shared/go.mod
index 6c34231c..51b4fe1a 100644
--- a/app/shared/go.mod
+++ b/app/shared/go.mod
@@ -4,12 +4,16 @@ go 1.23.3
require (
github.com/davecgh/go-spew v1.1.1
+ github.com/fatih/color v1.18.0
github.com/pkoukk/tiktoken-go v0.1.7
)
require (
+ github.com/mattn/go-colorable v0.1.13 // indirect
+ github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
+ golang.org/x/sys v0.25.0 // indirect
)
require (
diff --git a/app/shared/go.sum b/app/shared/go.sum
index 391df034..727a343a 100644
--- a/app/shared/go.sum
+++ b/app/shared/go.sum
@@ -2,8 +2,15 @@ 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/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
+github.com/fatih/color v1.18.0 h1:S8gINlzdQ840/4pfAwic/ZE0djQEH3wM94VfqLTZcOM=
+github.com/fatih/color v1.18.0/go.mod h1:4FelSpRwEGDpQ12mAdzqdOukCy4u8WUtOY6lkT/6HfU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
@@ -24,5 +31,9 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4=
+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
+golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/app/shared/plan_config.go b/app/shared/plan_config.go
index 8aaccd89..706f4487 100644
--- a/app/shared/plan_config.go
+++ b/app/shared/plan_config.go
@@ -4,6 +4,7 @@ import (
"database/sql/driver"
"encoding/json"
"fmt"
+ "strings"
)
const defaultAutoDebugTries = 5
@@ -15,14 +16,6 @@ const (
const defaultEditor = EditorTypeVim
-type ReplType string
-
-const (
- ReplTypeTell ReplType = "tell"
- ReplTypeChat ReplType = "chat"
- ReplTypeAuto ReplType = "auto"
-)
-
type AutoModeType string
const (
@@ -34,30 +27,13 @@ const (
AutoModeCustom AutoModeType = "custom"
)
-var AutoModeOptions = [][2]string{
- {string(AutoModeFull), "Full Auto"},
-
- // "→ automatically selects context, updates changes context, includes only necessary context for each step, continues iterating until plan is complete, builds plan into file edits, applies changes, executes commands, and debugs failed commands up to " + strconv.Itoa(defaultAutoDebugTries) + " attempts."},
-
- {string(AutoModeSemi), "Semi Auto"},
-
- // "→ automatically selects context, updates changed context, includes only necessary context for each step, continues iterating until plan is complete, and builds plan into pending file edits. User applies changes manually. Automatically commits after apply in git repos. Plans can include commands to execute, but user must approve execution and debugging of failed commands."},
-
- {string(AutoModeBasic), "Basic Plus"},
-
- // " → manual selection of context. Context with changes is updates automatically. Includes only necessary context for each step. Continues iterating until plan is complete, and builds plan into pending file edits. User applies changes manually. Automatically commits after apply in git repos. Plans can include commands to execute, but user must approve execution and debugging of failed commands."},
-
- {string(AutoModeBasic), "Basic"},
-
- // " → manual selection of context. Updating changed context must be approved. Each step includes all context (no smart context). Continues iterating until plan is complete, and builds plan into pending file edits. User applies changes manually. No automatic commit after apply in git repos. No execution of commands."},
-
- {string(AutoModeNone), "None"},
-
- // "→ manual selection of context. Updating changes context must be approved. Each step includes all context (no smart context). Plans only proceed one iteration at a time with no automatic continuation. Changes are not automatically built into pending file edits. User builds and applies changes manually. No automatic commit after apply in git repos. No execution of commands."},
-
- {string(AutoModeCustom), "Custom"},
-
- // " → mix and match config settings individually."},
+var AutoModeOptions = [][3]string{
+ {string(AutoModeFull), "Full Auto", "Fully automated: context, apply, execution and debugging"},
+ {string(AutoModeSemi), "Semi Auto", "Auto context, manual apply and execution"},
+ {string(AutoModeBasicPlus), "Basic Plus", "Manual context with auto updates and smart loading, manual apply and execution"},
+ {string(AutoModeBasic), "Basic", "Manual context, manual apply and execution"},
+ {string(AutoModeNone), "None", "Fully manual and step-by-step, one response at a time, manual builds"},
+ {string(AutoModeCustom), "Custom", "Choose your own combination of automation settings"},
}
var AutoModeLabels = map[AutoModeType]string{}
@@ -230,7 +206,7 @@ var ConfigSettingsByKey = map[string]ConfigSetting{
Choices: &AutoModeChoices,
ChoiceToKey: func(choice string) string {
for _, option := range AutoModeOptions {
- if option[1] == choice {
+ if strings.HasPrefix(choice, option[1]) {
return option[0]
}
}
@@ -467,8 +443,7 @@ func init() {
DefaultPlanConfig.SetAutoMode(AutoModeSemi)
for _, choice := range AutoModeOptions {
- AutoModeChoices = append(AutoModeChoices, choice[1])
+ AutoModeChoices = append(AutoModeChoices, fmt.Sprintf("%s → %s", choice[1], choice[2]))
AutoModeLabels[AutoModeType(choice[0])] = choice[1]
}
-
}
diff --git a/app/shared/plan_model_settings.go b/app/shared/plan_model_settings.go
index e4afbbfe..cd16da25 100644
--- a/app/shared/plan_model_settings.go
+++ b/app/shared/plan_model_settings.go
@@ -1,59 +1,5 @@
package shared
-type ModelProvider string
-
-const (
- ModelProviderOpenAI ModelProvider = "openai"
- // ModelProviderTogether ModelProvider = "together" // removing for now to simplify
- ModelProviderOpenRouter ModelProvider = "openrouter"
- ModelProviderCustom ModelProvider = "custom"
-)
-
-var AllModelProviders = []string{
- string(ModelProviderOpenAI),
- string(ModelProviderOpenRouter),
- // string(ModelProviderTogether),
- string(ModelProviderCustom),
-}
-
-var BaseUrlByProvider = map[ModelProvider]string{
- ModelProviderOpenAI: OpenAIV1BaseUrl,
- // ModelProviderTogether: "https://api.together.xyz/v1", // removing for now to simplify
- ModelProviderOpenRouter: "https://openrouter.ai/api/v1",
-}
-
-var ApiKeyByProvider = map[ModelProvider]string{
- ModelProviderOpenAI: OpenAIEnvVar,
- // ModelProviderTogether: "TOGETHER_API_KEY", // removing for now to simplify
- ModelProviderOpenRouter: "OPENROUTER_API_KEY",
-}
-
-type ModelRole string
-
-const (
- ModelRolePlanner ModelRole = "planner"
- ModelRoleCoder ModelRole = "coder"
- ModelRoleContextLoader ModelRole = "context-loader"
- ModelRolePlanSummary ModelRole = "summarizer"
- ModelRoleBuilder ModelRole = "builder"
- ModelRoleWholeFileBuilder ModelRole = "whole-file-builder"
- ModelRoleName ModelRole = "names"
- ModelRoleCommitMsg ModelRole = "commit-messages"
- ModelRoleExecStatus ModelRole = "auto-continue"
-)
-
-var AllModelRoles = []ModelRole{ModelRolePlanner, ModelRoleCoder, ModelRoleContextLoader, ModelRolePlanSummary, ModelRoleBuilder, ModelRoleWholeFileBuilder, ModelRoleName, ModelRoleCommitMsg, ModelRoleExecStatus}
-var ModelRoleDescriptions = map[ModelRole]string{
- ModelRolePlanner: "replies to prompts and makes plans",
- ModelRoleCoder: "writes code to implement a plan",
- ModelRolePlanSummary: "summarizes conversations exceeding max-convo-tokens",
- ModelRoleBuilder: "builds a plan into file diffs",
- ModelRoleWholeFileBuilder: "builds a plan into file diffs by writing the entire file",
- ModelRoleName: "names plans",
- ModelRoleCommitMsg: "writes commit messages",
- ModelRoleExecStatus: "determines whether to auto-continue",
- ModelRoleContextLoader: "decides what context to load using codebase map",
-}
var SettingDescriptions = map[string]string{
"max-convo-tokens": "max conversation 🪙 before summarization",
"max-tokens": "overall 🪙 limit",
@@ -65,9 +11,25 @@ var ModelOverridePropsDasherized = []string{"max-convo-tokens", "max-tokens", "r
func (ps PlanSettings) GetPlannerMaxTokens() int {
if ps.ModelOverrides.MaxTokens == nil {
if ps.ModelPack == nil {
- return DefaultModelPack.Planner.BaseModelConfig.MaxTokens
+ defaultPlanner := DefaultModelPack.Planner
+ return defaultPlanner.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ } else {
+ planner := ps.ModelPack.Planner
+ return planner.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ }
+ } else {
+ return *ps.ModelOverrides.MaxTokens
+ }
+}
+
+func (ps PlanSettings) GetPlannerMaxReservedOutputTokens() int {
+ if ps.ModelOverrides.MaxTokens == nil {
+ if ps.ModelPack == nil {
+ defaultPlanner := DefaultModelPack.Planner
+ return defaultPlanner.GetFinalLargeContextFallback().GetReservedOutputTokens()
} else {
- return ps.ModelPack.Planner.BaseModelConfig.MaxTokens
+ planner := ps.ModelPack.Planner
+ return planner.GetFinalLargeContextFallback().GetReservedOutputTokens()
}
} else {
return *ps.ModelOverrides.MaxTokens
@@ -77,9 +39,53 @@ func (ps PlanSettings) GetPlannerMaxTokens() int {
func (ps PlanSettings) GetContextLoaderMaxTokens() int {
if ps.ModelOverrides.MaxTokens == nil {
if ps.ModelPack == nil {
- return DefaultModelPack.GetContextLoader().BaseModelConfig.MaxTokens
+ defaultLoader := DefaultModelPack.GetContextLoader()
+ return defaultLoader.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ } else {
+ loader := ps.ModelPack.GetContextLoader()
+ return loader.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ }
+ } else {
+ return *ps.ModelOverrides.MaxTokens
+ }
+}
+
+func (ps PlanSettings) GetContextLoaderMaxReservedOutputTokens() int {
+ if ps.ModelOverrides.MaxTokens == nil {
+ if ps.ModelPack == nil {
+ defaultLoader := DefaultModelPack.GetContextLoader()
+ return defaultLoader.GetFinalLargeContextFallback().GetReservedOutputTokens()
+ } else {
+ loader := ps.ModelPack.GetContextLoader()
+ return loader.GetFinalLargeContextFallback().GetReservedOutputTokens()
+ }
+ } else {
+ return *ps.ModelOverrides.MaxTokens
+ }
+}
+
+func (ps PlanSettings) GetWholeFileBuilderMaxTokens() int {
+ if ps.ModelOverrides.MaxTokens == nil {
+ if ps.ModelPack == nil {
+ defaultBuilder := DefaultModelPack.WholeFileBuilder
+ return defaultBuilder.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ } else {
+ builder := ps.ModelPack.WholeFileBuilder
+ return builder.GetFinalLargeContextFallback().BaseModelConfig.MaxTokens
+ }
+ } else {
+ return *ps.ModelOverrides.MaxTokens
+ }
+}
+
+func (ps PlanSettings) GetWholeFileBuilderMaxReservedOutputTokens() int {
+ if ps.ModelOverrides.MaxTokens == nil {
+ if ps.ModelPack == nil {
+ defaultBuilder := DefaultModelPack.WholeFileBuilder
+ return defaultBuilder.GetFinalLargeContextFallback().GetReservedOutputTokens()
} else {
- return ps.ModelPack.GetContextLoader().BaseModelConfig.MaxTokens
+ builder := ps.ModelPack.WholeFileBuilder
+ return builder.GetFinalLargeContextFallback().GetReservedOutputTokens()
}
} else {
return *ps.ModelOverrides.MaxTokens
@@ -89,9 +95,11 @@ func (ps PlanSettings) GetContextLoaderMaxTokens() int {
func (ps PlanSettings) GetPlannerMaxConvoTokens() int {
if ps.ModelOverrides.MaxConvoTokens == nil {
if ps.ModelPack == nil {
- return DefaultModelPack.Planner.PlannerModelConfig.MaxConvoTokens
+ defaultPlanner := DefaultModelPack.Planner
+ return defaultPlanner.GetFinalLargeContextFallback().MaxConvoTokens
} else {
- return ps.ModelPack.Planner.PlannerModelConfig.MaxConvoTokens
+ planner := ps.ModelPack.Planner
+ return planner.GetFinalLargeContextFallback().MaxConvoTokens
}
} else {
return *ps.ModelOverrides.MaxConvoTokens
@@ -99,12 +107,24 @@ func (ps PlanSettings) GetPlannerMaxConvoTokens() int {
}
func (ps PlanSettings) GetPlannerEffectiveMaxTokens() int {
- return ps.GetPlannerMaxTokens() - ps.ModelPack.Planner.GetReservedOutputTokens()
+ maxPlannerTokens := ps.GetPlannerMaxTokens()
+ maxReservedOutputTokens := ps.GetPlannerMaxReservedOutputTokens()
+
+ return maxPlannerTokens - maxReservedOutputTokens
}
func (ps PlanSettings) GetContextLoaderEffectiveMaxTokens() int {
- config := ps.ModelPack.GetContextLoader()
- return ps.GetContextLoaderMaxTokens() - config.GetReservedOutputTokens()
+ maxContextLoaderTokens := ps.GetContextLoaderMaxTokens()
+ maxReservedOutputTokens := ps.GetContextLoaderMaxReservedOutputTokens()
+
+ return maxContextLoaderTokens - maxReservedOutputTokens
+}
+
+func (ps PlanSettings) GetWholeFileBuilderEffectiveMaxTokens() int {
+ maxWholeFileBuilderTokens := ps.GetWholeFileBuilderMaxTokens()
+ maxReservedOutputTokens := ps.GetWholeFileBuilderMaxReservedOutputTokens()
+
+ return maxWholeFileBuilderTokens - maxReservedOutputTokens
}
func (ps PlanSettings) GetRequiredEnvVars() map[string]bool {