Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 9 additions & 36 deletions packages/cli/src/ui/components/messages/ToolGroupMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import { theme } from '../../semantic-colors.js';
import { useConfig } from '../../contexts/ConfigContext.js';
import { isShellTool } from './ToolShared.js';
import {
shouldHideToolCall,
CoreToolCallStatus,
isVisibleInToolGroup,
Kind,
EDIT_DISPLAY_NAME,
GLOB_DISPLAY_NAME,
Expand All @@ -36,6 +35,7 @@ import {
READ_MANY_FILES_DISPLAY_NAME,
isFileDiff,
} from '@google/gemini-cli-core';
import { buildToolVisibilityContextFromDisplay } from '../../utils/historyUtils.js';
import { useUIState } from '../../contexts/UIStateContext.js';
import { getToolGroupBorderAppearance } from '../../utils/borderStyles.js';
import { useSettings } from '../../contexts/SettingsContext.js';
Expand Down Expand Up @@ -125,40 +125,13 @@ export const ToolGroupMessage: React.FC<ToolGroupMessageProps> = ({
// Filter out tool calls that should be hidden (e.g. in-progress Ask User, or Plan Mode operations).
const visibleToolCalls = useMemo(
() =>
allToolCalls.filter((t) => {
// Hide internal errors unless full verbosity
if (
isLowErrorVerbosity &&
t.status === CoreToolCallStatus.Error &&
!t.isClientInitiated
) {
return false;
}
// Standard hiding logic (e.g. Plan Mode internal edits)
if (
shouldHideToolCall({
displayName: t.name,
status: t.status,
approvalMode: t.approvalMode,
hasResultDisplay: !!t.resultDisplay,
parentCallId: t.parentCallId,
})
) {
return false;
}

// 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.
const displayStatus = mapCoreStatusToDisplayStatus(t.status);

// We hide Confirming tools from the history log because they are
// currently being rendered in the interactive ToolConfirmationQueue.
// We show everything else, including Pending (waiting to run) and
// Canceled (rejected by user), to ensure the history is complete
// and to avoid tools "vanishing" after approval.
return displayStatus !== ToolCallStatus.Confirming;
}),
allToolCalls.filter((t) =>
// Use the unified visibility utility
isVisibleInToolGroup(
buildToolVisibilityContextFromDisplay(t),
isLowErrorVerbosity ? 'low' : 'full',
),
),
[allToolCalls, isLowErrorVerbosity],
);

Expand Down
26 changes: 3 additions & 23 deletions packages/cli/src/ui/hooks/useGeminiStream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ import {
isBackgroundExecutionData,
Kind,
ACTIVATE_SKILL_TOOL_NAME,
shouldHideToolCall,
isRenderedInHistory,
buildToolVisibilityContext,
UPDATE_TOPIC_TOOL_NAME,
UPDATE_TOPIC_DISPLAY_NAME,
} from '@google/gemini-cli-core';
Expand Down Expand Up @@ -647,29 +648,8 @@ export const useGeminiStream = (
toolCalls.every((tc) => pushedToolCallIds.has(tc.request.callId));

const isToolVisible = (tc: TrackedToolCall) => {
const displayName = tc.tool?.displayName ?? tc.request.name;

let hasResultDisplay = false;
if (
tc.status === CoreToolCallStatus.Success ||
tc.status === CoreToolCallStatus.Error ||
tc.status === CoreToolCallStatus.Cancelled
) {
hasResultDisplay = !!tc.response?.resultDisplay;
} else if (tc.status === CoreToolCallStatus.Executing) {
hasResultDisplay = !!tc.liveOutput;
}

// AskUser tools and Plan Mode write/edit are handled by this logic
if (
shouldHideToolCall({
displayName,
status: tc.status,
approvalMode: tc.approvalMode,
hasResultDisplay,
parentCallId: tc.request.parentCallId,
})
) {
if (!isRenderedInHistory(buildToolVisibilityContext(tc))) {
return false;
}

Expand Down
18 changes: 14 additions & 4 deletions packages/cli/src/ui/utils/confirmingTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreToolCallStatus } from '@google/gemini-cli-core';
import {
CoreToolCallStatus,
belongsInConfirmationQueue,
} from '@google/gemini-cli-core';
import {
type HistoryItemWithoutId,
type IndividualToolCallDisplay,
} from '../types.js';
import { getAllToolCalls } from './historyUtils.js';
import {
getAllToolCalls,
buildToolVisibilityContextFromDisplay,
} from './historyUtils.js';

export interface ConfirmingToolState {
tool: IndividualToolCallDisplay;
Expand All @@ -33,14 +39,18 @@ export function getConfirmingToolState(
return null;
}

const actionablePendingTools = allPendingTools.filter((tool) =>
belongsInConfirmationQueue(buildToolVisibilityContextFromDisplay(tool)),
);

const head = confirmingTools[0];
const headIndexInFullList = allPendingTools.findIndex(
const headIndexInFullList = actionablePendingTools.findIndex(
(tool) => tool.callId === head.callId,
);

return {
tool: head,
index: headIndexInFullList + 1,
total: allPendingTools.length,
total: actionablePendingTools.length,
};
}
18 changes: 18 additions & 0 deletions packages/cli/src/ui/utils/historyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { type ToolVisibilityContext } from '@google/gemini-cli-core';
import { CoreToolCallStatus } from '../types.js';
import type {
HistoryItem,
Expand All @@ -12,6 +13,23 @@ import type {
IndividualToolCallDisplay,
} from '../types.js';

/**
* Maps an IndividualToolCallDisplay from the CLI to a ToolVisibilityContext for core logic.
*/
export function buildToolVisibilityContextFromDisplay(
tool: IndividualToolCallDisplay,
): ToolVisibilityContext {
return {
name: tool.originalRequestName ?? tool.name,
displayName: tool.name, // In CLI, 'name' is usually the resolved display name
status: tool.status,
hasResult: !!tool.resultDisplay,
approvalMode: tool.approvalMode,
isClientInitiated: tool.isClientInitiated,
parentCallId: tool.parentCallId,
};
}

export function getLastTurnToolCallIds(
history: HistoryItem[],
pendingHistoryItems: HistoryItemWithoutId[],
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ export {
PRIORITY_YOLO_ALLOW_ALL,
} from './policy/types.js';
export * from './utils/tool-utils.js';
export * from './utils/tool-visibility.js';
export * from './utils/terminalSerializer.js';
export * from './utils/systemEncoding.js';
export * from './utils/textUtils.js';
Expand Down
107 changes: 2 additions & 105 deletions packages/core/src/utils/tool-utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,113 +5,10 @@
*/

import { expect, describe, it } from 'vitest';
import {
doesToolInvocationMatch,
getToolSuggestion,
shouldHideToolCall,
} from './tool-utils.js';
import {
ReadFileTool,
ApprovalMode,
CoreToolCallStatus,
ASK_USER_DISPLAY_NAME,
WRITE_FILE_DISPLAY_NAME,
EDIT_DISPLAY_NAME,
READ_FILE_DISPLAY_NAME,
type AnyToolInvocation,
type Config,
} from '../index.js';
import { doesToolInvocationMatch, getToolSuggestion } from './tool-utils.js';
import { ReadFileTool, type AnyToolInvocation, type Config } from '../index.js';
import { createMockMessageBus } from '../test-utils/mock-message-bus.js';

describe('shouldHideToolCall', () => {
it.each([
{
status: CoreToolCallStatus.Scheduled,
hasResult: true,
shouldHide: true,
},
{
status: CoreToolCallStatus.Executing,
hasResult: true,
shouldHide: true,
},
{
status: CoreToolCallStatus.AwaitingApproval,
hasResult: true,
shouldHide: true,
},
{
status: CoreToolCallStatus.Validating,
hasResult: true,
shouldHide: true,
},
{
status: CoreToolCallStatus.Success,
hasResult: true,
shouldHide: false,
},
{
status: CoreToolCallStatus.Error,
hasResult: false,
shouldHide: true,
},
{
status: CoreToolCallStatus.Error,
hasResult: true,
shouldHide: false,
},
])(
'AskUser: status=$status, hasResult=$hasResult -> hide=$shouldHide',
({ status, hasResult, shouldHide }) => {
expect(
shouldHideToolCall({
displayName: ASK_USER_DISPLAY_NAME,
status,
hasResultDisplay: hasResult,
}),
).toBe(shouldHide);
},
);

it.each([
{
name: WRITE_FILE_DISPLAY_NAME,
mode: ApprovalMode.PLAN,
visible: false,
},
{ name: EDIT_DISPLAY_NAME, mode: ApprovalMode.PLAN, visible: false },
{
name: WRITE_FILE_DISPLAY_NAME,
mode: ApprovalMode.DEFAULT,
visible: true,
},
{ name: READ_FILE_DISPLAY_NAME, mode: ApprovalMode.PLAN, visible: true },
])(
'Plan Mode: tool=$name, mode=$mode -> visible=$visible',
({ name, mode, visible }) => {
expect(
shouldHideToolCall({
displayName: name,
status: CoreToolCallStatus.Success,
approvalMode: mode,
hasResultDisplay: true,
}),
).toBe(!visible);
},
);

it('hides tool calls with a parentCallId', () => {
expect(
shouldHideToolCall({
displayName: 'any_tool',
status: CoreToolCallStatus.Success,
hasResultDisplay: true,
parentCallId: 'some-parent',
}),
).toBe(true);
});
});

describe('getToolSuggestion', () => {
it('should suggest the top N closest tool names for a typo', () => {
const allToolNames = ['list_files', 'read_file', 'write_file'];
Expand Down
67 changes: 1 addition & 66 deletions packages/core/src/utils/tool-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,7 @@ import {
} from '../index.js';
import { SHELL_TOOL_NAMES } from './shell-utils.js';
import levenshtein from 'fast-levenshtein';
import { ApprovalMode } from '../policy/types.js';
import {
CoreToolCallStatus,
type ToolCallResponseInfo,
} from '../scheduler/types.js';
import {
ASK_USER_DISPLAY_NAME,
WRITE_FILE_DISPLAY_NAME,
EDIT_DISPLAY_NAME,
} from '../tools/tool-names.js';
import type { ToolCallResponseInfo } from '../scheduler/types.js';

/**
* Validates if an object is a ToolCallResponseInfo.
Expand All @@ -36,62 +27,6 @@ export function isToolCallResponseInfo(
);
}

/**
* Options for determining if a tool call should be hidden in the CLI history.
*/
export interface ShouldHideToolCallParams {
/** The display name of the tool. */
displayName: string;
/** The current status of the tool call. */
status: CoreToolCallStatus;
/** The approval mode active when the tool was called. */
approvalMode?: ApprovalMode;
/** Whether the tool has produced a result for display. */
hasResultDisplay: boolean;
/** The ID of the parent tool call, if any. */
parentCallId?: string;
}

/**
* Determines if a tool call should be hidden from the standard tool history UI.
*
* We hide tools in several cases:
* 1. Tool calls that have a parent, as they are "internal" to another tool (e.g. subagent).
* 2. Ask User tools that are in progress, displayed via specialized UI.
* 3. Ask User tools that errored without result display, typically param
* validation errors that the agent automatically recovers from.
* 4. WriteFile and Edit tools when in Plan Mode, redundant because the
* resulting plans are displayed separately upon exiting plan mode.
*/
export function shouldHideToolCall(params: ShouldHideToolCallParams): boolean {
const { displayName, status, approvalMode, hasResultDisplay, parentCallId } =
params;

if (parentCallId) {
return true;
}

switch (displayName) {
case ASK_USER_DISPLAY_NAME:
switch (status) {
case CoreToolCallStatus.Scheduled:
case CoreToolCallStatus.Validating:
case CoreToolCallStatus.Executing:
case CoreToolCallStatus.AwaitingApproval:
return true;
case CoreToolCallStatus.Error:
return !hasResultDisplay;
default:
return false;
}
case WRITE_FILE_DISPLAY_NAME:
case EDIT_DISPLAY_NAME:
return approvalMode === ApprovalMode.PLAN;
default:
return false;
}
}

/**
* Generates a suggestion string for a tool name that was not found in the registry.
* It finds the closest matches based on Levenshtein distance.
Expand Down
Loading
Loading