/** * @license * Copyright 2025 Google LLC * SPDX-License-Identifier: Apache-2.0 */ import type React from 'react'; import { useMemo } from 'react'; import { Box, Text } from 'ink'; import type { IndividualToolCallDisplay } from '../../types.js'; import { ToolCallStatus } from '../../types.js'; import { ToolMessage } from './ToolMessage.js'; import { ShellToolMessage } from './ShellToolMessage.js'; import { theme } from '../../semantic-colors.js'; import { useConfig } from '../../contexts/ConfigContext.js'; import { isShellTool, isThisShellFocused } from './ToolShared.js'; import { shouldHideAskUserTool } from '@google/gemini-cli-core'; import { ShowMoreLines } from '../ShowMoreLines.js'; import { useUIState } from '../../contexts/UIStateContext.js'; interface ToolGroupMessageProps { groupId: number; toolCalls: IndividualToolCallDisplay[]; availableTerminalHeight?: number; terminalWidth: number; activeShellPtyId?: number | null; embeddedShellFocused?: boolean; onShellInputSubmit?: (input: string) => void; borderTop?: boolean; borderBottom?: boolean; } // Main component renders the border and maps the tools using ToolMessage const TOOL_MESSAGE_HORIZONTAL_MARGIN = 4; export const ToolGroupMessage: React.FC = ({ toolCalls: allToolCalls, availableTerminalHeight, terminalWidth, activeShellPtyId, embeddedShellFocused, borderTop: borderTopOverride, borderBottom: borderBottomOverride, }) => { // Filter out Ask User tools that should be hidden (e.g. in-progress or errors without result) const toolCalls = useMemo( () => allToolCalls.filter( (t) => !shouldHideAskUserTool(t.name, t.status, !!t.resultDisplay), ), [allToolCalls], ); const config = useConfig(); const { constrainHeight } = useUIState(); // We HIDE tools that are still in pre-execution states (Confirming, Pending) // from the History log. They live in the Global Queue or wait for their turn. // Only show tools that are actually running or finished. // We explicitly exclude Pending and Confirming to ensure they only // appear in the Global Queue until they are approved and start executing. const visibleToolCalls = useMemo( () => toolCalls.filter( (t) => t.status !== ToolCallStatus.Pending && t.status !== ToolCallStatus.Confirming, ), [toolCalls], ); const isEmbeddedShellFocused = visibleToolCalls.some((t) => isThisShellFocused( t.name, t.status, t.ptyId, activeShellPtyId, embeddedShellFocused, ), ); const hasPending = !visibleToolCalls.every( (t) => t.status === ToolCallStatus.Success, ); const isShellCommand = toolCalls.some((t) => isShellTool(t.name)); const borderColor = (isShellCommand && hasPending) || isEmbeddedShellFocused ? theme.ui.symbol : hasPending ? theme.status.warning : theme.border.default; const borderDimColor = hasPending && (!isShellCommand || !isEmbeddedShellFocused); const staticHeight = /* border */ 2 + /* marginBottom */ 1; // If all tools are filtered out (e.g., in-progress AskUser tools, confirming tools), // only render if we need to close a border from previous // tool groups. borderBottomOverride=true means we must render the closing border; // undefined or false means there's nothing to display. if (visibleToolCalls.length === 0 && borderBottomOverride !== true) { return null; } let countToolCallsWithResults = 0; for (const tool of visibleToolCalls) { if (tool.resultDisplay !== undefined && tool.resultDisplay !== '') { countToolCallsWithResults++; } } const countOneLineToolCalls = visibleToolCalls.length - countToolCallsWithResults; const availableTerminalHeightPerToolMessage = availableTerminalHeight ? Math.max( Math.floor( (availableTerminalHeight - staticHeight - countOneLineToolCalls) / Math.max(1, countToolCallsWithResults), ), 1, ) : undefined; const contentWidth = terminalWidth - TOOL_MESSAGE_HORIZONTAL_MARGIN; return ( // This box doesn't have a border even though it conceptually does because // we need to allow the sticky headers to render the borders themselves so // that the top border can be sticky. {visibleToolCalls.map((tool, index) => { const isFirst = index === 0; const isShellToolCall = isShellTool(tool.name); const commonProps = { ...tool, availableTerminalHeight: availableTerminalHeightPerToolMessage, terminalWidth: contentWidth, emphasis: 'medium' as const, isFirst: borderTopOverride !== undefined ? borderTopOverride && isFirst : isFirst, borderColor, borderDimColor, }; return ( {isShellToolCall ? ( ) : ( )} {tool.outputFile && ( Output too long and was saved to: {tool.outputFile} )} ); })} { /* We have to keep the bottom border separate so it doesn't get drawn over by the sticky header directly inside it. */ (visibleToolCalls.length > 0 || borderBottomOverride !== undefined) && ( ) } {(borderBottomOverride ?? true) && visibleToolCalls.length > 0 && ( )} ); };