|
| 1 | +package searchfiles |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + tea "charm.land/bubbletea/v2" |
| 9 | + |
| 10 | + "github.com/docker/cagent/pkg/tools/builtin" |
| 11 | + "github.com/docker/cagent/pkg/tui/components/spinner" |
| 12 | + "github.com/docker/cagent/pkg/tui/components/toolcommon" |
| 13 | + "github.com/docker/cagent/pkg/tui/core/layout" |
| 14 | + "github.com/docker/cagent/pkg/tui/service" |
| 15 | + "github.com/docker/cagent/pkg/tui/types" |
| 16 | +) |
| 17 | + |
| 18 | +// Component is a specialized component for rendering search_files tool calls. |
| 19 | +type Component struct { |
| 20 | + message *types.Message |
| 21 | + spinner spinner.Spinner |
| 22 | + width int |
| 23 | + height int |
| 24 | +} |
| 25 | + |
| 26 | +// New creates a new search files component. |
| 27 | +func New( |
| 28 | + msg *types.Message, |
| 29 | + _ *service.SessionState, |
| 30 | +) layout.Model { |
| 31 | + return &Component{ |
| 32 | + message: msg, |
| 33 | + spinner: spinner.New(spinner.ModeSpinnerOnly), |
| 34 | + width: 80, |
| 35 | + height: 1, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (c *Component) SetSize(width, height int) tea.Cmd { |
| 40 | + c.width = width |
| 41 | + c.height = height |
| 42 | + return nil |
| 43 | +} |
| 44 | + |
| 45 | +func (c *Component) Init() tea.Cmd { |
| 46 | + if c.message.ToolStatus == types.ToolStatusPending || c.message.ToolStatus == types.ToolStatusRunning { |
| 47 | + return c.spinner.Init() |
| 48 | + } |
| 49 | + return nil |
| 50 | +} |
| 51 | + |
| 52 | +func (c *Component) Update(msg tea.Msg) (layout.Model, tea.Cmd) { |
| 53 | + if c.message.ToolStatus == types.ToolStatusPending || c.message.ToolStatus == types.ToolStatusRunning { |
| 54 | + var cmd tea.Cmd |
| 55 | + var model layout.Model |
| 56 | + model, cmd = c.spinner.Update(msg) |
| 57 | + c.spinner = model.(spinner.Spinner) |
| 58 | + return c, cmd |
| 59 | + } |
| 60 | + |
| 61 | + return c, nil |
| 62 | +} |
| 63 | + |
| 64 | +func (c *Component) View() string { |
| 65 | + msg := c.message |
| 66 | + |
| 67 | + // Parse arguments |
| 68 | + var args builtin.SearchFilesArgs |
| 69 | + if err := json.Unmarshal([]byte(msg.ToolCall.Function.Arguments), &args); err != nil { |
| 70 | + return toolcommon.RenderTool(toolcommon.Icon(msg.ToolStatus), msg.ToolDefinition.DisplayName(), c.spinner.View(), "", c.width) |
| 71 | + } |
| 72 | + |
| 73 | + // Format display name with pattern |
| 74 | + displayName := fmt.Sprintf("%s(%q)", msg.ToolDefinition.DisplayName(), args.Pattern) |
| 75 | + |
| 76 | + // For pending/running state, show spinner |
| 77 | + if msg.ToolStatus == types.ToolStatusPending || msg.ToolStatus == types.ToolStatusRunning { |
| 78 | + return toolcommon.RenderTool(toolcommon.Icon(msg.ToolStatus), displayName, c.spinner.View(), "", c.width) |
| 79 | + } |
| 80 | + |
| 81 | + // For completed/error state, show concise summary |
| 82 | + summary := formatSummary(msg.Content) |
| 83 | + params := fmt.Sprintf(": %s", summary) |
| 84 | + |
| 85 | + return toolcommon.RenderTool(toolcommon.Icon(msg.ToolStatus), displayName, params, "", c.width) |
| 86 | +} |
| 87 | + |
| 88 | +// formatSummary creates a concise summary of the search results |
| 89 | +func formatSummary(content string) string { |
| 90 | + if content == "" { |
| 91 | + return "no result" |
| 92 | + } |
| 93 | + |
| 94 | + // Handle "No files found" case |
| 95 | + if strings.HasPrefix(content, "No files found") { |
| 96 | + return "no file found" |
| 97 | + } |
| 98 | + |
| 99 | + // Handle error cases |
| 100 | + if strings.HasPrefix(content, "Error") { |
| 101 | + return content |
| 102 | + } |
| 103 | + |
| 104 | + // Parse "X files found:\n..." format |
| 105 | + lines := strings.Split(content, "\n") |
| 106 | + if len(lines) > 0 { |
| 107 | + firstLine := lines[0] |
| 108 | + // Extract count from "X files found:" or "X file found:" |
| 109 | + if strings.Contains(firstLine, "file found:") || strings.Contains(firstLine, "files found:") { |
| 110 | + // Check if it's a single file |
| 111 | + if strings.HasPrefix(firstLine, "1 file found:") && len(lines) > 1 { |
| 112 | + // Return "one file found: filename" |
| 113 | + fileName := strings.TrimSpace(lines[1]) |
| 114 | + return fmt.Sprintf("one file found: %s", fileName) |
| 115 | + } |
| 116 | + // Multiple files |
| 117 | + return strings.TrimSuffix(firstLine, ":") + "." |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Fallback |
| 122 | + return content |
| 123 | +} |
0 commit comments