assistant.go

  1package chat
  2
  3import (
  4	"fmt"
  5	"strings"
  6
  7	tea "charm.land/bubbletea/v2"
  8	"charm.land/lipgloss/v2"
  9	"github.com/charmbracelet/crush/internal/message"
 10	"github.com/charmbracelet/crush/internal/ui/anim"
 11	"github.com/charmbracelet/crush/internal/ui/common"
 12	"github.com/charmbracelet/crush/internal/ui/styles"
 13	"github.com/charmbracelet/x/ansi"
 14)
 15
 16// assistantMessageTruncateFormat is the text shown when an assistant message is
 17// truncated.
 18const assistantMessageTruncateFormat = "… (%d lines hidden) [click or space to expand]"
 19
 20// maxCollapsedThinkingHeight defines the maximum height of the thinking
 21const maxCollapsedThinkingHeight = 10
 22
 23// AssistantMessageItem represents an assistant message in the chat UI.
 24//
 25// This item includes thinking, and the content but does not include the tool calls.
 26type AssistantMessageItem struct {
 27	*highlightableMessageItem
 28	*cachedMessageItem
 29	*focusableMessageItem
 30
 31	message           *message.Message
 32	sty               *styles.Styles
 33	anim              *anim.Anim
 34	thinkingExpanded  bool
 35	thinkingBoxHeight int // Tracks the rendered thinking box height for click detection.
 36}
 37
 38// NewAssistantMessageItem creates a new AssistantMessageItem.
 39func NewAssistantMessageItem(sty *styles.Styles, message *message.Message) MessageItem {
 40	a := &AssistantMessageItem{
 41		highlightableMessageItem: defaultHighlighter(sty),
 42		cachedMessageItem:        &cachedMessageItem{},
 43		focusableMessageItem:     &focusableMessageItem{},
 44		message:                  message,
 45		sty:                      sty,
 46	}
 47
 48	a.anim = anim.New(anim.Settings{
 49		ID:          a.ID(),
 50		Size:        15,
 51		GradColorA:  sty.Primary,
 52		GradColorB:  sty.Secondary,
 53		LabelColor:  sty.FgBase,
 54		CycleColors: true,
 55	})
 56	return a
 57}
 58
 59// StartAnimation starts the assistant message animation if it should be spinning.
 60func (a *AssistantMessageItem) StartAnimation() tea.Cmd {
 61	if !a.isSpinning() {
 62		return nil
 63	}
 64	return a.anim.Start()
 65}
 66
 67// Animate progresses the assistant message animation if it should be spinning.
 68func (a *AssistantMessageItem) Animate(msg anim.StepMsg) tea.Cmd {
 69	if !a.isSpinning() {
 70		return nil
 71	}
 72	return a.anim.Animate(msg)
 73}
 74
 75// ID implements MessageItem.
 76func (a *AssistantMessageItem) ID() string {
 77	return a.message.ID
 78}
 79
 80// Render implements MessageItem.
 81func (a *AssistantMessageItem) Render(width int) string {
 82	cappedWidth := cappedMessageWidth(width)
 83	style := a.sty.Chat.Message.AssistantBlurred
 84	if a.focused {
 85		style = a.sty.Chat.Message.AssistantFocused
 86	}
 87
 88	var spinner string
 89	if a.isSpinning() {
 90		spinner = a.renderSpinning()
 91	}
 92
 93	content, height, ok := a.getCachedRender(cappedWidth)
 94	if !ok {
 95		content = a.renderMessageContent(cappedWidth)
 96		height = lipgloss.Height(content)
 97		// cache the rendered content
 98		a.setCachedRender(content, cappedWidth, height)
 99	}
100
101	highlightedContent := a.renderHighlighted(content, cappedWidth, height)
102	if spinner != "" {
103		if highlightedContent != "" {
104			highlightedContent += "\n\n"
105		}
106		return style.Render(highlightedContent + spinner)
107	}
108
109	return style.Render(highlightedContent)
110}
111
112// renderMessageContent renders the message content including thinking, main content, and finish reason.
113func (a *AssistantMessageItem) renderMessageContent(width int) string {
114	var messageParts []string
115	thinking := strings.TrimSpace(a.message.ReasoningContent().Thinking)
116	content := strings.TrimSpace(a.message.Content().Text)
117	// if the massage has reasoning content add that first
118	if thinking != "" {
119		messageParts = append(messageParts, a.renderThinking(a.message.ReasoningContent().Thinking, width))
120	}
121
122	// then add the main content
123	if content != "" {
124		// add a spacer between thinking and content
125		if thinking != "" {
126			messageParts = append(messageParts, "")
127		}
128		messageParts = append(messageParts, a.renderMarkdown(content, width))
129	}
130
131	// finally add any finish reason info
132	if a.message.IsFinished() {
133		switch a.message.FinishReason() {
134		case message.FinishReasonCanceled:
135			messageParts = append(messageParts, a.sty.Base.Italic(true).Render("Canceled"))
136		case message.FinishReasonError:
137			messageParts = append(messageParts, a.renderError(width))
138		}
139	}
140
141	return strings.Join(messageParts, "\n")
142}
143
144// renderThinking renders the thinking/reasoning content with footer.
145func (a *AssistantMessageItem) renderThinking(thinking string, width int) string {
146	renderer := common.PlainMarkdownRenderer(a.sty, width)
147	rendered, err := renderer.Render(thinking)
148	if err != nil {
149		rendered = thinking
150	}
151	rendered = strings.TrimSpace(rendered)
152
153	lines := strings.Split(rendered, "\n")
154	totalLines := len(lines)
155
156	isTruncated := totalLines > maxCollapsedThinkingHeight
157	if !a.thinkingExpanded && isTruncated {
158		lines = lines[totalLines-maxCollapsedThinkingHeight:]
159		hint := a.sty.Chat.Message.ThinkingTruncationHint.Render(
160			fmt.Sprintf(assistantMessageTruncateFormat, totalLines-maxCollapsedThinkingHeight),
161		)
162		lines = append([]string{hint, ""}, lines...)
163	}
164
165	thinkingStyle := a.sty.Chat.Message.ThinkingBox.Width(width)
166	result := thinkingStyle.Render(strings.Join(lines, "\n"))
167	a.thinkingBoxHeight = lipgloss.Height(result)
168
169	var footer string
170	// if thinking is done add the thought for footer
171	if !a.message.IsThinking() || len(a.message.ToolCalls()) > 0 {
172		duration := a.message.ThinkingDuration()
173		if duration.String() != "0s" {
174			footer = a.sty.Chat.Message.ThinkingFooterTitle.Render("Thought for ") +
175				a.sty.Chat.Message.ThinkingFooterDuration.Render(duration.String())
176		}
177	}
178
179	if footer != "" {
180		result += "\n\n" + footer
181	}
182
183	return result
184}
185
186// renderMarkdown renders content as markdown.
187func (a *AssistantMessageItem) renderMarkdown(content string, width int) string {
188	renderer := common.MarkdownRenderer(a.sty, width)
189	result, err := renderer.Render(content)
190	if err != nil {
191		return content
192	}
193	return strings.TrimSuffix(result, "\n")
194}
195
196func (a *AssistantMessageItem) renderSpinning() string {
197	if a.message.IsThinking() {
198		a.anim.SetLabel("Thinking")
199	} else if a.message.IsSummaryMessage {
200		a.anim.SetLabel("Summarizing")
201	}
202	return a.anim.Render()
203}
204
205// renderError renders an error message.
206func (a *AssistantMessageItem) renderError(width int) string {
207	finishPart := a.message.FinishPart()
208	errTag := a.sty.Chat.Message.ErrorTag.Render("ERROR")
209	truncated := ansi.Truncate(finishPart.Message, width-2-lipgloss.Width(errTag), "...")
210	title := fmt.Sprintf("%s %s", errTag, a.sty.Chat.Message.ErrorTitle.Render(truncated))
211	details := a.sty.Chat.Message.ErrorDetails.Width(width - 2).Render(finishPart.Details)
212	return fmt.Sprintf("%s\n\n%s", title, details)
213}
214
215// isSpinning returns true if the assistant message is still generating.
216func (a *AssistantMessageItem) isSpinning() bool {
217	isThinking := a.message.IsThinking()
218	isFinished := a.message.IsFinished()
219	hasContent := strings.TrimSpace(a.message.Content().Text) != ""
220	hasToolCalls := len(a.message.ToolCalls()) > 0
221	return (isThinking || !isFinished) && !hasContent && !hasToolCalls
222}
223
224// SetMessage is used to update the underlying message.
225func (a *AssistantMessageItem) SetMessage(message *message.Message) tea.Cmd {
226	wasSpinning := a.isSpinning()
227	a.message = message
228	a.clearCache()
229	if !wasSpinning && a.isSpinning() {
230		return a.StartAnimation()
231	}
232	return nil
233}
234
235// ToggleExpanded toggles the expanded state of the thinking box.
236func (a *AssistantMessageItem) ToggleExpanded() {
237	a.thinkingExpanded = !a.thinkingExpanded
238	a.clearCache()
239}
240
241// HandleMouseClick implements MouseClickable.
242func (a *AssistantMessageItem) HandleMouseClick(btn ansi.MouseButton, x, y int) bool {
243	if btn != ansi.MouseLeft {
244		return false
245	}
246	// check if the click is within the thinking box
247	if a.thinkingBoxHeight > 0 && y < a.thinkingBoxHeight {
248		a.ToggleExpanded()
249		return true
250	}
251	return false
252}