This repository has been archived on 2026-07-17. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
codespace/internal/api/acp_handler.go
T
tao.chen 2597d0d60c 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
2026-07-06 11:03:59 +08:00

70 lines
1.3 KiB
Go

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)
}