1package messages
2
3import (
4 "fmt"
5
6 "github.com/charmbracelet/bubbles/v2/spinner"
7 tea "github.com/charmbracelet/bubbletea/v2"
8 "github.com/charmbracelet/crush/internal/message"
9 "github.com/charmbracelet/crush/internal/tui/components/anim"
10 "github.com/charmbracelet/crush/internal/tui/components/core/layout"
11 "github.com/charmbracelet/crush/internal/tui/styles"
12 "github.com/charmbracelet/crush/internal/tui/util"
13 "github.com/charmbracelet/lipgloss/v2"
14 "github.com/charmbracelet/x/ansi"
15)
16
17// ToolCallCmp defines the interface for tool call components in the chat interface.
18// It manages the display of tool execution including pending states, results, and errors.
19type ToolCallCmp interface {
20 util.Model // Basic Bubble Tea model interface
21 layout.Sizeable // Width/height management
22 layout.Focusable // Focus state management
23 GetToolCall() message.ToolCall // Access to tool call data
24 GetToolResult() message.ToolResult // Access to tool result data
25 SetToolResult(message.ToolResult) // Update tool result
26 SetToolCall(message.ToolCall) // Update tool call
27 SetCancelled() // Mark as cancelled
28 ParentMessageID() string // Get parent message ID
29 Spinning() bool // Animation state for pending tools
30 GetNestedToolCalls() []ToolCallCmp // Get nested tool calls
31 SetNestedToolCalls([]ToolCallCmp) // Set nested tool calls
32 SetIsNested(bool) // Set whether this tool call is nested
33}
34
35// toolCallCmp implements the ToolCallCmp interface for displaying tool calls.
36// It handles rendering of tool execution states including pending, completed, and error states.
37type toolCallCmp struct {
38 width int // Component width for text wrapping
39 focused bool // Focus state for border styling
40 isNested bool // Whether this tool call is nested within another
41
42 // Tool call data and state
43 parentMessageID string // ID of the message that initiated this tool call
44 call message.ToolCall // The tool call being executed
45 result message.ToolResult // The result of the tool execution
46 cancelled bool // Whether the tool call was cancelled
47
48 // Animation state for pending tool calls
49 spinning bool // Whether to show loading animation
50 anim util.Model // Animation component for pending states
51
52 nestedToolCalls []ToolCallCmp // Nested tool calls for hierarchical display
53}
54
55// ToolCallOption provides functional options for configuring tool call components
56type ToolCallOption func(*toolCallCmp)
57
58// WithToolCallCancelled marks the tool call as cancelled
59func WithToolCallCancelled() ToolCallOption {
60 return func(m *toolCallCmp) {
61 m.cancelled = true
62 }
63}
64
65// WithToolCallResult sets the initial tool result
66func WithToolCallResult(result message.ToolResult) ToolCallOption {
67 return func(m *toolCallCmp) {
68 m.result = result
69 }
70}
71
72func WithToolCallNested(isNested bool) ToolCallOption {
73 return func(m *toolCallCmp) {
74 m.isNested = isNested
75 }
76}
77
78func WithToolCallNestedCalls(calls []ToolCallCmp) ToolCallOption {
79 return func(m *toolCallCmp) {
80 m.nestedToolCalls = calls
81 }
82}
83
84// NewToolCallCmp creates a new tool call component with the given parent message ID,
85// tool call, and optional configuration
86func NewToolCallCmp(parentMessageID string, tc message.ToolCall, opts ...ToolCallOption) ToolCallCmp {
87 m := &toolCallCmp{
88 call: tc,
89 parentMessageID: parentMessageID,
90 }
91 for _, opt := range opts {
92 opt(m)
93 }
94 m.anim = anim.New(15, "Working")
95 if m.isNested {
96 m.anim = anim.New(10, "")
97 }
98 return m
99}
100
101// Init initializes the tool call component and starts animations if needed.
102// Returns a command to start the animation for pending tool calls.
103func (m *toolCallCmp) Init() tea.Cmd {
104 m.spinning = m.shouldSpin()
105 if m.spinning {
106 return m.anim.Init()
107 }
108 return nil
109}
110
111// Update handles incoming messages and updates the component state.
112// Manages animation updates for pending tool calls.
113func (m *toolCallCmp) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
114 switch msg := msg.(type) {
115 case anim.ColorCycleMsg, anim.StepCharsMsg, spinner.TickMsg:
116 var cmds []tea.Cmd
117 for i, nested := range m.nestedToolCalls {
118 if nested.Spinning() {
119 u, cmd := nested.Update(msg)
120 m.nestedToolCalls[i] = u.(ToolCallCmp)
121 cmds = append(cmds, cmd)
122 }
123 }
124 if m.spinning {
125 u, cmd := m.anim.Update(msg)
126 m.anim = u.(util.Model)
127 cmds = append(cmds, cmd)
128 }
129 return m, tea.Batch(cmds...)
130 }
131 return m, nil
132}
133
134// View renders the tool call component based on its current state.
135// Shows either a pending animation or the tool-specific rendered result.
136func (m *toolCallCmp) View() tea.View {
137 box := m.style()
138
139 if !m.call.Finished && !m.cancelled {
140 return tea.NewView(box.Render(m.renderPending()))
141 }
142
143 r := registry.lookup(m.call.Name)
144
145 if m.isNested {
146 return tea.NewView(box.Render(r.Render(m)))
147 }
148 return tea.NewView(box.Render(r.Render(m)))
149}
150
151// State management methods
152
153// SetCancelled marks the tool call as cancelled
154func (m *toolCallCmp) SetCancelled() {
155 m.cancelled = true
156}
157
158// SetToolCall updates the tool call data and stops spinning if finished
159func (m *toolCallCmp) SetToolCall(call message.ToolCall) {
160 m.call = call
161 if m.call.Finished {
162 m.spinning = false
163 }
164}
165
166// ParentMessageID returns the ID of the message that initiated this tool call
167func (m *toolCallCmp) ParentMessageID() string {
168 return m.parentMessageID
169}
170
171// SetToolResult updates the tool result and stops the spinning animation
172func (m *toolCallCmp) SetToolResult(result message.ToolResult) {
173 m.result = result
174 m.spinning = false
175}
176
177// GetToolCall returns the current tool call data
178func (m *toolCallCmp) GetToolCall() message.ToolCall {
179 return m.call
180}
181
182// GetToolResult returns the current tool result data
183func (m *toolCallCmp) GetToolResult() message.ToolResult {
184 return m.result
185}
186
187// GetNestedToolCalls returns the nested tool calls
188func (m *toolCallCmp) GetNestedToolCalls() []ToolCallCmp {
189 return m.nestedToolCalls
190}
191
192// SetNestedToolCalls sets the nested tool calls
193func (m *toolCallCmp) SetNestedToolCalls(calls []ToolCallCmp) {
194 m.nestedToolCalls = calls
195 for _, nested := range m.nestedToolCalls {
196 nested.SetSize(m.width, 0)
197 }
198}
199
200// SetIsNested sets whether this tool call is nested within another
201func (m *toolCallCmp) SetIsNested(isNested bool) {
202 m.isNested = isNested
203}
204
205// Rendering methods
206
207// renderPending displays the tool name with a loading animation for pending tool calls
208func (m *toolCallCmp) renderPending() string {
209 t := styles.CurrentTheme()
210 icon := t.S().Base.Foreground(t.GreenDark).Render(styles.ToolPending)
211 tool := t.S().Base.Foreground(t.Blue).Render(prettifyToolName(m.call.Name))
212 return fmt.Sprintf("%s %s %s", icon, tool, m.anim.View())
213}
214
215// style returns the lipgloss style for the tool call component.
216// Applies muted colors and focus-dependent border styles.
217func (m *toolCallCmp) style() lipgloss.Style {
218 t := styles.CurrentTheme()
219
220 if m.isNested {
221 return t.S().Muted
222 }
223 return t.S().Muted.PaddingLeft(4)
224}
225
226// textWidth calculates the available width for text content,
227// accounting for borders and padding
228func (m *toolCallCmp) textWidth() int {
229 return m.width - 5 // take into account the border and PaddingLeft
230}
231
232// fit truncates content to fit within the specified width with ellipsis
233func (m *toolCallCmp) fit(content string, width int) string {
234 t := styles.CurrentTheme()
235 lineStyle := t.S().Muted.Background(t.BgSubtle)
236 dots := lineStyle.Render("...")
237 return ansi.Truncate(content, width, dots)
238}
239
240// Focus management methods
241
242// Blur removes focus from the tool call component
243func (m *toolCallCmp) Blur() tea.Cmd {
244 m.focused = false
245 return nil
246}
247
248// Focus sets focus on the tool call component
249func (m *toolCallCmp) Focus() tea.Cmd {
250 m.focused = true
251 return nil
252}
253
254// IsFocused returns whether the tool call component is currently focused
255func (m *toolCallCmp) IsFocused() bool {
256 return m.focused
257}
258
259// Size management methods
260
261// GetSize returns the current dimensions of the tool call component
262func (m *toolCallCmp) GetSize() (int, int) {
263 return m.width, 0
264}
265
266// SetSize updates the width of the tool call component for text wrapping
267func (m *toolCallCmp) SetSize(width int, height int) tea.Cmd {
268 m.width = width
269 for _, nested := range m.nestedToolCalls {
270 nested.SetSize(width, height)
271 }
272 return nil
273}
274
275// shouldSpin determines whether the tool call should show a loading animation.
276// Returns true if the tool call is not finished or if the result doesn't match the call ID.
277func (m *toolCallCmp) shouldSpin() bool {
278 return !m.call.Finished
279}
280
281// Spinning returns whether the tool call is currently showing a loading animation
282func (m *toolCallCmp) Spinning() bool {
283 if m.spinning {
284 return true
285 }
286 for _, nested := range m.nestedToolCalls {
287 if nested.Spinning() {
288 return true
289 }
290 }
291 return m.spinning
292}