| 1 | package serve |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "reasonix/internal/agent" |
| 7 | "reasonix/internal/control" |
| 8 | ) |
| 9 | |
| 10 | func (s *Server) cancelSession(w http.ResponseWriter, _ *http.Request) { |
| 11 | ctrl := s.ctl() |
| 12 | receipt := control.CancelReceipt{SessionRef: ctrl.SessionPath(), HeadID: agent.BranchID(ctrl.SessionPath()), Accepted: true} |
| 13 | if concrete, ok := ctrl.(*control.Controller); ok { |
| 14 | receipt = concrete.CancelSessionFrom("user_stop") |
| 15 | } else if cancellable, ok := ctrl.(interface{ CancelSession() control.CancelReceipt }); ok { |
| 16 | receipt = cancellable.CancelSession() |
| 17 | } else { |
| 18 | status := ctrl.RuntimeStatus() |
| 19 | receipt.AlreadyIdle = !status.Running && !status.PendingPrompt |
| 20 | ctrl.Cancel() |
| 21 | } |
| 22 | w.Header().Set("Content-Type", "application/json") |
| 23 | w.WriteHeader(http.StatusAccepted) |
| 24 | _ = json.NewEncoder(w).Encode(receipt) |
| 25 | } |
| 26 |