Add multiple terminals per task and saved commands that start one

This commit is contained in:
Ian Gulliver
2026-10-21 08:59:03 -07:00
parent fe2a2022c3
commit d062d7ccae
15 changed files with 1418 additions and 450 deletions
+63 -7
View File
@@ -18,16 +18,78 @@ const (
terminalExited = websocket.StatusCode(4000)
)
func (s *Server) GetTaskTerminal(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskTerminalParams) {
var newTerminalSize = api.TerminalSize{Cols: 80, Rows: 24}
func (s *Server) terminalsChanged(taskID string) {
s.store.Broadcast(api.Event{Kind: "terminals.changed", TaskId: &taskID, Actor: "byre", Payload: map[string]any{}})
}
func taskTerminal(term *terminal.Terminal) api.TaskTerminal {
return api.TaskTerminal{Id: term.ID, Name: term.Name, StartedAt: term.Started().UTC()}
}
func (s *Server) ListTaskTerminals(w http.ResponseWriter, r *http.Request, taskID string) {
_, ok := s.activeTask(w, r, taskID)
if !ok {
return
}
out := []api.TaskTerminal{}
for _, term := range s.terminals.List(taskID) {
out = append(out, taskTerminal(term))
}
writeJSON(w, http.StatusOK, out)
}
func (s *Server) StartTaskTerminal(w http.ResponseWriter, r *http.Request, taskID string) {
t, ok := s.activeTask(w, r, taskID)
if !ok {
return
}
req := api.TerminalStart{}
if !readJSON(w, r, &req) {
return
}
name, command := "Shell", ""
if req.CommandId != nil {
c, err := s.store.SavedCommand(r.Context(), actor(r), *req.CommandId)
if err != nil {
writeStoreError(w, err)
return
}
name, command = c.Name, c.Command
}
term, err := s.terminals.Start(t, name, command, newTerminalSize)
if err != nil {
slog.Error("start terminal", "task", taskID, "err", err)
writeError(w, http.StatusInternalServerError, err)
return
}
writeJSON(w, http.StatusCreated, taskTerminal(term))
}
func (s *Server) CloseTaskTerminal(w http.ResponseWriter, r *http.Request, taskID, terminalID string) {
if !s.terminals.Kill(taskID, terminalID) {
writeError(w, http.StatusNotFound, fmt.Errorf("no terminal %s in task %s", terminalID, taskID))
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *Server) GetTaskTerminal(w http.ResponseWriter, r *http.Request, taskID, terminalID string, params api.GetTaskTerminalParams) {
_, ok := s.activeTask(w, r, taskID)
if !ok {
return
}
size := api.TerminalSize{Cols: params.Cols, Rows: params.Rows}
if !terminal.ValidSize(size) {
writeError(w, http.StatusBadRequest, fmt.Errorf("cols and rows must be between 1 and %d", terminal.MaxSize))
return
}
term := s.terminals.Get(taskID, terminalID)
if term == nil {
writeError(w, http.StatusNotFound, fmt.Errorf("no terminal %s in task %s", terminalID, taskID))
return
}
conn, err := websocket.Accept(w, r, nil)
if err != nil {
@@ -37,12 +99,6 @@ func (s *Server) GetTaskTerminal(w http.ResponseWriter, r *http.Request, taskID
defer conn.CloseNow()
conn.SetReadLimit(terminalReadLimit)
term, err := s.terminals.Attach(t, size)
if err != nil {
slog.Error("start terminal", "task", taskID, "err", err)
conn.Close(websocket.StatusInternalError, "couldn't start the shell; see byre's log")
return
}
term.Resize(size)
backlog, out := term.Subscribe()