|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import React from 'react' |
| 3 | + |
| 4 | +import { useTheme } from '../../hooks/use-theme' |
| 5 | +import { defineToolComponent } from './types' |
| 6 | + |
| 7 | +import type { ToolRenderConfig } from './types' |
| 8 | + |
| 9 | +interface ReadFilesSimpleToolCallItemProps { |
| 10 | + name: string |
| 11 | + filePaths: string[] |
| 12 | + branchChar: string |
| 13 | + maxNewlineFiles?: number |
| 14 | +} |
| 15 | + |
| 16 | +const ReadFilesSimpleToolCallItem = ({ |
| 17 | + name, |
| 18 | + filePaths, |
| 19 | + branchChar, |
| 20 | + maxNewlineFiles = 2, |
| 21 | +}: ReadFilesSimpleToolCallItemProps) => { |
| 22 | + const theme = useTheme() |
| 23 | + const bulletChar = '• ' |
| 24 | + |
| 25 | + // Split files into two groups |
| 26 | + const firstFilePath = filePaths[0] |
| 27 | + const newlineFiles = filePaths.slice(1, maxNewlineFiles) |
| 28 | + const inlineFiles = filePaths.slice(maxNewlineFiles) |
| 29 | + |
| 30 | + return ( |
| 31 | + <box style={{ flexDirection: 'column', gap: 0, width: '100%' }}> |
| 32 | + {/* First line with name */} |
| 33 | + <box |
| 34 | + style={{ flexDirection: 'row', alignItems: 'center', width: '100%' }} |
| 35 | + > |
| 36 | + <text style={{ wrapMode: 'word' }}> |
| 37 | + <span fg={theme.foreground}>{branchChar || bulletChar}</span> |
| 38 | + <span fg={theme.foreground} attributes={TextAttributes.BOLD}> |
| 39 | + {name} |
| 40 | + </span> |
| 41 | + </text> |
| 42 | + {firstFilePath && ( |
| 43 | + <text style={{ wrapMode: 'word' }}> |
| 44 | + <span fg={theme.foreground}>{' ' + firstFilePath}</span> |
| 45 | + </text> |
| 46 | + )} |
| 47 | + </box> |
| 48 | + |
| 49 | + {/* Files on newlines */} |
| 50 | + {newlineFiles.map((file, index) => ( |
| 51 | + <box |
| 52 | + key={`newline-${index}`} |
| 53 | + style={{ |
| 54 | + flexDirection: 'row', |
| 55 | + alignItems: 'center', |
| 56 | + width: '100%', |
| 57 | + paddingLeft: 7, |
| 58 | + }} |
| 59 | + > |
| 60 | + <text style={{ wrapMode: 'word' }}> |
| 61 | + <span fg={theme.foreground}>{file}</span> |
| 62 | + </text> |
| 63 | + </box> |
| 64 | + ))} |
| 65 | + |
| 66 | + {/* Remaining files inline */} |
| 67 | + {inlineFiles.length > 0 && ( |
| 68 | + <box |
| 69 | + style={{ |
| 70 | + flexDirection: 'row', |
| 71 | + alignItems: 'center', |
| 72 | + width: '100%', |
| 73 | + paddingLeft: 7, |
| 74 | + }} |
| 75 | + > |
| 76 | + <text style={{ wrapMode: 'word' }}> |
| 77 | + <span fg={theme.foreground}>{inlineFiles.join(', ')}</span> |
| 78 | + </text> |
| 79 | + </box> |
| 80 | + )} |
| 81 | + </box> |
| 82 | + ) |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * UI component for read_files tool. |
| 87 | + * Displays file paths with first few on newlines, rest inline. |
| 88 | + * Does not support expand/collapse - always shows as a simple list. |
| 89 | + */ |
| 90 | +export const ReadFilesComponent = defineToolComponent({ |
| 91 | + toolName: 'read_files', |
| 92 | + |
| 93 | + render(toolBlock, theme, options): ToolRenderConfig | null { |
| 94 | + const input = toolBlock.input as any |
| 95 | + |
| 96 | + // Extract file paths from input |
| 97 | + let filePaths: string[] = [] |
| 98 | + |
| 99 | + if (Array.isArray(input?.paths)) { |
| 100 | + filePaths = input.paths.filter( |
| 101 | + (path: any) => typeof path === 'string' && path.trim().length > 0, |
| 102 | + ) |
| 103 | + } |
| 104 | + |
| 105 | + if (filePaths.length === 0) { |
| 106 | + return null |
| 107 | + } |
| 108 | + |
| 109 | + return { |
| 110 | + content: ( |
| 111 | + <ReadFilesSimpleToolCallItem |
| 112 | + name="Read" |
| 113 | + filePaths={filePaths} |
| 114 | + branchChar={options.branchChar} |
| 115 | + /> |
| 116 | + ), |
| 117 | + } |
| 118 | + }, |
| 119 | +}) |
0 commit comments