Open source AI coding agent. Designed for large projects and real world tasks.
Go aiai-agentsai-developer-toolsai-toolsclicommand-linedeveloper-toolsgitgolanggpt-4llmopenaipolyglot-programmingterminalterminal-basedterminal-ui
basic commands, context commands working with cloud--tell/build still needs work + testing
Dane Schneiderfecbb67110bc
diff --git a/README.md b/README.mdindex a95a0a30..02b37946 100644--- a/README.md+++ b/README.md@@ -4,8 +4,8 @@ Iterative development with AI.## Dependencies-* Go 1.21.3 - [install here](https://go.dev/doc/install)-* [reflex](https://github.com/cespare/reflex) 0.3.1 - for watching files and rebuilding in development. Install with `go install github.com/cespare/[email protected]`+- Go 1.21.3 - [install here](https://go.dev/doc/install)+- [reflex](https://github.com/cespare/reflex) 0.3.1 - for watching files and rebuilding in development. Install with `go install github.com/cespare/[email protected]`## Development@@ -20,7 +20,7 @@ This creates watchers with `reflex` to rebuild both the server and the CLI whenThe server runs on port 8088 by default.-After each build, the CLI is copied to `/usr/local/bin/plandex` so you can use it with just `plandex` in any directory. A `pdx` alias is also created.+After each build, the CLI is copied to `/usr/local/bin/plandex` so you can use it with just `plandex` in any directory. A `pdx` alias is also created.## Usage@@ -30,6 +30,8 @@ To start on a new plan, run:plandex new```+For many commands (like `plandex new`), after the command is run, additional commands that may make sense to run next will be suggested.+To see all available commands, run:```diff --git a/cli/api/methods.go b/cli/api/methods.goindex f96b111b..ab8bf8bc 100644--- a/cli/api/methods.go+++ b/cli/api/methods.go@@ -522,7 +522,15 @@ func (a *Api) UpdateContext(planId string, req shared.UpdateContextRequest) (*shreturn nil, fmt.Errorf("error marshalling request: %v", err)}- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(reqBytes))+ request, err := http.NewRequest(http.MethodPut, serverUrl, bytes.NewBuffer(reqBytes))++ if err != nil {+ return nil, fmt.Errorf("error creating request: %v", err)+ }++ request.Header.Set("Content-Type", "application/json")++ resp, err := http.DefaultClient.Do(request)if err != nil {return nil, fmt.Errorf("error sending request: %v", err)}diff --git a/cli/cmd/log.go b/cli/cmd/log.goindex 47e04b15..d277d134 100644--- a/cli/cmd/log.go+++ b/cli/cmd/log.go@@ -12,7 +12,7 @@ import (// logCmd represents the log commandvar logCmd = &cobra.Command{Use: "log",- Aliases: []string{"history"},+ Aliases: []string{"history", "logs"},Short: "Show plan history",Long: `Show plan history.`,Args: cobra.NoArgs,diff --git a/cli/cmd/plans.go b/cli/cmd/plans.goindex b8df0f74..08e51c6f 100644--- a/cli/cmd/plans.go+++ b/cli/cmd/plans.go@@ -50,7 +50,7 @@ func plans(cmd *cobra.Command, args []string) {for i, p := range plans {var name string- if p.Name == lib.CurrentPlanId {+ if p.Id == lib.CurrentPlanId {name = color.New(color.Bold, color.FgGreen).Sprint(p.Name) + color.New(color.FgWhite).Sprint(" π current")} else {name = p.Namediff --git a/cli/cmd/rewind.go b/cli/cmd/rewind.goindex bf8ae89b..3ec62305 100644--- a/cli/cmd/rewind.go+++ b/cli/cmd/rewind.go@@ -35,6 +35,12 @@ func init() {}func rewind(cmd *cobra.Command, args []string) {+ lib.MustResolveProject()++ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")+ return+ }// Check if either steps or sha is provided and not bothstepsOrSha := ""@@ -68,9 +74,12 @@ func rewind(cmd *cobra.Command, args []string) {// Rewind to the target sharwRes, err := api.Client.RewindPlan(lib.CurrentPlanId, shared.RewindPlanRequest{Sha: targetSha})- msg := "β Rewound "+ if err != nil {+ fmt.Fprintf(os.Stderr, "Error rewinding plan: %v\n", err)+ return+ }- msg += "to " + targetSha+ msg := "β Rewound to " + targetShafmt.Println(msg)fmt.Println()diff --git a/cli/cmd/rm.go b/cli/cmd/rm.goindex 98f3c184..51f991fd 100644--- a/cli/cmd/rm.go+++ b/cli/cmd/rm.go@@ -71,7 +71,6 @@ func contextRm(cmd *cobra.Command, args []string) {} else {fmt.Println("π€·ββοΈ No context removed")}-}func init() {diff --git a/cli/lib/apply.go b/cli/lib/apply.goindex b3f67470..d9b07685 100644--- a/cli/lib/apply.go+++ b/cli/lib/apply.go@@ -310,14 +310,8 @@ func ApplyPlanWithOutput(planId string, autoConfirm bool) error {}if isRepo {- desc := currentPlanState.LatestBuildDescription- if err != nil {- aborted = true- return fmt.Errorf("failed to get latest plan description: %w", err)- }-// Commit the changes- err = GitAddAndCommit(ProjectRoot, color.New(color.BgBlue, color.FgHiWhite, color.Bold).Sprintln(" π€ Plandex ")+desc.CommitMsg, true)+ err = GitAddAndCommit(ProjectRoot, color.New(color.BgBlue, color.FgHiWhite, color.Bold).Sprintln(" π€ Plandex ")+currentPlanState.PendingChangesSummary(), true)if err != nil {aborted = truereturn fmt.Errorf("failed to commit changes: %w", err)diff --git a/cli/lib/context_load.go b/cli/lib/context_load.goindex 84fb49ce..885fe149 100644--- a/cli/lib/context_load.go+++ b/cli/lib/context_load.go@@ -179,13 +179,14 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) {onErr(fmt.Errorf("failed to load context: %v", err))}+ s.Stop()+ term.ClearCurrentLine()+if res.MaxTokensExceeded {- s.Stop()- term.ClearCurrentLine()overage := res.TotalTokens - shared.MaxContextTokensfmt.Printf("π¨ Update would add %d πͺ and exceed token limit (%d) by %d πͺ", res.TokensAdded, shared.MaxContextTokens, overage)os.Exit(1)}- fmt.Println(res.Msg)+ fmt.Println("β " + res.Msg)}diff --git a/cli/lib/fs.go b/cli/lib/fs.goindex c2581678..ce7cf8c7 100644--- a/cli/lib/fs.go+++ b/cli/lib/fs.go@@ -208,9 +208,7 @@ func mustInitProject() {}CurrentProjectId = res.Id-- // TODO: placeholder until orgs/auth are implemented- CurrentOrgId = "org1"+ CurrentOrgId = res.OrgId// write project.jsonpath := filepath.Join(PlandexDir, "project.json")diff --git a/cli/lib/proposal.go b/cli/lib/proposal.goindex d63ac0a7..cff94dd5 100644--- a/cli/lib/proposal.go+++ b/cli/lib/proposal.go@@ -1,24 +1,7 @@package libimport (- "context"- "encoding/json"- "fmt"- "os"- "path/filepath"- "plandex/format"- "plandex/term"- "plandex/types"- "strconv"- "strings"- "sync""time"-- "github.com/fatih/color"- "github.com/looplab/fsm"- "github.com/plandex/plandex/shared"-- "github.com/briandowns/spinner")const replyStreamThrottle = 70 * time.Millisecond@@ -28,603 +11,605 @@ type key struct {}func Propose(prompt string) error {- var err error-- planState, err := GetPlanState()- if err != nil {- return fmt.Errorf("failed to get plan state: %s", err)- }-- s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)- if planState.ContextUpdatableTokens > 0 {- shouldContinue, err := checkOutdatedContext(s)- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to check outdated context: %v\n", err)- return err- }-- if !shouldContinue {- return nil- }- }-- start := time.Now()- s.Prefix = "π¬ Sending prompt... "- s.Start()-- if CurrentPlanIsDraft() {- fileNameResp, err := Api.FileName(prompt)- if err != nil {- fmt.Fprintln(os.Stderr, "\nError getting file name for prompt:", err)- return err- }- err = RenameCurrentDraftPlan(planState, format.GetFileNameWithoutExt(fileNameResp.FileName))- if err != nil {- fmt.Fprintln(os.Stderr, "\nError renaming draft plan:", err)- return err- }- }-- // fmt.Println("Checked current plan is draft")-- timestamp := shared.StringTs()- reply := ""- done := make(chan struct{})- closedDone := false-- termState := ""-- replyUpdateTimer := time.NewTimer(150 * time.Millisecond)- defer replyUpdateTimer.Stop()-- var proposalId string- var replyStarted bool- var terminalHasPendingUpdate bool- var desc *shared.PlanDescription- var state *fsm.FSM- var streamFinished bool- var filesFinished bool- var lastReplyTokenAdded time.Time- finishedByPath := make(map[string]bool)- var buildMu sync.Mutex-- numStreamedTokensByPath := make(map[string]int)-- replyTokenCounter := shared.NewReplyInfo()-- var parentProposalId string- var rootId string-- parentProposalId = planState.ProposalId- rootId = planState.RootId-- if rootId != "" {- err = saveLatestConvoSummary(rootId)- if err != nil {- return fmt.Errorf("failed to save latest convo summary: %s", err)- }- }-- // fmt.Println("Initialized locals")-- promptNumTokens, err := shared.GetNumTokens(prompt)- if err != nil {- return fmt.Errorf("failed to get number of tokens in prompt: %s", err)- }-- // fmt.Println("Got prompt num tokens")-- err = appendConversation(types.AppendConversationParams{- Timestamp: timestamp,- PlanState: planState,- PromptParams: &types.AppendConversationPromptParams{- Prompt: prompt,- PromptTokens: promptNumTokens,- },- })-- // fmt.Println("Appended conversation")-- if err != nil {- return fmt.Errorf("failed to append prompt to conversation: %s", err)- }-- printReply := func() {- term.ClearScreen()- term.MoveCursorToTopLeft()- mdFull, _ := term.GetMarkdown(reply)- fmt.Println(mdFull)- fmt.Println(displayHotkeys())- termState = mdFull- }-- go func() {- for range replyUpdateTimer.C {- if replyStarted && terminalHasPendingUpdate {- printReply()- terminalHasPendingUpdate = false- }- replyUpdateTimer.Reset(100 * time.Millisecond)- }- }()-- // fmt.Println("Starting proposal")-- keyChan := make(chan *key, 1)- ctx, cancelKeywatch := context.WithCancel(context.Background())- errChn := make(chan error, 1)-- endReply := func() {- replyUpdateTimer.Stop()- printReply()- term.BackToMain()- fmt.Print(termState)- }-- appendConvoReply := func() error {- var totalTokens int- _, _, _, totalTokens = replyTokenCounter.FinishAndRead()-- return appendConversation(types.AppendConversationParams{- Timestamp: timestamp,- PlanState: planState,- ReplyParams: &types.AppendConversationReplyParams{- ResponseTimestamp: desc.ResponseTimestamp,- Reply: reply,- ReplyTokens: totalTokens,- },- })- }-- writeFileProgress := func() {- files := desc.Files-- if len(files) == 0 {- return- }-- // Clear previous lines- numLines := len(files) + 4-- term.MoveUpLines(numLines)-- for _, filePath := range files {- numStreamedTokens := numStreamedTokensByPath[filePath]-- fmtStr := " π %s | %d πͺ"- fmtArgs := []interface{}{filePath, numStreamedTokens}-- _, finished := finishedByPath[filePath]-- if finished {- fmtStr += " | done β "- }-- term.ClearCurrentLine()-- fmt.Printf(fmtStr+"\n", fmtArgs...)- }-- if !(filesFinished && streamFinished) {- fmt.Println()- fmt.Printf(displayHotkeys() + "\n")- }- }-- contextByFilePath := make(map[string]*shared.ModelContextPart)-- running := false- queue := make(chan types.OnStreamPlanParams, 1)-- var apiReq *shared.PromptRequest-- var handleStream types.OnStreamPlan-- // fmt.Println("Starting handleStream")-- handleStream = func(params types.OnStreamPlanParams) {- if running {- queue <- params- return- }-- defer func() {- if len(queue) > 0 {- params := <-queue- handleStream(params)- } else {- running = false- }- }()-- state = params.State- err := params.Err- content := params.Content-- onError := func(err error) {- term.BackToMain()- fmt.Fprintln(os.Stderr, "Error:", err)- cancelKeywatch()- if !closedDone {- close(done)- closedDone = true- }- }-- if err != nil {- onError(err)- return- }-- if proposalId == "" {- if content == "" {- onError(fmt.Errorf("proposal id not sent in first chunk"))- return- } else {- proposalId = content- if rootId == "" {- rootId = proposalId- }-- // Save proposal id to [CurrentPlanRootDir]/plan.json- planState.ProposalId = proposalId- planState.RootId = rootId- err = SetPlanState(planState, shared.StringTs())- if err != nil {- onError(fmt.Errorf("failed to update plan state: %s", err))- return- }- return- }- } else if !replyStarted {- elapsed := time.Since(start)- if elapsed < 700*time.Millisecond {- time.Sleep(700*time.Millisecond - elapsed)- }-- s.Stop()- term.ClearCurrentLine()- term.AlternateScreen()-- replyStarted = true- lastReplyTokenAdded = time.Now()- }-- switch state.Current() {- case shared.STATE_REPLYING:- elapsed := time.Since(lastReplyTokenAdded)-- if elapsed < replyStreamThrottle {- time.Sleep(replyStreamThrottle - elapsed)- }-- reply += content- replyTokenCounter.AddToken(content, true)- terminalHasPendingUpdate = true- lastReplyTokenAdded = time.Now()-- case shared.STATE_FINISHED:- s.Stop()- buildMu.Lock()- defer buildMu.Unlock()- streamFinished = true- if filesFinished && !closedDone {- close(done)- closedDone = true- writeFileProgress()- fmt.Println()- fmt.Println()- }-- case shared.STATE_DESCRIBING:- if content == shared.STREAM_DESCRIPTION_PHASE {- return- }-- describeStart := time.Now()-- bytes := []byte(content)-- err := json.Unmarshal(bytes, &desc)- if err != nil {- onError(fmt.Errorf("error parsing plan description: %v", err))- return- }-- if len(desc.Files) > 0 {- s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)- s.Prefix = " "- s.Start()- }-- err = os.MkdirAll(DescriptionsSubdir, os.ModePerm)- if err != nil {- onError(fmt.Errorf("failed to create plan descriptions directory: %s", err))- return- }- descriptionsPath := filepath.Join(DescriptionsSubdir, desc.ResponseTimestamp+".json")- err = os.WriteFile(descriptionsPath, bytes, 0644)- if err != nil {- onError(fmt.Errorf("failed to write plan description to file: %s", err))- return- }-- // fmt.Println("appending convo reply")-- err = appendConvoReply()- if err != nil {- onError(fmt.Errorf("failed to append reply to conversation: %s", err))- return- }-- // fmt.Println("appended convo reply")-- // wait a bit if necessary to avoid jarring output of conversation as soon as stream finishes- elapsed := time.Since(describeStart)- if elapsed < 1500*time.Millisecond {- time.Sleep(1500*time.Millisecond - elapsed)- }-- // fmt.Println("waited elapsed")-- endReply()-- // fmt.Println("ended reply")- s.Stop()-- if desc.MadePlan && (len(desc.Files) > 0) {- fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))- for _, filePath := range desc.Files {- fmt.Printf(" π %s\n", filePath)- }- fmt.Println()- fmt.Printf(displayHotkeys() + "\n")- } else {- filesFinished = true- }-- case shared.STATE_BUILDING:- if content == shared.STREAM_BUILD_PHASE {- return-- }- err := updateTokenCounts(content, numStreamedTokensByPath, finishedByPath)-- if err != nil {- onError(err)- return- }-- writeFileProgress()-- case shared.STATE_WRITING:- if content == shared.STREAM_WRITE_PHASE {- // write phase started- return- }-- err := writePlanRes(content)-- if err != nil {- onError(fmt.Errorf("failed to write plan file: %s", err))- return- }-- // fmt.Printf("Wrote %d / %d files", len(finishedByPath), len(files))- if len(finishedByPath) == len(desc.Files) {- buildMu.Lock()- defer buildMu.Unlock()- filesFinished = true-- err = GitCommitPlanUpdate(desc.CommitMsg)- if err != nil {- onError(fmt.Errorf("failed to commit root update: %s", err))- return- }-- if streamFinished && !closedDone {- close(done)- closedDone = true- writeFileProgress()- fmt.Println()- fmt.Println()- }- }- }-- }-- apiReq, err = Api.Propose(prompt, parentProposalId, rootId, handleStream)- if err != nil {- term.BackToMain()- return fmt.Errorf("failed to send prompt to server: %s", err)- }-- // fmt.Println("Sent prompt to server")-- for _, part := range apiReq.ModelContext {- contextByFilePath[part.FilePath] = part- }-- // fmt.Println("Got context")-- go func(ctx context.Context, errChn chan error) {- for {- select {- case <-ctx.Done():- return- default:- k, err := term.GetUserInput()- if err != nil {- errChn <- err- return- }- keyChan <- &key{k}- }- }- }(ctx, errChn)-- handleKey := func(k *key) error {- return handleKeyPress(k.value, proposalId)- }--Loop:- for {- select {- case k := <-keyChan:- if err := handleKey(k); err != nil {- cancelKeywatch()- return err- }- case <-done: // Evidence of operation completion- cancelKeywatch()- break Loop- case err := <-errChn: // Listening for errors- cancelKeywatch()- return err- }- }-- // fmt.Println("Started key loop")-- if desc != nil {- if desc.MadePlan && len(desc.Files) > 0 {- fmt.Println()- for _, cmd := range []string{"apply", "diffs", "preview"} {- term.ClearCurrentLine()- term.PrintCmds(" ", cmd)- }- }-- term.ClearCurrentLine()- term.PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")-- term.ClearCurrentLine()- term.PrintCmds(" ", "continue")-- term.ClearCurrentLine()- term.PrintCmds(" ", "rewind")- }-- // fmt.Println("Finished proposal")+ // var err error++ // planState, err := GetPlanState()+ // if err != nil {+ // return fmt.Errorf("failed to get plan state: %s", err)+ // }++ // s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)+ // if planState.ContextUpdatableTokens > 0 {+ // shouldContinue, err := checkOutdatedContext(s)+ // if err != nil {+ // fmt.Fprintf(os.Stderr, "Failed to check outdated context: %v\n", err)+ // return err+ // }++ // if !shouldContinue {+ // return nil+ // }+ // }++ // start := time.Now()+ // s.Prefix = "π¬ Sending prompt... "+ // s.Start()++ // if CurrentPlanIsDraft() {+ // fileNameResp, err := Api.FileName(prompt)+ // if err != nil {+ // fmt.Fprintln(os.Stderr, "\nError getting file name for prompt:", err)+ // return err+ // }+ // err = RenameCurrentDraftPlan(planState, format.GetFileNameWithoutExt(fileNameResp.FileName))+ // if err != nil {+ // fmt.Fprintln(os.Stderr, "\nError renaming draft plan:", err)+ // return err+ // }+ // }++ // // fmt.Println("Checked current plan is draft")++ // timestamp := shared.StringTs()+ // reply := ""+ // done := make(chan struct{})+ // closedDone := false++ // termState := ""++ // replyUpdateTimer := time.NewTimer(150 * time.Millisecond)+ // defer replyUpdateTimer.Stop()++ // var proposalId string+ // var replyStarted bool+ // var terminalHasPendingUpdate bool+ // var desc *shared.PlanDescription+ // var state *fsm.FSM+ // var streamFinished bool+ // var filesFinished bool+ // var lastReplyTokenAdded time.Time+ // finishedByPath := make(map[string]bool)+ // var buildMu sync.Mutex++ // numStreamedTokensByPath := make(map[string]int)++ // replyTokenCounter := shared.NewReplyInfo()++ // var parentProposalId string+ // var rootId string++ // parentProposalId = planState.ProposalId+ // rootId = planState.RootId++ // if rootId != "" {+ // err = saveLatestConvoSummary(rootId)+ // if err != nil {+ // return fmt.Errorf("failed to save latest convo summary: %s", err)+ // }+ // }++ // // fmt.Println("Initialized locals")++ // promptNumTokens, err := shared.GetNumTokens(prompt)+ // if err != nil {+ // return fmt.Errorf("failed to get number of tokens in prompt: %s", err)+ // }++ // // fmt.Println("Got prompt num tokens")++ // err = appendConversation(types.AppendConversationParams{+ // Timestamp: timestamp,+ // PlanState: planState,+ // PromptParams: &types.AppendConversationPromptParams{+ // Prompt: prompt,+ // PromptTokens: promptNumTokens,+ // },+ // })++ // // fmt.Println("Appended conversation")++ // if err != nil {+ // return fmt.Errorf("failed to append prompt to conversation: %s", err)+ // }++ // printReply := func() {+ // term.ClearScreen()+ // term.MoveCursorToTopLeft()+ // mdFull, _ := term.GetMarkdown(reply)+ // fmt.Println(mdFull)+ // fmt.Println(displayHotkeys())+ // termState = mdFull+ // }++ // go func() {+ // for range replyUpdateTimer.C {+ // if replyStarted && terminalHasPendingUpdate {+ // printReply()+ // terminalHasPendingUpdate = false+ // }+ // replyUpdateTimer.Reset(100 * time.Millisecond)+ // }+ // }()++ // // fmt.Println("Starting proposal")++ // keyChan := make(chan *key, 1)+ // ctx, cancelKeywatch := context.WithCancel(context.Background())+ // errChn := make(chan error, 1)++ // endReply := func() {+ // replyUpdateTimer.Stop()+ // printReply()+ // term.BackToMain()+ // fmt.Print(termState)+ // }++ // appendConvoReply := func() error {+ // var totalTokens int+ // _, _, _, totalTokens = replyTokenCounter.FinishAndRead()++ // return appendConversation(types.AppendConversationParams{+ // Timestamp: timestamp,+ // PlanState: planState,+ // ReplyParams: &types.AppendConversationReplyParams{+ // ResponseTimestamp: desc.ResponseTimestamp,+ // Reply: reply,+ // ReplyTokens: totalTokens,+ // },+ // })+ // }++ // writeFileProgress := func() {+ // files := desc.Files++ // if len(files) == 0 {+ // return+ // }++ // // Clear previous lines+ // numLines := len(files) + 4++ // term.MoveUpLines(numLines)++ // for _, filePath := range files {+ // numStreamedTokens := numStreamedTokensByPath[filePath]++ // fmtStr := " π %s | %d πͺ"+ // fmtArgs := []interface{}{filePath, numStreamedTokens}++ // _, finished := finishedByPath[filePath]++ // if finished {+ // fmtStr += " | done β "+ // }++ // term.ClearCurrentLine()++ // fmt.Printf(fmtStr+"\n", fmtArgs...)+ // }++ // if !(filesFinished && streamFinished) {+ // fmt.Println()+ // fmt.Printf(displayHotkeys() + "\n")+ // }+ // }++ // contextByFilePath := make(map[string]*shared.ModelContextPart)++ // running := false+ // queue := make(chan types.OnStreamPlanParams, 1)++ // var apiReq *shared.PromptRequest++ // var handleStream types.OnStreamPlan++ // // fmt.Println("Starting handleStream")++ // handleStream = func(params types.OnStreamPlanParams) {+ // if running {+ // queue <- params+ // return+ // }++ // defer func() {+ // if len(queue) > 0 {+ // params := <-queue+ // handleStream(params)+ // } else {+ // running = false+ // }+ // }()++ // state = params.State+ // err := params.Err+ // content := params.Content++ // onError := func(err error) {+ // term.BackToMain()+ // fmt.Fprintln(os.Stderr, "Error:", err)+ // cancelKeywatch()+ // if !closedDone {+ // close(done)+ // closedDone = true+ // }+ // }++ // if err != nil {+ // onError(err)+ // return+ // }++ // if proposalId == "" {+ // if content == "" {+ // onError(fmt.Errorf("proposal id not sent in first chunk"))+ // return+ // } else {+ // proposalId = content+ // if rootId == "" {+ // rootId = proposalId+ // }++ // // Save proposal id to [CurrentPlanRootDir]/plan.json+ // planState.ProposalId = proposalId+ // planState.RootId = rootId+ // err = SetPlanState(planState, shared.StringTs())+ // if err != nil {+ // onError(fmt.Errorf("failed to update plan state: %s", err))+ // return+ // }+ // return+ // }+ // } else if !replyStarted {+ // elapsed := time.Since(start)+ // if elapsed < 700*time.Millisecond {+ // time.Sleep(700*time.Millisecond - elapsed)+ // }++ // s.Stop()+ // term.ClearCurrentLine()+ // term.AlternateScreen()++ // replyStarted = true+ // lastReplyTokenAdded = time.Now()+ // }++ // switch state.Current() {+ // case shared.STATE_REPLYING:+ // elapsed := time.Since(lastReplyTokenAdded)++ // if elapsed < replyStreamThrottle {+ // time.Sleep(replyStreamThrottle - elapsed)+ // }++ // reply += content+ // replyTokenCounter.AddToken(content, true)+ // terminalHasPendingUpdate = true+ // lastReplyTokenAdded = time.Now()++ // case shared.STATE_FINISHED:+ // s.Stop()+ // buildMu.Lock()+ // defer buildMu.Unlock()+ // streamFinished = true+ // if filesFinished && !closedDone {+ // close(done)+ // closedDone = true+ // writeFileProgress()+ // fmt.Println()+ // fmt.Println()+ // }++ // case shared.STATE_DESCRIBING:+ // if content == shared.STREAM_DESCRIPTION_PHASE {+ // return+ // }++ // describeStart := time.Now()++ // bytes := []byte(content)++ // err := json.Unmarshal(bytes, &desc)+ // if err != nil {+ // onError(fmt.Errorf("error parsing plan description: %v", err))+ // return+ // }++ // if len(desc.Files) > 0 {+ // s = spinner.New(spinner.CharSets[33], 100*time.Millisecond)+ // s.Prefix = " "+ // s.Start()+ // }++ // err = os.MkdirAll(DescriptionsSubdir, os.ModePerm)+ // if err != nil {+ // onError(fmt.Errorf("failed to create plan descriptions directory: %s", err))+ // return+ // }+ // descriptionsPath := filepath.Join(DescriptionsSubdir, desc.ResponseTimestamp+".json")+ // err = os.WriteFile(descriptionsPath, bytes, 0644)+ // if err != nil {+ // onError(fmt.Errorf("failed to write plan description to file: %s", err))+ // return+ // }++ // // fmt.Println("appending convo reply")++ // err = appendConvoReply()+ // if err != nil {+ // onError(fmt.Errorf("failed to append reply to conversation: %s", err))+ // return+ // }++ // // fmt.Println("appended convo reply")++ // // wait a bit if necessary to avoid jarring output of conversation as soon as stream finishes+ // elapsed := time.Since(describeStart)+ // if elapsed < 1500*time.Millisecond {+ // time.Sleep(1500*time.Millisecond - elapsed)+ // }++ // // fmt.Println("waited elapsed")++ // endReply()++ // // fmt.Println("ended reply")+ // s.Stop()++ // if desc.MadePlan && (len(desc.Files) > 0) {+ // fmt.Println(" " + color.New(color.BgGreen, color.FgHiWhite, color.Bold).Sprint(" π ") + color.New(color.BgGreen, color.FgHiWhite).Sprint("Building plan "))+ // for _, filePath := range desc.Files {+ // fmt.Printf(" π %s\n", filePath)+ // }+ // fmt.Println()+ // fmt.Printf(displayHotkeys() + "\n")+ // } else {+ // filesFinished = true+ // }++ // case shared.STATE_BUILDING:+ // if content == shared.STREAM_BUILD_PHASE {+ // return++ // }+ // err := updateTokenCounts(content, numStreamedTokensByPath, finishedByPath)++ // if err != nil {+ // onError(err)+ // return+ // }++ // writeFileProgress()++ // case shared.STATE_WRITING:+ // if content == shared.STREAM_WRITE_PHASE {+ // // write phase started+ // return+ // }++ // err := writePlanRes(content)++ // if err != nil {+ // onError(fmt.Errorf("failed to write plan file: %s", err))+ // return+ // }++ // // fmt.Printf("Wrote %d / %d files", len(finishedByPath), len(files))+ // if len(finishedByPath) == len(desc.Files) {+ // buildMu.Lock()+ // defer buildMu.Unlock()+ // filesFinished = true++ // err = GitCommitPlanUpdate(desc.CommitMsg)+ // if err != nil {+ // onError(fmt.Errorf("failed to commit root update: %s", err))+ // return+ // }++ // if streamFinished && !closedDone {+ // close(done)+ // closedDone = true+ // writeFileProgress()+ // fmt.Println()+ // fmt.Println()+ // }+ // }+ // }++ // }++ // apiReq, err = Api.Propose(prompt, parentProposalId, rootId, handleStream)+ // if err != nil {+ // term.BackToMain()+ // return fmt.Errorf("failed to send prompt to server: %s", err)+ // }++ // // fmt.Println("Sent prompt to server")++ // for _, part := range apiReq.ModelContext {+ // contextByFilePath[part.FilePath] = part+ // }++ // // fmt.Println("Got context")++ // go func(ctx context.Context, errChn chan error) {+ // for {+ // select {+ // case <-ctx.Done():+ // return+ // default:+ // k, err := term.GetUserInput()+ // if err != nil {+ // errChn <- err+ // return+ // }+ // keyChan <- &key{k}+ // }+ // }+ // }(ctx, errChn)++ // handleKey := func(k *key) error {+ // return handleKeyPress(k.value, proposalId)+ // }++ // Loop:+ // for {+ // select {+ // case k := <-keyChan:+ // if err := handleKey(k); err != nil {+ // cancelKeywatch()+ // return err+ // }+ // case <-done: // Evidence of operation completion+ // cancelKeywatch()+ // break Loop+ // case err := <-errChn: // Listening for errors+ // cancelKeywatch()+ // return err+ // }+ // }++ // // fmt.Println("Started key loop")++ // if desc != nil {+ // if desc.MadePlan && len(desc.Files) > 0 {+ // fmt.Println()+ // for _, cmd := range []string{"apply", "diffs", "preview"} {+ // term.ClearCurrentLine()+ // term.PrintCmds(" ", cmd)+ // }+ // }++ // term.ClearCurrentLine()+ // term.PrintCustomCmd(" ", "tell", "t", "update the plan, give more info, or chat")++ // term.ClearCurrentLine()+ // term.PrintCmds(" ", "continue")++ // term.ClearCurrentLine()+ // term.PrintCmds(" ", "rewind")+ // }++ // // fmt.Println("Finished proposal")++ // return nil+ // }++ // func Abort(proposalId string) error {+ // err := Api.Abort(proposalId)+ // return err+ // }++ // func checkOutdatedContext(s *spinner.Spinner) (bool, error) {+ // start := time.Now()+ // s.Prefix = "π¬ Checking context... "+ // s.Start()++ // stopSpinner := func() {+ // s.Stop()+ // term.ClearCurrentLine()+ // }++ // outdatedRes, err := CheckOutdatedContext()+ // if err != nil {+ // stopSpinner()+ // return false, fmt.Errorf("failed to check outdated context: %s", err)+ // }++ // elapsed := time.Since(start)+ // if elapsed < 700*time.Millisecond {+ // time.Sleep(700*time.Millisecond - elapsed)+ // }++ // stopSpinner()++ // if len(outdatedRes.UpdatedContexts) == 0 {+ // fmt.Println("β Context is up to date")+ // return true, nil+ // }+ // types := []string{}+ // if outdatedRes.NumFiles > 0 {+ // lbl := "file"+ // if outdatedRes.NumFiles > 1 {+ // lbl = "files"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl+ // types = append(types, lbl)+ // }+ // if outdatedRes.NumUrls > 0 {+ // lbl := "url"+ // if outdatedRes.NumUrls > 1 {+ // lbl = "urls"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl+ // types = append(types, lbl)+ // }+ // if outdatedRes.NumTrees > 0 {+ // lbl := "directory tree"+ // if outdatedRes.NumTrees > 1 {+ // lbl = "directory trees"+ // }+ // lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl+ // types = append(types, lbl)+ // }++ // var msg string+ // if len(types) <= 2 {+ // msg += strings.Join(types, " and ")+ // } else {+ // for i, add := range types {+ // if i == len(types)-1 {+ // msg += ", and " + add+ // } else {+ // msg += ", " + add+ // }+ // }+ // }++ // phrase := "have been"+ // if len(outdatedRes.UpdatedContexts) == 1 {+ // phrase = "has been"+ // }+ // color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)++ // tableString := TableForContextUpdateRes(outdatedRes)+ // fmt.Println(tableString)++ // fmt.Println()++ // confirmed, canceled, err := term.ConfirmYesNoCancel("Update context now?")++ // if err != nil {+ // return false, fmt.Errorf("failed to get user input: %s", err)+ // }++ // if confirmed {+ // MustUpdateContextWithOuput()+ // }++ // shouldContinue := !canceled++ // return shouldContinue, nilreturn nil}-func Abort(proposalId string) error {- err := Api.Abort(proposalId)- return err-}--func checkOutdatedContext(s *spinner.Spinner) (bool, error) {- start := time.Now()- s.Prefix = "π¬ Checking context... "- s.Start()-- stopSpinner := func() {- s.Stop()- term.ClearCurrentLine()- }-- outdatedRes, err := CheckOutdatedContext()- if err != nil {- stopSpinner()- return false, fmt.Errorf("failed to check outdated context: %s", err)- }-- elapsed := time.Since(start)- if elapsed < 700*time.Millisecond {- time.Sleep(700*time.Millisecond - elapsed)- }-- stopSpinner()-- if len(outdatedRes.UpdatedContexts) == 0 {- fmt.Println("β Context is up to date")- return true, nil- }- types := []string{}- if outdatedRes.NumFiles > 0 {- lbl := "file"- if outdatedRes.NumFiles > 1 {- lbl = "files"- }- lbl = strconv.Itoa(outdatedRes.NumFiles) + " " + lbl- types = append(types, lbl)- }- if outdatedRes.NumUrls > 0 {- lbl := "url"- if outdatedRes.NumUrls > 1 {- lbl = "urls"- }- lbl = strconv.Itoa(outdatedRes.NumUrls) + " " + lbl- types = append(types, lbl)- }- if outdatedRes.NumTrees > 0 {- lbl := "directory tree"- if outdatedRes.NumTrees > 1 {- lbl = "directory trees"- }- lbl = strconv.Itoa(outdatedRes.NumTrees) + " " + lbl- types = append(types, lbl)- }-- var msg string- if len(types) <= 2 {- msg += strings.Join(types, " and ")- } else {- for i, add := range types {- if i == len(types)-1 {- msg += ", and " + add- } else {- msg += ", " + add- }- }- }-- phrase := "have been"- if len(outdatedRes.UpdatedContexts) == 1 {- phrase = "has been"- }- color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)-- tableString := TableForContextUpdateRes(outdatedRes)- fmt.Println(tableString)-- fmt.Println()-- confirmed, canceled, err := term.ConfirmYesNoCancel("Update context now?")-- if err != nil {- return false, fmt.Errorf("failed to get user input: %s", err)- }-- if confirmed {- MustUpdateContextWithOuput()- }-- shouldContinue := !canceled-- return shouldContinue, nil-}--func handleAbortKey(proposalId string) error {- return Abort(proposalId)-}--func handleKeyPress(input rune, proposalId string) error {- switch input {- case 's':- return handleAbortKey(proposalId)- default:- return fmt.Errorf("invalid key pressed: %s", string(input))- }-}--func displayHotkeys() string {- divisionLine := term.GetDivisionLine()-- return divisionLine + "\n" +- " \x1b[1m(s)\x1b[0m" + `top-` + divisionLine-}+// func handleAbortKey(proposalId string) error {+// return Abort(proposalId)+// }++// func handleKeyPress(input rune, proposalId string) error {+// switch input {+// case 's':+// return handleAbortKey(proposalId)+// default:+// return fmt.Errorf("invalid key pressed: %s", string(input))+// }+// }++// func displayHotkeys() string {+// divisionLine := term.GetDivisionLine()++// return divisionLine + "\n" ++// " \x1b[1m(s)\x1b[0m" + `top+// ` + divisionLine+// }diff --git a/cli/main.go b/cli/main.goindex 13f10a1d..e217051a 100644--- a/cli/main.go+++ b/cli/main.go@@ -16,7 +16,7 @@ func init() {panic(err)}- dir := filepath.Join(home, ".plandex")+ dir := filepath.Join(home, ".plandex-home")err = os.MkdirAll(dir, os.ModePerm)if err != nil {panic(err)diff --git a/server/db/context_helpers.go b/server/db/context_helpers.goindex 629daf2c..9deacbf6 100644--- a/server/db/context_helpers.go+++ b/server/db/context_helpers.go@@ -98,7 +98,7 @@ func ContextRemove(contexts []*Context) error {for _, context := range contexts {contextDir := getPlanContextDir(context.OrgId, context.PlanId)for _, ext := range []string{".meta", ".body"} {- go func(dircontext *Context, dir, ext string) {+ go func(context *Context, dir, ext string) {errCh <- os.Remove(filepath.Join(dir, context.Id+ext))}(context, contextDir, ext)}diff --git a/server/db/convo_helpers.go b/server/db/convo_helpers.goindex e71b368b..885282a7 100644--- a/server/db/convo_helpers.go+++ b/server/db/convo_helpers.go@@ -62,7 +62,7 @@ func GetPlanConvo(orgId, planId string) ([]*ConvoMessage, error) {return convo, nil}-func StoreConvoMessage(message *ConvoMessage) error {+func StoreConvoMessage(message *ConvoMessage, commit bool) (string, error) {convoDir := getPlanConversationDir(message.OrgId, message.PlanId)id := uuid.New().String()@@ -74,19 +74,19 @@ func StoreConvoMessage(message *ConvoMessage) error {bytes, err := json.Marshal(message)if err != nil {- return fmt.Errorf("error marshalling convo message: %v", err)+ return "", fmt.Errorf("error marshalling convo message: %v", err)}err = os.WriteFile(filepath.Join(convoDir, message.Id+".json"), bytes, os.ModePerm)if err != nil {- return fmt.Errorf("error writing convo message: %v", err)+ return "", fmt.Errorf("error writing convo message: %v", err)}err = AddPlanConvoTokens(message.PlanId, message.Tokens)if err != nil {- return fmt.Errorf("error adding convo tokens: %v", err)+ return "", fmt.Errorf("error adding convo tokens: %v", err)}var desc string@@ -101,13 +101,15 @@ func StoreConvoMessage(message *ConvoMessage) error {}msg := fmt.Sprintf("Message #%d | %s | %d πͺ", message.Num, desc, message.Tokens)- err = GitAddAndCommit(message.OrgId, message.PlanId, msg)- if err != nil {- return fmt.Errorf("error committing convo message: %v", err)+ if commit {+ err = GitAddAndCommit(message.OrgId, message.PlanId, msg)+ if err != nil {+ return "", fmt.Errorf("error committing convo message: %v", err)+ }}- return nil+ return msg, nil}func GetPlanSummaries(planId string) ([]*ConvoSummary, error) {diff --git a/server/db/db.go b/server/db/db.goindex 9be70281..5735d0ec 100644--- a/server/db/db.go+++ b/server/db/db.go@@ -40,13 +40,27 @@ func MigrationsUp() error {}m, err := migrate.NewWithDatabaseInstance(- "file:///migrations",+ "file://migrations","postgres", driver)if err != nil {return fmt.Errorf("error creating migration instance: %v", err)}+ // Uncomment below to reset migration state after an error / dirty state+ // // Check if database is dirty+ // version, dirty, err := m.Version()+ // if err != nil {+ // return fmt.Errorf("error getting migration version: %v", err)+ // }++ // if dirty {+ // // Force the version to the current version to clean the dirty state+ // if err := m.Force(int(version)); err != nil {+ // return fmt.Errorf("error forcing migration version: %v", err)+ // }+ // }+err = m.Up()if err != nil && err != migrate.ErrNoChange {diff --git a/server/db/fs.go b/server/db/fs.goindex 5117b862..08de7650 100644--- a/server/db/fs.go+++ b/server/db/fs.go@@ -11,7 +11,7 @@ var BaseDir stringfunc init() {home, err := os.UserHomeDir()if err != nil {- panic(fmt.Errorf("Error getting user home dir: %v", err))+ panic(fmt.Errorf("error getting user home dir: %v", err))}BaseDir = filepath.Join(home, "plandex-server")@@ -28,7 +28,8 @@ func InitPlan(orgId, planId string) error {for _, subdirFn := range [](func(orgId, planId string) string){getPlanContextDir,getPlanConversationDir,- getPlanResultsDir} {+ getPlanResultsDir,+ getPlanDescriptionsDir} {err = os.MkdirAll(subdirFn(orgId, planId), os.ModePerm)if err != nil {@@ -71,3 +72,7 @@ func getPlanConversationDir(orgId, planId string) string {func getPlanResultsDir(orgId, planId string) string {return filepath.Join(getPlanDir(orgId, planId), "results")}++func getPlanDescriptionsDir(orgId, planId string) string {+ return filepath.Join(getPlanDir(orgId, planId), "descriptions")+}diff --git a/server/db/git.go b/server/db/git.goindex 19562ae5..3b70e261 100644--- a/server/db/git.go+++ b/server/db/git.go@@ -15,7 +15,7 @@ func init() {// ensure git is availablecmd := exec.Command("git", "--version")if err := cmd.Run(); err != nil {- panic(fmt.Errorf("Error running git --version: %v", err))+ panic(fmt.Errorf("error running git --version: %v", err))}}diff --git a/server/db/models.go b/server/db/models.goindex 254e3ae4..688b8e1d 100644--- a/server/db/models.go+++ b/server/db/models.go@@ -86,10 +86,9 @@ type Plan struct {ProjectId string `db:"project_id"`Name string `db:"name"`Status shared.PlanStatus `db:"status"`- Error string `db:"error"`+ Error *string `db:"error"`ContextTokens int `db:"context_tokens"`ConvoTokens int `db:"convo_tokens"`- AppliedAt *time.Time `db:"applied_at,omitempty"`ArchivedAt *time.Time `db:"archived_at,omitempty"`CreatedAt time.Time `db:"created_at"`UpdatedAt time.Time `db:"updated_at"`@@ -103,7 +102,6 @@ func (plan *Plan) ToApi() *shared.Plan {Status: plan.Status,ContextTokens: plan.ContextTokens,ConvoTokens: plan.ConvoTokens,- AppliedAt: plan.AppliedAt,ArchivedAt: plan.ArchivedAt,CreatedAt: plan.CreatedAt,UpdatedAt: plan.UpdatedAt,@@ -134,34 +132,6 @@ func (summary *ConvoSummary) ToApi() *shared.ConvoSummary {}}-type ConvoMessageDescription struct {- Id string `db:"id"`- OrgId string `db:"org_id"`- PlanId string `db:"plan_id"`- ConvoMessageId string `db:"convo_message_id"`- SummarizedToMessageId string `db:"summarized_to_message_id"`- MadePlan bool `db:"made_plan"`- CommitMsg string `db:"commit_msg"`- Files []string `db:"files"`- Error string `db:"error"`- CreatedAt time.Time `db:"created_at"`- UpdatedAt time.Time `db:"updated_at"`-}--func (desc *ConvoMessageDescription) ToApi() *shared.ConvoMessageDescription {- return &shared.ConvoMessageDescription{- Id: desc.Id,- ConvoMessageId: desc.ConvoMessageId,- SummarizedToMessageId: desc.SummarizedToMessageId,- MadePlan: desc.MadePlan,- CommitMsg: desc.CommitMsg,- Files: desc.Files,- Error: desc.Error,- CreatedAt: desc.CreatedAt,- UpdatedAt: desc.UpdatedAt,- }-}-type PlanBuild struct {Id string `db:"id"`OrgId string `db:"org_id"`@@ -245,35 +215,66 @@ func (msg *ConvoMessage) ToApi() *shared.ConvoMessage {}}+type ConvoMessageDescription struct {+ Id string `json:"id"`+ OrgId string `json:"orgId"`+ PlanId string `json:"planId"`+ ConvoMessageId string `json:"convoMessageId"`+ SummarizedToMessageId string `json:"summarizedToMessageId"`+ MadePlan bool `json:"madePlan"`+ CommitMsg string `json:"commitMsg"`+ Files []string `json:"files"`+ Error string `json:"error"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`+}++func (desc *ConvoMessageDescription) ToApi() *shared.ConvoMessageDescription {+ return &shared.ConvoMessageDescription{+ Id: desc.Id,+ ConvoMessageId: desc.ConvoMessageId,+ SummarizedToMessageId: desc.SummarizedToMessageId,+ MadePlan: desc.MadePlan,+ CommitMsg: desc.CommitMsg,+ Files: desc.Files,+ Error: desc.Error,+ CreatedAt: desc.CreatedAt,+ UpdatedAt: desc.UpdatedAt,+ }+}+type PlanFileResult struct {- Id string `json:"id"`- OrgId string `json:"orgId"`- PlanId string `json:"planId"`- PlanBuildId string `json:"planBuildId"`- Path string `json:"path"`- ContextSha string `json:"contextSha"`- Content string `json:"content,omitempty"`- Replacements []*shared.Replacement `json:"replacements"`- AnyFailed bool `json:"anyFailed"`- Error string `json:"error"`- AppliedAt *time.Time `json:"appliedAt,omitempty"`- RejectedAt *time.Time `json:"rejectedAt,omitempty"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ OrgId string `json:"orgId"`+ PlanId string `json:"planId"`+ ConvoMessageId string `json:"convoMessageId"`+ PlanBuildId string `json:"planBuildId"`+ Path string `json:"path"`+ ContextSha string `json:"contextSha"`+ Content string `json:"content,omitempty"`+ Replacements []*shared.Replacement `json:"replacements"`+ AnyFailed bool `json:"anyFailed"`+ Error string `json:"error"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}func (res *PlanFileResult) ToApi() *shared.PlanFileResult {return &shared.PlanFileResult{- Id: res.Id,- PlanBuildId: res.PlanBuildId,- Path: res.Path,- ContextSha: res.ContextSha,- Content: res.Content,- AnyFailed: res.AnyFailed,- AppliedAt: res.AppliedAt,- RejectedAt: res.RejectedAt,- Replacements: res.Replacements,- CreatedAt: res.CreatedAt,- UpdatedAt: res.UpdatedAt,+ Id: res.Id,+ PlanBuildId: res.PlanBuildId,+ ConvoMessageId: res.ConvoMessageId,+ Path: res.Path,+ ContextSha: res.ContextSha,+ Content: res.Content,+ AnyFailed: res.AnyFailed,+ AppliedAt: res.AppliedAt,+ RejectedAt: res.RejectedAt,+ Replacements: res.Replacements,+ CreatedAt: res.CreatedAt,+ UpdatedAt: res.UpdatedAt,}}diff --git a/server/db/plan_helpers.go b/server/db/plan_helpers.goindex 88f29d3b..420c8ede 100644--- a/server/db/plan_helpers.go+++ b/server/db/plan_helpers.go@@ -1,21 +1,26 @@package dbimport (+ "encoding/json""fmt""log"+ "os"+ "path/filepath""time"+ "github.com/google/uuid""github.com/plandex/plandex/shared")func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {- query := "INSERT INTO plans (org_id, creator_id, project_id, name) VALUES (:org_id, :creator_id, :project_id, :name) RETURNING id, created_at, updated_at"+ query := "INSERT INTO plans (org_id, creator_id, project_id, name, status) VALUES (:org_id, :creator_id, :project_id, :name, :status) RETURNING id, created_at, updated_at"plan := &Plan{OrgId: orgId,CreatorId: userId,ProjectId: projectId,Name: name,+ Status: shared.PlanStatusDraft,}row, err := Conn.NamedQuery(query, plan)@@ -48,7 +53,7 @@ func CreatePlan(orgId, projectId, userId, name string) (*Plan, error) {}func ListPlans(projectId, userId string, archived bool, status string) ([]shared.Plan, error) {- qs := "SELECT id, creator_id, name, status, context_tokens, convo_tokens, applied_at, archived_at, created_at, updated_at FROM plans WHERE project_id = $1 AND creator_id = $2 ORDER BY updated_at DESC"+ qs := "SELECT id, creator_id, name, status, context_tokens, convo_tokens, archived_at, created_at, updated_at FROM plans WHERE project_id = $1 AND creator_id = $2"qargs := []interface{}{projectId, userId}if archived {@@ -63,7 +68,6 @@ func ListPlans(projectId, userId string, archived bool, status string) ([]shared}qs += " ORDER BY updated_at DESC"-res, err := Conn.Query(qs, qargs...)if err != nil {@@ -71,13 +75,11 @@ func ListPlans(projectId, userId string, archived bool, status string) ([]shared}defer res.Close()-var plans []shared.Plan-for res.Next() {var plan shared.Plan- err := res.Scan(&plan.Id, &plan.CreatorId, &plan.Name, &plan.Status, &plan.ContextTokens, &plan.ConvoTokens, &plan.AppliedAt, &plan.ArchivedAt, &plan.CreatedAt, &plan.UpdatedAt)+ err := res.Scan(&plan.Id, &plan.CreatorId, &plan.Name, &plan.Status, &plan.ContextTokens, &plan.ConvoTokens, &plan.ArchivedAt, &plan.CreatedAt, &plan.UpdatedAt)if err != nil {return nil, fmt.Errorf("error scanning plan: %v", err)@@ -104,10 +106,53 @@ func AddPlanConvoTokens(planId string, addTokens int) error {return nil}-func GetPlan(planId string) (*shared.Plan, error) {- var plan shared.Plan+func SyncPlanTokens(orgId, planId string) error {+ var contexts []*Context+ var convos []*ConvoMessage+ errCh := make(chan error)++ go func() {+ var err error+ contexts, err = GetPlanContexts(orgId, planId, false)+ errCh <- err+ }()++ go func() {+ var err error+ convos, err = GetPlanConvo(orgId, planId)+ errCh <- err+ }()++ for i := 0; i < 2; i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error getting contexts or convo: %v", err)+ }+ }++ contextTokens := 0+ for _, context := range contexts {+ contextTokens += context.NumTokens+ }++ convoTokens := 0+ for _, msg := range convos {+ convoTokens += msg.Tokens+ }++ _, err := Conn.Exec("UPDATE plans SET context_tokens = $1, convo_tokens = $2 WHERE id = $3", contextTokens, convoTokens, planId)++ if err != nil {+ return fmt.Errorf("error updating plan tokens: %v", err)+ }++ return nil+}++func GetPlan(planId string) (*Plan, error) {+ var plan Plan- err := Conn.Get(&plan, "SELECT id, creator_id, name, status, context_tokens, convo_tokens, applied_at, archived_at, created_at, updated_at FROM plans WHERE id = $1", planId)+ err := Conn.Get(&plan, "SELECT id, org_id, creator_id, project_id, name, status, error, context_tokens, convo_tokens, archived_at, created_at, updated_at FROM plans WHERE id = $1", planId)if err != nil {return nil, fmt.Errorf("error getting plan: %v", err)@@ -126,27 +171,37 @@ func SetPlanStatus(planId string, status shared.PlanStatus, errStr string) errorreturn nil}+func RenamePlan(planId string, name string) error {+ _, err := Conn.Exec("UPDATE plans SET name = $1 WHERE id = $2", name, planId)++ if err != nil {+ return fmt.Errorf("error renaming plan: %v", err)+ }++ return nil+}+func StoreDescription(description *ConvoMessageDescription) error {- query := "INSERT INTO convo_message_descriptions (org_id, plan_id, convo_message_id, summarized_to_message_created_at, made_plan, commit_msg, files, error) VALUES (:id, :org_id, :plan_id, :convo_message_id, :summarized_to_message_created_at, :made_plan, :commit_msg, :files, :error) RETURNING id, created_at, updated_at"+ descriptionsDir := getPlanDescriptionsDir(description.OrgId, description.PlanId)- row, err := Conn.NamedQuery(query, description)+ now := time.Now()- if err != nil {- return fmt.Errorf("error storing convo message description: %v", err)+ if description.Id == "" {+ description.Id = uuid.New().String()+ description.CreatedAt = now}+ description.UpdatedAt = now- defer row.Close()+ bytes, err := json.Marshal(description)- if row.Next() {- var createdAt, updatedAt time.Time- var id string- if err := row.Scan(&id, &createdAt, &updatedAt); err != nil {- return fmt.Errorf("error storing convo message description: %v", err)- }+ if err != nil {+ return fmt.Errorf("error marshalling convo message description: %v", err)+ }- description.Id = id- description.CreatedAt = createdAt- description.UpdatedAt = updatedAt+ err = os.WriteFile(filepath.Join(descriptionsDir, description.Id+".json"), bytes, os.ModePerm)++ if err != nil {+ return fmt.Errorf("error writing convo message description: %v", err)}return nildiff --git a/server/db/result_helpers.go b/server/db/result_helpers.goindex 9bb10486..fe564c2e 100644--- a/server/db/result_helpers.go+++ b/server/db/result_helpers.go@@ -45,37 +45,76 @@ func StorePlanResult(result *PlanFileResult) error {}-func GetCurrentPlanState(orgId, planId string, contexts []*Context) (*shared.CurrentPlanState, error) {+type CurrentPlanStateParams struct {+ OrgId string+ PlanId string+ Contexts []*Context+ PlanFileResults []*PlanFileResult+ PendingBuildDescriptions []*ConvoMessageDescription+}++func GetCurrentPlanState(params CurrentPlanStateParams) (*shared.CurrentPlanState, error) {+ orgId := params.OrgId+ planId := params.PlanIdvar dbPlanFileResults []*PlanFileResult- var latestBuildDesc *ConvoMessageDescription- errCh := make(chan error, 2)+ var pendingBuildDescriptions []*shared.ConvoMessageDescription+ var contexts []*Context++ errCh := make(chan error)go func() {- res, err := GetPlanFileResults(orgId, planId)- dbPlanFileResults = res+ if params.PlanFileResults == nil {+ res, err := GetPlanFileResults(orgId, planId)+ dbPlanFileResults = res- if err != nil {- errCh <- fmt.Errorf("error getting plan file results: %v", err)- return+ if err != nil {+ errCh <- fmt.Errorf("error getting plan file results: %v", err)+ return+ }+ } else {+ dbPlanFileResults = params.PlanFileResults}errCh <- nil}()go func() {- res, err := GetLatestBuildDescription(planId)- latestBuildDesc = res+ if params.PendingBuildDescriptions == nil {+ res, err := GetPendingBuildDescriptions(orgId, planId)+ if err != nil {+ errCh <- fmt.Errorf("error getting latest plan build description: %v", err)+ return+ }- if err != nil {- errCh <- fmt.Errorf("error getting latest plan build description: %v", err)- return+ for _, desc := range res {+ pendingBuildDescriptions = append(pendingBuildDescriptions, desc.ToApi())+ }+ } else {+ for _, desc := range params.PendingBuildDescriptions {+ pendingBuildDescriptions = append(pendingBuildDescriptions, desc.ToApi())+ }}errCh <- nil}()- for i := 0; i < 2; i++ {+ go func() {+ if params.Contexts == nil {+ res, err := GetPlanContexts(orgId, planId, true)+ if err != nil {+ errCh <- fmt.Errorf("error getting plan contexts: %v", err)+ return+ }+ contexts = res+ } else {+ contexts = params.Contexts+ }++ errCh <- nil+ }()++ for i := 0; i < 3; i++ {err := <-errChif err != nil {return nil, err@@ -101,10 +140,10 @@ func GetCurrentPlanState(orgId, planId string, contexts []*Context) (*shared.Cur}planState := &shared.CurrentPlanState{- PlanResult: planResult,- Contexts: apiContexts,- ContextsByPath: apiContextsByPath,- LatestBuildDescription: latestBuildDesc.ToApi(),+ PlanResult: planResult,+ Contexts: apiContexts,+ ContextsByPath: apiContextsByPath,+ PendingBuildDescriptions: pendingBuildDescriptions,}currentPlanFiles, err := planState.GetFiles()@@ -118,16 +157,56 @@ func GetCurrentPlanState(orgId, planId string, contexts []*Context) (*shared.Curreturn planState, nil}-func GetLatestBuildDescription(planId string) (*ConvoMessageDescription, error) {- var description ConvoMessageDescription+func GetPendingBuildDescriptions(orgId, planId string) ([]*ConvoMessageDescription, error) {+ descriptionsDir := getPlanDescriptionsDir(orgId, planId)- err := Conn.Get(&description, "SELECT id, org_id, plan_id, convo_message_id, summarized_to_message_created_at, made_plan, commit_msg, files, error, created_at, updated_at FROM convo_message_descriptions WHERE plan_id = $1 AND made_plan = true ORDER BY created_at DESC LIMIT 1", planId)+ files, err := os.ReadDir(descriptionsDir)if err != nil {- return nil, fmt.Errorf("error getting plan build description: %v", err)+ return nil, fmt.Errorf("error reading descriptions dir: %v", err)}- return &description, nil+ var descriptions []*ConvoMessageDescription+ errCh := make(chan error, len(files))+ descCh := make(chan *ConvoMessageDescription, len(files))++ for _, file := range files {+ go func(file os.DirEntry) {+ bytes, err := os.ReadFile(filepath.Join(descriptionsDir, file.Name()))++ if err != nil {+ errCh <- fmt.Errorf("error reading description file: %v", err)+ return+ }++ var description ConvoMessageDescription+ err = json.Unmarshal(bytes, &description)++ if err != nil {+ errCh <- fmt.Errorf("error unmarshalling description file: %v", err)+ return+ }++ descCh <- &description+ }(file)+ }++ for i := 0; i < len(files); i++ {+ select {+ case err := <-errCh:+ return nil, fmt.Errorf("error reading description files: %v", err)+ case description := <-descCh:+ if description.MadePlan && description.AppliedAt == nil {+ descriptions = append(descriptions, description)+ }+ }+ }++ sort.Slice(descriptions, func(i, j int) bool {+ return descriptions[i].CreatedAt.Before(descriptions[j].CreatedAt)+ })++ return descriptions, nil}func GetPlanFileResults(orgId, planId string) ([]*PlanFileResult, error) {@@ -214,77 +293,131 @@ func GetPlanResult(planFileResults []*shared.PlanFileResult) *shared.PlanResultFileResultsByPath: resByPath,SortedPaths: paths,ReplacementsByPath: replacementsByPath,+ Results: planFileResults,}}func ApplyPlan(orgId, planId string) error {resultsDir := getPlanResultsDir(orgId, planId)- files, err := os.ReadDir(resultsDir)+ errCh := make(chan error)++ var results []*PlanFileResult+ var pendingBuildDescriptions []*ConvoMessageDescription+ var contexts []*Context++ go func() {+ res, err := GetPlanFileResults(orgId, planId)+ if err != nil {+ errCh <- fmt.Errorf("error getting plan file results: %v", err)+ return+ }+ results = res+ errCh <- nil+ }()++ go func() {+ res, err := GetPendingBuildDescriptions(orgId, planId)+ if err != nil {+ errCh <- fmt.Errorf("error getting latest plan build description: %v", err)+ return+ }+ pendingBuildDescriptions = res+ errCh <- nil+ }()++ go func() {+ res, err := GetPlanContexts(orgId, planId, false)+ if err != nil {+ errCh <- fmt.Errorf("error getting plan contexts: %v", err)+ return+ }+ contexts = res+ errCh <- nil+ }()++ for i := 0; i < 3; i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error applying plan: %v", err)+ }+ }++ planState, err := GetCurrentPlanState(CurrentPlanStateParams{+ OrgId: orgId,+ PlanId: planId,+ Contexts: contexts,+ PlanFileResults: results,+ PendingBuildDescriptions: pendingBuildDescriptions,+ })if err != nil {- return fmt.Errorf("error reading results dir: %v", err)+ return fmt.Errorf("error getting current plan state: %v", err)}- errCh := make(chan error, len(files))+ var pendingDbResults []*PlanFileResult++ for _, result := range results {+ apiResult := result.ToApi()+ if apiResult.IsPending() {+ pendingDbResults = append(pendingDbResults, result)+ }+ }+ errCh = make(chan error, len(pendingDbResults)+len(pendingBuildDescriptions))now := time.Now()- for _, file := range files {- go func(file os.DirEntry) {+ for _, result := range pendingDbResults {+ go func(result *PlanFileResult) {+ result.AppliedAt = &now- bytes, err := os.ReadFile(filepath.Join(resultsDir, file.Name()))+ bytes, err := json.MarshalIndent(result, "", " ")if err != nil {- errCh <- fmt.Errorf("error reading result file: %v", err)+ errCh <- fmt.Errorf("error marshalling result: %v", err)return}- var result PlanFileResult- err = json.Unmarshal(bytes, &result)+ err = os.WriteFile(filepath.Join(resultsDir, result.Id+".json"), bytes, 0644)if err != nil {- errCh <- fmt.Errorf("error unmarshalling result file: %v", err)- return- }-- if result.AppliedAt != nil {- errCh <- nil+ errCh <- fmt.Errorf("error writing result file: %v", err)return}- result.AppliedAt = &now+ errCh <- nil- bytes, err = json.MarshalIndent(result, "", " ")+ }(result)+ }- if err != nil {- errCh <- fmt.Errorf("error marshalling result: %v", err)- return- }+ for _, description := range pendingBuildDescriptions {+ go func(description *ConvoMessageDescription) {+ description.AppliedAt = &now- err = os.WriteFile(filepath.Join(resultsDir, file.Name()), bytes, 0644)+ err := StoreDescription(description)if err != nil {- errCh <- fmt.Errorf("error writing result file: %v", err)+ errCh <- fmt.Errorf("error storing convo message description: %v", err)return}errCh <- nil-- }(file)+ }(description)}- for i := 0; i < len(files); i++ {+ for i := 0; i < len(pendingDbResults)+len(pendingBuildDescriptions); i++ {err := <-errChif err != nil {return fmt.Errorf("error applying plan: %v", err)}}- _, err = Conn.Exec("UPDATE plans SET applied_at = NOW() WHERE id = $1", planId)+ msg := "Marked pending results as applied." + "\n\n" + planState.PendingChangesSummary()++ err = GitAddAndCommit(orgId, planId, msg)if err != nil {- return fmt.Errorf("error updating plan: %v", err)+ return fmt.Errorf("error committing plan: %v", err)}return nildiff --git a/server/handlers/plans_changes.go b/server/handlers/plans_changes.goindex 4f2b216f..3805947a 100644--- a/server/handlers/plans_changes.go+++ b/server/handlers/plans_changes.go@@ -14,7 +14,7 @@ func CurrentPlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ListPlanChangesHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan access@@ -31,7 +31,11 @@ func CurrentPlanHandler(w http.ResponseWriter, r *http.Request) {return}- planState, err := db.GetCurrentPlanState(currentOrgId, planId, contexts)+ planState, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{+ OrgId: currentOrgId,+ PlanId: planId,+ Contexts: contexts,+ })if err != nil {log.Printf("Error getting current plan state: %v\n", err)@@ -56,7 +60,7 @@ func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ApplyPlanHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan access@@ -80,7 +84,7 @@ func RejectAllChangesHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for RejectAllChangesHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan access@@ -104,7 +108,7 @@ func RejectResultHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for RejectResultHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan access@@ -129,7 +133,7 @@ func RejectReplacementHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for RejectReplacementHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan accessdiff --git a/server/handlers/plans_context.go b/server/handlers/plans_context.goindex beca1d47..8adb3562 100644--- a/server/handlers/plans_context.go+++ b/server/handlers/plans_context.go@@ -11,6 +11,7 @@ import ("plandex-server/db""sync"+ "github.com/davecgh/go-spew/spew""github.com/google/uuid""github.com/gorilla/mux""github.com/plandex/plandex/shared"@@ -20,8 +21,8 @@ func ListContextHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ListContextHandler")// TODO: get from auth when implemented- // currentUserId := "user1"- currentOrgId := "org1"+ // currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"// TODO: authenticate user and plan accessvars := mux.Vars(r)@@ -58,8 +59,8 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for LoadContextHandler")// TODO: get from auth when implemented- currentUserId := "user1"- currentOrgId := "org1"+ currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]@@ -94,11 +95,11 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {tokensAdded := 0totalTokens := plan.ContextTokens- paramsById := make(map[string]*shared.LoadContextParams)- numTokensById := make(map[string]int)+ paramsByTempId := make(map[string]*shared.LoadContextParams)+ numTokensByTempId := make(map[string]int)for _, context := range requestBody {- id := uuid.New().String()+ tempId := uuid.New().String()numTokens, err := shared.GetNumTokens(context.Body)if err != nil {@@ -107,8 +108,8 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {return}- paramsById[id] = context- numTokensById[id] = numTokens+ paramsByTempId[tempId] = context+ numTokensByTempId[tempId] = numTokenstokensAdded += numTokenstotalTokens += numTokens@@ -136,14 +137,14 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {dbContextsCh := make(chan *db.Context)errCh := make(chan error)- for id, params := range paramsById {+ for tempId, params := range paramsByTempId {- go func(id string, params *shared.LoadContextParams) {+ go func(tempId string, params *shared.LoadContextParams) {hash := sha256.Sum256([]byte(params.Body))sha := hex.EncodeToString(hash[:])context := db.Context{- Id: id,+ // Id generated by db layerOrgId: currentOrgId,CreatorId: currentUserId,PlanId: planId,@@ -151,7 +152,7 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {Name: params.Name,Url: params.Url,FilePath: params.FilePath,- NumTokens: numTokensById[id],+ NumTokens: numTokensByTempId[tempId],Sha: sha,Body: params.Body,}@@ -165,7 +166,7 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {dbContextsCh <- &context- }(id, params)+ }(tempId, params)}var apiContexts []*shared.Context@@ -183,7 +184,7 @@ func LoadContextHandler(w http.ResponseWriter, r *http.Request) {}}- commitMsg := shared.SummaryForLoadContext(apiContexts) + "\n\n" + shared.TableForLoadContext(apiContexts)+ commitMsg := shared.SummaryForLoadContext(apiContexts, tokensAdded, totalTokens) + "\n\n" + shared.TableForLoadContext(apiContexts)err = db.GitAddAndCommit(currentOrgId, planId, commitMsg)if err != nil {@@ -222,8 +223,8 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for UpdateContextHandler")// TODO: get from auth when implemented- // currentUserId := "user1"- currentOrgId := "org1"+ // currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]@@ -270,9 +271,6 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {for id, params := range requestBody {go func(id string, params *shared.UpdateContextParams) {- mu.Lock()- defer mu.Unlock()-context, err := db.GetContext(currentOrgId, planId, id, true)if err != nil {@@ -280,20 +278,27 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {return}+ spew.Dump(context)++ mu.Lock()+ defer mu.Unlock()+contextsById[id] = contextupdatedContexts = append(updatedContexts, context.ToApi())updateNumTokens, err := shared.GetNumTokens(params.Body)- tokenDiff := updateNumTokens - context.NumTokensif err != nil {errCh <- fmt.Errorf("error getting num tokens: %v", err)return}- tokensDiffById[id] = updateNumTokens+ tokenDiff := updateNumTokens - context.NumTokens+ tokensDiffById[id] = tokenDifftokensDiff += tokenDifftotalTokens += tokenDiff+ context.NumTokens = updateNumTokens+switch context.ContextType {case shared.ContextFileType:numFiles++@@ -302,9 +307,20 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {case shared.ContextDirectoryTreeType:numTrees++}++ errCh <- nil}(id, params)}+ for i := 0; i < len(requestBody); i++ {+ err := <-errCh+ if err != nil {+ log.Printf("Error updating context: %v\n", err)+ http.Error(w, "Error updating context: "+err.Error(), http.StatusInternalServerError)+ return+ }+ }+if totalTokens > maxTokens {log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)res := shared.LoadContextResponse{@@ -326,8 +342,10 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {}errCh = make(chan error)+for id, params := range requestBody {go func(id string, params *shared.UpdateContextParams) {+context := contextsById[id]hash := sha256.Sum256([]byte(params.Body))@@ -335,7 +353,6 @@ func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {context.Body = params.Bodycontext.Sha = sha- context.NumTokens = tokensDiffById[id]errCh <- db.StoreContext(context)}(id, params)@@ -399,8 +416,8 @@ func DeleteContextHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for DeleteContextHandler")// TODO: get from auth when implemented- // currentUserId := "user1"- currentOrgId := "org1"+ // currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]diff --git a/server/handlers/plans_crud.go b/server/handlers/plans_crud.goindex 4ff33f58..544e6c90 100644--- a/server/handlers/plans_crud.go+++ b/server/handlers/plans_crud.go@@ -16,8 +16,8 @@ func CreatePlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for CreatePlanHandler")// TODO: get from auth when implemented- currentUserId := "user1"- currentOrgId := "org1"+ currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)projectId := vars["projectId"]@@ -133,7 +133,7 @@ func DeletePlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for DeletePlanHandler")// TODO: get from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]@@ -176,8 +176,8 @@ func DeleteAllPlansHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for DeleteAllPlansHandler")// TODO: get from auth when implemented- currentOrgId := "org1"- currentUserId := "user1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"+ currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"vars := mux.Vars(r)projectId := vars["projectId"]@@ -195,7 +195,7 @@ func DeleteAllPlansHandler(w http.ResponseWriter, r *http.Request) {func ListPlansHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ListPlansHandler")- currentUserId := "user1" // TODO: get from auth when implemented+ currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f" // TODO: get from auth when implementedvars := mux.Vars(r)projectId := vars["projectId"]diff --git a/server/handlers/plans_exec.go b/server/handlers/plans_exec.goindex 0b3c2312..ffbef332 100644--- a/server/handlers/plans_exec.go+++ b/server/handlers/plans_exec.go@@ -15,8 +15,8 @@ func TellPlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for TellPlanHandler")// TODO: get this from auth when implemented- currentOrgId := "org1"- currentUserId := "user1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"+ currentUserId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f"// TODO: authenticate user and plan accessdiff --git a/server/handlers/plans_versions.go b/server/handlers/plans_versions.goindex 73e7293f..3f3818d9 100644--- a/server/handlers/plans_versions.go+++ b/server/handlers/plans_versions.go@@ -15,7 +15,7 @@ func ListLogsHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ListLogsHandler")// TODO: get from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]@@ -52,7 +52,7 @@ func RewindPlanHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for RewindPlanHandler")// TODO: get from auth when implemented- currentOrgId := "org1"+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a"vars := mux.Vars(r)planId := vars["planId"]@@ -83,6 +83,14 @@ func RewindPlanHandler(w http.ResponseWriter, r *http.Request) {return}+ err = db.SyncPlanTokens(currentOrgId, planId)++ if err != nil {+ log.Println("Error syncing plan tokens: ", err)+ w.WriteHeader(http.StatusInternalServerError)+ return+ }+sha, latest, err := db.GetLatestCommit(currentOrgId, planId)if err != nil {diff --git a/server/handlers/projects.go b/server/handlers/projects.goindex fc80cce7..5aca5380 100644--- a/server/handlers/projects.go+++ b/server/handlers/projects.go@@ -15,6 +15,8 @@ import (func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for CreateProjectHandler")+ currentOrgId := "2ff5bc12-1160-4305-8707-9a165319de5a" // TODO: get from auth when implemented+// read the request bodybody, err := io.ReadAll(r.Body)if err != nil {@@ -39,7 +41,7 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {id := uuid.New().String()- _, err = db.Conn.Exec("INSERT INTO projects (id, name) VALUES ($1, $2)", id, requestBody.Name)+ _, err = db.Conn.Exec("INSERT INTO projects (id, org_id, name) VALUES ($1, $2, $3)", id, currentOrgId, requestBody.Name)if err != nil {log.Printf("Error creating project: %v\n", err)@@ -48,7 +50,8 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {}resp := shared.CreateProjectResponse{- Id: id,+ Id: id,+ OrgId: currentOrgId,}bytes, err := json.Marshal(resp)@@ -68,7 +71,7 @@ func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {func ListProjectsHandler(w http.ResponseWriter, r *http.Request) {log.Println("Received request for ListProjectsHandler")- userId := "user1" // TODO: get from auth when implemented+ userId := "bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f" // TODO: get from auth when implementedrows, err := db.Conn.Query("SELECT project.id, project.name FROM users_projects INNER JOIN project ON users_projects.project_id = project.id WHERE users_projects.user_id = $1", userId)diff --git a/server/migrations/2023120500_init.down.sql b/server/migrations/2023120500_init.down.sqlindex 983a128c..f5b58ed4 100644--- a/server/migrations/2023120500_init.down.sql+++ b/server/migrations/2023120500_init.down.sql@@ -5,5 +5,4 @@ DROP TABLE IF EXISTS users_projects;DROP TABLE IF EXISTS users;DROP TABLE IF EXISTS plans;DROP TABLE IF EXISTS convo_summaries;-DROP TABLE IF EXISTS convo_message_descriptions;DROP TABLE IF EXISTS plan_builds;diff --git a/server/migrations/2023120500_init.up.sql b/server/migrations/2023120500_init.up.sqlindex 157fb420..c851e2f2 100644--- a/server/migrations/2023120500_init.up.sql+++ b/server/migrations/2023120500_init.up.sql@@ -8,6 +8,18 @@ BEGINEND;$$ LANGUAGE plpgsql;+CREATE TABLE IF NOT EXISTS users (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ name VARCHAR(255) NOT NULL,+ email VARCHAR(255) NOT NULL,+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW()+);+CREATE TRIGGER update_users_modtime BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++-- Default user, remove when auth is implemented+INSERT INTO users (id, name, email) VALUES ('bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f', 'Default User', '[email protected]');+CREATE TABLE IF NOT EXISTS orgs (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),name VARCHAR(255) NOT NULL,@@ -19,6 +31,9 @@ CREATE TABLE IF NOT EXISTS orgs ();CREATE TRIGGER update_orgs_modtime BEFORE UPDATE ON orgs FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();+-- Default org, remove when auth is implemented+INSERT INTO orgs (id, name, creator_id) VALUES ('2ff5bc12-1160-4305-8707-9a165319de5a', 'Default Org', 'bc9c75ee-57b0-4552-aa1b-f80cf8c09f3f');+CREATE TABLE IF NOT EXISTS projects (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,@@ -28,15 +43,6 @@ CREATE TABLE IF NOT EXISTS projects ();CREATE TRIGGER update_projects_modtime BEFORE UPDATE ON projects FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();-CREATE TABLE IF NOT EXISTS users (- id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),- name VARCHAR(255) NOT NULL,- email VARCHAR(255) NOT NULL,- created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW()-);-CREATE TRIGGER update_users_modtime BEFORE UPDATE ON users FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();-CREATE TABLE IF NOT EXISTS orgs_users (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,@@ -46,30 +52,16 @@ CREATE TABLE IF NOT EXISTS orgs_users ();CREATE TRIGGER update_orgs_users_modtime BEFORE UPDATE ON orgs_users FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();-CREATE TABLE IF NOT EXISTS users_projects (- id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),- org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,- user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,- project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,- last_active_plan_id UUID REFERENCES plans(id),- created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW()-);-CREATE TRIGGER update_users_projects_modtime BEFORE UPDATE ON users_projects FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();--CREATE INDEX users_projects_idx ON users_projects(project_id);-CREATE TABLE IF NOT EXISTS plans (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,creator_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,- project_id UUID N OT NULL REFERENCES projects(id) ON DELETE CASCADE,+ project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,name VARCHAR(255) NOT NULL,- status VARCHAR(32),+ status VARCHAR(32) NOT NULL,error TEXT,context_tokens INTEGER NOT NULL DEFAULT 0,convo_tokens INTEGER NOT NULL DEFAULT 0,- applied_at TIMESTAMP,archived_at TIMESTAMP,created_at TIMESTAMP NOT NULL DEFAULT NOW(),updated_at TIMESTAMP NOT NULL DEFAULT NOW()@@ -79,11 +71,24 @@ CREATE TRIGGER update_plans_modtime BEFORE UPDATE ON plans FOR EACH ROW EXECUTECREATE INDEX plans_name_idx ON plans(project_id, creator_id, name);CREATE INDEX plans_archived_idx ON plans(project_id, creator_id, archived_at);+CREATE TABLE IF NOT EXISTS users_projects (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,+ user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,+ project_id UUID NOT NULL REFERENCES projects(id) ON DELETE CASCADE,+ last_active_plan_id UUID REFERENCES plans(id),+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW()+);+CREATE TRIGGER update_users_projects_modtime BEFORE UPDATE ON users_projects FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++CREATE INDEX users_projects_idx ON users_projects(project_id);+CREATE TABLE IF NOT EXISTS convo_summaries (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,plan_id UUID NOT NULL REFERENCES plans(id) ON DELETE CASCADE,- latest_convo_message_id UUID NOT NULL REFERENCES convo_messages(id),+ latest_convo_message_id UUID NOT NULL,latest_convo_message_created_at TIMESTAMP NOT NULL,summary TEXT NOT NULL,tokens INTEGER NOT NULL,@@ -91,31 +96,15 @@ CREATE TABLE IF NOT EXISTS convo_summaries (created_at TIMESTAMP NOT NULL DEFAULT NOW());-CREATE TABLE IF NOT EXISTS convo_message_descriptions (- id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),- org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,- plan_id UUID NOT NULL REFERENCES plans(id) ON DELETE CASCADE,- convo_message_id UUID NOT NULL REFERENCES convo_messages(id),- summarized_to_message_id UUID REFERENCES convo_messages(id),- made_plan BOOLEAN NOT NULL,- commit_msg TEXT,- files TEXT[] NOT NULL,- error TEXT,- created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW()-);-CREATE TRIGGER update_convo_message_descriptions_modtime BEFORE UPDATE ON convo_message_descriptions FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();-CREATE INDEX latest_build_description_idx ON convo_message_descriptions(plan_id, made_plan, created_at DESC);-CREATE TABLE IF NOT EXISTS plan_builds (id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),org_id UUID NOT NULL REFERENCES orgs(id) ON DELETE CASCADE,plan_id UUID NOT NULL REFERENCES plans(id) ON DELETE CASCADE,- convo_message_id UUID NOT NULL REFERENCES convo_messages(id),+ convo_message_id UUID NOT NULL,error TEXT,error_path TEXT,finished_at TIMESTAMP,created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW());CREATE TRIGGER update_plan_builds_modtime BEFORE UPDATE ON plan_builds FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();diff --git a/server/model/name.go b/server/model/name.gonew file mode 100644index 00000000..71cc1ea5--- /dev/null+++ b/server/model/name.go@@ -0,0 +1,67 @@+package model++import (+ "context"+ "encoding/json"+ "fmt"+ "plandex-server/model/prompts"++ "github.com/sashabaranov/go-openai"+)++func GenPlanName(planContent string) (string, error) {+ messages := []openai.ChatCompletionMessage{+ {+ Role: openai.ChatMessageRoleSystem,+ Content: prompts.SysPlanName,+ },+ }++ messages = append(messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: prompts.GetPlanNamePrompt(planContent),+ })++ resp, err := Client.CreateChatCompletion(+ context.Background(),+ openai.ChatCompletionRequest{+ Model: NameModel,+ Functions: []openai.FunctionDefinition{prompts.PlanNameFn},+ Messages: messages,+ ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},+ },+ )++ var res string+ var nameRes prompts.PlanNameRes++ if err != nil {+ fmt.Printf("Error during plan name model call: %v\n", err)+ return "", err+ }++ for _, choice := range resp.Choices {+ if choice.FinishReason == "function_call" &&+ choice.Message.FunctionCall != nil &&+ choice.Message.FunctionCall.Name == "describePlan" {+ fnCall := choice.Message.FunctionCall+ res = fnCall.Arguments+ }+ }++ if res == "" {+ fmt.Println("no describePlan function call found in response")+ return "", err+ }++ bytes := []byte(res)++ err = json.Unmarshal(bytes, &nameRes)+ if err != nil {+ fmt.Printf("Error unmarshalling plan description response: %v\n", err)+ return "", err+ }++ return nameRes.PlanName, nil++}diff --git a/server/model/plan/build.go b/server/model/plan/build.goindex bc2731b8..3de03178 100644--- a/server/model/plan/build.go+++ b/server/model/plan/build.go@@ -83,7 +83,11 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {}()go func() {- currentPlan, err := db.GetCurrentPlanState(currentOrgId, planId, active.Contexts)+ currentPlan, err := db.GetCurrentPlanState(db.CurrentPlanStateParams{+ OrgId: currentOrgId,+ PlanId: planId,+ Contexts: active.Contexts,+ })if err != nil {errCh <- fmt.Errorf("error getting current plan state: %v", err)return@@ -194,11 +198,12 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {// new fileplanRes := &db.PlanFileResult{- OrgId: currentOrgId,- PlanId: planId,- PlanBuildId: build.Id,- Path: filePath,- Content: fileContents[filePath],+ OrgId: currentOrgId,+ PlanId: planId,+ PlanBuildId: build.Id,+ ConvoMessageId: build.ConvoMessageId,+ Path: filePath,+ Content: fileContents[filePath],}onFinishBuildFile(filePath, planRes)return@@ -429,13 +434,14 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {planFileResult, allSucceeded := getPlanResult(planResultParams{- orgId: currentOrgId,- planId: planId,- planBuildId: build.Id,- filePath: filePath,- currentState: currentState,- context: contextPart,- replacements: streamed.Replacements,+ orgId: currentOrgId,+ planId: planId,+ planBuildId: build.Id,+ convoMessageId: build.ConvoMessageId,+ filePath: filePath,+ currentState: currentState,+ context: contextPart,+ replacements: streamed.Replacements,},)@@ -488,13 +494,14 @@ func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {}type planResultParams struct {- orgId string- planId string- planBuildId string- filePath string- currentState string- context *db.Context- replacements []*shared.Replacement+ orgId string+ planId string+ planBuildId string+ convoMessageId string+ filePath string+ currentState string+ context *db.Context+ replacements []*shared.Replacement}func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {@@ -526,13 +533,14 @@ func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {}return &db.PlanFileResult{- OrgId: orgId,- PlanId: planId,- PlanBuildId: planBuildId,- Content: "",- Path: filePath,- Replacements: replacements,- ContextSha: contextSha,- AnyFailed: !allSucceeded,+ OrgId: orgId,+ PlanId: planId,+ PlanBuildId: planBuildId,+ ConvoMessageId: params.convoMessageId,+ Content: "",+ Path: filePath,+ Replacements: replacements,+ ContextSha: contextSha,+ AnyFailed: !allSucceeded,}, allSucceeded}diff --git a/server/model/plan/stop.go b/server/model/plan/stop.goindex 2e3dd4de..550fdafc 100644--- a/server/model/plan/stop.go+++ b/server/model/plan/stop.go@@ -30,7 +30,7 @@ func Stop(planId, currentUserId, currentOrgId string) error {Message: content,}- err := db.StoreConvoMessage(&userMsg)+ _, err := db.StoreConvoMessage(&userMsg, true)if err != nil {return fmt.Errorf("error storing convo message: %v", err)diff --git a/server/model/plan/tell.go b/server/model/plan/tell.goindex 524c7814..7283553f 100644--- a/server/model/plan/tell.go+++ b/server/model/plan/tell.go@@ -47,28 +47,60 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlreturn}- modelContextCh := make(chan []*db.Context)- convoCh := make(chan []*db.ConvoMessage)- summariesCh := make(chan []*db.ConvoSummary)errCh := make(chan error)+ var modelContext []*db.Context+ var convo []*db.ConvoMessage+ var summaries []*db.ConvoSummary- go func(planId string) {- modelContext, err := db.GetPlanContexts(currentOrgId, planId, true)+ // get name for plan and rename it's a draft+ go func() {+ plan, err := db.GetPlan(planId)+ if err != nil {+ log.Printf("Error getting plan: %v\n", err)+ errCh <- fmt.Errorf("error getting plan: %v", err)+ return+ }++ if plan.Name == "draft" {+ name, err := model.GenPlanName(req.Prompt)++ if err != nil {+ log.Printf("Error generating plan name: %v\n", err)+ errCh <- fmt.Errorf("error generating plan name: %v", err)+ return+ }++ err = db.RenamePlan(planId, name)++ if err != nil {+ log.Printf("Error renaming plan: %v\n", err)+ errCh <- fmt.Errorf("error renaming plan: %v", err)+ return+ }+ }++ errCh <- nil+ }()++ go func() {+ res, err := db.GetPlanContexts(currentOrgId, planId, true)if err != nil {log.Printf("Error getting plan modelContext: %v\n", err)errCh <- fmt.Errorf("error getting plan modelContext: %v", err)return}- modelContextCh <- modelContext- }(planId)+ modelContext = res+ errCh <- nil+ }()- go func(planId, prompt string) {- convo, err := db.GetPlanConvo(currentOrgId, planId)+ go func() {+ res, err := db.GetPlanConvo(currentOrgId, planId)if err != nil {log.Printf("Error getting plan convo: %v\n", err)errCh <- fmt.Errorf("error getting plan convo: %v", err)return}+ convo = respromptTokens, err := shared.GetNumTokens(req.Prompt)if err != nil {@@ -87,7 +119,7 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlMessage: req.Prompt,}- err = db.StoreConvoMessage(&userMsg)+ _, err = db.StoreConvoMessage(&userMsg, true)if err != nil {log.Printf("Error storing user message: %v\n", err)@@ -95,31 +127,26 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlreturn}- convoCh <- convo- }(planId, req.Prompt)+ errCh <- nil+ }()- go func(planId string) {- summaries, err := db.GetPlanSummaries(planId)+ go func() {+ res, err := db.GetPlanSummaries(planId)if err != nil {log.Printf("Error getting plan summaries: %v\n", err)errCh <- fmt.Errorf("error getting plan summaries: %v", err)return}- summariesCh <- summaries- }(planId)+ summaries = res- var modelContext []*db.Context- var convo []*db.ConvoMessage- var summaries []*db.ConvoSummary+ errCh <- nil+ }()- for i := 0; i < 3; i++ {- select {- case err := <-errCh:- active.StreamDoneCh <- fmt.Errorf("error getting plan modelContext, convo, or summaries: %v", err)+ for i := 0; i < 4; i++ {+ err := <-errCh+ if err != nil {+ active.StreamDoneCh <- fmt.Errorf("error getting plan, context, convo, or summaries: %v", err)return- case modelContext = <-modelContextCh:- case convo = <-convoCh:- case summaries = <-summariesCh:}}@@ -297,7 +324,7 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlreturn}- storeAssistantReply := func() (*db.ConvoMessage, []string, error) {+ storeAssistantReply := func() (*db.ConvoMessage, []string, string, error) {files, _, _, replyNumTokens := replyInfo.FinishAndRead()assistantMsg := db.ConvoMessage{@@ -310,42 +337,58 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlMessage: Active.Get(planId).Content,}- err := db.StoreConvoMessage(&assistantMsg)+ commitMsg, err := db.StoreConvoMessage(&assistantMsg, false)if err != nil {log.Printf("Error storing assistant message: %v\n", err)- return nil, files, err+ return nil, files, "", err}Active.Update(planId, func(active *types.ActivePlan) {active.AssistantMessageId = assistantMsg.Id})- return &assistantMsg, files, err+ return &assistantMsg, files, commitMsg, err}- onError := func(err error, storeDesc bool, convoMessageId string) {- log.Printf("\nStream error: %v\n", err)- active.StreamDoneCh <- err+ onError := func(streamErr error, storeDesc bool, convoMessageId, commitMsg string) {+ log.Printf("\nStream error: %v\n", streamErr)+ active.StreamDoneCh <- streamErr++ storedMessage := false+ storedDesc := falseif convoMessageId == "" {- assistantMsg, _, err := storeAssistantReply()+ assistantMsg, _, msg, err := storeAssistantReply()if err == nil {convoMessageId = assistantMsg.Id+ commitMsg = msg+ storedMessage = true+ } else {+ log.Printf("Error storing assistant message after stream error: %v\n", err)}}- if storeDesc {+ if storeDesc && convoMessageId != "" {err = db.StoreDescription(&db.ConvoMessageDescription{OrgId: currentOrgId,PlanId: planId,SummarizedToMessageId: summarizedToMessageId,MadePlan: false,ConvoMessageId: convoMessageId,- Error: err.Error(),+ Error: streamErr.Error(),})+ if err == nil {+ storedDesc = true+ } else {+ log.Printf("Error storing description after stream error: %v\n", err)+ }+ }++ if storedMessage || storedDesc {+ err = db.GitAddAndCommit(currentOrgId, planId, commitMsg)if err != nil {- log.Printf("Error storing error state description: %v\n", err)+ log.Printf("Error committing after stream error: %v\n", err)}}}@@ -365,7 +408,7 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlcase <-timer.C:// Timer triggered because no new chunk was received in timelog.Println("\nStream timeout due to inactivity")- onError(fmt.Errorf("stream timeout due to inactivity"), true, "")+ onError(fmt.Errorf("stream timeout due to inactivity"), true, "", "")returndefault:response, err := stream.Recv()@@ -379,17 +422,17 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPl}if err != nil {- onError(fmt.Errorf("stream error: %v", err), true, "")+ onError(fmt.Errorf("stream error: %v", err), true, "", "")return}if len(response.Choices) == 0 {- onError(fmt.Errorf("stream finished with no choices"), true, "")+ onError(fmt.Errorf("stream finished with no choices"), true, "", "")return}if len(response.Choices) > 1 {- onError(fmt.Errorf("stream finished with more than one choice"), true, "")+ onError(fmt.Errorf("stream finished with more than one choice"), true, "", "")return}@@ -399,7 +442,7 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlactive.StreamCh <- shared.STREAM_DESCRIPTION_PHASEerr := db.SetPlanStatus(planId, shared.PlanStatusDescribing, "")if err != nil {- onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "")+ onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "", "")return}@@ -414,10 +457,10 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPl})}- assistantMsg, files, err := storeAssistantReply()+ assistantMsg, files, convoCommitMsg, err := storeAssistantReply()if err != nil {- onError(fmt.Errorf("failed to store assistant message: %v", err), true, "")+ onError(fmt.Errorf("failed to store assistant message: %v", err), true, "", "")return}@@ -438,7 +481,7 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPldescription, err = genPlanDescription(planId, active.Ctx)if err != nil {- onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id)+ onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id, convoCommitMsg)return}@@ -452,7 +495,13 @@ func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlerr = db.StoreDescription(description)if err != nil {- onError(fmt.Errorf("failed to store description: %v", err), false, "")+ onError(fmt.Errorf("failed to store description: %v", err), false, assistantMsg.Id, convoCommitMsg)+ return+ }++ err = db.GitAddAndCommit(currentOrgId, planId, convoCommitMsg)+ if err != nil {+ onError(fmt.Errorf("failed to commit: %v", err), false, assistantMsg.Id, convoCommitMsg)return}diff --git a/server/model/prompts/name.go b/server/model/prompts/name.goindex c0e289e8..7570e43a 100644--- a/server/model/prompts/name.go+++ b/server/model/prompts/name.go@@ -5,22 +5,26 @@ import ("github.com/sashabaranov/go-openai/jsonschema")-const SysFileName = "You are an AI namer that creates a valid filename for the content. Content can be any text, including programs/code, documentation, websites, and more. Most text will be related to software development. Call the 'nameFile' function with a valid JSON object that includes the 'fileName' key. Only call the 'nameFile' function in your response. Don't call any other function."+type PlanNameRes struct {+ PlanName string `json:"PlanName"`+}++const SysPlanName = "You are an AI namer that creates a name for the plan. Most plans will be related to software development. Call the 'namePlan' function with a valid JSON object that includes the 'planName' key. Only call the 'namePlan' function in your response. Don't call any other function."-var FileNameFn = openai.FunctionDefinition{- Name: "nameFile",+var PlanNameFn = openai.FunctionDefinition{+ Name: "namePlan",Parameters: &jsonschema.Definition{Type: jsonschema.Object,Properties: map[string]jsonschema.Definition{- "fileName": {+ "planName": {Type: jsonschema.String,- Description: "A *short* file name for the text based on the content. Use dashes as word separators. No spaces or special characters. **2-3 words max**.",+ Description: "A *short* lowercase file name for the plan content. Use dashes as word separators. No spaces, numbers, or special characters. **2-3 words max**.",},},- Required: []string{"fileName"},+ Required: []string{"planName"},},}-func GetFileNamePrompt(text string) string {- return SysFileName + "\n\nText:\n" + text+func GetPlanNamePrompt(text string) string {+ return SysPlanName + "\n\nContent:\n" + text}diff --git a/shared/context.go b/shared/context.goindex a686c00b..dae31018 100644--- a/shared/context.go+++ b/shared/context.go@@ -69,9 +69,7 @@ func TableForLoadContext(contexts []*Context) string {return tableString.String()}-func SummaryForLoadContext(contexts []*Context) string {- var tokensAdded int- var totalTokens int+func SummaryForLoadContext(contexts []*Context, tokensAdded, totalTokens int) string {var hasNote boolvar hasPiped bool@@ -81,9 +79,6 @@ func SummaryForLoadContext(contexts []*Context) string {var numUrls intfor _, context := range contexts {- tokensAdded += context.NumTokens- totalTokens += context.NumTokens-switch context.ContextType {case ContextFileType:numFiles++@@ -168,6 +163,8 @@ func TableForRemoveContext(contexts []*Context) string {})}+ table.Render()+return tableString.String()}@@ -185,7 +182,7 @@ func SummaryForRemoveContext(contexts []*Context, previousTotalTokens int) strinsuffix = "s"}- return fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ \n", len(contexts), suffix, removedTokens, totalTokens)+ return fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ", len(contexts), suffix, removedTokens, totalTokens)}func SummaryForUpdateContext(updateRes *ContextUpdateResult) string {diff --git a/shared/models.go b/shared/models.goindex 5a3312f2..9b3d5d3b 100644--- a/shared/models.go+++ b/shared/models.go@@ -22,6 +22,7 @@ type Project struct {type PlanStatus stringconst (+ PlanStatusDraft PlanStatus = "draft"PlanStatusReplying PlanStatus = "replying"PlanStatusDescribing PlanStatus = "describing"PlanStatusBuilding PlanStatus = "building"@@ -31,17 +32,15 @@ const ()type Plan struct {- Id string `json:"id"`- CreatorId string `json:"creatorId"`- Name string `json:"name"`- Status PlanStatus `json:"status"`- ContextTokens int `json:"contextTokens"`- ConvoTokens int `json:"convoTokens"`- ConvoSummaryTokens int `json:"convoSummaryTokens"`- AppliedAt *time.Time `json:"appliedAt,omitempty"`- ArchivedAt *time.Time `json:"archivedAt,omitempty"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ CreatorId string `json:"creatorId"`+ Name string `json:"name"`+ Status PlanStatus `json:"status"`+ ContextTokens int `json:"contextTokens"`+ ConvoTokens int `json:"convoTokens"`+ ArchivedAt *time.Time `json:"archivedAt,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}type ContextType string@@ -90,15 +89,16 @@ type ConvoSummary struct {}type ConvoMessageDescription struct {- Id string `json:"id"`- ConvoMessageId string `json:"convoMessageId"`- SummarizedToMessageId string `json:"summarizedToMessageId"`- MadePlan bool `json:"madePlan"`- CommitMsg string `json:"commitMsg"`- Files []string `json:"files"`- Error string `json:"error"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ ConvoMessageId string `json:"convoMessageId"`+ SummarizedToMessageId string `json:"summarizedToMessageId"`+ MadePlan bool `json:"madePlan"`+ CommitMsg string `json:"commitMsg"`+ Files []string `json:"files"`+ Error string `json:"error"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}type PlanBuild struct {@@ -120,17 +120,18 @@ type Replacement struct {}type PlanFileResult struct {- Id string `json:"id"`- PlanBuildId string `json:"planBuildId"`- Path string `json:"path"`- ContextSha string `json:"contextSha"`- Content string `json:"content"`- AnyFailed bool `json:"anyFailed"`- AppliedAt *time.Time `json:"appliedAt,omitempty"`- RejectedAt *time.Time `json:"rejectedAt,omitempty"`- Replacements []*Replacement `json:"replacements"`- CreatedAt time.Time `json:"createdAt"`- UpdatedAt time.Time `json:"updatedAt"`+ Id string `json:"id"`+ ConvoMessageId string `json:"convoMessageId"`+ PlanBuildId string `json:"planBuildId"`+ Path string `json:"path"`+ ContextSha string `json:"contextSha"`+ Content string `json:"content"`+ AnyFailed bool `json:"anyFailed"`+ AppliedAt *time.Time `json:"appliedAt,omitempty"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+ Replacements []*Replacement `json:"replacements"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`}type CurrentPlanFiles struct {@@ -143,13 +144,14 @@ type PlanFileResultsByPath map[string][]*PlanFileResulttype PlanResult struct {SortedPaths []string `json:"sortedPaths"`FileResultsByPath PlanFileResultsByPath `json:"fileResultsByPath"`+ Results []*PlanFileResult `json:"results"`ReplacementsByPath map[string][]*Replacement `json:"replacementsByPath"`}type CurrentPlanState struct {- PlanResult *PlanResult- CurrentPlanFiles *CurrentPlanFiles- Contexts []*Context- ContextsByPath map[string]*Context- LatestBuildDescription *ConvoMessageDescription+ PlanResult *PlanResult `json:"planResult"`+ CurrentPlanFiles *CurrentPlanFiles `json:"currentPlanFiles"`+ Contexts []*Context `json:"contexts"`+ ContextsByPath map[string]*Context `json:"contextsByPath"`+ PendingBuildDescriptions []*ConvoMessageDescription `json:"pendingBuildDescriptions"`}diff --git a/shared/plan_result.go b/shared/plan_result.goindex 221c47cc..4af73709 100644--- a/shared/plan_result.go+++ b/shared/plan_result.go@@ -2,6 +2,7 @@ package sharedimport ("fmt"+ "sort""strings""time")@@ -200,3 +201,78 @@ func (planState *CurrentPlanState) GetFilesBeforeReplacement(return &CurrentPlanFiles{Files: files, ContextShas: shas}, nil}++func (state *CurrentPlanState) PendingChangesSummary() string {+ var msgs []string++ for _, desc := range state.PendingBuildDescriptions {++ pendingNewFilesSet := make(map[string]bool)+ pendingReplacementPathsSet := make(map[string]bool)+ pendingReplacementsByPath := make(map[string][]*Replacement)++ for _, result := range state.PlanResult.Results {+ if result.ConvoMessageId == desc.ConvoMessageId && result.IsPending() {+ if len(result.Replacements) == 0 && result.Content != "" {+ pendingNewFilesSet[result.Path] = true+ } else {+ pendingReplacementPathsSet[result.Path] = true+ pendingReplacementsByPath[result.Path] = append(pendingReplacementsByPath[result.Path], result.Replacements...)+ }+ }+ }++ if len(pendingNewFilesSet) == 0 && len(pendingReplacementPathsSet) == 0 {+ continue+ }++ var pendingNewFiles []string+ var pendingReplacementPaths []string++ for path := range pendingNewFilesSet {+ pendingNewFiles = append(pendingNewFiles, path)+ }++ for path := range pendingReplacementPathsSet {+ pendingReplacementPaths = append(pendingReplacementPaths, path)+ }++ sort.Slice(pendingReplacementPaths, func(i, j int) bool {+ return pendingReplacementPaths[i] < pendingReplacementPaths[j]+ })++ sort.Slice(pendingNewFiles, func(i, j int) bool {+ return pendingNewFiles[i] < pendingNewFiles[j]+ })++ var descMsgs []string++ descMsgs = append(descMsgs, fmt.Sprintf("π %s", desc.CommitMsg))++ if len(pendingNewFiles) > 0 {+ newMsg := " π New files:\n\n"+ for _, path := range pendingNewFiles {+ newMsg += fmt.Sprintf(" β’ %s\n", path)+ }+ descMsgs = append(descMsgs, newMsg)+ }++ if len(pendingReplacementPaths) > 0 {+ updatesMsg := " βοΈ Edits:\n\n"++ for _, path := range pendingReplacementPaths {+ updatesMsg += fmt.Sprintf(" β’ %s\n", path)++ replacements := pendingReplacementsByPath[path]++ for _, replacement := range replacements {+ updatesMsg += fmt.Sprintf(" β %s\n", replacement.Summary)+ }+ }+ descMsgs = append(descMsgs, updatesMsg)+ }++ msgs = append(msgs, strings.Join(descMsgs, "\n\n"))+ }+ return strings.Join(msgs, "\n\n")+}diff --git a/shared/req_res.go b/shared/req_res.goindex 8067b6c1..589ed3db 100644--- a/shared/req_res.go+++ b/shared/req_res.go@@ -5,7 +5,8 @@ type CreateProjectRequest struct {}type CreateProjectResponse struct {- Id string `json:"id"`+ Id string `json:"id"`+ OrgId string `json:"orgId"`}type SetProjectPlanRequest struct {