2026-11-27 16:35:51 +01:00
using AIStudio.AIOperations ;
using AIStudio.AIOperations.ConversationSummary ;
2026-11-21 14:16:04 +01:00
using AIStudio.Models ;
using AIStudio.Settings ;
using AIStudio.Tools.Services ;
namespace AIStudio.Chat.Compression ;
/// <summary>
2026-11-27 16:35:51 +01:00
/// Decides which model writes the summary of a chat, and whether compressing would make enough room in the context
/// window of the model of the chat.
2026-11-21 14:16:04 +01:00
/// </summary>
/// <remarks>
2026-11-27 16:35:51 +01:00
/// The summary itself is written by an AI operation, see ConversationSummaryOperation. This service holds what
/// belongs to the chat: which model may write its summary, and whether the notice before sending offers to compress
/// it. It keeps nothing of a call, so several chats may ask at the same time.
2026-11-21 14:16:04 +01:00
/// </remarks>
/// <param name="tokenCounter">Counts the tokens of the messages, with the tokenizer of the provider.</param>
2026-11-27 16:35:51 +01:00
/// <param name="providerResolver">Chooses the model which writes the summary.</param>
/// <param name="settingsManager">The settings, for the model picked for compressing.</param>
public sealed class ChatCompressionService ( ConversationTokenCounter tokenCounter , AIOperationProviderResolver providerResolver , SettingsManager settingsManager )
2026-11-21 14:16:04 +01:00
{
/// <summary>
2026-11-27 16:35:51 +01:00
/// What compressing a chat adds to the choice of the model which writes the summary.
2026-11-21 14:16:04 +01:00
/// </summary>
/// <remarks>
2026-11-27 16:35:51 +01:00
/// <para>
/// The model picked for compressing writes the summary when it may. Otherwise, the model of the chat does: it
/// saw everything the summary reads already, so the minimum confidence of compressing does not apply to it.
/// </para>
/// <para>
/// Both have to be allowed in the chat, with its confidence, the security of its data sources, and what the
/// organization prohibited, see ChatThreadExtensions.IsLLMProviderAllowed, and their model has to be large
/// enough for a summary, see ConversationSummaryBudget.CanSummarizeWith. Whether a model can see images does
/// not matter: the summary reads no image, see ConversationSummaryOperation.RunAsync.
/// </para>
/// <para>
/// Every place which runs the summary or tells about it in advance builds the options here, so that the model
/// it names is the one which writes the summary.
/// </para>
2026-11-21 14:16:04 +01:00
/// </remarks>
2026-11-27 16:35:51 +01:00
/// <param name="chat">The chat to summarize.</param>
/// <param name="chatProvider">The provider of the chat, which continues with the summary.</param>
/// <returns>The options for choosing the model which writes the summary.</returns>
public static AIOperationProviderOptions GetProviderOptions ( ChatThread chat , AIStudio . Settings . Provider chatProvider ) => new ()
{
FallbackProviderIds = [ chatProvider . Id ],
OwnMinimumConfidenceAppliesToFallbacks = false ,
CheckProvider = provider => CheckWriter ( chat , provider ),
};
2026-11-21 14:16:04 +01:00
/// <summary>
2026-11-27 16:35:51 +01:00
/// Whether a provider may write the summary of a chat, as far as the chat and the size of its model decide.
2026-11-21 14:16:04 +01:00
/// </summary>
2026-11-27 16:35:51 +01:00
/// <param name="chat">The chat to summarize.</param>
/// <param name="provider">The provider which would write the summary.</param>
/// <returns>NONE when it may, NOT_ALLOWED_FOR_CONTENT when the chat does not allow it, CONTEXT_WINDOW_TOO_SMALL when its model is too small.</returns>
internal static AIOperationProviderIssue CheckWriter ( ChatThread chat , AIStudio . Settings . Provider provider )
{
if (! chat . IsLLMProviderAllowed ( provider ))
return AIOperationProviderIssue . NOT_ALLOWED_FOR_CONTENT ;
2026-11-21 14:16:04 +01:00
2026-11-27 16:35:51 +01:00
return ConversationSummaryBudget . CanSummarizeWith ( provider . GetModelProfile (). Context )
? AIOperationProviderIssue . NONE
: AIOperationProviderIssue . CONTEXT_WINDOW_TOO_SMALL ;
}
2026-11-21 14:16:04 +01:00
/// <summary>
2026-11-27 16:35:51 +01:00
/// Chooses the model which would write the summary of a chat, without logging the choice.
2026-11-21 14:16:04 +01:00
/// </summary>
/// <remarks>
2026-11-27 16:35:51 +01:00
/// For the notice before sending, the button of the chat, and the dialog, which tells when the model of the chat
/// writes the summary instead of the one picked for it. Running the summary chooses again, see
/// AIOperationProviderResolver.Resolve, since the settings may have changed in between.
2026-11-21 14:16:04 +01:00
/// </remarks>
2026-11-27 16:35:51 +01:00
/// <param name="chat">The chat to summarize.</param>
/// <param name="chatProvider">The provider of the chat, which continues with the summary.</param>
/// <returns>The chosen provider with the reasons why the others were not chosen.</returns>
public AIOperationProviderChoice ChooseWriter ( ChatThread chat , AIStudio . Settings . Provider chatProvider ) => providerResolver . Preview ( ConversationSummaryOperation . GetProviderSettings ( settingsManager ), chat . RequiredProviderConfidence , GetProviderOptions ( chat , chatProvider ));
2026-11-21 14:16:04 +01:00
/// <summary>
/// Decides whether the notice before sending offers to compress a chat, or why compressing would not help.
/// </summary>
/// <remarks>
/// Compressing replaces only the messages the plan summarizes, see ChatCompressionPlan.Of: the system prompt, the
/// example conversation of the chat template, the last message of the user with its files, and the last answer of
/// the AI stay word for word, and the message about to be sent goes along unchanged. So it helps only when what is
/// left, together with the room for the summary, stays below the share from which AI Studio asks before sending,
2026-11-27 16:35:51 +01:00
/// see ChatCompressionOptions.ShareToAsk. The checks which need no count come first, see
2026-11-21 14:16:04 +01:00
/// OfferBeforeCounting. Then the summarized messages are counted with their files, which the count of the whole
/// conversation mostly left in the cache of the counter already, and taken off that count, see KeepsTooMuch.
/// When they cannot be counted, compressing is offered, since nothing speaks against it.<br/><br/>
///
2026-11-27 16:35:51 +01:00
/// All of it is counted with the tokenizer of the provider of the chat, since it is about the context window of
/// its model. The model which writes the summary only decides how long the summary may grow, see
/// ConversationSummaryBudget.TokensForTheSummary.<br/><br/>
///
2026-11-21 14:16:04 +01:00
/// Whether the place which asks allows compressing at all, e.g., with workspaces switched off, is left to that
/// place: it asks only where it does.
/// </remarks>
/// <param name="chat">The chat to compress.</param>
/// <param name="exampleConversationLength">How many blocks the example conversation of the chat template holds, 0 without a template.</param>
2026-11-27 16:35:51 +01:00
/// <param name="chatProvider">The provider of the chat, with the tokenizer to count with.</param>
2026-11-21 14:16:04 +01:00
/// <param name="tokensWithMessage">What the chat costs together with the message about to be sent, with the context window of the model.</param>
2026-11-27 16:35:51 +01:00
/// <param name="options">The settings of compressing, read once for this decision.</param>
2026-11-21 14:16:04 +01:00
/// <param name="token">Ends the count when nobody needs the answer anymore.</param>
/// <returns>Whether compressing is offered, or why not.</returns>
2026-11-27 16:35:51 +01:00
public async Task < ChatCompressionOffer > OfferBeforeSendingAsync ( ChatThread chat , int exampleConversationLength , AIStudio . Settings . Provider chatProvider , ConversationTokens tokensWithMessage , ChatCompressionOptions options , CancellationToken token = default )
2026-11-21 14:16:04 +01:00
{
2026-11-27 16:35:51 +01:00
var writer = this . ChooseWriter ( chat , chatProvider );
ContextWindow ? writerWindow = writer . HasProvider ? writer . Provider . GetModelProfile (). Context : null ;
2026-11-21 14:16:04 +01:00
// The message is in the input field, so an unanswered last message is summarized rather than taken along:
var plan = ChatCompressionPlan . Of ( chat , exampleConversationLength , draftIsEmpty : false );
2026-11-27 16:35:51 +01:00
var offer = OfferBeforeCounting ( tokensWithMessage . Window , writerWindow , plan is not null , tokensWithMessage . DraftTokens , options );
if ( offer is not ChatCompressionOffer . OFFERED || plan is null || writerWindow is not { } window )
2026-11-21 14:16:04 +01:00
return offer ;
var summarizedParts = ConversationParts . Of ( new ChatThread { Blocks = [.. plan . ToSummarize ] }, string . Empty , string . Empty , null , false , null );
2026-11-27 16:35:51 +01:00
var summarized = await tokenCounter . CountAsync ( chatProvider , summarizedParts , ReportedHistory . UNKNOWN , token );
2026-11-21 14:16:04 +01:00
if (! summarized . IsKnown )
return ChatCompressionOffer . OFFERED ;
2026-11-27 16:35:51 +01:00
var tokensForTheSummary = ConversationSummaryBudget . TokensForTheSummary ( window , tokensWithMessage . Window , options . MaxSummaryTokens );
return KeepsTooMuch ( tokensWithMessage . Window . DefaultTokens , tokensWithMessage . Tokens , summarized . HistoryTokens , tokensForTheSummary , options . ShareToAsk )
2026-11-21 14:16:04 +01:00
? ChatCompressionOffer . KEEPS_TOO_MUCH
: ChatCompressionOffer . OFFERED ;
}
/// <summary>
/// Decides what speaks against compressing before anything is counted.
/// </summary>
/// <remarks>
2026-11-27 16:35:51 +01:00
/// There has to be a model which may write the summary, see GetProviderOptions; the only reason for none which
/// the chat itself does not rule out already is a model too small for a summary. The message about to be sent
/// goes along unchanged, so it has to leave room for the summary by itself; that is checked before whether
/// there is something to summarize at all, since a message which does not fit would not fit after any
/// compressing either.
2026-11-21 14:16:04 +01:00
/// </remarks>
2026-11-27 16:35:51 +01:00
/// <param name="chatWindow">The context window of the model of the chat.</param>
/// <param name="writerWindow">The context window of the model which would write the summary, or null when no model may write it.</param>
2026-11-21 14:16:04 +01:00
/// <param name="hasSomethingToSummarize">Whether the chat holds messages to summarize besides the last exchange, see ChatCompressionPlan.Of.</param>
/// <param name="messageTokens">How many tokens the message about to be sent takes.</param>
2026-11-27 16:35:51 +01:00
/// <param name="options">The settings of compressing: the share from which AI Studio asks, and the most tokens of a summary.</param>
/// <returns>Whether compressing may be offered, as far as can be told without a count, or why not; NONE without a known window of the chat, since nothing then asks before sending.</returns>
internal static ChatCompressionOffer OfferBeforeCounting ( ContextWindow chatWindow , ContextWindow ? writerWindow , bool hasSomethingToSummarize , int messageTokens , ChatCompressionOptions options )
2026-11-21 14:16:04 +01:00
{
2026-11-27 16:35:51 +01:00
if (! chatWindow . IsKnown )
2026-11-21 14:16:04 +01:00
return ChatCompressionOffer . NONE ;
2026-11-27 16:35:51 +01:00
if ( writerWindow is not { } window || ! ConversationSummaryBudget . CanSummarizeWith ( window ))
2026-11-21 14:16:04 +01:00
return ChatCompressionOffer . WINDOW_TOO_SMALL ;
2026-11-27 16:35:51 +01:00
var tokensForTheSummary = ConversationSummaryBudget . TokensForTheSummary ( window , chatWindow , options . MaxSummaryTokens );
if ( messageTokens + tokensForTheSummary >= chatWindow . DefaultTokens * options . ShareToAsk )
2026-11-21 14:16:04 +01:00
return ChatCompressionOffer . MESSAGE_TOO_LONG ;
return hasSomethingToSummarize ? ChatCompressionOffer . OFFERED : ChatCompressionOffer . NOTHING_TO_SUMMARIZE ;
}
/// <summary>
/// Whether what compressing keeps still fills the context window so much that AI Studio would ask again.
/// </summary>
/// <remarks>
/// What the chat costs after compressing is what it costs now, less the summarized messages, plus the room for the
/// summary. A large file attached to the last message of the user stays, for example, and so does a long system
/// prompt.
/// </remarks>
2026-11-27 16:35:51 +01:00
/// <param name="windowTokens">The context window of the model of the chat.</param>
2026-11-21 14:16:04 +01:00
/// <param name="tokensWithMessage">What the chat costs together with the message about to be sent.</param>
/// <param name="summarizedTokens">What the messages which the summary replaces cost, with their files.</param>
2026-11-27 16:35:51 +01:00
/// <param name="tokensForTheSummary">The room kept for the summary, see ConversationSummaryBudget.TokensForTheSummary.</param>
/// <param name="shareToAsk">The share of the context window from which AI Studio asks before sending.</param>
2026-11-21 14:16:04 +01:00
/// <returns>True when compressing would not make enough room.</returns>
2026-11-27 16:35:51 +01:00
internal static bool KeepsTooMuch ( int windowTokens , int tokensWithMessage , int summarizedTokens , int tokensForTheSummary , double shareToAsk )
2026-11-21 14:16:04 +01:00
{
2026-11-27 16:35:51 +01:00
var tokensAfterCompressing = tokensWithMessage - summarizedTokens + tokensForTheSummary ;
return tokensAfterCompressing >= windowTokens * shareToAsk ;
2026-11-21 14:16:04 +01:00
}
}