fix(acp): group agent chunks by messageId (backend) and coalesce consecutive same-role entries in the UI (frontend)
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.
This commit is contained in:
+33
-8
@@ -7,27 +7,51 @@ import (
|
||||
|
||||
// Message is one entry in the conversation history.
|
||||
type Message struct {
|
||||
Role string `json:"role"`
|
||||
Text string `json:"text"`
|
||||
Time time.Time `json:"time"`
|
||||
Role string `json:"role"`
|
||||
Text string `json:"text"`
|
||||
Time time.Time `json:"time"`
|
||||
MessageID string `json:"messageId,omitempty"`
|
||||
}
|
||||
|
||||
// History accumulates prompt/response messages in memory for a session.
|
||||
type History struct {
|
||||
mu sync.RWMutex
|
||||
messages []Message
|
||||
mu sync.RWMutex
|
||||
messages []Message
|
||||
indexByID map[string]int
|
||||
}
|
||||
|
||||
// NewHistory creates an empty History.
|
||||
func NewHistory() *History {
|
||||
return &History{}
|
||||
return &History{indexByID: make(map[string]int)}
|
||||
}
|
||||
|
||||
// Add appends a message with the current time.
|
||||
// Add appends a message with the current time. It is used for messages that
|
||||
// have no messageId (e.g. the user prompt path).
|
||||
func (h *History) Add(role, text string) {
|
||||
h.AppendOrCreate(role, "", text)
|
||||
}
|
||||
|
||||
// AppendOrCreate finds the existing entry with the same messageId and appends
|
||||
// text to it; otherwise creates a new entry. When messageId is empty this
|
||||
// behaves like Add(role, text). It returns the time of the created or existing
|
||||
// entry.
|
||||
func (h *History) AppendOrCreate(role, messageId, text string) time.Time {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.messages = append(h.messages, Message{Role: role, Text: text, Time: time.Now()})
|
||||
|
||||
if messageId == "" {
|
||||
h.messages = append(h.messages, Message{Role: role, Text: text, Time: time.Now()})
|
||||
return h.messages[len(h.messages)-1].Time
|
||||
}
|
||||
|
||||
if idx, ok := h.indexByID[messageId]; ok {
|
||||
h.messages[idx].Text += text
|
||||
return h.messages[idx].Time
|
||||
}
|
||||
|
||||
h.messages = append(h.messages, Message{Role: role, Text: text, Time: time.Now(), MessageID: messageId})
|
||||
h.indexByID[messageId] = len(h.messages) - 1
|
||||
return h.messages[len(h.messages)-1].Time
|
||||
}
|
||||
|
||||
// List returns a copy of all messages, oldest first.
|
||||
@@ -66,4 +90,5 @@ func (h *History) Clear() {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
h.messages = h.messages[:0]
|
||||
h.indexByID = make(map[string]int)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user