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