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
lots of work toward cloud mode
Dane Schneiderc72587f9c0dd
diff --git a/cli/lib/api.go b/cli/api/client.gosimilarity index 52%rename from cli/lib/api.gorename to cli/api/client.goindex 9627302d..dada8eb6 100644--- a/cli/lib/api.go+++ b/cli/api/client.go@@ -1,16 +1,12 @@-package lib+package apiimport ("os"- "plandex/types")var apiHost string-// Check that API implements the types.APIHandler interface-var Api types.APIHandler = (*API)(nil)--type API struct{}+var Client ApiClient = (*Api)(nil)func init() {var port = os.Getenv("PORT")diff --git a/cli/api/methods.go b/cli/api/methods.gonew file mode 100644index 00000000..e4282756--- /dev/null+++ b/cli/api/methods.go@@ -0,0 +1,622 @@+package api++import (+ "bytes"+ "encoding/json"+ "fmt"+ "io"+ "net/http"++ "github.com/plandex/plandex/shared"+)++func (a *Api) CreateProject(req shared.CreateProjectRequest) (*shared.CreateProjectResponse, error) {+ serverUrl := apiHost + "/projects"+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("error marshalling request: %v", err)+ }++ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(reqBytes))+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error creating project: %d - %s", resp.StatusCode, string(errorBody))+ }++ var respBody shared.CreateProjectResponse+ err = json.NewDecoder(resp.Body).Decode(&respBody)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &respBody, nil+}++func (a *Api) ListProjects() ([]*shared.Project, error) {+ serverUrl := apiHost + "/projects"+ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing projects: %d - %s", resp.StatusCode, string(errorBody))+ }++ var projects []*shared.Project+ err = json.NewDecoder(resp.Body).Decode(&projects)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return projects, nil+}++func (a *Api) SetProjectPlan(projectId string, req shared.SetProjectPlanRequest) error {+ serverUrl := fmt.Sprintf("%s/projects/%s/set-plan", apiHost, projectId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return fmt.Errorf("error marshalling request: %v", err)+ }++ request, err := http.NewRequest(http.MethodPut, serverUrl, bytes.NewBuffer(reqBytes))+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }+ request.Header.Set("Content-Type", "application/json")++ resp, err := http.DefaultClient.Do(request)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error setting project plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) RenameProject(projectId string, req shared.RenameProjectRequest) error {+ serverUrl := fmt.Sprintf("%s/projects/%s/rename", apiHost, projectId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return fmt.Errorf("error marshalling request: %v", err)+ }++ request, err := http.NewRequest(http.MethodPut, serverUrl, bytes.NewBuffer(reqBytes))+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }+ request.Header.Set("Content-Type", "application/json")++ resp, err := http.DefaultClient.Do(request)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error renaming project: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) ListPlans(projectId string) ([]*shared.Plan, error) {+ serverUrl := fmt.Sprintf("%s/projects/%s/plans", apiHost, projectId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing plans: %d - %s", resp.StatusCode, string(errorBody))+ }++ var plans []*shared.Plan+ err = json.NewDecoder(resp.Body).Decode(&plans)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return plans, nil+}++func (a *Api) ListArchivedPlans(projectId string) ([]*shared.Plan, error) {+ serverUrl := fmt.Sprintf("%s/projects/%s/plans/archive", apiHost, projectId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing archived plans: %d - %s", resp.StatusCode, string(errorBody))+ }++ var plans []*shared.Plan+ err = json.NewDecoder(resp.Body).Decode(&plans)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return plans, nil+}++func (a *Api) ListPlansRunning(projectId string) ([]*shared.Plan, error) {+ serverUrl := fmt.Sprintf("%s/projects/%s/plans/ps", apiHost, projectId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing running plans: %d - %s", resp.StatusCode, string(errorBody))+ }++ var plans []*shared.Plan+ err = json.NewDecoder(resp.Body).Decode(&plans)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return plans, nil+}++func (a *Api) CreatePlan(projectId string, req shared.CreatePlanRequest) (*shared.CreatePlanResponse, error) {+ serverUrl := fmt.Sprintf("%s/projects/%s/plans", apiHost, projectId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("error marshalling request: %v", err)+ }++ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(reqBytes))+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error creating plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ var respBody shared.CreatePlanResponse+ err = json.NewDecoder(resp.Body).Decode(&respBody)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &respBody, nil+}++func (a *Api) GetPlan(planId string) (*shared.Plan, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s", apiHost, planId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }++ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error getting plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ var plan shared.Plan+ err = json.NewDecoder(resp.Body).Decode(&plan)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &plan, nil+}++func (a *Api) DeletePlan(planId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s", apiHost, planId)++ req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error deleting plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) DeleteAllPlans(projectId string) error {+ serverUrl := fmt.Sprintf("%s/projects/%s/plans", apiHost, projectId)++ req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error deleting all plans: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) TellPlan(planId string, req shared.TellPlanRequest, onStream OnStreamPlan) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/tell", apiHost, planId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return fmt.Errorf("error marshalling request: %v", err)+ }++ request, err := http.NewRequest(http.MethodPost, serverUrl, bytes.NewBuffer(reqBytes))+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }+ request.Header.Set("Content-Type", "application/json")++ resp, err := http.DefaultClient.Do(request)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error telling plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ if req.ConnectStream {+ connectPlanRespStream(resp.Body, onStream)+ }++ return nil+}++func (a *Api) ConnectPlan(planId string, onStream OnStreamPlan) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/connect", apiHost, planId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error connecting plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ connectPlanRespStream(resp.Body, onStream)++ return nil+}++func (a *Api) StopPlan(planId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/stop", apiHost, planId)++ req, err := http.NewRequest(http.MethodDelete, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error stopping plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) GetCurrentPlanState(planId string) (*shared.CurrentPlanState, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/current_plan", apiHost, planId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error getting current plan state: %d - %s", resp.StatusCode, string(errorBody))+ }++ var state shared.CurrentPlanState+ err = json.NewDecoder(resp.Body).Decode(&state)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &state, nil+}++func (a *Api) ApplyPlan(planId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/apply", apiHost, planId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error applying plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) ArchivePlan(planId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/archive", apiHost, planId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error archiving plan: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) RejectAllChanges(planId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/reject_all", apiHost, planId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error rejecting all changes: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) RejectResult(planId, resultId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/results/%s/reject", apiHost, planId, resultId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error rejecting result: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) RejectReplacement(planId, resultId, replacementId string) error {+ serverUrl := fmt.Sprintf("%s/plans/%s/results/%s/replacements/%s/reject", apiHost, planId, resultId, replacementId)++ req, err := http.NewRequest(http.MethodPatch, serverUrl, nil)+ if err != nil {+ return fmt.Errorf("error creating request: %v", err)+ }++ resp, err := http.DefaultClient.Do(req)+ if err != nil {+ return fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return fmt.Errorf("error rejecting replacement: %d - %s", resp.StatusCode, string(errorBody))+ }++ return nil+}++func (a *Api) LoadContext(planId string, req shared.LoadContextRequest) (*shared.LoadContextResponse, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/context", apiHost, planId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("error marshalling request: %v", err)+ }++ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(reqBytes))+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error loading context: %d - %s", resp.StatusCode, string(errorBody))+ }++ var loadContextResponse shared.LoadContextResponse+ err = json.NewDecoder(resp.Body).Decode(&loadContextResponse)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &loadContextResponse, nil+}++func (a *Api) UpdateContext(planId string, req shared.UpdateContextRequest) (*shared.UpdateContextResponse, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/context", apiHost, planId)++ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("error marshalling request: %v", err)+ }++ resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(reqBytes))+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error loading context: %d - %s", resp.StatusCode, string(errorBody))+ }++ var updateContextResponse shared.UpdateContextResponse+ err = json.NewDecoder(resp.Body).Decode(&updateContextResponse)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &updateContextResponse, nil+}++func (a *Api) DeleteContext(planId string, req shared.DeleteContextRequest) (*shared.DeleteContextResponse, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/context", apiHost, planId)+ reqBytes, err := json.Marshal(req)+ if err != nil {+ return nil, fmt.Errorf("error marshalling request: %v", err)+ }++ request, err := http.NewRequest(http.MethodDelete, 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)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error deleting context: %d - %s", resp.StatusCode, string(errorBody))+ }++ var deleteContextResponse shared.DeleteContextResponse+ err = json.NewDecoder(resp.Body).Decode(&deleteContextResponse)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return &deleteContextResponse, nil+}++func (a *Api) ListContext(planId string) ([]*shared.Context, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/context", apiHost, planId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing context: %d - %s", resp.StatusCode, string(errorBody))+ }++ var contexts []*shared.Context+ err = json.NewDecoder(resp.Body).Decode(&contexts)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return contexts, nil+}++func (a *Api) ListConvo(planId string) ([]*shared.ConvoMessage, error) {+ serverUrl := fmt.Sprintf("%s/plans/%s/convo", apiHost, planId)++ resp, err := http.Get(serverUrl)+ if err != nil {+ return nil, fmt.Errorf("error sending request: %v", err)+ }+ defer resp.Body.Close()++ if resp.StatusCode >= 400 {+ errorBody, _ := io.ReadAll(resp.Body)+ return nil, fmt.Errorf("error listing conversations: %d - %s", resp.StatusCode, string(errorBody))+ }++ var convos []*shared.ConvoMessage+ err = json.NewDecoder(resp.Body).Decode(&convos)+ if err != nil {+ return nil, fmt.Errorf("error decoding response: %v", err)+ }++ return convos, nil+}diff --git a/cli/api/stream.go b/cli/api/stream.gonew file mode 100644index 00000000..35266162--- /dev/null+++ b/cli/api/stream.go@@ -0,0 +1,77 @@+package api++import (+ "bufio"+ "bytes"+ "context"+ "fmt"+ "io"++ "github.com/plandex/plandex/shared"+)++func connectPlanRespStream(body io.ReadCloser, onStream OnStreamPlan) {+ streamState := shared.NewPlanStreamState()+ reader := bufio.NewReader(body)++ go func() {+ for {+ s, err := readUntilSeparator(reader, shared.STREAM_MESSAGE_SEPARATOR)+ if err != nil {+ fmt.Println("Error reading line:", err)+ streamState.Event(context.Background(), shared.EVENT_ERROR)+ onStream(OnStreamPlanParams{Content: "", State: streamState, Err: err})+ body.Close()+ return+ }++ if s == shared.STREAM_FINISHED || s == shared.STREAM_ABORTED {+ var evt string+ if s == shared.STREAM_FINISHED {+ evt = shared.EVENT_FINISH+ } else {+ evt = shared.STATE_ABORTED+ }+ err := streamState.Event(context.Background(), evt)+ if err != nil {+ fmt.Printf("Error triggering state change %s: %s\n", evt, err)+ }+ onStream(OnStreamPlanParams{Content: "", State: streamState, Err: err})+ body.Close()+ return+ }++ if s == shared.STREAM_DESCRIPTION_PHASE {+ err = streamState.Event(context.Background(), shared.EVENT_DESCRIBE)+ } else if s == shared.STREAM_BUILD_PHASE {+ err = streamState.Event(context.Background(), shared.EVENT_BUILD)+ } else if s == shared.STREAM_WRITE_PHASE {+ err = streamState.Event(context.Background(), shared.EVENT_WRITE)+ }++ if err != nil {+ fmt.Println("Error setting state:", err)+ onStream(OnStreamPlanParams{Content: "", State: streamState, Err: err})+ body.Close()+ return+ }++ onStream(OnStreamPlanParams{Content: s, State: streamState, Err: nil})+ }+ }()+}++func readUntilSeparator(reader *bufio.Reader, separator string) (string, error) {+ var result []byte+ sepBytes := []byte(separator)+ for {+ b, err := reader.ReadByte()+ if err != nil {+ return string(result), err+ }+ result = append(result, b)+ if len(result) >= len(sepBytes) && bytes.HasSuffix(result, sepBytes) {+ return string(result[:len(result)-len(separator)]), nil+ }+ }+}diff --git a/cli/api/types.go b/cli/api/types.gonew file mode 100644index 00000000..07958739--- /dev/null+++ b/cli/api/types.go@@ -0,0 +1,50 @@+package api++import (+ "github.com/looplab/fsm"+ "github.com/plandex/plandex/shared"+)++type OnStreamPlanParams struct {+ Content string+ State *fsm.FSM+ Err error+}++type OnStreamPlan func(params OnStreamPlanParams)++type Api struct{}++type ApiClient interface {+ CreateProject(req shared.CreateProjectRequest) (*shared.CreateProjectResponse, error)+ ListProjects() ([]*shared.Project, error)+ SetProjectPlan(projectId string, req shared.SetProjectPlanRequest) error+ RenameProject(projectId string, req shared.RenameProjectRequest) error++ ListPlans(projectId string) ([]*shared.Plan, error)+ ListArchivedPlans(projectId string) ([]*shared.Plan, error)+ ListPlansRunning(projectId string) ([]*shared.Plan, error)+ GetPlan(planId string) (*shared.Plan, error)+ CreatePlan(projectId string, req shared.CreatePlanRequest) (*shared.CreatePlanResponse, error)++ TellPlan(planId string, req shared.TellPlanRequest, onStreamPlan OnStreamPlan) error+ DeletePlan(planId string) error+ DeleteAllPlans(projectId string) error+ ConnectPlan(planId string, onStreamPlan OnStreamPlan) error+ StopPlan(planId string) error++ ArchivePlan(planId string) error++ GetCurrentPlanState(planId string) (*shared.CurrentPlanState, error)+ ApplyPlan(planId string) error+ RejectAllChanges(planId string) error+ RejectResult(planId, resultId string) error+ RejectReplacement(planId, resultId, replacementId string) error++ LoadContext(planId string, req shared.LoadContextRequest) (*shared.LoadContextResponse, error)+ UpdateContext(planId string, req shared.UpdateContextRequest) (*shared.UpdateContextResponse, error)+ DeleteContext(planId string, req shared.DeleteContextRequest) (*shared.DeleteContextResponse, error)+ ListContext(planId string) ([]*shared.Context, error)++ ListConvo(planId string) ([]*shared.ConvoMessage, error)+}diff --git a/cli/changes_tui/actions.go b/cli/changes_tui/actions.goindex d55b90a9..815a1eb2 100644--- a/cli/changes_tui/actions.go+++ b/cli/changes_tui/actions.go@@ -3,6 +3,7 @@ package changes_tuiimport ("fmt""log"+ "plandex/api""plandex/lib""strings"@@ -18,7 +19,7 @@ func (m *changesUIModel) rejectChange() {return}- err := lib.DropChange(lib.CurrentPlanName, m.selectionInfo.currentPath, m.selectionInfo.currentRep)+ err := api.Client.RejectReplacement(lib.CurrentPlanId, m.selectionInfo.currentRes.Id, m.selectionInfo.currentRep.Id)if err != nil {log.Printf("error dropping change: %v", err)@@ -51,7 +52,7 @@ func (m *changesUIModel) left() {}func (m *changesUIModel) right() {- paths := m.currentPlan.SortedPaths+ paths := m.currentPlan.PlanResult.SortedPathsif m.selectedFileIndex < len(paths)-1 {m.selectedFileIndex++@@ -78,7 +79,7 @@ func (m *changesUIModel) down() {// allow for selection of 'full file' option at bottom of replacement list in sidebarmax := len(currentReplacements) - 1- if m.selectionInfo.currentPlanBeforeReplacement.NumPendingForPath(m.selectionInfo.currentPath) > 0 {+ if m.currentPlan.PlanResult.NumPendingForPath(m.selectionInfo.currentPath) > 0 {max++}diff --git a/cli/changes_tui/main_viewport.go b/cli/changes_tui/main_viewport.goindex e5fea23c..21d5b13f 100644--- a/cli/changes_tui/main_viewport.go+++ b/cli/changes_tui/main_viewport.go@@ -74,7 +74,7 @@ func (m changesUIModel) renderMainViewHeader() string {var header stringif m.selectedFullFile() {- numChanges := m.selectionInfo.currentPlanBeforeReplacement.NumPendingForPath(m.selectionInfo.currentPath)+ numChanges := m.currentPlan.PlanResult.NumPendingForPath(m.selectionInfo.currentPath)if numChanges > 0 {suffix := "s"diff --git a/cli/changes_tui/model.go b/cli/changes_tui/model.goindex dc2490f0..ef5232f7 100644--- a/cli/changes_tui/model.go+++ b/cli/changes_tui/model.go@@ -3,13 +3,14 @@ package changes_tuiimport ("fmt""log"+ "plandex/api""plandex/lib"- "plandex/types""github.com/charmbracelet/bubbles/help"bubbleKey "github.com/charmbracelet/bubbles/key""github.com/charmbracelet/bubbles/viewport"tea "github.com/charmbracelet/bubbletea"+ "github.com/plandex/plandex/shared")type changesUIModel struct {@@ -18,7 +19,7 @@ type changesUIModel struct {selectedFileIndex intselectedReplacementIndex intselectedViewport int- currentPlan *types.CurrentPlanState+ currentPlan *shared.CurrentPlanStatechangeOldViewport viewport.ModelchangeNewViewport viewport.ModelfileViewport viewport.Model@@ -52,7 +53,7 @@ func (m changesUIModel) Init() tea.Cmd {}func initialModel() *changesUIModel {- currentPlan, err := lib.GetCurrentPlanState()+ currentPlan, err := api.Client.GetCurrentPlanState(lib.CurrentPlanId)if err != nil {err = fmt.Errorf("error getting current plan state: %v", err)log.Println(err)diff --git a/cli/changes_tui/path_tabs.go b/cli/changes_tui/path_tabs.goindex 0aae64c8..42fdf6e4 100644--- a/cli/changes_tui/path_tabs.go+++ b/cli/changes_tui/path_tabs.go@@ -17,7 +17,7 @@ func (m changesUIModel) renderPathTabs() string {lineWidth := 0lineNum := 0- paths := m.currentPlan.SortedPaths+ paths := m.currentPlan.PlanResult.SortedPathsfor i, path := range paths {selected := i == m.selectedFileIndex@@ -28,7 +28,7 @@ func (m changesUIModel) renderPathTabs() string {tab := " π " + path + " "- results := m.currentPlan.PlanResByPath[path]+ results := m.currentPlan.PlanResult.FileResultsByPath[path]pathColor := color.FgHiGreenbgColor := color.BgGreenanyFailed := falsediff --git a/cli/changes_tui/replace.go b/cli/changes_tui/replace.goindex d4be017a..b8921467 100644--- a/cli/changes_tui/replace.go+++ b/cli/changes_tui/replace.go@@ -21,7 +21,7 @@ type oldReplacementRes struct {func (m changesUIModel) getReplacementOldDisplay() oldReplacementRes {oldContent := m.selectionInfo.currentRep.Old- originalFile := m.selectionInfo.currentPlanBeforeReplacement.CurrentPlanFiles.Files[m.selectionInfo.currentPath]+ originalFile := m.selectionInfo.currentFilesBeforeReplacement.Files[m.selectionInfo.currentPath]// log.Printf("oldContent: %v", oldContent)// log.Printf("originalFile: %v", originalFile)diff --git a/cli/changes_tui/run.go b/cli/changes_tui/run.goindex 13463852..51b979a6 100644--- a/cli/changes_tui/run.go+++ b/cli/changes_tui/run.go@@ -2,6 +2,7 @@ package changes_tuiimport ("fmt"+ "plandex/api""plandex/lib"tea "github.com/charmbracelet/bubbletea"@@ -10,7 +11,7 @@ import (func StartChangesUI() error {initial := initialModel()- if len(initial.currentPlan.SortedPaths) == 0 {+ if len(initial.currentPlan.PlanResult.SortedPaths) == 0 {fmt.Println("π€·ββοΈ No changes pending")return nil}@@ -22,13 +23,13 @@ func StartChangesUI() error {}if m.(changesUIModel).shouldApplyAll {- err := lib.ApplyPlanWithOutput(lib.CurrentPlanName, false)+ err := lib.ApplyPlanWithOutput(lib.CurrentPlanId, false)if err != nil {return fmt.Errorf("error applying plan: %v", err)}} else if m.(changesUIModel).shouldRejectAll {- err := lib.DropChangesWithOutput(lib.CurrentPlanName)+ err := api.Client.RejectAllChanges(lib.CurrentPlanId)if err != nil {return fmt.Errorf("error rejecting plan: %v", err)diff --git a/cli/changes_tui/selection.go b/cli/changes_tui/selection.goindex b0455996..aa5b0856 100644--- a/cli/changes_tui/selection.go+++ b/cli/changes_tui/selection.go@@ -3,27 +3,25 @@ package changes_tuiimport ("fmt""log"- "plandex/lib"- "plandex/types""github.com/plandex/plandex/shared")type selectionInfo struct {- currentPath string- currentRes *shared.PlanResult- currentReplacements []*shared.Replacement- currentRep *shared.Replacement- currentPlanBeforeReplacement *types.CurrentPlanState+ currentPath string+ currentRes *shared.PlanFileResult+ currentReplacements []*shared.Replacement+ currentRep *shared.Replacement+ currentFilesBeforeReplacement *shared.CurrentPlanFiles}func (m *changesUIModel) setSelectionInfo() {- paths := m.currentPlan.SortedPaths+ paths := m.currentPlan.PlanResult.SortedPathscurrentPath := paths[m.selectedFileIndex]- results := m.currentPlan.PlanResByPath[currentPath]+ results := m.currentPlan.PlanResult.FileResultsByPath[currentPath]- var currentRes *shared.PlanResult+ var currentRes *shared.PlanFileResultvar currentRep *shared.Replacementvar pathReplacements []*shared.Replacement@@ -40,13 +38,13 @@ func (m *changesUIModel) setSelectionInfo() {}}- var currentPlanAtReplacement *types.CurrentPlanState+ var currentFilesBeforeReplacement *shared.CurrentPlanFilesvar err errorif currentRep == nil {- currentPlanAtReplacement = m.currentPlan+ currentFilesBeforeReplacement = m.currentPlan.CurrentPlanFiles} else {- currentPlanAtReplacement, err = lib.GetCurrentPlanStateBeforeReplacement(currentRep.Id)+ currentFilesBeforeReplacement, err = m.currentPlan.GetFilesBeforeReplacement(currentRep.Id)}if err != nil {err = fmt.Errorf("error getting current plan state: %v", err)@@ -55,10 +53,10 @@ func (m *changesUIModel) setSelectionInfo() {}m.selectionInfo = &selectionInfo{- currentPath: currentPath,- currentRes: currentRes,- currentReplacements: pathReplacements,- currentRep: currentRep,- currentPlanBeforeReplacement: currentPlanAtReplacement,+ currentPath: currentPath,+ currentRes: currentRes,+ currentReplacements: pathReplacements,+ currentRep: currentRep,+ currentFilesBeforeReplacement: currentFilesBeforeReplacement,}}diff --git a/cli/changes_tui/sidebar.go b/cli/changes_tui/sidebar.goindex 13d68f20..80f335e8 100644--- a/cli/changes_tui/sidebar.go+++ b/cli/changes_tui/sidebar.go@@ -17,7 +17,7 @@ func (m changesUIModel) renderSidebar() string {var sb strings.Builderpath := m.selectionInfo.currentPath- results := m.currentPlan.PlanResByPath[path]+ results := m.currentPlan.PlanResult.FileResultsByPath[path]anyFailed := falseanyApplied := falseanyReplacements := false@@ -36,13 +36,13 @@ func (m changesUIModel) renderSidebar() string {fgColor = color.FgHiRedbgColor = color.BgRedanyFailed = true- } else if rep.RejectedAt != "" {+ } else if rep.RejectedAt != nil {fgColor = color.FgWhitebgColor = color.BgBlack}var icon string- if rep.RejectedAt != "" {+ if rep.RejectedAt != nil {icon = "π"} else if rep.Failed {icon = "π«"@@ -50,7 +50,7 @@ func (m changesUIModel) renderSidebar() string {icon = "π"}- if !rep.Failed && rep.RejectedAt == "" {+ if !rep.Failed && rep.RejectedAt == nil {anyApplied = true}diff --git a/cli/changes_tui/view.go b/cli/changes_tui/view.goindex ff6b61cf..60441495 100644--- a/cli/changes_tui/view.go+++ b/cli/changes_tui/view.go@@ -100,7 +100,7 @@ func (m *changesUIModel) updateViewportSizes() {func (m changesUIModel) renderHelp() string {help := " "- if len(m.currentPlan.SortedPaths) > 1 {+ if len(m.currentPlan.PlanResult.SortedPaths) > 1 {help += "(β/β) select file β’ "}diff --git a/cli/cmd/apply.go b/cli/cmd/apply.goindex 80e264f6..301feb5c 100644--- a/cli/cmd/apply.go+++ b/cli/cmd/apply.go@@ -1,9 +1,13 @@package cmdimport (+ "fmt"+ "os"+ "plandex/api""plandex/lib"- "strings"+ "strconv"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -15,7 +19,7 @@ func init() {}var applyCmd = &cobra.Command{- Use: "apply [name]",+ Use: "apply [name-or-index]",Aliases: []string{"ap"},Short: "Apply a plan to the project",Args: cobra.MaximumNArgs(1),@@ -23,16 +27,64 @@ var applyCmd = &cobra.Command{}func apply(cmd *cobra.Command, args []string) error {- var name string+ lib.MustResolveProject()- if len(args) > 0 {- name = args[0]- name = strings.TrimSpace(name)- }+ var planId string++ if len(args) == 0 {+ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")+ os.Exit(1)+ }++ planId = lib.CurrentPlanId+ } else {+ nameOrIdx := args[0]++ if nameOrIdx == "current" {+ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")+ os.Exit(1)+ }++ planId = lib.CurrentPlanId+ } else {+ var plan *shared.Plan++ plans, err := api.Client.ListPlans(lib.CurrentProjectId)++ if err != nil {+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ os.Exit(1)+ }++ // see if it's an index+ idx, err := strconv.Atoi(nameOrIdx)++ if err == nil {+ if idx > 0 && idx <= len(plans) {+ plan = plans[idx-1]+ } else {+ fmt.Fprintln(os.Stderr, "Error: index out of range")+ os.Exit(1)+ }+ } else {+ for _, p := range plans {+ if p.Name == nameOrIdx {+ plan = p+ break+ }+ }++ if plan == nil {+ fmt.Printf("π€·ββοΈ Plan with name '%s' doesn't exist\n", nameOrIdx)+ os.Exit(1)+ }+ }- if name == "" || name == "current" {- name = lib.CurrentPlanName+ planId = plan.Id+ }}- return lib.ApplyPlanWithOutput(name, autoConfirm)+ return lib.ApplyPlanWithOutput(planId, autoConfirm)}diff --git a/cli/cmd/cd.go b/cli/cmd/cd.goindex 79170cb7..c027ddf6 100644--- a/cli/cmd/cd.go+++ b/cli/cmd/cd.go@@ -3,10 +3,12 @@ package cmdimport ("fmt""os"- "path/filepath"+ "plandex/api""plandex/lib""strconv"+ "time"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -23,38 +25,54 @@ var cdCmd = &cobra.Command{}func cd(cmd *cobra.Command, args []string) {- plandexDir, _, err := lib.FindOrCreatePlandex()- if err != nil {- fmt.Fprintln(os.Stderr, "Error:", err)- return- }+ lib.MustResolveProject()nameOrIdx := args[0]- var name string+ var plan *shared.Plan++ plans, err := api.Client.ListPlans(lib.CurrentProjectId)++ if err != nil {+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ os.Exit(1)+ }// see if it's an index- if idx, err := strconv.Atoi(nameOrIdx); err == nil {- plans, err := lib.GetPlans()- if err != nil {- fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ idx, err := strconv.Atoi(nameOrIdx)++ if err == nil {+ if idx > 0 && idx <= len(plans) {+ plan = plans[idx-1]+ } else {+ fmt.Fprintln(os.Stderr, "Error: index out of range")+ os.Exit(1)}- plan := plans[idx-1]- name = plan.Name} else {- name = nameOrIdx- }-- planDir := filepath.Join(plandexDir, name)+ for _, p := range plans {+ if p.Name == nameOrIdx {+ plan = p+ break+ }+ }- if _, err := os.Stat(planDir); os.IsNotExist(err) {- fmt.Fprintln(os.Stderr, "π€·ββοΈ Plan '"+name+"' does not exist")- os.Exit(1)+ if plan == nil {+ fmt.Fprintln(os.Stderr, "Error: plan not found")+ os.Exit(1)+ }}- err = lib.SetCurrentPlan(name)+ err = lib.SetCurrentPlan(plan.Id)if err != nil {fmt.Fprintln(os.Stderr, "Error setting current plan:", err)os.Exit(1)}++ // fire and forget SetProjectPlan request (we don't care about the response or errors)+ // this only matters for setting the current plan on a new device (i.e. when the current plan is not set)+ go api.Client.SetProjectPlan(lib.CurrentProjectId, shared.SetProjectPlanRequest{PlanId: plan.Id})++ // give the SetProjectPlan request some time to be sent before exiting+ time.Sleep(50 * time.Millisecond)+fmt.Fprintln(os.Stderr, "β Changed current plan to "+name)}diff --git a/cli/cmd/changes.go b/cli/cmd/changes.goindex adb3010d..2a7aed3d 100644--- a/cli/cmd/changes.go+++ b/cli/cmd/changes.go@@ -4,6 +4,7 @@ import ("fmt""os""plandex/changes_tui"+ "plandex/lib""github.com/spf13/cobra")@@ -20,6 +21,7 @@ var changesCmd = &cobra.Command{}func changes(cmd *cobra.Command, args []string) {+ lib.MustResolveProject()err := changes_tui.StartChangesUI()diff --git a/cli/cmd/clear.go b/cli/cmd/clear.goindex 5733d3b2..3205c059 100644--- a/cli/cmd/clear.go+++ b/cli/cmd/clear.go@@ -3,6 +3,7 @@ package cmdimport ("fmt""os"+ "plandex/api""plandex/lib""github.com/plandex/plandex/shared"@@ -17,51 +18,40 @@ var clearCmd = &cobra.Command{}func clearAllContext(cmd *cobra.Command, args []string) {- // clear all files from context dir- context, err := lib.GetAllContext(true)+ lib.MustResolveProject()- if err != nil {- fmt.Fprintln(os.Stderr, "Error retrieving context:", err)+ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")return}- toRemovePaths := []string{}- for _, part := range context {- path := lib.CreateContextFileName(part.Name, part.Sha)- toRemovePaths = append(toRemovePaths, path)- }-- err = lib.ContextRemoveFiles(toRemovePaths)- if err != nil {- fmt.Fprintln(os.Stderr, "Error removing context:", err)- return- }+ contexts, err := api.Client.ListContext(lib.CurrentPlanId)- // update plan state with new token count- planState, err := lib.GetPlanState()if err != nil {- fmt.Fprintln(os.Stderr, "Error retrieving plan state:", err)+ fmt.Fprintln(os.Stderr, "Error retrieving context:", err)return}- planState.ContextTokens = 0- planState.ContextUpdatableTokens = 0- err = lib.SetPlanState(planState, shared.StringTs())+ deleteIds := map[string]bool{}- if err != nil {- fmt.Fprintln(os.Stderr, "Error writing plan state:", err)- return+ for _, context := range contexts {+ deleteIds[context.Id] = true}- msg := "Context cleared"- err = lib.GitCommitContextUpdate(msg)+ if len(deleteIds) > 0 {+ res, err := api.Client.DeleteContext(lib.CurrentPlanId, shared.DeleteContextRequest{+ Ids: deleteIds,+ })- if err != nil {- fmt.Fprintln(os.Stderr, "Error committing context update:", err)- return- }+ if err != nil {+ fmt.Fprintln(os.Stderr, "Error deleting context:", err)+ return+ }- fmt.Println("β " + msg)+ fmt.Println("β " + res.Msg)+ } else {+ fmt.Println("π€·ββοΈ No context removed")+ }}diff --git a/cli/cmd/continue.go b/cli/cmd/continue.goindex 69fa6eef..e7be02b8 100644--- a/cli/cmd/continue.go+++ b/cli/cmd/continue.go@@ -24,6 +24,8 @@ func init() {}func next(cmd *cobra.Command, args []string) {+ lib.MustResolveProject()+err := lib.Propose(continuePrompt)if err != nil {fmt.Fprintln(os.Stderr, "Prompt error:", err)diff --git a/cli/cmd/convo.go b/cli/cmd/convo.goindex 83176fd9..592b2837 100644--- a/cli/cmd/convo.go+++ b/cli/cmd/convo.go@@ -3,13 +3,13 @@ package cmdimport ("fmt""os"+ "plandex/api""plandex/lib""plandex/term""strings""time""github.com/fatih/color"- "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -25,7 +25,9 @@ func init() {}func convo(cmd *cobra.Command, args []string) {- conversation, err := lib.LoadConversation()+ lib.MustResolveProject()++ conversation, err := api.Client.ListConvo(lib.CurrentPlanId)if err != nil {fmt.Fprintln(os.Stderr, "Error loading conversation:", err)return@@ -40,36 +42,33 @@ func convo(cmd *cobra.Command, args []string) {var totalTokens intfor i, msg := range conversation {var author string- if msg.Message.Role == "assistant" {+ if msg.Role == "assistant" {author = "π€ Plandex"- } else if msg.Message.Role == "user" {+ if msg.Stopped {+ author += " | π stopped early"+ }+ } else if msg.Role == "user" {author = "π¬ You"} else {- author = msg.Message.Role- }-- dt, err := time.Parse(shared.TsFormat, msg.Timestamp)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- continue+ author = msg.Role}// format as above but start with day of week- formattedTs := dt.Local().Format("Mon Jan 2, 2006 | 3:04:05pm MST")+ formattedTs := msg.CreatedAt.Local().Format("Mon Jan 2, 2006 | 3:04:05pm MST")// if it's today then use 'Today' instead of the date- if dt.Day() == time.Now().Day() {- formattedTs = dt.Local().Format("Today | 3:04:05pm MST")+ if msg.CreatedAt.Day() == time.Now().Day() {+ formattedTs = msg.CreatedAt.Local().Format("Today | 3:04:05pm MST")}// if it's yesterday then use 'Yesterday' instead of the date- if dt.Day() == time.Now().AddDate(0, 0, -1).Day() {- formattedTs = dt.Local().Format("Yesterday | 3:04:05pm MST")+ if msg.CreatedAt.Day() == time.Now().AddDate(0, 0, -1).Day() {+ formattedTs = msg.CreatedAt.Local().Format("Yesterday | 3:04:05pm MST")}header := fmt.Sprintf("#### %d | %s | %s | %d πͺ", i+1,author, formattedTs, msg.Tokens)- convMarkdown = append(convMarkdown, header, msg.Message.Content, "")+ convMarkdown = append(convMarkdown, header, msg.Message, "")totalTokens += msg.Tokens}diff --git a/cli/cmd/current.go b/cli/cmd/current.goindex a667f64f..5a3c8b9e 100644--- a/cli/cmd/current.go+++ b/cli/cmd/current.go@@ -3,15 +3,14 @@ package cmdimport ("fmt""os"+ "plandex/api""plandex/format""plandex/lib""plandex/term""strconv"- "time""github.com/fatih/color""github.com/olekukonko/tablewriter"- "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -27,14 +26,16 @@ func init() {}func current(cmd *cobra.Command, args []string) {- if lib.CurrentPlanName == "" {+ lib.MustResolveProject()++ if lib.CurrentPlanId == "" {fmt.Println("π€·ββοΈ No current plan")return}- planState, err := lib.GetPlanState()+ plan, err := api.Client.GetPlan(lib.CurrentPlanId)if err != nil {- fmt.Println("Error getting plan state:", err)+ fmt.Println("Error getting plan:", err)return}@@ -42,43 +43,18 @@ func current(cmd *cobra.Command, args []string) {table.SetAutoWrapText(false)table.SetHeader([]string{"Current Plan", "Updated", "Created", "Context", "Convo"})- var name string- if planState.Name == lib.CurrentPlanName {- name = color.New(color.Bold, color.FgGreen).Sprint(planState.Name)- } else {- name = planState.Name- }-- createdAt, err := time.Parse(shared.TsFormat, planState.CreatedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- return- }-- updatedAt, err := time.Parse(shared.TsFormat, planState.UpdatedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- return- }+ name := color.New(color.Bold, color.FgGreen).Sprint(plan.Name)row := []string{name,- format.Time(updatedAt),- format.Time(createdAt),- strconv.Itoa(planState.ContextTokens) + " πͺ",- strconv.Itoa(planState.ConvoTokens) + " πͺ",+ format.Time(plan.UpdatedAt),+ format.Time(plan.CreatedAt),+ strconv.Itoa(plan.ContextTokens) + " πͺ",+ strconv.Itoa(plan.ConvoTokens) + " πͺ",}- var style []tablewriter.Colors- if planState.Name == lib.CurrentPlanName {- style = []tablewriter.Colors{- {tablewriter.FgGreenColor, tablewriter.Bold},- }- } else {- style = []tablewriter.Colors{- {tablewriter.FgHiWhiteColor, tablewriter.Bold},- {tablewriter.FgHiWhiteColor},- }+ style := []tablewriter.Colors{+ {tablewriter.FgGreenColor, tablewriter.Bold},}table.Rich(row, style)diff --git a/cli/cmd/delete_plan.go b/cli/cmd/delete_plan.goindex 6e083d15..65b3a0c3 100644--- a/cli/cmd/delete_plan.go+++ b/cli/cmd/delete_plan.go@@ -3,12 +3,13 @@ package cmdimport ("fmt""os"- "path/filepath""strconv"+ "plandex/api""plandex/lib""github.com/fatih/color"+ "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -29,73 +30,69 @@ var rmCmd = &cobra.Command{}func del(cmd *cobra.Command, args []string) {+ lib.MustResolveProject()+if all {delAll()return}- plandexDir, _, err := lib.FindOrCreatePlandex()+ nameOrIdx := args[0]+ var plan *shared.Plan++ plans, err := api.Client.ListPlans(lib.CurrentProjectId)if err != nil {- fmt.Fprintln(os.Stderr, "Error:", err)- return+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ os.Exit(1)}- nameOrIdx := args[0]- var name string-// see if it's an index- if idx, err := strconv.Atoi(nameOrIdx); err == nil {- plans, err := lib.GetPlans()- if err != nil {- fmt.Fprintln(os.Stderr, "Error getting plans:", err)+ idx, err := strconv.Atoi(nameOrIdx)++ if err == nil {+ if idx > 0 && idx <= len(plans) {+ plan = plans[idx-1]+ } else {+ fmt.Fprintln(os.Stderr, "Error: index out of range")+ os.Exit(1)}- plan := plans[idx-1]- name = plan.Name} else {- name = nameOrIdx- }-- planDir := filepath.Join(plandexDir, name)+ for _, p := range plans {+ if p.Name == nameOrIdx {+ plan = p+ break+ }+ }- if _, err := os.Stat(planDir); os.IsNotExist(err) {- fmt.Fprintf(os.Stderr, "Plan %s does not exist", color.New(color.Bold).Sprint(name))- return+ if plan == nil {+ fmt.Fprintln(os.Stderr, "Error: plan not found")+ os.Exit(1)+ }}- err = os.RemoveAll(planDir)+ err = api.Client.DeletePlan(plan.Id)+if err != nil {- fmt.Fprintln(os.Stderr, "Error deleting the plan:", err)+ fmt.Fprintln(os.Stderr, "Error deleting plan:", err)return}- if lib.CurrentPlanName == name {- err = os.Remove(filepath.Join(plandexDir, "current_plan.json"))+ if lib.CurrentPlanId == plan.Id {+ err = lib.ClearCurrentPlan()if err != nil {- fmt.Fprintln(os.Stderr, "Error deleting current plan:", err)+ fmt.Fprintln(os.Stderr, "Error clearing current plan:", err)return}}- fmt.Printf("β Deleted plan %s\n", color.New(color.Bold).Sprint(name))+ fmt.Printf("β Deleted plan %s\n", color.New(color.Bold).Sprint(plan.Name))}func delAll() {- plandexDir, _, err := lib.FindOrCreatePlandex()- if err != nil {- fmt.Fprintln(os.Stderr, "Error:", err)- return- }-- err = os.RemoveAll(plandexDir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error deleting all the plans:", err)- return- }-- err = os.Mkdir(plandexDir, os.ModePerm)+ err := api.Client.DeleteAllPlans(lib.CurrentProjectId)if err != nil {- fmt.Fprintln(os.Stderr, "Error creating .plandex folder:", err)+ fmt.Fprintln(os.Stderr, "Error deleting all plans:", err)return}diff --git a/cli/cmd/diffs.go b/cli/cmd/diffs.godeleted file mode 100644index 9ac05edb..00000000--- a/cli/cmd/diffs.go+++ /dev/null@@ -1,126 +0,0 @@-package cmd--// import (-// "fmt"-// "os"-// "os/exec"-// "path/filepath"-// "plandex/lib"--// "github.com/spf13/cobra"-// )--// const tempBranchName = "pdx_temp"--// func init() {-// RootCmd.AddCommand(diffsCmd)-// }--// var diffsCmd = &cobra.Command{-// Use: "diffs",-// Aliases: []string{"d"},-// Short: "Show differences for each individual file between plan files and project files",-// Run: diffs,-// }--// func diffs(cmd *cobra.Command, args []string) {-// err := showDiffs()-// if err != nil {-// fmt.Println("An error occurred while generating diffs:", err)-// }-// }--// func showDiffs() error {-// // Check if git is installed-// if !lib.IsCommandAvailable("git") {-// return fmt.Errorf("git is required to generate diffs")-// }--// originalDir, err := os.Getwd()-// if err != nil {-// return fmt.Errorf("error getting current directory: %v", err)-// }--// // Create a temporary directory-// tempDir, err := os.MkdirTemp(os.TempDir(), "plandex-diffs-*")-// if err != nil {-// return fmt.Errorf("error creating temp directory: %v", err)-// }-// defer os.RemoveAll(tempDir) // Cleanup temporary directory--// // Copy relevant project files to the temporary directory-// paths, err := lib.GetCurrentPlanFilePaths()-// ch := make(chan error)-// for _, filePath := range paths {-// go func(filePath string) {-// ch <- lib.CopyFile(filepath.Join(lib.ProjectRoot, filePath), filepath.Join(tempDir, filePath))-// }(filePath)-// }--// for range paths {-// select {-// case err := <-ch:-// if err != nil {-// if !os.IsNotExist(err) {-// return fmt.Errorf("error copying file: %v", err)-// }-// }-// }-// }--// // Navigate to the temporary directory-// err = os.Chdir(tempDir)-// if err != nil {-// return fmt.Errorf("error changing to temp directory: %v", err)-// }-// defer os.Chdir(originalDir)--// // Initialize a new git repo in the temporary directory-// cmd := exec.Command("git", "init")-// err = cmd.Run()-// if err != nil {-// return fmt.Errorf("error initializing git repo in temp directory: %v", err)-// }--// // Add and commit changes in the temporary directory-// cmd = exec.Command("git", "add", ".")-// output, err := cmd.CombinedOutput()-// if err != nil {-// err = fmt.Errorf("error adding changes in temp directory: %v", err)-// fmt.Println(err)-// fmt.Println(string(output))-// }--// cmd = exec.Command("git", "commit", "-m", "temp commit", "--allow-empty")-// output, err = cmd.CombinedOutput()-// if err != nil {-// err = fmt.Errorf("error committing changes in temp directory: %v", err)-// fmt.Println(err)-// fmt.Println(string(output))-// }--// err = lib.CopyDir(lib.DraftFilesDir, tempDir)-// if err != nil {-// return fmt.Errorf("error copying files to temp directory: %v", err)-// }--// cmd = exec.Command("git", "add", ".")-// output, err = cmd.CombinedOutput()-// if err != nil {-// err = fmt.Errorf("error adding changes in temp directory: %v", err)-// fmt.Println(err)-// fmt.Println(string(output))-// }--// // Show the diff-// cmd = exec.Command("git", "diff", "--cached")-// cmd.Stdin = os.Stdin-// cmd.Stdout = os.Stdout-// cmd.Stderr = os.Stderr-// err = cmd.Run()-// if err != nil {-// return fmt.Errorf("error showing diffs: %v", err)-// }--// return nil-// }diff --git a/cli/cmd/load.go b/cli/cmd/load.goindex 15bc5033..7556d15c 100644--- a/cli/cmd/load.go+++ b/cli/cmd/load.go@@ -1,6 +1,8 @@package cmdimport (+ "fmt"+ "os""plandex/lib""plandex/term""plandex/types"@@ -30,6 +32,13 @@ func init() {}func contextLoad(cmd *cobra.Command, args []string) {+ lib.MustResolveProject()++ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")+ return+ }+lib.MustLoadContext(args, &types.LoadContextParams{Note: note,Recursive: recursive,diff --git a/cli/cmd/ls.go b/cli/cmd/ls.goindex 2e6201a5..ca82b93d 100644--- a/cli/cmd/ls.go+++ b/cli/cmd/ls.go@@ -3,15 +3,14 @@ package cmdimport ("fmt""os"+ "plandex/api""plandex/format""plandex/lib""plandex/term""strconv"- "time""github.com/fatih/color""github.com/olekukonko/tablewriter"- "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -23,7 +22,9 @@ var contextCmd = &cobra.Command{}func context(cmd *cobra.Command, args []string) {- context, err := lib.GetAllContext(true)+ lib.MustResolveProject()++ contexts, err := api.Client.ListContext(lib.CurrentPlanId)if err != nil {color.New(color.FgRed).Fprintln(os.Stderr, "Error listing context:", err)@@ -35,36 +36,25 @@ func context(cmd *cobra.Command, args []string) {table.SetHeader([]string{"#", "Name", "Type", "πͺ", "Added", "Updated"})table.SetAutoWrapText(false)- if len(context) == 0 {+ if len(contexts) == 0 {fmt.Println("π€·ββοΈ No context")fmt.Println()term.PrintCmds("", "load")return}- for i, part := range context {- totalTokens += part.NumTokens-- t, icon := lib.GetContextTypeAndIcon(part)- addedAt, err := time.Parse(shared.TsFormat, part.AddedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- continue- }+ for i, context := range contexts {+ totalTokens += context.NumTokens- updatedAt, err := time.Parse(shared.TsFormat, part.UpdatedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- continue- }+ t, icon := lib.GetContextTypeAndIcon(context)row := []string{strconv.Itoa(i + 1),- " " + icon + " " + part.Name,+ " " + icon + " " + context.Name,t,- strconv.Itoa(part.NumTokens), //+ " πͺ",- format.Time(addedAt),- format.Time(updatedAt),+ strconv.Itoa(context.NumTokens), //+ " πͺ",+ format.Time(context.CreatedAt),+ format.Time(context.UpdatedAt),}table.Rich(row, []tablewriter.Colors{{tablewriter.FgHiWhiteColor, tablewriter.Bold},diff --git a/cli/cmd/new.go b/cli/cmd/new.goindex 439fa662..50a75571 100644--- a/cli/cmd/new.go+++ b/cli/cmd/new.go@@ -3,11 +3,10 @@ package cmdimport ("fmt""os"- "path/filepath"+ "plandex/api""plandex/lib""plandex/term"- "plandex/types""github.com/fatih/color""github.com/plandex/plandex/shared"@@ -32,146 +31,23 @@ func init() {}func new(cmd *cobra.Command, args []string) {- isDraft := false+ lib.MustResolveProject()- if name == "" {- name = "draft"- isDraft = true- }-- // Check git installed- if !lib.IsCommandAvailable("git") {- fmt.Fprintln(os.Stderr, "Error: git is required")- os.Exit(1)- }+ res, err := api.Client.CreatePlan(lib.CurrentProjectId, shared.CreatePlanRequest{Name: name})- plandexDir, _, err := lib.FindOrCreatePlandex()if err != nil {- fmt.Fprintln(os.Stderr, "Error finding or creating .plandex dir:", err)+ fmt.Fprintln(os.Stderr, "Error creating plan:", err)return}- err = lib.ClearDraftPlans()- if err != nil {- fmt.Fprintln(os.Stderr, "Error clearing draft plans:", err)- return- }+ err = lib.SetCurrentPlan(res.Id)- name, err = lib.DedupPlanName(name)- if err != nil {- fmt.Fprintln(os.Stderr, "Error deduping plan name:", err)- return- }-- rootDir := filepath.Join(plandexDir, name)-- // Set the current plan to 'name'- err = lib.SetCurrentPlan(name)if err != nil {fmt.Fprintln(os.Stderr, "Error setting current plan:", err)return}- // Fixes current plan paths (CurrentPlanName, CurrentPlanRootDir, etc.)- err = lib.LoadCurrentPlan()- if err != nil {- fmt.Fprintln(os.Stderr, "Error loading current plan:", err)- return- }-- // Create 'name' directory inside .plandex- err = os.Mkdir(rootDir, os.ModePerm)- if err != nil && !os.IsExist(err) {- fmt.Fprintln(os.Stderr, "Error creating plan dir:", err)- return- }- // fmt.Fprintln(os.Stderr, "β Created plan at "+rootDir)-- // Create context subdirectory- contextDir := filepath.Join(rootDir, "context")- err = os.Mkdir(contextDir, os.ModePerm)- if err != nil && !os.IsExist(err) {- fmt.Fprintln(os.Stderr, "Error creating context subdir:", err)- return- }- // fmt.Fprintln(os.Stderr, "β Created context directory at "+contextDir)-- // Create plan subdirectory- planDir := filepath.Join(rootDir, "plan")- err = os.Mkdir(planDir, os.ModePerm)- if err != nil && !os.IsExist(err) {- fmt.Fprintln(os.Stderr, "Error creating plan subdir:", err)- return- }- // fmt.Fprintln(os.Stderr, "β Created plan directory at "+planDir)-- // Create conversation subdirectory- conversationDir := filepath.Join(rootDir, "conversation")- err = os.Mkdir(conversationDir, os.ModePerm)- if err != nil && !os.IsExist(err) {- fmt.Fprintln(os.Stderr, "Error creating conversation subdir:", err)- return- }- // fmt.Fprintln(os.Stderr, "β Created conversation directory at "+conversationDir)-- // Init empty git repositories in context, plan, and conversation directories- // init git repo in root plan dir- // fmt.Println("Initializing git repo in " + rootDir)- err = lib.InitGitRepo(rootDir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error initializing git repo:", err)- return- }- // fmt.Println("Adding git submodules to " + rootDir)-- initAndAddSubmodule := func(dir string) {-- // Initialize the Git repo in the directory- // fmt.Println("Initializing git repo in " + dir)- err = lib.InitGitRepo(dir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error initializing git repo:", err)- return- }-- // Add the directory as a submodule to the root repo- // fmt.Println("Adding git submodule " + dir + " to " + rootDir)- err = lib.AddGitSubmodule(rootDir, dir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error adding git submodules:", err)- return- }- }-- ts := shared.StringTs()- initialPlanState := &types.PlanState{- Name: "draft",- CreatedAt: ts,- UpdatedAt: ts,- }- err = lib.SetPlanState(initialPlanState, ts)- if err != nil {- fmt.Fprintln(os.Stderr, "Error writing initial plan state:", err)- return- }-- // fmt.Println("Wrote initial context state to " + contextDir)-- initAndAddSubmodule(contextDir)- initAndAddSubmodule(planDir)- initAndAddSubmodule(conversationDir)-- // After initializing and adding all submodules, ensure they're checked out correctly in the rootDir repo- // fmt.Println("Updating and initializing submodules in " + rootDir)- err = lib.UpdateAndInitSubmodules(rootDir)- if err != nil {- fmt.Fprintln(os.Stderr, "Error updating submodules:", err)- return- }-- // fmt.Fprintln(os.Stderr, "β Initialized context and plan git repositories")-- if isDraft {+ if name == "" {fmt.Println("β Started new plan")} else {fmt.Printf("β Started new plan: %s\n", color.New(color.Bold, color.FgHiWhite).Sprint(name))diff --git a/cli/cmd/plans.go b/cli/cmd/plans.goindex 4e7105ca..b8df0f74 100644--- a/cli/cmd/plans.go+++ b/cli/cmd/plans.go@@ -4,15 +4,14 @@ import ("fmt""os""strconv"- "time"+ "plandex/api""plandex/format""plandex/lib""plandex/term""github.com/fatih/color""github.com/olekukonko/tablewriter"- "github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -28,9 +27,12 @@ var plansCmd = &cobra.Command{}func plans(cmd *cobra.Command, args []string) {- plans, err := lib.GetPlans()+ lib.MustResolveProject()++ plans, err := api.Client.ListPlans(lib.CurrentProjectId)+if err != nil {- fmt.Fprintln(os.Stderr, "Error:", err)+ fmt.Fprintln(os.Stderr, "Error getting plans:", err)return}@@ -48,35 +50,23 @@ func plans(cmd *cobra.Command, args []string) {for i, p := range plans {var name string- if p.Name == lib.CurrentPlanName {+ if p.Name == lib.CurrentPlanId {name = color.New(color.Bold, color.FgGreen).Sprint(p.Name) + color.New(color.FgWhite).Sprint(" π current")} else {name = p.Name}- createdAt, err := time.Parse(shared.TsFormat, p.CreatedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- continue- }-- updatedAt, err := time.Parse(shared.TsFormat, p.UpdatedAt)- if err != nil {- fmt.Fprintln(os.Stderr, "Error parsing time:", err)- continue- }-row := []string{strconv.Itoa(i + 1),name,- format.Time(updatedAt),- format.Time(createdAt),+ format.Time(p.UpdatedAt),+ format.Time(p.CreatedAt),strconv.Itoa(p.ContextTokens) + " πͺ",strconv.Itoa(p.ConvoTokens) + " πͺ",}var style []tablewriter.Colors- if p.Name == lib.CurrentPlanName {+ if p.Name == lib.CurrentPlanId {style = []tablewriter.Colors{{tablewriter.FgGreenColor, tablewriter.Bold},}diff --git a/cli/cmd/preview.go b/cli/cmd/preview.godeleted file mode 100644index 6c840f02..00000000--- a/cli/cmd/preview.go+++ /dev/null@@ -1,83 +0,0 @@-package cmd--// import (-// "bytes"-// "fmt"-// "log"-// "os"-// "os/exec"-// "plandex/lib"-// "strings"--// "github.com/spf13/cobra"-// )--// func init() {-// RootCmd.AddCommand(checkoutCmd)-// }--// var checkoutCmd = &cobra.Command{-// Use: "preview [name]",-// Aliases: []string{"pv"},-// Short: "Preview changes in a new branch",-// Args: cobra.MaximumNArgs(1),-// Run: checkout,-// }--// func getCurrentBranch() (string, error) {-// cmd := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")-// var out bytes.Buffer-// cmd.Stdout = &out-// err := cmd.Run()-// if err != nil {-// return "", err-// }-// return strings.TrimSpace(out.String()), nil-// }--// func checkout(cmd *cobra.Command, args []string) {-// if !lib.IsCommandAvailable("git") {-// log.Fatalln("Error: git is required")-// }--// var name string--// if len(args) > 0 {-// name = args[0]-// name = strings.TrimSpace(name)-// }--// output, err := exec.Command("git", "rev-parse", "--is-inside-work-tree").CombinedOutput()-// if err != nil || strings.TrimSpace(string(output)) != "true" {-// log.Fatalln("Error: please make sure you're inside of a git repository")-// }--// if name == "" || name == "current" {-// name = lib.CurrentPlanName-// }--// branchName := name--// currentBranch, err := getCurrentBranch()-// if err != nil {-// log.Fatalf("Error: could not retrieve current branch: %v\n", err)-// }--// if currentBranch == branchName {-// fmt.Printf("Already on branch %s\n", branchName)-// } else {-// _, err = exec.Command("git", "checkout", "-b", branchName).CombinedOutput()-// fmt.Printf("β Checked out branch %s\n", branchName)-// if err != nil {-// log.Fatalf("Error: could not checkout to a new branch: %v\n", err)-// }-// }--// err = apply(cmd, args)--// if err != nil {-// fmt.Fprintln(os.Stderr, "Error committing plan: ", err)-// return-// }--// }diff --git a/cli/cmd/pwd.go b/cli/cmd/pwd.godeleted file mode 100644index 58e10c77..00000000--- a/cli/cmd/pwd.go+++ /dev/null@@ -1,30 +0,0 @@-package cmd--import (- "fmt"- "os"- "path/filepath"- "plandex/lib"-- "github.com/spf13/cobra"-)--var pwdCmd = &cobra.Command{- Use: "pwd",- Short: "Get the current plan directory.",- Long: "Use it like this `cd $(plandex pwd)` to go to the current plan directory.",- Run: pwd,-}--func init() {- RootCmd.AddCommand(pwdCmd)-}--func pwd(cmd *cobra.Command, args []string) {- if lib.CurrentPlanName == "" {- fmt.Fprintf(os.Stderr, "No current plan.\n")- os.Exit(1)- }- fmt.Println(filepath.Join(lib.PlandexDir, lib.CurrentPlanName))--}diff --git a/cli/cmd/rm.go b/cli/cmd/rm.goindex 12f9f4f1..98f3c184 100644--- a/cli/cmd/rm.go+++ b/cli/cmd/rm.go@@ -4,11 +4,9 @@ import ("fmt""os""path/filepath"+ "plandex/api""plandex/lib"- "strconv"- "strings"- "github.com/olekukonko/tablewriter""github.com/plandex/plandex/shared""github.com/spf13/cobra")@@ -23,111 +21,53 @@ var contextRmCmd = &cobra.Command{}func contextRm(cmd *cobra.Command, args []string) {- context, err := lib.GetAllContext(true)+ lib.MustResolveProject()++ if lib.CurrentPlanId == "" {+ fmt.Fprintln(os.Stderr, "No current plan")+ return+ }++ contexts, err := api.Client.ListContext(lib.CurrentPlanId)if err != nil {fmt.Fprintln(os.Stderr, "Error retrieving context:", err)return}- toRemovePaths := []string{}- toRemoveParts := []*shared.ModelContextPart{}+ deleteIds := map[string]bool{}- for i, part := range context {- path := lib.CreateContextFileName(part.Name, part.Sha)+ for i, context := range contexts {for _, id := range args {- if fmt.Sprintf("%d", i+1) == id || part.Name == id || part.FilePath == id || part.Url == id {- toRemovePaths = append(toRemovePaths, path)- toRemoveParts = append(toRemoveParts, part)+ if fmt.Sprintf("%d", i+1) == id || context.Name == id || context.FilePath == id || context.Url == id {+ deleteIds[context.Id] = truebreak- } else if part.FilePath != "" {+ } else if context.FilePath != "" {// Check if id is a glob pattern- matched, err := filepath.Match(id, part.FilePath)+ matched, err := filepath.Match(id, context.FilePath)if err != nil {fmt.Fprintln(os.Stderr, "Error matching glob pattern:", err)return}if matched {- toRemovePaths = append(toRemovePaths, path)- toRemoveParts = append(toRemoveParts, part)+ deleteIds[context.Id] = truebreak}}}}- err = lib.ContextRemoveFiles(toRemovePaths)- if err != nil {- fmt.Fprintln(os.Stderr, "Error removing context:", err)- return- }-- // update plan state with new token count- planState, err := lib.GetPlanState()- if err != nil {- fmt.Fprintln(os.Stderr, "Error retrieving plan state:", err)- return- }-- tableString := &strings.Builder{}- table := tablewriter.NewWriter(tableString)- table.SetHeader([]string{"Name", "Type", "πͺ"})- table.SetAutoWrapText(false)-- removedTokens := 0- totalTokens := planState.ContextTokens- totalUpdatableTokens := planState.ContextUpdatableTokens- for _, part := range toRemoveParts {- removedTokens += part.NumTokens- totalTokens -= part.NumTokens-- if part.Type == shared.ContextFileType || part.Type == shared.ContextDirectoryTreeType || part.Type == shared.ContextURLType {- totalUpdatableTokens -= part.NumTokens- }-- t, icon := lib.GetContextTypeAndIcon(part)- row := []string{- " " + icon + " " + part.Name,- t,- "-" + strconv.Itoa(part.NumTokens),- }-- table.Rich(row, []tablewriter.Colors{- {tablewriter.FgHiRedColor, tablewriter.Bold},- {tablewriter.FgHiRedColor},- {tablewriter.FgHiRedColor},+ if len(deleteIds) > 0 {+ res, err := api.Client.DeleteContext(lib.CurrentPlanId, shared.DeleteContextRequest{+ Ids: deleteIds,})- }- planState.ContextTokens = totalTokens- planState.ContextUpdatableTokens = totalUpdatableTokens-- err = lib.SetPlanState(planState, shared.StringTs())-- if err != nil {- fmt.Fprintln(os.Stderr, "Error writing plan state:", err)- return- }-- // output- if len(toRemovePaths) > 0 {- suffix := ""- if len(toRemovePaths) > 1 {- suffix = "s"- }- msg := fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ \n", len(toRemovePaths), suffix, removedTokens, totalTokens)- table.Render()-- msg += "\n" + tableString.String()-- err = lib.GitCommitContextUpdate(msg)if err != nil {- fmt.Fprintln(os.Stderr, "Error committing context update:", err)+ fmt.Fprintln(os.Stderr, "Error deleting context:", err)return}- fmt.Println("β " + msg)-+ fmt.Println("β " + res.Msg)} else {fmt.Println("π€·ββοΈ No context removed")}diff --git a/cli/cmd/update.go b/cli/cmd/update.goindex b8a13f94..e9eaf939 100644--- a/cli/cmd/update.go+++ b/cli/cmd/update.go@@ -1,7 +1,6 @@package cmdimport (- "fmt""plandex/lib""github.com/spf13/cobra"@@ -21,7 +20,7 @@ func init() {}func update(cmd *cobra.Command, args []string) {- updateRes := lib.MustUpdateContextWithOuput()- table := lib.TableForContextUpdateRes(updateRes)- fmt.Println(table)+ lib.MustResolveProject()++ lib.MustUpdateContextWithOuput()}diff --git a/cli/go.mod b/cli/go.modindex e594d55c..bf4ab0bc 100644--- a/cli/go.mod+++ b/cli/go.mod@@ -5,7 +5,6 @@ go 1.21.3require (github.com/atotto/clipboard v0.1.4github.com/charmbracelet/lipgloss v0.9.1- github.com/davecgh/go-spew v1.1.1github.com/fatih/color v1.16.0github.com/looplab/fsm v1.0.1github.com/muesli/reflow v0.3.0diff --git a/cli/lib/api_abort.go b/cli/lib/api_abort.godeleted file mode 100644index 8b0dc30c..00000000--- a/cli/lib/api_abort.go+++ /dev/null@@ -1,39 +0,0 @@-package lib--import (- "fmt"- "io"- "net/http"-)--func (api *API) Abort(proposalId string) error {- fmt.Println("api aborting proposal", proposalId)-- serverUrl := apiHost + "/abort"-- req, err := http.NewRequest("DELETE", serverUrl, nil)- if err != nil {- return fmt.Errorf("failed to create request: %s", err)- }-- q := req.URL.Query()- q.Add("proposalId", proposalId)- req.URL.RawQuery = q.Encode()-- resp, err := http.DefaultClient.Do(req)- if err != nil {- return fmt.Errorf("failed to send request to server: %s", err)- }-- defer resp.Body.Close()-- // Check the response status code- if resp.StatusCode != http.StatusOK {- // Read the error message from the body- errorBody, _ := io.ReadAll(resp.Body)- return fmt.Errorf("server returned an error %d: %s", resp.StatusCode,- string(errorBody))- }-- return nil-}diff --git a/cli/lib/api_convo.go b/cli/lib/api_convo.godeleted file mode 100644index 477c6c89..00000000--- a/cli/lib/api_convo.go+++ /dev/null@@ -1,57 +0,0 @@-package lib--import (- "encoding/json"- "fmt"- "io"- "net/http"-- "github.com/plandex/plandex/shared"-)--func (api *API) ConvoSummary(rootId, latestTimestamp string) (*shared.ConversationSummary, error) {- serverUrl := apiHost + "/convo-summary/" + rootId-- req, err := http.NewRequest("GET", serverUrl, nil)- if err != nil {- return nil, fmt.Errorf("failed to create request: %s", err)- }-- if latestTimestamp != "" {- q := req.URL.Query()- q.Add("latestTimestamp", latestTimestamp)- req.URL.RawQuery = q.Encode()- }-- resp, err := http.DefaultClient.Do(req)- if err != nil {- return nil, fmt.Errorf("failed to send request to server: %s", err)- }-- defer resp.Body.Close()-- if resp.StatusCode == http.StatusNoContent {- return nil, nil- }-- // Check the response status code- if resp.StatusCode != http.StatusOK {- // Read the error message from the body- errorBody, _ := io.ReadAll(resp.Body)- return nil, fmt.Errorf("server returned an error %d: %s", resp.StatusCode,- string(errorBody))- }-- body, err := io.ReadAll(resp.Body)- if err != nil {- return nil, fmt.Errorf("failed to read response body: %s", err)- }-- var convoSummary shared.ConversationSummary- err = json.Unmarshal(body, &convoSummary)- if err != nil {- return nil, fmt.Errorf("failed to parse response body: %s", err)- }-- return &convoSummary, nil-}diff --git a/cli/lib/api_meta.go b/cli/lib/api_meta.godeleted file mode 100644index f39ea10b..00000000--- a/cli/lib/api_meta.go+++ /dev/null@@ -1,78 +0,0 @@-package lib--import (- "bytes"- "encoding/json"- "io"- "net/http"-- "github.com/plandex/plandex/shared"-)--func (api *API) FileName(text string) (*shared.FileNameResponse, error) {- serverUrl := apiHost + "/filename"-- payload := shared.FileNameRequest{- Text: text,- }- jsonData, err := json.Marshal(payload)- if err != nil {- return nil, err- }-- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))- if err != nil {- return nil, err- }- defer resp.Body.Close()-- body, err := io.ReadAll(resp.Body)- if err != nil {- return nil, err- }-- // fmt.Println("FileName response body:")- // fmt.Println(string(body))-- var fileName shared.FileNameResponse- err = json.Unmarshal(body, &fileName)- if err != nil {- return nil, err- }-- return &fileName, nil-}--func (api *API) ShortSummary(text string) (*shared.ShortSummaryResponse, error) {- serverUrl := apiHost + "/summarize"-- payload := shared.ShortSummaryRequest{- Text: text,- }- jsonData, err := json.Marshal(payload)- if err != nil {- return nil, err- }-- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))- if err != nil {- return nil, err- }- defer resp.Body.Close()-- body, err := io.ReadAll(resp.Body)- if err != nil {- return nil, err- }-- // fmt.Println("ShortSummary response body:")- // fmt.Println(string(body))-- var summarized shared.ShortSummaryResponse- err = json.Unmarshal(body, &summarized)- if err != nil {- return nil, err- }-- return &summarized, nil-}diff --git a/cli/lib/api_propose.go b/cli/lib/api_propose.godeleted file mode 100644index ae757031..00000000--- a/cli/lib/api_propose.go+++ /dev/null@@ -1,152 +0,0 @@-package lib--import (- "bufio"- "bytes"- "context"- "encoding/json"- "fmt"- "io"- "net/http"- "plandex/types"-- "github.com/plandex/plandex/shared"-)--func (api *API) Propose(prompt, parentProposalId, rootId string, onStream types.OnStreamPlan) (*shared.PromptRequest, error) {- serverUrl := apiHost + "/proposal"-- errCh := make(chan error, 1)- contextCh := make(chan shared.ModelContext, 1)- conversationCh := make(chan []*shared.ConversationMessage, 1)- planFilesCh := make(chan *shared.CurrentPlanFiles, 1)- summaryCh := make(chan []*shared.ConversationSummary, 1)-- // Goroutine for loading conversation- go func() {- conversation, err := LoadConversation()- if err != nil {- fmt.Println("Error loading conversation")- errCh <- err- return- }- conversationCh <- conversation- }()-- // Goroutine for loading plan files and context- go func() {- res, err := GetCurrentPlanState()- if err != nil {- fmt.Println("Error loading plan")- errCh <- err- return- }- planFilesCh <- res.CurrentPlanFiles- contextCh <- res.ModelContext- }()-- // Goroutine for loading summaries- go func() {- summaries, err := LoadSummaries()- if err != nil {- fmt.Println("Error loading summaries")- errCh <- err- return- }- summaryCh <- summaries- }()-- var modelContext shared.ModelContext- var currentPlanFiles *shared.CurrentPlanFiles- var conversation []*shared.ConversationMessage- var summaries []*shared.ConversationSummary-- for i := 0; i < 4; i++ {- select {- case err := <-errCh:- return nil, fmt.Errorf("error loading plan data: %v", err)- case modelContext = <-contextCh:- case conversation = <-conversationCh:- case currentPlanFiles = <-planFilesCh:- case summaries = <-summaryCh:- }- }-- payload := shared.PromptRequest{- Prompt: prompt,- ModelContext: modelContext,- Conversation: conversation,- ConversationSummaries: summaries,- CurrentPlan: currentPlanFiles,- ParentProposalId: parentProposalId,- RootProposalId: rootId,- }-- jsonData, err := json.Marshal(payload)- if err != nil {- return nil, err- }-- resp, err := http.Post(serverUrl, "application/json", bytes.NewBuffer(jsonData))- if err != nil {- return nil, err- }-- // Check the HTTP status code- if resp.StatusCode >= 400 {- // Read the error message from the body- errorBody, _ := io.ReadAll(resp.Body)- return &payload, fmt.Errorf("server returned an error %d: %s", resp.StatusCode, string(errorBody))- }-- reader := bufio.NewReader(resp.Body)- streamState := shared.NewPlanStreamState()-- go func() {- for {- s, err := readUntilSeparator(reader, shared.STREAM_MESSAGE_SEPARATOR)- if err != nil {- fmt.Println("Error reading line:", err)- streamState.Event(context.Background(), shared.EVENT_ERROR)- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})- resp.Body.Close()- return- }-- if s == shared.STREAM_FINISHED || s == shared.STREAM_ABORTED {- var evt string- if s == shared.STREAM_FINISHED {- evt = shared.EVENT_FINISH- } else {- evt = shared.STATE_ABORTED- }- err := streamState.Event(context.Background(), evt)- if err != nil {- fmt.Printf("Error triggering state change %s: %s\n", evt, err)- }- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})- resp.Body.Close()- return- }-- if s == shared.STREAM_DESCRIPTION_PHASE {- err = streamState.Event(context.Background(), shared.EVENT_DESCRIBE)- } else if s == shared.STREAM_BUILD_PHASE {- err = streamState.Event(context.Background(), shared.EVENT_BUILD)- } else if s == shared.STREAM_WRITE_PHASE {- err = streamState.Event(context.Background(), shared.EVENT_WRITE)- }-- if err != nil {- fmt.Println("Error setting state:", err)- onStream(types.OnStreamPlanParams{Content: "", State: streamState, Err: err})- resp.Body.Close()- return- }-- onStream(types.OnStreamPlanParams{Content: s, State: streamState, Err: nil})- }- }()-- return &payload, nil-}diff --git a/cli/lib/apply.go b/cli/lib/apply.goindex 1f016a5c..b3f67470 100644--- a/cli/lib/apply.go+++ b/cli/lib/apply.go@@ -6,6 +6,7 @@ import ("fmt""os""path/filepath"+ "plandex/api""plandex/term""plandex/types"@@ -14,36 +15,15 @@ import ("github.com/plandex/plandex/shared")-func ApplyPlanWithOutput(name string, autoConfirm bool) error {- plandexDir, _, err := FindOrCreatePlandex()- if err != nil {- return fmt.Errorf("error finding or creating plandex dir: %w", err)- }-- if name == "" {- fmt.Println("π€·ββοΈ No plan specified and no current plan")- return nil- }-- rootDir := filepath.Join(plandexDir, name)-- _, err = os.Stat(rootDir)-- if os.IsNotExist(err) {- fmt.Printf("π€·ββοΈ Plan with name '%s' doesn't exist\n", name)- return nil- } else if err != nil {- return fmt.Errorf("error checking if plan exists: %w", err)- }-- res, err := GetCurrentPlanState()+func ApplyPlanWithOutput(planId string, autoConfirm bool) error {+ currentPlanState, err := api.Client.GetCurrentPlanState(planId)if err != nil {- return fmt.Errorf("error getting current plan files: %w", err)+ return fmt.Errorf("error getting current plan state: %w", err)}- currentPlanFiles := res.CurrentPlanFiles- planResByPath := res.PlanResByPath+ currentPlanFiles := currentPlanState.CurrentPlanFiles+ planResByPath := currentPlanState.PlanResult.FileResultsByPathif len(currentPlanFiles.Files) == 0 {fmt.Println("π€·ββοΈ No changes to apply")@@ -322,14 +302,15 @@ func ApplyPlanWithOutput(name string, autoConfirm bool) error {}}- err := SetPendingResultsApplied(planResByPath)+ err := api.Client.ApplyPlan(planId)+if err != nil {aborted = truereturn fmt.Errorf("failed to set pending results applied: %w", err)}if isRepo {- desc, err := GetLatestPlanDescription()+ desc := currentPlanState.LatestBuildDescriptionif err != nil {aborted = truereturn fmt.Errorf("failed to get latest plan description: %w", err)diff --git a/cli/lib/build.go b/cli/lib/build.godeleted file mode 100644index 55c21f80..00000000--- a/cli/lib/build.go+++ /dev/null@@ -1 +0,0 @@-package libdiff --git a/cli/lib/context_display.go b/cli/lib/context_display.goindex dde1aa44..d27ab238 100644--- a/cli/lib/context_display.go+++ b/cli/lib/context_display.go@@ -2,10 +2,10 @@ package libimport "github.com/plandex/plandex/shared"-func GetContextTypeAndIcon(part *shared.ModelContextPart) (string, string) {+func GetContextTypeAndIcon(context *shared.Context) (string, string) {var icon stringvar t string- switch part.Type {+ switch context.ContextType {case shared.ContextFileType:icon = "π"t = "file"diff --git a/cli/lib/context_fs.go b/cli/lib/context_fs.godeleted file mode 100644index a6048ddf..00000000--- a/cli/lib/context_fs.go+++ /dev/null@@ -1,109 +0,0 @@-package lib--import (- "encoding/json"- "fmt"- "os"- "path/filepath"- "strings"-- "github.com/plandex/plandex/shared"-)--// CreateContextFileName constructs a filename based on the given name and sha.-func CreateContextFileName(name, sha string) string {- // Extract the first 8 characters of the sha- shaSubstring := sha[:8]- s := strings.ReplaceAll(name, string(os.PathSeparator), "_")- return fmt.Sprintf("%s.%s", s, shaSubstring)-}--func ContextRemoveFiles(paths []string) error {- // remove files- errCh := make(chan error, len(paths)*2)- for _, path := range paths {- for _, ext := range []string{".meta", ".body"} {- go func(path, ext string) {- errCh <- os.Remove(filepath.Join(ContextSubdir, path+ext))- }(path, ext)- }- }-- for i := 0; i < len(paths)*2; i++ {- err := <-errCh- if err != nil {- return fmt.Errorf("error removing context file: %v", err)- }- }-- return nil-}--// writeContextPartToFile writes a single ModelContextPart to a file.-func writeContextPartToFile(part *shared.ModelContextPart) error {- metaFilename := CreateContextFileName(part.Name, part.Sha) + ".meta"- metaPath := filepath.Join(ContextSubdir, metaFilename)-- bodyFilename := CreateContextFileName(part.Name, part.Sha) + ".body"- bodyPath := filepath.Join(ContextSubdir, bodyFilename)- body := []byte(part.Body)- part.Body = ""-- // Convert the ModelContextPart to JSON- data, err := json.MarshalIndent(part, "", " ")- if err != nil {- return fmt.Errorf("failed to marshal context part: %v", err)- }-- // Open or create a bodyFile for writing- bodyFile, err := os.OpenFile(bodyPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)- if err != nil {- return fmt.Errorf("failed to open file %s for writing: %v", bodyPath, err)- }- defer bodyFile.Close()-- // Write the body to the file- if _, err = bodyFile.Write(body); err != nil {- return fmt.Errorf("failed to write data to file %s: %v", bodyPath, err)- }-- // Open or create a metaFile for writing- metaFile, err := os.OpenFile(metaPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)- if err != nil {- return fmt.Errorf("failed to open file %s for writing: %v", metaPath, err)- }- defer metaFile.Close()-- // Write the JSON data to the file- if _, err = metaFile.Write(data); err != nil {- return fmt.Errorf("failed to write data to file %s: %v", metaPath, err)- }-- return nil-}--// Write each context part in parallel-func writeContextParts(contextParts []*shared.ModelContextPart) error {- var errCh = make(chan error)- for _, part := range contextParts {- go func(p *shared.ModelContextPart) {- if err := writeContextPartToFile(p); err != nil {- // Handling the error in the goroutine by logging. Depending on your application,- // you might want a different strategy (e.g., collect errors and handle them after waiting).- err := fmt.Errorf("Error writing context part to file: %v", err)- errCh <- err- }- errCh <- nil- }(part)- }-- // Wait for all goroutines to finish- for range contextParts {- if err := <-errCh; err != nil {- return err- }- }-- return nil--}diff --git a/cli/lib/context_get.go b/cli/lib/context_get.godeleted file mode 100644index 7bd1bc30..00000000--- a/cli/lib/context_get.go+++ /dev/null@@ -1,71 +0,0 @@-package lib--import (- "encoding/json"- "fmt"- "os"- "path/filepath"- "sort"- "strings"-- "github.com/plandex/plandex/shared"-)--func GetAllContext(metaOnly bool) (shared.ModelContext, error) {- files, err := os.ReadDir(ContextSubdir)- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to read context directory: %v\n", err)- return nil, err- }-- var context shared.ModelContext- for _, file := range files {- filename := file.Name()-- if filename == ".git" || filename == "context.json" {- continue- }-- // Only process .meta files and then look for their corresponding .body files- if strings.HasSuffix(filename, ".meta") {- // fmt.Fprintf(os.Stderr, "Reading meta context file %s\n", filename)-- metaContent, err := os.ReadFile(filepath.Join(ContextSubdir, filename))- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to read meta file %s: %v", filename, err)- return nil, err- }-- var contextPart shared.ModelContextPart- if err := json.Unmarshal(metaContent, &contextPart); err != nil {- fmt.Fprintf(os.Stderr, "Failed to unmarshal JSON from file %s: %v\n", filename, err)- return nil, err- }-- if !metaOnly {- // get the body filename by replacing the .meta suffix with .body- bodyFilename := strings.TrimSuffix(filename, ".meta") + ".body"- bodyPath := filepath.Join(ContextSubdir, bodyFilename)-- // fmt.Fprintf(os.Stderr, "Reading body context file %s\n", bodyPath)-- bodyContent, err := os.ReadFile(bodyPath)- if err != nil {- fmt.Fprintf(os.Stderr, "Failed to read body file %s: %v\n", bodyPath, err)- return nil, err- }-- contextPart.Body = string(bodyContent)- }-- context = append(context, &contextPart)- }- }-- // sort by timestamp ascending- sort.Slice(context, func(i, j int) bool {- return context[i].AddedAt < context[j].AddedAt- })-- return context, nil-}diff --git a/cli/lib/context_load.go b/cli/lib/context_load.goindex 3a4497ad..84fb49ce 100644--- a/cli/lib/context_load.go+++ b/cli/lib/context_load.go@@ -2,194 +2,56 @@ package libimport ("bufio"- "crypto/sha256"- "encoding/hex""fmt""io""os"- "path/filepath"- "plandex/format"+ "plandex/api""plandex/term""plandex/types""plandex/url""strings"- "sync""time"- "strconv"-"github.com/briandowns/spinner"- "github.com/olekukonko/tablewriter""github.com/plandex/plandex/shared")-func MustLoadContext(resources []string, params *types.LoadContextParams) (int, int) {- timeStart := time.Now()-+func MustLoadContext(resources []string, params *types.LoadContextParams) {s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)s.Prefix = "π₯ Loading context... "s.Start()- maxTokens := shared.MaxContextTokens-- planState, err := GetPlanState()- if err != nil {+ onErr := func(err error) {s.Stop()term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get plan state: %v\n", err)+ fmt.Fprintf(os.Stderr, "Failed to load context: %v\n", err)os.Exit(1)}- tokensAdded := 0- totalTokens := planState.ContextTokens- totalUpdatableTokens := planState.ContextUpdatableTokens- var totalTokensMutex sync.Mutex-- var contextParts []*shared.ModelContextPart- var contextPartsMutex sync.Mutex-- wg := sync.WaitGroup{}+ var loadContextReq shared.LoadContextRequestif params.Note != "" {- wg.Add(1)-- go func() {- defer wg.Done()-- body := params.Note- numTokens, err := shared.GetNumTokens(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get number of tokens for the note: %v\n", err)- os.Exit(1)- }-- totalTokensMutex.Lock()- func() {- defer totalTokensMutex.Unlock()-- totalTokens += numTokens- tokensAdded += numTokens-- if totalTokens > maxTokens {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "π¨ The total number of tokens (%d) exceeds the maximum allowed (%d)\n", totalTokens, maxTokens)- os.Exit(1)- }- }()-- hash := sha256.Sum256([]byte(body))- sha := hex.EncodeToString(hash[:])-- fileNameResp, err := Api.FileName(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get a file name for the text: %v\n", err)- os.Exit(1)- }-- fileName := format.GetFileNameWithoutExt(fileNameResp.FileName)-- ts := shared.StringTs()- contextPart := &shared.ModelContextPart{- Type: shared.ContextNoteType,- Name: fileName,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: ts,- UpdatedAt: ts,- }-- contextPartsMutex.Lock()- contextParts = append(contextParts, contextPart)- contextPartsMutex.Unlock()-- }()-+ loadContextReq = append(loadContextReq, &shared.LoadContextParams{+ ContextType: shared.ContextNoteType,+ Body: params.Note,+ })}-- hasPipeData := falsefileInfo, err := os.Stdin.Stat()if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to stat stdin: %v\n", err)- os.Exit(1)+ onErr(fmt.Errorf("failed to stat stdin: %v", err))}if fileInfo.Mode()&os.ModeNamedPipe != 0 {reader := bufio.NewReader(os.Stdin)pipedData, err := io.ReadAll(reader)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to read piped data: %v\n", err)- os.Exit(1)+ onErr(fmt.Errorf("failed to read piped data: %v", err))}if len(pipedData) > 0 {- wg.Add(1)-- hasPipeData = true-- go func() {- defer wg.Done()-- body := string(pipedData)- numTokens, err := shared.GetNumTokens(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get number of tokens for the note: %v\n", err)- os.Exit(1)- }-- totalTokensMutex.Lock()- func() {- defer totalTokensMutex.Unlock()-- totalTokens += numTokens- tokensAdded += numTokens- if totalTokens > maxTokens {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "π¨ The total number of tokens (%d) exceeds the maximum allowed (%d)\n", totalTokens, maxTokens)- os.Exit(1)- }- }()-- hash := sha256.Sum256([]byte(body))- sha := hex.EncodeToString(hash[:])-- fileNameResp, err := Api.FileName(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get a file name for piped data: %v\n", err)- os.Exit(1)- }-- fileName := format.GetFileNameWithoutExt(fileNameResp.FileName)-- ts := shared.StringTs()- contextPart := &shared.ModelContextPart{- Type: shared.ContextPipedDataType,- Name: fileName,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: ts,- UpdatedAt: ts,- }-- contextPartsMutex.Lock()- contextParts = append(contextParts, contextPart)- contextPartsMutex.Unlock()-- }()+ loadContextReq = append(loadContextReq, &shared.LoadContextParams{+ ContextType: shared.ContextPipedDataType,+ Body: string(pipedData),+ })}}@@ -207,54 +69,23 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) (int,}}+ contextCh := make(chan *shared.LoadContextParams)+ errCh := make(chan error)+if len(inputFilePaths) > 0 {if params.NamesOnly {for _, inputFilePath := range inputFilePaths {- wg.Add(1)go func(inputFilePath string) {- defer wg.Done()-flattenedPaths, err := ParseInputPaths([]string{inputFilePath}, params)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to parse input paths: %v\n", err)- os.Exit(1)+ errCh <- fmt.Errorf("failed to parse input paths: %v", err)+ return}body := strings.Join(flattenedPaths, "\n")- bytes := []byte(body)-- hash := sha256.Sum256(bytes)- sha := hex.EncodeToString(hash[:])- numTokens, err := shared.GetNumTokens(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get number of tokens for the note: %v\n", err)- os.Exit(1)- }-- totalTokensMutex.Lock()- func() {- defer totalTokensMutex.Unlock()- totalTokens += numTokens- totalUpdatableTokens += numTokens- tokensAdded += numTokens- if totalTokens > maxTokens {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "π¨ The total number of tokens (%d) exceeds the maximum allowed (%d)\n", totalTokens, maxTokens)- os.Exit(1)- }-- }()-- ts := shared.StringTs()- // get last portion of directory path- name := filepath.Base(inputFilePath)+ name := inputFilePathif name == "." {name = "cwd"}@@ -262,90 +93,38 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) (int,name = "parent"}- contextPart := &shared.ModelContextPart{- Type: shared.ContextDirectoryTreeType,- Name: inputFilePath,- FilePath: inputFilePath,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: ts,- UpdatedAt: ts,+ contextCh <- &shared.LoadContextParams{+ ContextType: shared.ContextFileType,+ Name: name,+ Body: body,+ FilePath: inputFilePath,}-- contextPartsMutex.Lock()- contextParts = append(contextParts, contextPart)- contextPartsMutex.Unlock()-}(inputFilePath)}} else {flattenedPaths, err := ParseInputPaths(inputFilePaths, params)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to parse input paths: %v\n", err)- os.Exit(1)+ onErr(fmt.Errorf("failed to parse input paths: %v", err))}for _, path := range flattenedPaths {- wg.Add(1)go func(path string) {- defer wg.Done()-fileContent, err := os.ReadFile(path)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to read the file %s: %v", path, err)- os.Exit(1)+ errCh <- fmt.Errorf("failed to read the file %s: %v", path, err)+ return}-body := string(fileContent)- hash := sha256.Sum256(fileContent)- sha := hex.EncodeToString(hash[:])- numTokens, err := shared.GetNumTokens(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get number of tokens for the note: %v\n", err)- os.Exit(1)- }-- totalTokensMutex.Lock()- func() {- defer totalTokensMutex.Unlock()- totalTokens += numTokens- tokensAdded += numTokens- totalUpdatableTokens += numTokens- if totalTokens > maxTokens {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "π¨ The total number of tokens (%d) exceeds the maximum allowed (%d)\n", totalTokens, maxTokens)- os.Exit(1)- }- }()-- ts := shared.StringTs()-- contextPart := &shared.ModelContextPart{- Type: shared.ContextFileType,- Name: path,- Body: body,- FilePath: path,- Sha: sha,- NumTokens: numTokens,- AddedAt: ts,- UpdatedAt: ts,+ contextCh <- &shared.LoadContextParams{+ ContextType: shared.ContextFileType,+ Name: path,+ Body: body,+ FilePath: path,}- contextPartsMutex.Lock()- contextParts = append(contextParts, contextPart)- contextPartsMutex.Unlock()-}(path)}@@ -355,202 +134,58 @@ func MustLoadContext(resources []string, params *types.LoadContextParams) (int,if len(inputUrls) > 0 {for _, u := range inputUrls {- wg.Add(1)-go func(u string) {- defer wg.Done()-body, err := url.FetchURLContent(u)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to fetch content from URL %s: %v", u, err)- os.Exit(1)- }-- numTokens, err := shared.GetNumTokens(body)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get number of tokens for the note: %v\n", err)- os.Exit(1)+ errCh <- fmt.Errorf("failed to fetch content from URL %s: %v", u, err)+ return}- totalTokensMutex.Lock()- func() {- defer totalTokensMutex.Unlock()- totalTokens += numTokens- tokensAdded += numTokens- totalUpdatableTokens += numTokens- if totalTokens > maxTokens {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "π¨ The total number of tokens (%d) exceeds the maximum allowed (%d)\n", totalTokens, maxTokens)- os.Exit(1)- }- }()-- hash := sha256.Sum256([]byte(body))- sha := hex.EncodeToString(hash[:])-- ts := shared.StringTs()-name := url.SanitizeURL(u)// show the first 20 characters, then ellipsis then the last 20 characters of 'name'if len(name) > 40 {name = name[:20] + "β―" + name[len(name)-20:]}- contextPart := &shared.ModelContextPart{- Type: shared.ContextURLType,- Name: name,- Url: u,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: ts,- UpdatedAt: ts,+ contextCh <- &shared.LoadContextParams{+ ContextType: shared.ContextURLType,+ Name: name,+ Body: body,+ Url: u,}-- contextPartsMutex.Lock()- contextParts = append(contextParts, contextPart)- contextPartsMutex.Unlock()}(u)}}- wg.Wait()-- TableForLoadContext := func(contextParts []*shared.ModelContextPart) string {- tableString := &strings.Builder{}- table := tablewriter.NewWriter(tableString)- table.SetHeader([]string{"Name", "Type", "πͺ"})- table.SetAutoWrapText(false)-- for _, part := range contextParts {- t, icon := GetContextTypeAndIcon(part)- row := []string{- " " + icon + " " + part.Name,- t,- "+" + strconv.Itoa(part.NumTokens),- }-- table.Rich(row, []tablewriter.Colors{- {tablewriter.FgHiGreenColor, tablewriter.Bold},- {tablewriter.FgHiGreenColor},- {tablewriter.FgHiGreenColor},- })+ for i := 0; i < len(inputFilePaths)+len(inputUrls); i++ {+ select {+ case err := <-errCh:+ onErr(err)+ case context := <-contextCh:+ loadContextReq = append(loadContextReq, context)}-- table.Render()-- return tableString.String()}- if len(contextParts) == 0 {+ if len(loadContextReq) == 0 {+ s.Stop()+ term.ClearCurrentLine()fmt.Println("π€·ββοΈ No context loaded")- os.Exit(1)- }-- errCh := make(chan error, 2)- go func() {- errCh <- writeContextParts(contextParts)- }()-- go func() {- planState.ContextTokens = totalTokens- planState.ContextUpdatableTokens = totalUpdatableTokens- errCh <- SetPlanState(planState, shared.StringTs())- }()-- for i := 0; i < 2; i++ {- err := <-errCh- if err != nil {- fmt.Printf("Failed to write context: %v\n", err)- os.Exit(1)- }- }-- var added []string- if params.Note != "" {- added = append(added, "a note")+ os.Exit(0)}- if hasPipeData {- added = append(added, "piped data")- }- if len(inputFilePaths) > 0 {- var label string- if params.NamesOnly {- label = "directory tree"- if len(inputFilePaths) > 1 {- label = "directory trees"- }- } else {- label = "file"- if len(inputFilePaths) > 1 {- label = "files"- }- }- added = append(added, fmt.Sprintf("%d %s", len(inputFilePaths), label))- }- if len(inputUrls) > 0 {- label := "url"- if len(inputUrls) > 1 {- label = "urls"- }- added = append(added, fmt.Sprintf("%d %s", len(inputUrls), label))- }-- msg := "Loaded "- if len(added) <= 2 {- msg += strings.Join(added, " and ")- } else {- for i, add := range added {- if i == len(added)-1 {- msg += ", and " + add- } else {- msg += ", " + add- }- }- }- msg += fmt.Sprintf(" into context | added β %d πͺ | total β %d πͺ", tokensAdded, totalTokens)+ res, err := api.Client.LoadContext(CurrentPlanId, loadContextReq)if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to get total tokens: %v\n", err)- os.Exit(1)+ onErr(fmt.Errorf("failed to load context: %v", err))}- if err != nil {+ if res.MaxTokensExceeded {s.Stop()term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to commit context update to git: %v\n", err)+ overage := res.TotalTokens - shared.MaxContextTokens+ fmt.Printf("π¨ Update would add %d πͺ and exceed token limit (%d) by %d πͺ", res.TokensAdded, shared.MaxContextTokens, overage)os.Exit(1)}- elapsed := time.Since(timeStart)- if elapsed < 700*time.Millisecond {- time.Sleep(700*time.Millisecond - elapsed)- }-- s.Stop()- term.ClearCurrentLine()- fmt.Println("β " + msg)-- if len(contextParts) > 0 {- tableString := TableForLoadContext(contextParts)-- err = GitCommitContextUpdate(msg + "\n\n" + tableString)- if err != nil {- s.Stop()- term.ClearCurrentLine()- fmt.Fprintf(os.Stderr, "Failed to commit context update to git: %v\n", err)- os.Exit(1)- }- fmt.Println(tableString)- }-- return tokensAdded, totalTokens+ fmt.Println(res.Msg)}diff --git a/cli/lib/context_update.go b/cli/lib/context_update.goindex 346346e6..0b34a48d 100644--- a/cli/lib/context_update.go+++ b/cli/lib/context_update.go@@ -4,38 +4,20 @@ import ("crypto/sha256""encoding/hex""fmt"- "math""os"+ "plandex/api""plandex/term""plandex/types""plandex/url"- "strconv""strings""sync""time""github.com/briandowns/spinner"- "github.com/olekukonko/tablewriter""github.com/plandex/plandex/shared")-type contextUpdate struct {- Path string- Part *shared.ModelContextPart-}--type updateRes struct {- UpdatedParts []*shared.ModelContextPart- TokenDiffsByName map[string]int- TokensDiff int- TotalTokens int- MaxExceeded bool- NumFiles int- NumUrls int- NumTrees int-}--func MustUpdateContextWithOuput() *updateRes {+func MustUpdateContextWithOuput() {timeStart := time.Now()s := spinner.New(spinner.CharSets[33], 100*time.Millisecond)@@ -59,216 +41,80 @@ func MustUpdateContextWithOuput() *updateRes {os.Exit(1)}- maxExceeded := updateRes.MaxExceeded- updatedParts := updateRes.UpdatedParts- tokensDiff := updateRes.TokensDiff- totalTokens := updateRes.TotalTokens- numFiles := updateRes.NumFiles- numTrees := updateRes.NumTrees- numUrls := updateRes.NumUrls-- if maxExceeded {- stopFn()- overage := totalTokens - shared.MaxContextTokens- fmt.Printf("π¨ Update would add %d πͺ and exceed token limit (%d) by %d πͺ", tokensDiff, shared.MaxContextTokens, overage)- os.Exit(1)- }-- if len(updatedParts) == 0 {- stopFn()- fmt.Println("β Context is already up to date")- os.Exit(1)- }-- msg := "Updated"-- var toAdd []string- if numFiles > 0 {- postfix := "s"- if numFiles == 1 {- postfix = ""- }- toAdd = append(toAdd, fmt.Sprintf("%d file%s", numFiles, postfix))- }- if numTrees > 0 {- postfix := "s"- if numTrees == 1 {- postfix = ""- }- toAdd = append(toAdd, fmt.Sprintf("%d tree%s", numTrees, postfix))- }- if numUrls > 0 {- postfix := "s"- if numUrls == 1 {- postfix = ""- }- toAdd = append(toAdd, fmt.Sprintf("%d url%s", numUrls, postfix))- }-- if len(toAdd) <= 2 {- msg += " " + strings.Join(toAdd, " and ")- } else {- for i, add := range toAdd {- if i == len(toAdd)-1 {- msg += ", and " + add- } else {- msg += ", " + add- }- }- }-- msg += " in context"-- action := "added"- if tokensDiff < 0 {- action = "removed"- }- absTokenDiff := int(math.Abs(float64(tokensDiff)))- msg += fmt.Sprintf(" | %s β %d πͺ | total β %d πͺ", action, absTokenDiff, totalTokens)-- err = GitCommitContextUpdate(msg + "\n\n" + TableForContextUpdateRes(updateRes))-stopFn()- if err != nil {- fmt.Println("Error committing context update:", err)- os.Exit(1)- }-- fmt.Println("β " + msg)-- return updateRes-}--func TableForContextUpdateRes(updateRes *updateRes) string {- updatedParts := updateRes.UpdatedParts- tokenDiffsByName := updateRes.TokenDiffsByName-- if len(updatedParts) == 0 {- return ""- }-- tableString := &strings.Builder{}- table := tablewriter.NewWriter(tableString)- table.SetHeader([]string{"Name", "Type", "πͺ"})- table.SetAutoWrapText(false)-- for _, part := range updatedParts {- t, icon := GetContextTypeAndIcon(part)- diff := tokenDiffsByName[part.Name]-- diffStr := "+" + strconv.Itoa(diff)- tableColor := tablewriter.FgHiGreenColor-- if diff < 0 {- diffStr = strconv.Itoa(diff)- tableColor = tablewriter.FgHiRedColor- }-- row := []string{- " " + icon + " " + part.Name,- t,- diffStr,- }-- table.Rich(row, []tablewriter.Colors{- {tableColor, tablewriter.Bold},- {tableColor},- {tableColor},- })- }-- table.Render()+ fmt.Println("β " + updateRes.Msg)- return tableString.String()}-func UpdateContext() (*updateRes, error) {+func UpdateContext() (*types.ContextOutdatedResult, error) {return checkOutdatedAndMaybeUpdateContext(true)}-func CheckOutdatedContext() (*updateRes, error) {+func CheckOutdatedContext() (*types.ContextOutdatedResult, error) {return checkOutdatedAndMaybeUpdateContext(false)}-func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*updateRes, error) {- maxTokens := shared.MaxContextTokens- planState, err := GetPlanState()- if err != nil {- return nil, fmt.Errorf("error retrieving plan state: %w", err)- }-- tokenDiffsByName := make(map[string]int)- tokensDiff := 0- totalTokens := planState.ContextTokens- totalUpdatableTokens := planState.ContextUpdatableTokens- var mu sync.Mutex+func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*types.ContextOutdatedResult, error) {- context, err := GetAllContext(true)+ contexts, err := api.Client.ListContext(CurrentPlanId)if err != nil {return nil, fmt.Errorf("error retrieving context: %w", err)}- ts := shared.StringTs()-- updateCh := make(chan *contextUpdate, len(context))- errCh := make(chan error, len(context))+ var errs []error+ req := shared.UpdateContextRequest{}+ var updatedContexts []*shared.Context+ var numFiles int+ var numUrls int+ var numTrees int+ var mu sync.Mutexvar wg sync.WaitGroup- for _, part := range context {- contextPath := CreateContextFileName(part.Name, part.Sha)+ for _, context := range contexts {- if part.Type == shared.ContextFileType {+ if context.ContextType == shared.ContextFileType {wg.Add(1)- go func(part *shared.ModelContextPart) {+ go func(context *shared.Context) {defer wg.Done()- fileContent, err := os.ReadFile(part.FilePath)+ fileContent, err := os.ReadFile(context.FilePath)++ mu.Lock()+ defer mu.Unlock()if err != nil {- errCh <- fmt.Errorf("failed to read the file %s: %v", part.FilePath, err)+ errs = append(errs, fmt.Errorf("failed to read the file %s: %v", context.FilePath, err))return}hash := sha256.Sum256(fileContent)sha := hex.EncodeToString(hash[:])- if sha == part.Sha {- updateCh <- nil- } else {+ if sha != context.Sha {+ numFiles+++ updatedContexts = append(updatedContexts, context)+body := string(fileContent)- numTokens, err := shared.GetNumTokens(body)- if err != nil {- errCh <- fmt.Errorf("failed to get the number of tokens in the file %s: %v", part.FilePath, err)- return- }- mu.Lock()- tokensDiff += numTokens - part.NumTokens- tokenDiffsByName[part.Name] = numTokens - part.NumTokens- mu.Unlock()-- updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{- Type: shared.ContextFileType,- Name: part.Name,- FilePath: part.FilePath,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: part.AddedAt,- UpdatedAt: ts,- }}+ req[context.Id] = &shared.UpdateContextParams{+ Body: body,+ }}- }(part)+ }(context)- } else if part.Type == shared.ContextDirectoryTreeType {+ } else if context.ContextType == shared.ContextDirectoryTreeType {wg.Add(1)- go func(part *shared.ModelContextPart) {+ go func(context *shared.Context) {defer wg.Done()- flattenedPaths, err := ParseInputPaths([]string{part.FilePath}, &types.LoadContextParams{+ flattenedPaths, err := ParseInputPaths([]string{context.FilePath}, &types.LoadContextParams{NamesOnly: true,})++ mu.Lock()+ defer mu.Unlock()+if err != nil {- errCh <- fmt.Errorf("failed to get the directory tree %s: %v", part.FilePath, err)+ errs = append(errs, fmt.Errorf("failed to get the directory tree %s: %v", context.FilePath, err))return}body := strings.Join(flattenedPaths, "\n")@@ -277,151 +123,69 @@ func checkOutdatedAndMaybeUpdateContext(doUpdate bool) (*updateRes, error) {hash := sha256.Sum256(bytes)sha := hex.EncodeToString(hash[:])- if sha == part.Sha {- updateCh <- nil- } else {- numTokens, err := shared.GetNumTokens(body)- if err != nil {- errCh <- fmt.Errorf("failed to get the number of tokens in the directory tree %s: %v", part.FilePath, err)- return+ if sha != context.Sha {+ numTrees+++ updatedContexts = append(updatedContexts, context)+ req[context.Id] = &shared.UpdateContextParams{+ Body: body,}-- mu.Lock()- tokensDiff += numTokens - part.NumTokens- tokenDiffsByName[part.Name] = numTokens - part.NumTokens- mu.Unlock()-- updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{- Type: shared.ContextDirectoryTreeType,- Name: part.Name,- FilePath: part.FilePath,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: part.AddedAt,- UpdatedAt: ts,- }}}- }(part)+ }(context)- } else if part.Type == shared.ContextURLType {+ } else if context.ContextType == shared.ContextURLType {wg.Add(1)- go func(part *shared.ModelContextPart) {+ go func(part *shared.Context) {defer wg.Done()body, err := url.FetchURLContent(part.Url)++ mu.Lock()+ defer mu.Unlock()+if err != nil {- errCh <- fmt.Errorf("failed to fetch the URL %s: %v", part.Url, err)+ errs = append(errs, fmt.Errorf("failed to fetch the URL %s: %v", part.Url, err))return}hash := sha256.Sum256([]byte(body))sha := hex.EncodeToString(hash[:])- if sha == part.Sha {- updateCh <- nil- } else {- numTokens, err := shared.GetNumTokens(body)- if err != nil {- errCh <- fmt.Errorf("failed to get the number of tokens in the URL %s: %v", part.Url, err)- return+ if sha != part.Sha {+ numUrls+++ updatedContexts = append(updatedContexts, part)+ req[part.Id] = &shared.UpdateContextParams{+ Body: body,}-- mu.Lock()- tokensDiff += numTokens - part.NumTokens- tokenDiffsByName[part.Name] = numTokens - part.NumTokens- mu.Unlock()-- updateCh <- &contextUpdate{Path: contextPath, Part: &shared.ModelContextPart{- Type: shared.ContextURLType,- Name: part.Name,- Url: part.Url,- Body: body,- Sha: sha,- NumTokens: numTokens,- AddedAt: part.AddedAt,- UpdatedAt: ts,- }}}- }(part)- } else {- updateCh <- nil+ }(context)}}wg.Wait()- updatedPaths := []string{}- updatedParts := []*shared.ModelContextPart{}-- for range context {- select {- case err := <-errCh:- return nil, err- case update := <-updateCh:- if update != nil {- updatedPaths = append(updatedPaths, update.Path)- updatedParts = append(updatedParts, update.Part)- }- }+ if len(errs) > 0 {+ return nil, fmt.Errorf("failed to check context outdated: %v", errs)}- totalTokens += tokensDiff- totalUpdatableTokens += tokensDiff-- if doUpdate {- if totalTokens > maxTokens {- return &updateRes{- UpdatedParts: nil,- TokensDiff: tokensDiff,- TotalTokens: totalTokens,- MaxExceeded: true,- }, fmt.Errorf("context update would exceed the token limit of %d", maxTokens)- }+ var msg string- err = ContextRemoveFiles(updatedPaths)+ if len(req) == 0 {+ return &types.ContextOutdatedResult{+ Msg: "Context is up to date",+ }, nil+ } else if doUpdate {+ res, err := api.Client.UpdateContext(CurrentPlanId, req)if err != nil {- return nil, fmt.Errorf("error removing updated context paths: %w", err)- }-- err = writeContextParts(updatedParts)- if err != nil {- return nil, fmt.Errorf("error writing updated context parts: %w", err)- }-- planState.ContextTokens = totalTokens- planState.ContextUpdatableTokens = totalUpdatableTokens- err = SetPlanState(planState, ts)-- if err != nil {- return nil, fmt.Errorf("error writing plan state: %w", err)- }- }-- var numFiles int- var numUrls int- var numTrees int- for _, part := range updatedParts {- if part.Type == shared.ContextFileType {- numFiles++- }- if part.Type == shared.ContextURLType {- numUrls++- }- if part.Type == shared.ContextDirectoryTreeType {- numTrees+++ return nil, fmt.Errorf("failed to update context: %w", err)}+ msg = res.Msg}- return &updateRes{- UpdatedParts: updatedParts,- TokenDiffsByName: tokenDiffsByName,- TokensDiff: tokensDiff,- TotalTokens: totalTokens,- MaxExceeded: totalTokens > maxTokens,- NumFiles: numFiles,- NumUrls: numUrls,- NumTrees: numTrees,+ return &types.ContextOutdatedResult{+ Msg: msg,+ UpdatedContexts: updatedContexts,+ NumFiles: numFiles,+ NumUrls: numUrls,+ NumTrees: numTrees,}, nil-}diff --git a/cli/lib/conversation.go b/cli/lib/conversation.godeleted file mode 100644index f6aa93f7..00000000--- a/cli/lib/conversation.go+++ /dev/null@@ -1,290 +0,0 @@-package lib--import (- "bufio"- "fmt"- "os"- "path/filepath"- "plandex/types"- "sort"- "strconv"- "strings"-- "github.com/plandex/plandex/shared"- openai "github.com/sashabaranov/go-openai"-)--func LoadConversation() ([]*shared.ConversationMessage, error) {- var messages []*shared.ConversationMessage-- files, err := os.ReadDir(ConversationSubdir)- if err != nil {- return nil, err- }-- for _, file := range files {- // Ensure we are only reading files, not directories- if file.IsDir() {- continue- }- filePath := ConversationSubdir + "/" + file.Name()- file, err := os.Open(filePath)- if err != nil {- return nil, err- }-- scanner := bufio.NewScanner(file)- var currentRole string- var currentTokens int- var currentTimestamp string- var contentBuffer []string-- for scanner.Scan() {- line := scanner.Text()-- // Check if the line starts with user or response indicator- if strings.HasPrefix(line, "@@@!>user|") {- if currentRole != "" {- // Save the previous message before starting a new one- messages = append(messages, &shared.ConversationMessage{- Message: openai.ChatCompletionMessage{- Role: currentRole,- Content: strings.Join(contentBuffer, "\n"),- },- Tokens: currentTokens,- Timestamp: currentTimestamp,- })- contentBuffer = []string{}- }- currentRole = openai.ChatMessageRoleUser- // Parse the number of tokens from the line (tokens only)- split := strings.Split(line, "|")- currentTimestamp = split[1]- currentTokensStr := split[2]- currentTokens, err = strconv.Atoi(currentTokensStr)- if err != nil {- return nil, err- }-- continue- } else if strings.HasPrefix(line, "@@@!>response|") {- if currentRole != "" {- // Save the previous message before starting a new one- messages = append(messages, &shared.ConversationMessage{- Message: openai.ChatCompletionMessage{- Role: currentRole,- Content: strings.Join(contentBuffer, "\n"),- },- Tokens: currentTokens,- Timestamp: currentTimestamp,- })- contentBuffer = []string{}- }- currentRole = openai.ChatMessageRoleAssistant- split := strings.Split(line, "|")- currentTimestamp = split[1]- currentTokensStr := split[2]- currentTokens, err = strconv.Atoi(currentTokensStr)- if err != nil {- return nil, err- }- continue- }-- // Add content to the buffer- contentBuffer = append(contentBuffer, line)- }-- // Add the last message in the file- if currentRole != "" && len(contentBuffer) > 0 {- messages = append(messages, &shared.ConversationMessage{- Message: openai.ChatCompletionMessage{- Role: currentRole,- Content: strings.Join(contentBuffer, "\n"),- },- Tokens: currentTokens,- Timestamp: currentTimestamp,- })- }-- file.Close()- }-- return messages, nil-}--func LoadSummaries() ([]*shared.ConversationSummary, error) {- // check if summaries subdirectory exists- summariesPath := filepath.Join(ConversationSubdir, "summaries")- exists := false- _, err := os.Stat(summariesPath)- if err == nil {- exists = true- } else if !os.IsNotExist(err) {- return nil, err- }-- var summaries []*shared.ConversationSummary- if exists {-- files, err := os.ReadDir(summariesPath)- if err != nil {- return nil, fmt.Errorf("failed to read summaries directory: %s", err)- }-- for _, file := range files {- // Ensure we are only reading files, not directories- if file.IsDir() {- continue- }- filePath := summariesPath + "/" + file.Name()-- bytes, err := os.ReadFile(filePath)-- if err != nil {- return nil, fmt.Errorf("failed to read summary file: %s", err)- }-- content := string(bytes)-- header := strings.Split(content, "\n")[0]- summary := strings.Join(strings.Split(content, "\n")[2:], "\n")-- headerSplit := strings.Split(header, "|")- timestamp := headerSplit[1]- tokensStr := headerSplit[2]- tokens, err := strconv.Atoi(tokensStr)- if err != nil {- return nil, fmt.Errorf("failed to parse tokens: %s", err)- }-- summaries = append(summaries, &shared.ConversationSummary{- Summary: summary,- LastMessageTimestamp: timestamp,- Tokens: tokens,- })- }- }-- // sort by timestamp- sort.Slice(summaries, func(i, j int) bool {- return summaries[i].LastMessageTimestamp < summaries[j].LastMessageTimestamp- })-- return summaries, nil-}--func appendConversation(params types.AppendConversationParams) error {- // Create or append to conversation file- conversationFilePath := filepath.Join(ConversationSubdir, fmt.Sprintf("%s.md", params.Timestamp))-- var header string- var body string- var tokens int- var t string- if params.PromptParams != nil {- t = "prompt"- header = fmt.Sprintf("@@@!>user|%s|%d\n\n", params.Timestamp, params.PromptParams.PromptTokens)- body = params.PromptParams.Prompt- tokens = params.PromptParams.PromptTokens- } else if params.ReplyParams != nil {- t = "reply"- header = fmt.Sprintf("@@@!>response|%s|%d\n\n", params.ReplyParams.ResponseTimestamp, params.ReplyParams.ReplyTokens)- body = params.ReplyParams.Reply- tokens = params.ReplyParams.ReplyTokens- } else {- return fmt.Errorf("invalid params: either prompt or reply params must be provided")- }-- conversationFileContents := fmt.Sprintf("%s%s\n\n", header, body)- conversationFile, err := os.OpenFile(conversationFilePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)- if err != nil {- return fmt.Errorf("failed to write conversation file: %s", err)- }- defer conversationFile.Close()-- _, err = conversationFile.WriteString(conversationFileContents)- if err != nil {- return fmt.Errorf("failed to write conversation file: %s", err)- }-- params.PlanState.NumMessages++- params.PlanState.ConvoTokens += tokens- err = SetPlanState(params.PlanState, shared.StringTs())- if err != nil {- return fmt.Errorf("failed to update plan state: %s", err)- }-- var desc string- if t == "prompt" {- desc = "π¬ user prompt"- } else {- desc = "π€ Plandex reply"- }- err = GitCommitConvoUpdate(fmt.Sprintf("Message #%d | %s | %d πͺ", params.PlanState.NumMessages, desc, tokens))-- if err != nil {- return fmt.Errorf("failed to commit conversation update: %s", err)- }-- return nil-}--func saveLatestConvoSummary(rootId string) error {- summariesPath := filepath.Join(ConversationSubdir, "summaries")- exists := false- _, err := os.Stat(summariesPath)- if err == nil {- exists = true- } else if !os.IsNotExist(err) {- return err- }-- if !exists {- err = os.Mkdir(summariesPath, 0755)- if err != nil {- return err- }- }-- // get latest summary from directory- files, err := os.ReadDir(summariesPath)- if err != nil {- return fmt.Errorf("failed to read summaries directory: %s", err)- }-- var latestTimestamp string- if len(files) > 0 {- sort.Slice(files, func(i, j int) bool {- return files[i].Name() < files[j].Name()- })- latestFile := files[len(files)-1]- latestTimestamp = strings.Split(latestFile.Name(), ".")[0]- }-- summary, err := Api.ConvoSummary(rootId, latestTimestamp)- if err != nil {- return fmt.Errorf("failed to get convo summary: %s", err)- }-- if summary == nil {- return nil- }-- summaryFilePath := filepath.Join(summariesPath, fmt.Sprintf("%s.md", summary.LastMessageTimestamp))- summaryFileContents := fmt.Sprintf("@@@!>summary|%s|%d\n\n%s", summary.LastMessageTimestamp, summary.Tokens, summary.Summary)-- err = os.WriteFile(summaryFilePath, []byte(summaryFileContents), 0644)-- if err != nil {- return fmt.Errorf("failed to write summary file: %s", err)- }-- err = GitCommitConvoUpdate(fmt.Sprintf("π€ Summarized up to message #%d | %d πͺ", summary.NumMessages-1, summary.Tokens))-- if err != nil {- return fmt.Errorf("failed to commit conversation update: %s", err)- }-- return err-}diff --git a/cli/lib/current_plan.go b/cli/lib/current_plan.godeleted file mode 100644index 8555a369..00000000--- a/cli/lib/current_plan.go+++ /dev/null@@ -1,257 +0,0 @@-package lib--import (- "encoding/json"- "fmt"- "os"- "path/filepath"- "plandex/types"- "sort"-- "github.com/plandex/plandex/shared"-)--func GetCurrentPlanFiles() (*shared.CurrentPlanFiles, error) {- res, err := GetCurrentPlanState()- if err != nil {- return nil, err- }- return res.CurrentPlanFiles, err-}--func GetCurrentPlanState() (*types.CurrentPlanState, error) {- return GetCurrentPlanStateBeforeReplacement("")-}--func GetCurrentPlanStateBeforeReplacement(id string) (*types.CurrentPlanState, error) {- errCh := make(chan error, 1)- planResInfoCh := make(chan *types.PlanResultsInfo, 1)- contextCh := make(chan shared.ModelContext, 1)-- go func() {- modelContext, err := GetAllContext(false)- if err != nil {- errCh <- fmt.Errorf("error loading context: %v", err)- return- }- contextCh <- modelContext- }()-- go func() {- planResInfo, err := GetPlanResultsInfo()- if err != nil {- errCh <- fmt.Errorf("error getting plan results: %v", err)- return- }-- planResInfoCh <- planResInfo- }()-- var planResInfo *types.PlanResultsInfo- var modelContext shared.ModelContext-- for i := 0; i < 2; i++ {- select {- case err := <-errCh:- return nil, err- case planResInfo = <-planResInfoCh:- case modelContext = <-contextCh:- }- }-- files := make(map[string]string)- shas := make(map[string]string)-- for _, contextPart := range modelContext {- if contextPart.FilePath == "" {- continue- }-- _, hasPath := planResInfo.PlanResByPath[contextPart.FilePath]-- // fmt.Printf("hasPath: %v\n", hasPath)-- if hasPath {- files[contextPart.FilePath] = contextPart.Body- shas[contextPart.FilePath] = contextPart.Sha- }- }-- for path, planResults := range planResInfo.PlanResByPath {- updated := files[path]-- // fmt.Printf("path: %s\n", path)- // fmt.Printf("updated: %s\n", updated)-- PlanResLoop:- for _, planRes := range planResults {- if !planRes.IsPending() {- continue- }-- if len(planRes.Replacements) == 0 {- if updated != "" {- return nil, fmt.Errorf("plan updates out of order: %s", path)- }-- updated = planRes.Content- files[path] = updated- continue- }-- contextSha := shas[path]-- if contextSha != "" && planRes.ContextSha != contextSha {- return nil, fmt.Errorf("result sha doesn't match context sha: %s", path)- }-- if len(planRes.Replacements) == 0 {- continue- }-- replacements := []*shared.Replacement{}- for _, replacement := range planRes.Replacements {- if replacement.Id == id {- break PlanResLoop- }- replacements = append(replacements, replacement)- }-- var allSucceeded bool- updated, allSucceeded = shared.ApplyReplacements(updated, replacements, false)-- if !allSucceeded {- return nil, fmt.Errorf("plan replacement failed: %s", path)- }- }-- files[path] = updated- }-- return &types.CurrentPlanState{- CurrentPlanFiles: &shared.CurrentPlanFiles{Files: files, ContextShas: shas},- ModelContext: modelContext,- ContextByPath: modelContext.ByPath(),- PlanResultsInfo: *planResInfo,- }, nil-}--func SetPendingResultsApplied(planResByPath shared.PlanResultsByPath) error {- ts := shared.StringTs()- numPending := planResByPath.NumPending()- planResByPath.SetApplied(ts)-- errCh := make(chan error, numPending)-- for _, planResults := range planResByPath {- for _, planResult := range planResults {- // only write results that were just applied- if planResult.AppliedAt != ts {- continue- }-- go func(planResult *shared.PlanResult) {- bytes, err := json.Marshal(planResult)- if err != nil {- errCh <- fmt.Errorf("error marshalling plan result: %v", err)- return- }-- path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")-- err = os.WriteFile(path, bytes, 0644)- if err != nil {- errCh <- fmt.Errorf("error writing plan result: %v", err)- return- }-- errCh <- nil-- }(planResult)- }- }-- for i := 0; i < numPending; i++ {- err := <-errCh- if err != nil {- return fmt.Errorf("error wrting plan result file after SetApplied: %v", err)- }- }-- return nil-}--func GetPlanResultsInfo() (*types.PlanResultsInfo, error) {- resByPath := make(shared.PlanResultsByPath)- replacementsByPath := make(map[string][]*shared.Replacement)- var paths []string-- _, err := os.Stat(ResultsSubdir)- resDirExists := !os.IsNotExist(err)-- if !resDirExists {- return &types.PlanResultsInfo{- PlanResByPath: resByPath,- ReplacementsByPath: replacementsByPath,- }, nil- }-- err = filepath.Walk(ResultsSubdir, func(path string, info os.FileInfo, err error) error {- if err != nil {- return fmt.Errorf("error reading results dir: %v", err)- }-- if info.IsDir() {- return nil- }-- bytes, err := os.ReadFile(path)- if err != nil {- return fmt.Errorf("error reading file %s: %v", path, err)- }-- var planRes shared.PlanResult- err = json.Unmarshal(bytes, &planRes)- if err != nil {- return fmt.Errorf("error unmarshalling plan result JSON from file %s: %v", path, err)- }-- if planRes.IsPending() {- _, hasPath := resByPath[planRes.Path]-- resByPath[planRes.Path] = append(resByPath[planRes.Path], &planRes)-- if !hasPath {- paths = append(paths, planRes.Path)- }- }-- return nil- })-- if err != nil {- return nil, fmt.Errorf("error reading results dir: %v", err)- }-- for _, results := range resByPath {- // sort results by timestamp ascending- sort.Slice(results, func(i, j int) bool {- return results[i].Ts < results[j].Ts- })-- for _, planRes := range results {- replacementsByPath[planRes.Path] = append(replacementsByPath[planRes.Path], planRes.Replacements...)- }- }-- // sort paths ascending- sort.Slice(paths, func(i, j int) bool {- return paths[i] < paths[j]- })-- return &types.PlanResultsInfo{- PlanResByPath: resByPath,- SortedPaths: paths,- ReplacementsByPath: replacementsByPath,- }, nil-}diff --git a/cli/lib/drop_changes.go b/cli/lib/drop_changes.godeleted file mode 100644index a08f7cb3..00000000--- a/cli/lib/drop_changes.go+++ /dev/null@@ -1,135 +0,0 @@-package lib--import (- "encoding/json"- "fmt"- "os"- "path/filepath"-- "github.com/plandex/plandex/shared"-)--func DropChangesWithOutput(name string) error {- plandexDir, _, err := FindOrCreatePlandex()- if err != nil {- return fmt.Errorf("error finding or creating plandex dir: %w", err)- }-- if name == "" {- return fmt.Errorf("no plan specified and no current plan")- }-- rootDir := filepath.Join(plandexDir, name)-- _, err = os.Stat(rootDir)-- if os.IsNotExist(err) {- return fmt.Errorf("plan with name '%s' doesn't exist", name)- } else if err != nil {- return fmt.Errorf("error checking if plan exists: %w", err)- }-- res, err := GetCurrentPlanState()-- if err != nil {- return fmt.Errorf("error getting current plan files: %w", err)- }-- planResByPath := res.PlanResByPath- ts := shared.StringTs()- numRejected := planResByPath.SetRejected(ts)- errCh := make(chan error, numRejected)-- for _, planResults := range planResByPath {- for _, planResult := range planResults {- if planResult.RejectedAt != ts {- continue- }-- go func(planResult *shared.PlanResult) {- bytes, err := json.Marshal(planResult)- if err != nil {- errCh <- fmt.Errorf("error marshalling plan result: %v", err)- return- }-- path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")-- err = os.WriteFile(path, bytes, 0644)- if err != nil {- errCh <- fmt.Errorf("error writing plan result: %v", err)- return- }-- errCh <- nil-- }(planResult)- }- }-- for i := 0; i < numRejected; i++ {- err := <-errCh- if err != nil {- return fmt.Errorf("error wrting plan result file after SetApplied: %v", err)- }- }-- fmt.Println("ποΈ Pending changes dropped")-- return nil-}--func DropChange(name, path string, replacement *shared.Replacement) error {- plandexDir, _, err := FindOrCreatePlandex()- if err != nil {- return fmt.Errorf("error finding or creating plandex dir: %w", err)- }-- if name == "" {- return fmt.Errorf("no plan specified and no current plan")- }-- rootDir := filepath.Join(plandexDir, name)-- _, err = os.Stat(rootDir)-- if os.IsNotExist(err) {- return fmt.Errorf("plan with name '%s' doesn't exist", name)- } else if err != nil {- return fmt.Errorf("error checking if plan exists: %w", err)- }-- res, err := GetCurrentPlanState()-- if err != nil {- return fmt.Errorf("error getting current plan files: %w", err)- }-- planResByPath := res.PlanResByPath- planResults := planResByPath[path]- ts := shared.StringTs()-- for _, planResult := range planResults {- for _, resultReplacement := range planResult.Replacements {- if resultReplacement.Id == replacement.Id {- replacement.SetRejected(ts)- break- }- }-- bytes, err := json.Marshal(planResult)- if err != nil {- return fmt.Errorf("error marshalling plan result: %v", err)- }-- path := filepath.Join(ResultsSubdir, planResult.Path, planResult.Ts+".json")-- err = os.WriteFile(path, bytes, 0644)- if err != nil {- return fmt.Errorf("error writing plan result: %v", err)- }- }-- return fmt.Errorf("change with ID '%s' doesn't exist in plan '%s' at path '%s'", replacement.Id, name, path)--}diff --git a/cli/lib/fs.go b/cli/lib/fs.goindex 3e8b3053..c2581678 100644--- a/cli/lib/fs.go+++ b/cli/lib/fs.go@@ -3,9 +3,9 @@ package libimport ("encoding/json""fmt"- "io""os""path/filepath"+ "plandex/api""plandex/types""sync"@@ -18,12 +18,13 @@ var ProjectRoot stringvar HomePlandexDir stringvar CacheDir string-var CurrentPlanName string-var CurrentPlanDir string-var ResultsSubdir string-var DescriptionsSubdir string-var ConversationSubdir string-var ContextSubdir string+var CurrentProjectId string+var CurrentOrgId string+var CurrentUserId string+var CurrentPlanId string++var HomeCurrentProjectDir string+var HomeCurrentPlanPath stringfunc init() {var err error@@ -32,103 +33,103 @@ func init() {panic(err)}- wg := sync.WaitGroup{}+ home, err := os.UserHomeDir()+ if err != nil {+ panic(err)+ }+ HomePlandexDir = filepath.Join(home, ".plandex-home")+ CacheDir = filepath.Join(HomePlandexDir, "cache")+ err = os.MkdirAll(filepath.Join(CacheDir, "tiktoken"), os.ModePerm)+ if err != nil {+ panic(err)+ }+ err = os.Setenv("TIKTOKEN_CACHE_DIR", CacheDir)+ if err != nil {+ panic(err)+ }- wg.Add(1)- go func() {- defer wg.Done()+ PlandexDir, ProjectRoot = findPlandex()+}- home, err := os.UserHomeDir()- if err != nil {- panic(err)- }- HomePlandexDir = filepath.Join(home, ".plandex")- CacheDir = filepath.Join(HomePlandexDir, "cache")- err = os.MkdirAll(filepath.Join(CacheDir, "tiktoken"), os.ModePerm)- if err != nil {- panic(err)- }- err = os.Setenv("TIKTOKEN_CACHE_DIR", CacheDir)+func MustResolveProject() {+ if PlandexDir == "" {+ _, _, err := findOrCreatePlandex()if err != nil {- panic(err)+ panic(fmt.Errorf("error finding or creating plandex: %v", err))}- }()+ }- wg.Add(1)- go func() {- defer wg.Done()+ if PlandexDir == "" || ProjectRoot == "" {+ panic(fmt.Errorf("could not find or create plandex directory"))+ }- PlandexDir, ProjectRoot = findPlandex()+ // check if project.json exists in PlandexDir+ path := filepath.Join(PlandexDir, "project.json")+ _, err := os.Stat(path)- err = LoadCurrentPlan()- if err != nil {- panic(err)- }- }()-- wg.Wait()-}+ if os.IsNotExist(err) {+ mustInitProject()+ } else if err != nil {+ panic(fmt.Errorf("error checking if project.json exists: %v", err))+ }-func LoadCurrentPlan() error {- // Construct the path to the current_plan.json file- path := filepath.Join(PlandexDir, "current_plan.json")+ // read project.json+ bytes, err := os.ReadFile(path)- // Check if the file exists- if _, err := os.Stat(path); os.IsNotExist(err) {- return nil+ if err != nil {+ panic(fmt.Errorf("error reading project.json: %v", err))}- // Read the contents of the file- fileBytes, err := os.ReadFile(path)+ var settings types.ProjectSettings+ err = json.Unmarshal(bytes, &settings)+if err != nil {- return fmt.Errorf("error reading current_plan.json: %v", err)+ panic(fmt.Errorf("error unmarshalling project.json: %v", err))}- // Unmarshal the JSON data into the shared.PlanSettings type- var planSettings types.PlanSettings- err = json.Unmarshal(fileBytes, &planSettings)+ CurrentProjectId = settings.Id+ CurrentOrgId = settings.OrgId++ HomeCurrentProjectDir = filepath.Join(HomePlandexDir, CurrentProjectId)+ HomeCurrentPlanPath = filepath.Join(HomeCurrentProjectDir, "current_plan.json")++ err = os.MkdirAll(HomeCurrentProjectDir, os.ModePerm)+if err != nil {- return fmt.Errorf("error unmarshalling current_plan.json: %v", err)+ panic(fmt.Errorf("error creating project dir: %v", err))}- CurrentPlanName = planSettings.Name- CurrentPlanDir = filepath.Join(PlandexDir, CurrentPlanName)- ResultsSubdir = filepath.Join(CurrentPlanDir, "results")- DescriptionsSubdir = filepath.Join(CurrentPlanDir, "descriptions")- ConversationSubdir = filepath.Join(CurrentPlanDir, "conversation")- ContextSubdir = filepath.Join(CurrentPlanDir, "context")-- return nil+ MustLoadCurrentPlan()}-func FindOrCreatePlandex() (string, bool, error) {- var err error+func MustLoadCurrentPlan() {+ if CurrentProjectId == "" {+ panic("No current project")+ }- // Determine the directory path- dir := filepath.Join(Cwd, ".plandex")+ // Check if the file exists+ _, err := os.Stat(HomeCurrentPlanPath)- // Check if the directory already exists- _, err = os.Stat(dir)+ if os.IsNotExist(err) {+ return+ } else if err != nil {+ panic(fmt.Errorf("error checking if current_plan.json exists: %v", err))+ }++ // Read the contents of the file+ fileBytes, err := os.ReadFile(HomeCurrentPlanPath)if err != nil {- if os.IsNotExist(err) {- // If not found, create in the current directory- err = os.Mkdir(dir, os.ModePerm)- if err != nil {- return "", false, err- }- PlandexDir = dir- return dir, true, nil- }- return "", false, err+ panic(fmt.Errorf("error reading current_plan.json: %v", err))}- return dir, false, nil-}+ // Unmarshal the JSON data into the shared.PlanSettings type+ var planSettings types.PlanSettings+ err = json.Unmarshal(fileBytes, &planSettings)+ if err != nil {+ panic(fmt.Errorf("error unmarshalling current_plan.json: %v", err))+ }-func CwdIsPlan() bool {- // check if parent directory of cwd is '.plandex'- parentDir := filepath.Dir(Cwd)- return parentDir == PlandexDir+ CurrentPlanId = planSettings.Id}func ParseInputPaths(fileOrDirPaths []string, params *types.LoadContextParams) ([]string, error) {@@ -199,84 +200,77 @@ func ParseInputPaths(fileOrDirPaths []string, params *types.LoadContextParams) (return resPaths, nil}-func CopyFile(src, dst string) error {- srcFile, err := os.Open(src)+func mustInitProject() {+ res, err := api.Client.CreateProject(shared.CreateProjectRequest{Name: filepath.Base(ProjectRoot)})+if err != nil {- return err+ panic(fmt.Errorf("error creating project: %v", err))}- defer srcFile.Close()- // ensure parent directory exists- err = os.MkdirAll(filepath.Dir(dst), os.ModePerm)+ CurrentProjectId = res.Id++ // TODO: placeholder until orgs/auth are implemented+ CurrentOrgId = "org1"++ // write project.json+ path := filepath.Join(PlandexDir, "project.json")+ bytes, err := json.Marshal(types.ProjectSettings{+ Id: CurrentProjectId,+ OrgId: CurrentOrgId,+ })+if err != nil {- return err+ panic(fmt.Errorf("error marshalling project settings: %v", err))}- dstFile, err := os.Create(dst)+ err = os.WriteFile(path, bytes, os.ModePerm)+if err != nil {- return err+ panic(fmt.Errorf("error writing project.json: %v", err))}- defer dstFile.Close()- _, err = io.Copy(dstFile, srcFile)- return err-}+ // write current_plan.json to PlandexHomeDir/[projectId]/current_plan.json+ dir := filepath.Join(HomePlandexDir, CurrentProjectId)+ err = os.MkdirAll(dir, os.ModePerm)-func CopyDir(srcDir, dstDir string) error {- entries, err := os.ReadDir(srcDir)if err != nil {- return err+ panic(fmt.Errorf("error creating project dir: %v", err))}- for _, entry := range entries {- srcPath := filepath.Join(srcDir, entry.Name())- dstPath := filepath.Join(dstDir, entry.Name())+ path = filepath.Join(dir, "current_plan.json")+ bytes, err = json.Marshal(types.PlanSettings{+ Id: "",+ })- if entry.IsDir() {- err = CopyDir(srcPath, dstPath)- if err != nil {- return err- }- } else {- err = CopyFile(srcPath, dstPath)- if err != nil {- return err- }- }+ if err != nil {+ panic(fmt.Errorf("error marshalling plan settings: %v", err))}- return nil-}+ err = os.WriteFile(path, bytes, os.ModePerm)-func GetLatestPlanDescription() (*shared.PlanDescription, error) {- // List files in descriptions directory- entries, err := os.ReadDir(DescriptionsSubdir)if err != nil {- return nil, err+ panic(fmt.Errorf("error writing current_plan.json: %v", err))}+}- // Find the latest file (named by timestamp)- var latestFile os.DirEntry- for _, entry := range entries {- if latestFile == nil || entry.Name() > latestFile.Name() {- latestFile = entry- }- }+func findOrCreatePlandex() (string, bool, error) {+ PlandexDir, ProjectRoot = findPlandex()- // Read the contents of the latest file- bytes, err := os.ReadFile(filepath.Join(DescriptionsSubdir, latestFile.Name()))- if err != nil {- return nil, fmt.Errorf("error reading latest description file: %v", err)+ if PlandexDir != "" && ProjectRoot != "" {+ return PlandexDir, false, nil}- // Unmarshal the JSON data into the shared.PlanDescription type- var description shared.PlanDescription- err = json.Unmarshal(bytes, &description)+ // Determine the directory path+ dir := filepath.Join(Cwd, ".plandex")++ err := os.Mkdir(dir, os.ModePerm)if err != nil {- return nil, fmt.Errorf("error unmarshalling latest description file: %v", err)+ return "", false, err}+ PlandexDir = dir+ ProjectRoot = Cwd- return &description, nil+ return dir, true, nil}func findPlandex() (string, string) {diff --git a/cli/lib/git.go b/cli/lib/git.goindex fc20bd7e..8b4e2b45 100644--- a/cli/lib/git.go+++ b/cli/lib/git.go@@ -1,17 +1,11 @@package libimport (- "bytes""fmt""os/exec"- "path/filepath""plandex/types"- "strconv""strings""sync"- "time"-- "github.com/fatih/color")var gitMutex sync.Mutex@@ -28,41 +22,6 @@ func InitGitRepo(dir string) error {return GitAddAndCommit(dir, "New plan", false)}-func AddGitSubmodule(rootDir, submoduleDir string) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- // Calculate the relative path from rootDir to submoduleDir- relPath, err := filepath.Rel(rootDir, submoduleDir)- if err != nil {- return fmt.Errorf("error computing relative path from %s to %s: %v", rootDir, submoduleDir, err)- }-- // Ensure relative path starts with ./ or ../- if !strings.HasPrefix(relPath, "./") && !strings.HasPrefix(relPath, "../") {- relPath = "./" + relPath- }-- res, err := exec.Command("git", "-C", rootDir, "submodule", "add", relPath).CombinedOutput()- if err != nil {- return fmt.Errorf("error adding git submodule for dir: %s, err: %v, output: %s", submoduleDir, err, string(res))- }-- return nil-}--func UpdateAndInitSubmodules(rootDir string) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- res, err := exec.Command("git", "-C", rootDir, "submodule", "update", "--init", "--recursive").CombinedOutput()- if err != nil {- return fmt.Errorf("error updating git submodules for dir: %s, err: %v, output: %s", rootDir, err, string(res))- }-- return nil-}-func GitAddAndCommit(dir, message string, lockMutex bool) error {if lockMutex {gitMutex.Lock()@@ -211,26 +170,6 @@ func CwdIsGitRepo() bool {return isGitRepo}-func gitCommitRootUpdate(commitMsg string, lockMutex bool) error {- if lockMutex {- gitMutex.Lock()- defer gitMutex.Unlock()- }-- err := GitAdd(CurrentPlanDir, ".", false)- if err != nil {- return fmt.Errorf("failed to root plan dir changes: %s", err)- }-- // Commit these staged submodule changes in the root repo- err = GitCommit(CurrentPlanDir, commitMsg, false)- if err != nil {- return fmt.Errorf("failed to commit submodule updates in root dir: %s", err)- }-- return nil--}func GitClearUncommittedChanges() error {gitMutex.Lock()defer gitMutex.Unlock()@@ -242,207 +181,3 @@ func GitClearUncommittedChanges() error {return nil}--func GitCommitContextUpdate(commitMsg string) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- err := GitAddAndCommit(ContextSubdir, commitMsg, false)-- if err != nil {- return fmt.Errorf("failed to commit context: %v", err)- }-- err = GitAdd(CurrentPlanDir, ContextSubdir, false)- if err != nil {- return fmt.Errorf("failed to stage submodule changes in context dir: %s", err)- }-- return gitCommitRootUpdate(commitMsg, false)-}--func GitCommitConvoUpdate(commitMsg string) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- err := GitAddAndCommit(ConversationSubdir, commitMsg, false)-- if err != nil {- return fmt.Errorf("failed to commit convo update: %v", err)- }-- err = GitAdd(CurrentPlanDir, ConversationSubdir, false)- if err != nil {- return fmt.Errorf("failed to stage submodule changes in convo dir: %s", err)- }-- return gitCommitRootUpdate(commitMsg, false)-}--func GitCommitPlanUpdate(commitMsg string) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- err := GitAddAndCommit(ResultsSubdir, commitMsg, false)- if err != nil {- return fmt.Errorf("failed to commit files to plan dir: %s", err)- }-- return gitCommitRootUpdate(commitMsg, false)-}--// GetGitCommitHistory retrieves the git commit history for the given directory.-// It returns a formatted string with each commit's short SHA, timestamp, and message.-func GetGitCommitHistory(dir string) (string, error) {- gitMutex.Lock()- defer gitMutex.Unlock()-- var out bytes.Buffer- cmd := exec.Command("git", "log", "--pretty=%h@@|@@%at@@|@@%B@>>>@")- cmd.Dir = dir- cmd.Stdout = &out- err := cmd.Run()- if err != nil {- return "", fmt.Errorf("error getting git history for dir: %s, err: %v",- dir, err)- }-- // Process the log output to get it in the desired format.- history := processGitHistoryOutput(strings.TrimSpace(out.String()))-- var output []string- for _, el := range history {- output = append(output, el[1])- }-- return strings.Join(output, "\n\n"), nil-}--// GetLatestCommit retrieves the latest commit SHA, timestamp, and message for the given directory.-// It returns the short SHA along with a formatted commit message.-func GetLatestCommit(dir string) (string, string, error) {- gitMutex.Lock()- defer gitMutex.Unlock()-- var out bytes.Buffer- cmd := exec.Command("git", "log", "--pretty=%h@@|@@%at@@|@@%B@>>>@")- cmd.Dir = dir- cmd.Stdout = &out- err := cmd.Run()- if err != nil {- return "", "", fmt.Errorf("error getting git history for dir: %s, err: %v",- dir, err)- }-- // Process the log output to get it in the desired format.- history := processGitHistoryOutput(strings.TrimSpace(out.String()))-- first := history[0]-- return first[0], first[1], nil-}--// GitRewindSteps reverts the repository by a specified number of steps.-func GitRewindSteps(dir string, steps int, isRoot bool) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- res, err := exec.Command("git", "-C", dir, "reset", "--hard",- fmt.Sprintf("HEAD~%d", steps)).CombinedOutput()-- if err != nil {- return fmt.Errorf("error executing git reset for dir: %s, steps: %d, err: %v, output: %s", dir, steps, err, string(res))- }-- if isRoot {- // Update submodules- err = updateSubmodules(dir)- if err != nil {- return err- }- }-- return nil-}--// GitRewindToSHA reverts the repository to a specific commit SHA.-func GitRewindToSHA(dir, sha string, isRoot bool) error {- gitMutex.Lock()- defer gitMutex.Unlock()-- res, err := exec.Command("git", "-C", dir, "reset", "--hard",- sha).CombinedOutput()-- if err != nil {- return fmt.Errorf("error executing git reset for dir: %s, sha: %s, err: %v, output: %s", dir, sha, err, string(res))- }-- if isRoot {- err = updateSubmodules(dir)- if err != nil {- return err- }- }-- return nil-}--// git mutex will already be locked when this function is called-func updateSubmodules(dir string) error {- res, err := exec.Command("git", "-C", dir, "submodule", "update", "--recursive", "--init").CombinedOutput()- if err != nil {- return fmt.Errorf("error updating git submodules for dir: %s, err: %v, output: %s", dir, err, string(res))- }-- return nil-}--// processGitHistoryOutput processes the raw output from the git log command and returns a formatted string.-func processGitHistoryOutput(raw string) [][2]string {- var history [][2]string- entries := strings.Split(raw, "@>>>@") // Split entries using the custom separator.-- for _, entry := range entries {- // First clean up any leading/trailing whitespace or newlines from each entry.- entry = strings.TrimSpace(entry)-- // Now split the cleaned entry into its parts.- parts := strings.Split(entry, "@@|@@")- if len(parts) == 3 {- sha := parts[0]- timestampStr := parts[1]- message := strings.TrimSpace(parts[2]) // Trim whitespace from message as well.-- // Extract and format timestamp.- timestamp, err := strconv.ParseInt(timestampStr, 10, 64)- if err != nil {- continue // Skip entries with invalid timestamps.- }-- dt := time.Unix(timestamp, 0).Local()- formattedTs := dt.Format("Mon Jan 2, 2006 | 3:04:05pm MST")- if dt.Day() == time.Now().Day() {- formattedTs = dt.Format("Today | 3:04:05pm MST")- } else if dt.Day() == time.Now().AddDate(0, 0, -1).Day() {- formattedTs = dt.Format("Yesterday | 3:04:05pm MST")- }-- // Prepare the header with colors.- headerColor := color.New(color.FgCyan, color.Bold)- dateColor := color.New(color.FgCyan)-- // Combine SHA, formatted timestamp, and message header into one string.- header := fmt.Sprintf("%s | %s", headerColor.Sprintf("π Update %s", sha), dateColor.Sprintf("%s", formattedTs))-- // Combine header and message with a newline only if the message is not empty.- fullEntry := header- if message != "" {- fullEntry += "\n" + message- }-- history = append(history, [2]string{sha, fullEntry})- }- }-- return history-}diff --git a/cli/lib/plans.go b/cli/lib/plans.goindex 938e10d4..0ea2eed7 100644--- a/cli/lib/plans.go+++ b/cli/lib/plans.go@@ -1,250 +1,40 @@package libimport (- "encoding/json""fmt""os"- "path/filepath"- "plandex/types"- "regexp"- "sort"-- "github.com/plandex/plandex/shared")-func SetCurrentPlan(name string) error {- if PlandexDir == "" {- return fmt.Errorf("PlandexDir not set")- }-- currentPlanFilePath := filepath.Join(PlandexDir, "current_plan.json")- err := os.WriteFile(currentPlanFilePath, []byte(fmt.Sprintf(`{"name": "%s"}`, name)), 0644)- if err != nil {- return err- }-- return nil-}--func ClearCurrentPlan() error {- if PlandexDir == "" {- return fmt.Errorf("PlandexDir not set")- }-- currentPlanFilePath := filepath.Join(PlandexDir, "current_plan.json")- err := os.Remove(currentPlanFilePath)- if err != nil {- return err+func SetCurrentPlan(id string) error {+ if HomePlandexDir == "" {+ return fmt.Errorf("HomePlandexDir not set")}- return nil-}--var draftRegex = regexp.MustCompile("^draft(\\.[0-9]+)?$")--func ClearDraftPlans() error {- if PlandexDir == "" {- return fmt.Errorf("PlandexDir not set")+ if CurrentProjectId == "" || HomeCurrentPlanPath == "" {+ return fmt.Errorf("No current project")}- files, err := os.ReadDir(PlandexDir)+ err := os.WriteFile(HomeCurrentPlanPath, []byte(fmt.Sprintf(`{"id": "%s"}`, id)), 0644)if err != nil {return err}- for _, f := range files {- if draftRegex.MatchString(f.Name()) {- err = os.RemoveAll(filepath.Join(PlandexDir, f.Name()))- if err != nil {- return err- }- }- }-- return nil-}--func CurrentPlanIsDraft() bool {- if PlandexDir == "" {- return false- }- if CurrentPlanName == "" {- return false- }-- return draftRegex.MatchString(CurrentPlanName)-}--func RenameCurrentDraftPlan(planState *types.PlanState, name string) error {- if PlandexDir == "" {- return fmt.Errorf("PlandexDir not set")- }- if CurrentPlanName == "" {- return fmt.Errorf("CurrentPlanName not set")- }-- if CurrentPlanIsDraft() {- name, err := DedupPlanName(name)- if err != nil {- return fmt.Errorf("failed to deduplicate plan name: %w", err)- }-- oldPath := filepath.Join(PlandexDir, CurrentPlanName)- newPath := filepath.Join(PlandexDir, name)-- err = os.Rename(oldPath, newPath)- if err != nil {- return fmt.Errorf("failed to rename plan directory: %w", err)- }-- err = SetCurrentPlan(name)- if err != nil {- return fmt.Errorf("failed to set current plan: %w", err)- }-- // Fixes current plan paths (CurrentPlanName, CurrentPlanRootDir, etc.)- err = LoadCurrentPlan()- if err != nil {- return fmt.Errorf("failed to load current plan: %w", err)- }-- planState.Name = name- err = SetPlanState(planState, shared.StringTs())-- if err != nil {- return fmt.Errorf("failed to set plan state: %w", err)- }- }-return nil}-func DedupPlanName(name string) (string, error) {- if PlandexDir == "" {- return "", fmt.Errorf("PlandexDir not set")- }-- // If 'name' directory already exists, tack on an integer to differentiate- planDir := filepath.Join(PlandexDir, name)- _, err := os.Stat(planDir)- if err != nil {- if !os.IsNotExist(err) {- return "", err- }- }- exists := !os.IsNotExist(err)-- postfix := 1- var nameWithPostfix string- for exists {- postfix += 1- nameWithPostfix = fmt.Sprintf("%s.%d", name, postfix)- planDir = filepath.Join(PlandexDir, nameWithPostfix)- _, err = os.Stat(planDir)- if err != nil {- if !os.IsNotExist(err) {- return "", err- }- }- exists = !os.IsNotExist(err)- }- if nameWithPostfix != "" {- name = nameWithPostfix- }-- return name, nil-}--func GetPlanState() (*types.PlanState, error) {- return GetPlanStateForPlan(CurrentPlanName)-}--func GetPlanStateForPlan(name string) (*types.PlanState, error) {- if PlandexDir == "" {- return nil, fmt.Errorf("PlandexDir not set")- }-- var state types.PlanState- planStatePath := filepath.Join(PlandexDir, name, "plan.json")- fileBytes, err := os.ReadFile(planStatePath)- if err != nil {- return nil, fmt.Errorf("failed to open plan state file: %s", err)- }- err = json.Unmarshal(fileBytes, &state)- if err != nil {- return nil, fmt.Errorf("failed to parse plan state json: %s", err)+func ClearCurrentPlan() error {+ if HomePlandexDir == "" {+ return fmt.Errorf("HomePlandexDir not set")}- return &state, nil-}--func SetPlanState(state *types.PlanState, updatedAt string) error {- if updatedAt != "" {- state.UpdatedAt = updatedAt+ if CurrentProjectId == "" || HomeCurrentPlanPath == "" {+ return fmt.Errorf("No current project")}- planStatePath := filepath.Join(CurrentPlanDir, "plan.json")- planStateBytes, err := json.MarshalIndent(state, "", " ")+ err := os.Remove(HomeCurrentPlanPath)if err != nil {- return fmt.Errorf("failed to marshal plan state: %s", err)- }- err = os.WriteFile(planStatePath, planStateBytes, 0644)- if err != nil {- return fmt.Errorf("failed to write plan state: %s", err)+ return err}return nil}--func GetPlans() ([]*types.PlanState, error) {- if PlandexDir == "" {- return nil, fmt.Errorf("PlandexDir not set")- }-- var plans []*types.PlanState-- planContents, err := os.ReadDir(PlandexDir)-- if err != nil {- return nil, fmt.Errorf("failed to read plandex directory: %s", err)- }-- planCh := make(chan *types.PlanState, len(planContents))- errCh := make(chan error, 1)-- for _, planFileOrDir := range planContents {- if planFileOrDir.IsDir() {- go func(planDir os.DirEntry) {- planName := planDir.Name()-- planState, err := GetPlanStateForPlan(planName)- if err != nil {- errCh <- fmt.Errorf("failed to get plan state for plan %s: %s", planName, err)- return- }-- planCh <- planState- }(planFileOrDir)- } else {- planCh <- nil- }- }-- for range planContents {- select {- case plan := <-planCh:- if plan != nil {- plans = append(plans, plan)- }- case err := <-errCh:- return nil, err- }- }-- // sort plans by UpdatedAt descending- sort.Slice(plans, func(i, j int) bool {- return plans[i].UpdatedAt > plans[j].UpdatedAt- })-- return plans, nil-}diff --git a/cli/lib/proposal.go b/cli/lib/proposal.goindex 14632909..d63ac0a7 100644--- a/cli/lib/proposal.go+++ b/cli/lib/proposal.go@@ -539,7 +539,7 @@ func checkOutdatedContext(s *spinner.Spinner) (bool, error) {stopSpinner()- if len(outdatedRes.UpdatedParts) == 0 {+ if len(outdatedRes.UpdatedContexts) == 0 {fmt.Println("β Context is up to date")return true, nil}@@ -583,7 +583,7 @@ func checkOutdatedContext(s *spinner.Spinner) (bool, error) {}phrase := "have been"- if len(outdatedRes.UpdatedParts) == 1 {+ if len(outdatedRes.UpdatedContexts) == 1 {phrase = "has been"}color.New(color.FgHiCyan, color.Bold).Printf("%s in context %s modified π\n\n", msg, phrase)diff --git a/cli/lib/stream.go b/cli/lib/stream.goindex 5c28a284..4b20cfdf 100644--- a/cli/lib/stream.go+++ b/cli/lib/stream.go@@ -1,12 +1,8 @@package libimport (- "bufio"- "bytes""encoding/json""fmt"- "os"- "path/filepath""github.com/plandex/plandex/shared")@@ -25,40 +21,3 @@ func updateTokenCounts(content string, numStreamedTokensByPath map[string]int, freturn nil}--func writePlanRes(content string) error {- var planRes shared.PlanResult- err := json.Unmarshal([]byte(content), &planRes)- if err != nil {- return fmt.Errorf("failed to unmarshal plan file: %s", err)- }-- path := filepath.Join(ResultsSubdir, planRes.Path, planRes.Ts+".json")- err = os.MkdirAll(filepath.Dir(path), os.ModePerm)- if err != nil {- return fmt.Errorf("failed to create directory: %s", err)- }-- err = os.WriteFile(path, []byte(content), 0644)- if err != nil {- // fmt.Printf("failed to write plan file '%s': %v", path, err)- return fmt.Errorf("failed to write plan result '%s': %v", path, err)- }-- return nil-}--func readUntilSeparator(reader *bufio.Reader, separator string) (string, error) {- var result []byte- sepBytes := []byte(separator)- for {- b, err := reader.ReadByte()- if err != nil {- return string(result), err- }- result = append(result, b)- if len(result) >= len(sepBytes) && bytes.HasSuffix(result, sepBytes) {- return string(result[:len(result)-len(separator)]), nil- }- }-}diff --git a/cli/types/current_plan.go b/cli/types/current_plan.godeleted file mode 100644index bd4bc731..00000000--- a/cli/types/current_plan.go+++ /dev/null@@ -1,21 +0,0 @@-package types--import "github.com/plandex/plandex/shared"--type CurrentPlanState struct {- PlanResultsInfo- CurrentPlanFiles *shared.CurrentPlanFiles- ModelContext shared.ModelContext- ContextByPath map[string]*shared.ModelContextPart-}--func (p CurrentPlanState) NumPendingForPath(path string) int {- res := 0- results := p.PlanResByPath[path]- for _, result := range results {- if result.IsPending() {- res += result.NumPendingReplacements()- }- }- return res-}diff --git a/cli/types/types.go b/cli/types/types.goindex 3ea2d6e3..8c645876 100644--- a/cli/types/types.go+++ b/cli/types/types.go@@ -1,64 +1,6 @@package types-import (- "github.com/looplab/fsm"- "github.com/plandex/plandex/shared"-)--type OnStreamPlanParams struct {- Content string- State *fsm.FSM- Err error-}--type OnStreamPlan func(params OnStreamPlanParams)--// APIHandler is an interface that represents the public API functions-type APIHandler interface {- Propose(prompt, parentProposalId, rootId string, onStream OnStreamPlan) (*shared.PromptRequest, error)- ShortSummary(text string) (*shared.ShortSummaryResponse, error)- Abort(proposalId string) error- FileName(text string) (*shared.FileNameResponse, error)- ConvoSummary(rootId, latestTimestamp string) (*shared.ConversationSummary, error)-}--type AppendConversationParams struct {- Timestamp string- PlanState *PlanState- PromptParams *AppendConversationPromptParams- ReplyParams *AppendConversationReplyParams-}--type AppendConversationPromptParams struct {- Prompt string- PromptTokens int-}--type AppendConversationReplyParams struct {- ResponseTimestamp string- Reply string- ReplyTokens int-}--type PlanState struct {- Name string `json:"name"`- ProposalId string `json:"proposalId"`- RootId string `json:"rootId"`- CreatedAt string `json:"createdAt"`- UpdatedAt string `json:"updatedAt"`- ContextTokens int `json:"contextTokens"`- ContextUpdatableTokens int `json:"contextUpdatableTokens"`- ConvoTokens int `json:"convoTokens"`- ConvoSummarizedTokens int `json:"convoSummarizedTokens"`- NumMessages int `json:"numMessages"`- AppliedAt string `json:"appliedAt"`- BaseBranch string `json:"baseBranch"`- PreviewBranch string `json:"previewBranch"`-}--type PlanSettings struct {- Name string-}+import "github.com/plandex/plandex/shared"type LoadContextParams struct {Note string@@ -66,6 +8,14 @@ type LoadContextParams struct {NamesOnly bool}+type ContextOutdatedResult struct {+ Msg string+ UpdatedContexts []*shared.Context+ NumFiles int+ NumUrls int+ NumTrees int+}+const (PlanOutdatedStrategyOverwrite string = "Clear the modifications and then apply"PlanOutdatedStrategyApplyUnmodified string = "Apply only new and unmodified files"@@ -74,8 +24,11 @@ const (PlanOutdatedStrategyCancel string = "Cancel")-type PlanResultsInfo struct {- SortedPaths []string- PlanResByPath shared.PlanResultsByPath- ReplacementsByPath map[string][]*shared.Replacement+type PlanSettings struct {+ Id string `json:"id"`+}++type ProjectSettings struct {+ Id string `json:"id"`+ OrgId string `json:"orgId"`}diff --git a/server/db.go b/server/db.godeleted file mode 100644index 0a4cbee1..00000000--- a/server/db.go+++ /dev/null@@ -1,17 +0,0 @@-package main--import (- "github.com/jmoiron/sqlx"- _ "github.com/lib/pq" // postgres driver-)--var db *sqlx.DB--func InitDB(dataSourceName string) error {- var err error- db, err = sqlx.Connect("postgres", dataSourceName)- if err != nil {- return err- }- return nil-}diff --git a/server/db/build_helpers.go b/server/db/build_helpers.gonew file mode 100644index 00000000..06ef3a0d--- /dev/null+++ b/server/db/build_helpers.go@@ -0,0 +1,42 @@+package db++import (+ "fmt"+ "time"+)++func StorePlanBuild(build *PlanBuild) error {+ query := `INSERT INTO plan_builds (org_id, plan_id, convo_message_id) VALUES (:org_id, :plan_id, :convo_message_id) RETURNING id, created_at, updated_at`++ row, err := Conn.NamedQuery(query, build)++ if err != nil {+ return fmt.Errorf("error storing plan build: %v", err)+ }++ defer row.Close()++ 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 plan build: %v", err)+ }++ build.Id = id+ build.CreatedAt = createdAt+ build.UpdatedAt = updatedAt+ }++ return nil+}++func SetBuildError(build *PlanBuild) error {+ _, err := Conn.Exec("UPDATE plan_builds SET error = $1, error_path = $2 WHERE id = $3", build.Error, build.ErrorPath, build.Id)++ if err != nil {+ return fmt.Errorf("error setting build error: %v", err)+ }++ return nil+}diff --git a/server/db/context_helpers.go b/server/db/context_helpers.gonew file mode 100644index 00000000..629daf2c--- /dev/null+++ b/server/db/context_helpers.go@@ -0,0 +1,152 @@+package db++import (+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "sort"+ "strings"+ "time"++ "github.com/google/uuid"+)++func GetPlanContexts(orgId, planId string, includeBody bool) ([]*Context, error) {+ var contexts []*Context+ contextDir := getPlanContextDir(orgId, planId)++ // get all context files+ files, err := os.ReadDir(contextDir)+ if err != nil {+ return nil, fmt.Errorf("error reading context dir: %v", err)+ }++ errCh := make(chan error, len(files)/2)+ contextCh := make(chan *Context, len(files)/2)++ // read each context file+ for _, file := range files {+ if strings.HasSuffix(file.Name(), ".meta") {+ go func(file os.DirEntry) {+ context, err := GetContext(orgId, planId, strings.TrimSuffix(file.Name(), ".meta"), includeBody)++ if err != nil {+ errCh <- fmt.Errorf("error reading context file: %v", err)+ return+ }++ contextCh <- context+ }(file)+ }+ }++ for i := 0; i < len(files)/2; i++ {+ select {+ case err := <-errCh:+ return nil, fmt.Errorf("error reading context files: %v", err)+ case context := <-contextCh:+ contexts = append(contexts, context)+ }+ }++ // sort contexts by CreatedAt+ sort.Slice(contexts, func(i, j int) bool {+ return contexts[i].CreatedAt.Before(contexts[j].CreatedAt)+ })++ return contexts, nil+}++func GetContext(orgId, planId, contextId string, includeBody bool) (*Context, error) {+ contextDir := getPlanContextDir(orgId, planId)++ // read the meta file+ metaPath := filepath.Join(contextDir, contextId+".meta")++ metaBytes, err := os.ReadFile(metaPath)+ if err != nil {+ return nil, fmt.Errorf("error reading context meta file: %v", err)+ }++ var context Context+ err = json.Unmarshal(metaBytes, &context)+ if err != nil {+ return nil, fmt.Errorf("error unmarshalling context meta file: %v", err)+ }++ if includeBody {+ // read the body file+ bodyPath := filepath.Join(contextDir, strings.TrimSuffix(contextId, ".meta")+".body")+ bodyBytes, err := os.ReadFile(bodyPath)++ if err != nil {+ return nil, fmt.Errorf("error reading context body file: %v", err)+ }++ context.Body = string(bodyBytes)+ }++ return &context, nil+}++func ContextRemove(contexts []*Context) error {+ // remove files+ numFiles := len(contexts) * 2++ errCh := make(chan error, numFiles)+ for _, context := range contexts {+ contextDir := getPlanContextDir(context.OrgId, context.PlanId)+ for _, ext := range []string{".meta", ".body"} {+ go func(dircontext *Context, dir, ext string) {+ errCh <- os.Remove(filepath.Join(dir, context.Id+ext))+ }(context, contextDir, ext)+ }+ }++ for i := 0; i < numFiles; i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error removing context file: %v", err)+ }+ }++ return nil+}++func StoreContext(context *Context) error {+ contextDir := getPlanContextDir(context.OrgId, context.PlanId)++ ts := time.Now().UTC()+ if context.Id == "" {+ context.Id = uuid.New().String()+ context.CreatedAt = ts+ }+ context.UpdatedAt = ts++ metaFilename := context.Id + ".meta"+ metaPath := filepath.Join(contextDir, metaFilename)++ bodyFilename := context.Id + ".body"+ bodyPath := filepath.Join(contextDir, bodyFilename)+ body := []byte(context.Body)+ context.Body = ""++ // Convert the ModelContextPart to JSON+ data, err := json.MarshalIndent(context, "", " ")+ if err != nil {+ return fmt.Errorf("failed to marshal context context: %v", err)+ }++ // Write the body to the file+ if err = os.WriteFile(bodyPath, body, 0644); err != nil {+ return fmt.Errorf("failed to write context body to file %s: %v", bodyPath, err)+ }++ // Write the meta data to the file+ if err = os.WriteFile(metaPath, data, 0644); err != nil {+ return fmt.Errorf("failed to write context meta to file %s: %v", metaPath, err)+ }++ return nil+}diff --git a/server/db/convo_helpers.go b/server/db/convo_helpers.gonew file mode 100644index 00000000..e71b368b--- /dev/null+++ b/server/db/convo_helpers.go@@ -0,0 +1,147 @@+package db++import (+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "sort"+ "time"++ "github.com/google/uuid"+ "github.com/sashabaranov/go-openai"+)++func GetPlanConvo(orgId, planId string) ([]*ConvoMessage, error) {+ var convo []*ConvoMessage+ convoDir := getPlanConversationDir(orgId, planId)++ files, err := os.ReadDir(convoDir)+ if err != nil {+ return nil, fmt.Errorf("error reading convo dir: %v", err)+ }++ errCh := make(chan error, len(files))+ convoCh := make(chan *ConvoMessage, len(files))++ for _, file := range files {+ go func(file os.DirEntry) {+ bytes, err := os.ReadFile(filepath.Join(convoDir, file.Name()))++ if err != nil {+ errCh <- fmt.Errorf("error reading convo file: %v", err)+ return+ }++ var convoMessage ConvoMessage+ err = json.Unmarshal(bytes, &convoMessage)++ if err != nil {+ errCh <- fmt.Errorf("error unmarshalling convo file: %v", err)+ return+ }++ convoCh <- &convoMessage++ }(file)+ }++ for i := 0; i < len(files); i++ {+ select {+ case err := <-errCh:+ return nil, fmt.Errorf("error reading convo files: %v", err)+ case convoMessage := <-convoCh:+ convo = append(convo, convoMessage)+ }+ }++ sort.Slice(convo, func(i, j int) bool {+ return convo[i].CreatedAt.Before(convo[j].CreatedAt)+ })++ return convo, nil+}++func StoreConvoMessage(message *ConvoMessage) error {+ convoDir := getPlanConversationDir(message.OrgId, message.PlanId)++ id := uuid.New().String()+ ts := time.Now().UTC()++ message.Id = id+ message.CreatedAt = ts++ bytes, err := json.Marshal(message)++ if err != nil {+ 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)+ }++ err = AddPlanConvoTokens(message.PlanId, message.Tokens)++ if err != nil {+ return fmt.Errorf("error adding convo tokens: %v", err)+ }++ var desc string+ if message.Role == openai.ChatMessageRoleUser {+ desc = "π¬ User prompt"+ // TODO: add user name+ } else {+ desc = "π€ Plandex reply"+ if message.Stopped {+ desc += " | π stopped early"+ }+ }++ 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)+ }++ return nil+}++func GetPlanSummaries(planId string) ([]*ConvoSummary, error) {+ var summaries []*ConvoSummary++ err := Conn.Select(&summaries, "SELECT * FROM convo_summaries WHERE plan_id = $1 ORDER BY created_at", planId)++ if err != nil {+ return nil, fmt.Errorf("error getting plan summaries: %v", err)+ }+ return summaries, nil+}++func StoreSummary(summary *ConvoSummary) error {+ query := "INSERT INTO convo_summaries (org_id, plan_id, latest_convo_message_id, latest_convo_message_created_at, summary, tokens, num_messages, created_at) VALUES (:org_id, :plan_id, :latest_convo_message_id, :latest_convo_message_created_at, :summary, :tokens, :num_messages, :created_at) RETURNING id, created_at"++ row, err := Conn.NamedQuery(query, summary)++ if err != nil {+ return fmt.Errorf("error storing summary: %v", err)+ }++ defer row.Close()++ 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 summary: %v", err)+ }++ summary.Id = id+ summary.CreatedAt = createdAt+ }++ return nil+}diff --git a/server/db/db.go b/server/db/db.gonew file mode 100644index 00000000..9be70281--- /dev/null+++ b/server/db/db.go@@ -0,0 +1,57 @@+package db++import (+ "errors"+ "fmt"+ "os"++ "github.com/golang-migrate/migrate/v4"+ "github.com/golang-migrate/migrate/v4/database/postgres"+ _ "github.com/golang-migrate/migrate/v4/source/file"+ "github.com/jmoiron/sqlx"+ _ "github.com/lib/pq"+)++var Conn *sqlx.DB++func Connect() error {+ var err error++ if os.Getenv("DATABASE_URL") == "" {+ return errors.New("DATABASE_URL not set")+ }++ Conn, err = sqlx.Connect("postgres", os.Getenv("DATABASE_URL"))+ if err != nil {+ return err+ }+ return nil+}++func MigrationsUp() error {+ if Conn == nil {+ return errors.New("db not initialized")+ }++ driver, err := postgres.WithInstance(Conn.DB, &postgres.Config{})++ if err != nil {+ return fmt.Errorf("error creating postgres driver: %v", err)+ }++ m, err := migrate.NewWithDatabaseInstance(+ "file:///migrations",+ "postgres", driver)++ if err != nil {+ return fmt.Errorf("error creating migration instance: %v", err)+ }++ err = m.Up()++ if err != nil && err != migrate.ErrNoChange {+ return fmt.Errorf("error running migrations: %v", err)+ }++ return nil+}diff --git a/server/db/fs.go b/server/db/fs.gonew file mode 100644index 00000000..5117b862--- /dev/null+++ b/server/db/fs.go@@ -0,0 +1,73 @@+package db++import (+ "fmt"+ "os"+ "path/filepath"+)++var BaseDir string++func init() {+ home, err := os.UserHomeDir()+ if err != nil {+ panic(fmt.Errorf("Error getting user home dir: %v", err))+ }++ BaseDir = filepath.Join(home, "plandex-server")+}++func InitPlan(orgId, planId string) error {+ dir := getPlanDir(orgId, planId)+ err := os.MkdirAll(dir, os.ModePerm)++ if err != nil {+ return fmt.Errorf("error creating plan dir: %v", err)+ }++ for _, subdirFn := range [](func(orgId, planId string) string){+ getPlanContextDir,+ getPlanConversationDir,+ getPlanResultsDir} {+ err = os.MkdirAll(subdirFn(orgId, planId), os.ModePerm)++ if err != nil {+ return fmt.Errorf("error creating plan subdir: %v", err)+ }+ }++ err = InitGitRepo(orgId, planId)++ if err != nil {+ return fmt.Errorf("error initializing git repo: %v", err)+ }++ return nil+}++func DeletePlanDir(orgId, planId string) error {+ dir := getPlanDir(orgId, planId)+ err := os.RemoveAll(dir)++ if err != nil {+ return fmt.Errorf("error deleting plan dir: %v", err)+ }++ return nil+}++func getPlanDir(orgId, planId string) string {+ return filepath.Join(BaseDir, "orgs", orgId, "plans", planId)+}++func getPlanContextDir(orgId, planId string) string {+ return filepath.Join(getPlanDir(orgId, planId), "context")+}++func getPlanConversationDir(orgId, planId string) string {+ return filepath.Join(getPlanDir(orgId, planId), "conversation")+}++func getPlanResultsDir(orgId, planId string) string {+ return filepath.Join(getPlanDir(orgId, planId), "results")+}diff --git a/server/db/git.go b/server/db/git.gonew file mode 100644index 00000000..00f6ae2a--- /dev/null+++ b/server/db/git.go@@ -0,0 +1,168 @@+package db++import (+ "bytes"+ "fmt"+ "os/exec"+ "strconv"+ "strings"+ "time"++ "github.com/fatih/color"+)++func init() {+ // ensure git is available+ cmd := exec.Command("git", "--version")+ if err := cmd.Run(); err != nil {+ panic(fmt.Errorf("Error running git --version: %v", err))+ }+}++func InitGitRepo(orgId, planId string) error {+ dir := getPlanDir(orgId, planId)++ res, err := exec.Command("git", "init", dir).CombinedOutput()+ if err != nil {+ return fmt.Errorf("error initializing git repository for dir: %s, err: %v, output: %s", dir, err, string(res))+ }++ return nil+}++func GitAddAndCommit(orgId, planId, message string) error {+ dir := getPlanDir(orgId, planId)++ err := gitAdd(dir, ".")+ if err != nil {+ return fmt.Errorf("error adding files to git repository for dir: %s, err: %v", dir, err)+ }++ err = gitCommit(dir, message)+ if err != nil {+ return fmt.Errorf("error committing files to git repository for dir: %s, err: %v", dir, err)+ }++ return nil+}++func gitAdd(repoDir, path string) error {+ res, err := exec.Command("git", "-C", repoDir, "add", path).CombinedOutput()+ if err != nil {+ return fmt.Errorf("error adding files to git repository for dir: %s, err: %v, output: %s", repoDir, err, string(res))+ }++ return nil+}++func gitCommit(repoDir, commitMsg string) error {+ res, err := exec.Command("git", "-C", repoDir, "commit", "-m", commitMsg).CombinedOutput()+ if err != nil {+ return fmt.Errorf("error committing files to git repository for dir: %s, err: %v, output: %s", repoDir, err, string(res))+ }++ return nil+}++func GitRewindToSHA(repoDir, sha string) error {+ res, err := exec.Command("git", "-C", repoDir, "reset", "--hard",+ sha).CombinedOutput()++ if err != nil {+ return fmt.Errorf("error executing git reset for dir: %s, sha: %s, err: %v, output: %s", repoDir, sha, err, string(res))+ }++ return nil+}++func GetLatestCommit(dir string) (string, string, error) {+ var out bytes.Buffer+ cmd := exec.Command("git", "log", "--pretty=%h@@|@@%at@@|@@%B@>>>@")+ cmd.Dir = dir+ cmd.Stdout = &out+ err := cmd.Run()+ if err != nil {+ return "", "", fmt.Errorf("error getting git history for dir: %s, err: %v",+ dir, err)+ }++ // Process the log output to get it in the desired format.+ history := processGitHistoryOutput(strings.TrimSpace(out.String()))++ first := history[0]++ return first[0], first[1], nil+}++func GetGitCommitHistory(dir string) (string, error) {++ var out bytes.Buffer+ cmd := exec.Command("git", "log", "--pretty=%h@@|@@%at@@|@@%B@>>>@")+ cmd.Dir = dir+ cmd.Stdout = &out+ err := cmd.Run()+ if err != nil {+ return "", fmt.Errorf("error getting git history for dir: %s, err: %v",+ dir, err)+ }++ // Process the log output to get it in the desired format.+ history := processGitHistoryOutput(strings.TrimSpace(out.String()))++ var output []string+ for _, el := range history {+ output = append(output, el[1])+ }++ return strings.Join(output, "\n\n"), nil+}++// processGitHistoryOutput processes the raw output from the git log command and returns a formatted string.+func processGitHistoryOutput(raw string) [][2]string {+ var history [][2]string+ entries := strings.Split(raw, "@>>>@") // Split entries using the custom separator.++ for _, entry := range entries {+ // First clean up any leading/trailing whitespace or newlines from each entry.+ entry = strings.TrimSpace(entry)++ // Now split the cleaned entry into its parts.+ parts := strings.Split(entry, "@@|@@")+ if len(parts) == 3 {+ sha := parts[0]+ timestampStr := parts[1]+ message := strings.TrimSpace(parts[2]) // Trim whitespace from message as well.++ // Extract and format timestamp.+ timestamp, err := strconv.ParseInt(timestampStr, 10, 64)+ if err != nil {+ continue // Skip entries with invalid timestamps.+ }++ dt := time.Unix(timestamp, 0).Local()+ formattedTs := dt.Format("Mon Jan 2, 2006 | 3:04:05pm MST")+ if dt.Day() == time.Now().Day() {+ formattedTs = dt.Format("Today | 3:04:05pm MST")+ } else if dt.Day() == time.Now().AddDate(0, 0, -1).Day() {+ formattedTs = dt.Format("Yesterday | 3:04:05pm MST")+ }++ // Prepare the header with colors.+ headerColor := color.New(color.FgCyan, color.Bold)+ dateColor := color.New(color.FgCyan)++ // Combine SHA, formatted timestamp, and message header into one string.+ header := fmt.Sprintf("%s | %s", headerColor.Sprintf("π Update %s", sha), dateColor.Sprintf("%s", formattedTs))++ // Combine header and message with a newline only if the message is not empty.+ fullEntry := header+ if message != "" {+ fullEntry += "\n" + message+ }++ history = append(history, [2]string{sha, fullEntry})+ }+ }++ return history+}diff --git a/server/db/models.go b/server/db/models.gonew file mode 100644index 00000000..254e3ae4--- /dev/null+++ b/server/db/models.go@@ -0,0 +1,279 @@+package db++import (+ "time"++ "github.com/plandex/plandex/shared"+)++// The models below should only be used server-side.+// Many of them have corresponding models in shared/api for client-side use.+// This adds some duplication, but helps ensure that server-only data doesn't leak to the client.+// Models used client-side have a ToApi() method to convert it to the corresponding client-side model.++type Org struct {+ Id string `db:"id"`+ Name string `db:"name"`+ Domain string `db:"domain"`+ AutoAddDomainUsers bool `db:"auto_add_domain_users"`+ CreatorId string `db:"creator_id"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++func (org *Org) ToApi() *shared.Org {+ return &shared.Org{+ Id: org.Id,+ Name: org.Name,+ }+}++type User struct {+ Id string `db:"id"`+ Name string `db:"name"`+ Email string `db:"email"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++func (user *User) ToApi() *shared.User {+ return &shared.User{+ Id: user.Id,+ Name: user.Name,+ Email: user.Email,+ }+}++type OrgUser struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ UserId string `db:"user_id"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++type Project struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ Name string `db:"name"`+ LastActivePlanId string `db:"last_active_plan_id"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++func (project *Project) ToApi() *shared.Project {+ return &shared.Project{+ Id: project.Id,+ Name: project.Name,+ LastActivePlanId: project.LastActivePlanId,+ }+}++type UserProject struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ UserId string `db:"user_id"`+ ProjectId string `db:"project_id"`+ LastActivePlanId string `db:"last_active_plan_id"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++type Plan struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ CreatorId string `db:"creator_id"`+ ProjectId string `db:"project_id"`+ Name string `db:"name"`+ Status shared.PlanStatus `db:"status"`+ 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"`+}++func (plan *Plan) ToApi() *shared.Plan {+ return &shared.Plan{+ Id: plan.Id,+ CreatorId: plan.CreatorId,+ Name: plan.Name,+ Status: plan.Status,+ ContextTokens: plan.ContextTokens,+ ConvoTokens: plan.ConvoTokens,+ AppliedAt: plan.AppliedAt,+ ArchivedAt: plan.ArchivedAt,+ CreatedAt: plan.CreatedAt,+ UpdatedAt: plan.UpdatedAt,+ }+}++type ConvoSummary struct {+ Id string `db:"id"`+ OrgId string `db:"org_id"`+ PlanId string `db:"plan_id"`+ LatestConvoMessageId string `db:"latest_convo_message_id"`+ LatestConvoMessageCreatedAt time.Time `db:"latest_convo_message_created_at"`+ Summary string `db:"summary"`+ Tokens int `db:"tokens"`+ NumMessages int `db:"num_messages"`+ CreatedAt time.Time `db:"created_at"`+}++func (summary *ConvoSummary) ToApi() *shared.ConvoSummary {+ return &shared.ConvoSummary{+ Id: summary.Id,+ LatestConvoMessageId: summary.LatestConvoMessageId,+ LatestConvoMessageCreatedAt: summary.LatestConvoMessageCreatedAt,+ Summary: summary.Summary,+ Tokens: summary.Tokens,+ NumMessages: summary.NumMessages,+ CreatedAt: summary.CreatedAt,+ }+}++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"`+ PlanId string `db:"plan_id"`+ ConvoMessageId string `db:"convo_message_id"`+ Error string `db:"error"`+ ErrorPath string `db:"error_path"`+ CreatedAt time.Time `db:"created_at"`+ UpdatedAt time.Time `db:"updated_at"`+}++func (build *PlanBuild) ToApi() *shared.PlanBuild {+ return &shared.PlanBuild{+ Id: build.Id,+ ConvoMessageId: build.ConvoMessageId,+ Error: build.Error,+ ErrorPath: build.ErrorPath,+ CreatedAt: build.CreatedAt,+ UpdatedAt: build.UpdatedAt,+ }+}++// Models below are stored in files, not in the database.+// This allows us to store them in a git repo and use git to manage history.++type Context struct {+ Id string `json:"id"`+ OrgId string `json:"orgId"`+ CreatorId string `json:"creatorId"`+ PlanId string `json:"planId"`+ ContextType shared.ContextType `json:"contextType"`+ Name string `json:"name"`+ Url string `json:"url"`+ FilePath string `json:"filePath"`+ Sha string `json:"sha"`+ NumTokens int `json:"numTokens"`+ Body string `json:"body,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`+}++func (context *Context) ToApi() *shared.Context {+ return &shared.Context{+ Id: context.Id,+ CreatorId: context.CreatorId,+ ContextType: context.ContextType,+ Name: context.Name,+ Url: context.Url,+ FilePath: context.FilePath,+ Sha: context.Sha,+ NumTokens: context.NumTokens,+ Body: context.Body,+ CreatedAt: context.CreatedAt,+ UpdatedAt: context.UpdatedAt,+ }+}++type ConvoMessage struct {+ Id string `json:"id"`+ OrgId string `json:"orgId"`+ PlanId string `json:"planId"`+ UserId string `json:"userId"`+ Role string `json:"role"`+ Tokens int `json:"tokens"`+ Num int `json:"num"`+ Message string `json:"message"`+ Stopped bool `json:"stopped"`+ CreatedAt time.Time `json:"createdAt"`+}++func (msg *ConvoMessage) ToApi() *shared.ConvoMessage {+ return &shared.ConvoMessage{+ Id: msg.Id,+ UserId: msg.UserId,+ Role: msg.Role,+ Tokens: msg.Tokens,+ Num: msg.Num,+ Message: msg.Message,+ Stopped: msg.Stopped,+ CreatedAt: msg.CreatedAt,+ }+}++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"`+}++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,+ }+}diff --git a/server/db/plan_helpers.go b/server/db/plan_helpers.gonew file mode 100644index 00000000..88f29d3b--- /dev/null+++ b/server/db/plan_helpers.go@@ -0,0 +1,235 @@+package db++import (+ "fmt"+ "log"+ "time"++ "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"++ plan := &Plan{+ OrgId: orgId,+ CreatorId: userId,+ ProjectId: projectId,+ Name: name,+ }++ row, err := Conn.NamedQuery(query, plan)++ if err != nil {+ return nil, fmt.Errorf("error creating plan: %v", err)+ }++ defer row.Close()++ if row.Next() {+ var createdAt, updatedAt time.Time+ var id string+ if err := row.Scan(&id, &createdAt, &updatedAt); err != nil {+ return nil, fmt.Errorf("error creating plan: %v", err)+ }++ plan.Id = id+ plan.CreatedAt = createdAt+ plan.UpdatedAt = updatedAt+ }++ err = InitPlan(orgId, plan.Id)++ if err != nil {+ return nil, fmt.Errorf("error initializing plan dir: %v", err)+ }++ return plan, nil+}++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"+ qargs := []interface{}{projectId, userId}++ if archived {+ qs += " AND archived_at IS NOT NULL"+ } else {+ qs += " AND archived_at IS NULL"+ }++ if status != "" {+ qs += " AND status = $3"+ qargs = append(qargs, status)+ }++ qs += " ORDER BY updated_at DESC"++ res, err := Conn.Query(qs, qargs...)++ if err != nil {+ return nil, fmt.Errorf("error listing plans: %v", err)+ }++ 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)++ if err != nil {+ return nil, fmt.Errorf("error scanning plan: %v", err)+ }+ plans = append(plans, plan)+ }++ return plans, nil+}++func AddPlanContextTokens(planId string, addTokens int) error {+ _, err := Conn.Exec("UPDATE plans SET context_tokens = context_tokens + $1 WHERE id = $2", addTokens, planId)+ if err != nil {+ return fmt.Errorf("error updating plan tokens: %v", err)+ }+ return nil+}++func AddPlanConvoTokens(planId string, addTokens int) error {+ _, err := Conn.Exec("UPDATE plans SET convo_tokens = convo_tokens + $1 WHERE id = $2", addTokens, planId)+ if err != nil {+ return fmt.Errorf("error updating plan tokens: %v", err)+ }+ return nil+}++func GetPlan(planId string) (*shared.Plan, error) {+ var plan shared.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)++ if err != nil {+ return nil, fmt.Errorf("error getting plan: %v", err)+ }++ return &plan, nil+}++func SetPlanStatus(planId string, status shared.PlanStatus, errStr string) error {+ _, err := Conn.Exec("UPDATE plans SET status = $1, error = $2 WHERE id = $3", status, errStr, planId)++ if err != nil {+ return fmt.Errorf("error setting plan status: %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"++ row, err := Conn.NamedQuery(query, description)++ if err != nil {+ return fmt.Errorf("error storing convo message description: %v", err)+ }++ defer row.Close()++ 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)+ }++ description.Id = id+ description.CreatedAt = createdAt+ description.UpdatedAt = updatedAt+ }++ return nil+}++func DeleteDraftPlans(orgId, projectId, userId string) error {+ res, err := Conn.Query("DELETE FROM plans WHERE project_id = $1 AND creator_id = $2 AND name = 'draft' RETURNING id;", projectId, userId)+ if err != nil {+ return fmt.Errorf("error deleting draft plans: %v", err)+ }++ defer res.Close()++ // get ids+ var ids []string++ for res.Next() {+ var id string+ err := res.Scan(&id)+ if err != nil {+ return fmt.Errorf("error scanning deleted draft plan id: %v", err)+ }+ ids = append(ids, id)+ }++ errCh := make(chan error)+ for _, planId := range ids {+ go func(planId string) {+ errCh <- DeletePlanDir(orgId, planId)+ }(planId)+ }++ for i := 0; i < len(ids); i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error deleting draft plan dir: %v", err)+ }+ }++ if len(ids) > 0 {+ log.Println("Deleted", len(ids), "draft plans")+ }++ return nil+}++func DeletePlans(orgId, projectId, userId string) error {+ res, err := Conn.Query("DELETE FROM plans WHERE project_id = $1 AND creator_id = $2 RETURNING id;", projectId, userId)+ if err != nil {+ return fmt.Errorf("error deleting plans: %v", err)+ }++ defer res.Close()++ // get ids+ var ids []string++ for res.Next() {+ var id string+ err := res.Scan(&id)+ if err != nil {+ return fmt.Errorf("error scanning deleted draft plan id: %v", err)+ }+ ids = append(ids, id)+ }++ errCh := make(chan error)+ for _, planId := range ids {+ go func(planId string) {+ errCh <- DeletePlanDir(orgId, planId)+ }(planId)+ }++ for i := 0; i < len(ids); i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error deleting plan dir: %v", err)+ }+ }++ if len(ids) > 0 {+ log.Println("Deleted", len(ids), "plans")+ }++ return nil+}diff --git a/server/db/result_helpers.go b/server/db/result_helpers.gonew file mode 100644index 00000000..9bb10486--- /dev/null+++ b/server/db/result_helpers.go@@ -0,0 +1,403 @@+package db++import (+ "encoding/json"+ "fmt"+ "os"+ "path/filepath"+ "sort"+ "strings"+ "time"++ "github.com/google/uuid"+ "github.com/plandex/plandex/shared"+)++func StorePlanResult(result *PlanFileResult) error {+ now := time.Now()+ if result.Id == "" {+ result.Id = uuid.New().String()+ result.CreatedAt = now+ }+ result.UpdatedAt = now++ bytes, err := json.MarshalIndent(result, "", " ")++ if err != nil {+ return fmt.Errorf("error marshalling result: %v", err)+ }++ resultsDir := getPlanResultsDir(result.OrgId, result.PlanId)++ err = os.MkdirAll(resultsDir, 0755)++ if err != nil {+ return fmt.Errorf("error creating results dir: %v", err)+ }++ err = os.WriteFile(filepath.Join(resultsDir, result.Id+".json"), bytes, 0644)++ if err != nil {+ return fmt.Errorf("error writing result file: %v", err)+ }++ return nil++}++func GetCurrentPlanState(orgId, planId string, contexts []*Context) (*shared.CurrentPlanState, error) {++ var dbPlanFileResults []*PlanFileResult+ var latestBuildDesc *ConvoMessageDescription+ errCh := make(chan error, 2)++ go func() {+ res, err := GetPlanFileResults(orgId, planId)+ dbPlanFileResults = res++ if err != nil {+ errCh <- fmt.Errorf("error getting plan file results: %v", err)+ return+ }++ errCh <- nil+ }()++ go func() {+ res, err := GetLatestBuildDescription(planId)+ latestBuildDesc = res++ if err != nil {+ errCh <- fmt.Errorf("error getting latest plan build description: %v", err)+ return+ }++ errCh <- nil+ }()++ for i := 0; i < 2; i++ {+ err := <-errCh+ if err != nil {+ return nil, err+ }+ }++ var apiPlanFileResults []*shared.PlanFileResult++ for _, dbPlanFileResult := range dbPlanFileResults {+ apiPlanFileResults = append(apiPlanFileResults, dbPlanFileResult.ToApi())+ }+ planResult := GetPlanResult(apiPlanFileResults)++ var apiContexts []*shared.Context+ apiContextsByPath := make(map[string]*shared.Context)++ for _, context := range contexts {+ apiContexts = append(apiContexts, context.ToApi())++ if context.FilePath != "" {+ apiContextsByPath[context.FilePath] = context.ToApi()+ }+ }++ planState := &shared.CurrentPlanState{+ PlanResult: planResult,+ Contexts: apiContexts,+ ContextsByPath: apiContextsByPath,+ LatestBuildDescription: latestBuildDesc.ToApi(),+ }++ currentPlanFiles, err := planState.GetFiles()++ if err != nil {+ return nil, fmt.Errorf("error getting current plan files: %v", err)+ }++ planState.CurrentPlanFiles = currentPlanFiles++ return planState, nil+}++func GetLatestBuildDescription(planId string) (*ConvoMessageDescription, error) {+ var description ConvoMessageDescription++ 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)++ if err != nil {+ return nil, fmt.Errorf("error getting plan build description: %v", err)+ }++ return &description, nil+}++func GetPlanFileResults(orgId, planId string) ([]*PlanFileResult, error) {+ var results []*PlanFileResult++ resultsDir := getPlanResultsDir(orgId, planId)++ files, err := os.ReadDir(resultsDir)++ if err != nil {+ return nil, fmt.Errorf("error reading results dir: %v", err)+ }++ errCh := make(chan error, len(files))+ resultCh := make(chan *PlanFileResult, len(files))++ for _, file := range files {+ go func(file os.DirEntry) {++ bytes, err := os.ReadFile(filepath.Join(resultsDir, file.Name()))++ if err != nil {+ errCh <- fmt.Errorf("error reading result file: %v", err)+ return+ }++ var result PlanFileResult+ err = json.Unmarshal(bytes, &result)++ if err != nil {+ errCh <- fmt.Errorf("error unmarshalling result file: %v", err)+ return+ }++ resultCh <- &result+ }(file)+ }++ for i := 0; i < len(files); i++ {+ select {+ case err := <-errCh:+ return nil, fmt.Errorf("error reading result files: %v", err)+ case result := <-resultCh:+ results = append(results, result)+ }+ }++ sort.Slice(results, func(i, j int) bool {+ return results[i].CreatedAt.Before(results[j].CreatedAt)+ })++ return results, nil+}++func GetPlanResult(planFileResults []*shared.PlanFileResult) *shared.PlanResult {+ resByPath := make(shared.PlanFileResultsByPath)+ replacementsByPath := make(map[string][]*shared.Replacement)+ var paths []string++ for _, planFileRes := range planFileResults {+ if planFileRes.IsPending() {+ _, hasPath := resByPath[planFileRes.Path]++ resByPath[planFileRes.Path] = append(resByPath[planFileRes.Path], planFileRes)++ if !hasPath {+ paths = append(paths, planFileRes.Path)+ }+ }+ }++ for _, results := range resByPath {+ for _, planRes := range results {+ replacementsByPath[planRes.Path] = append(replacementsByPath[planRes.Path], planRes.Replacements...)+ }+ }++ // sort paths ascending+ sort.Slice(paths, func(i, j int) bool {+ return paths[i] < paths[j]+ })++ return &shared.PlanResult{+ FileResultsByPath: resByPath,+ SortedPaths: paths,+ ReplacementsByPath: replacementsByPath,+ }+}++func ApplyPlan(orgId, planId string) error {+ resultsDir := getPlanResultsDir(orgId, planId)++ files, err := os.ReadDir(resultsDir)++ if err != nil {+ return fmt.Errorf("error reading results dir: %v", err)+ }++ errCh := make(chan error, len(files))++ now := time.Now()++ for _, file := range files {+ go func(file os.DirEntry) {++ bytes, err := os.ReadFile(filepath.Join(resultsDir, file.Name()))++ if err != nil {+ errCh <- fmt.Errorf("error reading result file: %v", err)+ return+ }++ var result PlanFileResult+ err = json.Unmarshal(bytes, &result)++ if err != nil {+ errCh <- fmt.Errorf("error unmarshalling result file: %v", err)+ return+ }++ if result.AppliedAt != nil {+ errCh <- nil+ return+ }++ result.AppliedAt = &now++ bytes, err = json.MarshalIndent(result, "", " ")++ if err != nil {+ errCh <- fmt.Errorf("error marshalling result: %v", err)+ return+ }++ err = os.WriteFile(filepath.Join(resultsDir, file.Name()), bytes, 0644)++ if err != nil {+ errCh <- fmt.Errorf("error writing result file: %v", err)+ return+ }++ errCh <- nil++ }(file)+ }++ for i := 0; i < len(files); i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error applying plan: %v", err)+ }+ }++ _, err = Conn.Exec("UPDATE plans SET applied_at = NOW() WHERE id = $1", planId)++ if err != nil {+ return fmt.Errorf("error updating plan: %v", err)+ }++ return nil+}++func RejectAllResults(orgId, planId string) error {+ resultsDir := getPlanResultsDir(orgId, planId)++ files, err := os.ReadDir(resultsDir)++ if err != nil {+ return fmt.Errorf("error reading results dir: %v", err)+ }++ errCh := make(chan error, len(files))+ now := time.Now()++ for _, file := range files {+ resultId := strings.TrimSuffix(file.Name(), ".json")++ go func(resultId string) {+ err := RejectPlanFileResult(orgId, planId, resultId, now)++ if err != nil {+ errCh <- fmt.Errorf("error rejecting result: %v", err)+ return+ }++ errCh <- nil+ }(resultId)+ }++ for i := 0; i < len(files); i++ {+ err := <-errCh+ if err != nil {+ return fmt.Errorf("error rejecting plan: %v", err)+ }+ }++ return nil+}++func RejectPlanFileResult(orgId, planId, resultId string, now time.Time) error {+ resultsDir := getPlanResultsDir(orgId, planId)++ bytes, err := os.ReadFile(filepath.Join(resultsDir, resultId+".json"))++ if err != nil {+ return fmt.Errorf("error reading result file: %v", err)+ }++ var result PlanFileResult+ err = json.Unmarshal(bytes, &result)++ if err != nil {+ return fmt.Errorf("error unmarshalling result file: %v", err)+ }++ if result.RejectedAt != nil {+ return nil+ }++ result.RejectedAt = &now++ bytes, err = json.MarshalIndent(result, "", " ")++ if err != nil {+ return fmt.Errorf("error marshalling result: %v", err)+ }++ err = os.WriteFile(filepath.Join(resultsDir, resultId+".json"), bytes, 0644)++ if err != nil {+ return fmt.Errorf("error writing result file: %v", err)+ }++ return nil+}++func RejectReplacement(orgId, planId, resultId, replacementId string) error {+ resultsDir := getPlanResultsDir(orgId, planId)++ bytes, err := os.ReadFile(filepath.Join(resultsDir, resultId+".json"))++ if err != nil {+ return fmt.Errorf("error reading result file: %v", err)+ }++ var result PlanFileResult+ err = json.Unmarshal(bytes, &result)++ if err != nil {+ return fmt.Errorf("error unmarshalling result file: %v", err)+ }++ if result.RejectedAt != nil {+ return nil+ }++ now := time.Now()++ foundReplacement := false+ for _, replacement := range result.Replacements {+ if replacement.Id == replacementId {+ replacement.RejectedAt = &now+ foundReplacement = true+ break+ }+ }++ if !foundReplacement {+ return fmt.Errorf("replacement not found: %s", replacementId)+ }++ return nil+}diff --git a/server/go.mod b/server/go.modindex 7a382ee2..f052e33f 100644--- a/server/go.mod+++ b/server/go.mod@@ -12,9 +12,23 @@ require (require (github.com/dlclark/regexp2 v1.10.0 // indirect+ github.com/hashicorp/errwrap v1.1.0 // indirect+ github.com/hashicorp/go-multierror v1.1.1 // indirect+ github.com/mattn/go-colorable v0.1.13 // indirect+ github.com/mattn/go-isatty v0.0.20 // indirect+ github.com/mattn/go-runewidth v0.0.9 // indirect+ github.com/olekukonko/tablewriter v0.0.5 // indirectgithub.com/pkoukk/tiktoken-go v0.1.6 // indirect+ go.uber.org/atomic v1.7.0 // indirect+ golang.org/x/sys v0.14.0 // indirect)-require github.com/looplab/fsm v1.0.1 // indirect+require (+ github.com/fatih/color v1.16.0+ github.com/golang-migrate/migrate/v4 v4.16.2+ github.com/jmoiron/sqlx v1.3.5+ github.com/lib/pq v1.10.9+ github.com/looplab/fsm v1.0.1 // indirect+)replace github.com/plandex/plandex/shared => ../shareddiff --git a/server/go.sum b/server/go.sumindex 8d8a0e62..4cc6876f 100644--- a/server/go.sum+++ b/server/go.sum@@ -1,20 +1,93 @@+github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=+github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=+github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=+github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=+github.com/dhui/dktest v0.3.16 h1:i6gq2YQEtcrjKbeJpBkWjE8MmLZPYllcjOFbTZuPDnw=+github.com/dhui/dktest v0.3.16/go.mod h1:gYaA3LRmM8Z4vJl2MA0THIigJoZrwOansEOsp+kqxp0=github.com/dlclark/regexp2 v1.10.0 h1:+/GIL799phkJqYW+3YbOd8LCcbHzT0Pbo8zl70MHsq0=github.com/dlclark/regexp2 v1.10.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=+github.com/docker/distribution v2.8.2+incompatible h1:T3de5rq0dB1j30rp0sA2rER+m322EBzniBPB6ZIzuh8=+github.com/docker/distribution v2.8.2+incompatible/go.mod h1:J2gT2udsDAN96Uj4KfcMRqY0/ypR+oyYUYmja8H+y+w=+github.com/docker/docker v20.10.24+incompatible h1:Ugvxm7a8+Gz6vqQYQQ2W7GYq5EUPaAiuPgIfVyI3dYE=+github.com/docker/docker v20.10.24+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=+github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=+github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=+github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=+github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=+github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=+github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=+github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=+github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=+github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=+github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=+github.com/golang-migrate/migrate/v4 v4.16.2 h1:8coYbMKUyInrFk1lfGfRovTLAW7PhWp8qQDT2iKfuoA=+github.com/golang-migrate/migrate/v4 v4.16.2/go.mod h1:pfcJX4nPHaVdc5nmdCikFBWtm+UBpiZjRNNsyBbp0/o=github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=+github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=+github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=+github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=+github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=+github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=+github.com/jmoiron/sqlx v1.3.5 h1:vFFPA71p1o5gAeqtEAwLU4dnX2napprKtHr7PYIcN3g=+github.com/jmoiron/sqlx v1.3.5/go.mod h1:nRVWtLre0KfCLJvgxzCsLVMogSvQ1zNJtpYr2Ccp0mQ=+github.com/lib/pq v1.2.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=+github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=+github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=+github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=+github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=+github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=+github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=+github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=+github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=+github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=+github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=+github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=+github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=+github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=+github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=+github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=+github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=+github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=+github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=+github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=+github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=+github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=+github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=+github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=github.com/pkoukk/tiktoken-go v0.1.6 h1:JF0TlJzhTbrI30wCvFuiw6FzP2+/bR+FIxUdgEAcUsw=github.com/pkoukk/tiktoken-go v0.1.6/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=github.com/sashabaranov/go-openai v1.17.5 h1:ItBzlrrfTtkFWOFlgfOhk3y/xRBC4PJol4gdbiK7hgg=github.com/sashabaranov/go-openai v1.17.5/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=+github.com/sirupsen/logrus v1.9.2 h1:oxx1eChJGI6Uks2ZC4W1zpLlVgqB8ner4EuQwV4Ik1Y=+github.com/sirupsen/logrus v1.9.2/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=+github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=+go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=+go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=+golang.org/x/mod v0.10.0 h1:lFO9qtOdlre5W1jxS3r/4szv2/6iXxScdzjoBMXNhYk=+golang.org/x/mod v0.10.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=+golang.org/x/net v0.10.0 h1:X2//UzNDwYmtCLn7To6G58Wr6f5ahEAQgKNzv9Y951M=+golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=+golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=+golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=+golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=+golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=+golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=+golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=+golang.org/x/tools v0.9.1 h1:8WMNJAz3zrtPmnYC7ISf5dEn3MT0gY7jBJfw27yrrLo=+golang.org/x/tools v0.9.1/go.mod h1:owI94Op576fPu3cIGQeHs3joujW/2Oc6MtlxbF5dfNc=gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=diff --git a/server/handlers/build.go b/server/handlers/build.godeleted file mode 100644index 17e4551d..00000000--- a/server/handlers/build.go+++ /dev/null@@ -1,50 +0,0 @@-package handlers--import (- "encoding/json"- "io"- "log"- "net/http"-- "github.com/plandex/plandex/shared"-)--func BuildHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for BuildHandler")-- // Read the request body- body, err := io.ReadAll(r.Body)- if err != nil {- log.Printf("Error reading request body: %v\n", err)- http.Error(w, "Error reading request body", http.StatusInternalServerError)- return- }- defer r.Body.Close()-- // Unmarshal the request body to get the 'prompt'- var requestBody shared.BuildRequest- if err := json.Unmarshal(body, &requestBody); err != nil {- log.Printf("Error parsing request body: %v\n", err)- http.Error(w, "Error parsing request body", http.StatusBadRequest)- return- }-- // startStream(w, func(onStream types.OnStreamFunc) error {-- // err := proposal.BuildPlan(&types.BuildParams{- // ProposalId: requestBody.ProposalId,- // BuildPaths: requestBody.BuildPaths,- // CurrentPlan: requestBody.CurrentPlan,- // }, onStream)-- // if err != nil {- // log.Printf("Error building plan: %v\n", err)- // http.Error(w, "Error building plan: "+err.Error(), http.StatusInternalServerError)- // return err- // }-- // return nil-- // })--}diff --git a/server/handlers/convo.go b/server/handlers/convo.godeleted file mode 100644index f1852940..00000000--- a/server/handlers/convo.go+++ /dev/null@@ -1,50 +0,0 @@-package handlers--import (- "encoding/json"- "log"- "net/http"- "plandex-server/model/proposal"-- "github.com/gorilla/mux"-)--func ConvoSummaryHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for ConvoSummaryHandler")-- // get the rootId from the request- vars := mux.Vars(r)- rootId := vars["rootId"]-- if rootId == "" {- log.Println("Received empty rootId field")- http.Error(w, "rootId field is required", http.StatusBadRequest)- return- }-- latestTimestamp := r.URL.Query().Get("latestTimestamp")-- summary := proposal.GetConvoSummary(rootId)-- if summary == nil {- log.Printf("No summary found for rootId %s\n", rootId)- w.WriteHeader(http.StatusNoContent)- return- }-- if latestTimestamp != "" && latestTimestamp == summary.LastMessageTimestamp {- log.Printf("No new messages found for rootId %s\n", rootId)- w.WriteHeader(http.StatusNoContent)- return- }-- jsonBytes, err := json.Marshal(summary)- if err != nil {- log.Printf("Error marshalling summary: %v\n", err)- http.Error(w, "Error marshalling summary: "+err.Error(), http.StatusInternalServerError)- return- }-- log.Println("Successfully processed ConvoSummaryHandler request")- w.Write(jsonBytes)-}diff --git a/server/handlers/create.go b/server/handlers/create.godeleted file mode 100644index 86697694..00000000--- a/server/handlers/create.go+++ /dev/null@@ -1,80 +0,0 @@-package handlers--import (- "encoding/json"- "fmt"- "io"- "log"- "net/http"- "plandex-server/model/proposal"- "plandex-server/types"-- "github.com/plandex/plandex/shared"-)--func ProposalHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for ProposalHandler")-- // Read the request body- body, err := io.ReadAll(r.Body)- if err != nil {- log.Printf("Error reading request body: %v\n", err)- http.Error(w, "Error reading request body", http.StatusInternalServerError)- return- }- defer r.Body.Close()-- // Unmarshal the request body to get the 'prompt'- var requestBody shared.PromptRequest- if err := json.Unmarshal(body, &requestBody); err != nil {- log.Printf("Error parsing request body: %v\n", err)- http.Error(w, "Error parsing request body", http.StatusBadRequest)- return- }-- if requestBody.Prompt == "" {- log.Println("Received empty prompt field")- http.Error(w, "prompt field is required", http.StatusBadRequest)- return- }-- w.Header().Set("Transfer-Encoding", "chunked")- w.Header().Set("Content-Type", "text/plain; charset=utf-8")-- startStream(w, func(onStream types.OnStreamFunc) error {- fmt.Println("creating proposal and starting stream")- err = proposal.CreateProposal(requestBody, onStream)-- if err != nil {- log.Printf("Error creating proposal: %v\n", err)- http.Error(w, "Error creating proposal: "+err.Error(), http.StatusInternalServerError)- return err- }-- return nil- })-}--func AbortProposalHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for AbortProposalHandler")-- proposalId := r.URL.Query().Get("proposalId")-- if proposalId == "" {- log.Println("Received empty proposalId field")- http.Error(w, "proposalId field is required", http.StatusBadRequest)- return- }-- err := proposal.AbortProposal(proposalId)- if err != nil {- log.Printf("Error aborting proposal: %v\n", err)- http.Error(w, "Error aborting proposal: "+err.Error(), http.StatusInternalServerError)- return- }-- log.Println("Successfully aborted proposal")-- w.WriteHeader(http.StatusOK)- w.Write([]byte("Ok"))-}diff --git a/server/handlers/name.go b/server/handlers/name.godeleted file mode 100644index b0560ba3..00000000--- a/server/handlers/name.go+++ /dev/null@@ -1,49 +0,0 @@-package handlers--import (- "encoding/json"- "io"- "log"- "net/http"- "plandex-server/model"-- "github.com/plandex/plandex/shared"-)--func FileNameHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for FileNameHandler")-- // Read the request body- body, err := io.ReadAll(r.Body)- if err != nil {- log.Printf("Error reading request body: %v\n", err)- http.Error(w, "Error reading request body", http.StatusInternalServerError)- return- }- defer r.Body.Close()-- // Unmarshal the request body to get the 'prompt'- var requestBody shared.FileNameRequest- if err := json.Unmarshal(body, &requestBody); err != nil {- log.Printf("Error parsing request body: %v\n", err)- http.Error(w, "Error parsing request body", http.StatusBadRequest)- return- }-- if requestBody.Text == "" {- log.Println("Received empty text field")- http.Error(w, "text field is required", http.StatusBadRequest)- return- }-- modelResp, _, err := model.FileName(requestBody.Text)- if err != nil {- log.Printf("Error creating file name for text: %v\n", err)- http.Error(w, "Error creating file name for text: "+err.Error(), http.StatusInternalServerError)- return- }-- log.Println("Successfully processed file name request", string(modelResp))- // Return the response from OpenAI to the client- w.Write(modelResp)-}diff --git a/server/handlers/plans_changes.go b/server/handlers/plans_changes.gonew file mode 100644index 00000000..4f2b216f--- /dev/null+++ b/server/handlers/plans_changes.go@@ -0,0 +1,186 @@+package handlers++import (+ "encoding/json"+ "log"+ "net/http"+ "plandex-server/db"+ "time"++ "github.com/gorilla/mux"+)++func CurrentPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListPlanChangesHandler")++ // TODO: get this from auth when implemented+ currentOrgId := "org1"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ contexts, err := db.GetPlanContexts(currentOrgId, planId, true)++ if err != nil {+ log.Printf("Error getting plan contexts: %v\n", err)+ http.Error(w, "Error getting plan contexts: "+err.Error(), http.StatusInternalServerError)+ return+ }++ planState, err := db.GetCurrentPlanState(currentOrgId, planId, contexts)++ if err != nil {+ log.Printf("Error getting current plan state: %v\n", err)+ http.Error(w, "Error getting current plan state: "+err.Error(), http.StatusInternalServerError)+ return+ }++ jsonBytes, err := json.Marshal(planState)++ if err != nil {+ log.Printf("Error marshalling plan state: %v\n", err)+ http.Error(w, "Error marshalling plan state: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully retrieved current plan state")++ w.Write(jsonBytes)+}++func ApplyPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ApplyPlanHandler")++ // TODO: get this from auth when implemented+ currentOrgId := "org1"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ err := db.ApplyPlan(currentOrgId, planId)++ if err != nil {+ log.Printf("Error applying plan: %v\n", err)+ http.Error(w, "Error applying plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully applied plan", planId)+}++func RejectAllChangesHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RejectAllChangesHandler")++ // TODO: get this from auth when implemented+ currentOrgId := "org1"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ err := db.RejectAllResults(currentOrgId, planId)++ if err != nil {+ log.Printf("Error rejecting all changes: %v\n", err)+ http.Error(w, "Error rejecting all changes: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully rejected all changes for plan", planId)+}++func RejectResultHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RejectResultHandler")++ // TODO: get this from auth when implemented+ currentOrgId := "org1"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]+ resultId := vars["resultId"]++ log.Println("planId: ", planId, "resultId: ", resultId)++ err := db.RejectPlanFileResult(currentOrgId, planId, resultId, time.Now())++ if err != nil {+ log.Printf("Error rejecting result: %v\n", err)+ http.Error(w, "Error rejecting result: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully rejected plan result", resultId)+}++func RejectReplacementHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RejectReplacementHandler")++ // TODO: get this from auth when implemented+ currentOrgId := "org1"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]+ resultId := vars["resultId"]+ replacementId := vars["replacementId"]++ log.Println("planId: ", planId, "resultId: ", resultId, "replacementId: ", replacementId)++ err := db.RejectReplacement(currentOrgId, planId, resultId, replacementId)++ if err != nil {+ log.Printf("Error rejecting replacement: %v\n", err)+ http.Error(w, "Error rejecting replacement: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully rejected replacement", replacementId)+}++func ArchivePlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ArchivePlanHandler")++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ res, err := db.Conn.Exec("UPDATE plans SET archived_at = NOW() WHERE id = $1", planId)++ if err != nil {+ log.Printf("Error archiving plan: %v\n", err)+ http.Error(w, "Error archiving plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ rowsAffected, err := res.RowsAffected()+ if err != nil {+ log.Printf("Error getting rows affected: %v\n", err)+ http.Error(w, "Error getting rows affected: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if rowsAffected == 0 {+ log.Println("Plan not found")+ http.Error(w, "Not found", http.StatusNotFound)+ return+ }++ log.Println("Successfully archived plan", planId)+}diff --git a/server/handlers/plans_context.go b/server/handlers/plans_context.gonew file mode 100644index 00000000..fd2aa19e--- /dev/null+++ b/server/handlers/plans_context.go@@ -0,0 +1,489 @@+package handlers++import (+ "crypto/sha256"+ "encoding/hex"+ "encoding/json"+ "fmt"+ "io"+ "log"+ "net/http"+ "plandex-server/db"+ "sync"++ "github.com/google/uuid"+ "github.com/gorilla/mux"+ "github.com/plandex/plandex/shared"+)++func ListContextHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListContextHandler")++ // TODO: get from auth when implemented+ // currentUserId := "user1"+ currentOrgId := "org1"++ // TODO: authenticate user and plan access+ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ dbContexts, err := db.GetPlanContexts(currentOrgId, planId, false)++ if err != nil {+ log.Printf("Error getting contexts: %v\n", err)+ http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)+ return+ }++ var apiContexts []*shared.Context++ for _, dbContext := range dbContexts {+ apiContexts = append(apiContexts, dbContext.ToApi())+ }++ bytes, err := json.Marshal(apiContexts)++ if err != nil {+ log.Printf("Error marshalling contexts: %v\n", err)+ http.Error(w, "Error marshalling contexts: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)+}++func LoadContextHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for LoadContextHandler")++ // TODO: get from auth when implemented+ currentUserId := "user1"+ currentOrgId := "org1"++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.LoadContextRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ // load plan+ plan, err := db.GetPlan(planId)++ if err != nil {+ log.Printf("Plan not found: %v\n", err)+ http.Error(w, "Plan not found: "+err.Error(), http.StatusBadRequest)+ }++ maxTokens := shared.MaxContextTokens+ tokensAdded := 0+ totalTokens := plan.ContextTokens++ paramsById := make(map[string]*shared.LoadContextParams)+ numTokensById := make(map[string]int)++ for _, context := range requestBody {+ id := uuid.New().String()+ numTokens, err := shared.GetNumTokens(context.Body)++ if err != nil {+ log.Printf("Error getting num tokens: %v\n", err)+ http.Error(w, "Error getting num tokens: "+err.Error(), http.StatusInternalServerError)+ return+ }++ paramsById[id] = context+ numTokensById[id] = numTokens++ tokensAdded += numTokens+ totalTokens += numTokens+ }++ if totalTokens > maxTokens {+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)+ res := shared.LoadContextResponse{+ TokensAdded: tokensAdded,+ TotalTokens: totalTokens,+ MaxTokensExceeded: true,+ }++ bytes, err := json.Marshal(res)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)+ return+ }++ dbContextsCh := make(chan *db.Context)+ errCh := make(chan error)+ for id, params := range paramsById {++ go func(id string, params *shared.LoadContextParams) {+ hash := sha256.Sum256([]byte(params.Body))+ sha := hex.EncodeToString(hash[:])++ context := db.Context{+ Id: id,+ OrgId: currentOrgId,+ CreatorId: currentUserId,+ PlanId: planId,+ ContextType: params.ContextType,+ Name: params.Name,+ Url: params.Url,+ FilePath: params.FilePath,+ NumTokens: numTokensById[id],+ Sha: sha,+ Body: params.Body,+ }++ err := db.StoreContext(&context)++ if err != nil {+ errCh <- err+ return+ }++ dbContextsCh <- &context++ }(id, params)+ }++ var apiContexts []*shared.Context++ for i := 0; i < len(requestBody); i++ {+ select {+ case err := <-errCh:+ log.Printf("Error creating context: %v\n", err)+ http.Error(w, "Error creating context: "+err.Error(), http.StatusInternalServerError)+ return+ case dbContext := <-dbContextsCh:+ apiContext := dbContext.ToApi()+ apiContext.Body = ""+ apiContexts = append(apiContexts, apiContext)+ }+ }++ commitMsg := shared.SummaryForLoadContext(apiContexts) + "\n\n" + shared.TableForLoadContext(apiContexts)+ err = db.GitAddAndCommit(currentOrgId, planId, commitMsg)++ if err != nil {+ log.Printf("Error committing changes: %v\n", err)+ http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)+ return+ }++ err = db.AddPlanContextTokens(planId, tokensAdded)+ if err != nil {+ log.Printf("Error updating plan tokens: %v\n", err)+ http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)+ return+ }++ res := shared.LoadContextResponse{+ TokensAdded: tokensAdded,+ TotalTokens: totalTokens,+ Msg: commitMsg,+ }++ bytes, err := json.Marshal(res)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully processed LoadContextHandler request")++ w.Write(bytes)+}++func UpdateContextHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for UpdateContextHandler")++ // TODO: get from auth when implemented+ // currentUserId := "user1"+ currentOrgId := "org1"++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.UpdateContextRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ // load plan+ plan, err := db.GetPlan(planId)++ if err != nil {+ log.Printf("Plan not found: %v\n", err)+ http.Error(w, "Plan not found: "+err.Error(), http.StatusBadRequest)+ }++ maxTokens := shared.MaxContextTokens+ tokensAdded := 0+ totalTokens := plan.ContextTokens+ updateNumTokensById := make(map[string]int)+ contextsById := make(map[string]*db.Context)++ var mu sync.Mutex+ errCh := make(chan error)++ 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 {+ errCh <- fmt.Errorf("error getting context: %v", err)+ return+ }++ contextsById[id] = context++ updateNumTokens, err := shared.GetNumTokens(params.Body)++ tokenDiff := updateNumTokens - context.NumTokens++ if err != nil {+ errCh <- fmt.Errorf("error getting num tokens: %v", err)+ return+ }++ updateNumTokensById[id] = updateNumTokens+ tokensAdded += tokenDiff+ totalTokens += tokenDiff+ }(id, params)+ }++ if totalTokens > maxTokens {+ log.Printf("The total number of tokens (%d) exceeds the maximum allowed (%d)", totalTokens, maxTokens)+ res := shared.LoadContextResponse{+ TokensAdded: tokensAdded,+ TotalTokens: totalTokens,+ MaxTokensExceeded: true,+ }++ bytes, err := json.Marshal(res)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)+ return+ }++ updatedContextsCh := make(chan *db.Context)+ 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))+ sha := hex.EncodeToString(hash[:])++ context.Body = params.Body+ context.Sha = sha+ context.NumTokens = updateNumTokensById[id]++ err = db.StoreContext(context)++ if err != nil {+ errCh <- err+ return+ }++ updatedContextsCh <- context++ }(id, params)+ }++ var apiContexts []*shared.Context+ for i := 0; i < len(requestBody); i++ {+ select {+ case err := <-errCh:+ log.Printf("Error creating context: %v\n", err)+ http.Error(w, "Error creating context: "+err.Error(), http.StatusInternalServerError)+ return+ case dbContext := <-updatedContextsCh:+ apiContext := dbContext.ToApi()+ apiContext.Body = ""+ apiContexts = append(apiContexts, apiContext)+ }+ }++ commitMsg := shared.SummaryForUpdateContext(apiContexts) + "\n\n" + shared.TableForContextUpdate(apiContexts)+ err = db.GitAddAndCommit(currentOrgId, planId, commitMsg)++ if err != nil {+ log.Printf("Error committing changes: %v\n", err)+ http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)+ return+ }++ err = db.AddPlanContextTokens(planId, tokensAdded)+ if err != nil {+ log.Printf("Error updating plan tokens: %v\n", err)+ http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)+ return+ }++ res := shared.UpdateContextResponse{+ TokensAdded: tokensAdded,+ TotalTokens: totalTokens,+ Msg: commitMsg,+ }++ bytes, err := json.Marshal(res)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully processed UpdateContextHandler request")++ w.Write(bytes)+}++func DeleteContextHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for DeleteContextHandler")++ // TODO: get from auth when implemented+ // currentUserId := "user1"+ currentOrgId := "org1"++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ // load plan+ plan, err := db.GetPlan(planId)++ if err != nil {+ log.Printf("Plan not found: %v\n", err)+ http.Error(w, "Plan not found: "+err.Error(), http.StatusBadRequest)+ return+ }++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.DeleteContextRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ dbContexts, err := db.GetPlanContexts(currentOrgId, planId, false)++ if err != nil {+ log.Printf("Error getting contexts: %v\n", err)+ http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)+ return+ }++ var toRemove []*db.Context+ for _, dbContext := range dbContexts {+ if _, ok := requestBody.Ids[dbContext.Id]; ok {+ toRemove = append(toRemove, dbContext)+ }+ }++ err = db.ContextRemove(toRemove)++ if err != nil {+ log.Printf("Error deleting contexts: %v\n", err)+ http.Error(w, "Error deleting contexts: "+err.Error(), http.StatusInternalServerError)+ return+ }++ removeTokens := 0+ var toRemoveApiContexts []*shared.Context+ for _, dbContext := range toRemove {+ toRemoveApiContexts = append(toRemoveApiContexts, dbContext.ToApi())+ removeTokens += dbContext.NumTokens+ }++ commitMsg := shared.SummaryForRemoveContext(toRemoveApiContexts, plan.ContextTokens) + "\n\n" + shared.TableForRemoveContext(toRemoveApiContexts)+ err = db.GitAddAndCommit(currentOrgId, planId, commitMsg)++ if err != nil {+ log.Printf("Error committing changes: %v\n", err)+ http.Error(w, "Error committing changes: "+err.Error(), http.StatusInternalServerError)+ return+ }++ err = db.AddPlanContextTokens(planId, -removeTokens)+ if err != nil {+ log.Printf("Error updating plan tokens: %v\n", err)+ http.Error(w, "Error updating plan tokens: "+err.Error(), http.StatusInternalServerError)+ return+ }++ res := shared.DeleteContextResponse{+ TokensRemoved: removeTokens,+ TotalTokens: plan.ContextTokens - removeTokens,+ Msg: commitMsg,+ }++ bytes, err := json.Marshal(res)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully deleted contexts")++ w.Write(bytes)+}diff --git a/server/handlers/plans_convo.go b/server/handlers/plans_convo.gonew file mode 100644index 00000000..38165a21--- /dev/null+++ b/server/handlers/plans_convo.go@@ -0,0 +1,58 @@+package handlers++import (+ "log"+ "net/http"++ "github.com/gorilla/mux"+)++// func ConvoSummaryHandler(w http.ResponseWriter, r *http.Request) {+// log.Println("Received a request for ConvoSummaryHandler")++// // get the rootId from the request+// vars := mux.Vars(r)+// rootId := vars["rootId"]++// if rootId == "" {+// log.Println("Received empty rootId field")+// http.Error(w, "rootId field is required", http.StatusBadRequest)+// return+// }++// latestTimestamp := r.URL.Query().Get("latestTimestamp")++// summary := plan.GetConvoSummary(rootId)++// if summary == nil {+// log.Printf("No summary found for rootId %s\n", rootId)+// w.WriteHeader(http.StatusNoContent)+// return+// }++// if latestTimestamp != "" && latestTimestamp == summary.LastMessageTimestamp {+// log.Printf("No new messages found for rootId %s\n", rootId)+// w.WriteHeader(http.StatusNoContent)+// return+// }++// jsonBytes, err := json.Marshal(summary)+// if err != nil {+// log.Printf("Error marshalling summary: %v\n", err)+// http.Error(w, "Error marshalling summary: "+err.Error(), http.StatusInternalServerError)+// return+// }++// log.Println("Successfully processed ConvoSummaryHandler request")+// w.Write(jsonBytes)+// }++func ListConvoHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received a request for ListConvoHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}diff --git a/server/handlers/plans_crud.go b/server/handlers/plans_crud.gonew file mode 100644index 00000000..4ff33f58--- /dev/null+++ b/server/handlers/plans_crud.go@@ -0,0 +1,263 @@+package handlers++import (+ "encoding/json"+ "fmt"+ "io"+ "log"+ "net/http"+ "plandex-server/db"++ "github.com/gorilla/mux"+ "github.com/plandex/plandex/shared"+)++func CreatePlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for CreatePlanHandler")++ // TODO: get from auth when implemented+ currentUserId := "user1"+ currentOrgId := "org1"++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.CreatePlanRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ name := requestBody.Name+ if name == "" {+ name = "draft"+ }++ if name == "draft" {+ // delete any existing draft plans+ err = db.DeleteDraftPlans(currentOrgId, projectId, currentUserId)++ if err != nil {+ log.Printf("Error deleting draft plans: %v\n", err)+ http.Error(w, "Error deleting draft plans: "+err.Error(), http.StatusInternalServerError)+ return+ }+ } else {+ i := 2+ originalName := name+ for {+ var count int+ err := db.Conn.Get(&count, "SELECT COUNT(*) FROM plans WHERE project_id = $1 AND creator_id = $2 AND name = $3", projectId, currentUserId, name)++ if err != nil {+ log.Printf("Error checking if plan exists: %v\n", err)+ http.Error(w, "Error checking if plan exists: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if count == 0 {+ break+ }++ name = originalName + "." + fmt.Sprint(i)+ i+++ }+ }++ plan, err := db.CreatePlan(currentOrgId, projectId, currentUserId, name)++ if err != nil {+ log.Printf("Error creating plan: %v\n", err)+ http.Error(w, "Error creating plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ resp := shared.CreatePlanResponse{+ Id: plan.Id,+ Name: plan.Name,+ }++ bytes, err := json.Marshal(resp)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)++ log.Printf("Successfully created plan: %v\n", plan)+}++func GetPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for GetPlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ plan, err := db.GetPlan(planId)++ if err != nil {+ log.Printf("Error getting plan: %v\n", err)+ http.Error(w, "Error getting plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ bytes, err := json.Marshal(plan)++ if err != nil {+ log.Printf("Error marshalling plan: %v\n", err)+ http.Error(w, "Error marshalling plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)+}++func DeletePlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for DeletePlanHandler")++ // TODO: get from auth when implemented+ currentOrgId := "org1"++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ res, err := db.Conn.Exec("DELETE FROM plans WHERE id = $1", planId)++ if err != nil {+ log.Printf("Error deleting plan: %v\n", err)+ http.Error(w, "Error deleting plan: "+err.Error(), http.StatusInternalServerError)+ return+ }++ rowsAffected, err := res.RowsAffected()+ if err != nil {+ log.Printf("Error getting rows affected: %v\n", err)+ http.Error(w, "Error getting rows affected: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if rowsAffected == 0 {+ log.Println("Plan not found")+ http.Error(w, "Not found", http.StatusNotFound)+ return+ }++ err = db.DeletePlanDir(currentOrgId, planId)++ if err != nil {+ log.Printf("Error deleting plan dir: %v\n", err)+ http.Error(w, "Error deleting plan dir: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully deleted plan", planId)+}++func DeleteAllPlansHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for DeleteAllPlansHandler")++ // TODO: get from auth when implemented+ currentOrgId := "org1"+ currentUserId := "user1"++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ err := db.DeletePlans(currentOrgId, projectId, currentUserId)++ if err != nil {+ log.Printf("Error deleting plans: %v\n", err)+ http.Error(w, "Error deleting plans: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully deleted all plans")+}++func ListPlansHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListPlansHandler")+ currentUserId := "user1" // TODO: get from auth when implemented++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ plans, err := db.ListPlans(projectId, currentUserId, false, "")++ if err != nil {+ log.Printf("Error listing plans: %v\n", err)+ http.Error(w, "Error listing plans: "+err.Error(), http.StatusInternalServerError)+ return+ }++ jsonBytes, err := json.Marshal(plans)+ if err != nil {+ log.Printf("Error marshalling plans: %v\n", err)+ http.Error(w, "Error marshalling plans: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully processed ListPlansHandler request")++ w.Write(jsonBytes)+}++func ListArchivedPlansHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListArchivedPlansHandler")++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ plans, err := db.ListPlans(projectId, "", true, "")++ if err != nil {+ log.Printf("Error listing plans: %v\n", err)+ http.Error(w, "Error listing plans: "+err.Error(), http.StatusInternalServerError)+ return+ }++ jsonBytes, err := json.Marshal(plans)+ if err != nil {+ log.Printf("Error marshalling plans: %v\n", err)+ http.Error(w, "Error marshalling plans: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully processed ListArchivedPlansHandler request")++ w.Write(jsonBytes)+}++func ListPlansRunningHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListPlansRunningHandler")++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ // TODO: implement when status is figured out++}diff --git a/server/handlers/plans_exec.go b/server/handlers/plans_exec.gonew file mode 100644index 00000000..0b3c2312--- /dev/null+++ b/server/handlers/plans_exec.go@@ -0,0 +1,90 @@+package handlers++import (+ "encoding/json"+ "io"+ "log"+ "net/http"+ "plandex-server/model/plan"++ "github.com/gorilla/mux"+ "github.com/plandex/plandex/shared"+)++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"++ // TODO: authenticate user and plan access++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.TellPlanRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ err = plan.Tell(planId, currentUserId, currentOrgId, &requestBody)++ if err != nil {+ log.Printf("Error telling plan: %v\n", err)+ http.Error(w, "Error telling plan", http.StatusInternalServerError)+ return+ }++ if requestBody.ConnectStream {+ active := plan.Active.Get(planId)+ subscriptionId, ch := plan.SubscribePlan(planId)++ startResponseStream(w, ch, active.Ctx, func() {+ plan.UnsubscribePlan(planId, subscriptionId)+ })+ }++}++func BuildPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for BuildPlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}++func ConnectPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ConnectPlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}++func StopPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for StopPlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}diff --git a/server/handlers/plans_versions.go b/server/handlers/plans_versions.gonew file mode 100644index 00000000..c2101a47--- /dev/null+++ b/server/handlers/plans_versions.go@@ -0,0 +1,28 @@+package handlers++import (+ "log"+ "net/http"++ "github.com/gorilla/mux"+)++func ListLogsHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListLogsHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}++func RewindPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RewindPlanHandler")++ vars := mux.Vars(r)+ planId := vars["planId"]++ log.Println("planId: ", planId)++}diff --git a/server/handlers/projects.go b/server/handlers/projects.gonew file mode 100644index 00000000..fc80cce7--- /dev/null+++ b/server/handlers/projects.go@@ -0,0 +1,199 @@+package handlers++import (+ "encoding/json"+ "io"+ "log"+ "net/http"+ "plandex-server/db"++ "github.com/google/uuid"+ "github.com/gorilla/mux"+ "github.com/plandex/plandex/shared"+)++func CreateProjectHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for CreateProjectHandler")++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.CreateProjectRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ if requestBody.Name == "" {+ log.Println("Received empty name field")+ http.Error(w, "name field is required", http.StatusBadRequest)+ return+ }++ id := uuid.New().String()++ _, err = db.Conn.Exec("INSERT INTO projects (id, name) VALUES ($1, $2)", id, requestBody.Name)++ if err != nil {+ log.Printf("Error creating project: %v\n", err)+ http.Error(w, "Error creating project: "+err.Error(), http.StatusInternalServerError)+ return+ }++ resp := shared.CreateProjectResponse{+ Id: id,+ }++ bytes, err := json.Marshal(resp)++ if err != nil {+ log.Printf("Error marshalling response: %v\n", err)+ http.Error(w, "Error marshalling response: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)++ log.Println("Successfully created project", id)++}++func ListProjectsHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for ListProjectsHandler")++ userId := "user1" // TODO: get from auth when implemented++ rows, 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)++ if err != nil {+ log.Printf("Error listing projects: %v\n", err)+ http.Error(w, "Error listing projects: "+err.Error(), http.StatusInternalServerError)+ return+ }++ var projects []shared.Project++ for rows.Next() {+ var project shared.Project+ err := rows.Scan(&project.Id, &project.Name)+ if err != nil {+ log.Printf("Error scanning project: %v\n", err)+ http.Error(w, "Error scanning project: "+err.Error(), http.StatusInternalServerError)+ return+ }+ projects = append(projects, project)+ }++ bytes, err := json.Marshal(projects)+ if err != nil {+ log.Printf("Error marshalling projects: %v\n", err)+ http.Error(w, "Error marshalling projects: "+err.Error(), http.StatusInternalServerError)+ return+ }++ w.Write(bytes)+}++func ProjectSetPlanHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for UpdateProjectSetPlanHandler")++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.SetProjectPlanRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ if requestBody.PlanId == "" {+ log.Println("Received empty planId field")+ http.Error(w, "planId field is required", http.StatusBadRequest)+ return+ }++ // update statement here -- need auth / current user id++ if err != nil {+ log.Printf("Error updating project: %v\n", err)+ http.Error(w, "Error updating project: "+err.Error(), http.StatusInternalServerError)+ return+ }++ log.Println("Successfully set project plan", projectId)+}++func RenameProjectHandler(w http.ResponseWriter, r *http.Request) {+ log.Println("Received request for RenameProjectHandler")++ vars := mux.Vars(r)+ projectId := vars["projectId"]++ log.Println("projectId: ", projectId)++ // read the request body+ body, err := io.ReadAll(r.Body)+ if err != nil {+ log.Printf("Error reading request body: %v\n", err)+ http.Error(w, "Error reading request body", http.StatusInternalServerError)+ return+ }+ defer r.Body.Close()++ var requestBody shared.RenameProjectRequest+ if err := json.Unmarshal(body, &requestBody); err != nil {+ log.Printf("Error parsing request body: %v\n", err)+ http.Error(w, "Error parsing request body", http.StatusBadRequest)+ return+ }++ if requestBody.Name == "" {+ log.Println("Received empty name field")+ http.Error(w, "name field is required", http.StatusBadRequest)+ return+ }++ res, err := db.Conn.Exec("UPDATE projects SET name = $1 WHERE id = $2", requestBody.Name, projectId)++ if err != nil {+ log.Printf("Error updating project: %v\n", err)+ http.Error(w, "Error updating project: "+err.Error(), http.StatusInternalServerError)+ return+ }++ rowsAffected, err := res.RowsAffected()++ if err != nil {+ log.Printf("Error getting rows affected: %v\n", err)+ http.Error(w, "Error getting rows affected: "+err.Error(), http.StatusInternalServerError)+ return+ }++ if rowsAffected == 0 {+ log.Printf("Project not found: %v\n", projectId)+ http.Error(w, "Project not found: "+projectId, http.StatusNotFound)+ return+ }++ log.Println("Successfully renamed project", projectId)++}diff --git a/server/handlers/stream.go b/server/handlers/stream.godeleted file mode 100644index 3718ab55..00000000--- a/server/handlers/stream.go+++ /dev/null@@ -1,71 +0,0 @@-package handlers--import (- "fmt"- "log"- "net/http"- "plandex-server/types"- "sync"-- "github.com/plandex/plandex/shared"-)--func startStream(w http.ResponseWriter, startFn func(onStream types.OnStreamFunc) error) {- var isResponseClosed bool- done := make(chan struct{})- var streamMu sync.Mutex-- onStream := func(content string, err error) {- streamMu.Lock()- defer streamMu.Unlock()-- if isResponseClosed {- return- }-- if err != nil {- isResponseClosed = true- log.Printf("Error writing stream content to client: %v\n", err)- close(done)- return- }-- // stream content to client- if content != "" {- // fmt.Println("writing stream content to client:", content)-- bytes := []byte(content + shared.STREAM_MESSAGE_SEPARATOR)- _, err = w.Write(bytes)- if err != nil {- isResponseClosed = true- log.Printf("Error writing stream content to client: %v\n", err)- log.Printf("Content: %s\n", string(bytes))- close(done)- return- }- if flusher, ok := w.(http.Flusher); ok {- flusher.Flush()- }- }-- if content == shared.STREAM_FINISHED {- isResponseClosed = true- close(done)- fmt.Println("proposal stream finished")- return- }- }-- err := startFn(onStream)- if err != nil {- log.Printf("Error starting stream: %v\n", err)- close(done)- return- }-- // block until streaming is done- <-done-- fmt.Println("done")--}diff --git a/server/handlers/stream_helper.go b/server/handlers/stream_helper.gonew file mode 100644index 00000000..66db2275--- /dev/null+++ b/server/handlers/stream_helper.go@@ -0,0 +1,38 @@+package handlers++import (+ "context"+ "log"+ "net/http"++ "github.com/plandex/plandex/shared"+)++func startResponseStream(w http.ResponseWriter, ch chan string, ctx context.Context, closeFn func()) {+ w.Header().Set("Transfer-Encoding", "chunked")+ w.Header().Set("Content-Type", "text/plain; charset=utf-8")++ defer func() {+ log.Println("Response stream manager: plan stream done")+ closeFn()+ }()++ for {+ select {+ case <-ctx.Done():+ log.Println("Response stream manager: context done")+ return+ case msg := <-ch:+ bytes := []byte(msg + shared.STREAM_MESSAGE_SEPARATOR)+ _, err := w.Write(bytes)+ if err != nil {+ log.Printf("Response stream mananger: error writing to client: %v\n", err)+ return+ }+ if flusher, ok := w.(http.Flusher); ok {+ flusher.Flush()+ }+ }+ }++}diff --git a/server/handlers/summarize.go b/server/handlers/summarize.godeleted file mode 100644index a1dc5a9f..00000000--- a/server/handlers/summarize.go+++ /dev/null@@ -1,49 +0,0 @@-package handlers--import (- "encoding/json"- "io"- "log"- "net/http"- "plandex-server/model"-- "github.com/plandex/plandex/shared"-)--func ShortSummaryHandler(w http.ResponseWriter, r *http.Request) {- log.Println("Received a request for ShortSummaryHandler")-- // Read the request body- body, err := io.ReadAll(r.Body)- if err != nil {- log.Printf("Error reading request body: %v\n", err)- http.Error(w, "Error reading request body", http.StatusInternalServerError)- return- }- defer r.Body.Close()-- // Unmarshal the request body to get the 'prompt'- var requestBody shared.ShortSummaryRequest- if err := json.Unmarshal(body, &requestBody); err != nil {- log.Printf("Error parsing request body: %v\n", err)- http.Error(w, "Error parsing request body", http.StatusBadRequest)- return- }-- if requestBody.Text == "" {- log.Println("Received empty text field")- http.Error(w, "text field is required", http.StatusBadRequest)- return- }-- modelResp, err := model.ShortSummary(requestBody.Text)- if err != nil {- log.Printf("Error summarizing text: %v\n", err)- http.Error(w, "Error summarizing text: "+err.Error(), http.StatusInternalServerError)- return- }-- log.Println("Successfully processed summarize request")- // Return the response from OpenAI to the client- w.Write(modelResp)-}diff --git a/server/main.go b/server/main.goindex 7aa1253a..45bf897a 100644--- a/server/main.go+++ b/server/main.go@@ -5,12 +5,9 @@ import ("log""net/http""os"+ "plandex-server/db"openai "github.com/sashabaranov/go-openai"-- "github.com/gorilla/mux"-- "plandex-server/handlers")var client *openai.Client@@ -20,18 +17,17 @@ func main() {log.Fatal("OPENAI_API_KEY environment variable is not set")}- r := mux.NewRouter()+ err := db.Connect()+ if err != nil {+ log.Fatal("Error initializing database: ", err)+ }- r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {- fmt.Fprintf(w, "OK")- })+ err = db.MigrationsUp()+ if err != nil {+ log.Fatal("Error running migrations: ", err)+ }- r.HandleFunc("/proposal", handlers.ProposalHandler).Methods("POST")- r.HandleFunc("/abort", handlers.AbortProposalHandler).Methods("DELETE")- r.HandleFunc("/short-summary", handlers.ShortSummaryHandler).Methods("POST")- r.HandleFunc("/filename", handlers.FileNameHandler).Methods("POST")- r.HandleFunc("/convo-summary/{rootId}", handlers.ConvoSummaryHandler).Methods("GET")- r.HandleFunc("/build", handlers.BuildHandler).Methods("PUT")+ routes := InitRoutes()// Get port from the environment variable or default to 8088port := os.Getenv("PORT")@@ -40,5 +36,5 @@ func main() {}log.Printf("Plandex server is running on :%s", port)- log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), r))+ log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), routes))}diff --git a/server/migrations/20220301120000_orgs.down.sql b/server/migrations/20220301120000_orgs.down.sqldeleted file mode 100644index aa05d756..00000000--- a/server/migrations/20220301120000_orgs.down.sql+++ /dev/null@@ -1 +0,0 @@-DROP TABLE IF EXISTS orgs;diff --git a/server/migrations/20220301120000_orgs.up.sql b/server/migrations/20220301120000_orgs.up.sqldeleted file mode 100644index 7deff9e7..00000000--- a/server/migrations/20220301120000_orgs.up.sql+++ /dev/null@@ -1,5 +0,0 @@-CREATE TABLE IF NOT EXISTS orgs (- id UUID PRIMARY KEY,- created_at TIMESTAMP NOT NULL DEFAULT NOW(),- updated_at TIMESTAMP NOT NULL DEFAULT NOW()-);diff --git a/server/migrations/2023120500_init.down.sql b/server/migrations/2023120500_init.down.sqlnew file mode 100644index 00000000..983a128c--- /dev/null+++ b/server/migrations/2023120500_init.down.sql@@ -0,0 +1,9 @@+DROP TABLE IF EXISTS orgs;+DROP TABLE IF EXISTS projects;+DROP TABLE IF EXISTS orgs_users;+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.sqlnew file mode 100644index 00000000..157fb420--- /dev/null+++ b/server/migrations/2023120500_init.up.sql@@ -0,0 +1,121 @@+CREATE EXTENSION IF NOT EXISTS "uuid-ossp";++CREATE OR REPLACE FUNCTION update_updated_at_column()+RETURNS TRIGGER AS $$+BEGIN+ NEW.updated_at = NOW();+ RETURN NEW;+END;+$$ LANGUAGE plpgsql;++CREATE TABLE IF NOT EXISTS orgs (+ id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),+ name VARCHAR(255) NOT NULL,+ domain VARCHAR(255),+ auto_add_domain_users BOOLEAN NOT NULL DEFAULT FALSE,+ creator_id UUID NOT NULL REFERENCES users(id),+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW()+);+CREATE TRIGGER update_orgs_modtime BEFORE UPDATE ON orgs FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++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,+ name VARCHAR(255) NOT NULL,+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW()+);+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,+ user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,+ created_at TIMESTAMP NOT NULL DEFAULT NOW(),+ updated_at TIMESTAMP NOT NULL DEFAULT NOW()+);+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,+ name VARCHAR(255) NOT NULL,+ status VARCHAR(32),+ 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()+);+CREATE TRIGGER update_plans_modtime BEFORE UPDATE ON plans FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();++CREATE 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 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_created_at TIMESTAMP NOT NULL,+ summary TEXT NOT NULL,+ tokens INTEGER NOT NULL,+ num_messages INTEGER NOT NULL,+ 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),+ error TEXT,+ error_path TEXT,+ finished_at TIMESTAMP,+ created_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/lib/context.go b/server/model/lib/context.goindex 33d46fd6..933e068d 100644--- a/server/model/lib/context.go+++ b/server/model/lib/context.go@@ -2,12 +2,13 @@ package libimport ("fmt"+ "plandex-server/db""strings""github.com/plandex/plandex/shared")-func FormatModelContext(context shared.ModelContext) (string, int, error) {+func FormatModelContext(context []*db.Context) (string, int, error) {var contextMessages []stringvar numTokens intfor _, part := range context {@@ -15,10 +16,10 @@ func FormatModelContext(context shared.ModelContext) (string, int, error) {var fmtStr stringvar args []any- if part.Type == shared.ContextDirectoryTreeType {+ if part.ContextType == shared.ContextDirectoryTreeType {fmtStr = "\n\n- %s | directory tree:\n\n```\n%s\n```"args = append(args, part.FilePath, part.Body)- } else if part.Type == shared.ContextFileType {+ } else if part.ContextType == shared.ContextFileType {fmtStr = "\n\n- %s:\n\n```\n%s\n```"args = append(args, part.FilePath, part.Body)} else if part.Url != "" {@@ -43,12 +44,3 @@ func FormatModelContext(context shared.ModelContext) (string, int, error) {}return strings.Join(contextMessages, "\n"), numTokens, nil}--func FormatCurrentPlan(plan shared.CurrentPlanFiles) string {- var planMessages []string- for path, content := range plan.Files {- planMessages = append(planMessages, fmt.Sprintf("\n\n- %s:\n\n```%s```", path, content))- }-- return strings.Join(planMessages, "\n")-}diff --git a/server/model/name.go b/server/model/name.godeleted file mode 100644index 7c1f63c0..00000000--- a/server/model/name.go+++ /dev/null@@ -1,62 +0,0 @@-package model--import (- "context"- "encoding/json"- "fmt"- "plandex-server/model/prompts"- "strings"-- "github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"-)--func FileName(text string) ([]byte, int, error) {- resp, err := Client.CreateChatCompletion(- context.Background(),- openai.ChatCompletionRequest{- Model: NameModel,- Functions: []openai.FunctionDefinition{prompts.FileNameFn},- Messages: []openai.ChatCompletionMessage{- {- Role: openai.ChatMessageRoleSystem,- Content: prompts.SysFileName,- },- {- Role: openai.ChatMessageRoleUser,- Content: prompts.GetFileNamePrompt(text),- },- },- ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},- },- )-- if err != nil {- return nil, 0, err- }-- var byteRes []byte- for _, choice := range resp.Choices {- if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "nameFile" {- fnCall := choice.Message.FunctionCall-- if strings.HasSuffix(fnCall.Arguments, ",\n}") { // remove trailing comma- fnCall.Arguments = strings.TrimSuffix(fnCall.Arguments, ",\n}") + "\n}"- }-- byteRes = []byte(fnCall.Arguments)- }- }-- if len(byteRes) == 0 {- return nil, 0, fmt.Errorf("no nameFile function call found in response")- }-- // validate the JSON response- var nameFileResp shared.FileNameResponse- if err := json.Unmarshal(byteRes, &nameFileResp); err != nil {- return nil, resp.Usage.CompletionTokens, err- }-- return byteRes, resp.Usage.CompletionTokens, nil-}diff --git a/server/model/proposal/build.go b/server/model/plan/build.gosimilarity index 58%rename from server/model/proposal/build.gorename to server/model/plan/build.goindex da7feb6d..bc2731b8 100644--- a/server/model/proposal/build.go+++ b/server/model/plan/build.go@@ -1,11 +1,12 @@-package proposal+package planimport ("context""encoding/json"- "errors""fmt"+ "log""math"+ "plandex-server/db""plandex-server/model""plandex-server/model/prompts""plandex-server/types"@@ -22,13 +23,8 @@ import (const MaxRetries = 3const MaxReplacementRetries = 1-func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {- fmt.Printf("Building plan: %s\n", params.ProposalId)-- // proposalId := params.ProposalId- // fileContents := params.FileContents- // numTokensByFile := params.NumTokensByFile- // onStream := params.OnStream+func Build(currentOrgId, planId string) *types.ActivePlan {+ log.Printf("Building plan: %s\n", planId)// goEnv := os.Getenv("GOENV")// if goEnv == "test" {@@ -36,81 +32,105 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {// return nil// }- proposalId := params.ProposalId-- proposal := proposals.Get(proposalId)- if proposal == nil {- return errors.New("proposal not found")- }+ active := Active.Get(planId)- if !proposal.IsFinished() {- return errors.New("proposal not finished")+ if active == nil {+ log.Printf("Active plan not found for plan %s\n", planId)+ // TODO: to allow for rebuilds or selective builds, construct the active plan here from the db+ } else {+ go execPlanBuild(currentOrgId, planId, active)}- buildUUID, err := uuid.NewRandom()- if err != nil {- fmt.Printf("Failed to generate build id: %v\n", err)- return err- }- buildId := buildUUID.String()+ return active+}- replyInfo := shared.NewReplyInfo()- replyInfo.AddToken(proposal.Content, true)+func execPlanBuild(currentOrgId, planId string, active *types.ActivePlan) {+ replyInfo := types.NewReplyInfo()+ replyInfo.AddToken(active.Content, true)_, fileContents, numTokensByFile, _ := replyInfo.FinishAndRead()- ctx, cancel := context.WithCancel(context.Background())-- builds.Set(proposalId, &types.Build{- BuildId: buildId,- ProposalId: proposalId,- NumFiles: len(proposal.PlanDescription.Files),- Buffers: map[string]string{},- Results: map[string]*shared.PlanResult{},- Errs: map[string]error{},- ProposalStage: types.ProposalStage{- CancelFn: &cancel,- },- })+ buildCh := make(chan *db.PlanBuild)+ updateStatusCh := make(chan bool)+ currentPlanCh := make(chan *shared.CurrentPlanState)+ errCh := make(chan error)- onFinishBuild := func() {- fmt.Println("Build finished. Starting write phase.")+ var build *db.PlanBuild+ var currentPlan *shared.CurrentPlanState- onStream(shared.STREAM_WRITE_PHASE, nil)+ go func() {+ build := db.PlanBuild{+ OrgId: currentOrgId,+ PlanId: planId,+ ConvoMessageId: active.AssistantMessageId,+ }+ err := db.StorePlanBuild(&build)- plan := builds.Get(proposalId)- ts := shared.StringTs()+ if err != nil {+ errCh <- fmt.Errorf("error storing plan build: %v", err)+ return+ }- for _, planRes := range plan.Results {- planRes.Ts = ts- bytes, err := json.Marshal(planRes)- if err != nil {- onStream("", err)- return- }+ buildCh <- &build+ }()- onStream(string(bytes), nil)+ go func() {+ err := db.SetPlanStatus(planId, shared.PlanStatusBuilding, "")+ if err != nil {+ errCh <- fmt.Errorf("error setting plan status to building: %v", err)+ return}+ updateStatusCh <- true+ }()- fmt.Println("Stream finished")- onStream(shared.STREAM_FINISHED, nil)+ go func() {+ currentPlan, err := db.GetCurrentPlanState(currentOrgId, planId, active.Contexts)+ if err != nil {+ errCh <- fmt.Errorf("error getting current plan state: %v", err)+ return+ }+ currentPlanCh <- currentPlan+ }()++ for i := 0; i < 3; i++ {+ select {+ case err := <-errCh:+ log.Printf("Error building plan %s: %v\n", planId, err)+ active.StreamDoneCh <- err+ return+ case <-updateStatusCh:+ case build = <-buildCh:+ case currentPlan = <-currentPlanCh:+ }}- onFinishBuildFile := func(filePath string, planRes *shared.PlanResult) {+ onFinishBuild := func() {+ log.Println("Build finished.")+ active.StreamCh <- shared.STREAM_FINISHED+ active.StreamDoneCh <- nil+ }++ onFinishBuildFile := func(filePath string, planRes *db.PlanFileResult) {finished := false- // fmt.Println("onFinishBuildFile: " + filePath)+ // log.Println("onFinishBuildFile: " + filePath)// spew.Dump(planRes)- builds.Update(proposalId, func(plan *types.Build) {- plan.Results[filePath] = planRes+ err := db.StorePlanResult(planRes)+ if err != nil {+ log.Printf("Error storing plan result: %v\n", err)+ active.StreamDoneCh <- err+ return+ }++ Active.Update(planId, func(ap *types.ActivePlan) {+ ap.BuiltFiles[filePath] = true- if plan.DidFinish() {- plan.Finish()+ if ap.BuildFinished() {finished = true}})- fmt.Printf("Finished building file %s\n", filePath)+ log.Printf("Finished building file %s\n", filePath)if finished {onFinishBuild()@@ -118,42 +138,45 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {}onBuildFileError := func(filePath string, err error) {- fmt.Printf("Error for file %s: %v\n", filePath, err)- builds.Update(proposalId, func(p *types.Build) {- p.Errs[filePath] = err- p.SetErr(err)- })- onStream("", err)+ log.Printf("Error for file %s: %v\n", filePath, err)++ active.StreamDoneCh <- err++ if err != nil {+ log.Printf("Error storing plan error result: %v\n", err)+ }++ build.Error = err.Error()+ build.ErrorPath = filePath++ err = db.SetBuildError(build)+ if err != nil {+ log.Printf("Error setting build error: %v\n", err)+ }}- var buildFile func(filePath string, numRetry int, numReplacementRetry int, res *shared.PlanResult)- buildFile = func(filePath string, numRetry int, numReplacementsRetry int, res *shared.PlanResult) {- fmt.Printf("Building file %s, numRetry: %d\n", filePath, numRetry)+ var buildFile func(filePath string, numRetry int, numReplacementRetry int, res *db.PlanFileResult)+ buildFile = func(filePath string, numRetry int, numReplacementsRetry int, res *db.PlanFileResult) {+ log.Printf("Building file %s, numRetry: %d\n", filePath, numRetry)// get relevant file context (if any)- var contextPart *shared.ModelContextPart- for _, part := range proposal.Request.ModelContext {- if part.FilePath == filePath {- contextPart = part- break- }- }+ contextPart := active.ContextsByPath[filePath]var currentState string- currentPlanFile, fileInCurrentPlan := proposal.Request.CurrentPlan.Files[filePath]+ currentPlanFile, fileInCurrentPlan := currentPlan.CurrentPlanFiles.Files[filePath]if fileInCurrentPlan {currentState = currentPlanFile- fmt.Printf("File %s found in current plan. Using current state.\n", filePath)- fmt.Println("Current state:")- fmt.Println(currentState)+ log.Printf("File %s found in current plan. Using current state.\n", filePath)+ log.Println("Current state:")+ log.Println(currentState)} else if contextPart != nil {currentState = contextPart.Body}if currentState == "" {- fmt.Printf("File %s not found in model context or current plan. Creating new file.\n", filePath)+ log.Printf("File %s not found in model context or current plan. Creating new file.\n", filePath)planTokenCount := &shared.PlanTokenCount{Path: filePath,@@ -161,26 +184,28 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {Finished: true,}- // fmt.Printf("%s: %s", filePath, content)+ // log.Printf("%s: %s", filePath, content)planTokenCountJson, err := json.Marshal(planTokenCount)if err != nil {onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))return}- onStream(string(planTokenCountJson), nil)+ active.StreamCh <- string(planTokenCountJson)// new file- planRes := &shared.PlanResult{- ProposalId: proposalId,- Path: filePath,- Content: fileContents[filePath],+ planRes := &db.PlanFileResult{+ OrgId: currentOrgId,+ PlanId: planId,+ PlanBuildId: build.Id,+ Path: filePath,+ Content: fileContents[filePath],}onFinishBuildFile(filePath, planRes)return}- fmt.Println("Getting file from model: " + filePath)- // fmt.Println("File context:", fileContext)+ log.Println("Getting file from model: " + filePath)+ // log.Println("File context:", fileContext)replacePrompt := prompts.GetReplacePrompt(filePath)currentStatePrompt := prompts.GetBuildCurrentStatePrompt(filePath, currentState)@@ -192,10 +217,10 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {Content: sysPrompt,}, {Role: openai.ChatMessageRoleUser,- Content: proposal.Request.Prompt,+ Content: active.Prompt,}, {Role: openai.ChatMessageRoleAssistant,- Content: proposal.Content,+ Content: active.Content,},{Role: openai.ChatMessageRoleUser,@@ -228,10 +253,10 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {})}- fmt.Println("Calling model for file: " + filePath)+ log.Println("Calling model for file: " + filePath)// for _, msg := range fileMessages {- // fmt.Printf("%s: %s\n", msg.Role, msg.Content)+ // log.Printf("%s: %s\n", msg.Role, msg.Content)// }modelReq := openai.ChatCompletionRequest{@@ -243,14 +268,14 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {// ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},}- stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)+ stream, err := model.Client.CreateChatCompletionStream(active.Ctx, modelReq)if err != nil {- fmt.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)+ log.Printf("Error creating plan file stream for path '%s': %v\n", filePath, err)if numRetry >= MaxRetries {onBuildFileError(filePath, fmt.Errorf("failed to create plan file stream for path '%s' after %d retries: %v", filePath, numRetry, err))} else {- fmt.Println("Retrying build plan for file: " + filePath)+ log.Println("Retrying build plan for file: " + filePath)buildFile(filePath, numRetry+1, numReplacementsRetry, res)if err != nil {onBuildFileError(filePath, fmt.Errorf("failed to retry build plan for file '%s': %v", filePath, err))@@ -266,8 +291,8 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)defer timer.Stop()- handleErrorRetry := func(maxRetryErr error, shouldSleep bool, isReplacementsRetry bool, res *shared.PlanResult) {- fmt.Printf("Error for file %s: %v\n", filePath, maxRetryErr)+ handleErrorRetry := func(maxRetryErr error, shouldSleep bool, isReplacementsRetry bool, res *db.PlanFileResult) {+ log.Printf("Error for file %s: %v\n", filePath, maxRetryErr)if isReplacementsRetry && numReplacementsRetry >= MaxReplacementRetries {// in this case, we just want to ignore the error and continue@@ -291,7 +316,7 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {for {select {- case <-ctx.Done():+ case <-active.Ctx.Done():// The main context was canceled (not the timer)returncase <-timer.C:@@ -313,10 +338,10 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {}timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)} else {- fmt.Printf("File %s: Error receiving stream chunk: %v\n", filePath, err)+ log.Printf("File %s: Error receiving stream chunk: %v\n", filePath, err)if err == context.Canceled {- fmt.Printf("File %s: Stream canceled\n", filePath)+ log.Printf("File %s: Stream canceled\n", filePath)return}@@ -347,14 +372,14 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {return}- fmt.Printf("File %s: Stream finished with non-function call\n", filePath)- fmt.Println("finish reason: " + choice.FinishReason)+ log.Printf("File %s: Stream finished with non-function call\n", filePath)+ log.Println("finish reason: " + choice.FinishReason)- plan := builds.Get(proposalId)- if plan.Results[filePath] == nil {- fmt.Printf("Stream finished before replacements parsed. File: %s\n", filePath)- fmt.Println("Buffer:")- fmt.Println(plan.Buffers[filePath])+ active := Active.Get(planId)+ if active.BuiltFiles[filePath] == false {+ log.Printf("Stream finished before replacements parsed. File: %s\n", filePath)+ log.Println("Buffer:")+ log.Println(active.BuildBuffers[filePath])handleErrorRetry(fmt.Errorf("stream finished before replacements parsed. File: %s", filePath),@@ -370,7 +395,7 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {delta := response.Choices[0].Deltaif delta.FunctionCall == nil {- fmt.Println("No function call in delta. File:", filePath)+ log.Println("No function call in delta. File:", filePath)spew.Dump(delta)continue} else {@@ -383,40 +408,42 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {Finished: false,}- // fmt.Printf("%s: %s", filePath, content)+ // log.Printf("%s: %s", filePath, content)planTokenCountJson, err := json.Marshal(planTokenCount)if err != nil {onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))return}- onStream(string(planTokenCountJson), nil)+ active.StreamCh <- string(planTokenCountJson)var buffer string- builds.Update(proposalId, func(p *types.Build) {- p.Buffers[filePath] += content- buffer = p.Buffers[filePath]+ Active.Update(planId, func(ap *types.ActivePlan) {+ ap.BuildBuffers[filePath] += content+ buffer = ap.BuildBuffers[filePath]})- var replacements shared.StreamedReplacements- err = json.Unmarshal([]byte(buffer), &replacements)- if err == nil && len(replacements.Replacements) > 0 {- fmt.Printf("File %s: Parsed replacements\n", filePath)+ var streamed types.StreamedReplacements+ err = json.Unmarshal([]byte(buffer), &streamed)+ if err == nil && len(streamed.Replacements) > 0 {+ log.Printf("File %s: Parsed replacements\n", filePath)- planResult, allSucceeded := getPlanResult(+ planFileResult, allSucceeded := getPlanResult(planResultParams{- proposalId: proposalId,+ orgId: currentOrgId,+ planId: planId,+ planBuildId: build.Id,filePath: filePath,currentState: currentState,- contextPart: contextPart,- replacements: replacements.Replacements,+ context: contextPart,+ replacements: streamed.Replacements,},)// proposalId, filePath, currentState, contextPart, replacements.Replacements)if !allSucceeded {- fmt.Println("Failed replacements:")- for _, replacement := range planResult.Replacements {+ log.Println("Failed replacements:")+ for _, replacement := range planFileResult.Replacements {if replacement.Failed {spew.Dump(replacement)}@@ -427,7 +454,7 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {nil, // no error -- if we reach MAX_REPLACEMENT_RETRIES, we just ignore the error and continuefalse,true,- planResult,+ planFileResult,)return}@@ -439,15 +466,15 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {Finished: true,}- // fmt.Printf("%s: %s", filePath, content)+ // log.Printf("%s: %s", filePath, content)planTokenCountJson, err := json.Marshal(planTokenCount)if err != nil {onBuildFileError(filePath, fmt.Errorf("error marshalling plan chunk: %v", err))return}- onStream(string(planTokenCountJson), nil)+ active.StreamCh <- string(planTokenCountJson)- onFinishBuildFile(filePath, planResult)+ onFinishBuildFile(filePath, planFileResult)return}}@@ -455,30 +482,30 @@ func BuildPlan(params *types.BuildParams, onStream types.OnStreamFunc) error {}()}- for _, filePath := range proposal.PlanDescription.Files {+ for _, filePath := range active.Files {go buildFile(filePath, 0, 0, nil)}-- return nil}type planResultParams struct {- proposalId string+ orgId string+ planId string+ planBuildId stringfilePath stringcurrentState string- contextPart *shared.ModelContextPart+ context *db.Contextreplacements []*shared.Replacement- fileBlock string}-func getPlanResult(params planResultParams) (*shared.PlanResult, bool) {- proposalId := params.proposalId+func getPlanResult(params planResultParams) (*db.PlanFileResult, bool) {+ orgId := params.orgId+ planId := params.planId+ planBuildId := params.planBuildIdfilePath := params.filePathcurrentState := params.currentState- contextPart := params.contextPart+ contextPart := params.contextreplacements := params.replacements-- updated := currentState+ updated := params.currentStatesort.Slice(replacements, func(i, j int) bool {iIdx := strings.Index(updated, replacements[i].Old)@@ -494,17 +521,15 @@ func getPlanResult(params planResultParams) (*shared.PlanResult, bool) {}for _, replacement := range replacements {- replacementUUID, err := uuid.NewRandom()- if err != nil {- fmt.Printf("Failed to generate replacement id: %v\n", err)- return nil, false- }- replacement.Id = replacementUUID.String()+ id := uuid.New().String()+ replacement.Id = id}- return &shared.PlanResult{- Content: params.fileBlock,- ProposalId: proposalId,+ return &db.PlanFileResult{+ OrgId: orgId,+ PlanId: planId,+ PlanBuildId: planBuildId,+ Content: "",Path: filePath,Replacements: replacements,ContextSha: contextSha,diff --git a/server/model/proposal/describe.go b/server/model/plan/describe.gosimilarity index 61%rename from server/model/proposal/describe.gorename to server/model/plan/describe.goindex eebd1f42..e2abc163 100644--- a/server/model/proposal/describe.go+++ b/server/model/plan/describe.go@@ -1,9 +1,10 @@-package proposal+package planimport ("context""encoding/json""fmt"+ "plandex-server/db""plandex-server/model""plandex-server/model/prompts"@@ -11,10 +12,9 @@ import ("github.com/sashabaranov/go-openai")-func genPlanDescriptionJson(proposalId string, ctx context.Context) (*shared.PlanDescription, error) {- proposal := proposals.Get(proposalId)+func genPlanDescription(planId string, ctx context.Context) (*db.ConvoMessageDescription, error) {- planDescResp, err := model.Client.CreateChatCompletion(+ descResp, err := model.Client.CreateChatCompletion(ctx,openai.ChatCompletionRequest{Model: model.CommitMsgModel,@@ -26,44 +26,45 @@ func genPlanDescriptionJson(proposalId string, ctx context.Context) (*shared.Pla},{Role: openai.ChatMessageRoleAssistant,- Content: proposal.Content,+ Content: Active.Get(planId).Content,},},ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},},)- var planDescStrRes string- var planDesc shared.PlanDescription+ var descStrRes string+ var desc shared.ConvoMessageDescriptionif err != nil {fmt.Printf("Error during plan description model call: %v\n", err)- planDesc = shared.PlanDescription{}- return &planDesc, err+ return nil, err}- for _, choice := range planDescResp.Choices {+ for _, choice := range descResp.Choices {if choice.FinishReason == "function_call" &&choice.Message.FunctionCall != nil &&choice.Message.FunctionCall.Name == "describePlan" {fnCall := choice.Message.FunctionCall- planDescStrRes = fnCall.Arguments+ descStrRes = fnCall.Arguments}}- if planDescStrRes == "" {+ if descStrRes == "" {fmt.Println("no describePlan function call found in response")- planDesc = shared.PlanDescription{}- return &planDesc, err+ return nil, err}- planDescByteRes := []byte(planDescStrRes)+ descByteRes := []byte(descStrRes)- err = json.Unmarshal(planDescByteRes, &planDesc)+ err = json.Unmarshal(descByteRes, &desc)if err != nil {fmt.Printf("Error unmarshalling plan description response: %v\n", err)return nil, err}- return &planDesc, nil+ return &db.ConvoMessageDescription{+ PlanId: planId,+ CommitMsg: desc.CommitMsg,+ }, nil}diff --git a/server/model/proposal/lorem_ipsum.go b/server/model/plan/lorem_ipsum.gosimilarity index 99%rename from server/model/proposal/lorem_ipsum.gorename to server/model/plan/lorem_ipsum.goindex 694ea409..c16c6cdb 100644--- a/server/model/proposal/lorem_ipsum.go+++ b/server/model/plan/lorem_ipsum.go@@ -1,4 +1,4 @@-package proposal+package plan// import (// "encoding/json"diff --git a/server/model/plan/state.go b/server/model/plan/state.gonew file mode 100644index 00000000..ae51b003--- /dev/null+++ b/server/model/plan/state.go@@ -0,0 +1,66 @@+package plan++import (+ "log"+ "plandex-server/db"+ "plandex-server/types"++ "github.com/plandex/plandex/shared"+)++var (+ Active types.SafeMap[*types.ActivePlan] = *types.NewSafeMap[*types.ActivePlan]()+)++func CreateActivePlan(planId, prompt string) *types.ActivePlan {+ activePlan := types.NewActivePlan(prompt)+ Active.Set(planId, activePlan)++ go func() {+ for {+ select {+ case <-activePlan.Ctx.Done():+ return+ case err := <-activePlan.StreamDoneCh:+ if err == nil {+ log.Printf("Plan %s stream completed successfully", planId)++ err = db.SetPlanStatus(planId, shared.PlanStatusFinished, "")+ if err != nil {+ log.Printf("Error setting plan %s status to ready: %v\n", planId, err)+ }++ } else {+ log.Printf("Error streaming plan %s: %v\n", planId, err)++ err = db.SetPlanStatus(planId, shared.PlanStatusError, err.Error())+ if err != nil {+ log.Printf("Error setting plan %s status to error: %v\n", planId, err)+ }+ }++ activePlan.CancelFn()++ Active.Delete(planId)+ return+ }+ }+ }()++ return activePlan+}++func SubscribePlan(planId string) (string, chan string) {+ var id string+ var ch chan string+ Active.Update(planId, func(activePlan *types.ActivePlan) {+ id, ch = activePlan.Subscribe()+ })+ return id, ch+}++func UnsubscribePlan(planId, subscriptionId string) {+ Active.Update(planId, func(activePlan *types.ActivePlan) {+ activePlan.Unsubscribe(subscriptionId)+ })+}diff --git a/server/model/plan/stop.go b/server/model/plan/stop.gonew file mode 100644index 00000000..2e3dd4de--- /dev/null+++ b/server/model/plan/stop.go@@ -0,0 +1,40 @@+package plan++import (+ "fmt"+ "plandex-server/db"++ "github.com/sashabaranov/go-openai"+)++func Stop(planId, currentUserId, currentOrgId string) error {++ active := Active.Get(planId)++ if active == nil {+ return fmt.Errorf("no active plan with id %s", planId)+ }++ content := active.Content++ active.CancelFn()++ userMsg := db.ConvoMessage{+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleAssistant,+ Tokens: active.NumTokens,+ Num: active.PromptMessageNum + 1,+ Stopped: true,+ Message: content,+ }++ err := db.StoreConvoMessage(&userMsg)++ if err != nil {+ return fmt.Errorf("error storing convo message: %v", err)+ }++ return nil+}diff --git a/server/model/plan/tell.go b/server/model/plan/tell.gonew file mode 100644index 00000000..524c7814--- /dev/null+++ b/server/model/plan/tell.go@@ -0,0 +1,559 @@+package plan++import (+ "errors"+ "fmt"+ "log"+ "strings"+ "time"++ "plandex-server/db"+ "plandex-server/model"+ "plandex-server/model/lib"+ "plandex-server/model/prompts"+ "plandex-server/types"++ "github.com/plandex/plandex/shared"+ "github.com/sashabaranov/go-openai"+)++// Proposal function to create a new proposal+func Tell(planId, currentUserId, currentOrgId string, req *shared.TellPlanRequest) error {+ // goEnv := os.Getenv("GOENV") // Fetch the GO_ENV environment variable++ // log.Println("GOENV: " + goEnv)+ // if goEnv == "test" {+ // streamLoremIpsum(onStream)+ // return nil+ // }++ active := Active.Get(planId)+ if active != nil {+ return fmt.Errorf("plan %s already has an active stream", planId)+ }++ active = CreateActivePlan(planId, req.Prompt)++ go execTellPlan(planId, currentUserId, currentOrgId, req, active)++ return nil+}++func execTellPlan(planId, currentUserId, currentOrgId string, req *shared.TellPlanRequest, active *types.ActivePlan) {+ err := db.SetPlanStatus(planId, shared.PlanStatusReplying, "")+ if err != nil {+ log.Printf("Error setting plan %s status to replying: %v\n", planId, err)+ active.StreamDoneCh <- fmt.Errorf("error setting plan status to replying: %v", err)+ return+ }++ modelContextCh := make(chan []*db.Context)+ convoCh := make(chan []*db.ConvoMessage)+ summariesCh := make(chan []*db.ConvoSummary)+ errCh := make(chan error)++ go func(planId string) {+ modelContext, 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)++ go func(planId, prompt string) {+ convo, 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+ }++ promptTokens, err := shared.GetNumTokens(req.Prompt)+ if err != nil {+ log.Printf("Error getting prompt num tokens: %v\n", err)+ errCh <- fmt.Errorf("error getting prompt num tokens: %v", err)+ return+ }++ userMsg := db.ConvoMessage{+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleUser,+ Tokens: promptTokens,+ Num: len(convo) + 1,+ Message: req.Prompt,+ }++ err = db.StoreConvoMessage(&userMsg)++ if err != nil {+ log.Printf("Error storing user message: %v\n", err)+ errCh <- fmt.Errorf("error storing user message: %v", err)+ return+ }++ convoCh <- convo+ }(planId, req.Prompt)++ go func(planId string) {+ summaries, 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)++ var modelContext []*db.Context+ var convo []*db.ConvoMessage+ var summaries []*db.ConvoSummary++ for i := 0; i < 3; i++ {+ select {+ case err := <-errCh:+ active.StreamDoneCh <- fmt.Errorf("error getting plan modelContext, convo, or summaries: %v", err)+ return+ case modelContext = <-modelContextCh:+ case convo = <-convoCh:+ case summaries = <-summariesCh:+ }+ }++ Active.Update(planId, func(ap *types.ActivePlan) {+ ap.Contexts = modelContext+ ap.PromptMessageNum = len(convo) + 1++ for _, context := range modelContext {+ if context.FilePath != "" {+ ap.ContextsByPath[context.FilePath] = context+ }+ }+ })++ modelContextText, modelContextTokens, err := lib.FormatModelContext(modelContext)+ if err != nil {+ err = fmt.Errorf("error formatting model modelContext: %v", err)+ log.Println(err)+ active.StreamDoneCh <- err+ return+ }++ systemMessageText := prompts.SysCreate + modelContextText+ systemMessage := openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleSystem,+ Content: systemMessageText,+ }++ messages := []openai.ChatCompletionMessage{+ systemMessage,+ }++ numPromptTokens, err := shared.GetNumTokens(req.Prompt)+ if err != nil {+ err = fmt.Errorf("error getting number of tokens in prompt: %v", err)+ log.Println(err)+ active.StreamDoneCh <- err+ return+ }++ promptTokens := prompts.PromptWrapperTokens + numPromptTokens+ totalTokens := prompts.CreateSysMsgNumTokens + modelContextTokens + promptTokens++ // print out breakdown of token usage+ log.Printf("System message tokens: %d\n", prompts.CreateSysMsgNumTokens)+ log.Printf("Context tokens: %d\n", modelContextTokens)+ log.Printf("Prompt tokens: %d\n", promptTokens)+ log.Printf("Total tokens before convo: %d\n", totalTokens)++ if totalTokens > shared.MaxTokens {+ // token limit already exceeded before adding conversation+ err := fmt.Errorf("token limit exceeded before adding conversation")+ log.Printf("Error: %v\n", err)+ active.StreamDoneCh <- err+ return+ }++ conversationTokens := 0+ tokensUpToTimestamp := make(map[time.Time]int)+ for _, convoMessage := range convo {+ conversationTokens += convoMessage.Tokens+ tokensUpToTimestamp[convoMessage.CreatedAt] = conversationTokens+ // log.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)+ }++ log.Printf("Conversation tokens: %d\n", conversationTokens)++ var summary *db.ConvoSummary+ var summarizedToMessageId string+ if (totalTokens+conversationTokens) > shared.MaxTokens ||+ conversationTokens > shared.MaxConvoTokens {+ log.Println("Token limit exceeded. Attempting to reduce via conversation summary.")++ // token limit exceeded after adding conversation+ // get summary for as much as the conversation as necessary to stay under the token limit+ for _, s := range summaries {+ tokens, ok := tokensUpToTimestamp[s.LatestConvoMessageCreatedAt]++ log.Printf("Last message id: %s\n", s.LatestConvoMessageCreatedAt)++ if !ok {+ err := fmt.Errorf("conversation summary timestamp not found in conversation")+ log.Printf("Error: %v\n", err)+ active.StreamDoneCh <- err+ return+ }++ updatedConversationTokens := (conversationTokens - tokens) + s.Tokens+ savedTokens := conversationTokens - updatedConversationTokens++ log.Printf("Conversation summary tokens: %d\n", tokens)+ log.Printf("Updated conversation tokens: %d\n", updatedConversationTokens)+ log.Printf("Saved tokens: %d\n", savedTokens)++ if updatedConversationTokens <= shared.MaxConvoTokens &&+ (totalTokens+updatedConversationTokens) <= shared.MaxTokens {+ log.Printf("Summarizing up to %s | saving %d tokens\n", s.LatestConvoMessageCreatedAt.Format(time.RFC3339), savedTokens)+ summary = s+ break+ }+ }++ if summary == nil {+ err := errors.New("couldn't get under token limit with conversation summary")+ log.Printf("Error: %v\n", err)+ active.StreamDoneCh <- err+ return+ }+ }++ if summary == nil {+ for _, convoMessage := range convo {+ messages = append(messages, openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ }+ } else {+ if (totalTokens + summary.Tokens) > shared.MaxTokens {+ err := fmt.Errorf("token limit still exceeded after summarizing conversation")+ active.StreamDoneCh <- err+ return+ }+ summarizedToMessageId = summary.LatestConvoMessageId+ messages = append(messages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: summary.Summary,+ })++ // add messages after the last message in the summary+ for _, convoMessage := range convo {+ if convoMessage.CreatedAt.After(summary.LatestConvoMessageCreatedAt) {+ messages = append(messages, openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ }+ }+ }++ promptMessage := openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleUser,+ Content: fmt.Sprintf(prompts.PromptWrapperFormatStr, req.Prompt),+ }++ messages = append(messages, promptMessage)++ // log.Println("\n\nMessages:")+ // for _, message := range messages {+ // log.Printf("%s: %s\n", message.Role, message.Content)+ // }++ replyInfo := types.NewReplyInfo()++ modelReq := openai.ChatCompletionRequest{+ Model: model.PlannerModel,+ Messages: messages,+ Stream: true,+ Temperature: 0.6,+ TopP: 0.7,+ }++ stream, err := model.Client.CreateChatCompletionStream(active.Ctx, modelReq)+ if err != nil {+ log.Printf("Error creating proposal GPT4 stream: %v\n", err)+ log.Println(err)++ errStr := err.Error()+ if strings.Contains(errStr, "status code: 400") &&+ strings.Contains(errStr, "reduce the length of the messages") {+ log.Println("Token limit exceeded")+ }++ active.StreamDoneCh <- err+ return+ }++ storeAssistantReply := func() (*db.ConvoMessage, []string, error) {+ files, _, _, replyNumTokens := replyInfo.FinishAndRead()++ assistantMsg := db.ConvoMessage{+ OrgId: currentOrgId,+ PlanId: planId,+ UserId: currentUserId,+ Role: openai.ChatMessageRoleAssistant,+ Tokens: replyNumTokens,+ Num: len(convo) + 2,+ Message: Active.Get(planId).Content,+ }++ err := db.StoreConvoMessage(&assistantMsg)++ if err != nil {+ log.Printf("Error storing assistant message: %v\n", err)+ return nil, files, err+ }++ Active.Update(planId, func(active *types.ActivePlan) {+ active.AssistantMessageId = assistantMsg.Id+ })++ return &assistantMsg, files, err+ }++ onError := func(err error, storeDesc bool, convoMessageId string) {+ log.Printf("\nStream error: %v\n", err)+ active.StreamDoneCh <- err++ if convoMessageId == "" {+ assistantMsg, _, err := storeAssistantReply()+ if err == nil {+ convoMessageId = assistantMsg.Id+ }+ }++ if storeDesc {+ err = db.StoreDescription(&db.ConvoMessageDescription{+ OrgId: currentOrgId,+ PlanId: planId,+ SummarizedToMessageId: summarizedToMessageId,+ MadePlan: false,+ ConvoMessageId: convoMessageId,+ Error: err.Error(),+ })+ if err != nil {+ log.Printf("Error storing error state description: %v\n", err)+ }+ }+ }++ go func() {+ defer stream.Close()++ // Create a timer that will trigger if no chunk is received within the specified duration+ timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ defer timer.Stop()++ for {+ select {+ case <-active.Ctx.Done():+ // The main modelContext was canceled (not the timer)+ return+ case <-timer.C:+ // Timer triggered because no new chunk was received in time+ log.Println("\nStream timeout due to inactivity")+ onError(fmt.Errorf("stream timeout due to inactivity"), true, "")+ return+ default:+ response, err := stream.Recv()++ if err == nil {+ // Successfully received a chunk, reset the timer+ if !timer.Stop() {+ <-timer.C+ }+ timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)+ }++ if err != nil {+ onError(fmt.Errorf("stream error: %v", err), true, "")+ return+ }++ if len(response.Choices) == 0 {+ 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, "")+ return+ }++ choice := response.Choices[0]++ if choice.FinishReason != "" {+ active.StreamCh <- shared.STREAM_DESCRIPTION_PHASE+ err := db.SetPlanStatus(planId, shared.PlanStatusDescribing, "")+ if err != nil {+ onError(fmt.Errorf("failed to set plan status to describing: %v", err), true, "")+ return+ }++ if len(convo) > 0 {+ // summarize in the background+ go summarizeConvo(summarizeConvoParams{+ planId: planId,+ convo: convo,+ summaries: summaries,+ promptMessage: promptMessage,+ currentOrgId: currentOrgId,+ })+ }++ assistantMsg, files, err := storeAssistantReply()++ if err != nil {+ onError(fmt.Errorf("failed to store assistant message: %v", err), true, "")+ return+ }++ var description *db.ConvoMessageDescription++ if len(files) == 0 {+ description = &db.ConvoMessageDescription{+ OrgId: currentOrgId,+ PlanId: planId,+ ConvoMessageId: assistantMsg.Id,+ SummarizedToMessageId: summarizedToMessageId,+ MadePlan: false,+ }+ } else {+ Active.Update(planId, func(ap *types.ActivePlan) {+ ap.Files = files+ })++ description, err = genPlanDescription(planId, active.Ctx)+ if err != nil {+ onError(fmt.Errorf("failed to generate plan description: %v", err), true, assistantMsg.Id)+ return+ }++ description.OrgId = currentOrgId+ description.ConvoMessageId = assistantMsg.Id+ description.SummarizedToMessageId = summarizedToMessageId+ description.MadePlan = true+ description.Files = files+ }++ err = db.StoreDescription(description)++ if err != nil {+ onError(fmt.Errorf("failed to store description: %v", err), false, "")+ return+ }++ if len(files) == 0 {+ active.StreamCh <- shared.STREAM_FINISHED+ active.StreamDoneCh <- nil+ } else {+ active.StreamCh <- shared.STREAM_BUILD_PHASE+ Build(currentOrgId, planId)+ }+ return+ }++ delta := choice.Delta+ content := delta.Content+ Active.Update(planId, func(active *types.ActivePlan) {+ active.Content += content+ active.NumTokens+++ })++ // log.Printf("%s", content)+ active.StreamCh <- content+ replyInfo.AddToken(content, true)++ }+ }+ }()+}++type summarizeConvoParams struct {+ planId string+ convo []*db.ConvoMessage+ summaries []*db.ConvoSummary+ promptMessage openai.ChatCompletionMessage+ currentOrgId string+}++func summarizeConvo(params summarizeConvoParams) error {+ planId := params.planId+ convo := params.convo+ summaries := params.summaries+ promptMessage := params.promptMessage+ currentOrgId := params.currentOrgId++ log.Println("Generating plan summary for planId:", planId)++ var summaryMessages []openai.ChatCompletionMessage+ var latestSummary *db.ConvoSummary+ var numMessagesSummarized int = 0+ var latestMessageSummarizedAt time.Time+ var latestMessageId string+ if len(summaries) > 0 {+ latestSummary = summaries[len(summaries)-1]+ numMessagesSummarized = latestSummary.NumMessages+ }++ if latestSummary == nil {+ for _, convoMessage := range convo {+ summaryMessages = append(summaryMessages, openai.ChatCompletionMessage{+ Role: convoMessage.Role,+ Content: convoMessage.Message,+ })+ latestMessageId = convoMessage.Id+ latestMessageSummarizedAt = convoMessage.CreatedAt+ }+ } else {+ summaryMessages = append(summaryMessages, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: latestSummary.Summary,+ })+ latestMessageId = latestSummary.LatestConvoMessageId+ latestMessageSummarizedAt = latestSummary.LatestConvoMessageCreatedAt+ }++ summaryMessages = append(summaryMessages, promptMessage, openai.ChatCompletionMessage{+ Role: openai.ChatMessageRoleAssistant,+ Content: Active.Get(planId).Content,+ })++ summary, err := model.PlanSummary(model.PlanSummaryParams{+ Conversation: summaryMessages,+ LatestConvoMessageId: latestMessageId,+ LatestConvoMessageCreatedAt: latestMessageSummarizedAt,+ NumMessages: numMessagesSummarized + 1,+ OrgId: currentOrgId,+ PlanId: planId,+ })++ if err != nil {+ log.Printf("Error generating plan summary for plan %s: %v\n", planId, err)+ return err+ }++ log.Println("Generated plan summary for plan", planId)++ err = db.StoreSummary(summary)++ if err != nil {+ log.Printf("Error storing plan summary for plan %s: %v\n", planId, err)+ return err+ }++ return nil+}diff --git a/server/model/proposal/abort.go b/server/model/proposal/abort.godeleted file mode 100644index 3daaa857..00000000--- a/server/model/proposal/abort.go+++ /dev/null@@ -1,27 +0,0 @@-package proposal--import "fmt"--func AbortProposal(proposalId string) error {- fmt.Println("aborting proposal", proposalId)-- proposal := proposals.Get(proposalId)- if proposal == nil {- return fmt.Errorf("proposal not found")- }-- aborted := proposal.Abort()- if aborted {- proposals.Set(proposalId, proposal)- }-- plan := builds.Get(proposalId)- if plan != nil {- aborted := plan.Abort()- if aborted {- builds.Set(proposalId, plan)- }- }-- return nil-}diff --git a/server/model/proposal/create.go b/server/model/proposal/create.godeleted file mode 100644index 272bd5ee..00000000--- a/server/model/proposal/create.go+++ /dev/null@@ -1,404 +0,0 @@-package proposal--import (- "context"- "encoding/json"- "errors"- "fmt"- "strings"- "time"-- "plandex-server/model"- "plandex-server/model/lib"- "plandex-server/model/prompts"- "plandex-server/types"-- "github.com/google/uuid"- "github.com/plandex/plandex/shared"- "github.com/sashabaranov/go-openai"-)--// Proposal function to create a new proposal-func CreateProposal(req shared.PromptRequest, onStream types.OnStreamFunc) error {- // goEnv := os.Getenv("GOENV") // Fetch the GO_ENV environment variable-- // fmt.Println("GOENV: " + goEnv)- // if goEnv == "test" {- // streamLoremIpsum(onStream)- // return nil- // }-- proposalUUID, err := uuid.NewRandom()- if err != nil {- fmt.Printf("Failed to generate proposal id: %v\n", err)- return err- }- proposalId := proposalUUID.String()-- ctx, cancel := context.WithCancel(context.Background())-- rootId := req.RootProposalId- if rootId == "" {- rootId = proposalId- }- proposal := types.Proposal{- Id: proposalId,- ParentId: req.ParentProposalId,- IsRoot: req.ParentProposalId == "",- RootId: rootId,- Request: &req,- Content: "",- ProposalStage: types.ProposalStage{- CancelFn: &cancel,- },- }-- onStream(proposalId, nil)-- contextText, contextTokens, err := lib.FormatModelContext(req.ModelContext)- if err != nil {- err = fmt.Errorf("error formatting model context: %v", err)- fmt.Println(err)- return err- }-- systemMessageText := prompts.SysCreate + contextText- systemMessage := openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleSystem,- Content: systemMessageText,- }-- messages := []openai.ChatCompletionMessage{- systemMessage,- }-- numPromptTokens, err := shared.GetNumTokens(req.Prompt)- if err != nil {- err = fmt.Errorf("error getting number of tokens in prompt: %v", err)- fmt.Println(err)- return err- }-- promptTokens := prompts.PromptWrapperTokens + numPromptTokens- totalTokens := prompts.CreateSysMsgNumTokens + contextTokens + promptTokens-- // print out breakdown of token usage- fmt.Printf("System message tokens: %d\n", prompts.CreateSysMsgNumTokens)- fmt.Printf("Context tokens: %d\n", contextTokens)- fmt.Printf("Prompt tokens: %d\n", promptTokens)- fmt.Printf("Total tokens before convo: %d\n", totalTokens)-- if totalTokens > shared.MaxTokens {- // token limit already exceeded before adding conversation- err := fmt.Errorf("token limit exceeded before adding conversation")- fmt.Printf("Error: %v\n", err)- return err- }-- conversationTokens := 0- tokensUpToTimestamp := make(map[string]int)- for _, convoMessage := range req.Conversation {- conversationTokens += convoMessage.Tokens- tokensUpToTimestamp[convoMessage.Timestamp] = conversationTokens- // fmt.Printf("Timestamp: %s | Tokens: %d | Total: %d | conversationTokens\n", convoMessage.Timestamp, convoMessage.Tokens, conversationTokens)- }-- fmt.Printf("Conversation tokens: %d\n", conversationTokens)-- var summary *shared.ConversationSummary- if (totalTokens+conversationTokens) > shared.MaxTokens ||- conversationTokens > shared.MaxConvoTokens {- fmt.Println("Token limit exceeded. Attempting to reduce via conversation summary.")-- // token limit exceeded after adding conversation- // get summary for as much as the conversation as necessary to stay under the token limit- for _, s := range req.ConversationSummaries {- tokens, ok := tokensUpToTimestamp[s.LastMessageTimestamp]-- fmt.Printf("Last message timestamp: %s\n", s.LastMessageTimestamp)-- if !ok {- err := fmt.Errorf("conversation summary timestamp not found in conversation")- fmt.Printf("Error: %v\n", err)- return err- }-- updatedConversationTokens := (conversationTokens - tokens) + s.Tokens- savedTokens := conversationTokens - updatedConversationTokens-- fmt.Printf("Conversation summary tokens: %d\n", tokens)- fmt.Printf("Updated conversation tokens: %d\n", updatedConversationTokens)- fmt.Printf("Saved tokens: %d\n", savedTokens)-- if updatedConversationTokens <= shared.MaxConvoTokens &&- (totalTokens+updatedConversationTokens) <= shared.MaxTokens {- fmt.Printf("Summarizing up to %s | saving %d tokens\n", s.LastMessageTimestamp, savedTokens)- summary = s- break- }- }-- if summary == nil {- err := errors.New("couldn't get under token limit with conversation summary")- fmt.Printf("Error: %v\n", err)- return err- }- }-- if summary == nil {- for _, convoMessage := range req.Conversation {- messages = append(messages, convoMessage.Message)- }- } else {- if (totalTokens + summary.Tokens) > shared.MaxTokens {- err := fmt.Errorf("token limit still exceeded after summarizing conversation")- return err- }-- messages = append(messages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: summary.Summary,- })-- // add messages after the last message in the summary- for _, convoMessage := range req.Conversation {- if convoMessage.Timestamp > summary.LastMessageTimestamp {- messages = append(messages, convoMessage.Message)- }- }- }-- promptMessage := openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleUser,- Content: fmt.Sprintf(prompts.PromptWrapperFormatStr, req.Prompt),- }-- messages = append(messages, promptMessage)-- // fmt.Println("\n\nMessages:")- // for _, message := range messages {- // fmt.Printf("%s: %s\n", message.Role, message.Content)- // }-- // store the proposal- proposals.Set(proposalId, &proposal)-- replyInfo := shared.NewReplyInfo()-- modelReq := openai.ChatCompletionRequest{- Model: model.PlannerModel,- Messages: messages,- Stream: true,- Temperature: 0.6,- TopP: 0.7,- }-- stream, err := model.Client.CreateChatCompletionStream(ctx, modelReq)- if err != nil {- fmt.Printf("Error creating proposal GPT4 stream: %v\n", err)- fmt.Println(err)-- errStr := err.Error()- if strings.Contains(errStr, "status code: 400") &&- strings.Contains(errStr, "reduce the length of the messages") {- fmt.Println("Token limit exceeded")- }-- proposals.Delete(proposalId)- return err- }-- onError := func(err error) {- fmt.Printf("\nStream error: %v\n", err)-- proposals.Update(proposalId, func(proposal *types.Proposal) {- proposal.SetErr(err)- })- onStream("", err)- }-- go func() {- defer stream.Close()-- // Create a timer that will trigger if no chunk is received within the specified duration- timer := time.NewTimer(model.OPENAI_STREAM_CHUNK_TIMEOUT)- defer timer.Stop()-- for {- select {- case <-ctx.Done():- // The main context was canceled (not the timer)- return- case <-timer.C:- // Timer triggered because no new chunk was received in time- fmt.Println("\nStream timeout due to inactivity")- onError(fmt.Errorf("stream timeout due to inactivity"))- return- default:- response, err := stream.Recv()-- if err == nil {- // Successfully received a chunk, reset the timer- if !timer.Stop() {- <-timer.C- }- timer.Reset(model.OPENAI_STREAM_CHUNK_TIMEOUT)- }-- if err != nil {- onError(fmt.Errorf("stream error: %v", err))- return- }-- if len(response.Choices) == 0 {- onError(fmt.Errorf("stream finished with no choices"))- return- }-- if len(response.Choices) > 1 {- onError(fmt.Errorf("stream finished with more than one choice"))- return- }-- choice := response.Choices[0]-- if choice.FinishReason != "" {- onStream(shared.STREAM_DESCRIPTION_PHASE, nil)- responseTs := shared.StringTs()-- if len(req.Conversation) > 0 {- summaryCh := make(chan *shared.ConversationSummary)- errCh := make(chan error)-- summaryProc := types.ConvoSummaryProc{- SummaryCh: summaryCh,- ErrCh: errCh,- }-- convoSummaryProcs.Set(proposal.RootId, &summaryProc)-- go func() {- fmt.Println("Generating plan summary for rootId:", proposal.RootId)-- defer func() {- close(summaryCh)- close(errCh)- }()-- var summaryMessages []openai.ChatCompletionMessage- var latestSummary *shared.ConversationSummary- var numMessagesSummarized int = 0- if len(req.ConversationSummaries) > 0 {- latestSummary = req.ConversationSummaries[len(req.ConversationSummaries)-1]- numMessagesSummarized = latestSummary.NumMessages- }-- if latestSummary == nil {- for _, convoMessage := range req.Conversation {- summaryMessages = append(summaryMessages, convoMessage.Message)- }- } else {- summaryMessages = append(summaryMessages, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: latestSummary.Summary,- })- }-- summaryMessages = append(summaryMessages, promptMessage, openai.ChatCompletionMessage{- Role: openai.ChatMessageRoleAssistant,- Content: proposal.Content,- })-- summary, err := model.PlanSummary(summaryMessages, responseTs, numMessagesSummarized+1)- if err != nil {- fmt.Printf("Error generating plan summary for root %s: %v\n", proposal.RootId, err)-- convoSummaryProcs.Update(proposal.RootId, func(proc *types.ConvoSummaryProc) {- proc.Err = err- })- errCh <- err- return- }-- fmt.Println("Generated plan summary for root", proposal.RootId)-- convoSummaries.Set(proposal.RootId, summary)- summaryCh <- summary-- }()- }-- files, _, _, _ := replyInfo.FinishAndRead()-- var planDescription *shared.PlanDescription-- if len(files) == 0 {- planDescription = &shared.PlanDescription{- Files: []string{},- MadePlan: false,- ResponseTimestamp: responseTs,- }- } else {- planDescription, err = genPlanDescriptionJson(proposalId, ctx)- if err != nil {- onError(fmt.Errorf("failed to generate plan description json: %v", err))- return- }-- planDescription.MadePlan = true- planDescription.Files = files- planDescription.ResponseTimestamp = responseTs- }-- if summary != nil {- planDescription.SummarizedToTimestamp = summary.LastMessageTimestamp- }-- bytes, err := json.Marshal(planDescription)- if err != nil {- onError(fmt.Errorf("failed to marshal plan description: %v", err))- return- }- planDescriptionJson := string(bytes)-- fmt.Println("Plan description json:")- fmt.Println(planDescriptionJson)-- proposals.Update(proposalId, func(proposal *types.Proposal) {- proposal.Finish(planDescription)- })-- onStream(planDescriptionJson, nil)-- if len(files) == 0 {- onStream(shared.STREAM_FINISHED, nil)- } else {- onStream(shared.STREAM_BUILD_PHASE, nil)- err = BuildPlan(&types.BuildParams{- ProposalId: proposalId,- BuildPaths: files,- CurrentPlan: req.CurrentPlan,- }, onStream)- if err != nil {- onError(fmt.Errorf("failed to confirm proposal: %v", err))- }- }- return- }-- delta := choice.Delta- content := delta.Content- proposals.Update(proposalId, func(proposal *types.Proposal) {- proposal.Content += content- })-- // fmt.Printf("%s", content)- onStream(content, nil)- replyInfo.AddToken(content, true)-- }- }- }()-- return nil-}diff --git a/server/model/proposal/state.go b/server/model/proposal/state.godeleted file mode 100644index 4eaa4c22..00000000--- a/server/model/proposal/state.go+++ /dev/null@@ -1,18 +0,0 @@-package proposal--import (- "plandex-server/types"-- "github.com/plandex/plandex/shared"-)--var (- proposals types.SafeMap[*types.Proposal] = *types.NewSafeMap[*types.Proposal]()- builds types.SafeMap[*types.Build] = *types.NewSafeMap[*types.Build]()- convoSummaryProcs types.SafeMap[*types.ConvoSummaryProc] = *types.NewSafeMap[*types.ConvoSummaryProc]()- convoSummaries types.SafeMap[*shared.ConversationSummary] = *types.NewSafeMap[*shared.ConversationSummary]()-)--func GetConvoSummary(rootId string) *shared.ConversationSummary {- return convoSummaries.Get(rootId)-}diff --git a/server/model/summarize.go b/server/model/summarize.goindex e7e121fb..2cac6fae 100644--- a/server/model/summarize.go+++ b/server/model/summarize.go@@ -2,66 +2,24 @@ package modelimport ("context"- "encoding/json""fmt"+ "plandex-server/db""plandex-server/model/prompts"- "strings"+ "time"- "github.com/plandex/plandex/shared""github.com/sashabaranov/go-openai")-func ShortSummary(text string) ([]byte, error) {- resp, err := Client.CreateChatCompletion(- context.Background(),- openai.ChatCompletionRequest{- Model: ShortSummaryModel,- Functions: []openai.FunctionDefinition{prompts.ShortSummaryFn},- Messages: []openai.ChatCompletionMessage{- {- Role: openai.ChatMessageRoleSystem,- Content: prompts.SysShortSummary,- },- {- Role: openai.ChatMessageRoleUser,- Content: prompts.GetShortSummaryPrompt(text),- },- },- ResponseFormat: &openai.ChatCompletionResponseFormat{Type: "json_object"},- },- )-- if err != nil {- return nil, err- }-- var byteRes []byte- for _, choice := range resp.Choices {- if choice.FinishReason == "function_call" && choice.Message.FunctionCall != nil && choice.Message.FunctionCall.Name == "summarize" {- fnCall := choice.Message.FunctionCall-- if strings.HasSuffix(fnCall.Arguments, ",\n}") { // remove trailing comma- fnCall.Arguments = strings.TrimSuffix(fnCall.Arguments, ",\n}") + "\n}"- }-- byteRes = []byte(fnCall.Arguments)- }- }-- if len(byteRes) == 0 {- return nil, fmt.Errorf("no summarize function call found in response")- }-- // validate the JSON response- var summarizeResp shared.ShortSummaryResponse- if err := json.Unmarshal(byteRes, &summarizeResp); err != nil {- return nil, err- }-- return byteRes, nil+type PlanSummaryParams struct {+ Conversation []openai.ChatCompletionMessage+ LatestConvoMessageId string+ LatestConvoMessageCreatedAt time.Time+ NumMessages int+ OrgId string+ PlanId string}-func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimestamp string, numMessages int) (*shared.ConversationSummary, error) {+func PlanSummary(params PlanSummaryParams) (*db.ConvoSummary, error) {messages := []openai.ChatCompletionMessage{{Role: openai.ChatMessageRoleSystem,@@ -69,7 +27,7 @@ func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimesta},}- messages = append(messages, conversation...)+ messages = append(messages, params.Conversation...)messages = append(messages, openai.ChatCompletionMessage{Role: openai.ChatMessageRoleUser,@@ -99,11 +57,14 @@ func PlanSummary(conversation []openai.ChatCompletionMessage, lastMessageTimestacontent := resp.Choices[0].Message.Content- return &shared.ConversationSummary{- Summary: content,- Tokens: resp.Usage.CompletionTokens,- LastMessageTimestamp: lastMessageTimestamp,- NumMessages: numMessages,+ return &db.ConvoSummary{+ OrgId: params.OrgId,+ PlanId: params.PlanId,+ Summary: content,+ Tokens: resp.Usage.CompletionTokens,+ LatestConvoMessageId: params.LatestConvoMessageId,+ LatestConvoMessageCreatedAt: params.LatestConvoMessageCreatedAt,+ NumMessages: params.NumMessages,}, nil}diff --git a/server/routes.go b/server/routes.gonew file mode 100644index 00000000..b20d9b44--- /dev/null+++ b/server/routes.go@@ -0,0 +1,53 @@+package main++import (+ "fmt"+ "net/http"+ "plandex-server/handlers"++ "github.com/gorilla/mux"+)++func InitRoutes() *mux.Router {+ r := mux.NewRouter()++ r.HandleFunc("/health", func(w http.ResponseWriter, r *http.Request) {+ fmt.Fprintf(w, "OK")+ })++ r.HandleFunc("/projects", handlers.CreateProjectHandler).Methods("POST")+ r.HandleFunc("/projects", handlers.ListProjectsHandler).Methods("GET")+ r.HandleFunc("/projects/{projectId}/set-plan", handlers.ProjectSetPlanHandler).Methods("PUT")+ r.HandleFunc("/projects/{projectId}/rename", handlers.RenameProjectHandler).Methods("PUT")+ r.HandleFunc("/projects/{projectId}/plans", handlers.ListPlansHandler).Methods("GET")+ r.HandleFunc("/projects/{projectId}/plans/archive", handlers.ListArchivedPlansHandler).Methods("GET")+ r.HandleFunc("/projects/{projectId}/plans/ps", handlers.ListPlansRunningHandler).Methods("GET")++ r.HandleFunc("/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("POST")+ r.HandleFunc("/projects/{projectId}/plans", handlers.CreatePlanHandler).Methods("DELETE")+ r.HandleFunc("/plans/{planId}", handlers.GetPlanHandler).Methods("GET")+ r.HandleFunc("/plans/{planId}", handlers.DeletePlanHandler).Methods("DELETE")++ r.HandleFunc("/plans/{planId}/tell", handlers.TellPlanHandler).Methods("POST")+ r.HandleFunc("/plans/{planId}/build", handlers.BuildPlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/connect", handlers.ConnectPlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/stop", handlers.StopPlanHandler).Methods("DELETE")++ r.HandleFunc("/plans/{planId}/current_plan", handlers.CurrentPlanHandler).Methods("GET")+ r.HandleFunc("/plans/{planId}/apply", handlers.ApplyPlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/archive", handlers.ArchivePlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/reject_all", handlers.RejectAllChangesHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/results/{resultId}/reject", handlers.RejectResultHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/results/{resultId}/replacements/{replacementId}/reject", handlers.RejectReplacementHandler).Methods("PATCH")++ r.HandleFunc("/plans/{planId}/context", handlers.ListContextHandler).Methods("GET")+ r.HandleFunc("/plans/{planId}/context", handlers.LoadContextHandler).Methods("POST")+ r.HandleFunc("/plans/{planId}/context", handlers.UpdateContextHandler).Methods("PUT")+ r.HandleFunc("/plans/{planId}/context", handlers.DeleteContextHandler).Methods("DELETE")++ r.HandleFunc("/plans/{planId}/convo", handlers.ListConvoHandler).Methods("GET")+ r.HandleFunc("/plans/{planId}/rewind", handlers.RewindPlanHandler).Methods("PATCH")+ r.HandleFunc("/plans/{planId}/logs", handlers.ListLogsHandler).Methods("GET")++ return r+}diff --git a/server/types/active_plan.go b/server/types/active_plan.gonew file mode 100644index 00000000..a8b96eb2--- /dev/null+++ b/server/types/active_plan.go@@ -0,0 +1,76 @@+package types++import (+ "context"+ "plandex-server/db"++ "github.com/google/uuid"+)++type ActivePlan struct {+ Prompt string+ StreamCh chan string+ StreamDoneCh chan error+ Ctx context.Context+ CancelFn context.CancelFunc+ Contexts []*db.Context+ ContextsByPath map[string]*db.Context+ Content string+ NumTokens int+ PromptMessageNum int+ AssistantMessageId string+ Files []string+ BuildBuffers map[string]string+ BuiltFiles map[string]bool+ subscriptions map[string]chan string+}++func NewActivePlan(prompt string) *ActivePlan {+ ctx, cancel := context.WithCancel(context.Background())++ active := ActivePlan{+ Prompt: prompt,+ StreamCh: make(chan string),+ StreamDoneCh: make(chan error),+ Ctx: ctx,+ CancelFn: cancel,+ Files: []string{},+ BuiltFiles: map[string]bool{},+ Contexts: []*db.Context{},+ ContextsByPath: map[string]*db.Context{},+ BuildBuffers: map[string]string{},+ subscriptions: map[string]chan string{},+ }++ go func() {+ for {+ select {+ case <-active.Ctx.Done():+ return+ case msg := <-active.StreamCh:+ for _, ch := range active.subscriptions {+ go func(ch chan string) {+ ch <- msg+ }(ch)+ }+ }+ }+ }()++ return &active+}++func (ap *ActivePlan) BuildFinished() bool {+ return len(ap.Files) == len(ap.BuiltFiles)+}++func (ap *ActivePlan) Subscribe() (string, chan string) {+ id := uuid.New().String()+ ch := make(chan string)+ ap.subscriptions[id] = ch+ return id, ch+}++func (ap *ActivePlan) Unsubscribe(id string) {+ delete(ap.subscriptions, id)+}diff --git a/server/types/build.go b/server/types/build.godeleted file mode 100644index 45d60c81..00000000--- a/server/types/build.go+++ /dev/null@@ -1,23 +0,0 @@-package types--import "github.com/plandex/plandex/shared"--type BuildParams struct {- ProposalId string `json:"proposalId"`- BuildPaths []string `json:"buildPaths"`- CurrentPlan *shared.CurrentPlanFiles `json:"currentPlan"`-}--type Build struct {- BuildId string- ProposalId string- NumFiles int- Buffers map[string]string- Results map[string]*shared.PlanResult- Errs map[string]error- ProposalStage-}--func (s *Build) DidFinish() bool {- return len(s.Results) == int(s.NumFiles)-}diff --git a/server/types/conversation.go b/server/types/conversation.godeleted file mode 100644index 89787bcf..00000000--- a/server/types/conversation.go+++ /dev/null@@ -1,11 +0,0 @@-package types--import (- "github.com/plandex/plandex/shared"-)--type ConvoSummaryProc struct {- Err error- SummaryCh chan *shared.ConversationSummary- ErrCh chan error-}diff --git a/server/types/model.go b/server/types/model.gonew file mode 100644index 00000000..c2f1255b--- /dev/null+++ b/server/types/model.go@@ -0,0 +1,11 @@+package types++import "github.com/plandex/plandex/shared"++type StreamedFile struct {+ Content string `json:"content"`+}++type StreamedReplacements struct {+ Replacements []*shared.Replacement `json:"replacements"`+}diff --git a/server/types/proposal.go b/server/types/proposal.godeleted file mode 100644index 98154d0f..00000000--- a/server/types/proposal.go+++ /dev/null@@ -1,34 +0,0 @@-package types--import (- "github.com/plandex/plandex/shared"-)--type Proposal struct {- Id string- ParentId string- RootId string- IsRoot bool- Request *shared.PromptRequest- Content string- ProposalStage- PlanDescription *shared.PlanDescription-}--type OnStreamFunc func(content string, err error)--func (s *Proposal) Finish(desc *shared.PlanDescription) bool {- if desc == nil {- return false- }-- stopped := s.ProposalStage.Finish()- if stopped {- s.PlanDescription = desc- }- return stopped-}--func (s *Proposal) IsFinished() bool {- return s.ProposalStage.Finished && s.PlanDescription != nil-}diff --git a/server/types/proposal_stage.go b/server/types/proposal_stage.godeleted file mode 100644index 54dc94b9..00000000--- a/server/types/proposal_stage.go+++ /dev/null@@ -1,61 +0,0 @@-package types--import "context"--type ProposalStage struct {- CancelFn *context.CancelFunc- Finished bool- Err error- Aborted bool-}--func (s *ProposalStage) Abort() bool {- if s.IsStopped() {- return false- }-- if s.CancelFn != nil {- (*s.CancelFn)()- }- s.Aborted = true- return true-}--func (s *ProposalStage) Finish() bool {- if s.IsStopped() {- return false- }-- if s.CancelFn != nil {- (*s.CancelFn)()- }- s.Finished = true- return true-}--func (s *ProposalStage) IsStopped() bool {- return s.Finished || s.Aborted || s.Err != nil-}--func (s *ProposalStage) Cancel() bool {- if s.IsStopped() {- return false- }-- if s.CancelFn == nil {- return false- }-- (*s.CancelFn)()-- return true-}--func (s *ProposalStage) SetErr(err error) bool {- if s.IsStopped() {- return false- }-- s.Err = err- return s.Cancel()-}diff --git a/shared/reply.go b/server/types/reply.gosimilarity index 99%rename from shared/reply.gorename to server/types/reply.goindex a2cac626..f04b5b7e 100644--- a/shared/reply.go+++ b/server/types/reply.go@@ -1,4 +1,4 @@-package shared+package typesimport ("strings"diff --git a/shared/reply_test.go b/server/types/reply_test.gosimilarity index 99%rename from shared/reply_test.gorename to server/types/reply_test.goindex 981348bb..4d51aea7 100644--- a/shared/reply_test.go+++ b/server/types/reply_test.go@@ -1,4 +1,4 @@-package shared+package typesimport ("fmt"diff --git a/shared/reply_test_examples/1.md b/server/types/reply_test_examples/1.mdsimilarity index 100%rename from shared/reply_test_examples/1.mdrename to server/types/reply_test_examples/1.mddiff --git a/shared/reply_test_examples/2.md b/server/types/reply_test_examples/2.mdsimilarity index 100%rename from shared/reply_test_examples/2.mdrename to server/types/reply_test_examples/2.mddiff --git a/shared/reply_test_examples/3.md b/server/types/reply_test_examples/3.mdsimilarity index 100%rename from shared/reply_test_examples/3.mdrename to server/types/reply_test_examples/3.mddiff --git a/shared/reply_test_examples/4.md b/server/types/reply_test_examples/4.mdsimilarity index 100%rename from shared/reply_test_examples/4.mdrename to server/types/reply_test_examples/4.mddiff --git a/shared/reply_test_examples/5.md b/server/types/reply_test_examples/5.mdsimilarity index 100%rename from shared/reply_test_examples/5.mdrename to server/types/reply_test_examples/5.mddiff --git a/shared/reply_test_examples/6.md b/server/types/reply_test_examples/6.mdsimilarity index 100%rename from shared/reply_test_examples/6.mdrename to server/types/reply_test_examples/6.mddiff --git a/shared/context.go b/shared/context.gonew file mode 100644index 00000000..a2c46c08--- /dev/null+++ b/shared/context.go@@ -0,0 +1,288 @@+package shared++import (+ "fmt"+ "math"+ "strconv"+ "strings"++ "github.com/olekukonko/tablewriter"+)++type ContextUpdateResult struct {+ UpdatedContexts []Context+ TokenDiffsByName map[string]int+ TokensDiff int+ TotalTokens int+ MaxExceeded bool+ NumFiles int+ NumUrls int+ NumTrees int+}++func (c *Context) TypeAndIcon() (string, string) {+ var icon string+ var t string+ switch c.ContextType {+ case ContextFileType:+ icon = "π"+ t = "file"+ case ContextURLType:+ icon = "π"+ t = "url"+ case ContextDirectoryTreeType:+ icon = "π "+ t = "tree"+ case ContextNoteType:+ icon = "βοΈ "+ t = "note"+ case ContextPipedDataType:+ icon = "βοΈ "+ t = "piped"+ }++ return t, icon+}++func TableForLoadContext(contexts []*Context) string {+ tableString := &strings.Builder{}+ table := tablewriter.NewWriter(tableString)+ table.SetHeader([]string{"Name", "Type", "πͺ"})+ table.SetAutoWrapText(false)++ for _, context := range contexts {+ t, icon := context.TypeAndIcon()+ row := []string{+ " " + icon + " " + context.Name,+ t,+ "+" + strconv.Itoa(context.NumTokens),+ }++ table.Rich(row, []tablewriter.Colors{+ {tablewriter.FgHiGreenColor, tablewriter.Bold},+ {tablewriter.FgHiGreenColor},+ {tablewriter.FgHiGreenColor},+ })+ }++ table.Render()++ return tableString.String()+}++func SummaryForLoadContext(contexts []*Context) string {+ var tokensAdded int+ var totalTokens int++ var hasNote bool+ var hasPiped bool++ var numFiles int+ var numTrees int+ var numUrls int++ for _, context := range contexts {+ tokensAdded += context.NumTokens+ totalTokens += context.NumTokens++ switch context.ContextType {+ case ContextFileType:+ numFiles+++ case ContextURLType:+ numUrls+++ case ContextDirectoryTreeType:+ numTrees+++ case ContextNoteType:+ hasNote = true+ case ContextPipedDataType:+ hasPiped = true+ }+ }++ var added []string++ if hasNote {+ added = append(added, "a note")+ }+ if hasPiped {+ added = append(added, "piped data")+ }+ if numFiles > 0 {+ label := "file"+ if numFiles > 1 {+ label = "files"+ }+ added = append(added, fmt.Sprintf("%d %s", numFiles, label))+ }+ if numTrees > 0 {+ label := "directory tree"+ if numTrees > 1 {+ label = "directory trees"+ }+ added = append(added, fmt.Sprintf("%d %s", numTrees, label))+ }+ if numUrls > 0 {+ label := "url"+ if numUrls > 1 {+ label = "urls"+ }+ added = append(added, fmt.Sprintf("%d %s", numUrls, label))+ }++ msg := "Loaded "++ if len(added) <= 2 {+ msg += strings.Join(added, " and ")+ } else {+ for i, add := range added {+ if i == len(added)-1 {+ msg += ", and " + add+ } else {+ msg += ", " + add+ }+ }+ }++ msg += fmt.Sprintf(" into context | added β %d πͺ | total β %d πͺ", tokensAdded, totalTokens)++ return msg+}++func TableForRemoveContext(contexts []*Context) string {+ tableString := &strings.Builder{}+ table := tablewriter.NewWriter(tableString)+ table.SetHeader([]string{"Name", "Type", "πͺ"})+ table.SetAutoWrapText(false)++ for _, context := range contexts {+ t, icon := context.TypeAndIcon()+ row := []string{+ " " + icon + " " + context.Name,+ t,+ "-" + strconv.Itoa(context.NumTokens),+ }++ table.Rich(row, []tablewriter.Colors{+ {tablewriter.FgHiRedColor, tablewriter.Bold},+ {tablewriter.FgHiRedColor},+ {tablewriter.FgHiRedColor},+ })+ }++ return tableString.String()+}++func SummaryForRemoveContext(contexts []*Context, previousTotalTokens int) string {+ removedTokens := 0++ for _, context := range contexts {+ removedTokens += context.NumTokens+ }++ totalTokens := previousTotalTokens - removedTokens++ suffix := ""+ if len(contexts) > 1 {+ suffix = "s"+ }++ return fmt.Sprintf("Removed %d piece%s of context | removed β %d πͺ | total β %d πͺ \n", len(contexts), suffix, removedTokens, totalTokens)+}++func SummaryForUpdateContext(updateRes *ContextUpdateResult) string {+ numFiles := updateRes.NumFiles+ numTrees := updateRes.NumTrees+ numUrls := updateRes.NumUrls+ tokensDiff := updateRes.TokensDiff+ totalTokens := updateRes.TotalTokens++ msg := "Updated"+ var toAdd []string+ if numFiles > 0 {+ postfix := "s"+ if numFiles == 1 {+ postfix = ""+ }+ toAdd = append(toAdd, fmt.Sprintf("%d file%s", numFiles, postfix))+ }+ if numTrees > 0 {+ postfix := "s"+ if numTrees == 1 {+ postfix = ""+ }+ toAdd = append(toAdd, fmt.Sprintf("%d tree%s", numTrees, postfix))+ }+ if numUrls > 0 {+ postfix := "s"+ if numUrls == 1 {+ postfix = ""+ }+ toAdd = append(toAdd, fmt.Sprintf("%d url%s", numUrls, postfix))+ }++ if len(toAdd) <= 2 {+ msg += " " + strings.Join(toAdd, " and ")+ } else {+ for i, add := range toAdd {+ if i == len(toAdd)-1 {+ msg += ", and " + add+ } else {+ msg += ", " + add+ }+ }+ }++ msg += " in context"++ action := "added"+ if tokensDiff < 0 {+ action = "removed"+ }+ absTokenDiff := int(math.Abs(float64(tokensDiff)))+ msg += fmt.Sprintf(" | %s β %d πͺ | total β %d πͺ", action, absTokenDiff, totalTokens)++ return msg+}++func TableForContextUpdate(updateRes *ContextUpdateResult) string {+ contexts := updateRes.UpdatedContexts+ tokenDiffsByName := updateRes.TokenDiffsByName++ if len(contexts) == 0 {+ return ""+ }++ tableString := &strings.Builder{}+ table := tablewriter.NewWriter(tableString)+ table.SetHeader([]string{"Name", "Type", "πͺ"})+ table.SetAutoWrapText(false)++ for _, context := range contexts {+ t, icon := context.TypeAndIcon()+ diff := tokenDiffsByName[context.Name]++ diffStr := "+" + strconv.Itoa(diff)+ tableColor := tablewriter.FgHiGreenColor++ if diff < 0 {+ diffStr = strconv.Itoa(diff)+ tableColor = tablewriter.FgHiRedColor+ }++ row := []string{+ " " + icon + " " + context.Name,+ t,+ diffStr,+ }++ table.Rich(row, []tablewriter.Colors{+ {tableColor, tablewriter.Bold},+ {tableColor},+ {tableColor},+ })+ }++ table.Render()++ return tableString.String()+}diff --git a/shared/go.mod b/shared/go.modindex 0d245924..55317c46 100644--- a/shared/go.mod+++ b/shared/go.mod@@ -8,7 +8,10 @@ require (github.com/sashabaranov/go-openai v1.15.1)+require github.com/mattn/go-runewidth v0.0.9 // indirect+require (github.com/dlclark/regexp2 v1.10.0 // indirectgithub.com/google/uuid v1.3.0 // indirect+ github.com/olekukonko/tablewriter v0.0.5)diff --git a/shared/go.sum b/shared/go.sumindex addacd10..9f354986 100644--- a/shared/go.sum+++ b/shared/go.sum@@ -6,6 +6,10 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=github.com/looplab/fsm v1.0.1 h1:OEW0ORrIx095N/6lgoGkFkotqH6s7vaFPsgjLAaF5QU=github.com/looplab/fsm v1.0.1/go.mod h1:PmD3fFvQEIsjMEfvZdrCDZ6y8VwKTwWNjlpEr6IKPO4=+github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=+github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=+github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=+github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=github.com/pkoukk/tiktoken-go v0.1.6 h1:JF0TlJzhTbrI30wCvFuiw6FzP2+/bR+FIxUdgEAcUsw=github.com/pkoukk/tiktoken-go v0.1.6/go.mod h1:9NiV+i9mJKGj1rYOT+njbv+ZwA/zJxYdewGl6qVatpg=github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=diff --git a/shared/model_context.go b/shared/model_context.godeleted file mode 100644index be995458..00000000--- a/shared/model_context.go+++ /dev/null@@ -1,34 +0,0 @@-package shared--type ModelContextPartType string--const (- ContextFileType ModelContextPartType = "file"- ContextURLType ModelContextPartType = "url"- ContextNoteType ModelContextPartType = "note"- ContextDirectoryTreeType ModelContextPartType = "directory tree"- ContextPipedDataType ModelContextPartType = "piped data"-)--type ModelContextPart struct {- Type ModelContextPartType `json:"type"`- Name string `json:"name"`- Body string `json:"body"`- Url string `json:"url"`- FilePath string `json:"filePath"`- Sha string `json:"sha"`- NumTokens int `json:"numTokens"`- AddedAt string `json:"addedAt"`- UpdatedAt string `json:"updatedAt"`-}-type ModelContext []*ModelContextPart--func (c ModelContext) ByPath() map[string]*ModelContextPart {- byPath := make(map[string]*ModelContextPart)- for _, part := range c {- if part.FilePath != "" {- byPath[part.FilePath] = part- }- }- return byPath-}diff --git a/shared/models.go b/shared/models.gonew file mode 100644index 00000000..5a3312f2--- /dev/null+++ b/shared/models.go@@ -0,0 +1,155 @@+package shared++import "time"++type Org struct {+ Id string `json:"id"`+ Name string `json:"name"`+}++type User struct {+ Id string `json:"id"`+ Name string `json:"name"`+ Email string `json:"email"`+}++type Project struct {+ Id string `json:"id"`+ Name string `json:"name"`+ LastActivePlanId string `json:"lastActivePlanId"`+}++type PlanStatus string++const (+ PlanStatusReplying PlanStatus = "replying"+ PlanStatusDescribing PlanStatus = "describing"+ PlanStatusBuilding PlanStatus = "building"+ PlanStatusFinished PlanStatus = "finished"+ PlanStatusStopped PlanStatus = "stopped"+ PlanStatusError PlanStatus = "error"+)++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"`+}++type ContextType string++const (+ ContextFileType ContextType = "file"+ ContextURLType ContextType = "url"+ ContextNoteType ContextType = "note"+ ContextDirectoryTreeType ContextType = "directory tree"+ ContextPipedDataType ContextType = "piped data"+)++type Context struct {+ Id string `json:"id"`+ CreatorId string `json:"creatorId"`+ ContextType ContextType `json:"contextType"`+ Name string `json:"name"`+ Url string `json:"url"`+ FilePath string `json:"file_path"`+ Sha string `json:"sha"`+ NumTokens int `json:"numTokens"`+ Body string `json:"body,omitempty"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`+}++type ConvoMessage struct {+ Id string `json:"id"`+ UserId string `json:"userId"`+ Role string `json:"role"`+ Tokens int `json:"tokens"`+ Num int `json:"num"`+ Message string `json:"message"`+ Stopped bool `json:"stopped"`+ CreatedAt time.Time `json:"createdAt"`+}++type ConvoSummary struct {+ Id string `json:"id"`+ LatestConvoMessageCreatedAt time.Time `json:"latestConvoMessageCreatedAt"`+ LatestConvoMessageId string `json:"lastestConvoMessageId"`+ Summary string `json:"summary"`+ Tokens int `json:"tokens"`+ NumMessages int `json:"numMessages"`+ CreatedAt time.Time `json:"createdAt"`+}++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"`+}++type PlanBuild struct {+ Id string `json:"id"`+ ConvoMessageId string `json:"convoMessageId"`+ Error string `json:"error"`+ ErrorPath string `json:"errorPath"`+ CreatedAt time.Time `json:"createdAt"`+ UpdatedAt time.Time `json:"updatedAt"`+}++type Replacement struct {+ Id string `json:"id"`+ Old string `json:"old"`+ New string `json:"new"`+ Summary string `json:"summary"`+ Failed bool `json:"failed"`+ RejectedAt *time.Time `json:"rejectedAt,omitempty"`+}++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"`+}++type CurrentPlanFiles struct {+ Files map[string]string `json:"files"`+ ContextShas map[string]string `json:"contextShas"`+}++type PlanFileResultsByPath map[string][]*PlanFileResult++type PlanResult struct {+ SortedPaths []string `json:"sortedPaths"`+ FileResultsByPath PlanFileResultsByPath `json:"fileResultsByPath"`+ ReplacementsByPath map[string][]*Replacement `json:"replacementsByPath"`+}++type CurrentPlanState struct {+ PlanResult *PlanResult+ CurrentPlanFiles *CurrentPlanFiles+ Contexts []*Context+ ContextsByPath map[string]*Context+ LatestBuildDescription *ConvoMessageDescription+}diff --git a/shared/plan_result.go b/shared/plan_result.goindex 71f19f65..221c47cc 100644--- a/shared/plan_result.go+++ b/shared/plan_result.go@@ -1,20 +1,20 @@package shared-type PlanResult struct {- ProposalId string `json:"proposalId"`- Path string `json:"path"`- ContextSha string `json:"contextSha"`- Content string `json:"content"`- Ts string `json:"ts"`- AnyFailed bool `json:"anyFailed"`- Replacements []*Replacement `json:"replacements"`- AppliedAt string `json:"appliedAt"`- RejectedAt string `json:"rejectedAt"`+import (+ "fmt"+ "strings"+ "time"+)++func (rep *Replacement) IsPending() bool {+ return !rep.Failed && rep.RejectedAt == nil}-type PlanResultsByPath map[string][]*PlanResult+func (rep *Replacement) SetRejected(t time.Time) {+ rep.RejectedAt = &t+}-func (res *PlanResult) NumPendingReplacements() int {+func (res *PlanFileResult) NumPendingReplacements() int {numPending := 0for _, rep := range res.Replacements {if rep.IsPending() {@@ -24,40 +24,40 @@ func (res *PlanResult) NumPendingReplacements() int {return numPending}-func (res *PlanResult) IsPending() bool {- return res.AppliedAt == "" && res.RejectedAt == "" && (res.Content != "" || res.NumPendingReplacements() > 0)+func (res *PlanFileResult) IsPending() bool {+ return res.AppliedAt == nil && res.RejectedAt == nil && (res.Content != "" || res.NumPendingReplacements() > 0)}-func (p PlanResultsByPath) SetApplied(ts string) {+func (p PlanFileResultsByPath) SetApplied(t time.Time) {for _, planResults := range p {for _, planResult := range planResults {if !planResult.IsPending() {continue}- planResult.AppliedAt = ts+ planResult.AppliedAt = &t}}}-func (p PlanResultsByPath) SetRejected(ts string) int {+func (p PlanFileResultsByPath) SetRejected(t time.Time) int {numRejected := 0for _, planResults := range p {for _, planResult := range planResults {if !planResult.IsPending() {continue}- planResult.RejectedAt = ts+ planResult.RejectedAt = &tnumRejected++for _, rep := range planResult.Replacements {- rep.SetRejected(ts)+ rep.SetRejected(t)}}}return numRejected}-func (p PlanResultsByPath) NumPending() int {+func (p PlanFileResultsByPath) NumPending() int {numPending := 0for _, planResults := range p {for _, planResult := range planResults {@@ -68,3 +68,135 @@ func (p PlanResultsByPath) NumPending() int {}return numPending}++func (r PlanResult) NumPendingForPath(path string) int {+ res := 0+ results := r.FileResultsByPath[path]+ for _, result := range results {+ if result.IsPending() {+ res += result.NumPendingReplacements()+ }+ }+ return res+}++func ApplyReplacements(content string, replacements []*Replacement, setFailed bool) (string, bool) {+ updated := content+ lastInsertedIdx := 0++ allSucceeded := true++ for _, replacement := range replacements {+ pre := updated[:lastInsertedIdx]+ sub := updated[lastInsertedIdx:]+ originalIdx := strings.Index(sub, replacement.Old)++ if originalIdx == -1 {+ allSucceeded = false+ if setFailed {+ replacement.Failed = true+ }++ // jsonBytes, _ := json.Marshal(replacement)+ // log.Println(string(jsonBytes))++ // log.Println("Replacement: " + replacement.Old + " -> " + replacement.New)++ } else {+ replaced := strings.Replace(sub, replacement.Old, replacement.New, 1)++ // log.Println("Replacement: " + replacement.Old + " -> " + replacement.New)+ // log.Println("Pre: " + pre)+ // log.Println("Sub: " + sub)+ // log.Println("Idx: " + fmt.Sprintf("%d", idx))+ // log.Println("Updated: " + updated)++ updated = pre + replaced+ lastInsertedIdx = lastInsertedIdx + originalIdx + len(replacement.New)+ }+ }++ return updated, allSucceeded+}++func (planState *CurrentPlanState) GetFiles() (*CurrentPlanFiles, error) {+ return planState.GetFilesBeforeReplacement("")+}++func (planState *CurrentPlanState) GetFilesBeforeReplacement(+ replacementId string,+) (*CurrentPlanFiles, error) {+ contexts := planState.Contexts+ planRes := planState.PlanResult++ files := make(map[string]string)+ shas := make(map[string]string)++ for _, contextPart := range contexts {+ if contextPart.FilePath == "" {+ continue+ }++ _, hasPath := planRes.FileResultsByPath[contextPart.FilePath]++ // fmt.Printf("hasPath: %v\n", hasPath)++ if hasPath {+ files[contextPart.FilePath] = contextPart.Body+ shas[contextPart.FilePath] = contextPart.Sha+ }+ }++ for path, planResults := range planRes.FileResultsByPath {+ updated := files[path]++ // fmt.Printf("path: %s\n", path)+ // fmt.Printf("updated: %s\n", updated)++ PlanResLoop:+ for _, planRes := range planResults {+ if !planRes.IsPending() {+ continue+ }++ if len(planRes.Replacements) == 0 {+ if updated != "" {+ return nil, fmt.Errorf("plan updates out of order: %s", path)+ }++ updated = planRes.Content+ files[path] = updated+ continue+ }++ contextSha := shas[path]++ if contextSha != "" && planRes.ContextSha != contextSha {+ return nil, fmt.Errorf("result sha doesn't match context sha: %s", path)+ }++ if len(planRes.Replacements) == 0 {+ continue+ }++ replacements := []*Replacement{}+ for _, replacement := range planRes.Replacements {+ if replacement.Id == replacementId {+ break PlanResLoop+ }+ replacements = append(replacements, replacement)+ }++ var allSucceeded bool+ updated, allSucceeded = ApplyReplacements(updated, replacements, false)++ if !allSucceeded {+ return nil, fmt.Errorf("plan replacement failed: %s", path)+ }+ }++ files[path] = updated+ }++ return &CurrentPlanFiles{Files: files, ContextShas: shas}, nil+}diff --git a/shared/replace.go b/shared/replace.godeleted file mode 100644index 00fcb5f9..00000000--- a/shared/replace.go+++ /dev/null@@ -1,61 +0,0 @@-package shared--import (- "strings"-)--type Replacement struct {- Id string `json:"id"`- Old string `json:"old"`- New string `json:"new"`- Summary string `json:"summary"`- Failed bool `json:"failed"`- RejectedAt string `json:"rejectedAt"`-}--func ApplyReplacements(content string, replacements []*Replacement, setFailed bool) (string, bool) {- updated := content- lastInsertedIdx := 0-- allSucceeded := true-- for _, replacement := range replacements {- pre := updated[:lastInsertedIdx]- sub := updated[lastInsertedIdx:]- originalIdx := strings.Index(sub, replacement.Old)-- if originalIdx == -1 {- allSucceeded = false- if setFailed {- replacement.Failed = true- }-- // jsonBytes, _ := json.Marshal(replacement)- // log.Println(string(jsonBytes))-- // log.Println("Replacement: " + replacement.Old + " -> " + replacement.New)-- } else {- replaced := strings.Replace(sub, replacement.Old, replacement.New, 1)-- // log.Println("Replacement: " + replacement.Old + " -> " + replacement.New)- // log.Println("Pre: " + pre)- // log.Println("Sub: " + sub)- // log.Println("Idx: " + fmt.Sprintf("%d", idx))- // log.Println("Updated: " + updated)-- updated = pre + replaced- lastInsertedIdx = lastInsertedIdx + originalIdx + len(replacement.New)- }- }-- return updated, allSucceeded-}--func (rep *Replacement) IsPending() bool {- return !rep.Failed && rep.RejectedAt == ""-}--func (rep *Replacement) SetRejected(ts string) {- rep.RejectedAt = ts-}diff --git a/shared/req_res.go b/shared/req_res.gonew file mode 100644index 00000000..7cdd1f5f--- /dev/null+++ b/shared/req_res.go@@ -0,0 +1,81 @@+package shared++type CreateProjectRequest struct {+ Name string `json:"name"`+}++type CreateProjectResponse struct {+ Id string `json:"id"`+}++type SetProjectPlanRequest struct {+ PlanId string `json:"planId"`+}++type RenameProjectRequest struct {+ Name string `json:"name"`+}++type CreatePlanRequest struct {+ Name string `json:"name"`+}++type CreatePlanResponse struct {+ Id string `json:"id"`+ Name string `json:"name"`+}++type TellPlanRequest struct {+ Prompt string `json:"prompt"`+ ConnectStream bool `json:"connectStream"`+}++type LoadContextParams struct {+ ContextType ContextType `json:"contextType"`+ Name string `json:"name"`+ Url string `json:"url"`+ FilePath string `json:"file_path"`+ Body string `json:"body"`+}++type LoadContextRequest []*LoadContextParams++type LoadContextResponse struct {+ TokensAdded int `json:"tokensAdded"`+ TotalTokens int `json:"totalTokens"`+ MaxTokensExceeded bool `json:"maxTokensExceeded"`+ Msg string `json:"msg"`+}++type UpdateContextParams struct {+ Body string `json:"body"`+}++type UpdateContextRequest map[string]*UpdateContextParams++type UpdateContextResponse = LoadContextResponse++type DeleteContextRequest struct {+ Ids map[string]bool `json:"ids"`+}++type DeleteContextResponse struct {+ TokensRemoved int `json:"tokensRemoved"`+ TotalTokens int `json:"totalTokens"`+ Msg string `json:"msg"`+}++type RewindPlanRequest struct {+ Sha string `json:"sha"`+}++type ListPlanChangesResponse struct {+ Results []*PlanFileResult `json:"planResults"`+ Replacements []*Replacement `json:"planReplacements"`+}++type PlanTokenCount struct {+ Path string `json:"path"`+ NumTokens int `json:"numTokens"`+ Finished bool `json:"finished"`+}diff --git a/shared/stream.go b/shared/stream.goindex ff2a2a45..7a8c603c 100644--- a/shared/stream.go+++ b/shared/stream.go@@ -47,11 +47,3 @@ func NewPlanStreamState() *fsm.FSM {return sm}--type StreamedFile struct {- Content string `json:"content"`-}--type StreamedReplacements struct {- Replacements []*Replacement `json:"replacements"`-}diff --git a/shared/types.go b/shared/types.godeleted file mode 100644index b1e9bc10..00000000--- a/shared/types.go+++ /dev/null@@ -1,68 +0,0 @@-package shared--import (- openai "github.com/sashabaranov/go-openai"-)--type CurrentPlanFiles struct {- Files map[string]string `json:"files"`- ContextShas map[string]string `json:"contextShas"`-}--type ConversationMessage struct {- Message openai.ChatCompletionMessage `json:"message"`- Tokens int `json:"tokens"`- Timestamp string `json:"timestamp"`-}--type PromptRequest struct {- Timestamp string `json:"timestamp"`- Prompt string `json:"prompt"`- ModelContext ModelContext `json:"modelContext"`- CurrentPlan *CurrentPlanFiles `json:"currentPlan"`- Conversation []*ConversationMessage `json:"conversation"`- ConversationSummaries []*ConversationSummary `json:"conversationSummaries"`- ParentProposalId string `json:"parentProposalId"`- RootProposalId string `json:"rootProposalId"`-}--type ShortSummaryRequest struct {- Text string `json:"text"`-}--type ShortSummaryResponse struct {- Summary string `json:"summary"`-}--type FileNameRequest struct {- Text string `json:"text"`-}--type FileNameResponse struct {- FileName string `json:"fileName"`-}--type BuildRequest struct {- ProposalIds []string `json:"proposalIds"`-}--type PlanTokenCount struct {- Path string `json:"path"`- NumTokens int `json:"numTokens"`- Finished bool `json:"finished"`-}--type PlanDescription struct {- MadePlan bool `json:"madePlan"`- CommitMsg string `json:"commitMsg"`- Files []string `json:"files"`- ResponseTimestamp string `json:"responseTimestamp"`- SummarizedToTimestamp string `json:"summarizedToTimestamp"`-}--type ConversationSummary struct {- Summary string- Tokens int- LastMessageTimestamp string- NumMessages int-}