Fix message too large issue. (#19499)

This commit is contained in:
Christian Gunderman
2026-02-19 19:06:36 +00:00
committed by GitHub
parent a00eb3b8e6
commit c276d0c7b6
5 changed files with 52 additions and 5 deletions

View File

@@ -240,10 +240,7 @@ export class ChatCompressionService {
const curatedHistory = chat.getHistory(true);
// Regardless of `force`, don't do anything if the history is empty.
if (
curatedHistory.length === 0 ||
(hasFailedCompressionAttempt && !force)
) {
if (curatedHistory.length === 0) {
return {
newHistory: null,
info: {
@@ -285,6 +282,35 @@ export class ChatCompressionService {
config,
);
// If summarization previously failed (and not forced), we only rely on truncation.
// We do NOT attempt to invoke the LLM for summarization again to avoid repeated failures/costs.
if (hasFailedCompressionAttempt && !force) {
const truncatedTokenCount = estimateTokenCountSync(
truncatedHistory.flatMap((c) => c.parts || []),
);
// If truncation reduced the size, we consider it a successful "compression" (truncation only).
if (truncatedTokenCount < originalTokenCount) {
return {
newHistory: truncatedHistory,
info: {
originalTokenCount,
newTokenCount: truncatedTokenCount,
compressionStatus: CompressionStatus.CONTENT_TRUNCATED,
},
};
}
return {
newHistory: null,
info: {
originalTokenCount,
newTokenCount: originalTokenCount,
compressionStatus: CompressionStatus.NOOP,
},
};
}
const splitPoint = findCompressSplitPoint(
truncatedHistory,
1 - COMPRESSION_PRESERVE_THRESHOLD,