| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 1 | // Package server provides HTTP server functionality for the sketch loop. |
| 2 | package server |
| 3 | |
| 4 | import ( |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 5 | "context" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 6 | "encoding/base64" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "html" |
| 10 | "io" |
| 11 | "io/fs" |
| 12 | "log/slog" |
| 13 | "net/http" |
| 14 | "net/http/pprof" |
| 15 | "os" |
| 16 | "os/exec" |
| 17 | "strconv" |
| 18 | "strings" |
| 19 | "sync" |
| 20 | "syscall" |
| 21 | "time" |
| 22 | |
| Philip Zeyliger | 176de79 | 2025-04-21 12:25:18 -0700 | [diff] [blame] | 23 | "sketch.dev/loop/server/gzhandler" |
| 24 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 25 | "github.com/creack/pty" |
| 26 | "sketch.dev/ant" |
| 27 | "sketch.dev/loop" |
| Philip Zeyliger | 2032b1c | 2025-04-23 19:40:42 -0700 | [diff] [blame] | 28 | "sketch.dev/webui" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | // terminalSession represents a terminal session with its PTY and the event channel |
| 32 | type terminalSession struct { |
| 33 | pty *os.File |
| 34 | eventsClients map[chan []byte]bool |
| 35 | lastEventClientID int |
| 36 | eventsClientsMutex sync.Mutex |
| 37 | cmd *exec.Cmd |
| 38 | } |
| 39 | |
| 40 | // TerminalMessage represents a message sent from the client for terminal resize events |
| 41 | type TerminalMessage struct { |
| 42 | Type string `json:"type"` |
| 43 | Cols uint16 `json:"cols"` |
| 44 | Rows uint16 `json:"rows"` |
| 45 | } |
| 46 | |
| 47 | // TerminalResponse represents the response for a new terminal creation |
| 48 | type TerminalResponse struct { |
| 49 | SessionID string `json:"sessionId"` |
| 50 | } |
| 51 | |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 52 | type State struct { |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 53 | MessageCount int `json:"message_count"` |
| 54 | TotalUsage *ant.CumulativeUsage `json:"total_usage,omitempty"` |
| 55 | InitialCommit string `json:"initial_commit"` |
| 56 | Title string `json:"title"` |
| Josh Bleecher Snyder | 47b1936 | 2025-04-30 01:34:14 +0000 | [diff] [blame] | 57 | BranchName string `json:"branch_name,omitempty"` |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 58 | Hostname string `json:"hostname"` // deprecated |
| 59 | WorkingDir string `json:"working_dir"` // deprecated |
| 60 | OS string `json:"os"` // deprecated |
| 61 | GitOrigin string `json:"git_origin,omitempty"` |
| 62 | OutstandingLLMCalls int `json:"outstanding_llm_calls"` |
| 63 | OutstandingToolCalls []string `json:"outstanding_tool_calls"` |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 64 | SessionID string `json:"session_id"` |
| 65 | SSHAvailable bool `json:"ssh_available"` |
| 66 | SSHError string `json:"ssh_error,omitempty"` |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 67 | InContainer bool `json:"in_container"` |
| 68 | FirstMessageIndex int `json:"first_message_index"` |
| Sean McCullough | d9d4581 | 2025-04-30 16:53:41 -0700 | [diff] [blame] | 69 | AgentState string `json:"agent_state,omitempty"` |
| 70 | OutsideHostname string `json:"outside_hostname,omitempty"` |
| 71 | InsideHostname string `json:"inside_hostname,omitempty"` |
| 72 | OutsideOS string `json:"outside_os,omitempty"` |
| 73 | InsideOS string `json:"inside_os,omitempty"` |
| 74 | OutsideWorkingDir string `json:"outside_working_dir,omitempty"` |
| 75 | InsideWorkingDir string `json:"inside_working_dir,omitempty"` |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 78 | type InitRequest struct { |
| 79 | HostAddr string `json:"host_addr"` |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 80 | OutsideHTTP string `json:"outside_http"` |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 81 | GitRemoteAddr string `json:"git_remote_addr"` |
| 82 | Commit string `json:"commit"` |
| 83 | SSHAuthorizedKeys []byte `json:"ssh_authorized_keys"` |
| 84 | SSHServerIdentity []byte `json:"ssh_server_identity"` |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 85 | SSHAvailable bool `json:"ssh_available"` |
| 86 | SSHError string `json:"ssh_error,omitempty"` |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 89 | // Server serves sketch HTTP. Server implements http.Handler. |
| 90 | type Server struct { |
| 91 | mux *http.ServeMux |
| 92 | agent loop.CodingAgent |
| 93 | hostname string |
| 94 | logFile *os.File |
| 95 | // Mutex to protect terminalSessions |
| 96 | ptyMutex sync.Mutex |
| 97 | terminalSessions map[string]*terminalSession |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 98 | sshAvailable bool |
| 99 | sshError string |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) { |
| 103 | s.mux.ServeHTTP(w, r) |
| 104 | } |
| 105 | |
| 106 | // New creates a new HTTP server. |
| 107 | func New(agent loop.CodingAgent, logFile *os.File) (*Server, error) { |
| 108 | s := &Server{ |
| 109 | mux: http.NewServeMux(), |
| 110 | agent: agent, |
| 111 | hostname: getHostname(), |
| 112 | logFile: logFile, |
| 113 | terminalSessions: make(map[string]*terminalSession), |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 114 | sshAvailable: false, |
| 115 | sshError: "", |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | webBundle, err := webui.Build() |
| 119 | if err != nil { |
| 120 | return nil, fmt.Errorf("failed to build web bundle, did you run 'go generate sketch.dev/loop/...'?: %w", err) |
| 121 | } |
| 122 | |
| 123 | s.mux.HandleFunc("/diff", func(w http.ResponseWriter, r *http.Request) { |
| 124 | // Check if a specific commit hash was requested |
| 125 | commit := r.URL.Query().Get("commit") |
| 126 | |
| 127 | // Get the diff, optionally for a specific commit |
| 128 | var diff string |
| 129 | var err error |
| 130 | if commit != "" { |
| 131 | // Validate the commit hash format |
| 132 | if !isValidGitSHA(commit) { |
| 133 | http.Error(w, fmt.Sprintf("Invalid git commit SHA format: %s", commit), http.StatusBadRequest) |
| 134 | return |
| 135 | } |
| 136 | |
| 137 | diff, err = agent.Diff(&commit) |
| 138 | } else { |
| 139 | diff, err = agent.Diff(nil) |
| 140 | } |
| 141 | |
| 142 | if err != nil { |
| 143 | http.Error(w, fmt.Sprintf("Error generating diff: %v", err), http.StatusInternalServerError) |
| 144 | return |
| 145 | } |
| 146 | |
| 147 | w.Header().Set("Content-Type", "text/plain") |
| 148 | w.Write([]byte(diff)) |
| 149 | }) |
| 150 | |
| 151 | // Handler for initialization called by host sketch binary when inside docker. |
| 152 | s.mux.HandleFunc("/init", func(w http.ResponseWriter, r *http.Request) { |
| 153 | defer func() { |
| 154 | if err := recover(); err != nil { |
| 155 | slog.ErrorContext(r.Context(), "/init panic", slog.Any("recovered_err", err)) |
| 156 | |
| 157 | // Return an error response to the client |
| 158 | http.Error(w, fmt.Sprintf("panic: %v\n", err), http.StatusInternalServerError) |
| 159 | } |
| 160 | }() |
| 161 | |
| 162 | if r.Method != "POST" { |
| 163 | http.Error(w, "POST required", http.StatusBadRequest) |
| 164 | return |
| 165 | } |
| 166 | |
| 167 | body, err := io.ReadAll(r.Body) |
| 168 | r.Body.Close() |
| 169 | if err != nil { |
| 170 | http.Error(w, "failed to read request body: "+err.Error(), http.StatusBadRequest) |
| 171 | return |
| 172 | } |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 173 | |
| 174 | m := &InitRequest{} |
| 175 | if err := json.Unmarshal(body, m); err != nil { |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 176 | http.Error(w, "bad request body: "+err.Error(), http.StatusBadRequest) |
| 177 | return |
| 178 | } |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 179 | |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 180 | // Store SSH availability info |
| 181 | s.sshAvailable = m.SSHAvailable |
| 182 | s.sshError = m.SSHError |
| 183 | |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 184 | // Start the SSH server if the init request included ssh keys. |
| 185 | if len(m.SSHAuthorizedKeys) > 0 && len(m.SSHServerIdentity) > 0 { |
| 186 | go func() { |
| 187 | ctx := context.Background() |
| 188 | if err := s.ServeSSH(ctx, m.SSHServerIdentity, m.SSHAuthorizedKeys); err != nil { |
| 189 | slog.ErrorContext(r.Context(), "/init ServeSSH", slog.String("err", err.Error())) |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 190 | // Update SSH error if server fails to start |
| 191 | s.sshAvailable = false |
| 192 | s.sshError = err.Error() |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 193 | } |
| 194 | }() |
| 195 | } |
| 196 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 197 | ini := loop.AgentInit{ |
| 198 | WorkingDir: "/app", |
| 199 | InDocker: true, |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 200 | Commit: m.Commit, |
| Josh Bleecher Snyder | 3e2111b | 2025-04-30 17:53:28 +0000 | [diff] [blame] | 201 | OutsideHTTP: m.OutsideHTTP, |
| Sean McCullough | baa2b59 | 2025-04-23 10:40:08 -0700 | [diff] [blame] | 202 | GitRemoteAddr: m.GitRemoteAddr, |
| 203 | HostAddr: m.HostAddr, |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 204 | } |
| 205 | if err := agent.Init(ini); err != nil { |
| 206 | http.Error(w, "init failed: "+err.Error(), http.StatusInternalServerError) |
| 207 | return |
| 208 | } |
| 209 | w.Header().Set("Content-Type", "application/json") |
| 210 | io.WriteString(w, "{}\n") |
| 211 | }) |
| 212 | |
| 213 | // Handler for /messages?start=N&end=M (start/end are optional) |
| 214 | s.mux.HandleFunc("/messages", func(w http.ResponseWriter, r *http.Request) { |
| 215 | w.Header().Set("Content-Type", "application/json") |
| 216 | |
| 217 | // Extract query parameters for range |
| 218 | var start, end int |
| 219 | var err error |
| 220 | |
| 221 | currentCount := agent.MessageCount() |
| 222 | |
| 223 | startParam := r.URL.Query().Get("start") |
| 224 | if startParam != "" { |
| 225 | start, err = strconv.Atoi(startParam) |
| 226 | if err != nil { |
| 227 | http.Error(w, "Invalid 'start' parameter", http.StatusBadRequest) |
| 228 | return |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | endParam := r.URL.Query().Get("end") |
| 233 | if endParam != "" { |
| 234 | end, err = strconv.Atoi(endParam) |
| 235 | if err != nil { |
| 236 | http.Error(w, "Invalid 'end' parameter", http.StatusBadRequest) |
| 237 | return |
| 238 | } |
| 239 | } else { |
| 240 | end = currentCount |
| 241 | } |
| 242 | |
| 243 | if start < 0 || start > end || end > currentCount { |
| 244 | http.Error(w, fmt.Sprintf("Invalid range: start %d end %d currentCount %d", start, end, currentCount), http.StatusBadRequest) |
| 245 | return |
| 246 | } |
| 247 | |
| 248 | start = max(0, start) |
| 249 | end = min(agent.MessageCount(), end) |
| 250 | messages := agent.Messages(start, end) |
| 251 | |
| 252 | // Create a JSON encoder with indentation for pretty-printing |
| 253 | encoder := json.NewEncoder(w) |
| 254 | encoder.SetIndent("", " ") // Two spaces for each indentation level |
| 255 | |
| 256 | err = encoder.Encode(messages) |
| 257 | if err != nil { |
| 258 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 259 | } |
| 260 | }) |
| 261 | |
| 262 | // Handler for /logs - displays the contents of the log file |
| 263 | s.mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) { |
| 264 | if s.logFile == nil { |
| 265 | http.Error(w, "log file not set", http.StatusNotFound) |
| 266 | return |
| 267 | } |
| 268 | logContents, err := os.ReadFile(s.logFile.Name()) |
| 269 | if err != nil { |
| 270 | http.Error(w, "error reading log file: "+err.Error(), http.StatusInternalServerError) |
| 271 | return |
| 272 | } |
| 273 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| 274 | fmt.Fprintf(w, "<!DOCTYPE html>\n<html>\n<head>\n<title>Sketchy Log File</title>\n</head>\n<body>\n") |
| 275 | fmt.Fprintf(w, "<pre>%s</pre>\n", html.EscapeString(string(logContents))) |
| 276 | fmt.Fprintf(w, "</body>\n</html>") |
| 277 | }) |
| 278 | |
| 279 | // Handler for /download - downloads both messages and status as a JSON file |
| 280 | s.mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) { |
| 281 | // Set headers for file download |
| 282 | w.Header().Set("Content-Type", "application/octet-stream") |
| 283 | |
| 284 | // Generate filename with format: sketch-YYYYMMDD-HHMMSS.json |
| 285 | timestamp := time.Now().Format("20060102-150405") |
| 286 | filename := fmt.Sprintf("sketch-%s.json", timestamp) |
| 287 | |
| 288 | w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename)) |
| 289 | |
| 290 | // Get all messages |
| 291 | messageCount := agent.MessageCount() |
| 292 | messages := agent.Messages(0, messageCount) |
| 293 | |
| 294 | // Get status information (usage and other metadata) |
| 295 | totalUsage := agent.TotalUsage() |
| 296 | hostname := getHostname() |
| 297 | workingDir := getWorkingDir() |
| 298 | |
| 299 | // Create a combined structure with all information |
| 300 | downloadData := struct { |
| 301 | Messages []loop.AgentMessage `json:"messages"` |
| 302 | MessageCount int `json:"message_count"` |
| 303 | TotalUsage ant.CumulativeUsage `json:"total_usage"` |
| 304 | Hostname string `json:"hostname"` |
| 305 | WorkingDir string `json:"working_dir"` |
| 306 | DownloadTime string `json:"download_time"` |
| 307 | }{ |
| 308 | Messages: messages, |
| 309 | MessageCount: messageCount, |
| 310 | TotalUsage: totalUsage, |
| 311 | Hostname: hostname, |
| 312 | WorkingDir: workingDir, |
| 313 | DownloadTime: time.Now().Format(time.RFC3339), |
| 314 | } |
| 315 | |
| 316 | // Marshal the JSON with indentation for better readability |
| 317 | jsonData, err := json.MarshalIndent(downloadData, "", " ") |
| 318 | if err != nil { |
| 319 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 320 | return |
| 321 | } |
| 322 | w.Write(jsonData) |
| 323 | }) |
| 324 | |
| 325 | // The latter doesn't return until the number of messages has changed (from seen |
| 326 | // or from when this was called.) |
| 327 | s.mux.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) { |
| 328 | pollParam := r.URL.Query().Get("poll") |
| 329 | seenParam := r.URL.Query().Get("seen") |
| 330 | |
| 331 | // Get the client's current message count (if provided) |
| 332 | clientMessageCount := -1 |
| 333 | var err error |
| 334 | if seenParam != "" { |
| 335 | clientMessageCount, err = strconv.Atoi(seenParam) |
| 336 | if err != nil { |
| 337 | http.Error(w, "Invalid 'seen' parameter", http.StatusBadRequest) |
| 338 | return |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | serverMessageCount := agent.MessageCount() |
| 343 | |
| 344 | // Let lazy clients not have to specify this. |
| 345 | if clientMessageCount == -1 { |
| 346 | clientMessageCount = serverMessageCount |
| 347 | } |
| 348 | |
| 349 | if pollParam == "true" { |
| 350 | ch := make(chan string) |
| 351 | go func() { |
| 352 | // This is your blocking operation |
| 353 | agent.WaitForMessageCount(r.Context(), clientMessageCount) |
| 354 | close(ch) |
| 355 | }() |
| 356 | select { |
| 357 | case <-r.Context().Done(): |
| 358 | slog.DebugContext(r.Context(), "abandoned poll request") |
| 359 | return |
| 360 | case <-time.After(90 * time.Second): |
| 361 | // Let the user call /state again to get the latest to limit how long our long polls hang out. |
| 362 | slog.DebugContext(r.Context(), "longish poll request") |
| 363 | break |
| 364 | case <-ch: |
| 365 | break |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | serverMessageCount = agent.MessageCount() |
| 370 | totalUsage := agent.TotalUsage() |
| 371 | |
| 372 | w.Header().Set("Content-Type", "application/json") |
| 373 | |
| Sean McCullough | d9f1337 | 2025-04-21 15:08:49 -0700 | [diff] [blame] | 374 | state := State{ |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 375 | MessageCount: serverMessageCount, |
| 376 | TotalUsage: &totalUsage, |
| 377 | Hostname: s.hostname, |
| 378 | WorkingDir: getWorkingDir(), |
| 379 | InitialCommit: agent.InitialCommit(), |
| 380 | Title: agent.Title(), |
| Josh Bleecher Snyder | 47b1936 | 2025-04-30 01:34:14 +0000 | [diff] [blame] | 381 | BranchName: agent.BranchName(), |
| Philip Zeyliger | 99a9a02 | 2025-04-27 15:15:25 +0000 | [diff] [blame] | 382 | OS: agent.OS(), |
| 383 | OutsideHostname: agent.OutsideHostname(), |
| 384 | InsideHostname: s.hostname, |
| 385 | OutsideOS: agent.OutsideOS(), |
| 386 | InsideOS: agent.OS(), |
| 387 | OutsideWorkingDir: agent.OutsideWorkingDir(), |
| 388 | InsideWorkingDir: getWorkingDir(), |
| 389 | GitOrigin: agent.GitOrigin(), |
| 390 | OutstandingLLMCalls: agent.OutstandingLLMCallCount(), |
| 391 | OutstandingToolCalls: agent.OutstandingToolCalls(), |
| Philip Zeyliger | c72fff5 | 2025-04-29 20:17:54 +0000 | [diff] [blame] | 392 | SessionID: agent.SessionID(), |
| 393 | SSHAvailable: s.sshAvailable, |
| 394 | SSHError: s.sshError, |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 395 | InContainer: agent.IsInContainer(), |
| 396 | FirstMessageIndex: agent.FirstMessageIndex(), |
| Sean McCullough | d9d4581 | 2025-04-30 16:53:41 -0700 | [diff] [blame] | 397 | AgentState: agent.CurrentStateName(), |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | // Create a JSON encoder with indentation for pretty-printing |
| 401 | encoder := json.NewEncoder(w) |
| 402 | encoder.SetIndent("", " ") // Two spaces for each indentation level |
| 403 | |
| 404 | err = encoder.Encode(state) |
| 405 | if err != nil { |
| 406 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 407 | } |
| 408 | }) |
| 409 | |
| Philip Zeyliger | 176de79 | 2025-04-21 12:25:18 -0700 | [diff] [blame] | 410 | s.mux.Handle("/static/", http.StripPrefix("/static/", gzhandler.New(webBundle))) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 411 | |
| 412 | // Terminal WebSocket handler |
| 413 | // Terminal endpoints - predefined terminals 1-9 |
| 414 | // TODO: The UI doesn't actually know how to use terminals 2-9! |
| 415 | s.mux.HandleFunc("/terminal/events/", func(w http.ResponseWriter, r *http.Request) { |
| 416 | if r.Method != http.MethodGet { |
| 417 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 418 | return |
| 419 | } |
| 420 | pathParts := strings.Split(r.URL.Path, "/") |
| 421 | if len(pathParts) < 4 { |
| 422 | http.Error(w, "Invalid terminal ID", http.StatusBadRequest) |
| 423 | return |
| 424 | } |
| 425 | |
| 426 | sessionID := pathParts[3] |
| 427 | // Validate that the terminal ID is between 1-9 |
| 428 | if len(sessionID) != 1 || sessionID[0] < '1' || sessionID[0] > '9' { |
| 429 | http.Error(w, "Terminal ID must be between 1 and 9", http.StatusBadRequest) |
| 430 | return |
| 431 | } |
| 432 | |
| 433 | s.handleTerminalEvents(w, r, sessionID) |
| 434 | }) |
| 435 | |
| 436 | s.mux.HandleFunc("/terminal/input/", func(w http.ResponseWriter, r *http.Request) { |
| 437 | if r.Method != http.MethodPost { |
| 438 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 439 | return |
| 440 | } |
| 441 | pathParts := strings.Split(r.URL.Path, "/") |
| 442 | if len(pathParts) < 4 { |
| 443 | http.Error(w, "Invalid terminal ID", http.StatusBadRequest) |
| 444 | return |
| 445 | } |
| 446 | sessionID := pathParts[3] |
| 447 | s.handleTerminalInput(w, r, sessionID) |
| 448 | }) |
| 449 | |
| 450 | s.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { |
| Sean McCullough | 86b5686 | 2025-04-18 13:04:03 -0700 | [diff] [blame] | 451 | // Serve the sketch-app-shell.html file directly from the embedded filesystem |
| 452 | data, err := fs.ReadFile(webBundle, "sketch-app-shell.html") |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 453 | if err != nil { |
| 454 | http.Error(w, "File not found", http.StatusNotFound) |
| 455 | return |
| 456 | } |
| 457 | w.Header().Set("Content-Type", "text/html") |
| 458 | w.Write(data) |
| 459 | }) |
| 460 | |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 461 | // Handler for POST /restart - restarts the conversation |
| 462 | s.mux.HandleFunc("/restart", func(w http.ResponseWriter, r *http.Request) { |
| 463 | if r.Method != http.MethodPost { |
| 464 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 465 | return |
| 466 | } |
| 467 | |
| 468 | // Parse the request body |
| 469 | var requestBody struct { |
| 470 | Revision string `json:"revision"` |
| 471 | InitialPrompt string `json:"initial_prompt"` |
| 472 | } |
| 473 | |
| 474 | decoder := json.NewDecoder(r.Body) |
| 475 | if err := decoder.Decode(&requestBody); err != nil { |
| 476 | http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest) |
| 477 | return |
| 478 | } |
| 479 | defer r.Body.Close() |
| 480 | |
| 481 | // Call the restart method |
| 482 | err := agent.RestartConversation(r.Context(), requestBody.Revision, requestBody.InitialPrompt) |
| 483 | if err != nil { |
| 484 | http.Error(w, "Failed to restart conversation: "+err.Error(), http.StatusInternalServerError) |
| 485 | return |
| 486 | } |
| 487 | |
| 488 | // Return success response |
| 489 | w.Header().Set("Content-Type", "application/json") |
| 490 | json.NewEncoder(w).Encode(map[string]string{"status": "restarted"}) |
| 491 | }) |
| 492 | |
| 493 | // Handler for /suggest-reprompt - suggests a reprompt based on conversation history |
| 494 | // Handler for /commit-description - returns the description of a git commit |
| 495 | s.mux.HandleFunc("/commit-description", func(w http.ResponseWriter, r *http.Request) { |
| 496 | if r.Method != http.MethodGet { |
| 497 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 498 | return |
| 499 | } |
| 500 | |
| 501 | // Get the revision parameter |
| 502 | revision := r.URL.Query().Get("revision") |
| 503 | if revision == "" { |
| 504 | http.Error(w, "Missing revision parameter", http.StatusBadRequest) |
| 505 | return |
| 506 | } |
| 507 | |
| 508 | // Run git command to get commit description |
| 509 | cmd := exec.Command("git", "log", "--oneline", "--decorate", "-n", "1", revision) |
| 510 | // Use the working directory from the agent |
| 511 | cmd.Dir = s.agent.WorkingDir() |
| 512 | |
| 513 | output, err := cmd.CombinedOutput() |
| 514 | if err != nil { |
| 515 | http.Error(w, "Failed to get commit description: "+err.Error(), http.StatusInternalServerError) |
| 516 | return |
| 517 | } |
| 518 | |
| 519 | // Prepare the response |
| 520 | resp := map[string]string{ |
| 521 | "description": strings.TrimSpace(string(output)), |
| 522 | } |
| 523 | |
| 524 | w.Header().Set("Content-Type", "application/json") |
| 525 | if err := json.NewEncoder(w).Encode(resp); err != nil { |
| 526 | slog.ErrorContext(r.Context(), "Error encoding commit description response", slog.Any("err", err)) |
| 527 | } |
| 528 | }) |
| 529 | |
| 530 | // Handler for /suggest-reprompt - suggests a reprompt based on conversation history |
| 531 | s.mux.HandleFunc("/suggest-reprompt", func(w http.ResponseWriter, r *http.Request) { |
| 532 | if r.Method != http.MethodGet { |
| 533 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 534 | return |
| 535 | } |
| 536 | |
| 537 | // Call the suggest reprompt method |
| 538 | suggestedPrompt, err := agent.SuggestReprompt(r.Context()) |
| 539 | if err != nil { |
| 540 | http.Error(w, "Failed to suggest reprompt: "+err.Error(), http.StatusInternalServerError) |
| 541 | return |
| 542 | } |
| 543 | |
| 544 | // Return success response |
| 545 | w.Header().Set("Content-Type", "application/json") |
| 546 | json.NewEncoder(w).Encode(map[string]string{"prompt": suggestedPrompt}) |
| 547 | }) |
| 548 | |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 549 | // Handler for POST /chat |
| 550 | s.mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) { |
| 551 | if r.Method != http.MethodPost { |
| 552 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 553 | return |
| 554 | } |
| 555 | |
| 556 | // Parse the request body |
| 557 | var requestBody struct { |
| 558 | Message string `json:"message"` |
| 559 | } |
| 560 | |
| 561 | decoder := json.NewDecoder(r.Body) |
| 562 | if err := decoder.Decode(&requestBody); err != nil { |
| 563 | http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest) |
| 564 | return |
| 565 | } |
| 566 | defer r.Body.Close() |
| 567 | |
| 568 | if requestBody.Message == "" { |
| 569 | http.Error(w, "Message cannot be empty", http.StatusBadRequest) |
| 570 | return |
| 571 | } |
| 572 | |
| 573 | agent.UserMessage(r.Context(), requestBody.Message) |
| 574 | |
| 575 | w.WriteHeader(http.StatusOK) |
| 576 | }) |
| 577 | |
| 578 | // Handler for /cancel - cancels the current inner loop in progress |
| 579 | s.mux.HandleFunc("/cancel", func(w http.ResponseWriter, r *http.Request) { |
| 580 | if r.Method != http.MethodPost { |
| 581 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 582 | return |
| 583 | } |
| 584 | |
| 585 | // Parse the request body (optional) |
| 586 | var requestBody struct { |
| 587 | Reason string `json:"reason"` |
| 588 | ToolCallID string `json:"tool_call_id"` |
| 589 | } |
| 590 | |
| 591 | decoder := json.NewDecoder(r.Body) |
| 592 | if err := decoder.Decode(&requestBody); err != nil && err != io.EOF { |
| 593 | http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest) |
| 594 | return |
| 595 | } |
| 596 | defer r.Body.Close() |
| 597 | |
| 598 | cancelReason := "user requested cancellation" |
| 599 | if requestBody.Reason != "" { |
| 600 | cancelReason = requestBody.Reason |
| 601 | } |
| 602 | |
| 603 | if requestBody.ToolCallID != "" { |
| 604 | err := agent.CancelToolUse(requestBody.ToolCallID, fmt.Errorf("%s", cancelReason)) |
| 605 | if err != nil { |
| 606 | http.Error(w, err.Error(), http.StatusBadRequest) |
| 607 | return |
| 608 | } |
| 609 | // Return a success response |
| 610 | w.Header().Set("Content-Type", "application/json") |
| 611 | json.NewEncoder(w).Encode(map[string]string{ |
| 612 | "status": "cancelled", |
| 613 | "too_use_id": requestBody.ToolCallID, |
| Philip Zeyliger | 8d50d7b | 2025-04-23 13:12:40 -0700 | [diff] [blame] | 614 | "reason": cancelReason, |
| 615 | }) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 616 | return |
| 617 | } |
| Sean McCullough | edc88dc | 2025-04-30 02:55:01 +0000 | [diff] [blame] | 618 | // Call the CancelTurn method |
| 619 | agent.CancelTurn(fmt.Errorf("%s", cancelReason)) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 620 | // Return a success response |
| 621 | w.Header().Set("Content-Type", "application/json") |
| 622 | json.NewEncoder(w).Encode(map[string]string{"status": "cancelled", "reason": cancelReason}) |
| 623 | }) |
| 624 | |
| 625 | debugMux := initDebugMux() |
| 626 | s.mux.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) { |
| 627 | debugMux.ServeHTTP(w, r) |
| 628 | }) |
| 629 | |
| 630 | return s, nil |
| 631 | } |
| 632 | |
| 633 | // Utility functions |
| 634 | func getHostname() string { |
| 635 | hostname, err := os.Hostname() |
| 636 | if err != nil { |
| 637 | return "unknown" |
| 638 | } |
| 639 | return hostname |
| 640 | } |
| 641 | |
| 642 | func getWorkingDir() string { |
| 643 | wd, err := os.Getwd() |
| 644 | if err != nil { |
| 645 | return "unknown" |
| 646 | } |
| 647 | return wd |
| 648 | } |
| 649 | |
| 650 | // createTerminalSession creates a new terminal session with the given ID |
| 651 | func (s *Server) createTerminalSession(sessionID string) (*terminalSession, error) { |
| 652 | // Start a new shell process |
| 653 | shellPath := getShellPath() |
| 654 | cmd := exec.Command(shellPath) |
| 655 | |
| 656 | // Get working directory from the agent if possible |
| 657 | workDir := getWorkingDir() |
| 658 | cmd.Dir = workDir |
| 659 | |
| 660 | // Set up environment |
| 661 | cmd.Env = append(os.Environ(), "TERM=xterm-256color") |
| 662 | |
| 663 | // Start the command with a pty |
| 664 | ptmx, err := pty.Start(cmd) |
| 665 | if err != nil { |
| 666 | slog.Error("Failed to start pty", "error", err) |
| 667 | return nil, err |
| 668 | } |
| 669 | |
| 670 | // Create the terminal session |
| 671 | session := &terminalSession{ |
| 672 | pty: ptmx, |
| 673 | eventsClients: make(map[chan []byte]bool), |
| 674 | cmd: cmd, |
| 675 | } |
| 676 | |
| 677 | // Start goroutine to read from pty and broadcast to all connected SSE clients |
| 678 | go s.readFromPtyAndBroadcast(sessionID, session) |
| 679 | |
| 680 | return session, nil |
| 681 | } // handleTerminalEvents handles SSE connections for terminal output |
| 682 | func (s *Server) handleTerminalEvents(w http.ResponseWriter, r *http.Request, sessionID string) { |
| 683 | // Check if the session exists, if not, create it |
| 684 | s.ptyMutex.Lock() |
| 685 | session, exists := s.terminalSessions[sessionID] |
| 686 | |
| 687 | if !exists { |
| 688 | // Create a new terminal session |
| 689 | var err error |
| 690 | session, err = s.createTerminalSession(sessionID) |
| 691 | if err != nil { |
| 692 | s.ptyMutex.Unlock() |
| 693 | http.Error(w, fmt.Sprintf("Failed to create terminal: %v", err), http.StatusInternalServerError) |
| 694 | return |
| 695 | } |
| 696 | |
| 697 | // Store the new session |
| 698 | s.terminalSessions[sessionID] = session |
| 699 | } |
| 700 | s.ptyMutex.Unlock() |
| 701 | |
| 702 | // Set headers for SSE |
| 703 | w.Header().Set("Content-Type", "text/event-stream") |
| 704 | w.Header().Set("Cache-Control", "no-cache") |
| 705 | w.Header().Set("Connection", "keep-alive") |
| 706 | w.Header().Set("Access-Control-Allow-Origin", "*") |
| 707 | |
| 708 | // Create a channel for this client |
| 709 | events := make(chan []byte, 4096) // Buffer to prevent blocking |
| 710 | |
| 711 | // Register this client's channel |
| 712 | session.eventsClientsMutex.Lock() |
| 713 | clientID := session.lastEventClientID + 1 |
| 714 | session.lastEventClientID = clientID |
| 715 | session.eventsClients[events] = true |
| 716 | session.eventsClientsMutex.Unlock() |
| 717 | |
| 718 | // When the client disconnects, remove their channel |
| 719 | defer func() { |
| 720 | session.eventsClientsMutex.Lock() |
| 721 | delete(session.eventsClients, events) |
| 722 | close(events) |
| 723 | session.eventsClientsMutex.Unlock() |
| 724 | }() |
| 725 | |
| 726 | // Flush to send headers to client immediately |
| 727 | if f, ok := w.(http.Flusher); ok { |
| 728 | f.Flush() |
| 729 | } |
| 730 | |
| 731 | // Send events to the client as they arrive |
| 732 | for { |
| 733 | select { |
| 734 | case <-r.Context().Done(): |
| 735 | return |
| 736 | case data := <-events: |
| 737 | // Format as SSE with base64 encoding |
| 738 | fmt.Fprintf(w, "data: %s\n\n", base64.StdEncoding.EncodeToString(data)) |
| 739 | |
| 740 | // Flush the data immediately |
| 741 | if f, ok := w.(http.Flusher); ok { |
| 742 | f.Flush() |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | |
| 748 | // handleTerminalInput processes input to the terminal |
| 749 | func (s *Server) handleTerminalInput(w http.ResponseWriter, r *http.Request, sessionID string) { |
| 750 | // Check if the session exists |
| 751 | s.ptyMutex.Lock() |
| 752 | session, exists := s.terminalSessions[sessionID] |
| 753 | s.ptyMutex.Unlock() |
| 754 | |
| 755 | if !exists { |
| 756 | http.Error(w, "Terminal session not found", http.StatusNotFound) |
| 757 | return |
| 758 | } |
| 759 | |
| 760 | // Read the request body (terminal input or resize command) |
| 761 | body, err := io.ReadAll(r.Body) |
| 762 | if err != nil { |
| 763 | http.Error(w, "Failed to read request body", http.StatusBadRequest) |
| 764 | return |
| 765 | } |
| 766 | |
| 767 | // Check if it's a resize message |
| 768 | if len(body) > 0 && body[0] == '{' { |
| 769 | var msg TerminalMessage |
| 770 | if err := json.Unmarshal(body, &msg); err == nil && msg.Type == "resize" { |
| 771 | if msg.Cols > 0 && msg.Rows > 0 { |
| 772 | pty.Setsize(session.pty, &pty.Winsize{ |
| 773 | Cols: msg.Cols, |
| 774 | Rows: msg.Rows, |
| 775 | }) |
| 776 | |
| 777 | // Respond with success |
| 778 | w.WriteHeader(http.StatusOK) |
| 779 | return |
| 780 | } |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | // Regular terminal input |
| 785 | _, err = session.pty.Write(body) |
| 786 | if err != nil { |
| 787 | slog.Error("Failed to write to pty", "error", err) |
| 788 | http.Error(w, "Failed to write to terminal", http.StatusInternalServerError) |
| 789 | return |
| 790 | } |
| 791 | |
| 792 | // Respond with success |
| 793 | w.WriteHeader(http.StatusOK) |
| 794 | } |
| 795 | |
| 796 | // readFromPtyAndBroadcast reads output from the PTY and broadcasts it to all connected clients |
| 797 | func (s *Server) readFromPtyAndBroadcast(sessionID string, session *terminalSession) { |
| 798 | buf := make([]byte, 4096) |
| 799 | defer func() { |
| 800 | // Clean up when done |
| 801 | s.ptyMutex.Lock() |
| 802 | delete(s.terminalSessions, sessionID) |
| 803 | s.ptyMutex.Unlock() |
| 804 | |
| 805 | // Close the PTY |
| 806 | session.pty.Close() |
| 807 | |
| 808 | // Ensure process is terminated |
| 809 | if session.cmd.Process != nil { |
| 810 | session.cmd.Process.Signal(syscall.SIGTERM) |
| 811 | time.Sleep(100 * time.Millisecond) |
| 812 | session.cmd.Process.Kill() |
| 813 | } |
| 814 | |
| 815 | // Close all client channels |
| 816 | session.eventsClientsMutex.Lock() |
| 817 | for ch := range session.eventsClients { |
| 818 | delete(session.eventsClients, ch) |
| 819 | close(ch) |
| 820 | } |
| 821 | session.eventsClientsMutex.Unlock() |
| 822 | }() |
| 823 | |
| 824 | for { |
| 825 | n, err := session.pty.Read(buf) |
| 826 | if err != nil { |
| 827 | if err != io.EOF { |
| 828 | slog.Error("Failed to read from pty", "error", err) |
| 829 | } |
| 830 | break |
| 831 | } |
| 832 | |
| 833 | // Make a copy of the data for each client |
| 834 | data := make([]byte, n) |
| 835 | copy(data, buf[:n]) |
| 836 | |
| 837 | // Broadcast to all connected clients |
| 838 | session.eventsClientsMutex.Lock() |
| 839 | for ch := range session.eventsClients { |
| 840 | // Try to send, but don't block if channel is full |
| 841 | select { |
| 842 | case ch <- data: |
| 843 | default: |
| 844 | // Channel is full, drop the message for this client |
| 845 | } |
| 846 | } |
| 847 | session.eventsClientsMutex.Unlock() |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | // getShellPath returns the path to the shell to use |
| 852 | func getShellPath() string { |
| 853 | // Try to use the user's preferred shell |
| 854 | shell := os.Getenv("SHELL") |
| 855 | if shell != "" { |
| 856 | return shell |
| 857 | } |
| 858 | |
| 859 | // Default to bash on Unix-like systems |
| 860 | if _, err := os.Stat("/bin/bash"); err == nil { |
| 861 | return "/bin/bash" |
| 862 | } |
| 863 | |
| 864 | // Fall back to sh |
| 865 | return "/bin/sh" |
| 866 | } |
| 867 | |
| 868 | func initDebugMux() *http.ServeMux { |
| 869 | mux := http.NewServeMux() |
| 870 | mux.HandleFunc("GET /debug/{$}", func(w http.ResponseWriter, r *http.Request) { |
| 871 | w.Header().Set("Content-Type", "text/html; charset=utf-8") |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 872 | // TODO: pid is not as useful as "outside pid" |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 873 | fmt.Fprintf(w, `<!doctype html> |
| 874 | <html><head><title>sketch debug</title></head><body> |
| 875 | <h1>sketch debug</h1> |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 876 | pid %d |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 877 | <ul> |
| 878 | <li><a href="/debug/pprof/cmdline">pprof/cmdline</a></li> |
| 879 | <li><a href="/debug/pprof/profile">pprof/profile</a></li> |
| 880 | <li><a href="/debug/pprof/symbol">pprof/symbol</a></li> |
| 881 | <li><a href="/debug/pprof/trace">pprof/trace</a></li> |
| 882 | <li><a href="/debug/pprof/goroutine?debug=1">pprof/goroutine?debug=1</a></li> |
| 883 | <li><a href="/debug/metrics">metrics</a></li> |
| 884 | </ul> |
| 885 | </body> |
| 886 | </html> |
| Philip Zeyliger | 2c4db09 | 2025-04-28 16:57:50 -0700 | [diff] [blame] | 887 | `, os.Getpid()) |
| Earl Lee | 2e463fb | 2025-04-17 11:22:22 -0700 | [diff] [blame] | 888 | }) |
| 889 | mux.HandleFunc("GET /debug/pprof/", pprof.Index) |
| 890 | mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline) |
| 891 | mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile) |
| 892 | mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol) |
| 893 | mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace) |
| 894 | return mux |
| 895 | } |
| 896 | |
| 897 | // isValidGitSHA validates if a string looks like a valid git SHA hash. |
| 898 | // Git SHAs are hexadecimal strings of at least 4 characters but typically 7, 8, or 40 characters. |
| 899 | func isValidGitSHA(sha string) bool { |
| 900 | // Git SHA must be a hexadecimal string with at least 4 characters |
| 901 | if len(sha) < 4 || len(sha) > 40 { |
| 902 | return false |
| 903 | } |
| 904 | |
| 905 | // Check if the string only contains hexadecimal characters |
| 906 | for _, char := range sha { |
| 907 | if !(char >= '0' && char <= '9') && !(char >= 'a' && char <= 'f') && !(char >= 'A' && char <= 'F') { |
| 908 | return false |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | return true |
| 913 | } |