section.go

 1package chat
 2
 3import (
 4	"fmt"
 5	"time"
 6
 7	"github.com/charmbracelet/crush/internal/message"
 8	"github.com/charmbracelet/crush/internal/ui/common"
 9	"github.com/charmbracelet/crush/internal/ui/styles"
10	"github.com/google/uuid"
11)
12
13// SectionItem represents a section separator showing model info and response time.
14type SectionItem struct {
15	id                  string
16	msg                 message.Message
17	lastUserMessageTime time.Time
18	modelName           string
19	sty                 *styles.Styles
20}
21
22// NewSectionItem creates a new section item showing assistant response metadata.
23func NewSectionItem(msg message.Message, lastUserMessageTime time.Time, modelName string, sty *styles.Styles) *SectionItem {
24	return &SectionItem{
25		id:                  uuid.NewString(),
26		msg:                 msg,
27		lastUserMessageTime: lastUserMessageTime,
28		modelName:           modelName,
29		sty:                 sty,
30	}
31}
32
33// ID implements Identifiable.
34func (m *SectionItem) ID() string {
35	return m.id
36}
37
38// Render implements list.Item.
39func (m *SectionItem) Render(width int) string {
40	finishData := m.msg.FinishPart()
41	if finishData == nil {
42		return ""
43	}
44
45	finishTime := time.Unix(finishData.Time, 0)
46	duration := finishTime.Sub(m.lastUserMessageTime)
47
48	icon := m.sty.Chat.Message.SectionIcon.Render(styles.ModelIcon)
49	modelFormatted := m.sty.Chat.Message.SectionModel.Render(m.modelName)
50	durationFormatted := m.sty.Chat.Message.SectionDuration.Render(duration.String())
51
52	text := fmt.Sprintf("%s %s %s", icon, modelFormatted, durationFormatted)
53
54	section := common.Section(m.sty, text, width-2)
55	return m.sty.Chat.Message.SectionHeader.Render(section)
56}