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// RawRender implements [MessageItem].
 81func (a *AssistantMessageItem) RawRender(width int) string {
 82	cappedWidth := cappedMessageWidth(width)
 83
 84	var spinner string
 85	if a.isSpinning() {
 86		spinner = a.renderSpinning()
 87	}
 88
 89	content, height, ok := a.getCachedRender(cappedWidth)
 90	if !ok {
 91		content = a.renderMessageContent(cappedWidth)
 92		height = lipgloss.Height(content)
 93		// cache the rendered content
 94		a.setCachedRender(content, cappedWidth, height)
 95	}
 96
 97	highlightedContent := a.renderHighlighted(content, cappedWidth, height)
 98	if spinner != "" {
 99		if highlightedContent != "" {
100			highlightedContent += "\n\n"
101		}
102		return highlightedContent + spinner
103	}
104
105	return highlightedContent
106}
107
108// Render implements MessageItem.
109func (a *AssistantMessageItem) Render(width int) string {
110	style := a.sty.Chat.Message.AssistantBlurred
111	if a.focused {
112		style = a.sty.Chat.Message.AssistantFocused
113	}
114	return style.Render(a.RawRender(width))
115}
116
117// renderMessageContent renders the message content including thinking, main content, and finish reason.
118func (a *AssistantMessageItem) renderMessageContent(width int) string {
119	var messageParts []string
120	thinking := strings.TrimSpace(a.message.ReasoningContent().Thinking)
121	content := strings.TrimSpace(a.message.Content().Text)
122	// if the massage has reasoning content add that first
123	if thinking != "" {
124		messageParts = append(messageParts, a.renderThinking(a.message.ReasoningContent().Thinking, width))
125	}
126
127	// then add the main content
128	if content != "" {
129		// add a spacer between thinking and content
130		if thinking != "" {
131			messageParts = append(messageParts, "")
132		}
133		messageParts = append(messageParts, a.renderMarkdown(content, width))
134	}
135
136	// finally add any finish reason info
137	if a.message.IsFinished() {
138		switch a.message.FinishReason() {
139		case message.FinishReasonCanceled:
140			messageParts = append(messageParts, a.sty.Base.Italic(true).Render("Canceled"))
141		case message.FinishReasonError:
142			messageParts = append(messageParts, a.renderError(width))
143		}
144	}
145
146	return strings.Join(messageParts, "\n")
147}
148
149// renderThinking renders the thinking/reasoning content with footer.
150func (a *AssistantMessageItem) renderThinking(thinking string, width int) string {
151	renderer := common.PlainMarkdownRenderer(a.sty, width)
152	rendered, err := renderer.Render(thinking)
153	if err != nil {
154		rendered = thinking
155	}
156	rendered = strings.TrimSpace(rendered)
157
158	lines := strings.Split(rendered, "\n")
159	totalLines := len(lines)
160
161	isTruncated := totalLines > maxCollapsedThinkingHeight
162	if !a.thinkingExpanded && isTruncated {
163		lines = lines[totalLines-maxCollapsedThinkingHeight:]
164		hint := a.sty.Chat.Message.ThinkingTruncationHint.Render(
165			fmt.Sprintf(assistantMessageTruncateFormat, totalLines-maxCollapsedThinkingHeight),
166		)
167		lines = append([]string{hint, ""}, lines...)
168	}
169
170	thinkingStyle := a.sty.Chat.Message.ThinkingBox.Width(width)
171	result := thinkingStyle.Render(strings.Join(lines, "\n"))
172	a.thinkingBoxHeight = lipgloss.Height(result)
173
174	var footer string
175	// if thinking is done add the thought for footer
176	if !a.message.IsThinking() || len(a.message.ToolCalls()) > 0 {
177		duration := a.message.ThinkingDuration()
178		if duration.String() != "0s" {
179			footer = a.sty.Chat.Message.ThinkingFooterTitle.Render("Thought for ") +
180				a.sty.Chat.Message.ThinkingFooterDuration.Render(duration.String())
181		}
182	}
183
184	if footer != "" {
185		result += "\n\n" + footer
186	}
187
188	return result
189}
190
191// renderMarkdown renders content as markdown.
192func (a *AssistantMessageItem) renderMarkdown(content string, width int) string {
193	renderer := common.MarkdownRenderer(a.sty, width)
194	result, err := renderer.Render(content)
195	if err != nil {
196		return content
197	}
198	return strings.TrimSuffix(result, "\n")
199}
200
201func (a *AssistantMessageItem) renderSpinning() string {
202	if a.message.IsThinking() {
203		a.anim.SetLabel("Thinking")
204	} else if a.message.IsSummaryMessage {
205		a.anim.SetLabel("Summarizing")
206	}
207	return a.anim.Render()
208}
209
210// renderError renders an error message.
211func (a *AssistantMessageItem) renderError(width int) string {
212	finishPart := a.message.FinishPart()
213	errTag := a.sty.Chat.Message.ErrorTag.Render("ERROR")
214	truncated := ansi.Truncate(finishPart.Message, width-2-lipgloss.Width(errTag), "...")
215	title := fmt.Sprintf("%s %s", errTag, a.sty.Chat.Message.ErrorTitle.Render(truncated))
216	details := a.sty.Chat.Message.ErrorDetails.Width(width - 2).Render(finishPart.Details)
217	return fmt.Sprintf("%s\n\n%s", title, details)
218}
219
220// isSpinning returns true if the assistant message is still generating.
221func (a *AssistantMessageItem) isSpinning() bool {
222	isThinking := a.message.IsThinking()
223	isFinished := a.message.IsFinished()
224	hasContent := strings.TrimSpace(a.message.Content().Text) != ""
225	hasToolCalls := len(a.message.ToolCalls()) > 0
226	return (isThinking || !isFinished) && !hasContent && !hasToolCalls
227}
228
229// SetMessage is used to update the underlying message.
230func (a *AssistantMessageItem) SetMessage(message *message.Message) tea.Cmd {
231	wasSpinning := a.isSpinning()
232	a.message = message
233	a.clearCache()
234	if !wasSpinning && a.isSpinning() {
235		return a.StartAnimation()
236	}
237	return nil
238}
239
240// ToggleExpanded toggles the expanded state of the thinking box.
241func (a *AssistantMessageItem) ToggleExpanded() {
242	a.thinkingExpanded = !a.thinkingExpanded
243	a.clearCache()
244}
245
246// HandleMouseClick implements MouseClickable.
247func (a *AssistantMessageItem) HandleMouseClick(btn ansi.MouseButton, x, y int) bool {
248	if btn != ansi.MouseLeft {
249		return false
250	}
251	// check if the click is within the thinking box
252	if a.thinkingBoxHeight > 0 && y < a.thinkingBoxHeight {
253		a.ToggleExpanded()
254		return true
255	}
256	return false
257}
258
259// HandleKeyEvent implements KeyEventHandler.
260func (a *AssistantMessageItem) HandleKeyEvent(key tea.KeyMsg) (bool, tea.Cmd) {
261	if k := key.String(); k == "c" || k == "y" {
262		text := a.message.Content().Text
263		return true, common.CopyToClipboard(text, "Message copied to clipboard")
264	}
265	return false, nil
266}