Skip to content

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

🤖 Plandex → Implement context show API and CLI command, add GetContextBody method, and fix syntax error in req_res.go.

Dane Schneiderda9d2df623a1
diff --git a/app/cli/api/methods.go b/app/cli/api/methods.go
index 9f29b6ba..1e8dc34a 100644
--- a/app/cli/api/methods.go
+++ b/app/cli/api/methods.go
@@ -2118,6 +2118,34 @@ func (a *Api) GetFileMap(req shared.GetFileMapRequest) (*shared.GetFileMapRespon
return &respBody, nil
}
+func (a *Api) GetContextBody(planId, branch, contextId string) (*shared.GetContextBodyResponse, *shared.ApiError) {
+ serverUrl := fmt.Sprintf("%s/plans/%s/%s/context/%s/body", GetApiHost(), planId, branch, contextId)
+
+ resp, err := authenticatedFastClient.Get(serverUrl)
+ if err != nil {
+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error sending request: %v", err)}
+ }
+ defer resp.Body.Close()
+
+ if resp.StatusCode >= 400 {
+ errorBody, _ := io.ReadAll(resp.Body)
+ apiErr := HandleApiError(resp, errorBody)
+ tokenRefreshed, apiErr := refreshTokenIfNeeded(apiErr)
+ if tokenRefreshed {
+ return a.GetContextBody(planId, branch, contextId)
+ }
+ return nil, apiErr
+ }
+
+ var respBody shared.GetContextBodyResponse
+ err = json.NewDecoder(resp.Body).Decode(&respBody)
+ if err != nil {
+ return nil, &shared.ApiError{Type: shared.ApiErrorTypeOther, Msg: fmt.Sprintf("error decoding response: %v", err)}
+ }
+
+ return &respBody, nil
+}
+
func (a *Api) AutoLoadContext(planId, branch string, req shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError) {
serverUrl := fmt.Sprintf("%s/plans/%s/%s/auto_load_context", GetApiHost(), planId, branch)
reqBytes, err := json.Marshal(req)
diff --git a/app/cli/cmd/context_show.go b/app/cli/cmd/context_show.go
new file mode 100644
index 00000000..795f975f
--- /dev/null
+++ b/app/cli/cmd/context_show.go
@@ -0,0 +1,35 @@
+package cmd
+
+import (
+ "fmt"
+ "log"
+
+ "github.com/spf13/cobra"
+)
+
+func init() {
+ contextCmd.AddCommand(contextShowCmd)
+}
+
+var contextShowCmd = &cobra.Command{
+ Use: "show [context-id]",
+ Short: "Show the body of a context",
+ Args: cobra.ExactArgs(1),
+ RunE: func(cmd *cobra.Command, args []string) error {
+ contextId := args[0]
+
+ planId, branch, err := getCurrentPlanAndBranch()
+ if err != nil {
+ return err
+ }
+
+ res, apiErr := api.GetContextBody(planId, branch, contextId)
+ if apiErr != nil {
+ log.Printf("Error getting context body: %v\n", apiErr)
+ return fmt.Errorf("error getting context body: %v", apiErr)
+ }
+
+ fmt.Println(res.Body)
+ return nil
+ },
+}
diff --git a/app/cli/types/api.go b/app/cli/types/api.go
index 7515ae36..5ffb46ac 100644
--- a/app/cli/types/api.go
+++ b/app/cli/types/api.go
@@ -103,5 +103,7 @@ type ApiClient interface {
GetCreditsTransactions(pageSize, pageNum int) (*shared.CreditsLogResponse, *shared.ApiError)
GetFileMap(req shared.GetFileMapRequest) (*shared.GetFileMapResponse, *shared.ApiError)
+ GetContextBody(planId, branch, contextId string) (*shared.GetContextBodyResponse, *shared.ApiError)
AutoLoadContext(planId, branch string, req shared.LoadContextRequest) (*shared.LoadContextResponse, *shared.ApiError)
}
+
diff --git a/app/server/handlers/plans_context.go b/app/server/handlers/plans_context.go
index aa1cedfd..9de9ee07 100644
--- a/app/server/handlers/plans_context.go
+++ b/app/server/handlers/plans_context.go
@@ -64,6 +64,68 @@ func ListContextHandler(w http.ResponseWriter, r *http.Request) {
w.Write(bytes)
}
+func GetContextBodyHandler(w http.ResponseWriter, r *http.Request) {
+ log.Println("Received request for GetContextBodyHandler")
+
+ auth := Authenticate(w, r, true)
+ if auth == nil {
+ return
+ }
+
+ vars := mux.Vars(r)
+ planId := vars["planId"]
+ contextId := vars["contextId"]
+ log.Println("planId:", planId, "contextId:", contextId)
+
+ if authorizePlan(w, planId, auth) == nil {
+ return
+ }
+
+ var err error
+ ctx, cancel := context.WithCancel(context.Background())
+ unlockFn := LockRepo(w, r, auth, db.LockScopeRead, ctx, cancel, true)
+ if unlockFn == nil {
+ return
+ } else {
+ defer func() {
+ (*unlockFn)(err)
+ }()
+ }
+
+ dbContexts, err := db.GetPlanContexts(auth.OrgId, planId, false, false)
+ if err != nil {
+ log.Printf("Error getting contexts: %v\n", err)
+ http.Error(w, "Error getting contexts: "+err.Error(), http.StatusInternalServerError)
+ return
+ }
+
+ var targetContext *db.Context
+ for _, dbContext := range dbContexts {
+ if dbContext.Id == contextId {
+ targetContext = dbContext
+ break
+ }
+ }
+
+ if targetContext == nil {
+ http.Error(w, "Context not found", http.StatusNotFound)
+ return
+ }
+
+ response := shared.GetContextBodyResponse{
+ Body: targetContext.Body,
+ }
+
+ bytes, err := json.Marshal(response)
+ 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)
+}
+
func LoadContextHandler(w http.ResponseWriter, r *http.Request) {
log.Println("Received request for LoadContextHandler")
diff --git a/app/shared/req_res.go b/app/shared/req_res.go
index 52458250..22211a3a 100644
--- a/app/shared/req_res.go
+++ b/app/shared/req_res.go
@@ -201,8 +201,17 @@ type GetFileMapResponse struct {
MapBodies FileMapBodies `json:"mapBodies"`
}
+type GetContextBodyRequest struct {
+ ContextId string `json:"contextId"`
+}
+
+type GetContextBodyResponse struct {
+ Body string `json:"body"`
+}
+
type UpdateContextRequest map[string]*UpdateContextParams
+
type UpdateContextResponse = LoadContextResponse
type DeleteContextRequest struct {