feat(backend): add opencode ACP (Agent Client Protocol) client

Adds a minimal but real ACP stack for the opencode process:

- pkg/config: process.args default ["acp"] (opencodeCommand still "opencode")
- internal/process: NewManager(command, args) — exec.Command uses args
- internal/acp (new): NDJSON transport + JSON-RPC client over the existing
  process stdio. Implements initialize / session/new / session/prompt /
  session/cancel. Serves fs/read_text_file and fs/write_text_file from the
  workspace's fs.FileSystem. terminal/* requests get MethodNotFound.
- internal/service/acp_service: per-workspace Client + mutex; starts the
  process on first prompt; transparently re-init on restart.
- internal/api/acp_handler: GET /acp/status, GET /acp/history,
  POST /acp/prompt, POST /acp/cancel.
- internal/model/acp: API DTOs.
- internal/acp/client_test: NDJSON split-lines, request/response correlation,
  notification dispatch, agent-initiated request handling (fs + terminal).

Existing process WS endpoint and Shell subsystem are unchanged.

Conversation: 019f354c-a51b-7ec3-83ad-c647e9b50b19
This commit is contained in:
tao.chen
2026-07-06 11:03:59 +08:00
parent 18cb3a0151
commit 2597d0d60c
17 changed files with 1352 additions and 17 deletions
+69
View File
@@ -0,0 +1,69 @@
package api
import (
"net/http"
"codespace/internal/model"
"codespace/internal/service"
"github.com/gin-gonic/gin"
)
type acpHandler struct {
svc *service.AcpService
}
func (h *acpHandler) status(c *gin.Context) {
id := c.Param("id")
status, err := h.svc.Status(id)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, model.AcpStatusResponse{
WorkspaceID: status.WorkspaceID,
Ready: status.Ready,
SessionID: status.SessionID,
Running: status.Running,
PID: status.PID,
Error: status.Error,
})
}
func (h *acpHandler) history(c *gin.Context) {
id := c.Param("id")
hist, err := h.svc.History(id)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, hist)
}
func (h *acpHandler) prompt(c *gin.Context) {
id := c.Param("id")
var req model.AcpPromptRequest
if err := c.ShouldBindJSON(&req); err != nil {
writeBadRequest(c, "invalid json")
return
}
if req.Content == "" {
writeBadRequest(c, "content is required")
return
}
res, err := h.svc.Prompt(id, req.Content)
if err != nil {
writeError(c, err)
return
}
c.JSON(http.StatusOK, res)
}
func (h *acpHandler) cancel(c *gin.Context) {
id := c.Param("id")
if err := h.svc.Cancel(id); err != nil {
writeError(c, err)
return
}
c.Status(http.StatusNoContent)
}
+6 -4
View File
@@ -29,14 +29,15 @@ func TestProcessWS(t *testing.T) {
wsRoot := filepath.Join(tmpDir, "workspaces")
wsMgr := workspace.NewLocalManager(wsRoot)
procMgr := process.NewManager(opencodePath)
procMgr := process.NewManager(opencodePath, nil)
shellMgr := shell.NewManager("bash", []string{"-i"})
wsSvc := service.NewWorkspaceService(wsMgr, procMgr, shellMgr, nil)
fileSvc := service.NewFileService(wsMgr, 1<<20)
procSvc := service.NewProcessService(wsMgr, procMgr, nil)
shellSvc := service.NewShellService(wsMgr, shellMgr, nil)
acpSvc := service.NewAcpService(procMgr, wsMgr, nil)
r := NewRouter(wsSvc, fileSvc, procSvc, shellSvc, nil, gin.TestMode)
r := NewRouter(wsSvc, fileSvc, procSvc, shellSvc, acpSvc, nil, gin.TestMode)
srv := httptest.NewServer(r)
defer srv.Close()
@@ -104,14 +105,15 @@ func TestProcessWSMultiSubscriber(t *testing.T) {
wsRoot := filepath.Join(tmpDir, "workspaces")
wsMgr := workspace.NewLocalManager(wsRoot)
procMgr := process.NewManager(opencodePath)
procMgr := process.NewManager(opencodePath, nil)
shellMgr := shell.NewManager("bash", []string{"-i"})
wsSvc := service.NewWorkspaceService(wsMgr, procMgr, shellMgr, nil)
fileSvc := service.NewFileService(wsMgr, 1<<20)
procSvc := service.NewProcessService(wsMgr, procMgr, nil)
shellSvc := service.NewShellService(wsMgr, shellMgr, nil)
acpSvc := service.NewAcpService(procMgr, wsMgr, nil)
r := NewRouter(wsSvc, fileSvc, procSvc, shellSvc, nil, gin.TestMode)
r := NewRouter(wsSvc, fileSvc, procSvc, shellSvc, acpSvc, nil, gin.TestMode)
srv := httptest.NewServer(r)
defer srv.Close()
+6 -1
View File
@@ -10,7 +10,7 @@ import (
)
// NewRouter builds a Gin engine with all API routes registered.
func NewRouter(workspaces *service.WorkspaceService, files *service.FileService, processes *service.ProcessService, shells *service.ShellService, lg *slog.Logger, ginMode string) *gin.Engine {
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())
@@ -20,6 +20,7 @@ func NewRouter(workspaces *service.WorkspaceService, files *service.FileService,
fileHandler := &fileHandler{svc: files}
procHandler := &processHandler{svc: processes}
shellHandler := &shellHandler{svc: shells}
acpHandler := &acpHandler{svc: acpSvc}
r.GET("/healthz", healthHandler)
@@ -48,6 +49,10 @@ func NewRouter(workspaces *service.WorkspaceService, files *service.FileService,
api.POST("/workspaces/:id/shell/restart", shellHandler.restart)
api.GET("/workspaces/:id/shell/status", shellHandler.status)
api.GET("/workspaces/:id/shell/ws", shellHandler.ws)
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)
return r
}
+3 -2
View File
@@ -24,13 +24,14 @@ func setupTestRouterWithMaxWriteBytes(t *testing.T, maxWriteBytes int64) http.Ha
t.Helper()
root := t.TempDir()
wsMgr := workspace.NewLocalManager(root)
procMgr := process.NewManager("")
procMgr := process.NewManager("", nil)
shellMgr := shell.NewManager("bash", []string{"-i"})
wsSvc := service.NewWorkspaceService(wsMgr, procMgr, shellMgr, nil)
fileSvc := service.NewFileService(wsMgr, maxWriteBytes)
procSvc := service.NewProcessService(wsMgr, procMgr, nil)
shellSvc := service.NewShellService(wsMgr, shellMgr, nil)
return NewRouter(wsSvc, fileSvc, procSvc, shellSvc, nil, gin.TestMode)
acpSvc := service.NewAcpService(procMgr, wsMgr, nil)
return NewRouter(wsSvc, fileSvc, procSvc, shellSvc, acpSvc, nil, gin.TestMode)
}
func createWorkspaceForTest(t *testing.T, router http.Handler, id string) {