|
| 1 | +import { SimpleToolCallItem } from './tool-call-item' |
| 2 | +import { defineToolComponent } from './types' |
| 3 | + |
| 4 | +import type { ToolRenderConfig } from './types' |
| 5 | + |
| 6 | +/** |
| 7 | + * UI component for glob tool. |
| 8 | + * Displays a single line showing the glob pattern and number of matching files. |
| 9 | + * Does not support expand/collapse - always shows as a single line. |
| 10 | + */ |
| 11 | +export const GlobComponent = defineToolComponent({ |
| 12 | + toolName: 'glob', |
| 13 | + |
| 14 | + render(toolBlock, theme, options): ToolRenderConfig | null { |
| 15 | + const input = toolBlock.input as any |
| 16 | + const pattern = input?.pattern ?? '' |
| 17 | + const cwd = input?.cwd ?? '' |
| 18 | + |
| 19 | + // Parse output to get file count |
| 20 | + let fileCount = 0 |
| 21 | + let hasError = false |
| 22 | + |
| 23 | + if (toolBlock.output) { |
| 24 | + const outputArray = Array.isArray(toolBlock.output) |
| 25 | + ? toolBlock.output |
| 26 | + : [toolBlock.output] |
| 27 | + |
| 28 | + for (const item of outputArray) { |
| 29 | + const output = item as any |
| 30 | + if (output?.type === 'json' && output?.value) { |
| 31 | + const value = output.value as any |
| 32 | + if (value.errorMessage) { |
| 33 | + hasError = true |
| 34 | + } else if (typeof value.count === 'number') { |
| 35 | + fileCount = value.count |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + if (!pattern) { |
| 42 | + return null |
| 43 | + } |
| 44 | + |
| 45 | + // Build single-line summary |
| 46 | + let summary = pattern |
| 47 | + |
| 48 | + if (cwd) { |
| 49 | + summary += ` in ${cwd}` |
| 50 | + } |
| 51 | + |
| 52 | + if (hasError) { |
| 53 | + summary += ' (error)' |
| 54 | + } |
| 55 | + // TODO(James): Reenable when we pass tool results as an object |
| 56 | + // else { |
| 57 | + // summary += ` (${fileCount} file${fileCount === 1 ? '' : 's'})` |
| 58 | + // } |
| 59 | + |
| 60 | + return { |
| 61 | + content: <SimpleToolCallItem name="Glob" description={summary} />, |
| 62 | + } |
| 63 | + }, |
| 64 | +}) |
0 commit comments