Files
gemini-cli/packages/cli/src/ui/components/messages/ToolConfirmationMessage.tsx
T

171 lines
4.9 KiB
TypeScript
Raw Normal View History

/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
2025-04-15 21:41:08 -07:00
import React from 'react';
import { Box, Text, useInput } from 'ink';
2025-04-21 10:53:11 -04:00
import { DiffRenderer } from './DiffRenderer.js';
import { Colors } from '../../colors.js';
2025-04-17 18:06:21 -04:00
import {
ToolCallConfirmationDetails,
ToolConfirmationOutcome,
ToolExecuteConfirmationDetails,
ToolMcpConfirmationDetails,
Config,
} from '@gemini-cli/core';
import {
RadioButtonSelect,
RadioSelectItem,
} from '../shared/RadioButtonSelect.js';
2025-04-15 21:41:08 -07:00
export interface ToolConfirmationMessageProps {
confirmationDetails: ToolCallConfirmationDetails;
config?: Config;
2025-06-12 02:21:54 +01:00
isFocused?: boolean;
2025-04-15 21:41:08 -07:00
}
2025-04-18 19:09:41 -04:00
export const ToolConfirmationMessage: React.FC<
ToolConfirmationMessageProps
2025-06-12 18:28:20 +01:00
> = ({ confirmationDetails, isFocused = true }) => {
2025-04-15 21:41:08 -07:00
const { onConfirm } = confirmationDetails;
useInput((_, key) => {
2025-06-12 02:21:54 +01:00
if (!isFocused) return;
2025-04-15 21:41:08 -07:00
if (key.escape) {
onConfirm(ToolConfirmationOutcome.Cancel);
}
});
const handleSelect = (item: ToolConfirmationOutcome) => onConfirm(item);
2025-04-15 21:41:08 -07:00
let bodyContent: React.ReactNode | null = null; // Removed contextDisplay here
let question: string;
const options: Array<RadioSelectItem<ToolConfirmationOutcome>> = new Array<
RadioSelectItem<ToolConfirmationOutcome>
>();
2025-04-15 21:41:08 -07:00
if (confirmationDetails.type === 'edit') {
if (confirmationDetails.isModifying) {
return (
<Box
minWidth="90%"
borderStyle="round"
borderColor={Colors.Gray}
justifyContent="space-around"
padding={1}
overflow="hidden"
>
<Text>Modify in progress: </Text>
<Text color={Colors.AccentGreen}>
Save and close external editor to continue
</Text>
</Box>
);
}
2025-04-15 21:41:08 -07:00
// Body content is now the DiffRenderer, passing filename to it
// The bordered box is removed from here and handled within DiffRenderer
bodyContent = (
<DiffRenderer
diffContent={confirmationDetails.fileDiff}
filename={confirmationDetails.fileName}
/>
);
2025-04-15 21:41:08 -07:00
question = `Apply this change?`;
options.push(
2025-04-17 18:06:21 -04:00
{
label: 'Yes, allow once',
2025-04-17 18:06:21 -04:00
value: ToolConfirmationOutcome.ProceedOnce,
},
{
label: 'Yes, allow always',
2025-04-17 18:06:21 -04:00
value: ToolConfirmationOutcome.ProceedAlways,
},
2025-06-12 18:28:20 +01:00
{
2025-06-12 02:21:54 +01:00
label: 'Modify with external editor',
value: ToolConfirmationOutcome.ModifyWithEditor,
2025-06-12 18:28:20 +01:00
},
{ label: 'No (esc)', value: ToolConfirmationOutcome.Cancel },
);
} else if (confirmationDetails.type === 'exec') {
2025-04-17 18:06:21 -04:00
const executionProps =
confirmationDetails as ToolExecuteConfirmationDetails;
2025-04-15 21:41:08 -07:00
bodyContent = (
2025-04-17 18:06:21 -04:00
<Box flexDirection="column">
<Box paddingX={1} marginLeft={1}>
<Text color={Colors.AccentCyan}>{executionProps.command}</Text>
2025-04-15 21:41:08 -07:00
</Box>
2025-04-17 18:06:21 -04:00
</Box>
2025-04-15 21:41:08 -07:00
);
question = `Allow execution?`;
options.push(
2025-04-17 18:06:21 -04:00
{
label: 'Yes, allow once',
2025-04-17 18:06:21 -04:00
value: ToolConfirmationOutcome.ProceedOnce,
},
{
label: `Yes, allow always "${executionProps.rootCommand} ..."`,
2025-04-17 18:06:21 -04:00
value: ToolConfirmationOutcome.ProceedAlways,
},
2025-04-22 08:39:58 -04:00
{ label: 'No (esc)', value: ToolConfirmationOutcome.Cancel },
2025-04-15 21:41:08 -07:00
);
} else {
// mcp tool confirmation
const mcpProps = confirmationDetails as ToolMcpConfirmationDetails;
bodyContent = (
<Box flexDirection="column" paddingX={1} marginLeft={1}>
<Text color={Colors.AccentCyan}>MCP Server: {mcpProps.serverName}</Text>
<Text color={Colors.AccentCyan}>Tool: {mcpProps.toolName}</Text>
</Box>
);
question = `Allow execution of MCP tool "${mcpProps.toolName}" from server "${mcpProps.serverName}"?`;
options.push(
{
label: 'Yes, allow once',
value: ToolConfirmationOutcome.ProceedOnce,
},
{
label: `Yes, always allow tool "${mcpProps.toolName}" from server "${mcpProps.serverName}"`,
value: ToolConfirmationOutcome.ProceedAlwaysTool, // Cast until types are updated
},
{
label: `Yes, always allow all tools from server "${mcpProps.serverName}"`,
value: ToolConfirmationOutcome.ProceedAlwaysServer,
},
{ label: 'No (esc)', value: ToolConfirmationOutcome.Cancel },
);
2025-04-15 21:41:08 -07:00
}
return (
<Box flexDirection="column" padding={1} minWidth="90%">
2025-04-15 21:41:08 -07:00
{/* Body Content (Diff Renderer or Command Info) */}
{/* No separate context display here anymore for edits */}
<Box flexGrow={1} flexShrink={1} overflow="hidden" marginBottom={1}>
2025-04-17 18:06:21 -04:00
{bodyContent}
2025-04-15 21:41:08 -07:00
</Box>
{/* Confirmation Question */}
<Box marginBottom={1} flexShrink={0}>
<Text>{question}</Text>
</Box>
{/* Select Input for Options */}
<Box flexShrink={0}>
2025-06-12 02:21:54 +01:00
<RadioButtonSelect
items={options}
onSelect={handleSelect}
isFocused={isFocused}
/>
2025-04-15 21:41:08 -07:00
</Box>
</Box>
);
};