|
| 1 | +package api |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/labstack/echo/v4" |
| 10 | + "github.com/opensandbox/opensandbox/internal/sandbox" |
| 11 | + "github.com/opensandbox/opensandbox/pkg/types" |
| 12 | +) |
| 13 | + |
| 14 | +func (s *Server) createAgentSession(c echo.Context) error { |
| 15 | + if s.execSessionManager == nil { |
| 16 | + return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable) |
| 17 | + } |
| 18 | + |
| 19 | + id := c.Param("id") |
| 20 | + |
| 21 | + var req types.AgentSessionCreateRequest |
| 22 | + if err := c.Bind(&req); err != nil { |
| 23 | + return c.JSON(http.StatusBadRequest, map[string]string{ |
| 24 | + "error": "invalid request body: " + err.Error(), |
| 25 | + }) |
| 26 | + } |
| 27 | + |
| 28 | + execReq := types.ExecSessionCreateRequest{ |
| 29 | + Command: "claude-agent-wrapper", |
| 30 | + } |
| 31 | + |
| 32 | + var session *sandbox.ExecSessionHandle |
| 33 | + |
| 34 | + routeOp := func(_ context.Context) error { |
| 35 | + var err error |
| 36 | + session, err = s.execSessionManager.CreateSession(id, execReq) |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + if s.router != nil { |
| 41 | + if err := s.router.Route(c.Request().Context(), id, "agentSessionCreate", routeOp); err != nil { |
| 42 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 43 | + } |
| 44 | + } else { |
| 45 | + if err := routeOp(c.Request().Context()); err != nil { |
| 46 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Send configure command if any config options provided |
| 51 | + hasConfig := req.Model != "" || req.SystemPrompt != "" || len(req.AllowedTools) > 0 || |
| 52 | + req.PermissionMode != "" || req.MaxTurns > 0 || req.Cwd != "" || len(req.McpServers) > 0 |
| 53 | + if hasConfig && session.StdinWriter != nil { |
| 54 | + configCmd := map[string]interface{}{"type": "configure"} |
| 55 | + if req.Model != "" { |
| 56 | + configCmd["model"] = req.Model |
| 57 | + } |
| 58 | + if req.SystemPrompt != "" { |
| 59 | + configCmd["systemPrompt"] = req.SystemPrompt |
| 60 | + } |
| 61 | + if len(req.AllowedTools) > 0 { |
| 62 | + configCmd["allowedTools"] = req.AllowedTools |
| 63 | + } |
| 64 | + if req.PermissionMode != "" { |
| 65 | + configCmd["permissionMode"] = req.PermissionMode |
| 66 | + } |
| 67 | + if req.MaxTurns > 0 { |
| 68 | + configCmd["maxTurns"] = req.MaxTurns |
| 69 | + } |
| 70 | + if req.Cwd != "" { |
| 71 | + configCmd["cwd"] = req.Cwd |
| 72 | + } |
| 73 | + if len(req.McpServers) > 0 { |
| 74 | + configCmd["mcpServers"] = req.McpServers |
| 75 | + } |
| 76 | + configJSON, _ := json.Marshal(configCmd) |
| 77 | + session.StdinWriter.Write(append(configJSON, '\n')) |
| 78 | + } |
| 79 | + |
| 80 | + // Send initial prompt if provided |
| 81 | + if req.Prompt != "" && session.StdinWriter != nil { |
| 82 | + promptCmd := map[string]interface{}{ |
| 83 | + "type": "prompt", |
| 84 | + "text": req.Prompt, |
| 85 | + } |
| 86 | + promptJSON, _ := json.Marshal(promptCmd) |
| 87 | + session.StdinWriter.Write(append(promptJSON, '\n')) |
| 88 | + } |
| 89 | + |
| 90 | + return c.JSON(http.StatusCreated, types.AgentSessionInfo{ |
| 91 | + SessionID: session.ID, |
| 92 | + SandboxID: id, |
| 93 | + Running: true, |
| 94 | + StartedAt: session.StartedAt.Format(time.RFC3339), |
| 95 | + }) |
| 96 | +} |
| 97 | + |
| 98 | +func (s *Server) listAgentSessions(c echo.Context) error { |
| 99 | + if s.execSessionManager == nil { |
| 100 | + return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable) |
| 101 | + } |
| 102 | + |
| 103 | + id := c.Param("id") |
| 104 | + allSessions := s.execSessionManager.ListSessions(id) |
| 105 | + |
| 106 | + var agentSessions []types.AgentSessionInfo |
| 107 | + for _, sess := range allSessions { |
| 108 | + if sess.Command == "claude-agent-wrapper" { |
| 109 | + agentSessions = append(agentSessions, types.AgentSessionInfo{ |
| 110 | + SessionID: sess.SessionID, |
| 111 | + SandboxID: sess.SandboxID, |
| 112 | + Running: sess.Running, |
| 113 | + StartedAt: sess.StartedAt, |
| 114 | + }) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if agentSessions == nil { |
| 119 | + agentSessions = []types.AgentSessionInfo{} |
| 120 | + } |
| 121 | + |
| 122 | + return c.JSON(http.StatusOK, agentSessions) |
| 123 | +} |
| 124 | + |
| 125 | +func (s *Server) sendAgentPrompt(c echo.Context) error { |
| 126 | + if s.execSessionManager == nil { |
| 127 | + return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable) |
| 128 | + } |
| 129 | + |
| 130 | + id := c.Param("id") |
| 131 | + sessionID := c.Param("sid") |
| 132 | + |
| 133 | + var req types.AgentPromptRequest |
| 134 | + if err := c.Bind(&req); err != nil { |
| 135 | + return c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid request body: " + err.Error()}) |
| 136 | + } |
| 137 | + if req.Text == "" { |
| 138 | + return c.JSON(http.StatusBadRequest, map[string]string{"error": "text is required"}) |
| 139 | + } |
| 140 | + |
| 141 | + session, err := s.execSessionManager.GetSession(sessionID) |
| 142 | + if err != nil { |
| 143 | + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) |
| 144 | + } |
| 145 | + |
| 146 | + if session.SandboxID != id { |
| 147 | + return c.JSON(http.StatusNotFound, map[string]string{"error": "session not found"}) |
| 148 | + } |
| 149 | + |
| 150 | + if session.StdinWriter == nil { |
| 151 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": "session stdin not available"}) |
| 152 | + } |
| 153 | + |
| 154 | + promptCmd := map[string]interface{}{ |
| 155 | + "type": "prompt", |
| 156 | + "text": req.Text, |
| 157 | + } |
| 158 | + promptJSON, _ := json.Marshal(promptCmd) |
| 159 | + session.StdinWriter.Write(append(promptJSON, '\n')) |
| 160 | + |
| 161 | + return c.NoContent(http.StatusNoContent) |
| 162 | +} |
| 163 | + |
| 164 | +func (s *Server) interruptAgent(c echo.Context) error { |
| 165 | + if s.execSessionManager == nil { |
| 166 | + return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable) |
| 167 | + } |
| 168 | + |
| 169 | + id := c.Param("id") |
| 170 | + sessionID := c.Param("sid") |
| 171 | + |
| 172 | + session, err := s.execSessionManager.GetSession(sessionID) |
| 173 | + if err != nil { |
| 174 | + return c.JSON(http.StatusNotFound, map[string]string{"error": err.Error()}) |
| 175 | + } |
| 176 | + |
| 177 | + if session.SandboxID != id { |
| 178 | + return c.JSON(http.StatusNotFound, map[string]string{"error": "session not found"}) |
| 179 | + } |
| 180 | + |
| 181 | + if session.StdinWriter == nil { |
| 182 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": "session stdin not available"}) |
| 183 | + } |
| 184 | + |
| 185 | + interruptCmd := map[string]interface{}{"type": "interrupt"} |
| 186 | + interruptJSON, _ := json.Marshal(interruptCmd) |
| 187 | + session.StdinWriter.Write(append(interruptJSON, '\n')) |
| 188 | + |
| 189 | + return c.NoContent(http.StatusNoContent) |
| 190 | +} |
| 191 | + |
| 192 | +func (s *Server) killAgentSession(c echo.Context) error { |
| 193 | + if s.execSessionManager == nil { |
| 194 | + return c.JSON(http.StatusServiceUnavailable, errSandboxNotAvailable) |
| 195 | + } |
| 196 | + |
| 197 | + id := c.Param("id") |
| 198 | + sessionID := c.Param("sid") |
| 199 | + |
| 200 | + var body struct { |
| 201 | + Signal int `json:"signal"` |
| 202 | + } |
| 203 | + _ = c.Bind(&body) |
| 204 | + |
| 205 | + if body.Signal == 0 { |
| 206 | + body.Signal = 9 |
| 207 | + } |
| 208 | + |
| 209 | + routeOp := func(_ context.Context) error { |
| 210 | + return s.execSessionManager.KillSession(sessionID, body.Signal) |
| 211 | + } |
| 212 | + |
| 213 | + if s.router != nil { |
| 214 | + if err := s.router.Route(c.Request().Context(), id, "agentSessionKill", routeOp); err != nil { |
| 215 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 216 | + } |
| 217 | + } else { |
| 218 | + if err := routeOp(c.Request().Context()); err != nil { |
| 219 | + return c.JSON(http.StatusInternalServerError, map[string]string{"error": err.Error()}) |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + return c.NoContent(http.StatusNoContent) |
| 224 | +} |
0 commit comments