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

123 lines
3.6 KiB
TypeScript
Raw Normal View History

2025-04-15 21:41:08 -07:00
import React from 'react';
import { Box, Text, useInput } from 'ink';
import SelectInput from 'ink-select-input';
2025-04-17 18:06:21 -04:00
import {
ToolCallConfirmationDetails,
ToolEditConfirmationDetails,
ToolConfirmationOutcome,
ToolExecuteConfirmationDetails,
} from '../../types.js'; // Adjust path as needed
2025-04-15 21:41:08 -07:00
import { PartListUnion } from '@google/genai';
import DiffRenderer from './DiffRenderer.js';
import { UI_WIDTH } from '../../constants.js';
export interface ToolConfirmationMessageProps {
confirmationDetails: ToolCallConfirmationDetails;
onSubmit: (value: PartListUnion) => void;
}
2025-04-17 18:06:21 -04:00
function isEditDetails(
props: ToolCallConfirmationDetails,
): props is ToolEditConfirmationDetails {
2025-04-15 21:41:08 -07:00
return (props as ToolEditConfirmationDetails).fileName !== undefined;
}
interface InternalOption {
label: string;
value: ToolConfirmationOutcome;
}
2025-04-17 18:06:21 -04:00
const ToolConfirmationMessage: React.FC<ToolConfirmationMessageProps> = ({
confirmationDetails,
}) => {
2025-04-15 21:41:08 -07:00
const { onConfirm } = confirmationDetails;
useInput((_, key) => {
if (key.escape) {
onConfirm(ToolConfirmationOutcome.Cancel);
}
});
const handleSelect = (item: InternalOption) => {
onConfirm(item.value);
};
let title: string;
let bodyContent: React.ReactNode | null = null; // Removed contextDisplay here
let question: string;
const options: InternalOption[] = [];
if (isEditDetails(confirmationDetails)) {
2025-04-17 18:06:21 -04:00
title = 'Edit'; // Title for the outer 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
2025-04-17 18:06:21 -04:00
bodyContent = <DiffRenderer diffContent={confirmationDetails.fileDiff} />;
2025-04-15 21:41:08 -07:00
question = `Apply this change?`;
options.push(
2025-04-17 18:06:21 -04:00
{
label: '1. Yes, apply change',
value: ToolConfirmationOutcome.ProceedOnce,
},
{
label: '2. Yes, always apply file edits',
value: ToolConfirmationOutcome.ProceedAlways,
},
{ label: '3. No (esc)', value: ToolConfirmationOutcome.Cancel },
2025-04-15 21:41:08 -07:00
);
} else {
2025-04-17 18:06:21 -04:00
const executionProps =
confirmationDetails as ToolExecuteConfirmationDetails;
title = 'Execute Command'; // Title for the outer box
2025-04-15 21:41:08 -07:00
// For execution, we still need context display and description
const commandDisplay = <Text color="cyan">{executionProps.command}</Text>;
// Combine command and description into bodyContent for layout consistency
bodyContent = (
2025-04-17 18:06:21 -04:00
<Box flexDirection="column">
<Box paddingX={1} marginLeft={1}>
{commandDisplay}
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?`;
const alwaysLabel = `2. Yes, always allow '${executionProps.rootCommand}' commands`;
options.push(
2025-04-17 18:06:21 -04:00
{
label: '1. Yes, allow once',
value: ToolConfirmationOutcome.ProceedOnce,
},
{
label: alwaysLabel,
value: ToolConfirmationOutcome.ProceedAlways,
},
{ label: '3. No (esc)', value: ToolConfirmationOutcome.Cancel },
2025-04-15 21:41:08 -07:00
);
}
return (
<Box flexDirection="column" padding={1} minWidth={UI_WIDTH}>
{/* 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}>
<SelectInput items={options} onSelect={handleSelect} />
</Box>
</Box>
);
};
2025-04-17 18:06:21 -04:00
export default ToolConfirmationMessage;