From 25baa158aa3837285fd97415f7b1b21b3043f01a Mon Sep 17 00:00:00 2001 From: jacob314 Date: Tue, 17 Feb 2026 08:34:59 -0800 Subject: [PATCH 1/3] Fix bottom border color --- packages/cli/src/ui/App.test.tsx | 14 +- .../src/ui/components/HistoryItemDisplay.tsx | 6 + .../src/ui/components/MainContent.test.tsx | 207 +++++++++++++++++- .../cli/src/ui/components/MainContent.tsx | 174 ++++++++++++--- .../messages/ToolGroupMessage.test.tsx | 89 ++++++-- .../components/messages/ToolGroupMessage.tsx | 36 +-- .../ToolResultDisplayOverflow.test.tsx | 2 + .../ToolStickyHeaderRegression.test.tsx | 4 + .../src/ui/hooks/atCommandProcessor.test.ts | 8 +- packages/cli/src/ui/hooks/toolMapping.ts | 5 +- .../cli/src/ui/hooks/useGeminiStream.test.tsx | 1 + packages/cli/src/ui/hooks/useGeminiStream.ts | 138 ++++++++---- 12 files changed, 565 insertions(+), 119 deletions(-) diff --git a/packages/cli/src/ui/App.test.tsx b/packages/cli/src/ui/App.test.tsx index a6687343281..2e40d35260a 100644 --- a/packages/cli/src/ui/App.test.tsx +++ b/packages/cli/src/ui/App.test.tsx @@ -216,8 +216,18 @@ describe('App', () => { const stateWithConfirmingTool = { ...mockUIState, - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], - pendingGeminiHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], + pendingGeminiHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], } as UIState; const configWithExperiment = makeFakeConfig(); diff --git a/packages/cli/src/ui/components/HistoryItemDisplay.tsx b/packages/cli/src/ui/components/HistoryItemDisplay.tsx index f41ee20895d..3b99bf3d070 100644 --- a/packages/cli/src/ui/components/HistoryItemDisplay.tsx +++ b/packages/cli/src/ui/components/HistoryItemDisplay.tsx @@ -47,6 +47,8 @@ interface HistoryItemDisplayProps { activeShellPtyId?: number | null; embeddedShellFocused?: boolean; availableTerminalHeightGemini?: number; + borderColor?: string; + borderDimColor?: boolean; } export const HistoryItemDisplay: React.FC = ({ @@ -58,6 +60,8 @@ export const HistoryItemDisplay: React.FC = ({ activeShellPtyId, embeddedShellFocused, availableTerminalHeightGemini, + borderColor, + borderDimColor, }) => { const settings = useSettings(); const inlineThinkingMode = getInlineThinkingMode(settings); @@ -181,6 +185,8 @@ export const HistoryItemDisplay: React.FC = ({ embeddedShellFocused={embeddedShellFocused} borderTop={itemForDisplay.borderTop} borderBottom={itemForDisplay.borderBottom} + borderColor={borderColor ?? ''} + borderDimColor={borderDimColor ?? false} /> )} {itemForDisplay.type === 'compression' && ( diff --git a/packages/cli/src/ui/components/MainContent.test.tsx b/packages/cli/src/ui/components/MainContent.test.tsx index 8b69a0f187b..2403f0f3306 100644 --- a/packages/cli/src/ui/components/MainContent.test.tsx +++ b/packages/cli/src/ui/components/MainContent.test.tsx @@ -6,7 +6,7 @@ import { renderWithProviders } from '../../test-utils/render.js'; import { waitFor } from '../../test-utils/async.js'; -import { MainContent } from './MainContent.js'; +import { MainContent, getToolGroupBorderAppearance } from './MainContent.js'; import { describe, it, expect, vi, beforeEach } from 'vitest'; import { Box, Text } from 'ink'; import { act, useState, type JSX } from 'react'; @@ -18,6 +18,7 @@ import { type UIState, } from '../contexts/UIStateContext.js'; import { CoreToolCallStatus } from '@google/gemini-cli-core'; +import { type IndividualToolCallDisplay } from '../types.js'; // Mock dependencies vi.mock('../contexts/SettingsContext.js', async () => { @@ -76,6 +77,208 @@ vi.mock('./shared/ScrollableList.js', () => ({ SCROLL_TO_ITEM_END: 0, })); +import { theme } from '../semantic-colors.js'; +import { type BackgroundShell } from '../hooks/shellReducer.js'; + +describe('getToolGroupBorderAppearance', () => { + const mockBackgroundShells = new Map(); + const activeShellPtyId = 123; + + it('returns default empty values for non-tool_group items', () => { + const item = { type: 'user' as const, text: 'Hello', id: 1 }; + const result = getToolGroupBorderAppearance( + item, + null, + false, + [], + mockBackgroundShells, + ); + expect(result).toEqual({ borderColor: '', borderDimColor: false }); + }); + + it('inspects only the last pending tool_group item if current has no tools', () => { + const item = { type: 'tool_group' as const, tools: [], id: 1 }; + const pendingItems = [ + { + type: 'tool_group' as const, + tools: [ + { + callId: '1', + name: 'some_tool', + description: '', + status: CoreToolCallStatus.Executing, + ptyId: undefined, + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + }, + { + type: 'tool_group' as const, + tools: [ + { + callId: '2', + name: 'other_tool', + description: '', + status: CoreToolCallStatus.Success, + ptyId: undefined, + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + }, + ]; + + // Only the last item (Success) should be inspected, so hasPending = false. + // The previous item was Executing (pending) but it shouldn't be counted. + const result = getToolGroupBorderAppearance( + item, + null, + false, + pendingItems, + mockBackgroundShells, + ); + expect(result).toEqual({ + borderColor: theme.border.default, + borderDimColor: false, + }); + }); + + it('returns default border for completed normal tools', () => { + const item = { + type: 'tool_group' as const, + tools: [ + { + callId: '1', + name: 'some_tool', + description: '', + status: CoreToolCallStatus.Success, + ptyId: undefined, + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + id: 1, + }; + const result = getToolGroupBorderAppearance( + item, + null, + false, + [], + mockBackgroundShells, + ); + expect(result).toEqual({ + borderColor: theme.border.default, + borderDimColor: false, + }); + }); + + it('returns warning border for pending normal tools', () => { + const item = { + type: 'tool_group' as const, + tools: [ + { + callId: '1', + name: 'some_tool', + description: '', + status: CoreToolCallStatus.Executing, + ptyId: undefined, + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + id: 1, + }; + const result = getToolGroupBorderAppearance( + item, + null, + false, + [], + mockBackgroundShells, + ); + expect(result).toEqual({ + borderColor: theme.status.warning, + borderDimColor: true, + }); + }); + + it('returns symbol border for executing shell commands', () => { + const item = { + type: 'tool_group' as const, + tools: [ + { + callId: '1', + name: SHELL_COMMAND_NAME, + description: '', + status: CoreToolCallStatus.Executing, + ptyId: activeShellPtyId, + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + id: 1, + }; + // While executing shell commands, it's dim false, border symbol + const result = getToolGroupBorderAppearance( + item, + activeShellPtyId, + true, + [], + mockBackgroundShells, + ); + expect(result).toEqual({ + borderColor: theme.ui.symbol, + borderDimColor: false, + }); + }); + + it('returns symbol border and dims color for background executing shell command when another shell is active', () => { + const item = { + type: 'tool_group' as const, + tools: [ + { + callId: '1', + name: SHELL_COMMAND_NAME, + description: '', + status: CoreToolCallStatus.Executing, + ptyId: 456, // Different ptyId, not active + resultDisplay: undefined, + confirmationDetails: undefined, + } as IndividualToolCallDisplay, + ], + id: 1, + }; + const result = getToolGroupBorderAppearance( + item, + activeShellPtyId, + false, + [], + mockBackgroundShells, + ); + expect(result).toEqual({ + borderColor: theme.ui.symbol, + borderDimColor: true, + }); + }); + + it('handles empty tools with active shell turn (isCurrentlyInShellTurn)', () => { + const item = { type: 'tool_group' as const, tools: [], id: 1 }; + + // active shell turn + const result = getToolGroupBorderAppearance( + item, + activeShellPtyId, + true, + [], + mockBackgroundShells, + ); + // Since there are no tools to inspect, it falls back to empty pending, but isCurrentlyInShellTurn=true + // so it counts as pending shell. + expect(result.borderColor).toEqual(theme.ui.symbol); + // It shouldn't be dim because there are no tools to say it isEmbeddedShellFocused = false + }); +}); + describe('MainContent', () => { const defaultMockUiState = { history: [ @@ -258,7 +461,7 @@ describe('MainContent', () => { history: [], pendingHistoryItems: [ { - type: 'tool_group' as const, + type: 'tool_group', id: 1, tools: [ { diff --git a/packages/cli/src/ui/components/MainContent.tsx b/packages/cli/src/ui/components/MainContent.tsx index 1dcc32ffd48..c69be8858a1 100644 --- a/packages/cli/src/ui/components/MainContent.tsx +++ b/packages/cli/src/ui/components/MainContent.tsx @@ -19,10 +19,85 @@ import { useMemo, memo, useCallback, useEffect, useRef } from 'react'; import { MAX_GEMINI_MESSAGE_LINES } from '../constants.js'; import { useConfirmingTool } from '../hooks/useConfirmingTool.js'; import { ToolConfirmationQueue } from './ToolConfirmationQueue.js'; +import { CoreToolCallStatus } from '@google/gemini-cli-core'; +import { isShellTool } from './messages/ToolShared.js'; +import { theme } from '../semantic-colors.js'; +import type { + HistoryItem, + HistoryItemWithoutId, + HistoryItemToolGroup, +} from '../types.js'; +import type { UIState } from '../contexts/UIStateContext.js'; const MemoizedHistoryItemDisplay = memo(HistoryItemDisplay); const MemoizedAppHeader = memo(AppHeader); +/** + * Calculates the border color and dimming state for a tool group message. + */ +export function getToolGroupBorderAppearance( + item: HistoryItem | HistoryItemWithoutId, + activeShellPtyId: number | null | undefined, + embeddedShellFocused: boolean | undefined, + allPendingItems: HistoryItemWithoutId[], + backgroundShells: UIState['backgroundShells'], +): { borderColor: string; borderDimColor: boolean } { + if (item.type !== 'tool_group') { + return { borderColor: '', borderDimColor: false }; + } + + // If this item has no tools, it's a closing slice for the current batch. + // We need to look at the last pending item to determine the batch's appearance. + const toolsToInspect = + item.tools.length > 0 + ? item.tools + : allPendingItems + .filter( + (i): i is HistoryItemToolGroup => + i !== null && i !== undefined && i.type === 'tool_group', + ) + .slice(-1) + .flatMap((i) => i.tools); + + const hasPending = toolsToInspect.some( + (t) => + t.status !== CoreToolCallStatus.Success && + t.status !== CoreToolCallStatus.Error && + t.status !== CoreToolCallStatus.Cancelled, + ); + + const isEmbeddedShellFocused = toolsToInspect.some( + (t) => + isShellTool(t.name) && + t.status === CoreToolCallStatus.Executing && + t.ptyId === activeShellPtyId && + !!embeddedShellFocused, + ); + + const isShellCommand = toolsToInspect.some((t) => isShellTool(t.name)); + + // If we have an active PTY that isn't a background shell, then the current + // pending batch is definitely a shell batch. + const isCurrentlyInShellTurn = + !!activeShellPtyId && !backgroundShells.has(activeShellPtyId); + + const isShell = + isShellCommand || (item.tools.length === 0 && isCurrentlyInShellTurn); + const isPending = + hasPending || (item.tools.length === 0 && isCurrentlyInShellTurn); + + const borderColor = + (isShell && isPending) || isEmbeddedShellFocused + ? theme.ui.symbol + : isPending + ? theme.status.warning + : theme.border.default; + + const borderDimColor = isPending && (!isShell || !isEmbeddedShellFocused); + + return { borderColor, borderDimColor }; +} + // Limit Gemini messages to a very high number of lines to mitigate performance // issues in the worst case if we somehow get an enormous response from Gemini. // This threshold is arbitrary but should be high enough to never impact normal @@ -49,49 +124,77 @@ export const MainContent = () => { staticAreaMaxItemHeight, availableTerminalHeight, cleanUiDetailsVisible, + activePtyId, + embeddedShellFocused, + backgroundShells, } = uiState; const showHeaderDetails = cleanUiDetailsVisible; const historyItems = useMemo( () => - uiState.history.map((h) => ( - - )), + uiState.history.map((h) => { + const { borderColor, borderDimColor } = getToolGroupBorderAppearance( + h, + activePtyId, + embeddedShellFocused, + [], + backgroundShells, + ); + return ( + + ); + }), [ uiState.history, mainAreaWidth, staticAreaMaxItemHeight, uiState.slashCommands, + activePtyId, + embeddedShellFocused, + backgroundShells, ], ); const pendingItems = useMemo( () => ( - {pendingHistoryItems.map((item, i) => ( - - ))} + {pendingHistoryItems.map((item, i) => { + const { borderColor, borderDimColor } = getToolGroupBorderAppearance( + item, + activePtyId, + embeddedShellFocused, + pendingHistoryItems, + backgroundShells, + ); + return ( + + ); + })} {showConfirmationQueue && confirmingTool && ( )} @@ -103,8 +206,9 @@ export const MainContent = () => { isAlternateBuffer, availableTerminalHeight, mainAreaWidth, - uiState.activePtyId, - uiState.embeddedShellFocused, + activePtyId, + embeddedShellFocused, + backgroundShells, showConfirmationQueue, confirmingTool, ], @@ -130,6 +234,13 @@ export const MainContent = () => { /> ); } else if (item.type === 'history') { + const { borderColor, borderDimColor } = getToolGroupBorderAppearance( + item.item, + activePtyId, + embeddedShellFocused, + [], + backgroundShells, + ); return ( { item={item.item} isPending={false} commands={uiState.slashCommands} + borderColor={borderColor} + borderDimColor={borderDimColor} /> ); } else { @@ -151,6 +264,9 @@ export const MainContent = () => { mainAreaWidth, uiState.slashCommands, pendingItems, + activePtyId, + embeddedShellFocused, + backgroundShells, ], ); diff --git a/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx b/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx index deef0cf91fc..c2342a0d5b2 100644 --- a/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx +++ b/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx @@ -42,6 +42,8 @@ describe('', () => { const baseProps = { groupId: 1, terminalWidth: 80, + borderColor: 'grey', + borderDimColor: false, }; const baseMockConfig = makeFakeConfig({ @@ -61,7 +63,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -119,7 +126,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -159,7 +171,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -197,7 +214,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -222,7 +244,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -236,7 +263,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: [] }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: [], + }, + ], }, }, ); @@ -267,7 +299,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -290,7 +327,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -325,8 +367,14 @@ describe('', () => { config: baseMockConfig, uiState: { pendingHistoryItems: [ - { type: 'tool_group', tools: toolCalls1 }, - { type: 'tool_group', tools: toolCalls2 }, + { + type: 'tool_group', + tools: toolCalls1, + }, + { + type: 'tool_group', + tools: toolCalls2, + }, ], }, }, @@ -349,7 +397,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -371,7 +424,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); @@ -405,7 +463,12 @@ describe('', () => { { config: baseMockConfig, uiState: { - pendingHistoryItems: [{ type: 'tool_group', tools: toolCalls }], + pendingHistoryItems: [ + { + type: 'tool_group', + tools: toolCalls, + }, + ], }, }, ); diff --git a/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx b/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx index 18179b6a92c..aade976e0bf 100644 --- a/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx +++ b/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx @@ -13,11 +13,8 @@ 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 { - CoreToolCallStatus, - shouldHideToolCall, -} from '@google/gemini-cli-core'; +import { isShellTool } from './ToolShared.js'; +import { shouldHideToolCall } from '@google/gemini-cli-core'; import { ShowMoreLines } from '../ShowMoreLines.js'; import { useUIState } from '../../contexts/UIStateContext.js'; @@ -31,6 +28,8 @@ interface ToolGroupMessageProps { onShellInputSubmit?: (input: string) => void; borderTop?: boolean; borderBottom?: boolean; + borderColor: string; + borderDimColor: boolean; } // Main component renders the border and maps the tools using ToolMessage @@ -44,6 +43,8 @@ export const ToolGroupMessage: React.FC = ({ embeddedShellFocused, borderTop: borderTopOverride, borderBottom: borderBottomOverride, + borderColor, + borderDimColor, }) => { // Filter out tool calls that should be hidden (e.g. in-progress Ask User, or Plan Mode operations). const toolCalls = useMemo( @@ -80,31 +81,6 @@ export const ToolGroupMessage: React.FC = ({ [toolCalls], ); - const isEmbeddedShellFocused = visibleToolCalls.some((t) => - isThisShellFocused( - t.name, - t.status, - t.ptyId, - activeShellPtyId, - embeddedShellFocused, - ), - ); - - const hasPending = !visibleToolCalls.every( - (t) => t.status === CoreToolCallStatus.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), diff --git a/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx b/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx index bc805d1f1c6..f52ee6892b8 100644 --- a/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx +++ b/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx @@ -35,6 +35,8 @@ describe('ToolResultDisplay Overflow', () => { const { lastFrame } = renderWithProviders( { data={['item1']} renderItem={() => ( { data={['item1']} renderItem={() => ( { // Assert // It SHOULD be called for the tool_group expect(mockAddItem).toHaveBeenCalledWith( - expect.objectContaining({ type: 'tool_group' }), + expect.objectContaining({ + type: 'tool_group', + }), 999, ); @@ -1343,7 +1345,9 @@ describe('handleAtCommand', () => { }); expect(containsResourceText).toBe(true); expect(mockAddItem).toHaveBeenCalledWith( - expect.objectContaining({ type: 'tool_group' }), + expect.objectContaining({ + type: 'tool_group', + }), expect.any(Number), ); }); diff --git a/packages/cli/src/ui/hooks/toolMapping.ts b/packages/cli/src/ui/hooks/toolMapping.ts index d921651e514..1d0ab40ce96 100644 --- a/packages/cli/src/ui/hooks/toolMapping.ts +++ b/packages/cli/src/ui/hooks/toolMapping.ts @@ -23,7 +23,10 @@ import { */ export function mapToDisplay( toolOrTools: ToolCall[] | ToolCall, - options: { borderTop?: boolean; borderBottom?: boolean } = {}, + options: { + borderTop?: boolean; + borderBottom?: boolean; + } = {}, ): HistoryItemToolGroup { const toolCalls = Array.isArray(toolOrTools) ? toolOrTools : [toolOrTools]; const { borderTop, borderBottom } = options; diff --git a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx index 8b5a312d37d..d63f86cd70b 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.test.tsx +++ b/packages/cli/src/ui/hooks/useGeminiStream.test.tsx @@ -44,6 +44,7 @@ import type { Part, PartListUnion } from '@google/genai'; import type { UseHistoryManagerReturn } from './useHistoryManager.js'; import type { SlashCommandProcessorResult } from '../types.js'; import { MessageType, StreamingState } from '../types.js'; + import type { LoadedSettings } from '../../config/settings.js'; // --- MOCKS --- diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index f23574858f0..1d4289f3351 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -78,6 +78,11 @@ import { type TrackedWaitingToolCall, type TrackedExecutingToolCall, } from './useToolScheduler.js'; +import { theme } from '../semantic-colors.js'; +import { + isShellTool, + isThisShellFocused, +} from '../components/messages/ToolShared.js'; import { promises as fs } from 'node:fs'; import path from 'node:path'; import { useSessionStats } from '../contexts/SessionContext.js'; @@ -120,6 +125,42 @@ function showCitations(settings: LoadedSettings): boolean { return true; } +export function getToolGroupBorderAppearance( + toolCalls: TrackedToolCall[], + activeShellPtyId: number | null, + embeddedShellFocused: boolean, +): { borderColor: string; borderDimColor: boolean } { + const hasPending = toolCalls.some( + (t) => + t.status !== 'success' && + t.status !== 'error' && + t.status !== 'cancelled', + ); + + const isEmbeddedShellFocused = toolCalls.some((t) => + isThisShellFocused( + t.request.name, + t.status, + t.status === 'executing' ? t.pid : undefined, + activeShellPtyId, + embeddedShellFocused, + ), + ); + + const isShellCommand = toolCalls.some((t) => isShellTool(t.request.name)); + const borderColor = + (isShellCommand && hasPending) || isEmbeddedShellFocused + ? theme.ui.symbol + : hasPending + ? theme.status.warning + : theme.border.default; + + const borderDimColor = + hasPending && (!isShellCommand || !isEmbeddedShellFocused); + + return { borderColor, borderDimColor }; +} + /** * Calculates the current streaming state based on tool call status and responding flag. */ @@ -250,6 +291,8 @@ export const useGeminiStream = ( mapTrackedToolCallsToDisplay(toolsToPush as TrackedToolCall[], { borderTop: isFirstToolInGroupRef.current, borderBottom: true, + borderColor: theme.border.default, + borderDimColor: false, }), ); } @@ -290,6 +333,45 @@ export const useGeminiStream = ( getPreferredEditor, ); + const activeToolPtyId = useMemo(() => { + const executingShellTool = toolCalls.find( + (tc) => + tc.status === 'executing' && tc.request.name === 'run_shell_command', + ); + // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion + return (executingShellTool as TrackedExecutingToolCall | undefined)?.pid; + }, [toolCalls]); + + const onExec = useCallback(async (done: Promise) => { + setIsResponding(true); + await done; + setIsResponding(false); + }, []); + + const { + handleShellCommand, + activeShellPtyId, + lastShellOutputTime, + backgroundShellCount, + isBackgroundShellVisible, + toggleBackgroundShell, + backgroundCurrentShell, + registerBackgroundShell, + dismissBackgroundShell, + backgroundShells, + } = useShellCommandProcessor( + addItem, + setPendingHistoryItem, + onExec, + onDebugMessage, + config, + geminiClient, + setShellInputFocused, + terminalWidth, + terminalHeight, + activeToolPtyId, + ); + const streamingState = useMemo( () => calculateStreamingState(isResponding, toolCalls), [isResponding, toolCalls], @@ -347,6 +429,11 @@ export const useGeminiStream = ( const historyItem = mapTrackedToolCallsToDisplay(tc, { borderTop: isFirst, borderBottom: isLastInBatch, + ...getToolGroupBorderAppearance( + toolCalls, + activeShellPtyId, + !!isShellFocused, + ), }); addItem(historyItem); isFirst = false; @@ -362,6 +449,8 @@ export const useGeminiStream = ( setPushedToolCallIds, setIsFirstToolInGroup, addItem, + activeShellPtyId, + isShellFocused, ]); const pendingToolGroupItems = useMemo((): HistoryItemWithoutId[] => { @@ -371,11 +460,18 @@ export const useGeminiStream = ( const items: HistoryItemWithoutId[] = []; + const appearance = getToolGroupBorderAppearance( + toolCalls, + activeShellPtyId, + !!isShellFocused, + ); + if (remainingTools.length > 0) { items.push( mapTrackedToolCallsToDisplay(remainingTools, { borderTop: pushedToolCallIds.size === 0, borderBottom: false, // Stay open to connect with the slice below + ...appearance, }), ); } @@ -423,20 +519,12 @@ export const useGeminiStream = ( tools: [] as IndividualToolCallDisplay[], borderTop: false, borderBottom: true, + ...appearance, }); } return items; - }, [toolCalls, pushedToolCallIds]); - - const activeToolPtyId = useMemo(() => { - const executingShellTool = toolCalls.find( - (tc) => - tc.status === 'executing' && tc.request.name === 'run_shell_command', - ); - // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion - return (executingShellTool as TrackedExecutingToolCall | undefined)?.pid; - }, [toolCalls]); + }, [toolCalls, pushedToolCallIds, activeShellPtyId, isShellFocused]); const lastQueryRef = useRef(null); const lastPromptIdRef = useRef(null); @@ -448,36 +536,6 @@ export const useGeminiStream = ( onComplete: (result: { userSelection: 'disable' | 'keep' }) => void; } | null>(null); - const onExec = useCallback(async (done: Promise) => { - setIsResponding(true); - await done; - setIsResponding(false); - }, []); - - const { - handleShellCommand, - activeShellPtyId, - lastShellOutputTime, - backgroundShellCount, - isBackgroundShellVisible, - toggleBackgroundShell, - backgroundCurrentShell, - registerBackgroundShell, - dismissBackgroundShell, - backgroundShells, - } = useShellCommandProcessor( - addItem, - setPendingHistoryItem, - onExec, - onDebugMessage, - config, - geminiClient, - setShellInputFocused, - terminalWidth, - terminalHeight, - activeToolPtyId, - ); - const activePtyId = activeShellPtyId || activeToolPtyId; const prevActiveShellPtyIdRef = useRef(null); From f927439d9b11e703c31f253c6528e7b5e5940e48 Mon Sep 17 00:00:00 2001 From: jacob314 Date: Tue, 17 Feb 2026 18:02:53 -0800 Subject: [PATCH 2/3] Checkpoint of code review fixes --- .../src/ui/components/HistoryItemDisplay.tsx | 3 +- .../src/ui/components/MainContent.test.tsx | 3 +- .../cli/src/ui/components/MainContent.tsx | 76 +----------- packages/cli/src/ui/hooks/toolMapping.ts | 6 +- packages/cli/src/ui/hooks/useGeminiStream.ts | 58 +++------ packages/cli/src/ui/types.ts | 2 + packages/cli/src/ui/utils/borderStyles.ts | 117 ++++++++++++++++++ 7 files changed, 144 insertions(+), 121 deletions(-) create mode 100644 packages/cli/src/ui/utils/borderStyles.ts diff --git a/packages/cli/src/ui/components/HistoryItemDisplay.tsx b/packages/cli/src/ui/components/HistoryItemDisplay.tsx index 3b99bf3d070..bc06b7f9319 100644 --- a/packages/cli/src/ui/components/HistoryItemDisplay.tsx +++ b/packages/cli/src/ui/components/HistoryItemDisplay.tsx @@ -6,6 +6,7 @@ import type React from 'react'; import { useMemo } from 'react'; +import { theme } from '../semantic-colors.js'; import { escapeAnsiCtrlCodes } from '../utils/textUtils.js'; import type { HistoryItem } from '../types.js'; import { UserMessage } from './messages/UserMessage.js'; @@ -185,7 +186,7 @@ export const HistoryItemDisplay: React.FC = ({ embeddedShellFocused={embeddedShellFocused} borderTop={itemForDisplay.borderTop} borderBottom={itemForDisplay.borderBottom} - borderColor={borderColor ?? ''} + borderColor={borderColor ?? theme.border.default} borderDimColor={borderDimColor ?? false} /> )} diff --git a/packages/cli/src/ui/components/MainContent.test.tsx b/packages/cli/src/ui/components/MainContent.test.tsx index 2403f0f3306..64ebda516de 100644 --- a/packages/cli/src/ui/components/MainContent.test.tsx +++ b/packages/cli/src/ui/components/MainContent.test.tsx @@ -6,7 +6,8 @@ import { renderWithProviders } from '../../test-utils/render.js'; import { waitFor } from '../../test-utils/async.js'; -import { MainContent, getToolGroupBorderAppearance } from './MainContent.js'; +import { MainContent } from './MainContent.js'; +import { getToolGroupBorderAppearance } from '../utils/borderStyles.js'; import { describe, it, expect, vi, beforeEach } from 'vitest'; import { Box, Text } from 'ink'; import { act, useState, type JSX } from 'react'; diff --git a/packages/cli/src/ui/components/MainContent.tsx b/packages/cli/src/ui/components/MainContent.tsx index c69be8858a1..11cc1a70f83 100644 --- a/packages/cli/src/ui/components/MainContent.tsx +++ b/packages/cli/src/ui/components/MainContent.tsx @@ -19,85 +19,11 @@ import { useMemo, memo, useCallback, useEffect, useRef } from 'react'; import { MAX_GEMINI_MESSAGE_LINES } from '../constants.js'; import { useConfirmingTool } from '../hooks/useConfirmingTool.js'; import { ToolConfirmationQueue } from './ToolConfirmationQueue.js'; -import { CoreToolCallStatus } from '@google/gemini-cli-core'; -import { isShellTool } from './messages/ToolShared.js'; -import { theme } from '../semantic-colors.js'; -import type { - HistoryItem, - HistoryItemWithoutId, - HistoryItemToolGroup, -} from '../types.js'; -import type { UIState } from '../contexts/UIStateContext.js'; +import { getToolGroupBorderAppearance } from '../utils/borderStyles.js'; const MemoizedHistoryItemDisplay = memo(HistoryItemDisplay); const MemoizedAppHeader = memo(AppHeader); -/** - * Calculates the border color and dimming state for a tool group message. - */ -export function getToolGroupBorderAppearance( - item: HistoryItem | HistoryItemWithoutId, - activeShellPtyId: number | null | undefined, - embeddedShellFocused: boolean | undefined, - allPendingItems: HistoryItemWithoutId[], - backgroundShells: UIState['backgroundShells'], -): { borderColor: string; borderDimColor: boolean } { - if (item.type !== 'tool_group') { - return { borderColor: '', borderDimColor: false }; - } - - // If this item has no tools, it's a closing slice for the current batch. - // We need to look at the last pending item to determine the batch's appearance. - const toolsToInspect = - item.tools.length > 0 - ? item.tools - : allPendingItems - .filter( - (i): i is HistoryItemToolGroup => - i !== null && i !== undefined && i.type === 'tool_group', - ) - .slice(-1) - .flatMap((i) => i.tools); - - const hasPending = toolsToInspect.some( - (t) => - t.status !== CoreToolCallStatus.Success && - t.status !== CoreToolCallStatus.Error && - t.status !== CoreToolCallStatus.Cancelled, - ); - - const isEmbeddedShellFocused = toolsToInspect.some( - (t) => - isShellTool(t.name) && - t.status === CoreToolCallStatus.Executing && - t.ptyId === activeShellPtyId && - !!embeddedShellFocused, - ); - - const isShellCommand = toolsToInspect.some((t) => isShellTool(t.name)); - - // If we have an active PTY that isn't a background shell, then the current - // pending batch is definitely a shell batch. - const isCurrentlyInShellTurn = - !!activeShellPtyId && !backgroundShells.has(activeShellPtyId); - - const isShell = - isShellCommand || (item.tools.length === 0 && isCurrentlyInShellTurn); - const isPending = - hasPending || (item.tools.length === 0 && isCurrentlyInShellTurn); - - const borderColor = - (isShell && isPending) || isEmbeddedShellFocused - ? theme.ui.symbol - : isPending - ? theme.status.warning - : theme.border.default; - - const borderDimColor = isPending && (!isShell || !isEmbeddedShellFocused); - - return { borderColor, borderDimColor }; -} - // Limit Gemini messages to a very high number of lines to mitigate performance // issues in the worst case if we somehow get an enormous response from Gemini. // This threshold is arbitrary but should be high enough to never impact normal diff --git a/packages/cli/src/ui/hooks/toolMapping.ts b/packages/cli/src/ui/hooks/toolMapping.ts index 1d0ab40ce96..bd8718b9bd2 100644 --- a/packages/cli/src/ui/hooks/toolMapping.ts +++ b/packages/cli/src/ui/hooks/toolMapping.ts @@ -26,10 +26,12 @@ export function mapToDisplay( options: { borderTop?: boolean; borderBottom?: boolean; + borderColor?: string; + borderDimColor?: boolean; } = {}, ): HistoryItemToolGroup { const toolCalls = Array.isArray(toolOrTools) ? toolOrTools : [toolOrTools]; - const { borderTop, borderBottom } = options; + const { borderTop, borderBottom, borderColor, borderDimColor } = options; const toolDisplays = toolCalls.map((call): IndividualToolCallDisplay => { let description: string; @@ -107,5 +109,7 @@ export function mapToDisplay( tools: toolDisplays, borderTop, borderBottom, + borderColor, + borderDimColor, }; } diff --git a/packages/cli/src/ui/hooks/useGeminiStream.ts b/packages/cli/src/ui/hooks/useGeminiStream.ts index 1d4289f3351..5fc7d628ac7 100644 --- a/packages/cli/src/ui/hooks/useGeminiStream.ts +++ b/packages/cli/src/ui/hooks/useGeminiStream.ts @@ -79,10 +79,7 @@ import { type TrackedExecutingToolCall, } from './useToolScheduler.js'; import { theme } from '../semantic-colors.js'; -import { - isShellTool, - isThisShellFocused, -} from '../components/messages/ToolShared.js'; +import { getToolGroupBorderAppearance } from '../utils/borderStyles.js'; import { promises as fs } from 'node:fs'; import path from 'node:path'; import { useSessionStats } from '../contexts/SessionContext.js'; @@ -125,42 +122,6 @@ function showCitations(settings: LoadedSettings): boolean { return true; } -export function getToolGroupBorderAppearance( - toolCalls: TrackedToolCall[], - activeShellPtyId: number | null, - embeddedShellFocused: boolean, -): { borderColor: string; borderDimColor: boolean } { - const hasPending = toolCalls.some( - (t) => - t.status !== 'success' && - t.status !== 'error' && - t.status !== 'cancelled', - ); - - const isEmbeddedShellFocused = toolCalls.some((t) => - isThisShellFocused( - t.request.name, - t.status, - t.status === 'executing' ? t.pid : undefined, - activeShellPtyId, - embeddedShellFocused, - ), - ); - - const isShellCommand = toolCalls.some((t) => isShellTool(t.request.name)); - const borderColor = - (isShellCommand && hasPending) || isEmbeddedShellFocused - ? theme.ui.symbol - : hasPending - ? theme.status.warning - : theme.border.default; - - const borderDimColor = - hasPending && (!isShellCommand || !isEmbeddedShellFocused); - - return { borderColor, borderDimColor }; -} - /** * Calculates the current streaming state based on tool call status and responding flag. */ @@ -430,9 +391,11 @@ export const useGeminiStream = ( borderTop: isFirst, borderBottom: isLastInBatch, ...getToolGroupBorderAppearance( - toolCalls, + { type: 'tool_group', tools: toolCalls }, activeShellPtyId, !!isShellFocused, + [], + backgroundShells, ), }); addItem(historyItem); @@ -451,6 +414,7 @@ export const useGeminiStream = ( addItem, activeShellPtyId, isShellFocused, + backgroundShells, ]); const pendingToolGroupItems = useMemo((): HistoryItemWithoutId[] => { @@ -461,9 +425,11 @@ export const useGeminiStream = ( const items: HistoryItemWithoutId[] = []; const appearance = getToolGroupBorderAppearance( - toolCalls, + { type: 'tool_group', tools: toolCalls }, activeShellPtyId, !!isShellFocused, + [], + backgroundShells, ); if (remainingTools.length > 0) { @@ -524,7 +490,13 @@ export const useGeminiStream = ( } return items; - }, [toolCalls, pushedToolCallIds, activeShellPtyId, isShellFocused]); + }, [ + toolCalls, + pushedToolCallIds, + activeShellPtyId, + isShellFocused, + backgroundShells, + ]); const lastQueryRef = useRef(null); const lastPromptIdRef = useRef(null); diff --git a/packages/cli/src/ui/types.ts b/packages/cli/src/ui/types.ts index 8481cca71f2..290ab63417a 100644 --- a/packages/cli/src/ui/types.ts +++ b/packages/cli/src/ui/types.ts @@ -221,6 +221,8 @@ export type HistoryItemToolGroup = HistoryItemBase & { tools: IndividualToolCallDisplay[]; borderTop?: boolean; borderBottom?: boolean; + borderColor?: string; + borderDimColor?: boolean; }; export type HistoryItemUserShell = HistoryItemBase & { diff --git a/packages/cli/src/ui/utils/borderStyles.ts b/packages/cli/src/ui/utils/borderStyles.ts new file mode 100644 index 00000000000..02be0c08712 --- /dev/null +++ b/packages/cli/src/ui/utils/borderStyles.ts @@ -0,0 +1,117 @@ +/** + * @license + * Copyright 2026 Google LLC + * SPDX-License-Identifier: Apache-2.0 + */ + +import { CoreToolCallStatus } from '@google/gemini-cli-core'; +import { isShellTool } from '../components/messages/ToolShared.js'; +import { theme } from '../semantic-colors.js'; +import type { + HistoryItem, + HistoryItemWithoutId, + HistoryItemToolGroup, + IndividualToolCallDisplay, +} from '../types.js'; +import type { BackgroundShell } from '../hooks/shellReducer.js'; +import type { TrackedToolCall } from '../hooks/useToolScheduler.js'; + +function isTrackedToolCall( + tool: IndividualToolCallDisplay | TrackedToolCall, +): tool is TrackedToolCall { + return 'request' in tool; +} + +/** + * Calculates the border color and dimming state for a tool group message. + */ +export function getToolGroupBorderAppearance( + item: + | HistoryItem + | HistoryItemWithoutId + | { type: 'tool_group'; tools: TrackedToolCall[] }, + activeShellPtyId: number | null | undefined, + embeddedShellFocused: boolean | undefined, + allPendingItems: HistoryItemWithoutId[] = [], + backgroundShells: Map = new Map(), +): { borderColor: string; borderDimColor: boolean } { + if (item.type !== 'tool_group') { + return { borderColor: '', borderDimColor: false }; + } + + // If this item has no tools, it's a closing slice for the current batch. + // We need to look at the last pending item to determine the batch's appearance. + const toolsToInspect: Array = + item.tools.length > 0 + ? item.tools + : allPendingItems + .filter( + (i): i is HistoryItemToolGroup => + i !== null && i !== undefined && i.type === 'tool_group', + ) + .slice(-1) + .flatMap((i) => i.tools); + + const hasPending = toolsToInspect.some((t) => { + if (isTrackedToolCall(t)) { + return ( + t.status !== 'success' && + t.status !== 'error' && + t.status !== 'cancelled' + ); + } else { + return ( + t.status !== CoreToolCallStatus.Success && + t.status !== CoreToolCallStatus.Error && + t.status !== CoreToolCallStatus.Cancelled + ); + } + }); + + const isEmbeddedShellFocused = toolsToInspect.some((t) => { + if (isTrackedToolCall(t)) { + return ( + isShellTool(t.request.name) && + t.status === 'executing' && + t.pid === activeShellPtyId && + !!embeddedShellFocused + ); + } else { + return ( + isShellTool(t.name) && + t.status === CoreToolCallStatus.Executing && + t.ptyId === activeShellPtyId && + !!embeddedShellFocused + ); + } + }); + + const isShellCommand = toolsToInspect.some((t) => { + if (isTrackedToolCall(t)) { + return isShellTool(t.request.name); + } else { + return isShellTool(t.name); + } + }); + + // If we have an active PTY that isn't a background shell, then the current + // pending batch is definitely a shell batch. + const isCurrentlyInShellTurn = + !!activeShellPtyId && !backgroundShells.has(activeShellPtyId); + + const isShell = + isShellCommand || (item.tools.length === 0 && isCurrentlyInShellTurn); + const isPending = + hasPending || (item.tools.length === 0 && isCurrentlyInShellTurn); + + const borderColor = + (isShell && isPending) || isEmbeddedShellFocused + ? theme.ui.symbol + : isPending + ? theme.status.warning + : theme.border.default; + + const borderDimColor = isPending && (!isShell || !isEmbeddedShellFocused); + + return { borderColor, borderDimColor }; +} From bfd02c5683224d46602943b5274d6a3809e5de26 Mon Sep 17 00:00:00 2001 From: jacob314 Date: Tue, 17 Feb 2026 18:28:35 -0800 Subject: [PATCH 3/3] Refactor to avoid memoizing params that shouldn't be memoized. --- .../AlternateBufferQuittingDisplay.tsx | 2 - .../src/ui/components/HistoryItemDisplay.tsx | 15 +-- .../src/ui/components/MainContent.test.tsx | 1 + .../cli/src/ui/components/MainContent.tsx | 96 +++++-------------- .../messages/ShellToolMessage.test.tsx | 48 ++++++---- .../components/messages/ShellToolMessage.tsx | 9 +- .../messages/ToolGroupMessage.test.tsx | 76 +++++++++++---- .../components/messages/ToolGroupMessage.tsx | 51 ++++++---- .../ToolResultDisplayOverflow.test.tsx | 4 +- .../ToolStickyHeaderRegression.test.tsx | 8 +- packages/cli/src/ui/utils/borderStyles.ts | 10 +- 11 files changed, 161 insertions(+), 159 deletions(-) diff --git a/packages/cli/src/ui/components/AlternateBufferQuittingDisplay.tsx b/packages/cli/src/ui/components/AlternateBufferQuittingDisplay.tsx index 8e0ede2e098..7b81875c7b9 100644 --- a/packages/cli/src/ui/components/AlternateBufferQuittingDisplay.tsx +++ b/packages/cli/src/ui/components/AlternateBufferQuittingDisplay.tsx @@ -53,8 +53,6 @@ export const AlternateBufferQuittingDisplay = () => { terminalWidth={uiState.mainAreaWidth} item={{ ...item, id: 0 }} isPending={true} - activeShellPtyId={uiState.activePtyId} - embeddedShellFocused={uiState.embeddedShellFocused} /> ))} {showPromptedTool && ( diff --git a/packages/cli/src/ui/components/HistoryItemDisplay.tsx b/packages/cli/src/ui/components/HistoryItemDisplay.tsx index bc06b7f9319..87355666411 100644 --- a/packages/cli/src/ui/components/HistoryItemDisplay.tsx +++ b/packages/cli/src/ui/components/HistoryItemDisplay.tsx @@ -6,7 +6,6 @@ import type React from 'react'; import { useMemo } from 'react'; -import { theme } from '../semantic-colors.js'; import { escapeAnsiCtrlCodes } from '../utils/textUtils.js'; import type { HistoryItem } from '../types.js'; import { UserMessage } from './messages/UserMessage.js'; @@ -45,11 +44,7 @@ interface HistoryItemDisplayProps { terminalWidth: number; isPending: boolean; commands?: readonly SlashCommand[]; - activeShellPtyId?: number | null; - embeddedShellFocused?: boolean; availableTerminalHeightGemini?: number; - borderColor?: string; - borderDimColor?: boolean; } export const HistoryItemDisplay: React.FC = ({ @@ -58,11 +53,7 @@ export const HistoryItemDisplay: React.FC = ({ terminalWidth, isPending, commands, - activeShellPtyId, - embeddedShellFocused, availableTerminalHeightGemini, - borderColor, - borderDimColor, }) => { const settings = useSettings(); const inlineThinkingMode = getInlineThinkingMode(settings); @@ -178,16 +169,12 @@ export const HistoryItemDisplay: React.FC = ({ )} {itemForDisplay.type === 'tool_group' && ( )} {itemForDisplay.type === 'compression' && ( diff --git a/packages/cli/src/ui/components/MainContent.test.tsx b/packages/cli/src/ui/components/MainContent.test.tsx index 64ebda516de..427a1c821e9 100644 --- a/packages/cli/src/ui/components/MainContent.test.tsx +++ b/packages/cli/src/ui/components/MainContent.test.tsx @@ -277,6 +277,7 @@ describe('getToolGroupBorderAppearance', () => { // so it counts as pending shell. expect(result.borderColor).toEqual(theme.ui.symbol); // It shouldn't be dim because there are no tools to say it isEmbeddedShellFocused = false + expect(result.borderDimColor).toBe(false); }); }); diff --git a/packages/cli/src/ui/components/MainContent.tsx b/packages/cli/src/ui/components/MainContent.tsx index 11cc1a70f83..cba57756e32 100644 --- a/packages/cli/src/ui/components/MainContent.tsx +++ b/packages/cli/src/ui/components/MainContent.tsx @@ -19,7 +19,6 @@ import { useMemo, memo, useCallback, useEffect, useRef } from 'react'; import { MAX_GEMINI_MESSAGE_LINES } from '../constants.js'; import { useConfirmingTool } from '../hooks/useConfirmingTool.js'; import { ToolConfirmationQueue } from './ToolConfirmationQueue.js'; -import { getToolGroupBorderAppearance } from '../utils/borderStyles.js'; const MemoizedHistoryItemDisplay = memo(HistoryItemDisplay); const MemoizedAppHeader = memo(AppHeader); @@ -50,77 +49,47 @@ export const MainContent = () => { staticAreaMaxItemHeight, availableTerminalHeight, cleanUiDetailsVisible, - activePtyId, - embeddedShellFocused, - backgroundShells, } = uiState; const showHeaderDetails = cleanUiDetailsVisible; const historyItems = useMemo( () => - uiState.history.map((h) => { - const { borderColor, borderDimColor } = getToolGroupBorderAppearance( - h, - activePtyId, - embeddedShellFocused, - [], - backgroundShells, - ); - return ( - - ); - }), + uiState.history.map((h) => ( + + )), [ uiState.history, mainAreaWidth, staticAreaMaxItemHeight, uiState.slashCommands, - activePtyId, - embeddedShellFocused, - backgroundShells, ], ); const pendingItems = useMemo( () => ( - {pendingHistoryItems.map((item, i) => { - const { borderColor, borderDimColor } = getToolGroupBorderAppearance( - item, - activePtyId, - embeddedShellFocused, - pendingHistoryItems, - backgroundShells, - ); - return ( - - ); - })} + {pendingHistoryItems.map((item, i) => ( + + ))} {showConfirmationQueue && confirmingTool && ( )} @@ -132,9 +101,6 @@ export const MainContent = () => { isAlternateBuffer, availableTerminalHeight, mainAreaWidth, - activePtyId, - embeddedShellFocused, - backgroundShells, showConfirmationQueue, confirmingTool, ], @@ -160,13 +126,6 @@ export const MainContent = () => { /> ); } else if (item.type === 'history') { - const { borderColor, borderDimColor } = getToolGroupBorderAppearance( - item.item, - activePtyId, - embeddedShellFocused, - [], - backgroundShells, - ); return ( { item={item.item} isPending={false} commands={uiState.slashCommands} - borderColor={borderColor} - borderDimColor={borderDimColor} /> ); } else { @@ -190,9 +147,6 @@ export const MainContent = () => { mainAreaWidth, uiState.slashCommands, pendingItems, - activePtyId, - embeddedShellFocused, - backgroundShells, ], ); diff --git a/packages/cli/src/ui/components/messages/ShellToolMessage.test.tsx b/packages/cli/src/ui/components/messages/ShellToolMessage.test.tsx index c698445f8f2..c1b2ce53b38 100644 --- a/packages/cli/src/ui/components/messages/ShellToolMessage.test.tsx +++ b/packages/cli/src/ui/components/messages/ShellToolMessage.test.tsx @@ -88,20 +88,16 @@ describe('', () => { CoreToolCallStatus.Executing, ); updateStatus = setStatus; - return ( - - ); + return ; }; const { lastFrame } = renderWithProviders(, { uiActions, - uiState: { streamingState: StreamingState.Idle }, + uiState: { + streamingState: StreamingState.Idle, + embeddedShellFocused: true, + activePtyId: 1, + }, }); // Verify it is initially focused @@ -143,21 +139,29 @@ describe('', () => { 'renders in Alternate Buffer mode while focused', { status: CoreToolCallStatus.Executing, - embeddedShellFocused: true, - activeShellPtyId: 1, ptyId: 1, }, - { useAlternateBuffer: true }, + { + useAlternateBuffer: true, + uiState: { + embeddedShellFocused: true, + activePtyId: 1, + }, + }, ], [ 'renders in Alternate Buffer mode while unfocused', { status: CoreToolCallStatus.Executing, - embeddedShellFocused: false, - activeShellPtyId: 1, ptyId: 1, }, - { useAlternateBuffer: true }, + { + useAlternateBuffer: true, + uiState: { + embeddedShellFocused: false, + activePtyId: 1, + }, + }, ], ])('%s', async (_, props, options) => { const { lastFrame } = renderShell(props, options); @@ -199,12 +203,16 @@ describe('', () => { resultDisplay: LONG_OUTPUT, renderOutputAsMarkdown: false, availableTerminalHeight, - activeShellPtyId: 1, - ptyId: focused ? 1 : 2, + ptyId: 1, status: CoreToolCallStatus.Executing, - embeddedShellFocused: focused, }, - { useAlternateBuffer: true }, + { + useAlternateBuffer: true, + uiState: { + activePtyId: focused ? 1 : 2, + embeddedShellFocused: focused, + }, + }, ); await waitFor(() => { diff --git a/packages/cli/src/ui/components/messages/ShellToolMessage.tsx b/packages/cli/src/ui/components/messages/ShellToolMessage.tsx index cb6f27b3172..50af3bc1e66 100644 --- a/packages/cli/src/ui/components/messages/ShellToolMessage.tsx +++ b/packages/cli/src/ui/components/messages/ShellToolMessage.tsx @@ -29,9 +29,9 @@ import { import { useAlternateBuffer } from '../../hooks/useAlternateBuffer.js'; import { type Config, CoreToolCallStatus } from '@google/gemini-cli-core'; +import { useUIState } from '../../contexts/UIStateContext.js'; + export interface ShellToolMessageProps extends ToolMessageProps { - activeShellPtyId?: number | null; - embeddedShellFocused?: boolean; config?: Config; } @@ -52,10 +52,6 @@ export const ShellToolMessage: React.FC = ({ renderOutputAsMarkdown = true, - activeShellPtyId, - - embeddedShellFocused, - ptyId, config, @@ -66,6 +62,7 @@ export const ShellToolMessage: React.FC = ({ borderDimColor, }) => { + const { activePtyId: activeShellPtyId, embeddedShellFocused } = useUIState(); const isAlternateBuffer = useAlternateBuffer(); const isThisShellFocused = checkIsShellFocused( name, diff --git a/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx b/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx index c2342a0d5b2..b02b34ec4e6 100644 --- a/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx +++ b/packages/cli/src/ui/components/messages/ToolGroupMessage.test.tsx @@ -7,7 +7,11 @@ import { renderWithProviders } from '../../../test-utils/render.js'; import { describe, it, expect, vi, afterEach } from 'vitest'; import { ToolGroupMessage } from './ToolGroupMessage.js'; -import type { IndividualToolCallDisplay } from '../../types.js'; +import type { + HistoryItem, + HistoryItemWithoutId, + IndividualToolCallDisplay, +} from '../../types.js'; import { Scrollable } from '../shared/Scrollable.js'; import { makeFakeConfig, @@ -40,12 +44,17 @@ describe('', () => { }); const baseProps = { - groupId: 1, terminalWidth: 80, - borderColor: 'grey', - borderDimColor: false, }; + const createItem = ( + tools: IndividualToolCallDisplay[], + ): HistoryItem | HistoryItemWithoutId => ({ + id: 1, + type: 'tool_group', + tools, + }); + const baseMockConfig = makeFakeConfig({ model: 'gemini-pro', targetDir: os.tmpdir(), @@ -58,8 +67,9 @@ describe('', () => { describe('Golden Snapshots', () => { it('renders single successful tool call', () => { const toolCalls = [createToolCall()]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -88,9 +98,10 @@ describe('', () => { }, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig }, ); @@ -120,9 +131,10 @@ describe('', () => { status: CoreToolCallStatus.Error, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -165,9 +177,10 @@ describe('', () => { status: CoreToolCallStatus.Scheduled, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -205,9 +218,11 @@ describe('', () => { resultDisplay: 'More output here', }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( , @@ -235,9 +250,11 @@ describe('', () => { 'This is a very long description that might cause wrapping issues', }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( , @@ -258,8 +275,10 @@ describe('', () => { }); it('renders empty tool calls array', () => { + const toolCalls: IndividualToolCallDisplay[] = []; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -292,9 +311,10 @@ describe('', () => { resultDisplay: 'line1\nline2', }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - + , { config: baseMockConfig, @@ -322,8 +342,9 @@ describe('', () => { outputFile: '/path/to/output.txt', }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -349,6 +370,7 @@ describe('', () => { resultDisplay: 'line1\nline2\nline3\nline4\nline5', }), ]; + const item1 = createItem(toolCalls1); const toolCalls2 = [ createToolCall({ callId: '2', @@ -357,11 +379,20 @@ describe('', () => { resultDisplay: 'line1', }), ]; + const item2 = createItem(toolCalls2); const { lastFrame, unmount } = renderWithProviders( - - + + , { config: baseMockConfig, @@ -392,8 +423,9 @@ describe('', () => { status: CoreToolCallStatus.Success, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -419,8 +451,9 @@ describe('', () => { status: CoreToolCallStatus.Success, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig, uiState: { @@ -454,9 +487,11 @@ describe('', () => { resultDisplay: '', // No result }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( , @@ -516,9 +551,10 @@ describe('', () => { resultDisplay, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig }, ); @@ -544,9 +580,10 @@ describe('', () => { status: CoreToolCallStatus.Scheduled, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig }, ); @@ -565,10 +602,12 @@ describe('', () => { status: CoreToolCallStatus.Executing, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( , @@ -603,9 +642,10 @@ describe('', () => { approvalMode: mode, }), ]; + const item = createItem(toolCalls); const { lastFrame, unmount } = renderWithProviders( - , + , { config: baseMockConfig }, ); diff --git a/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx b/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx index aade976e0bf..4c8abc3189b 100644 --- a/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx +++ b/packages/cli/src/ui/components/messages/ToolGroupMessage.tsx @@ -7,7 +7,11 @@ import type React from 'react'; import { useMemo } from 'react'; import { Box, Text } from 'ink'; -import type { IndividualToolCallDisplay } from '../../types.js'; +import type { + HistoryItem, + HistoryItemWithoutId, + IndividualToolCallDisplay, +} from '../../types.js'; import { ToolCallStatus, mapCoreStatusToDisplayStatus } from '../../types.js'; import { ToolMessage } from './ToolMessage.js'; import { ShellToolMessage } from './ShellToolMessage.js'; @@ -17,34 +21,28 @@ import { isShellTool } from './ToolShared.js'; import { shouldHideToolCall } from '@google/gemini-cli-core'; import { ShowMoreLines } from '../ShowMoreLines.js'; import { useUIState } from '../../contexts/UIStateContext.js'; +import { getToolGroupBorderAppearance } from '../../utils/borderStyles.js'; interface ToolGroupMessageProps { - groupId: number; + item: HistoryItem | HistoryItemWithoutId; toolCalls: IndividualToolCallDisplay[]; availableTerminalHeight?: number; terminalWidth: number; - activeShellPtyId?: number | null; - embeddedShellFocused?: boolean; onShellInputSubmit?: (input: string) => void; borderTop?: boolean; borderBottom?: boolean; - borderColor: string; - borderDimColor: boolean; } // Main component renders the border and maps the tools using ToolMessage const TOOL_MESSAGE_HORIZONTAL_MARGIN = 4; export const ToolGroupMessage: React.FC = ({ + item, toolCalls: allToolCalls, availableTerminalHeight, terminalWidth, - activeShellPtyId, - embeddedShellFocused, borderTop: borderTopOverride, borderBottom: borderBottomOverride, - borderColor, - borderDimColor, }) => { // Filter out tool calls that should be hidden (e.g. in-progress Ask User, or Plan Mode operations). const toolCalls = useMemo( @@ -62,7 +60,31 @@ export const ToolGroupMessage: React.FC = ({ ); const config = useConfig(); - const { constrainHeight } = useUIState(); + const { + constrainHeight, + activePtyId, + embeddedShellFocused, + backgroundShells, + pendingHistoryItems, + } = useUIState(); + + const { borderColor, borderDimColor } = useMemo( + () => + getToolGroupBorderAppearance( + item, + activePtyId, + embeddedShellFocused, + pendingHistoryItems, + backgroundShells, + ), + [ + item, + activePtyId, + embeddedShellFocused, + pendingHistoryItems, + backgroundShells, + ], + ); // 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. @@ -151,12 +173,7 @@ export const ToolGroupMessage: React.FC = ({ width={contentWidth} > {isShellToolCall ? ( - + ) : ( )} diff --git a/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx b/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx index f52ee6892b8..dd3184a19ca 100644 --- a/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx +++ b/packages/cli/src/ui/components/messages/ToolResultDisplayOverflow.test.tsx @@ -35,9 +35,7 @@ describe('ToolResultDisplay Overflow', () => { const { lastFrame } = renderWithProviders( { data={['item1']} renderItem={() => ( @@ -167,9 +165,7 @@ describe('ToolMessage Sticky Header Regression', () => { data={['item1']} renderItem={() => ( diff --git a/packages/cli/src/ui/utils/borderStyles.ts b/packages/cli/src/ui/utils/borderStyles.ts index 02be0c08712..b3a0cb52bb6 100644 --- a/packages/cli/src/ui/utils/borderStyles.ts +++ b/packages/cli/src/ui/utils/borderStyles.ts @@ -104,14 +104,20 @@ export function getToolGroupBorderAppearance( const isPending = hasPending || (item.tools.length === 0 && isCurrentlyInShellTurn); + const isEffectivelyFocused = + isEmbeddedShellFocused || + (item.tools.length === 0 && + isCurrentlyInShellTurn && + !!embeddedShellFocused); + const borderColor = - (isShell && isPending) || isEmbeddedShellFocused + (isShell && isPending) || isEffectivelyFocused ? theme.ui.symbol : isPending ? theme.status.warning : theme.border.default; - const borderDimColor = isPending && (!isShell || !isEmbeddedShellFocused); + const borderDimColor = isPending && (!isShell || !isEffectivelyFocused); return { borderColor, borderDimColor }; }