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