From 3cf705a45a231e6212a111d3508c61e70a3d94c7 Mon Sep 17 00:00:00 2001 From: omartelo Date: Fri, 4 Dec 2026 14:54:21 -0300 Subject: [PATCH] feat(native): a command run with ! is one message with its output The command stood in the person's balloon as typed and its output under it on the agent's side, with a stray 0ms. Both are now one balloon on the person's side, with the time it took when it is a second or more. --- CHANGELOG.md | 1 + native/DESIGN.md | 8 +- native/cmd/lich-native/chat_command.go | 171 ++++++++++++++++++++++ native/cmd/lich-native/chat_copy_menu.go | 11 +- native/cmd/lich-native/chat_items.go | 116 ++------------- native/cmd/lich-native/chat_life_test.go | 12 +- native/cmd/lich-native/chat_rows.go | 19 ++- native/cmd/lich-native/chat_shell_test.go | 68 +++++++-- native/cmd/lich-native/chat_turn_fold.go | 4 +- native/cmd/lich-native/chat_view.go | 20 +-- 10 files changed, 270 insertions(+), 160 deletions(-) create mode 100644 native/cmd/lich-native/chat_command.go diff --git a/CHANGELOG.md b/CHANGELOG.md index e14c4bce1..b789aa694 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - **The Git panel shows one thing at a time.** Changes and History are now two tabs, and a commit opens beside the log when you click it. The branches open from the branch's name in the panel's header: click one to check it out, type a new name to create it, and find the rest in its `⋯`. - **A finished turn in the chat is your message, the answer and the files it changed.** The calls, thoughts and texts on the way fold under one line with the time they took, like Worked for 4m 6s; click it to read them in order. Each changed file is listed once under the answer, and a click opens all its edits. - **A permission the agent asks for in the chat now waits over the message box.** It says what the agent wants to do and shows the whole command, or each file with its diff, with the answers always in view. Once you answer, only the call is left in the conversation, or a Denied line. +- **A command you run with `!` in the chat is one message with its output.** The command and what it printed stand together on your side of the conversation, with the time it took when it is a second or more, and in red when it fails. On Claude Code, Codex and opencode, the agents whose chat runs commands. - **Review threads in Pulls show who answers whom.** Each thread is a card with its file and code, and its replies hang from a line under the comment they answer, in Conversation and between the lines in Files. - **Long conversations no longer slow the window down.** Scrolling, typing and watching an answer come in cost about the same in a chat of thousands of messages as in a new one, and several agents working at once take about a third of the diff --git a/native/DESIGN.md b/native/DESIGN.md index 4d5346982..b15722395 100644 --- a/native/DESIGN.md +++ b/native/DESIGN.md @@ -898,9 +898,11 @@ never makes the Latest pill show. time takes it back. - **Shell mode**: a line that starts with `!` turns the box's edge and fill toward `ToneWait`, the tone of a mode that runs unchecked, and the box's foot shows "Shell mode" in that tone in the chips' place, which do not apply - to a command; Send stays at the right, wearing Enter's cap in place of its arrow. The command's call is a row of its own under - the person's message, hung by the connector, its output whole in mono under it, 20 lines and "Show N more - lines". Shift+Tab walks the mode chip's choices in the provider's order, never onto a `Danger` one. + to a command; Send stays at the right, wearing Enter's cap in place of its arrow. The command is the + person's, so it and all it printed are one balloon on their side (`chat_command.go`), never a call on the agent's: + the terminal glyph in the mark's place and the command in mono `TextSM` medium, the output under it in muted mono + `TextXS`, 20 lines and "Show N more lines". It shimmers while it runs; failed, the glyph and the output are + `Destructive`. How long it took stands before the message's time, "2s · 14:41", and not under a second. Shift+Tab walks the mode chip's choices in the provider's order, never onto a `Danger` one. - **Background shells** (`chat_shells*.go`): while one runs, a chip in the box's foot says "2 shells · 1 monitor"; it and `/tasks` open a panel of them in the panel's place, "N running" beside the title, the running ones under a Running group label and the ended ones under Ended. A row is a state glyph, the command in mono and its diff --git a/native/cmd/lich-native/chat_command.go b/native/cmd/lich-native/chat_command.go new file mode 100644 index 000000000..c013bbb09 --- /dev/null +++ b/native/cmd/lich-native/chat_command.go @@ -0,0 +1,171 @@ +package main + +import ( + "image" + "strings" + "time" + + "gioui.org/font" + "gioui.org/layout" + "gioui.org/op/clip" + "gioui.org/op/paint" + + "github.com/lichdotdev/lich/api" + "github.com/lichdotdev/lich/native/backend" + "github.com/lichdotdev/lich/native/i18n" + "github.com/lichdotdev/lich/native/ui" +) + +// chatCommandLines is how much of a shell command's output shows before the +// rest opens on a click. +const chatCommandLines = 20 + +// isCommand is whether it is a command the person ran after "!", in a chat +// whose provider runs it: a file attached makes the line a message. +func isCommand(it backend.ChatItem, runsShell bool) bool { + return runsShell && it.Kind == api.ItemUser && strings.HasPrefix(it.Text, chatShellMark) && len(it.Files) == 0 +} + +// chatCommand is the person's own shell command, in their message's place: the +// command, what it printed under it, and how long it took by its time. +func (u *window) chatCommand(gtx C, th *ui.Theme, p *chatPane, c *chatCopy, r chatRow) D { + text := commandText(r.item) + return c.hover(gtx, func(gtx C, over bool) D { + return layout.Flex{Axis: layout.Vertical}.Layout(gtx, + layout.Rigid(func(gtx C) D { + return chatBalloon(gtx, th, func(gtx C) D { return chatCommandBody(gtx, th.On(th.Accent), p, r, text) }) + }), + ui.VGap(1), + layout.Rigid(func(gtx C) D { + return layout.E.Layout(gtx, func(gtx C) D { + return u.chatStampRow(gtx, th, c, text, func(gtx C) D { + return layout.Flex{Alignment: layout.Middle}.Layout(gtx, + layout.Rigid(func(gtx C) D { return chatCommandTook(gtx, th, r) }), + layout.Rigid(func(gtx C) D { return u.chatDatedStamp(gtx, th, p, r.item) }), + ) + }, over, true) + }) + }), + ) + }) +} + +// commandText is the command the person's message it ran, without its mark. +func commandText(it backend.ChatItem) string { + return strings.TrimSpace(strings.TrimPrefix(it.Text, chatShellMark)) +} + +func chatCommandBody(gtx C, th *ui.Theme, p *chatPane, r chatRow, text string) D { + failed := r.call != nil && r.call.State == api.ToolFailed + ink := th.MutedForeground + if failed { + ink = th.Destructive + } + command := th.Text(ui.TextSM, expandTabs(text)).Mono().Weight(font.Medium).Wrapped() + if r.live { + // The band lights toward Foreground, so the command runs muted to show it. + command = command.Muted().Shimmer() + } + rows := []layout.FlexChild{layout.Rigid(func(gtx C) D { + return layout.Flex{}.Layout(gtx, + layout.Rigid(func(gtx C) D { + // The glyph stands on the first line of a command that wraps. + return layout.Inset{Top: ui.Space(0.75)}.Layout(gtx, func(gtx C) D { return icToolRun.Layout(gtx, ui.Space(3.5), ink) }) + }), + ui.Gap(2), + layout.Rigid(command.Layout), + ) + })} + out := commandOutput(r.call) + if out == "" { + return layout.Flex{Axis: layout.Vertical}.Layout(gtx, rows...) + } + more := p.fold("out:" + r.item.ID) + more.update(gtx) + lines := strings.Split(out, "\n") + hidden := len(lines) - chatCommandLines + if hidden > 0 && !more.open { + lines = lines[:chatCommandLines] + } + rows = append(rows, ui.VGap(2)) + for _, l := range lines { + rows = append(rows, layout.Rigid(th.Text(ui.TextXS, expandTabs(l)).Mono().In(ink).Wrapped().Layout)) + } + if hidden > 0 { + rows = append(rows, layout.Rigid(func(gtx C) D { return chatCommandMore(gtx, th, more, hidden) })) + } + return layout.Flex{Axis: layout.Vertical}.Layout(gtx, rows...) +} + +// commandOutput is what call printed, whole; a failure that printed nothing +// says it failed. +func commandOutput(call *backend.ChatToolCall) string { + if call == nil { + return "" + } + out := strings.Trim(call.Output, "\n") + if out == "" && call.State == api.ToolFailed { + return i18n.T("chat.view.toolFailed") + } + return out +} + +// chatCommandMore opens the lines of a command's output past the head, or +// shuts them again. +func chatCommandMore(gtx C, th *ui.Theme, more *chatFold, hidden int) D { + text := i18n.T("chat.view.shellMore", i18n.Args{"count": hidden}) + if more.open { + text = i18n.T("chat.view.shellLess") + } + return layout.Inset{Top: ui.Space(1.5)}.Layout(gtx, func(gtx C) D { + return more.click.Layout(gtx, func(gtx C) D { + fg := th.MutedForeground + if more.click.Hovered() { + fg = th.Foreground + } + return th.Text(ui.TextXS, text).In(fg).Layout(gtx) + }) + }) +} + +// chatCommandTook is how long the command has run or ran, from the person's +// message: Claude Code tells of the call only once it ended. Nothing under a +// second, or where a time is missing. +func chatCommandTook(gtx C, th *ui.Theme, r chatRow) D { + if r.item.At <= 0 { + return D{} + } + start := time.UnixMilli(r.item.At) + var took time.Duration + switch { + case r.live: + chatTick(gtx, start) + took = gtx.Now.Sub(start) + case r.call != nil && r.call.Ended > 0: + took = time.UnixMilli(r.call.Ended).Sub(start) + } + if took < time.Second { + return D{} + } + return th.Text(ui.TextXS, chatElapsed(took)+" · ").Muted().Layout(gtx) +} + +// chatBalloon is w on the person's balloon, against the column's right edge, +// its corner by that edge tighter than the others. +func chatBalloon(gtx C, th *ui.Theme, w layout.Widget) D { + maxW := int(float32(gtx.Constraints.Max.X) * chatUserShare) + return layout.E.Layout(gtx, func(gtx C) D { + gtx.Constraints.Min = image.Point{} + gtx.Constraints.Max.X = maxW + return layout.Background{}.Layout(gtx, + func(gtx C) D { + r, tip := gtx.Dp(ui.RadiusXL), gtx.Dp(ui.RadiusSM) + box := clip.RRect{Rect: image.Rectangle{Max: gtx.Constraints.Min}, NW: r, NE: r, SW: r, SE: tip} + paint.FillShape(gtx.Ops, th.Accent, box.Op(gtx.Ops)) + return D{Size: gtx.Constraints.Min} + }, + func(gtx C) D { + return layout.Inset{Top: ui.Space(3), Bottom: ui.Space(3), Left: ui.Space(4), Right: ui.Space(4)}.Layout(gtx, w) + }) + }) +} diff --git a/native/cmd/lich-native/chat_copy_menu.go b/native/cmd/lich-native/chat_copy_menu.go index 3486a954d..68c8ef0ca 100644 --- a/native/cmd/lich-native/chat_copy_menu.go +++ b/native/cmd/lich-native/chat_copy_menu.go @@ -31,7 +31,6 @@ const ( speakerYou chatSpeaker = iota // the person's message, under its own heading speakerAgent // the agent's text, calls and edits, under the agent's heading speakerAside // a compaction: no heading, and the next row starts a section - speakerSame // the person's shell command: in the section it follows ) // messageMenu lays out message w under its right-click menu: Copy takes text, @@ -139,9 +138,6 @@ func chatRowMarkdown(r chatRow, checkout string) (string, chatSpeaker) { case chatRowWork: label, _ := callSummary(r.tools, noVeto) return label, speakerAgent - case chatRowShell: - label, _ := callSummary([]backend.ChatItem{r.item}, noVeto) - return label, speakerSame case chatRowEdit: t := r.item.Tool if t == nil || t.State == api.ToolFailed { @@ -152,7 +148,12 @@ func chatRowMarkdown(r chatRow, checkout string) (string, chatSpeaker) { case chatRowItem: switch it := r.item; it.Kind { case api.ItemUser: - return userMessageMarkdown(it), speakerYou + said := userMessageMarkdown(it) + if r.call != nil { + label, _ := callSummary([]backend.ChatItem{{Kind: api.ItemTool, Tool: r.call}}, noVeto) + said += "\n\n" + label + } + return said, speakerYou case api.ItemAgent: return it.Text, speakerAgent case api.ItemCompaction: diff --git a/native/cmd/lich-native/chat_items.go b/native/cmd/lich-native/chat_items.go index 46caaa2f7..3af3e557c 100644 --- a/native/cmd/lich-native/chat_items.go +++ b/native/cmd/lich-native/chat_items.go @@ -12,8 +12,6 @@ import ( "gioui.org/font" "gioui.org/layout" "gioui.org/op" - "gioui.org/op/clip" - "gioui.org/op/paint" "gioui.org/unit" "github.com/lichdotdev/lich/api" @@ -34,17 +32,6 @@ var ( chatToolRow = ui.Space(7) ) -// chatShellLines is how much of a shell command's output shows before the -// rest opens on a click. -const chatShellLines = 20 - -var ( - // chatBranchGutter is the room a shell command's call gives its connector, - // and chatBranchX where the connector runs. - chatBranchGutter = ui.Space(6) - chatBranchX = ui.Space(1.75) -) - // chatClock is the short time under a message, the 24-hour layout the window's // other clocks (the Git graph, a stopped console) use. const chatClock = "15:04" @@ -53,8 +40,6 @@ func (u *window) chatRow(gtx C, th *ui.Theme, p *chatPane, sv sessionView, r cha switch r.kind { case chatRowWork: return u.chatWork(gtx, th, p, p.fold("work:"+r.run), sv, r) - case chatRowShell: - return chatBranch(gtx, th, true, func(gtx C) D { return chatShellCall(gtx, th, p.fold("out:"+r.item.ID), r.item, r.live) }) case chatRowEdit: return u.chatEdit(gtx, th, p, sv, r) case chatRowLive: @@ -81,6 +66,9 @@ func (u *window) chatRow(gtx C, th *ui.Theme, p *chatPane, sv sessionView, r cha return u.chatTaskNotes(gtx, th, p, sv.Kind, it, notes) } c := p.copyOf(it.ID) + if r.command { + return u.messageMenu(gtx, th, p, sv, c, it.ID, "user:"+it.ID, commandText(it), func(gtx C) D { return u.chatCommand(gtx, th, p, c, r) }) + } return u.messageMenu(gtx, th, p, sv, c, it.ID, "user:"+it.ID, it.Text, func(gtx C) D { return u.chatUserRow(gtx, th, p, c, it) }) case api.ItemAgent: if r.tucked != "" { @@ -295,7 +283,7 @@ func chatThought(gtx C, th *ui.Theme, f *chatFold, it backend.ChatItem, live boo func (u *window) chatWork(gtx C, th *ui.Theme, p *chatPane, f *chatFold, sv sessionView, r chatRow) D { outcome, vetoed := u.chatOutcome(p, sv) if len(r.tools) == 1 { - return chatToolWith(gtx, th, r.tools[0], r.live, outcome(r.tools[0]), 0) + return chatTool(gtx, th, r.tools[0], r.live, outcome(r.tools[0]), 0) } f.update(gtx) label := func(gtx C, fg color.NRGBA) D { return chatWorkLabel(gtx, th, r, vetoed, fg) } @@ -382,7 +370,7 @@ func chatCalls(gtx C, th *ui.Theme, f, earlier *chatFold, r chatRow, outcome fun if i == 0 && hidden == 0 { top = ui.Space(1) } - return layout.Inset{Top: top}.Layout(gtx, func(gtx C) D { return chatToolWith(gtx, th, it, r.live, outcome(it), verbW) }) + return layout.Inset{Top: top}.Layout(gtx, func(gtx C) D { return chatTool(gtx, th, it, r.live, outcome(it), verbW) }) }) }) })) @@ -390,65 +378,6 @@ func chatCalls(gtx C, th *ui.Theme, f, earlier *chatFold, r chatRow, outcome fun return children } -// chatShellCall is a shell command's call with its output under it, as -// Claude Code prints it, on a branch from the person's command: the row keeps -// the mark, neither the command the message over it already shows nor the -// output's first line, and output past chatShellLines opens on a click of the -// line under it. -func chatShellCall(gtx C, th *ui.Theme, more *chatFold, it backend.ChatItem, live bool) D { - tool := *it.Tool - out := strings.Trim(tool.Output, "\n") - tool.Output, tool.Title = "", "" - row := it - row.Tool = &tool - if out == "" { - return chatTool(gtx, th, row, live) - } - more.update(gtx) - lines := strings.Split(out, "\n") - hidden := len(lines) - chatShellLines - if hidden > 0 && !more.open { - lines = lines[:chatShellLines] - } - children := make([]layout.FlexChild, 0, len(lines)+1) - for _, l := range lines { - children = append(children, layout.Rigid(shellLine(th, l))) - } - if hidden > 0 { - children = append(children, layout.Rigid(func(gtx C) D { return chatShellMore(gtx, th, more, hidden) })) - } - return layout.Flex{Axis: layout.Vertical}.Layout(gtx, - layout.Rigid(func(gtx C) D { return chatTool(gtx, th, row, live) }), - layout.Rigid(func(gtx C) D { - return layout.Background{}.Layout(gtx, - func(gtx C) D { return ui.Fill(gtx, th.Background, ui.RadiusSM) }, - func(gtx C) D { - return layout.Inset{Top: ui.Space(2), Bottom: ui.Space(2)}.Layout(gtx, func(gtx C) D { - return layout.Flex{Axis: layout.Vertical}.Layout(gtx, children...) - }) - }) - }), - ) -} - -// chatShellMore opens the lines of a shell command's output past the head, -// or shuts them again. -func chatShellMore(gtx C, th *ui.Theme, more *chatFold, hidden int) D { - text := i18n.T("chat.view.shellMore", i18n.Args{"count": hidden}) - if more.open { - text = i18n.T("chat.view.shellLess") - } - return layout.Inset{Top: ui.Space(1), Left: ui.Space(2), Right: ui.Space(2)}.Layout(gtx, func(gtx C) D { - return more.click.Layout(gtx, func(gtx C) D { - fg := th.MutedForeground - if more.click.Hovered() { - fg = th.Foreground - } - return th.Text(ui.TextXS, text).In(fg).Layout(gtx) - }) - }) -} - // chatEarlierCalls shows a group's calls before its last chatCallsShown, or // hides them again. func chatEarlierCalls(gtx C, th *ui.Theme, earlier *chatFold, hidden int) D { @@ -466,29 +395,6 @@ func chatEarlierCalls(gtx C, th *ui.Theme, earlier *chatFold, hidden int) D { }) } -// chatBranch lays w to the right of a connector from the fold's line: a thin -// vertical line in Border under the fold's chevron, with a stub into the row, -// and ending there on the last row, as the sidebar's tree links its children. -func chatBranch(gtx C, th *ui.Theme, last bool, w layout.Widget) D { - gutter := gtx.Dp(chatBranchGutter) - macro := op.Record(gtx.Ops) - gtx.Constraints.Max.X = max(gtx.Constraints.Max.X-gutter, 0) - gtx.Constraints.Min.X = min(gtx.Constraints.Min.X, gtx.Constraints.Max.X) - d := w(gtx) - call := macro.Stop() - line := th.Over(th.Border) - x, mid := gtx.Dp(chatBranchX), gtx.Dp(chatToolRow)/2 - bottom := d.Size.Y - if last { - bottom = mid + 1 - } - paint.FillShape(gtx.Ops, line, clip.Rect{Min: image.Pt(x, 0), Max: image.Pt(x+1, bottom)}.Op()) - paint.FillShape(gtx.Ops, line, clip.Rect{Min: image.Pt(x, mid), Max: image.Pt(gutter-gtx.Dp(ui.Space(1)), mid+1)}.Op()) - defer op.Offset(image.Pt(gutter, 0)).Push(gtx.Ops).Pop() - call.Add(gtx.Ops) - return D{Size: image.Pt(d.Size.X+gutter, d.Size.Y)} -} - // chatNothing stands in for what a shut fold hides. func chatNothing(C) D { return D{} } @@ -496,14 +402,10 @@ func chatNothing(C) D { return D{} } // the time it has run, and why it failed when it did; a call that went well // says nothing more. The diff it made, if any, is under it. While it runs in a // live turn its verb and target shimmer and its time counts; once it ends the -// time stands still, and a call whose end nobody reported shows none. -func chatTool(gtx C, th *ui.Theme, it backend.ChatItem, live bool) D { - return chatToolWith(gtx, th, it, live, nil, 0) -} - -// chatToolWith is chatTool drawn as the card it was opened as when opened is -// not nil, and its verb at least verbW wide, so a group's targets line up. -func chatToolWith(gtx C, th *ui.Theme, it backend.ChatItem, live bool, opened *chatOpened, verbW int) D { +// time stands still, and a call whose end nobody reported shows none. It is +// drawn as the card it was opened as when opened is not nil, and its verb at +// least verbW wide, so a group's targets line up. +func chatTool(gtx C, th *ui.Theme, it backend.ChatItem, live bool, opened *chatOpened, verbW int) D { t := it.Tool if t == nil { return D{} diff --git a/native/cmd/lich-native/chat_life_test.go b/native/cmd/lich-native/chat_life_test.go index 685fe5535..25f0798d1 100644 --- a/native/cmd/lich-native/chat_life_test.go +++ b/native/cmd/lich-native/chat_life_test.go @@ -221,12 +221,12 @@ func TestARunningCallShimmersAndCountsAndAnEndedOneStandsStill(t *testing.T) { start := time.Unix(1000, 0) call := backend.ChatItem{ID: "k1", Kind: api.ItemTool, At: start.UnixMilli(), Tool: &backend.ChatToolCall{Name: "Bash", Title: "go test", State: api.ToolRunning}} draw := func(it backend.ChatItem, live bool) func(C, *ui.Theme) { - return func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, live) } + return func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, live, nil, 0) } } r := newFrameRig(image.Pt(400, 40), draw(call, true)) r.now = start.Add(2 * time.Second) wake := func(it backend.ChatItem, live bool) (time.Time, bool) { - return wakeAfter(r.now, func(gtx C) { chatTool(gtx, r.theme, it, live) }) + return wakeAfter(r.now, func(gtx C) { chatTool(gtx, r.theme, it, live, nil, 0) }) } if at, ok := wake(call, true); !ok || at.After(r.now.Add(ui.LoopFrame)) { t.Errorf("a running call asked for its next frame at %v (asked %v): it does not shimmer", at, ok) @@ -283,7 +283,7 @@ func TestTheBandCrossesARunningCallsNameAndTarget(t *testing.T) { moves bool }{{api.ToolRunning, true}, {api.ToolDone, false}} { it := call(tc.state) - r := newFrameRig(size, func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, true) }) + r := newFrameRig(size, func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, true, nil, 0) }) r.now = start r.frame() before := renderRig(t, r) @@ -378,10 +378,10 @@ func TestACallOfAnEndedTurnStandsStill(t *testing.T) { row = r } } - r := newFrameRig(image.Pt(400, 40), func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, row.live) }) + r := newFrameRig(image.Pt(400, 40), func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, row.live, nil, 0) }) r.now = start.Add(time.Minute) r.frame() - if at, ok := wakeAfter(r.now, func(gtx C) { chatTool(gtx, r.theme, it, row.live) }); ok { + if at, ok := wakeAfter(r.now, func(gtx C) { chatTool(gtx, r.theme, it, row.live, nil, 0) }); ok { t.Errorf("a call of an ended turn asked to be drawn again at %v", at) } if tc.want != "" && !r.shows(tc.want) { @@ -398,7 +398,7 @@ func TestACallUnderASecondShowsNoTime(t *testing.T) { start := time.Unix(1000, 0) it := backend.ChatItem{ID: "k1", Kind: api.ItemTool, At: start.UnixMilli(), Tool: &backend.ChatToolCall{Name: "Bash", Title: "ls", State: api.ToolDone, Ended: start.Add(476 * time.Millisecond).UnixMilli()}} - r := newFrameRig(image.Pt(400, 40), func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, false) }) + r := newFrameRig(image.Pt(400, 40), func(gtx C, th *ui.Theme) { chatTool(gtx, th, it, false, nil, 0) }) r.frame() if r.shows(chatElapsed(0)) || !r.shows("ls") { t.Errorf("want the call with no time:\n%s", r.drawn()) diff --git a/native/cmd/lich-native/chat_rows.go b/native/cmd/lich-native/chat_rows.go index 6064a1bf3..54774064e 100644 --- a/native/cmd/lich-native/chat_rows.go +++ b/native/cmd/lich-native/chat_rows.go @@ -15,7 +15,6 @@ type chatRowKind uint8 const ( chatRowItem chatRowKind = iota // one item as it is chatRowWork // a run of tool calls between the agent's words, folded into one line - chatRowShell // the call of the person's shell command, its output under it chatRowEdit // a call that changed a file, its diff under it, out of the turn's fold chatRowLive // the turn in flight, unless a permission or a question says it waits chatRowDay // the day a turn starts on, when it is not the day of the turn before @@ -27,7 +26,7 @@ const ( // chatRow is one line of the chat view. type chatRow struct { kind chatRowKind - item backend.ChatItem // chatRowItem, chatRowShell, chatRowEdit + item backend.ChatItem // chatRowItem, chatRowEdit // day is a day row's moment, unix ms: when the turn it starts began. day int64 // more marks an answer's text another follows in the turn, with nothing @@ -57,6 +56,10 @@ type chatRow struct { folds bool // tucked is the fold a row is drawn in only while open, "" for none. tucked string + // command marks the person's message that ran a shell command, and call is + // the command's call, nil until the provider tells of it. + command bool + call *backend.ChatToolCall // foot marks the answer of a finished turn, whose time, model and copy // stand under it. foot bool @@ -95,8 +98,8 @@ type chatTurnSpan struct { // carry no durations. // // In a chat of kind whose provider runs the person's shell command, the call -// right after their "!" message is that command's: a row of its own, kept out -// of the turn's work row, whose agent answers after it. +// right after their "!" message is that command's: it is drawn in the message, +// kept out of the turn's work row, whose agent answers after it. func chatRows(items []backend.ChatItem, kind string, running bool, now time.Time) []chatRow { rows, _ := chatRowsAfter(time.Time{}, items, kind, running, now) return rows @@ -127,7 +130,7 @@ func chatRowsAfter(turnDay time.Time, items []backend.ChatItem, kind string, run runOf := map[string]string{} open := map[int]string{} for _, it := range items { - if runsShell && it.Kind == api.ItemTool && prev.Kind == api.ItemUser && prev.Turn == it.Turn && strings.HasPrefix(prev.Text, chatShellMark) { + if it.Kind == api.ItemTool && prev.Turn == it.Turn && isCommand(prev, runsShell) { shell[it.ID] = true } prev = it @@ -167,7 +170,8 @@ func chatRowsAfter(turnDay time.Time, items []backend.ChatItem, kind string, run continue } if it.Kind != api.ItemTool { - r := chatRow{kind: chatRowItem, item: it, live: it.ID == liveID && (it.Kind == api.ItemAgent || it.Kind == api.ItemThought)} + r := chatRow{kind: chatRowItem, item: it, command: isCommand(it, runsShell)} + r.live = it.ID == liveID && (it.Kind == api.ItemAgent || it.Kind == api.ItemThought || r.command) // Thoughts back to back read as one: the first keeps its id, so // its fold stays put while the rest streams into its text. if it.Kind == api.ItemThought && len(rows) > 0 { @@ -182,7 +186,8 @@ func chatRowsAfter(turnDay time.Time, items []backend.ChatItem, kind string, run continue } if shell[it.ID] { - rows = append(rows, chatRow{kind: chatRowShell, item: it, turn: it.Turn, live: running && it.Turn == last && callRunning(it)}) + said := &rows[len(rows)-1] + said.call, said.live = it.Tool, running && it.Turn == last && callRunning(it) rows = append(rows, fileRows(it)...) continue } diff --git a/native/cmd/lich-native/chat_shell_test.go b/native/cmd/lich-native/chat_shell_test.go index b21efcb41..f47242a14 100644 --- a/native/cmd/lich-native/chat_shell_test.go +++ b/native/cmd/lich-native/chat_shell_test.go @@ -173,12 +173,13 @@ func TestBangLineIsTextWhereTheProviderHasNoShell(t *testing.T) { } } -// The call right after the person's "!" message is their command's, a row of -// its own out of the turn's work row, where the provider runs it; the +// The call right after the person's "!" message is their command's, drawn in +// that message and out of the turn's work row, where the provider runs it (the +// contract changed: it was a row of its own under the message); the // agent's answer and calls after it keep the usual fold. The contract changed: // on Claude Code the agent now answers a command, and its answer used to fold // the command's output away. -func TestAShellCommandsCallIsARowOfItsOwn(t *testing.T) { +func TestAShellCommandsCallIsInItsMessage(t *testing.T) { user := backend.ChatItem{ID: "u", Kind: api.ItemUser, Text: "!ls", Turn: 1} call := backend.ChatItem{ID: "c", Kind: api.ItemTool, Turn: 1, Tool: &api.ChatToolCall{Name: "Bash", State: api.ToolDone}} answer := backend.ChatItem{ID: "a", Kind: api.ItemAgent, Text: "ok", Turn: 1} @@ -198,15 +199,15 @@ func TestAShellCommandsCallIsARowOfItsOwn(t *testing.T) { } { shell, work := "", 0 for _, r := range chatRows(c.items, c.kind, false, time.Time{}) { - switch r.kind { - case chatRowShell: - shell = r.item.ID - case chatRowWork: - work += len(r.tools) + for _, it := range c.items { + if r.call != nil && it.Tool == r.call { + shell = it.ID + } } + work += len(r.tools) } if shell != c.shell || work != c.work { - t.Errorf("%s: shell row %q and %d folded calls, want %q and %d", name, shell, work, c.shell, c.work) + t.Errorf("%s: command %q and %d folded calls, want %q and %d", name, shell, work, c.shell, c.work) } } } @@ -245,10 +246,55 @@ func TestAShellCommandsOutputIsShownWhole(t *testing.T) { } } +// The person's command says how it stands in their own message: running with +// no call told yet (Claude Code tells of it once it ended), how long it took +// past a second, and a failure's output in the failure's ink. +func TestAShellCommandSaysHowItStands(t *testing.T) { + at := time.Date(2026, 12, 4, 14, 41, 0, 0, time.Local) + ended := func(state, out string, took time.Duration) *api.ChatToolCall { + return &api.ChatToolCall{Name: "Bash", Title: "git pull", State: state, Output: out, Ended: at.Add(took).UnixMilli()} + } + for _, c := range []struct { + name string + call *api.ChatToolCall + running bool + shows []string + took string + }{ + {"running", nil, true, nil, "3s · "}, + {"quick", ended(api.ToolDone, "Already up to date.", 40*time.Millisecond), false, []string{"Already up to date."}, ""}, + {"slow", ended(api.ToolDone, "Fast-forward", 2*time.Second), false, []string{"Fast-forward"}, "2s · "}, + {"silent", ended(api.ToolDone, "", 0), false, nil, ""}, + {"failed", ended(api.ToolFailed, "fatal: no upstream branch", time.Second), false, []string{"fatal: no upstream branch"}, "1s · "}, + {"failed-silent", ended(api.ToolFailed, "", 0), false, []string{i18n.T("chat.view.toolFailed")}, ""}, + } { + t.Run(c.name, func(t *testing.T) { + u, r, _, _ := shellFrames(t, claudeKind) + items := []backend.ChatItem{{ID: "u1", Kind: api.ItemUser, Text: "!git pull", Turn: 1, At: at.UnixMilli()}} + if c.call != nil { + items = append(items, backend.ChatItem{ID: "shell-1", Kind: api.ItemTool, Turn: 1, At: at.UnixMilli(), Tool: c.call}) + } + r.now = at.Add(2 * time.Second) + u.model.chat.history("s1", items) + u.model.chat.turn(api.ChatTurn{SessionID: "s1", Running: c.running}) + r.frames(2) + r.now = at.Add(3 * time.Second) + r.frame() + writeShot(t, r, "chat-command-"+c.name+".png") + if !r.showsAll(append(c.shows, "git pull")...) || r.shows("!git pull") { + t.Errorf("the command and its output are not shown:\n%s", r.drawn()) + } + if c.took != "" && !r.shows(c.took) || c.took == "" && strings.Contains(r.drawn(), "s · ") { + t.Errorf("want the time %q:\n%s", c.took, r.drawn()) + } + }) + } +} + // Long output shows its head and a line that opens the rest. func TestALongShellOutputOpensTheRest(t *testing.T) { u, r, _, _ := shellFrames(t, claudeKind) - n := chatShellLines + 5 + n := chatCommandLines + 5 u.model.chat.history("s1", []backend.ChatItem{ {ID: "user-1", Kind: api.ItemUser, Text: "!ls", Turn: 1}, {ID: "shell-1", Kind: api.ItemTool, Turn: 1, Tool: &api.ChatToolCall{Name: "Bash", Title: "ls", Output: shellListing(n), State: api.ToolDone}}, @@ -256,7 +302,7 @@ func TestALongShellOutputOpensTheRest(t *testing.T) { r.now = r.now.Add(time.Second) r.frames(3) last, more := fmt.Sprintf("file-%02d.go", n), i18n.T("chat.view.shellMore", i18n.Args{"count": 5}) - if !r.shows(fmt.Sprintf("file-%02d.go", chatShellLines)) || r.shows(last) || !r.shows(more) { + if !r.shows(fmt.Sprintf("file-%02d.go", chatCommandLines)) || r.shows(last) || !r.shows(more) { t.Fatalf("the head and its way to the rest are not shown:\n%s", r.drawn()) } r.clickLabel(t, more) diff --git a/native/cmd/lich-native/chat_turn_fold.go b/native/cmd/lich-native/chat_turn_fold.go index 1267ccb4d..a80b13557 100644 --- a/native/cmd/lich-native/chat_turn_fold.go +++ b/native/cmd/lich-native/chat_turn_fold.go @@ -81,7 +81,7 @@ func (f turnFold) turn(rows []chatRow, turn int) []chatRow { if j == len(rows) { head.reason = reason } - if i > 0 && isItemOf(rows[i-1], api.ItemUser) { + if i > 0 && isItemOf(rows[i-1], api.ItemUser) && !rows[i-1].command { head.since = rows[i-1].item.At } at := len(out) @@ -157,7 +157,7 @@ func isItemOf(r chatRow, kind string) bool { return r.kind == chatRowItem && r.i // ends a stretch of work: a message, an answer to a question, their command, a // day or a compaction. func standsAlone(r chatRow) bool { - return r.kind == chatRowDay || r.kind == chatRowShell || + return r.kind == chatRowDay || isItemOf(r, api.ItemUser) || isItemOf(r, api.ItemQuestion) || isItemOf(r, api.ItemCompaction) } diff --git a/native/cmd/lich-native/chat_view.go b/native/cmd/lich-native/chat_view.go index 0885096c2..4ed0c5581 100644 --- a/native/cmd/lich-native/chat_view.go +++ b/native/cmd/lich-native/chat_view.go @@ -13,7 +13,6 @@ import ( "gioui.org/layout" "gioui.org/op" "gioui.org/op/clip" - "gioui.org/op/paint" "gioui.org/unit" "gioui.org/widget" @@ -832,24 +831,7 @@ func (u *window) chatComposer(gtx C, th *ui.Theme, p *chatPane, sv sessionView, // chatUser is the person's message, in an Accent balloon against the column's // right edge, its corner by that edge tighter than the others, its text Markdown as the answers' is; key names it in p's cache. func (u *window) chatUser(gtx C, th *ui.Theme, p *chatPane, key, text string) D { - maxW := int(float32(gtx.Constraints.Max.X) * chatUserShare) - on := th.On(th.Accent) - return layout.E.Layout(gtx, func(gtx C) D { - gtx.Constraints.Min = image.Point{} - gtx.Constraints.Max.X = maxW - return layout.Background{}.Layout(gtx, - func(gtx C) D { - r, tip := gtx.Dp(ui.RadiusXL), gtx.Dp(ui.RadiusSM) - box := clip.RRect{Rect: image.Rectangle{Max: gtx.Constraints.Min}, NW: r, NE: r, SW: r, SE: tip} - paint.FillShape(gtx.Ops, th.Accent, box.Op(gtx.Ops)) - return D{Size: gtx.Constraints.Min} - }, - func(gtx C) D { - return layout.Inset{Top: ui.Space(3), Bottom: ui.Space(3), Left: ui.Space(4), Right: ui.Space(4)}.Layout(gtx, func(gtx C) D { - return u.userMarkdown(gtx, on, p, key, text) - }) - }) - }) + return chatBalloon(gtx, th, func(gtx C) D { return u.userMarkdown(gtx, th.On(th.Accent), p, key, text) }) } // userMarkdown is the person's text as Markdown at the box's TextSM: the