|
| 1 | +import { memo, useCallback } from 'react' |
| 2 | + |
| 3 | +import { renderToolComponent } from '../tools/registry' |
| 4 | +import { ToolCallItem } from '../tools/tool-call-item' |
| 5 | +import { useTheme } from '../../hooks/use-theme' |
| 6 | +import { getToolDisplayInfo } from '../../utils/codebuff-client' |
| 7 | +import type { MarkdownPalette } from '../../utils/markdown-renderer' |
| 8 | +import { ContentWithMarkdown } from './content-with-markdown' |
| 9 | +import type { ContentBlock } from '../../types/chat' |
| 10 | + |
| 11 | +interface ToolBranchProps { |
| 12 | + toolBlock: Extract<ContentBlock, { type: 'tool' }> |
| 13 | + indentLevel: number |
| 14 | + keyPrefix: string |
| 15 | + availableWidth: number |
| 16 | + collapsedAgents: Set<string> |
| 17 | + streamingAgents: Set<string> |
| 18 | + onToggleCollapsed: (id: string) => void |
| 19 | + markdownPalette: MarkdownPalette |
| 20 | +} |
| 21 | + |
| 22 | +export const ToolBranch = memo( |
| 23 | + ({ |
| 24 | + toolBlock, |
| 25 | + indentLevel, |
| 26 | + keyPrefix, |
| 27 | + availableWidth, |
| 28 | + collapsedAgents, |
| 29 | + streamingAgents, |
| 30 | + onToggleCollapsed, |
| 31 | + markdownPalette, |
| 32 | + }: ToolBranchProps) => { |
| 33 | + const theme = useTheme() |
| 34 | + |
| 35 | + const sanitizePreview = (value: string): string => |
| 36 | + value.replace(/[#*_`~\[\]()]/g, '').trim() |
| 37 | + |
| 38 | + if (toolBlock.toolName === 'end_turn') { |
| 39 | + return null |
| 40 | + } |
| 41 | + if ('includeToolCall' in toolBlock && toolBlock.includeToolCall === false) { |
| 42 | + return null |
| 43 | + } |
| 44 | + |
| 45 | + const displayInfo = getToolDisplayInfo(toolBlock.toolName) |
| 46 | + const isCollapsed = collapsedAgents.has(toolBlock.toolCallId) |
| 47 | + const isStreaming = streamingAgents.has(toolBlock.toolCallId) |
| 48 | + |
| 49 | + const inputContent = `\`\`\`json\n${JSON.stringify(toolBlock.input, null, 2)}\n\`\`\`` |
| 50 | + const codeBlockLang = |
| 51 | + toolBlock.toolName === 'run_terminal_command' ? '' : 'yaml' |
| 52 | + const resultContent = toolBlock.output |
| 53 | + ? `\n\n**Result:**\n\`\`\`${codeBlockLang}\n${toolBlock.output}\n\`\`\`` |
| 54 | + : '' |
| 55 | + const fullContent = inputContent + resultContent |
| 56 | + |
| 57 | + const lines = fullContent.split('\n').filter((line) => line.trim()) |
| 58 | + const firstLine = lines[0] || '' |
| 59 | + const lastLine = lines[lines.length - 1] || firstLine |
| 60 | + const commandPreview = |
| 61 | + toolBlock.toolName === 'run_terminal_command' && |
| 62 | + toolBlock.input && |
| 63 | + typeof toolBlock.input === 'object' && |
| 64 | + 'command' in toolBlock.input && |
| 65 | + typeof toolBlock.input.command === 'string' |
| 66 | + ? `$ ${toolBlock.input.command.trim()}` |
| 67 | + : null |
| 68 | + |
| 69 | + let toolRenderConfig = renderToolComponent(toolBlock, theme, { |
| 70 | + availableWidth, |
| 71 | + indentationOffset: 0, |
| 72 | + previewPrefix: '', |
| 73 | + labelWidth: 0, |
| 74 | + }) |
| 75 | + |
| 76 | + const streamingPreview = isStreaming |
| 77 | + ? commandPreview ?? `${sanitizePreview(firstLine)}...` |
| 78 | + : '' |
| 79 | + |
| 80 | + const getToolFinishedPreview = useCallback( |
| 81 | + (commandPrev: string | null, lastLn: string): string => { |
| 82 | + if (commandPrev) { |
| 83 | + return commandPrev |
| 84 | + } |
| 85 | + |
| 86 | + if (toolBlock.toolName === 'run_terminal_command' && toolBlock.output) { |
| 87 | + const outputLines = toolBlock.output |
| 88 | + .split('\n') |
| 89 | + .filter((line) => line.trim()) |
| 90 | + const lastThreeLines = outputLines.slice(-3) |
| 91 | + const hasMoreLines = outputLines.length > 3 |
| 92 | + const preview = lastThreeLines.join('\n') |
| 93 | + return hasMoreLines ? `...\n${preview}` : preview |
| 94 | + } |
| 95 | + |
| 96 | + return sanitizePreview(lastLn) |
| 97 | + }, |
| 98 | + [toolBlock], |
| 99 | + ) |
| 100 | + |
| 101 | + const finishedPreview = !isStreaming |
| 102 | + ? toolRenderConfig?.collapsedPreview ?? |
| 103 | + getToolFinishedPreview(commandPreview, lastLine) |
| 104 | + : '' |
| 105 | + |
| 106 | + const indentationOffset = indentLevel * 2 |
| 107 | + const agentMarkdownOptions = { |
| 108 | + codeBlockWidth: Math.max(10, availableWidth - 12 - indentationOffset), |
| 109 | + palette: { |
| 110 | + ...markdownPalette, |
| 111 | + codeTextFg: theme.foreground, |
| 112 | + }, |
| 113 | + } |
| 114 | + |
| 115 | + const displayContent = ( |
| 116 | + <ContentWithMarkdown |
| 117 | + content={fullContent} |
| 118 | + isStreaming={false} |
| 119 | + codeBlockWidth={agentMarkdownOptions.codeBlockWidth} |
| 120 | + palette={agentMarkdownOptions.palette} |
| 121 | + /> |
| 122 | + ) |
| 123 | + |
| 124 | + const renderableDisplayContent = |
| 125 | + displayContent === null || |
| 126 | + displayContent === undefined || |
| 127 | + displayContent === false || |
| 128 | + displayContent === '' ? null : ( |
| 129 | + <text |
| 130 | + fg={theme.foreground} |
| 131 | + style={{ wrapMode: 'word' }} |
| 132 | + attributes={ |
| 133 | + theme.messageTextAttributes && theme.messageTextAttributes !== 0 |
| 134 | + ? theme.messageTextAttributes |
| 135 | + : undefined |
| 136 | + } |
| 137 | + > |
| 138 | + {displayContent} |
| 139 | + </text> |
| 140 | + ) |
| 141 | + |
| 142 | + const headerName = displayInfo.name |
| 143 | + |
| 144 | + const handleToggle = useCallback(() => { |
| 145 | + onToggleCollapsed(toolBlock.toolCallId) |
| 146 | + }, [onToggleCollapsed, toolBlock.toolCallId]) |
| 147 | + |
| 148 | + return ( |
| 149 | + <box key={keyPrefix}> |
| 150 | + {toolRenderConfig ? ( |
| 151 | + toolRenderConfig.content |
| 152 | + ) : ( |
| 153 | + <ToolCallItem |
| 154 | + name={headerName} |
| 155 | + content={renderableDisplayContent} |
| 156 | + isCollapsed={isCollapsed} |
| 157 | + isStreaming={isStreaming} |
| 158 | + streamingPreview={streamingPreview} |
| 159 | + finishedPreview={finishedPreview} |
| 160 | + onToggle={handleToggle} |
| 161 | + titleSuffix={undefined} |
| 162 | + /> |
| 163 | + )} |
| 164 | + </box> |
| 165 | + ) |
| 166 | + }, |
| 167 | +) |
0 commit comments