| |
| |
| |
| |
| |
|
|
| import React from 'react'; |
| import { Box, Text } from 'ink'; |
| import type { IndividualToolCallDisplay } from '../../types.js'; |
| import { ToolCallStatus } from '../../types.js'; |
| import { DiffRenderer } from './DiffRenderer.js'; |
| import { Colors } from '../../colors.js'; |
| import { MarkdownDisplay } from '../../utils/MarkdownDisplay.js'; |
| import { GeminiRespondingSpinner } from '../GeminiRespondingSpinner.js'; |
| import { MaxSizedBox } from '../shared/MaxSizedBox.js'; |
| import { TOOL_STATUS } from '../../constants.js'; |
|
|
| const STATIC_HEIGHT = 1; |
| const RESERVED_LINE_COUNT = 5; |
| const STATUS_INDICATOR_WIDTH = 3; |
| const MIN_LINES_SHOWN = 2; |
|
|
| |
| |
| const MAXIMUM_RESULT_DISPLAY_CHARACTERS = 1000000; |
| export type TextEmphasis = 'high' | 'medium' | 'low'; |
|
|
| export interface ToolMessageProps extends IndividualToolCallDisplay { |
| availableTerminalHeight?: number; |
| terminalWidth: number; |
| emphasis?: TextEmphasis; |
| renderOutputAsMarkdown?: boolean; |
| } |
|
|
| export const ToolMessage: React.FC<ToolMessageProps> = ({ |
| name, |
| description, |
| resultDisplay, |
| status, |
| availableTerminalHeight, |
| terminalWidth, |
| emphasis = 'medium', |
| renderOutputAsMarkdown = true, |
| }) => { |
| const availableHeight = availableTerminalHeight |
| ? Math.max( |
| availableTerminalHeight - STATIC_HEIGHT - RESERVED_LINE_COUNT, |
| MIN_LINES_SHOWN + 1, |
| ) |
| : undefined; |
|
|
| |
| |
| |
| if (availableHeight) { |
| renderOutputAsMarkdown = false; |
| } |
|
|
| const childWidth = terminalWidth - 3; |
| if (typeof resultDisplay === 'string') { |
| if (resultDisplay.length > MAXIMUM_RESULT_DISPLAY_CHARACTERS) { |
| |
| resultDisplay = |
| '...' + resultDisplay.slice(-MAXIMUM_RESULT_DISPLAY_CHARACTERS); |
| } |
| } |
| return ( |
| <Box paddingX={1} paddingY={0} flexDirection="column"> |
| <Box minHeight={1}> |
| <ToolStatusIndicator status={status} /> |
| <ToolInfo |
| name={name} |
| status={status} |
| description={description} |
| emphasis={emphasis} |
| /> |
| {emphasis === 'high' && <TrailingIndicator />} |
| </Box> |
| {resultDisplay && ( |
| <Box paddingLeft={STATUS_INDICATOR_WIDTH} width="100%" marginTop={1}> |
| <Box flexDirection="column"> |
| {typeof resultDisplay === 'string' && renderOutputAsMarkdown && ( |
| <Box flexDirection="column"> |
| <MarkdownDisplay |
| text={resultDisplay} |
| isPending={false} |
| availableTerminalHeight={availableHeight} |
| terminalWidth={childWidth} |
| /> |
| </Box> |
| )} |
| {typeof resultDisplay === 'string' && !renderOutputAsMarkdown && ( |
| <MaxSizedBox maxHeight={availableHeight} maxWidth={childWidth}> |
| <Box> |
| <Text wrap="wrap">{resultDisplay}</Text> |
| </Box> |
| </MaxSizedBox> |
| )} |
| {typeof resultDisplay !== 'string' && ( |
| <DiffRenderer |
| diffContent={resultDisplay.fileDiff} |
| filename={resultDisplay.fileName} |
| availableTerminalHeight={availableHeight} |
| terminalWidth={childWidth} |
| /> |
| )} |
| </Box> |
| </Box> |
| )} |
| </Box> |
| ); |
| }; |
|
|
| type ToolStatusIndicatorProps = { |
| status: ToolCallStatus; |
| }; |
|
|
| const ToolStatusIndicator: React.FC<ToolStatusIndicatorProps> = ({ |
| status, |
| }) => ( |
| <Box minWidth={STATUS_INDICATOR_WIDTH}> |
| {status === ToolCallStatus.Pending && ( |
| <Text color={Colors.AccentGreen}>{TOOL_STATUS.PENDING}</Text> |
| )} |
| {status === ToolCallStatus.Executing && ( |
| <GeminiRespondingSpinner |
| spinnerType="toggle" |
| nonRespondingDisplay={TOOL_STATUS.EXECUTING} |
| /> |
| )} |
| {status === ToolCallStatus.Success && ( |
| <Text color={Colors.AccentGreen} aria-label={'Success:'}> |
| {TOOL_STATUS.SUCCESS} |
| </Text> |
| )} |
| {status === ToolCallStatus.Confirming && ( |
| <Text color={Colors.AccentYellow} aria-label={'Confirming:'}> |
| {TOOL_STATUS.CONFIRMING} |
| </Text> |
| )} |
| {status === ToolCallStatus.Canceled && ( |
| <Text color={Colors.AccentYellow} aria-label={'Canceled:'} bold> |
| {TOOL_STATUS.CANCELED} |
| </Text> |
| )} |
| {status === ToolCallStatus.Error && ( |
| <Text color={Colors.AccentRed} aria-label={'Error:'} bold> |
| {TOOL_STATUS.ERROR} |
| </Text> |
| )} |
| </Box> |
| ); |
|
|
| type ToolInfo = { |
| name: string; |
| description: string; |
| status: ToolCallStatus; |
| emphasis: TextEmphasis; |
| }; |
| const ToolInfo: React.FC<ToolInfo> = ({ |
| name, |
| description, |
| status, |
| emphasis, |
| }) => { |
| const nameColor = React.useMemo<string>(() => { |
| switch (emphasis) { |
| case 'high': |
| return Colors.Foreground; |
| case 'medium': |
| return Colors.Foreground; |
| case 'low': |
| return Colors.Gray; |
| default: { |
| const exhaustiveCheck: never = emphasis; |
| return exhaustiveCheck; |
| } |
| } |
| }, [emphasis]); |
| return ( |
| <Box> |
| <Text |
| wrap="truncate-end" |
| strikethrough={status === ToolCallStatus.Canceled} |
| > |
| <Text color={nameColor} bold> |
| {name} |
| </Text>{' '} |
| <Text color={Colors.Gray}>{description}</Text> |
| </Text> |
| </Box> |
| ); |
| }; |
|
|
| const TrailingIndicator: React.FC = () => ( |
| <Text color={Colors.Foreground} wrap="truncate"> |
| {' '} |
| ← |
| </Text> |
| ); |
|
|