| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | // goal sets or clears the active goal. An empty goal string clears it. |
| 10 | // Setting a non-empty goal disables plan mode (matching the desktop behavior). |
| 11 | func (s *Server) goal(w http.ResponseWriter, r *http.Request) { |
| 12 | var body struct { |
| 13 | Goal string `json:"goal"` |
| 14 | } |
| 15 | if err := json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 16 | http.Error(w, "bad body", http.StatusBadRequest) |
| 17 | return |
| 18 | } |
| 19 | goal := strings.TrimSpace(body.Goal) |
| 20 | ctrl := s.ctl() |
| 21 | if err := ctrl.SetGoalDurable(goal); err != nil { |
| 22 | http.Error(w, "persist goal: "+err.Error(), http.StatusServiceUnavailable) |
| 23 | return |
| 24 | } |
| 25 | if goal != "" { |
| 26 | // Disable plan mode only after the goal mutation has been accepted. |
| 27 | ctrl.SetPlanMode(false) |
| 28 | } |
| 29 | w.WriteHeader(http.StatusNoContent) |
| 30 | } |
| 31 | |
| 32 | func (s *Server) goalEdit(w http.ResponseWriter, r *http.Request) { |
| 33 | var body struct { |
| 34 | Objective string `json:"objective"` |
| 35 | MaxGoalRounds *uint64 `json:"maxGoalRounds"` |
| 36 | } |
| 37 | if err := json.NewDecoder(r.Body).Decode(&body); err != nil { |
| 38 | http.Error(w, "bad body", http.StatusBadRequest) |
| 39 | return |
| 40 | } |
| 41 | if err := s.ctl().EditGoalDurable(body.Objective, body.MaxGoalRounds); err != nil { |
| 42 | http.Error(w, "edit goal: "+err.Error(), http.StatusConflict) |
| 43 | return |
| 44 | } |
| 45 | w.WriteHeader(http.StatusNoContent) |
| 46 | } |
| 47 |