blob: c72fe509889abf6a9989295fc45fc5cd09d74835 [file] [log] [blame]
Earl Lee2e463fb2025-04-17 11:22:22 -07001// Package server provides HTTP server functionality for the sketch loop.
2package server
3
4import (
Sean McCulloughbaa2b592025-04-23 10:40:08 -07005 "context"
Philip Zeyligerf84e88c2025-05-14 23:19:01 +00006 "crypto/rand"
Earl Lee2e463fb2025-04-17 11:22:22 -07007 "encoding/base64"
Philip Zeyligerf84e88c2025-05-14 23:19:01 +00008 "encoding/hex"
Earl Lee2e463fb2025-04-17 11:22:22 -07009 "encoding/json"
Josh Bleecher Snyder5c29b3e2025-07-08 18:07:28 +000010 "errors"
Earl Lee2e463fb2025-04-17 11:22:22 -070011 "fmt"
12 "html"
13 "io"
Earl Lee2e463fb2025-04-17 11:22:22 -070014 "log/slog"
15 "net/http"
Philip Zeyligera9710d72025-07-02 02:50:14 +000016 "net/http/httputil"
Earl Lee2e463fb2025-04-17 11:22:22 -070017 "net/http/pprof"
Philip Zeyligera9710d72025-07-02 02:50:14 +000018 "net/url"
Earl Lee2e463fb2025-04-17 11:22:22 -070019 "os"
20 "os/exec"
Philip Zeyligerf84e88c2025-05-14 23:19:01 +000021 "path/filepath"
Philip Zeyliger8d8b7ac2025-05-21 09:57:23 -070022 "runtime/debug"
Earl Lee2e463fb2025-04-17 11:22:22 -070023 "strconv"
24 "strings"
25 "sync"
Earl Lee2e463fb2025-04-17 11:22:22 -070026 "time"
27
28 "github.com/creack/pty"
Philip Zeyliger33d282f2025-05-03 04:01:54 +000029 "sketch.dev/claudetool/browse"
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -070030 "sketch.dev/embedded"
31 "sketch.dev/git_tools"
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070032 "sketch.dev/llm/conversation"
Earl Lee2e463fb2025-04-17 11:22:22 -070033 "sketch.dev/loop"
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -070034 "sketch.dev/loop/server/gzhandler"
Earl Lee2e463fb2025-04-17 11:22:22 -070035)
36
37// terminalSession represents a terminal session with its PTY and the event channel
38type terminalSession struct {
39 pty *os.File
40 eventsClients map[chan []byte]bool
41 lastEventClientID int
42 eventsClientsMutex sync.Mutex
43 cmd *exec.Cmd
44}
45
46// TerminalMessage represents a message sent from the client for terminal resize events
47type TerminalMessage struct {
48 Type string `json:"type"`
49 Cols uint16 `json:"cols"`
50 Rows uint16 `json:"rows"`
51}
52
53// TerminalResponse represents the response for a new terminal creation
54type TerminalResponse struct {
55 SessionID string `json:"sessionId"`
56}
57
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -070058// TodoItem represents a single todo item for task management
59type TodoItem struct {
60 ID string `json:"id"`
61 Task string `json:"task"`
62 Status string `json:"status"` // queued, in-progress, completed
63}
64
65// TodoList represents a collection of todo items
66type TodoList struct {
67 Items []TodoItem `json:"items"`
68}
69
Sean McCulloughd9f13372025-04-21 15:08:49 -070070type State struct {
Philip Zeyligerd03318d2025-05-08 13:09:12 -070071 // null or 1: "old"
72 // 2: supports SSE for message updates
73 StateVersion int `json:"state_version"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070074 MessageCount int `json:"message_count"`
75 TotalUsage *conversation.CumulativeUsage `json:"total_usage,omitempty"`
76 InitialCommit string `json:"initial_commit"`
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -070077 Slug string `json:"slug,omitempty"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070078 BranchName string `json:"branch_name,omitempty"`
Philip Zeyligerbe7802a2025-06-04 20:15:25 +000079 BranchPrefix string `json:"branch_prefix,omitempty"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070080 Hostname string `json:"hostname"` // deprecated
81 WorkingDir string `json:"working_dir"` // deprecated
82 OS string `json:"os"` // deprecated
83 GitOrigin string `json:"git_origin,omitempty"`
bankseancad67b02025-06-27 21:57:05 +000084 GitUsername string `json:"git_username,omitempty"`
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -070085 OutstandingLLMCalls int `json:"outstanding_llm_calls"`
86 OutstandingToolCalls []string `json:"outstanding_tool_calls"`
87 SessionID string `json:"session_id"`
88 SSHAvailable bool `json:"ssh_available"`
89 SSHError string `json:"ssh_error,omitempty"`
90 InContainer bool `json:"in_container"`
91 FirstMessageIndex int `json:"first_message_index"`
92 AgentState string `json:"agent_state,omitempty"`
93 OutsideHostname string `json:"outside_hostname,omitempty"`
94 InsideHostname string `json:"inside_hostname,omitempty"`
95 OutsideOS string `json:"outside_os,omitempty"`
96 InsideOS string `json:"inside_os,omitempty"`
97 OutsideWorkingDir string `json:"outside_working_dir,omitempty"`
98 InsideWorkingDir string `json:"inside_working_dir,omitempty"`
philip.zeyliger8773e682025-06-11 21:36:21 -070099 TodoContent string `json:"todo_content,omitempty"` // Contains todo list JSON data
100 SkabandAddr string `json:"skaband_addr,omitempty"` // URL of the skaband server
101 LinkToGitHub bool `json:"link_to_github,omitempty"` // Enable GitHub branch linking in UI
102 SSHConnectionString string `json:"ssh_connection_string,omitempty"` // SSH connection string for container
Philip Zeyliger64f60462025-06-16 13:57:10 -0700103 DiffLinesAdded int `json:"diff_lines_added"` // Lines added from sketch-base to HEAD
104 DiffLinesRemoved int `json:"diff_lines_removed"` // Lines removed from sketch-base to HEAD
Philip Zeyliger5f26a342025-07-04 01:30:29 +0000105 OpenPorts []Port `json:"open_ports,omitempty"` // Currently open TCP ports
106}
107
108// Port represents an open TCP port
109type Port struct {
110 Proto string `json:"proto"` // "tcp" or "udp"
111 Port uint16 `json:"port"` // port number
112 Process string `json:"process"` // optional process name
113 Pid int `json:"pid"` // process ID
Sean McCulloughd9f13372025-04-21 15:08:49 -0700114}
115
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700116type InitRequest struct {
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700117 // Passed to agent so that the URL it prints in the termui prompt is correct (when skaband is not used)
118 HostAddr string `json:"host_addr"`
119
120 // POST /init will start the SSH server with these configs
Sean McCullough7013e9e2025-05-14 02:03:58 +0000121 SSHAuthorizedKeys []byte `json:"ssh_authorized_keys"`
122 SSHServerIdentity []byte `json:"ssh_server_identity"`
123 SSHContainerCAKey []byte `json:"ssh_container_ca_key"`
124 SSHHostCertificate []byte `json:"ssh_host_certificate"`
125 SSHAvailable bool `json:"ssh_available"`
126 SSHError string `json:"ssh_error,omitempty"`
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700127}
128
Earl Lee2e463fb2025-04-17 11:22:22 -0700129// Server serves sketch HTTP. Server implements http.Handler.
130type Server struct {
131 mux *http.ServeMux
132 agent loop.CodingAgent
133 hostname string
134 logFile *os.File
135 // Mutex to protect terminalSessions
136 ptyMutex sync.Mutex
137 terminalSessions map[string]*terminalSession
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000138 sshAvailable bool
139 sshError string
Earl Lee2e463fb2025-04-17 11:22:22 -0700140}
141
142func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Philip Zeyligera9710d72025-07-02 02:50:14 +0000143 // Check if Host header matches "p<port>.localhost" pattern and proxy to that port
144 if port := s.ParsePortProxyHost(r.Host); port != "" {
145 s.proxyToPort(w, r, port)
146 return
147 }
148
Earl Lee2e463fb2025-04-17 11:22:22 -0700149 s.mux.ServeHTTP(w, r)
150}
151
Philip Zeyligera9710d72025-07-02 02:50:14 +0000152// ParsePortProxyHost checks if host matches "p<port>.localhost" pattern and returns the port
153func (s *Server) ParsePortProxyHost(host string) string {
154 // Remove port suffix if present (e.g., "p8000.localhost:8080" -> "p8000.localhost")
155 hostname := host
156 if idx := strings.LastIndex(host, ":"); idx > 0 {
157 hostname = host[:idx]
158 }
159
160 // Check if hostname matches p<port>.localhost pattern
161 if strings.HasSuffix(hostname, ".localhost") {
162 prefix := strings.TrimSuffix(hostname, ".localhost")
163 if strings.HasPrefix(prefix, "p") && len(prefix) > 1 {
164 port := prefix[1:] // Remove 'p' prefix
165 // Basic validation - port should be numeric and in valid range
166 if portNum, err := strconv.Atoi(port); err == nil && portNum > 0 && portNum <= 65535 {
167 return port
168 }
169 }
170 }
171
172 return ""
173}
174
175// proxyToPort proxies the request to localhost:<port>
176func (s *Server) proxyToPort(w http.ResponseWriter, r *http.Request, port string) {
177 // Create a reverse proxy to localhost:<port>
178 target, err := url.Parse(fmt.Sprintf("http://localhost:%s", port))
179 if err != nil {
180 http.Error(w, "Failed to parse proxy target", http.StatusInternalServerError)
181 return
182 }
183
184 proxy := httputil.NewSingleHostReverseProxy(target)
185
186 // Customize the Director to modify the request
187 originalDirector := proxy.Director
188 proxy.Director = func(req *http.Request) {
189 originalDirector(req)
190 // Set the target host
191 req.URL.Host = target.Host
192 req.URL.Scheme = target.Scheme
193 req.Host = target.Host
194 }
195
196 // Handle proxy errors
197 proxy.ErrorHandler = func(w http.ResponseWriter, r *http.Request, err error) {
198 slog.Error("Proxy error", "error", err, "target", target.String(), "port", port)
199 http.Error(w, "Proxy error: "+err.Error(), http.StatusBadGateway)
200 }
201
202 proxy.ServeHTTP(w, r)
203}
204
Earl Lee2e463fb2025-04-17 11:22:22 -0700205// New creates a new HTTP server.
206func New(agent loop.CodingAgent, logFile *os.File) (*Server, error) {
207 s := &Server{
208 mux: http.NewServeMux(),
209 agent: agent,
210 hostname: getHostname(),
211 logFile: logFile,
212 terminalSessions: make(map[string]*terminalSession),
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000213 sshAvailable: false,
214 sshError: "",
Earl Lee2e463fb2025-04-17 11:22:22 -0700215 }
216
Philip Zeyliger25f6ff12025-05-02 04:24:10 +0000217 s.mux.HandleFunc("/stream", s.handleSSEStream)
Philip Zeyligerd3ac1122025-05-14 02:54:18 +0000218
219 // Git tool endpoints
220 s.mux.HandleFunc("/git/rawdiff", s.handleGitRawDiff)
221 s.mux.HandleFunc("/git/show", s.handleGitShow)
Philip Zeyliger272a90e2025-05-16 14:49:51 -0700222 s.mux.HandleFunc("/git/cat", s.handleGitCat)
223 s.mux.HandleFunc("/git/save", s.handleGitSave)
Philip Zeyligerd3ac1122025-05-14 02:54:18 +0000224 s.mux.HandleFunc("/git/recentlog", s.handleGitRecentLog)
225
Earl Lee2e463fb2025-04-17 11:22:22 -0700226 s.mux.HandleFunc("/diff", func(w http.ResponseWriter, r *http.Request) {
227 // Check if a specific commit hash was requested
228 commit := r.URL.Query().Get("commit")
229
230 // Get the diff, optionally for a specific commit
231 var diff string
232 var err error
233 if commit != "" {
234 // Validate the commit hash format
235 if !isValidGitSHA(commit) {
236 http.Error(w, fmt.Sprintf("Invalid git commit SHA format: %s", commit), http.StatusBadRequest)
237 return
238 }
239
240 diff, err = agent.Diff(&commit)
241 } else {
242 diff, err = agent.Diff(nil)
243 }
244
245 if err != nil {
246 http.Error(w, fmt.Sprintf("Error generating diff: %v", err), http.StatusInternalServerError)
247 return
248 }
249
250 w.Header().Set("Content-Type", "text/plain")
251 w.Write([]byte(diff))
252 })
253
254 // Handler for initialization called by host sketch binary when inside docker.
255 s.mux.HandleFunc("/init", func(w http.ResponseWriter, r *http.Request) {
256 defer func() {
257 if err := recover(); err != nil {
258 slog.ErrorContext(r.Context(), "/init panic", slog.Any("recovered_err", err))
259
260 // Return an error response to the client
261 http.Error(w, fmt.Sprintf("panic: %v\n", err), http.StatusInternalServerError)
262 }
263 }()
264
265 if r.Method != "POST" {
266 http.Error(w, "POST required", http.StatusBadRequest)
267 return
268 }
269
270 body, err := io.ReadAll(r.Body)
271 r.Body.Close()
272 if err != nil {
273 http.Error(w, "failed to read request body: "+err.Error(), http.StatusBadRequest)
274 return
275 }
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700276
277 m := &InitRequest{}
278 if err := json.Unmarshal(body, m); err != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700279 http.Error(w, "bad request body: "+err.Error(), http.StatusBadRequest)
280 return
281 }
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700282
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000283 // Store SSH availability info
284 s.sshAvailable = m.SSHAvailable
285 s.sshError = m.SSHError
286
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700287 // Start the SSH server if the init request included ssh keys.
288 if len(m.SSHAuthorizedKeys) > 0 && len(m.SSHServerIdentity) > 0 {
289 go func() {
290 ctx := context.Background()
Sean McCullough7013e9e2025-05-14 02:03:58 +0000291 if err := s.ServeSSH(ctx, m.SSHServerIdentity, m.SSHAuthorizedKeys, m.SSHContainerCAKey, m.SSHHostCertificate); err != nil {
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700292 slog.ErrorContext(r.Context(), "/init ServeSSH", slog.String("err", err.Error()))
Philip Zeyligerc72fff52025-04-29 20:17:54 +0000293 // Update SSH error if server fails to start
294 s.sshAvailable = false
295 s.sshError = err.Error()
Sean McCulloughbaa2b592025-04-23 10:40:08 -0700296 }
297 }()
298 }
299
Earl Lee2e463fb2025-04-17 11:22:22 -0700300 ini := loop.AgentInit{
Philip Zeyligerbc8c8dc2025-05-21 13:19:13 -0700301 InDocker: true,
302 HostAddr: m.HostAddr,
Earl Lee2e463fb2025-04-17 11:22:22 -0700303 }
304 if err := agent.Init(ini); err != nil {
305 http.Error(w, "init failed: "+err.Error(), http.StatusInternalServerError)
306 return
307 }
308 w.Header().Set("Content-Type", "application/json")
309 io.WriteString(w, "{}\n")
310 })
311
312 // Handler for /messages?start=N&end=M (start/end are optional)
313 s.mux.HandleFunc("/messages", func(w http.ResponseWriter, r *http.Request) {
314 w.Header().Set("Content-Type", "application/json")
315
316 // Extract query parameters for range
317 var start, end int
318 var err error
319
320 currentCount := agent.MessageCount()
321
322 startParam := r.URL.Query().Get("start")
323 if startParam != "" {
324 start, err = strconv.Atoi(startParam)
325 if err != nil {
326 http.Error(w, "Invalid 'start' parameter", http.StatusBadRequest)
327 return
328 }
329 }
330
331 endParam := r.URL.Query().Get("end")
332 if endParam != "" {
333 end, err = strconv.Atoi(endParam)
334 if err != nil {
335 http.Error(w, "Invalid 'end' parameter", http.StatusBadRequest)
336 return
337 }
338 } else {
339 end = currentCount
340 }
341
342 if start < 0 || start > end || end > currentCount {
343 http.Error(w, fmt.Sprintf("Invalid range: start %d end %d currentCount %d", start, end, currentCount), http.StatusBadRequest)
344 return
345 }
346
347 start = max(0, start)
348 end = min(agent.MessageCount(), end)
349 messages := agent.Messages(start, end)
350
351 // Create a JSON encoder with indentation for pretty-printing
352 encoder := json.NewEncoder(w)
353 encoder.SetIndent("", " ") // Two spaces for each indentation level
354
355 err = encoder.Encode(messages)
356 if err != nil {
357 http.Error(w, err.Error(), http.StatusInternalServerError)
358 }
359 })
360
361 // Handler for /logs - displays the contents of the log file
362 s.mux.HandleFunc("/logs", func(w http.ResponseWriter, r *http.Request) {
363 if s.logFile == nil {
364 http.Error(w, "log file not set", http.StatusNotFound)
365 return
366 }
367 logContents, err := os.ReadFile(s.logFile.Name())
368 if err != nil {
369 http.Error(w, "error reading log file: "+err.Error(), http.StatusInternalServerError)
370 return
371 }
372 w.Header().Set("Content-Type", "text/html; charset=utf-8")
373 fmt.Fprintf(w, "<!DOCTYPE html>\n<html>\n<head>\n<title>Sketchy Log File</title>\n</head>\n<body>\n")
374 fmt.Fprintf(w, "<pre>%s</pre>\n", html.EscapeString(string(logContents)))
375 fmt.Fprintf(w, "</body>\n</html>")
376 })
377
378 // Handler for /download - downloads both messages and status as a JSON file
379 s.mux.HandleFunc("/download", func(w http.ResponseWriter, r *http.Request) {
380 // Set headers for file download
381 w.Header().Set("Content-Type", "application/octet-stream")
382
383 // Generate filename with format: sketch-YYYYMMDD-HHMMSS.json
384 timestamp := time.Now().Format("20060102-150405")
385 filename := fmt.Sprintf("sketch-%s.json", timestamp)
386
387 w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
388
389 // Get all messages
390 messageCount := agent.MessageCount()
391 messages := agent.Messages(0, messageCount)
392
393 // Get status information (usage and other metadata)
394 totalUsage := agent.TotalUsage()
395 hostname := getHostname()
396 workingDir := getWorkingDir()
397
398 // Create a combined structure with all information
399 downloadData := struct {
Josh Bleecher Snyder4f84ab72025-04-22 16:40:54 -0700400 Messages []loop.AgentMessage `json:"messages"`
401 MessageCount int `json:"message_count"`
402 TotalUsage conversation.CumulativeUsage `json:"total_usage"`
403 Hostname string `json:"hostname"`
404 WorkingDir string `json:"working_dir"`
405 DownloadTime string `json:"download_time"`
Earl Lee2e463fb2025-04-17 11:22:22 -0700406 }{
407 Messages: messages,
408 MessageCount: messageCount,
409 TotalUsage: totalUsage,
410 Hostname: hostname,
411 WorkingDir: workingDir,
412 DownloadTime: time.Now().Format(time.RFC3339),
413 }
414
415 // Marshal the JSON with indentation for better readability
416 jsonData, err := json.MarshalIndent(downloadData, "", " ")
417 if err != nil {
418 http.Error(w, err.Error(), http.StatusInternalServerError)
419 return
420 }
421 w.Write(jsonData)
422 })
423
424 // The latter doesn't return until the number of messages has changed (from seen
425 // or from when this was called.)
426 s.mux.HandleFunc("/state", func(w http.ResponseWriter, r *http.Request) {
427 pollParam := r.URL.Query().Get("poll")
428 seenParam := r.URL.Query().Get("seen")
429
430 // Get the client's current message count (if provided)
431 clientMessageCount := -1
432 var err error
433 if seenParam != "" {
434 clientMessageCount, err = strconv.Atoi(seenParam)
435 if err != nil {
436 http.Error(w, "Invalid 'seen' parameter", http.StatusBadRequest)
437 return
438 }
439 }
440
441 serverMessageCount := agent.MessageCount()
442
443 // Let lazy clients not have to specify this.
444 if clientMessageCount == -1 {
445 clientMessageCount = serverMessageCount
446 }
447
448 if pollParam == "true" {
449 ch := make(chan string)
450 go func() {
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700451 it := agent.NewIterator(r.Context(), clientMessageCount)
452 it.Next()
Earl Lee2e463fb2025-04-17 11:22:22 -0700453 close(ch)
Philip Zeyligerb7c58752025-05-01 10:10:17 -0700454 it.Close()
Earl Lee2e463fb2025-04-17 11:22:22 -0700455 }()
456 select {
457 case <-r.Context().Done():
458 slog.DebugContext(r.Context(), "abandoned poll request")
459 return
460 case <-time.After(90 * time.Second):
461 // Let the user call /state again to get the latest to limit how long our long polls hang out.
462 slog.DebugContext(r.Context(), "longish poll request")
463 break
464 case <-ch:
465 break
466 }
467 }
468
Earl Lee2e463fb2025-04-17 11:22:22 -0700469 w.Header().Set("Content-Type", "application/json")
470
Philip Zeyliger25f6ff12025-05-02 04:24:10 +0000471 // Use the shared getState function
472 state := s.getState()
Earl Lee2e463fb2025-04-17 11:22:22 -0700473
474 // Create a JSON encoder with indentation for pretty-printing
475 encoder := json.NewEncoder(w)
476 encoder.SetIndent("", " ") // Two spaces for each indentation level
477
478 err = encoder.Encode(state)
479 if err != nil {
480 http.Error(w, err.Error(), http.StatusInternalServerError)
481 }
482 })
483
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700484 s.mux.Handle("/static/", http.StripPrefix("/static/", gzhandler.New(embedded.WebUIFS())))
Earl Lee2e463fb2025-04-17 11:22:22 -0700485
486 // Terminal WebSocket handler
487 // Terminal endpoints - predefined terminals 1-9
488 // TODO: The UI doesn't actually know how to use terminals 2-9!
489 s.mux.HandleFunc("/terminal/events/", func(w http.ResponseWriter, r *http.Request) {
490 if r.Method != http.MethodGet {
491 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
492 return
493 }
494 pathParts := strings.Split(r.URL.Path, "/")
495 if len(pathParts) < 4 {
496 http.Error(w, "Invalid terminal ID", http.StatusBadRequest)
497 return
498 }
499
500 sessionID := pathParts[3]
501 // Validate that the terminal ID is between 1-9
502 if len(sessionID) != 1 || sessionID[0] < '1' || sessionID[0] > '9' {
503 http.Error(w, "Terminal ID must be between 1 and 9", http.StatusBadRequest)
504 return
505 }
506
507 s.handleTerminalEvents(w, r, sessionID)
508 })
509
510 s.mux.HandleFunc("/terminal/input/", func(w http.ResponseWriter, r *http.Request) {
511 if r.Method != http.MethodPost {
512 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
513 return
514 }
515 pathParts := strings.Split(r.URL.Path, "/")
516 if len(pathParts) < 4 {
517 http.Error(w, "Invalid terminal ID", http.StatusBadRequest)
518 return
519 }
520 sessionID := pathParts[3]
521 s.handleTerminalInput(w, r, sessionID)
522 })
523
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700524 // Handler for interface selection via URL parameters (?m for mobile)
Earl Lee2e463fb2025-04-17 11:22:22 -0700525 s.mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700526 webuiFS := embedded.WebUIFS()
527 appShell := "sketch-app-shell.html"
528 if r.URL.Query().Has("m") {
529 appShell = "mobile-app-shell.html"
Philip Zeyligere08c7ff2025-06-06 13:22:12 -0700530 }
Josh Bleecher Snyder1c18ec92025-07-08 10:55:54 -0700531 http.ServeFileFS(w, r, webuiFS, appShell)
Earl Lee2e463fb2025-04-17 11:22:22 -0700532 })
533
Philip Zeyliger2c4db092025-04-28 16:57:50 -0700534 // Handler for /commit-description - returns the description of a git commit
535 s.mux.HandleFunc("/commit-description", func(w http.ResponseWriter, r *http.Request) {
536 if r.Method != http.MethodGet {
537 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
538 return
539 }
540
541 // Get the revision parameter
542 revision := r.URL.Query().Get("revision")
543 if revision == "" {
544 http.Error(w, "Missing revision parameter", http.StatusBadRequest)
545 return
546 }
547
548 // Run git command to get commit description
549 cmd := exec.Command("git", "log", "--oneline", "--decorate", "-n", "1", revision)
550 // Use the working directory from the agent
551 cmd.Dir = s.agent.WorkingDir()
552
553 output, err := cmd.CombinedOutput()
554 if err != nil {
555 http.Error(w, "Failed to get commit description: "+err.Error(), http.StatusInternalServerError)
556 return
557 }
558
559 // Prepare the response
560 resp := map[string]string{
561 "description": strings.TrimSpace(string(output)),
562 }
563
564 w.Header().Set("Content-Type", "application/json")
565 if err := json.NewEncoder(w).Encode(resp); err != nil {
566 slog.ErrorContext(r.Context(), "Error encoding commit description response", slog.Any("err", err))
567 }
568 })
569
Philip Zeyliger33d282f2025-05-03 04:01:54 +0000570 // Handler for /screenshot/{id} - serves screenshot images
571 s.mux.HandleFunc("/screenshot/", func(w http.ResponseWriter, r *http.Request) {
572 if r.Method != http.MethodGet {
573 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
574 return
575 }
576
577 // Extract the screenshot ID from the path
578 pathParts := strings.Split(r.URL.Path, "/")
579 if len(pathParts) < 3 {
580 http.Error(w, "Invalid screenshot ID", http.StatusBadRequest)
581 return
582 }
583
584 screenshotID := pathParts[2]
585
586 // Validate the ID format (prevent directory traversal)
587 if strings.Contains(screenshotID, "/") || strings.Contains(screenshotID, "\\") {
588 http.Error(w, "Invalid screenshot ID format", http.StatusBadRequest)
589 return
590 }
591
592 // Get the screenshot file path
593 filePath := browse.GetScreenshotPath(screenshotID)
594
595 // Check if the file exists
596 if _, err := os.Stat(filePath); os.IsNotExist(err) {
597 http.Error(w, "Screenshot not found", http.StatusNotFound)
598 return
599 }
600
601 // Serve the file
602 w.Header().Set("Content-Type", "image/png")
603 w.Header().Set("Cache-Control", "max-age=3600") // Cache for an hour
604 http.ServeFile(w, r, filePath)
605 })
606
Earl Lee2e463fb2025-04-17 11:22:22 -0700607 // Handler for POST /chat
608 s.mux.HandleFunc("/chat", func(w http.ResponseWriter, r *http.Request) {
609 if r.Method != http.MethodPost {
610 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
611 return
612 }
613
614 // Parse the request body
615 var requestBody struct {
616 Message string `json:"message"`
617 }
618
619 decoder := json.NewDecoder(r.Body)
620 if err := decoder.Decode(&requestBody); err != nil {
621 http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest)
622 return
623 }
624 defer r.Body.Close()
625
626 if requestBody.Message == "" {
627 http.Error(w, "Message cannot be empty", http.StatusBadRequest)
628 return
629 }
630
631 agent.UserMessage(r.Context(), requestBody.Message)
632
633 w.WriteHeader(http.StatusOK)
634 })
635
Philip Zeyligerf84e88c2025-05-14 23:19:01 +0000636 // Handler for POST /upload - uploads a file to /tmp
637 s.mux.HandleFunc("/upload", func(w http.ResponseWriter, r *http.Request) {
638 if r.Method != http.MethodPost {
639 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
640 return
641 }
642
643 // Limit to 10MB file size
644 r.Body = http.MaxBytesReader(w, r.Body, 10*1024*1024)
645
646 // Parse the multipart form
647 if err := r.ParseMultipartForm(10 * 1024 * 1024); err != nil {
648 http.Error(w, "Failed to parse form: "+err.Error(), http.StatusBadRequest)
649 return
650 }
651
652 // Get the file from the multipart form
653 file, handler, err := r.FormFile("file")
654 if err != nil {
655 http.Error(w, "Failed to get uploaded file: "+err.Error(), http.StatusBadRequest)
656 return
657 }
658 defer file.Close()
659
660 // Generate a unique ID (8 random bytes converted to 16 hex chars)
661 randBytes := make([]byte, 8)
662 if _, err := rand.Read(randBytes); err != nil {
663 http.Error(w, "Failed to generate random filename: "+err.Error(), http.StatusInternalServerError)
664 return
665 }
666
667 // Get file extension from the original filename
668 ext := filepath.Ext(handler.Filename)
669
670 // Create a unique filename in the /tmp directory
671 filename := fmt.Sprintf("/tmp/sketch_file_%s%s", hex.EncodeToString(randBytes), ext)
672
673 // Create the destination file
674 destFile, err := os.Create(filename)
675 if err != nil {
676 http.Error(w, "Failed to create destination file: "+err.Error(), http.StatusInternalServerError)
677 return
678 }
679 defer destFile.Close()
680
681 // Copy the file contents to the destination file
682 if _, err := io.Copy(destFile, file); err != nil {
683 http.Error(w, "Failed to save file: "+err.Error(), http.StatusInternalServerError)
684 return
685 }
686
687 // Return the path to the saved file
688 w.Header().Set("Content-Type", "application/json")
689 json.NewEncoder(w).Encode(map[string]string{"path": filename})
690 })
691
Earl Lee2e463fb2025-04-17 11:22:22 -0700692 // Handler for /cancel - cancels the current inner loop in progress
693 s.mux.HandleFunc("/cancel", func(w http.ResponseWriter, r *http.Request) {
694 if r.Method != http.MethodPost {
695 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
696 return
697 }
698
699 // Parse the request body (optional)
700 var requestBody struct {
701 Reason string `json:"reason"`
702 ToolCallID string `json:"tool_call_id"`
703 }
704
705 decoder := json.NewDecoder(r.Body)
706 if err := decoder.Decode(&requestBody); err != nil && err != io.EOF {
707 http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest)
708 return
709 }
710 defer r.Body.Close()
711
712 cancelReason := "user requested cancellation"
713 if requestBody.Reason != "" {
714 cancelReason = requestBody.Reason
715 }
716
717 if requestBody.ToolCallID != "" {
718 err := agent.CancelToolUse(requestBody.ToolCallID, fmt.Errorf("%s", cancelReason))
719 if err != nil {
720 http.Error(w, err.Error(), http.StatusBadRequest)
721 return
722 }
723 // Return a success response
724 w.Header().Set("Content-Type", "application/json")
725 json.NewEncoder(w).Encode(map[string]string{
726 "status": "cancelled",
727 "too_use_id": requestBody.ToolCallID,
Philip Zeyliger8d50d7b2025-04-23 13:12:40 -0700728 "reason": cancelReason,
729 })
Earl Lee2e463fb2025-04-17 11:22:22 -0700730 return
731 }
Sean McCulloughedc88dc2025-04-30 02:55:01 +0000732 // Call the CancelTurn method
733 agent.CancelTurn(fmt.Errorf("%s", cancelReason))
Earl Lee2e463fb2025-04-17 11:22:22 -0700734 // Return a success response
735 w.Header().Set("Content-Type", "application/json")
736 json.NewEncoder(w).Encode(map[string]string{"status": "cancelled", "reason": cancelReason})
737 })
738
Pokey Rule397871d2025-05-19 15:02:45 +0100739 // Handler for /end - shuts down the inner sketch process
740 s.mux.HandleFunc("/end", func(w http.ResponseWriter, r *http.Request) {
741 if r.Method != http.MethodPost {
742 http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
743 return
744 }
745
746 // Parse the request body (optional)
747 var requestBody struct {
Philip Zeyligerb5739402025-06-02 07:04:34 -0700748 Reason string `json:"reason"`
749 Happy *bool `json:"happy,omitempty"`
750 Comment string `json:"comment,omitempty"`
Pokey Rule397871d2025-05-19 15:02:45 +0100751 }
752
753 decoder := json.NewDecoder(r.Body)
754 if err := decoder.Decode(&requestBody); err != nil && err != io.EOF {
755 http.Error(w, "Invalid request body: "+err.Error(), http.StatusBadRequest)
756 return
757 }
758 defer r.Body.Close()
759
760 endReason := "user requested end of session"
761 if requestBody.Reason != "" {
762 endReason = requestBody.Reason
763 }
764
765 // Send success response before exiting
766 w.Header().Set("Content-Type", "application/json")
767 json.NewEncoder(w).Encode(map[string]string{"status": "ending", "reason": endReason})
768 if f, ok := w.(http.Flusher); ok {
769 f.Flush()
770 }
771
772 // Log that we're shutting down
773 slog.Info("Ending session", "reason", endReason)
774
philip.zeyliger28e39ac2025-06-16 22:04:35 +0000775 // Give a brief moment for the response to be sent before exiting
Pokey Rule397871d2025-05-19 15:02:45 +0100776 go func() {
philip.zeyliger28e39ac2025-06-16 22:04:35 +0000777 time.Sleep(100 * time.Millisecond)
Pokey Rule397871d2025-05-19 15:02:45 +0100778 os.Exit(0)
779 }()
780 })
781
Earl Lee2e463fb2025-04-17 11:22:22 -0700782 debugMux := initDebugMux()
783 s.mux.HandleFunc("/debug/", func(w http.ResponseWriter, r *http.Request) {
784 debugMux.ServeHTTP(w, r)
785 })
786
787 return s, nil
788}
789
790// Utility functions
791func getHostname() string {
792 hostname, err := os.Hostname()
793 if err != nil {
794 return "unknown"
795 }
796 return hostname
797}
798
799func getWorkingDir() string {
800 wd, err := os.Getwd()
801 if err != nil {
802 return "unknown"
803 }
804 return wd
805}
806
807// createTerminalSession creates a new terminal session with the given ID
808func (s *Server) createTerminalSession(sessionID string) (*terminalSession, error) {
809 // Start a new shell process
810 shellPath := getShellPath()
811 cmd := exec.Command(shellPath)
812
813 // Get working directory from the agent if possible
814 workDir := getWorkingDir()
815 cmd.Dir = workDir
816
817 // Set up environment
818 cmd.Env = append(os.Environ(), "TERM=xterm-256color")
819
820 // Start the command with a pty
821 ptmx, err := pty.Start(cmd)
822 if err != nil {
823 slog.Error("Failed to start pty", "error", err)
824 return nil, err
825 }
826
827 // Create the terminal session
828 session := &terminalSession{
829 pty: ptmx,
830 eventsClients: make(map[chan []byte]bool),
831 cmd: cmd,
832 }
833
834 // Start goroutine to read from pty and broadcast to all connected SSE clients
835 go s.readFromPtyAndBroadcast(sessionID, session)
836
837 return session, nil
David Crawshawb8431462025-07-09 13:10:32 +1000838}
839
840// handleTerminalEvents handles SSE connections for terminal output
Earl Lee2e463fb2025-04-17 11:22:22 -0700841func (s *Server) handleTerminalEvents(w http.ResponseWriter, r *http.Request, sessionID string) {
842 // Check if the session exists, if not, create it
843 s.ptyMutex.Lock()
844 session, exists := s.terminalSessions[sessionID]
845
846 if !exists {
847 // Create a new terminal session
848 var err error
849 session, err = s.createTerminalSession(sessionID)
850 if err != nil {
851 s.ptyMutex.Unlock()
852 http.Error(w, fmt.Sprintf("Failed to create terminal: %v", err), http.StatusInternalServerError)
853 return
854 }
855
856 // Store the new session
857 s.terminalSessions[sessionID] = session
858 }
859 s.ptyMutex.Unlock()
860
861 // Set headers for SSE
862 w.Header().Set("Content-Type", "text/event-stream")
863 w.Header().Set("Cache-Control", "no-cache")
864 w.Header().Set("Connection", "keep-alive")
865 w.Header().Set("Access-Control-Allow-Origin", "*")
866
867 // Create a channel for this client
868 events := make(chan []byte, 4096) // Buffer to prevent blocking
869
870 // Register this client's channel
871 session.eventsClientsMutex.Lock()
872 clientID := session.lastEventClientID + 1
873 session.lastEventClientID = clientID
874 session.eventsClients[events] = true
875 session.eventsClientsMutex.Unlock()
876
877 // When the client disconnects, remove their channel
878 defer func() {
879 session.eventsClientsMutex.Lock()
880 delete(session.eventsClients, events)
881 close(events)
882 session.eventsClientsMutex.Unlock()
883 }()
884
885 // Flush to send headers to client immediately
886 if f, ok := w.(http.Flusher); ok {
887 f.Flush()
888 }
889
890 // Send events to the client as they arrive
891 for {
892 select {
893 case <-r.Context().Done():
894 return
895 case data := <-events:
896 // Format as SSE with base64 encoding
897 fmt.Fprintf(w, "data: %s\n\n", base64.StdEncoding.EncodeToString(data))
898
899 // Flush the data immediately
900 if f, ok := w.(http.Flusher); ok {
901 f.Flush()
902 }
903 }
904 }
905}
906
907// handleTerminalInput processes input to the terminal
908func (s *Server) handleTerminalInput(w http.ResponseWriter, r *http.Request, sessionID string) {
909 // Check if the session exists
910 s.ptyMutex.Lock()
911 session, exists := s.terminalSessions[sessionID]
912 s.ptyMutex.Unlock()
913
914 if !exists {
915 http.Error(w, "Terminal session not found", http.StatusNotFound)
916 return
917 }
918
919 // Read the request body (terminal input or resize command)
920 body, err := io.ReadAll(r.Body)
921 if err != nil {
922 http.Error(w, "Failed to read request body", http.StatusBadRequest)
923 return
924 }
925
926 // Check if it's a resize message
927 if len(body) > 0 && body[0] == '{' {
928 var msg TerminalMessage
929 if err := json.Unmarshal(body, &msg); err == nil && msg.Type == "resize" {
930 if msg.Cols > 0 && msg.Rows > 0 {
931 pty.Setsize(session.pty, &pty.Winsize{
932 Cols: msg.Cols,
933 Rows: msg.Rows,
934 })
935
936 // Respond with success
937 w.WriteHeader(http.StatusOK)
938 return
939 }
940 }
941 }
942
943 // Regular terminal input
944 _, err = session.pty.Write(body)
945 if err != nil {
946 slog.Error("Failed to write to pty", "error", err)
947 http.Error(w, "Failed to write to terminal", http.StatusInternalServerError)
948 return
949 }
950
951 // Respond with success
952 w.WriteHeader(http.StatusOK)
953}
954
955// readFromPtyAndBroadcast reads output from the PTY and broadcasts it to all connected clients
956func (s *Server) readFromPtyAndBroadcast(sessionID string, session *terminalSession) {
957 buf := make([]byte, 4096)
958 defer func() {
959 // Clean up when done
960 s.ptyMutex.Lock()
961 delete(s.terminalSessions, sessionID)
962 s.ptyMutex.Unlock()
963
964 // Close the PTY
965 session.pty.Close()
966
967 // Ensure process is terminated
968 if session.cmd.Process != nil {
Earl Lee2e463fb2025-04-17 11:22:22 -0700969 session.cmd.Process.Kill()
970 }
David Crawshawb8431462025-07-09 13:10:32 +1000971 session.cmd.Wait()
Earl Lee2e463fb2025-04-17 11:22:22 -0700972
973 // Close all client channels
974 session.eventsClientsMutex.Lock()
975 for ch := range session.eventsClients {
976 delete(session.eventsClients, ch)
977 close(ch)
978 }
979 session.eventsClientsMutex.Unlock()
980 }()
981
982 for {
983 n, err := session.pty.Read(buf)
984 if err != nil {
985 if err != io.EOF {
986 slog.Error("Failed to read from pty", "error", err)
987 }
988 break
989 }
990
991 // Make a copy of the data for each client
992 data := make([]byte, n)
993 copy(data, buf[:n])
994
995 // Broadcast to all connected clients
996 session.eventsClientsMutex.Lock()
997 for ch := range session.eventsClients {
998 // Try to send, but don't block if channel is full
999 select {
1000 case ch <- data:
1001 default:
1002 // Channel is full, drop the message for this client
1003 }
1004 }
1005 session.eventsClientsMutex.Unlock()
1006 }
1007}
1008
1009// getShellPath returns the path to the shell to use
1010func getShellPath() string {
1011 // Try to use the user's preferred shell
1012 shell := os.Getenv("SHELL")
1013 if shell != "" {
1014 return shell
1015 }
1016
1017 // Default to bash on Unix-like systems
1018 if _, err := os.Stat("/bin/bash"); err == nil {
1019 return "/bin/bash"
1020 }
1021
1022 // Fall back to sh
1023 return "/bin/sh"
1024}
1025
1026func initDebugMux() *http.ServeMux {
1027 mux := http.NewServeMux()
Philip Zeyliger8d8b7ac2025-05-21 09:57:23 -07001028 build := "unknown build"
1029 bi, ok := debug.ReadBuildInfo()
1030 if ok {
1031 build = fmt.Sprintf("%s@%v\n", bi.Path, bi.Main.Version)
1032 }
Earl Lee2e463fb2025-04-17 11:22:22 -07001033 mux.HandleFunc("GET /debug/{$}", func(w http.ResponseWriter, r *http.Request) {
1034 w.Header().Set("Content-Type", "text/html; charset=utf-8")
Philip Zeyliger2c4db092025-04-28 16:57:50 -07001035 // TODO: pid is not as useful as "outside pid"
Earl Lee2e463fb2025-04-17 11:22:22 -07001036 fmt.Fprintf(w, `<!doctype html>
1037 <html><head><title>sketch debug</title></head><body>
1038 <h1>sketch debug</h1>
Philip Zeyliger8d8b7ac2025-05-21 09:57:23 -07001039 pid %d<br>
1040 build %s<br>
Earl Lee2e463fb2025-04-17 11:22:22 -07001041 <ul>
Philip Zeyligera14b0182025-06-30 14:31:18 -07001042 <li><a href="pprof/cmdline">pprof/cmdline</a></li>
1043 <li><a href="pprof/profile">pprof/profile</a></li>
1044 <li><a href="pprof/symbol">pprof/symbol</a></li>
1045 <li><a href="pprof/trace">pprof/trace</a></li>
1046 <li><a href="pprof/goroutine?debug=1">pprof/goroutine?debug=1</a></li>
Earl Lee2e463fb2025-04-17 11:22:22 -07001047 </ul>
1048 </body>
1049 </html>
Philip Zeyliger8d8b7ac2025-05-21 09:57:23 -07001050 `, os.Getpid(), build)
Earl Lee2e463fb2025-04-17 11:22:22 -07001051 })
1052 mux.HandleFunc("GET /debug/pprof/", pprof.Index)
1053 mux.HandleFunc("GET /debug/pprof/cmdline", pprof.Cmdline)
1054 mux.HandleFunc("GET /debug/pprof/profile", pprof.Profile)
1055 mux.HandleFunc("GET /debug/pprof/symbol", pprof.Symbol)
1056 mux.HandleFunc("GET /debug/pprof/trace", pprof.Trace)
1057 return mux
1058}
1059
1060// isValidGitSHA validates if a string looks like a valid git SHA hash.
1061// Git SHAs are hexadecimal strings of at least 4 characters but typically 7, 8, or 40 characters.
1062func isValidGitSHA(sha string) bool {
1063 // Git SHA must be a hexadecimal string with at least 4 characters
1064 if len(sha) < 4 || len(sha) > 40 {
1065 return false
1066 }
1067
1068 // Check if the string only contains hexadecimal characters
1069 for _, char := range sha {
1070 if !(char >= '0' && char <= '9') && !(char >= 'a' && char <= 'f') && !(char >= 'A' && char <= 'F') {
1071 return false
1072 }
1073 }
1074
1075 return true
1076}
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001077
1078// /stream?from=N endpoint for Server-Sent Events
1079func (s *Server) handleSSEStream(w http.ResponseWriter, r *http.Request) {
1080 w.Header().Set("Content-Type", "text/event-stream")
1081 w.Header().Set("Cache-Control", "no-cache")
1082 w.Header().Set("Connection", "keep-alive")
1083 w.Header().Set("Access-Control-Allow-Origin", "*")
1084
1085 // Extract the 'from' parameter
1086 fromParam := r.URL.Query().Get("from")
1087 var fromIndex int
1088 var err error
1089 if fromParam != "" {
1090 fromIndex, err = strconv.Atoi(fromParam)
1091 if err != nil {
1092 http.Error(w, "Invalid 'from' parameter", http.StatusBadRequest)
1093 return
1094 }
1095 }
1096
1097 // Ensure 'from' is valid
1098 currentCount := s.agent.MessageCount()
1099 if fromIndex < 0 {
1100 fromIndex = 0
1101 } else if fromIndex > currentCount {
1102 fromIndex = currentCount
1103 }
1104
1105 // Send the current state immediately
1106 state := s.getState()
1107
1108 // Create JSON encoder
1109 encoder := json.NewEncoder(w)
1110
1111 // Send state as an event
1112 fmt.Fprintf(w, "event: state\n")
1113 fmt.Fprintf(w, "data: ")
1114 encoder.Encode(state)
1115 fmt.Fprintf(w, "\n\n")
1116
1117 if f, ok := w.(http.Flusher); ok {
1118 f.Flush()
1119 }
1120
1121 // Create a context for the SSE stream
1122 ctx := r.Context()
1123
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001124 // Setup heartbeat timer
1125 heartbeatTicker := time.NewTicker(45 * time.Second)
1126 defer heartbeatTicker.Stop()
1127
1128 // Create a channel for messages
1129 messageChan := make(chan *loop.AgentMessage, 10)
1130
Philip Zeyligereab12de2025-05-14 02:35:53 +00001131 // Create a channel for state transitions
1132 stateChan := make(chan *loop.StateTransition, 10)
1133
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001134 // Start a goroutine to read messages without blocking the heartbeat
1135 go func() {
Pokey Rule9d7f0cc2025-05-20 11:43:26 +01001136 // Create an iterator to receive new messages as they arrive
1137 iterator := s.agent.NewIterator(ctx, fromIndex) // Start from the requested index
1138 defer iterator.Close()
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001139 defer close(messageChan)
1140 for {
1141 // This can block, but it's in its own goroutine
1142 newMessage := iterator.Next()
1143 if newMessage == nil {
1144 // No message available (likely due to context cancellation)
1145 slog.InfoContext(ctx, "No more messages available, ending message stream")
1146 return
1147 }
1148
1149 select {
1150 case messageChan <- newMessage:
1151 // Message sent to channel
1152 case <-ctx.Done():
1153 // Context cancelled
1154 return
1155 }
1156 }
1157 }()
1158
Philip Zeyligereab12de2025-05-14 02:35:53 +00001159 // Start a goroutine to read state transitions
1160 go func() {
Pokey Rule9d7f0cc2025-05-20 11:43:26 +01001161 // Create an iterator to receive state transitions
1162 stateIterator := s.agent.NewStateTransitionIterator(ctx)
1163 defer stateIterator.Close()
Philip Zeyligereab12de2025-05-14 02:35:53 +00001164 defer close(stateChan)
1165 for {
1166 // This can block, but it's in its own goroutine
1167 newTransition := stateIterator.Next()
1168 if newTransition == nil {
1169 // No transition available (likely due to context cancellation)
1170 slog.InfoContext(ctx, "No more state transitions available, ending state stream")
1171 return
1172 }
1173
1174 select {
1175 case stateChan <- newTransition:
1176 // Transition sent to channel
1177 case <-ctx.Done():
1178 // Context cancelled
1179 return
1180 }
1181 }
1182 }()
1183
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001184 // Stay connected and stream real-time updates
1185 for {
1186 select {
1187 case <-heartbeatTicker.C:
1188 // Send heartbeat event
1189 fmt.Fprintf(w, "event: heartbeat\n")
1190 fmt.Fprintf(w, "data: %d\n\n", time.Now().Unix())
1191
1192 // Flush to send the heartbeat immediately
1193 if f, ok := w.(http.Flusher); ok {
1194 f.Flush()
1195 }
1196
1197 case <-ctx.Done():
1198 // Client disconnected
1199 slog.InfoContext(ctx, "Client disconnected from SSE stream")
1200 return
1201
Philip Zeyligereab12de2025-05-14 02:35:53 +00001202 case _, ok := <-stateChan:
1203 if !ok {
1204 // Channel closed
1205 slog.InfoContext(ctx, "State transition channel closed, ending SSE stream")
1206 return
1207 }
1208
1209 // Get updated state
1210 state = s.getState()
1211
1212 // Send updated state after the state transition
1213 fmt.Fprintf(w, "event: state\n")
1214 fmt.Fprintf(w, "data: ")
1215 encoder.Encode(state)
1216 fmt.Fprintf(w, "\n\n")
1217
1218 // Flush to send the state immediately
1219 if f, ok := w.(http.Flusher); ok {
1220 f.Flush()
1221 }
1222
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001223 case newMessage, ok := <-messageChan:
1224 if !ok {
1225 // Channel closed
1226 slog.InfoContext(ctx, "Message channel closed, ending SSE stream")
1227 return
1228 }
1229
1230 // Send the new message as an event
1231 fmt.Fprintf(w, "event: message\n")
1232 fmt.Fprintf(w, "data: ")
1233 encoder.Encode(newMessage)
1234 fmt.Fprintf(w, "\n\n")
1235
1236 // Get updated state
1237 state = s.getState()
1238
1239 // Send updated state after the message
1240 fmt.Fprintf(w, "event: state\n")
1241 fmt.Fprintf(w, "data: ")
1242 encoder.Encode(state)
1243 fmt.Fprintf(w, "\n\n")
1244
1245 // Flush to send the message and state immediately
1246 if f, ok := w.(http.Flusher); ok {
1247 f.Flush()
1248 }
1249 }
1250 }
1251}
1252
1253// Helper function to get the current state
1254func (s *Server) getState() State {
1255 serverMessageCount := s.agent.MessageCount()
1256 totalUsage := s.agent.TotalUsage()
1257
Philip Zeyliger64f60462025-06-16 13:57:10 -07001258 // Get diff stats
1259 diffAdded, diffRemoved := s.agent.DiffStats()
1260
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001261 return State{
Philip Zeyliger49edc922025-05-14 09:45:45 -07001262 StateVersion: 2,
1263 MessageCount: serverMessageCount,
1264 TotalUsage: &totalUsage,
1265 Hostname: s.hostname,
1266 WorkingDir: getWorkingDir(),
1267 // TODO: Rename this field to sketch-base?
1268 InitialCommit: s.agent.SketchGitBase(),
Josh Bleecher Snyder19969a92025-06-05 14:34:02 -07001269 Slug: s.agent.Slug(),
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001270 BranchName: s.agent.BranchName(),
Philip Zeyligerbe7802a2025-06-04 20:15:25 +00001271 BranchPrefix: s.agent.BranchPrefix(),
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001272 OS: s.agent.OS(),
1273 OutsideHostname: s.agent.OutsideHostname(),
1274 InsideHostname: s.hostname,
1275 OutsideOS: s.agent.OutsideOS(),
1276 InsideOS: s.agent.OS(),
1277 OutsideWorkingDir: s.agent.OutsideWorkingDir(),
1278 InsideWorkingDir: getWorkingDir(),
1279 GitOrigin: s.agent.GitOrigin(),
bankseancad67b02025-06-27 21:57:05 +00001280 GitUsername: s.agent.GitUsername(),
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001281 OutstandingLLMCalls: s.agent.OutstandingLLMCallCount(),
1282 OutstandingToolCalls: s.agent.OutstandingToolCalls(),
1283 SessionID: s.agent.SessionID(),
1284 SSHAvailable: s.sshAvailable,
1285 SSHError: s.sshError,
1286 InContainer: s.agent.IsInContainer(),
1287 FirstMessageIndex: s.agent.FirstMessageIndex(),
1288 AgentState: s.agent.CurrentStateName(),
Josh Bleecher Snyder112b9232025-05-23 11:26:33 -07001289 TodoContent: s.agent.CurrentTodoContent(),
Philip Zeyliger0113be52025-06-07 23:53:41 +00001290 SkabandAddr: s.agent.SkabandAddr(),
philip.zeyliger6d3de482025-06-10 19:38:14 -07001291 LinkToGitHub: s.agent.LinkToGitHub(),
philip.zeyliger8773e682025-06-11 21:36:21 -07001292 SSHConnectionString: s.agent.SSHConnectionString(),
Philip Zeyliger64f60462025-06-16 13:57:10 -07001293 DiffLinesAdded: diffAdded,
1294 DiffLinesRemoved: diffRemoved,
Philip Zeyliger5f26a342025-07-04 01:30:29 +00001295 OpenPorts: s.getOpenPorts(),
Philip Zeyliger25f6ff12025-05-02 04:24:10 +00001296 }
1297}
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001298
Philip Zeyliger5f26a342025-07-04 01:30:29 +00001299// getOpenPorts retrieves the current open ports from the agent
1300func (s *Server) getOpenPorts() []Port {
1301 ports := s.agent.GetPorts()
1302 if ports == nil {
1303 return nil
1304 }
1305
1306 result := make([]Port, len(ports))
1307 for i, port := range ports {
1308 result[i] = Port{
1309 Proto: port.Proto,
1310 Port: port.Port,
1311 Process: port.Process,
1312 Pid: port.Pid,
1313 }
1314 }
1315 return result
1316}
1317
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001318func (s *Server) handleGitRawDiff(w http.ResponseWriter, r *http.Request) {
1319 if r.Method != "GET" {
1320 w.WriteHeader(http.StatusMethodNotAllowed)
1321 return
1322 }
1323
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +00001324 // Get the git repository root directory from agent
1325 repoDir := s.agent.RepoRoot()
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001326
1327 // Parse query parameters
1328 query := r.URL.Query()
1329 commit := query.Get("commit")
1330 from := query.Get("from")
1331 to := query.Get("to")
1332
1333 // If commit is specified, use commit^ and commit as from and to
1334 if commit != "" {
1335 from = commit + "^"
1336 to = commit
1337 }
1338
1339 // Check if we have enough parameters
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001340 if from == "" {
1341 http.Error(w, "Missing required parameter: either 'commit' or at least 'from'", http.StatusBadRequest)
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001342 return
1343 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001344 // Note: 'to' can be empty to indicate working directory (unstaged changes)
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001345
1346 // Call the git_tools function
1347 diff, err := git_tools.GitRawDiff(repoDir, from, to)
1348 if err != nil {
1349 http.Error(w, fmt.Sprintf("Error getting git diff: %v", err), http.StatusInternalServerError)
1350 return
1351 }
1352
1353 // Return the result as JSON
1354 w.Header().Set("Content-Type", "application/json")
1355 if err := json.NewEncoder(w).Encode(diff); err != nil {
1356 http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError)
1357 return
1358 }
1359}
1360
1361func (s *Server) handleGitShow(w http.ResponseWriter, r *http.Request) {
1362 if r.Method != "GET" {
1363 w.WriteHeader(http.StatusMethodNotAllowed)
1364 return
1365 }
1366
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +00001367 // Get the git repository root directory from agent
1368 repoDir := s.agent.RepoRoot()
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001369
1370 // Parse query parameters
1371 hash := r.URL.Query().Get("hash")
1372 if hash == "" {
1373 http.Error(w, "Missing required parameter: 'hash'", http.StatusBadRequest)
1374 return
1375 }
1376
1377 // Call the git_tools function
1378 show, err := git_tools.GitShow(repoDir, hash)
1379 if err != nil {
1380 http.Error(w, fmt.Sprintf("Error running git show: %v", err), http.StatusInternalServerError)
1381 return
1382 }
1383
1384 // Create a JSON response
1385 response := map[string]string{
1386 "hash": hash,
1387 "output": show,
1388 }
1389
1390 // Return the result as JSON
1391 w.Header().Set("Content-Type", "application/json")
1392 if err := json.NewEncoder(w).Encode(response); err != nil {
1393 http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError)
1394 return
1395 }
1396}
1397
1398func (s *Server) handleGitRecentLog(w http.ResponseWriter, r *http.Request) {
1399 if r.Method != "GET" {
1400 w.WriteHeader(http.StatusMethodNotAllowed)
1401 return
1402 }
1403
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +00001404 // Get the git repository root directory and initial commit from agent
1405 repoDir := s.agent.RepoRoot()
Philip Zeyligerd3ac1122025-05-14 02:54:18 +00001406 initialCommit := s.agent.SketchGitBaseRef()
1407
1408 // Call the git_tools function
1409 log, err := git_tools.GitRecentLog(repoDir, initialCommit)
1410 if err != nil {
1411 http.Error(w, fmt.Sprintf("Error getting git log: %v", err), http.StatusInternalServerError)
1412 return
1413 }
1414
1415 // Return the result as JSON
1416 w.Header().Set("Content-Type", "application/json")
1417 if err := json.NewEncoder(w).Encode(log); err != nil {
1418 http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError)
1419 return
1420 }
1421}
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001422
1423func (s *Server) handleGitCat(w http.ResponseWriter, r *http.Request) {
1424 if r.Method != "GET" {
1425 w.WriteHeader(http.StatusMethodNotAllowed)
1426 return
1427 }
1428
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +00001429 // Get the git repository root directory from agent
1430 repoDir := s.agent.RepoRoot()
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001431
1432 // Parse query parameters
1433 query := r.URL.Query()
1434 path := query.Get("path")
1435
1436 // Check if path is provided
1437 if path == "" {
1438 http.Error(w, "Missing required parameter: path", http.StatusBadRequest)
1439 return
1440 }
1441
1442 // Get file content using GitCat
1443 content, err := git_tools.GitCat(repoDir, path)
Josh Bleecher Snyder5c29b3e2025-07-08 18:07:28 +00001444 if errors.Is(err, os.ErrNotExist) {
1445 w.WriteHeader(http.StatusNoContent)
1446 return
1447 }
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001448 if err != nil {
1449 http.Error(w, fmt.Sprintf("Error reading file: %v", err), http.StatusInternalServerError)
1450 return
1451 }
1452
1453 // Return the content as JSON for consistency with other endpoints
1454 w.Header().Set("Content-Type", "application/json")
1455 if err := json.NewEncoder(w).Encode(map[string]string{"output": content}); err != nil {
1456 http.Error(w, fmt.Sprintf("Error encoding response: %v", err), http.StatusInternalServerError)
1457 return
1458 }
1459}
1460
1461func (s *Server) handleGitSave(w http.ResponseWriter, r *http.Request) {
1462 if r.Method != "POST" {
1463 w.WriteHeader(http.StatusMethodNotAllowed)
1464 return
1465 }
1466
Josh Bleecher Snyderc5848f32025-05-28 18:50:58 +00001467 // Get the git repository root directory from agent
1468 repoDir := s.agent.RepoRoot()
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001469
1470 // Parse request body
1471 var requestBody struct {
1472 Path string `json:"path"`
1473 Content string `json:"content"`
1474 }
1475
1476 if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil {
1477 http.Error(w, fmt.Sprintf("Error parsing request body: %v", err), http.StatusBadRequest)
1478 return
1479 }
1480 defer r.Body.Close()
1481
1482 // Check if path is provided
1483 if requestBody.Path == "" {
1484 http.Error(w, "Missing required parameter: path", http.StatusBadRequest)
1485 return
1486 }
1487
1488 // Save file content using GitSaveFile
1489 err := git_tools.GitSaveFile(repoDir, requestBody.Path, requestBody.Content)
1490 if err != nil {
1491 http.Error(w, fmt.Sprintf("Error saving file: %v", err), http.StatusInternalServerError)
1492 return
1493 }
1494
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001495 // Auto-commit the changes
1496 err = git_tools.AutoCommitDiffViewChanges(r.Context(), repoDir, requestBody.Path)
1497 if err != nil {
1498 http.Error(w, fmt.Sprintf("Error auto-committing changes: %v", err), http.StatusInternalServerError)
1499 return
1500 }
1501
1502 // Detect git changes to push and notify user
Philip Zeyliger9bca61e2025-05-22 12:40:06 -07001503 if err = s.agent.DetectGitChanges(r.Context()); err != nil {
1504 http.Error(w, fmt.Sprintf("Error detecting git changes: %v", err), http.StatusInternalServerError)
1505 return
1506 }
Philip Zeyliger75bd37d2025-05-22 18:49:14 +00001507
Philip Zeyliger272a90e2025-05-16 14:49:51 -07001508 // Return simple success response
1509 w.WriteHeader(http.StatusOK)
1510 w.Write([]byte("ok"))
1511}