Files
magpie/internal/claudetui/transcript.go
T
3leg0ocat efb62fe901 claudetui: a message Claude Code didn't get whole is asked again as claude -p (#5205)
The helper took a message as sent once a row of it reached the
transcript (rows > before), whatever the row held. A console that loses
part of a paste, as Windows 10's own ConPTY does (#5205), gave Claude
Code the rest, and the model answered that as the message.

Now the words the transcript records of it (a user row's text, or a
queued message's) are compared with those pasted, white space, paste
tags, image marks and format characters aside, read in place with no
copy (whole.go). The paths of images Claude Code says it took
([Image: source: <path>] meta rows) are left out and the images counted.
What Claude Code answers meanwhile is held for up to 2s. A message that
isn't whole ends the turn as Unpastable before any of its reply, so the
bridge asks it again as claude -p, as it does an image the interactive
mode didn't take.

This is 3leg0ocat's patch (github.com/3leg0ocat/magpie-lite, branch
claudetui-whole-message, b800c0cb3b), applied as written, with
TestShimCutMessageIsUnpastable added: the fake Claude Code gets a
FAKE_CUT knob that keeps only a long paste's tail past its 1024-byte
blocks, as Windows 10's conhost lost them.

Run:
- go test ./internal/claudetui ./internal/conptypair on mote (macOS):
  ok. TestShimCutMessageIsUnpastable broken by hand (send returning nil
  in place of checkWhole): red, the cut tail answered as "echo: ...".
- go vet ./..., GOOS=linux go vet -tags nogui ./..., GOOS=windows go
  vet ./... on mote: ok (with the next commit).
- win-run go test ./internal/claudetui on xiaomi-gpu (Windows 11 26200,
  whose own ConPTY keeps the marks): every test ok but
  TestShimFirstMessageWaitsForTools, red on Windows at main (#4810).
3leg0ocat's own record: Windows 10 19044, Claude Code 2.1.319, Windows'
own ConPTY at 64 KB caught 4 of 4 before any reply; with ConPTY
1.25.260930003, none caught across 8/64 KB, a leading /, two images, a
queued message and two turns.

Not tried by me: Windows 10 itself (no Windows 10 box here).
2026-11-16 21:51:26 +08:00

163 lines
4.7 KiB
Go

package claudetui
import (
"encoding/json"
)
// emitter is where a translator's lines go.
type emitter interface {
emit(v any)
// a message the user sent reached the transcript, and a turn ended
sawRow()
turnOver()
}
// translator tells the bridge, as `claude -p`'s stream-json, what the
// session's transcript says. Claude Code writes a row a content block of an
// assistant message, under the message's id, as -p tells one not streamed:
// each goes on as it is. An API error is a <synthetic> message whose text
// is the error, and a turn's end a turn_duration row: they become the
// turn's result, as -p ends a turn.
type translator struct {
out emitter
// the turn failed: its <synthetic> row's result is told, and the
// turn_duration after it says nothing more
failed bool
// tap has the streams of the messages (tap.go): a message is told
// after its stream has begun, and a turn's end after its streams'
tap *tap
}
func newTranslator(out emitter) *translator { return &translator{out: out} }
// quiet and flush are for a translator that holds rows back; this one
// tells each as it comes.
func (t *translator) quiet() {}
func (t *translator) flush() {}
func (t *translator) row(b []byte) {
var r struct {
Type string `json:"type"`
Subtype string `json:"subtype"`
UUID string `json:"uuid"`
IsSidechain bool `json:"isSidechain"`
IsMeta bool `json:"isMeta"`
RequestID string `json:"requestId"`
Error json.RawMessage `json:"error"`
IsAPIError bool `json:"isApiErrorMessage"`
APIStatus int `json:"apiErrorStatus"`
ToolUseResult json.RawMessage `json:"toolUseResult"`
Operation string `json:"operation"`
Content string `json:"content"`
APIParams struct {
RateLimitInfo json.RawMessage `json:"rate_limit_info"`
} `json:"apiErrorParams"`
Message json.RawMessage `json:"message"`
}
if json.Unmarshal(b, &r) != nil || r.IsSidechain {
return
}
switch r.Type {
case "queue-operation":
// a message sent mid-turn waits in Claude Code's queue
if r.Operation == "enqueue" {
t.heard(func() heardRow { return heardRow{r.Content, -1} })
t.out.sawRow()
}
case "user":
if !r.IsMeta && isPrompt(r.Message) {
t.failed = false
t.heard(func() heardRow { return promptRow(r.Message) })
t.out.sawRow()
}
if r.IsMeta {
t.heardTaken(r.Message)
}
if len(r.ToolUseResult) > 0 && r.ToolUseResult[0] == '{' {
t.out.emit(map[string]any{"type": "user", "tool_use_result": r.ToolUseResult})
}
case "assistant":
var m struct {
ID string `json:"id"`
Model string `json:"model"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
}
_ = json.Unmarshal(r.Message, &m)
if m.Model != "<synthetic>" && m.ID != "" {
t.tap.before(m.ID)
}
line := map[string]any{"type": "assistant", "message": r.Message}
// the row's place in the session, as -p tells it: the bridge
// branches a conversation off there (cutSession)
if r.UUID != "" {
line["uuid"] = r.UUID
}
if r.RequestID != "" {
line["request_id"] = r.RequestID
}
if len(r.Error) > 0 {
line["error"] = r.Error
}
t.out.emit(line)
if m.Model != "<synthetic>" || !r.IsAPIError {
return
}
text := ""
for _, c := range m.Content {
if c.Type == "text" {
text += c.Text
}
}
t.failed = true
// the error's own allowance after any an earlier reply of the
// turn told (tap.settle), as it is the latest
t.tap.settle()
if len(r.APIParams.RateLimitInfo) > 0 && string(r.APIParams.RateLimitInfo) != "null" {
t.out.emit(map[string]any{"type": "rate_limit_event", "rate_limit_info": r.APIParams.RateLimitInfo})
}
t.out.turnOver()
t.out.emit(map[string]any{"type": "result", "subtype": "error_during_execution", "is_error": true,
"result": text, "api_error_status": r.APIStatus})
case "system":
if r.Subtype == "turn_duration" {
t.tap.settle()
}
if r.Subtype == "turn_duration" && !t.failed {
t.out.emit(map[string]any{"type": "result", "subtype": "success", "is_error": false})
}
if r.Subtype == "turn_duration" {
t.failed = false
t.out.turnOver()
}
}
}
// isPrompt says whether a user row's message is one typed in: text, not a
// tool's result.
func isPrompt(raw json.RawMessage) bool {
var m struct {
Content json.RawMessage `json:"content"`
}
if json.Unmarshal(raw, &m) != nil || len(m.Content) == 0 {
return false
}
if m.Content[0] == '"' {
return true
}
var blocks []struct {
Type string `json:"type"`
}
if json.Unmarshal(m.Content, &blocks) != nil {
return false
}
for _, b := range blocks {
if b.Type == "tool_result" {
return false
}
}
return len(blocks) > 0
}