user.go

  1package chat
  2
  3import (
  4	"encoding/xml"
  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/attachments"
 11	"github.com/charmbracelet/crush/internal/ui/common"
 12	"github.com/charmbracelet/crush/internal/ui/list"
 13	"github.com/charmbracelet/crush/internal/ui/styles"
 14)
 15
 16// skillInvocation represents the XML structure for a loaded skill.
 17type skillInvocation struct {
 18	Name         string `xml:"name"`
 19	Description  string `xml:"description"`
 20	Location     string `xml:"location"`
 21	Instructions string `xml:"instructions"`
 22}
 23
 24// UserMessageItem represents a user message in the chat UI.
 25type UserMessageItem struct {
 26	*list.Versioned
 27	*highlightableMessageItem
 28	*cachedMessageItem
 29	*focusableMessageItem
 30
 31	attachments *attachments.Renderer
 32	message     *message.Message
 33	sty         *styles.Styles
 34}
 35
 36// NewUserMessageItem creates a new UserMessageItem.
 37func NewUserMessageItem(sty *styles.Styles, message *message.Message, attachments *attachments.Renderer) MessageItem {
 38	v := list.NewVersioned()
 39	return &UserMessageItem{
 40		Versioned:                v,
 41		highlightableMessageItem: defaultHighlighter(sty, v),
 42		cachedMessageItem:        &cachedMessageItem{},
 43		focusableMessageItem:     newFocusableMessageItem(v),
 44		attachments:              attachments,
 45		message:                  message,
 46		sty:                      sty,
 47	}
 48}
 49
 50// Finished implements list.Item. User messages are immutable once
 51// submitted, so the entry is always safe to freeze.
 52func (m *UserMessageItem) Finished() bool {
 53	return true
 54}
 55
 56// RawRender implements [MessageItem].
 57func (m *UserMessageItem) RawRender(width int) string {
 58	cappedWidth := cappedMessageWidth(width)
 59
 60	content, height, ok := m.getCachedRender(cappedWidth)
 61	// cache hit
 62	if ok {
 63		return m.renderHighlighted(content, cappedWidth, height)
 64	}
 65
 66	msgContent := strings.TrimSpace(m.message.Content().Text)
 67
 68	// Check if this is a skill invocation (loaded_skill XML)
 69	if strings.HasPrefix(msgContent, "<loaded_skill>") {
 70		content = m.renderSkillInvocation(msgContent, cappedWidth)
 71		height = lipgloss.Height(content)
 72		m.setCachedRender(content, cappedWidth, height)
 73		return m.renderHighlighted(content, cappedWidth, height)
 74	}
 75
 76	renderer := common.MarkdownRenderer(m.sty, cappedWidth)
 77
 78	result, err := renderer.Render(msgContent)
 79	if err != nil {
 80		content = msgContent
 81	} else {
 82		content = strings.TrimSuffix(result, "\n")
 83	}
 84
 85	if len(m.message.BinaryContent()) > 0 {
 86		attachmentsStr := m.renderAttachments(cappedWidth)
 87		if content == "" {
 88			content = attachmentsStr
 89		} else {
 90			content = strings.Join([]string{content, "", attachmentsStr}, "\n")
 91		}
 92	}
 93
 94	height = lipgloss.Height(content)
 95	m.setCachedRender(content, cappedWidth, height)
 96	return m.renderHighlighted(content, cappedWidth, height)
 97}
 98
 99// renderSkillInvocation renders a loaded_skill XML as a special UI element.
100func (m *UserMessageItem) renderSkillInvocation(content string, width int) string {
101	var skill skillInvocation
102	if err := xml.Unmarshal([]byte(content), &skill); err != nil {
103		// If parsing fails, just render as markdown
104		renderer := common.MarkdownRenderer(m.sty, width)
105		result, err := renderer.Render(content)
106		if err != nil {
107			return content
108		}
109		return strings.TrimSuffix(result, "\n")
110	}
111
112	return toolOutputSkillContent(m.sty, skill.Name, skill.Description)
113}
114
115// Render implements MessageItem.
116func (m *UserMessageItem) Render(width int) string {
117	// Bypass the prefix cache while a highlight range is active so
118	// selection drags reflect immediately without invalidating the
119	// cache. Highlight changes are intentionally applied "above" the
120	// prefix cache.
121	useCache := !m.isHighlighted()
122	var key uint64
123	if m.focused {
124		key = 1
125	}
126	if useCache {
127		if cached, ok := m.getCachedPrefixedRender(width, key); ok {
128			return cached
129		}
130	}
131	var prefix string
132	if m.focused {
133		prefix = m.sty.Messages.UserFocused.Render()
134	} else {
135		prefix = m.sty.Messages.UserBlurred.Render()
136	}
137	lines := strings.Split(m.RawRender(width), "\n")
138	for i, line := range lines {
139		lines[i] = prefix + line
140	}
141	out := strings.Join(lines, "\n")
142	if useCache {
143		m.setCachedPrefixedRender(out, width, key)
144	}
145	return out
146}
147
148// ID implements MessageItem.
149func (m *UserMessageItem) ID() string {
150	return m.message.ID
151}
152
153// renderAttachments renders attachments.
154func (m *UserMessageItem) renderAttachments(width int) string {
155	var attachments []message.Attachment
156	for _, at := range m.message.BinaryContent() {
157		attachments = append(attachments, message.Attachment{
158			FileName: at.Path,
159			MimeType: at.MIMEType,
160		})
161	}
162	return m.attachments.Render(attachments, false, width)
163}
164
165// HandleKeyEvent implements KeyEventHandler.
166func (m *UserMessageItem) HandleKeyEvent(key tea.KeyMsg) (bool, tea.Cmd) {
167	if k := key.String(); k == "c" || k == "y" {
168		text := m.message.Content().Text
169		return true, common.CopyToClipboard(text, "Message copied to clipboard")
170	}
171	return false, nil
172}