2025-06-13 21:21:40 -07:00
|
|
|
/**
|
|
|
|
|
* @license
|
|
|
|
|
* Copyright 2025 Google LLC
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { Box, Text } from 'ink';
|
2025-08-26 00:04:53 +02:00
|
|
|
import type { CompressionProps } from '../../types.js';
|
2025-10-07 10:28:35 -07:00
|
|
|
import { CliSpinner } from '../CliSpinner.js';
|
2025-09-10 10:57:07 -07:00
|
|
|
import { theme } from '../../semantic-colors.js';
|
2025-08-28 20:52:14 +00:00
|
|
|
import { SCREEN_READER_MODEL_PREFIX } from '../../textConstants.js';
|
2026-03-17 23:00:00 -07:00
|
|
|
import { CompressionStatus, tokenLimit } from '@google/gemini-cli-core';
|
2025-06-13 21:21:40 -07:00
|
|
|
|
|
|
|
|
export interface CompressionDisplayProps {
|
|
|
|
|
compression: CompressionProps;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
2025-07-05 17:23:39 +02:00
|
|
|
* Compression messages appear when the /compress command is run, and show a loading spinner
|
2025-06-13 21:21:40 -07:00
|
|
|
* while compression is in progress, followed up by some compression stats.
|
|
|
|
|
*/
|
2025-09-12 03:11:08 +05:30
|
|
|
export function CompressionMessage({
|
2025-06-13 21:21:40 -07:00
|
|
|
compression,
|
2025-09-12 03:11:08 +05:30
|
|
|
}: CompressionDisplayProps): React.JSX.Element {
|
2026-03-17 23:00:00 -07:00
|
|
|
const {
|
|
|
|
|
isPending,
|
|
|
|
|
originalTokenCount,
|
|
|
|
|
newTokenCount,
|
|
|
|
|
compressionStatus,
|
|
|
|
|
model,
|
|
|
|
|
} = compression;
|
2025-09-12 03:11:08 +05:30
|
|
|
|
|
|
|
|
const originalTokens = originalTokenCount ?? 0;
|
|
|
|
|
const newTokens = newTokenCount ?? 0;
|
|
|
|
|
|
|
|
|
|
const getCompressionText = () => {
|
|
|
|
|
if (isPending) {
|
|
|
|
|
return 'Compressing chat history';
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-17 23:00:00 -07:00
|
|
|
const limit = model ? tokenLimit(model) : 0;
|
|
|
|
|
const formatPercent = (tokens: number) =>
|
|
|
|
|
limit > 0
|
|
|
|
|
? `${Math.round((tokens / limit) * 100)}% (${tokens.toLocaleString()} tokens)`
|
|
|
|
|
: `${tokens.toLocaleString()} tokens`;
|
|
|
|
|
|
2025-09-12 03:11:08 +05:30
|
|
|
switch (compressionStatus) {
|
|
|
|
|
case CompressionStatus.COMPRESSED:
|
2026-03-17 23:00:00 -07:00
|
|
|
return `Context compressed from ${formatPercent(originalTokens)} to ${formatPercent(newTokens)}. Change threshold in /settings.`;
|
2025-09-12 03:11:08 +05:30
|
|
|
case CompressionStatus.COMPRESSION_FAILED_INFLATED_TOKEN_COUNT:
|
|
|
|
|
// For smaller histories (< 50k tokens), compression overhead likely exceeds benefits
|
|
|
|
|
if (originalTokens < 50000) {
|
|
|
|
|
return 'Compression was not beneficial for this history size.';
|
|
|
|
|
}
|
|
|
|
|
// For larger histories where compression should work but didn't,
|
|
|
|
|
// this suggests an issue with the compression process itself
|
2026-03-17 23:00:00 -07:00
|
|
|
return 'Chat history compression did not reduce size.';
|
2025-09-12 03:11:08 +05:30
|
|
|
case CompressionStatus.COMPRESSION_FAILED_TOKEN_COUNT_ERROR:
|
|
|
|
|
return 'Could not compress chat history due to a token counting error.';
|
2026-01-20 09:43:15 +01:00
|
|
|
case CompressionStatus.COMPRESSION_FAILED_EMPTY_SUMMARY:
|
2026-03-17 23:00:00 -07:00
|
|
|
return 'Chat history compression failed: empty summary.';
|
2025-09-12 03:11:08 +05:30
|
|
|
case CompressionStatus.NOOP:
|
2025-10-14 00:02:03 -04:00
|
|
|
return 'Nothing to compress.';
|
2025-09-12 03:11:08 +05:30
|
|
|
default:
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const text = getCompressionText();
|
2025-06-13 21:21:40 -07:00
|
|
|
|
|
|
|
|
return (
|
2026-03-17 23:00:00 -07:00
|
|
|
<Box flexDirection="row" paddingLeft={1} marginBottom={1}>
|
|
|
|
|
<Box marginRight={1}>{isPending && <CliSpinner type="dots" />}</Box>
|
2025-06-13 21:21:40 -07:00
|
|
|
<Box>
|
|
|
|
|
<Text
|
2026-03-17 23:00:00 -07:00
|
|
|
color={theme.text.secondary}
|
2025-08-21 22:29:15 +00:00
|
|
|
aria-label={SCREEN_READER_MODEL_PREFIX}
|
2026-03-17 23:00:00 -07:00
|
|
|
italic
|
2025-06-13 21:21:40 -07:00
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</Text>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
2025-09-12 03:11:08 +05:30
|
|
|
}
|