The opencode acp binary streams agent_message_chunk notifications with an EMPTY messageId. The backend was already correct (it groups when a messageId is present), but the UI rendered every chunk as its own bubble, so a single 'count 1 to 5' reply showed as 9 separate bubbles. Backend: - internal/acp/history.go: add MessageID to Message; new AppendOrCreate method that finds or creates an entry by messageId and appends text to it (preserving the original timestamp). - internal/acp/client.go: agent_message_chunk and user_message_chunk use AppendOrCreate. - internal/model/acp.go: expose MessageID in the wire response. - internal/service/acp_service.go: copy MessageID into the API DTO. - internal/acp/client_test.go: add TestHistoryAppendOrCreateGroupsByMessageID. Frontend: - web/src/components/acp/groupMessages.ts: new pure helper that coalesces consecutive same-role history entries into a single bubble (keeps the first chunk's time + messageId). - web/src/components/acp/AcpPanel.tsx: render coalesced messages; auto-scroll effect now depends on coalesced so it fires as the agent streams. Verified via e2e: 'count from 1 to 5' now returns a single agent entry with text '1\n2\n3\n4\n5' instead of 9 separate entries.
278 lines
7.4 KiB
Go
278 lines
7.4 KiB
Go
package acp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"log/slog"
|
|
"strings"
|
|
"sync"
|
|
|
|
"codespace/internal/fs"
|
|
"codespace/internal/process"
|
|
"codespace/internal/util"
|
|
)
|
|
|
|
// PromptResult is the outcome of a single prompt turn.
|
|
type PromptResult struct {
|
|
StopReason string
|
|
Text string
|
|
}
|
|
|
|
// Client manages one ACP session for a single workspace process.
|
|
type Client struct {
|
|
workspaceID string
|
|
root string
|
|
processes process.Manager
|
|
fs fs.FileSystem
|
|
lg *slog.Logger
|
|
|
|
mu sync.RWMutex
|
|
ready bool
|
|
sessionID string
|
|
transport *transport
|
|
history *History
|
|
}
|
|
|
|
// NewClient creates an ACP client for the given workspace.
|
|
func NewClient(workspaceID, root string, processes process.Manager, filesystem fs.FileSystem, lg *slog.Logger) *Client {
|
|
if lg == nil {
|
|
lg = slog.Default()
|
|
}
|
|
return &Client{
|
|
workspaceID: workspaceID,
|
|
root: root,
|
|
processes: processes,
|
|
fs: filesystem,
|
|
lg: lg,
|
|
history: NewHistory(),
|
|
}
|
|
}
|
|
|
|
// Initialize starts the process (if necessary), performs the ACP handshake,
|
|
// and creates a session. It is safe to call repeatedly; the client will reset
|
|
// and re-initialize if the process is not running.
|
|
func (c *Client) Initialize(ctx context.Context) error {
|
|
c.mu.RLock()
|
|
already := c.ready && c.sessionID != "" && c.processes.Status(c.workspaceID).Running
|
|
c.mu.RUnlock()
|
|
if already {
|
|
return nil
|
|
}
|
|
|
|
c.mu.Lock()
|
|
c.ready = false
|
|
c.sessionID = ""
|
|
c.history.Clear()
|
|
old := c.transport
|
|
c.transport = nil
|
|
c.mu.Unlock()
|
|
|
|
if old != nil {
|
|
old.onRequest = nil
|
|
old.onNotification = nil
|
|
old.onClose = nil
|
|
_ = old.Close()
|
|
}
|
|
|
|
if !c.processes.Status(c.workspaceID).Running {
|
|
if err := c.processes.Start(c.workspaceID, c.root); err != nil {
|
|
return util.Wrap(util.CodeInternal, "failed to start opencode", err)
|
|
}
|
|
}
|
|
|
|
stdin, err := c.processes.Stdin(c.workspaceID)
|
|
if err != nil {
|
|
return util.Wrap(util.CodeInternal, "failed to get opencode stdin", err)
|
|
}
|
|
sub, err := c.processes.Subscribe(c.workspaceID)
|
|
if err != nil {
|
|
return util.Wrap(util.CodeInternal, "failed to subscribe to opencode output", err)
|
|
}
|
|
|
|
tr := newTransport(stdin, sub, c.lg)
|
|
tr.onRequest = c.handleRequest
|
|
tr.onNotification = c.handleNotification
|
|
tr.onClose = c.onTransportClose
|
|
tr.start()
|
|
|
|
initParams := InitializeParams{
|
|
ProtocolVersion: 1,
|
|
ClientCapabilities: ClientCapabilities{
|
|
FS: ClientFSCapabilities{ReadTextFile: true, WriteTextFile: true},
|
|
},
|
|
ClientInfo: ClientInfo{Name: "codespace", Version: "0.1.0"},
|
|
}
|
|
var initRes InitializeResult
|
|
if err := tr.call(ctx, "initialize", initParams, &initRes); err != nil {
|
|
_ = tr.Close()
|
|
return util.Wrap(util.CodeInternal, "initialize failed", err)
|
|
}
|
|
|
|
sessParams := SessionNewParams{Cwd: c.root, MCPServers: []any{}}
|
|
var sessRes SessionNewResult
|
|
if err := tr.call(ctx, "session/new", sessParams, &sessRes); err != nil {
|
|
_ = tr.Close()
|
|
return util.Wrap(util.CodeInternal, "session/new failed", err)
|
|
}
|
|
|
|
c.mu.Lock()
|
|
c.transport = tr
|
|
c.sessionID = sessRes.SessionID
|
|
c.ready = true
|
|
c.mu.Unlock()
|
|
|
|
c.lg.Info("acp initialized", "workspace_id", c.workspaceID, "session_id", sessRes.SessionID)
|
|
return nil
|
|
}
|
|
|
|
// Prompt sends a user message and returns the agent's final text for this turn.
|
|
func (c *Client) Prompt(ctx context.Context, content string) (PromptResult, error) {
|
|
var result PromptResult
|
|
c.mu.RLock()
|
|
if !c.ready {
|
|
c.mu.RUnlock()
|
|
return result, util.New(util.CodeConflict, "client not ready")
|
|
}
|
|
sessionID := c.sessionID
|
|
tr := c.transport
|
|
startIdx := c.history.Len()
|
|
c.mu.RUnlock()
|
|
|
|
c.history.Add("user", content)
|
|
|
|
params := SessionPromptParams{
|
|
SessionID: sessionID,
|
|
Prompt: []PromptMessage{{Type: "text", Text: content}},
|
|
}
|
|
if err := tr.call(ctx, "session/prompt", params, &result); err != nil {
|
|
return result, util.Wrap(util.CodeInternal, "session/prompt failed", err)
|
|
}
|
|
|
|
msgs := c.history.Since(startIdx)
|
|
var sb strings.Builder
|
|
for _, m := range msgs {
|
|
if m.Role == "agent" {
|
|
sb.WriteString(m.Text)
|
|
}
|
|
}
|
|
result.Text = sb.String()
|
|
return result, nil
|
|
}
|
|
|
|
// Cancel sends a best-effort session/cancel notification.
|
|
func (c *Client) Cancel() error {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
if !c.ready || c.transport == nil {
|
|
return nil
|
|
}
|
|
return c.transport.sendNotification("session/cancel", SessionCancelParams{SessionID: c.sessionID})
|
|
}
|
|
|
|
// Status returns whether the client has a ready session and its id.
|
|
func (c *Client) Status() (ready bool, sessionID string) {
|
|
c.mu.RLock()
|
|
defer c.mu.RUnlock()
|
|
return c.ready, c.sessionID
|
|
}
|
|
|
|
// History returns the in-memory conversation history.
|
|
func (c *Client) History() []Message {
|
|
return c.history.List()
|
|
}
|
|
|
|
// handleRequest dispatches agent-initiated JSON-RPC requests.
|
|
func (c *Client) handleRequest(method string, params json.RawMessage, id int) {
|
|
var result any
|
|
var rpcErr *rpcError
|
|
|
|
switch method {
|
|
case "fs/read_text_file":
|
|
var p FSReadParams
|
|
if err := json.Unmarshal(params, &p); err != nil {
|
|
rpcErr = &rpcError{Code: -32700, Message: "Parse error"}
|
|
break
|
|
}
|
|
data, err := c.fs.Read(p.Path)
|
|
if err != nil {
|
|
rpcErr = c.rpcErrorFromErr(err)
|
|
} else {
|
|
result = FSReadResult{Content: string(data)}
|
|
}
|
|
case "fs/write_text_file":
|
|
var p FSWriteParams
|
|
if err := json.Unmarshal(params, &p); err != nil {
|
|
rpcErr = &rpcError{Code: -32700, Message: "Parse error"}
|
|
break
|
|
}
|
|
if err := c.fs.Write(p.Path, []byte(p.Content)); err != nil {
|
|
rpcErr = c.rpcErrorFromErr(err)
|
|
} else {
|
|
result = FSWriteResult{}
|
|
}
|
|
default:
|
|
rpcErr = &rpcError{Code: -32601, Message: "Method not found: " + method}
|
|
}
|
|
|
|
c.mu.RLock()
|
|
tr := c.transport
|
|
c.mu.RUnlock()
|
|
if tr != nil {
|
|
if err := tr.sendResponse(id, result, rpcErr); err != nil {
|
|
c.lg.Error("acp failed to send response", "workspace_id", c.workspaceID, "method", method, "error", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
// handleNotification processes agent-initiated notifications.
|
|
func (c *Client) handleNotification(method string, params json.RawMessage) {
|
|
if method != "session/update" {
|
|
c.lg.Debug("acp drop notification", "workspace_id", c.workspaceID, "method", method)
|
|
return
|
|
}
|
|
var up SessionUpdateParams
|
|
if err := json.Unmarshal(params, &up); err != nil {
|
|
c.lg.Error("acp invalid session/update", "workspace_id", c.workspaceID, "error", err)
|
|
return
|
|
}
|
|
switch up.Update.SessionUpdate {
|
|
case "agent_message_chunk":
|
|
if up.Update.Content.Type == "text" {
|
|
c.history.AppendOrCreate("agent", up.Update.MessageID, up.Update.Content.Text)
|
|
} else {
|
|
c.lg.Debug("acp drop non-text agent chunk", "workspace_id", c.workspaceID, "type", up.Update.Content.Type)
|
|
}
|
|
case "user_message_chunk":
|
|
if up.Update.Content.Type == "text" {
|
|
c.history.AppendOrCreate("user", up.Update.MessageID, up.Update.Content.Text)
|
|
} else {
|
|
c.lg.Debug("acp drop non-text user chunk", "workspace_id", c.workspaceID, "type", up.Update.Content.Type)
|
|
}
|
|
default:
|
|
c.lg.Debug("acp drop update", "workspace_id", c.workspaceID, "type", up.Update.SessionUpdate)
|
|
}
|
|
}
|
|
|
|
// onTransportClose is invoked when the process output stream closes.
|
|
func (c *Client) onTransportClose() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
if c.ready {
|
|
c.ready = false
|
|
c.sessionID = ""
|
|
c.transport = nil
|
|
c.history.Clear()
|
|
c.lg.Info("acp transport closed", "workspace_id", c.workspaceID)
|
|
}
|
|
}
|
|
|
|
// rpcErrorFromErr maps a filesystem error to a JSON-RPC error.
|
|
func (c *Client) rpcErrorFromErr(err error) *rpcError {
|
|
switch util.CodeOf(err) {
|
|
case util.CodeNotFound, util.CodeBadRequest:
|
|
return &rpcError{Code: -32602, Message: err.Error()}
|
|
default:
|
|
return &rpcError{Code: -32603, Message: err.Error()}
|
|
}
|
|
}
|