Adds GET /api/workspaces/:id/acp/stream. Client opens a WebSocket,
sends {"type":"prompt","content":"..."}, and receives a stream of
{"type":"chunk","messageId","text"} events followed by exactly
one {"type":"complete","stopReason"} or {"type":"error","error"}.
Closing the WS early triggers session/cancel.
- internal/acp/messages.go: StreamEvent wire shape.
- internal/acp/client.go:
- streamChs []chan StreamEvent set; AddStream / RemoveStream.
- sendStream non-blocking fanout.
- Client.Stream(ctx, content, out) registers out, sends prompt,
emits complete/error after the prompt response, unregisters.
- handleNotification fans chunk events to all stream consumers.
- notifyWG ensures chunk ordering vs the terminal event.
- internal/acp/service.go: Service.Stream(workspaceID, content, out)
mirrors Prompt (per-workspace lock, 5-min timeout, EnsureReady).
- internal/service/acp_service.go: thin AcpService.Stream wrapper
that maps acp.StreamEvent -> model.AcpStreamEvent.
- internal/model/acp.go: AcpStreamRequest, AcpStreamEvent DTOs.
- internal/api/acp_handler.go: stream WS handler (upgrade, read
prompt, run Stream in a goroutine, write events, ping/pong, Cancel
on client close).
- internal/api/router.go: register the new route.
- internal/acp/transport.go: dispatch notifications synchronously
(vs. goroutine per notification) so chunks preserve order before
the session/prompt response.
- internal/acp/client_test.go: TestClientStreamEmitsChunkAndComplete
with a fake transport that drives a known sequence.
- internal/api/acp_handler_test.go: TestAcpStreamHandlerRoutes
smoke test using a fake opencode acp script.
Existing POST /api/workspaces/:id/acp/prompt is unchanged.
E2E: prompt 'say hi in exactly 3 words' -> 3 chunk events
('Hi',' there','!') + 1 complete {stopReason: 'end_turn'}.
Conversation: 019f3680-200f-79b0-860b-43302e60d0ea
66 lines
2.4 KiB
Go
66 lines
2.4 KiB
Go
package api
|
|
|
|
import (
|
|
"log/slog"
|
|
"net/http"
|
|
|
|
"codespace/internal/service"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// NewRouter builds a Gin engine with all API routes registered.
|
|
func NewRouter(workspaces *service.WorkspaceService, files *service.FileService, processes *service.ProcessService, shells *service.ShellService, acpSvc *service.AcpService, lg *slog.Logger, ginMode string) *gin.Engine {
|
|
gin.SetMode(ginMode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
r.Use(requestLogger(lg))
|
|
|
|
wsHandler := &workspaceHandler{svc: workspaces}
|
|
fileHandler := &fileHandler{svc: files}
|
|
procHandler := &processHandler{svc: processes}
|
|
shellHandler := &shellHandler{svc: shells}
|
|
acpHandler := &acpHandler{svc: acpSvc}
|
|
|
|
r.GET("/healthz", healthHandler)
|
|
|
|
api := r.Group("/api")
|
|
api.GET("/workspaces", wsHandler.list)
|
|
api.POST("/workspaces", wsHandler.create)
|
|
api.GET("/workspaces/:id", wsHandler.get)
|
|
api.DELETE("/workspaces/:id", wsHandler.delete)
|
|
|
|
api.GET("/workspaces/:id/files", fileHandler.list)
|
|
api.GET("/workspaces/:id/files/read", fileHandler.read)
|
|
api.GET("/workspaces/:id/files/stat", fileHandler.stat)
|
|
api.PUT("/workspaces/:id/files/write", fileHandler.write)
|
|
api.POST("/workspaces/:id/files/mkdir", fileHandler.mkdir)
|
|
api.DELETE("/workspaces/:id/files", fileHandler.remove)
|
|
api.POST("/workspaces/:id/files/rename", fileHandler.rename)
|
|
|
|
api.POST("/workspaces/:id/process/start", procHandler.start)
|
|
api.POST("/workspaces/:id/process/stop", procHandler.stop)
|
|
api.POST("/workspaces/:id/process/restart", procHandler.restart)
|
|
api.GET("/workspaces/:id/process/status", procHandler.status)
|
|
api.GET("/workspaces/:id/process/ws", procHandler.ws)
|
|
|
|
api.POST("/workspaces/:id/shell/start", shellHandler.start)
|
|
api.POST("/workspaces/:id/shell/stop", shellHandler.stop)
|
|
api.POST("/workspaces/:id/shell/restart", shellHandler.restart)
|
|
api.GET("/workspaces/:id/shell", shellHandler.list)
|
|
api.GET("/workspaces/:id/shell/status", shellHandler.status)
|
|
api.GET("/workspaces/:id/shell/ws", shellHandler.ws)
|
|
api.POST("/workspaces/:id/shell/resize", shellHandler.resize)
|
|
api.GET("/workspaces/:id/acp/status", acpHandler.status)
|
|
api.GET("/workspaces/:id/acp/history", acpHandler.history)
|
|
api.POST("/workspaces/:id/acp/prompt", acpHandler.prompt)
|
|
api.POST("/workspaces/:id/acp/cancel", acpHandler.cancel)
|
|
api.GET("/workspaces/:id/acp/stream", acpHandler.stream)
|
|
|
|
return r
|
|
}
|
|
|
|
func healthHandler(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
}
|