|
| 1 | +import { TextAttributes } from '@opentui/core' |
| 2 | +import React from 'react' |
| 3 | +import stringWidth from 'string-width' |
| 4 | + |
| 5 | +import type { ContentBlock } from '../chat' |
| 6 | +import type { ChatTheme } from '../utils/theme-system' |
| 7 | + |
| 8 | +type ToolBlock = Extract<ContentBlock, { type: 'tool' }> |
| 9 | + |
| 10 | +export type ToolRenderConfig = { |
| 11 | + titleAccessory?: React.ReactNode |
| 12 | + content?: React.ReactNode |
| 13 | + collapsedPreview?: string |
| 14 | +} |
| 15 | + |
| 16 | +export type ToolRenderOptions = { |
| 17 | + availableWidth: number |
| 18 | + indentationOffset: number |
| 19 | +} |
| 20 | + |
| 21 | +const isRecord = (value: unknown): value is Record<string, unknown> => { |
| 22 | + return typeof value === 'object' && value !== null |
| 23 | +} |
| 24 | + |
| 25 | +const extractPath = (toolBlock: ToolBlock, resultValue: unknown): string | null => { |
| 26 | + if (isRecord(toolBlock.input) && typeof toolBlock.input.path === 'string') { |
| 27 | + const trimmed = toolBlock.input.path.trim() |
| 28 | + if (trimmed.length > 0) { |
| 29 | + return trimmed |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + if (isRecord(resultValue) && typeof resultValue.path === 'string') { |
| 34 | + const trimmed = resultValue.path.trim() |
| 35 | + if (trimmed.length > 0) { |
| 36 | + return trimmed |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + return null |
| 41 | +} |
| 42 | + |
| 43 | +const summarizeFiles = ( |
| 44 | + entries: unknown, |
| 45 | + maxItems: number, |
| 46 | + options: ToolRenderOptions, |
| 47 | +): string | null => { |
| 48 | + const maxWidth = Math.max( |
| 49 | + 20, |
| 50 | + options.availableWidth - options.indentationOffset - 6, |
| 51 | + ) |
| 52 | + |
| 53 | + if (!Array.isArray(entries) || entries.length === 0) { |
| 54 | + return null |
| 55 | + } |
| 56 | + |
| 57 | + const validNames = entries |
| 58 | + .filter((entry): entry is string => typeof entry === 'string') |
| 59 | + .map((entry) => entry.trim()) |
| 60 | + .filter((entry) => entry.length > 0) |
| 61 | + |
| 62 | + if (validNames.length === 0) { |
| 63 | + return null |
| 64 | + } |
| 65 | + |
| 66 | + const summaryNames: string[] = [] |
| 67 | + let widthUsed = 0 |
| 68 | + |
| 69 | + for (let index = 0; index < validNames.length; index += 1) { |
| 70 | + if (summaryNames.length >= maxItems) { |
| 71 | + break |
| 72 | + } |
| 73 | + |
| 74 | + const name = validNames[index] |
| 75 | + const prefix = summaryNames.length === 0 ? '' : ', ' |
| 76 | + const candidate = `${prefix}${name}` |
| 77 | + const candidateWidth = stringWidth(candidate) |
| 78 | + const wouldExceedWidth = widthUsed + candidateWidth > maxWidth |
| 79 | + |
| 80 | + if (summaryNames.length > 0 && wouldExceedWidth) { |
| 81 | + break |
| 82 | + } |
| 83 | + |
| 84 | + summaryNames.push(name) |
| 85 | + widthUsed += candidateWidth |
| 86 | + |
| 87 | + if (summaryNames.length === 1 && wouldExceedWidth) { |
| 88 | + break |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if (summaryNames.length === 0) { |
| 93 | + summaryNames.push(validNames[0]) |
| 94 | + } |
| 95 | + |
| 96 | + const hasMore = summaryNames.length < validNames.length |
| 97 | + const summary = summaryNames.join(', ') |
| 98 | + return hasMore ? `${summary}, ...` : summary |
| 99 | +} |
| 100 | + |
| 101 | +const getListDirectoryRender = ( |
| 102 | + toolBlock: ToolBlock, |
| 103 | + theme: ChatTheme, |
| 104 | + options: ToolRenderOptions, |
| 105 | +): ToolRenderConfig => { |
| 106 | + const MAX_ITEMS = 3 |
| 107 | + const resultValue = Array.isArray(toolBlock.outputRaw) |
| 108 | + ? (toolBlock.outputRaw[0] as any)?.value |
| 109 | + : undefined |
| 110 | + |
| 111 | + if (!isRecord(resultValue)) { |
| 112 | + return {} |
| 113 | + } |
| 114 | + |
| 115 | + const filesLine = summarizeFiles(resultValue.files, MAX_ITEMS, options) |
| 116 | + const fallbackLine = filesLine |
| 117 | + ? null |
| 118 | + : summarizeFiles(resultValue.directories, MAX_ITEMS, options) |
| 119 | + const path = extractPath(toolBlock, resultValue) |
| 120 | + |
| 121 | + const summaryLine = filesLine ?? fallbackLine |
| 122 | + |
| 123 | + if (!summaryLine && !path) { |
| 124 | + return {} |
| 125 | + } |
| 126 | + |
| 127 | + const content = |
| 128 | + summaryLine !== null ? ( |
| 129 | + <text |
| 130 | + fg={theme.agentResponseCount} |
| 131 | + attributes={TextAttributes.ITALIC} |
| 132 | + style={{ wrapMode: 'word' }} |
| 133 | + > |
| 134 | + {summaryLine} |
| 135 | + </text> |
| 136 | + ) : null |
| 137 | + |
| 138 | + const collapsedPreview = summaryLine ?? undefined |
| 139 | + |
| 140 | + const titleAccessory = path ? ( |
| 141 | + <span fg={theme.agentText} attributes={TextAttributes.BOLD}> |
| 142 | + {path} |
| 143 | + </span> |
| 144 | + ) : undefined |
| 145 | + |
| 146 | + return { |
| 147 | + titleAccessory, |
| 148 | + content, |
| 149 | + collapsedPreview, |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +export const getToolRenderConfig = ( |
| 154 | + toolBlock: ToolBlock, |
| 155 | + theme: ChatTheme, |
| 156 | + options: ToolRenderOptions, |
| 157 | +): ToolRenderConfig => { |
| 158 | + switch (toolBlock.toolName) { |
| 159 | + case 'list_directory': |
| 160 | + return getListDirectoryRender(toolBlock, theme, options) |
| 161 | + default: |
| 162 | + return {} |
| 163 | + } |
| 164 | +} |
0 commit comments