chat: fold an agent turn into one reply, split reasoning from answer, keep tool name/input

The phone's chat transcript rendered one agent reply as ~36 bubbles, each with
its own thinking block, and a finished tool call lost its name and its input.

- Turn fold (android): ChatFormat.kt gains the pure ChatTurn / TranscriptRow /
  transcriptRows fold - each run of consecutive assistant messages between two of
  the operator's messages is one agent turn - and ChatTranscript renders one
  bubble per turn: the turn's single collapsible thinking block (all its
  reasoning, in order, growing live while it streams), then its tool-call chips,
  then one answer block of just the turn's text parts. AssistantParts/MessagePart
  are replaced by AgentTurnItem/UserMessageItem; ChatConversationPane counts the
  folded rows.
- Delta part ids (server + client): internal/protocol/chat.go adds
  ChatDeltaPartID(messageID, kind, ordinal), mirrored as ChatProtocol.deltaPartId
  in data/protocol/Chat.kt, and NormalizeChatEvent keys a text or reasoning delta
  on it. OpenCode numbers a message's text and reasoning parts independently from
  0, so the old "%s#%d" gave the first text delta and first reasoning part the
  same id: the answer was appended into the thinking block, and the server's
  retained state (chat_hub.go) re-collided on resume. ChatStore's upsert now
  routes a delta through the kind-aware deltaPartId fold, so it stays robust even
  against a peer that still shares one id.
- Tool fields (server + client): NormalizeChatEvent takes a call's arguments from
  session.tool.input.ended's text as well as session.tool.called's input object
  (toolInput), and chat_hub.go's new mergeChatToolPart folds each tool frame onto
  the retained part so the progress/success frames that omit name and input never
  blank them. ChatFormat.kt's mergeToolPart mirrors that on the client, so the
  chip and panel header keep the real tool name and the panel shows INPUT beside
  OUTPUT.

Tests: chat_v2_test.go and chat_test.go add the text/reasoning id-collision and
the tool name+input retention cases; ChatReasoningTurnTest, ChatTranscriptTest
and ChatRunningWorkTest cover the turn fold, the collision and the tool lifecycle.

Checks: Go build, vet, gofmt and the package tests pass (go build ./... &&
go vet ./...; gofmt -l . empty; go test ./internal/server ./internal/opencode
./internal/protocol ./internal/stage ./internal/pipeline; go test ./...).
Android passes offline with --no-daemon (assembleDebug and the full
testDebugUnitTest suite, 660 tests). The on-phone observation was not performed:
the device lock was held by another run and adb was not available here.

Documentation inventory checked: no packaged OpenCode skill reference in
internal/author/skill/references/ and no packaged template documents the app's
chat transcript, this delta part-id derivation or the tool-field retention, and
no internal/author/ managed file inventory entry names any touched file, so none
of them needs updating. The shared android/README.md behaviour statements are
made stale by this change and are recorded instead in
docs/notes/chat-transcript-turn-fold.md for the docs-fold writer, per the repo
rule that an item never edits a shared document itself.
This commit is contained in:
2026-10-18 01:47:52 +01:00
parent 0d1ddc73a4
commit 798e958cb7
14 changed files with 801 additions and 161 deletions
+22 -2
View File
@@ -1,6 +1,9 @@
package protocol
import "encoding/json"
import (
"encoding/json"
"fmt"
)
// Chat requests and responses are brokered by the Pipelines server (route B in
// the OpenCode chat research): the phone keeps talking only to /connect and the
@@ -213,8 +216,21 @@ type ChatMessage struct {
Parts []ChatPart `json:"parts"`
}
// ChatDeltaPartID is the stable part id an incremental text or reasoning delta
// stream addresses. OpenCode numbers one message's text and reasoning parts
// independently from 0, so the kind is part of the id: for a single message its
// first text delta and its first reasoning delta are distinct ids and a delta can
// never be appended into the other kind's part. The derivation is deterministic,
// so a reconnect or a resume re-addresses the same part, and it is mirrored in
// the client's ChatProtocol.deltaPartId (data/protocol/Chat.kt).
func ChatDeltaPartID(messageID, kind string, ordinal int) string {
return fmt.Sprintf("%s#%s#%d", messageID, kind, ordinal)
}
// ChatPart is one element of a ChatMessage. Type selects the meaning: text and
// reasoning use Text and ID, tool uses ID, Name, Executed, State and Time.
// reasoning use Text and ID, tool uses ID, Name, Executed, State and Time. A text
// or reasoning part's ID is ChatDeltaPartID(message id, kind, ordinal), so the two
// kinds never share an id within a message.
type ChatPart struct {
Type string `json:"type"`
ID string `json:"id,omitempty"`
@@ -227,6 +243,10 @@ type ChatPart struct {
// ChatToolState is the lifecycle of a tool call. Input is a raw JSON value: a
// partial-JSON string while streaming, an object once running or completed.
// OpenCode spreads one call across several frames (name in input.started, the
// arguments in input.ended's text or called's input, then progress/success
// carrying neither), so Input is absent on the frames that do not carry it and
// every consumer retains the value it already learned instead of blanking it.
// Content is the tool's textual/file result, Metadata its provider stats,
// Error the structured failure for status "error".
type ChatToolState struct {