Files
AI-Studio/app/MindWork AI Studio/Chat/Compression/ChatCompressionService.cs
T

484 lines
27 KiB
C#
Raw Normal View History

2026-11-21 14:16:04 +01:00
using AIStudio.Models;
using AIStudio.Provider;
using AIStudio.Settings;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.Security;
using AIStudio.Tools.Services;
using AIStudio.Tools.ToolCallingSystem;
namespace AIStudio.Chat.Compression;
/// <summary>
/// Asks the AI to summarize a chat, so that the conversation can continue in a new chat which costs far fewer tokens.
/// </summary>
/// <remarks>
/// The summary is written by the provider the chat would send its next message to. It reads
/// everything the chat holds, so it has to be a provider the chat allows, and its requests carry
/// the restrictions of the chat, see ChatThread.RequireRestrictionsOf.<br/><br/>
///
/// What comes back is what the AI wrote. Before it reaches a model again, as a message of the new
/// chat, it has to pass the prompt injection filter, see AssistantLaunchService.CreateTextPayloadAsync.
/// </remarks>
/// <param name="tokenCounter">Counts the tokens of the messages, with the tokenizer of the provider.</param>
/// <param name="promptInjectionGuard">Reports what the filter took out of the attached documents, once for the whole compression.</param>
/// <param name="logger">The logger.</param>
public sealed class ChatCompressionService(ConversationTokenCounter tokenCounter, PromptInjectionGuardService promptInjectionGuard, ILogger<ChatCompressionService> logger)
{
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(ChatCompressionService).Namespace, nameof(ChatCompressionService));
private static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(ChatCompressionService).Namespace, nameof(ChatCompressionService));
/// <summary>
/// The fewest tokens of messages a part has to hold for a summary to make sense.
/// </summary>
private const int MIN_TOKENS_PER_PART = 1_024;
/// <summary>
/// The least room kept free for the summary the AI writes.
/// </summary>
private const int MIN_TOKENS_FOR_THE_SUMMARY = 1_024;
/// <summary>
/// The most room kept free for the summary the AI writes.
/// </summary>
/// <remarks>
/// Context windows of 250,000 tokens and more are common by now. A summary of 8,192 tokens takes
/// a small share of them, yet keeps room for the details of a long conversation. A summary meant
/// to save tokens has no business being longer anyway.
/// </remarks>
private const int MAX_TOKENS_FOR_THE_SUMMARY = 8_192;
/// <summary>
/// The room kept free for the words of a request around the messages, and for the rounding of estimates.
/// </summary>
private const int TOKENS_FOR_THE_REQUEST_FRAME = 512;
/// <summary>
/// The smallest context window a model needs for compressing a chat.
/// </summary>
/// <remarks>
/// The system prompt, the room for the summary, and the summary so far take about 4,600 tokens
/// before a single message fits. Close to that, every part holds only a few messages, and the
/// summary is written anew for each of them. From 8,192 tokens on, a part holds a few
/// thousand tokens of the conversation.
/// </remarks>
public const int MIN_CONTEXT_WINDOW = 8_192;
/// <summary>
/// Whether a model with the given context window can compress a chat.
/// </summary>
/// <remarks>
/// An unknown window does not stop it: the whole conversation then goes into one request, and
/// the provider tells when it is too long, see SummarizeAsync.
/// </remarks>
/// <param name="window">The context window of the model which writes the summary.</param>
/// <returns>True when the window is unknown or at least MIN_CONTEXT_WINDOW.</returns>
public static bool CanSummarizeWith(ContextWindow window) => !window.IsKnown || window.DefaultTokens >= MIN_CONTEXT_WINDOW;
/// <summary>
/// How much longer than the room kept for it a summary may grow before the AI shortens it, see IsTooLong.
/// </summary>
/// <remarks>
/// Models cannot count what they write. The request already asks for half as many words as the
/// room holds tokens, which leaves a margin of its own; beyond half as much again, the summary
/// takes room the next part or the new chat needs.
/// </remarks>
internal const double SUMMARY_TOLERANCE = 1.5;
/// <summary>
/// How many tokens the summary should take in a model with the given context window.
/// </summary>
/// <remarks>
/// A quarter of the window, at least MIN_TOKENS_FOR_THE_SUMMARY and at most MAX_TOKENS_FOR_THE_SUMMARY.
/// It is a target, not a limit: the AI is asked for a summary of this length, and one which grows
/// far beyond it is shortened once, see SummarizeAsync.
/// </remarks>
/// <param name="windowTokens">The context window of the model which writes the summary.</param>
/// <returns>The tokens kept free for the summary.</returns>
public static int TokensForTheSummary(int windowTokens) => Math.Clamp(windowTokens / 4, MIN_TOKENS_FOR_THE_SUMMARY, MAX_TOKENS_FOR_THE_SUMMARY);
/// <summary>
/// Whether a summary grew so long beyond the room kept for it that the AI has to shorten it.
/// </summary>
/// <param name="summaryTokens">The tokens the summary takes.</param>
/// <param name="tokensForTheSummary">The room kept for the summary, or 0 when the window of the model is unknown.</param>
/// <returns>True when the summary takes more than SUMMARY_TOLERANCE times the room kept for it; never without a room.</returns>
internal static bool IsTooLong(int summaryTokens, int tokensForTheSummary) => tokensForTheSummary > 0 && summaryTokens > tokensForTheSummary * SUMMARY_TOLERANCE;
/// <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,
/// see ConversationTokens.SHARE_TO_ASK_BEFORE_SENDING. The checks which need no count come first, see
/// 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/>
///
/// 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>
/// <param name="provider">The provider which would write the summary, with the tokenizer to count with.</param>
/// <param name="tokensWithMessage">What the chat costs together with the message about to be sent, with the context window of the model.</param>
/// <param name="token">Ends the count when nobody needs the answer anymore.</param>
/// <returns>Whether compressing is offered, or why not.</returns>
public async Task<ChatCompressionOffer> OfferBeforeSendingAsync(ChatThread chat, int exampleConversationLength, AIStudio.Settings.Provider provider, ConversationTokens tokensWithMessage, CancellationToken token = default)
{
// 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);
var offer = OfferBeforeCounting(tokensWithMessage.Window, plan is not null, tokensWithMessage.DraftTokens);
if (offer is not ChatCompressionOffer.OFFERED || plan is null)
return offer;
var summarizedParts = ConversationParts.Of(new ChatThread { Blocks = [.. plan.ToSummarize] }, string.Empty, string.Empty, null, false, null);
var summarized = await tokenCounter.CountAsync(provider, summarizedParts, ReportedHistory.UNKNOWN, token);
if (!summarized.IsKnown)
return ChatCompressionOffer.OFFERED;
return KeepsTooMuch(tokensWithMessage.Window.DefaultTokens, tokensWithMessage.Tokens, summarized.HistoryTokens)
? ChatCompressionOffer.KEEPS_TOO_MUCH
: ChatCompressionOffer.OFFERED;
}
/// <summary>
/// Decides what speaks against compressing before anything is counted.
/// </summary>
/// <remarks>
/// The model has to be large enough for a summary, see CanSummarizeWith. 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.
/// </remarks>
/// <param name="window">The context window of the model which would write the summary.</param>
/// <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>
/// <returns>Whether compressing may be offered, as far as can be told without a count, or why not; NONE without a known window, since nothing then asks before sending.</returns>
internal static ChatCompressionOffer OfferBeforeCounting(ContextWindow window, bool hasSomethingToSummarize, int messageTokens)
{
if (!window.IsKnown)
return ChatCompressionOffer.NONE;
if (!CanSummarizeWith(window))
return ChatCompressionOffer.WINDOW_TOO_SMALL;
if (messageTokens + TokensForTheSummary(window.DefaultTokens) >= window.DefaultTokens * ConversationTokens.SHARE_TO_ASK_BEFORE_SENDING)
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>
/// <param name="windowTokens">The context window of the model.</param>
/// <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>
/// <returns>True when compressing would not make enough room.</returns>
internal static bool KeepsTooMuch(int windowTokens, int tokensWithMessage, int summarizedTokens)
{
var tokensAfterCompressing = tokensWithMessage - summarizedTokens + TokensForTheSummary(windowTokens);
return tokensAfterCompressing >= windowTokens * ConversationTokens.SHARE_TO_ASK_BEFORE_SENDING;
}
/// <summary>
/// Asks the AI for a summary of the messages the plan names.
/// </summary>
/// <remarks>
/// The summary takes in the summarized messages as the AI received them in the chat, with the
/// text of their documents and transcripts, see ContentText.PrepareTextContentForAI. A document
/// stored at its first send comes from the provenance store, so the summary reads what the AI
/// read, even when its file changed or is gone by now. The last exchange goes along as well,
/// marked, so that the summary leads up to it, see ChatCompressionInput.Render.<br/><br/>
///
/// Images do not go along. What matters of an image for the conversation shows in what the
/// conversation said about it, the answers of the AI above all, and the summary keeps that.
/// Describing every image anew would cost a request of its own per image, which adds up to
/// hundreds in a chat full of screenshots.<br/><br/>
///
/// A conversation which does not fit into one request is summarized in parts, one after the
/// other, see ChatCompressionInput.TakePart. Each part gets what the context window of the model
/// leaves after the summary so far, which is counted anew after every answer: a model may write
/// more than it was asked to, and some languages take several tokens per word. When the window
/// is unknown, the whole conversation goes into one request, and the provider tells when it is
/// too long.<br/><br/>
///
/// A summary which grew beyond SUMMARY_TOLERANCE times its room is shortened by the AI in one
/// request of its own, after a part as well as at the end, see IsTooLong. What comes back is taken
/// as it is: a second round would cost another request and could not promise more, and the notice
/// before sending tells the user when the new chat is still too long. A summary which the provider
/// cut off, e.g., because it reached the most tokens the model writes at once, cannot be told
/// apart from a complete one, since the providers do not report why an answer ended.
/// </remarks>
/// <param name="sourceChat">The chat to summarize. It stays unchanged.</param>
/// <param name="plan">Which messages to summarize, see ChatCompressionPlan.Of.</param>
/// <param name="provider">The provider which writes the summary.</param>
/// <param name="focus">What the user wants the summary to pay particular attention to, or an empty string.</param>
/// <param name="progress">Learns which part is being summarized, or null.</param>
/// <param name="token">Ends the summarizing.</param>
/// <returns>The summary, or why there is none.</returns>
public async Task<ChatCompressionResult> SummarizeAsync(ChatThread sourceChat, ChatCompressionPlan plan, AIStudio.Settings.Provider provider, string focus, IProgress<ChatCompressionProgress>? progress, CancellationToken token)
{
if (provider == AIStudio.Settings.Provider.NONE)
return ChatCompressionResult.Failed(TA("Please select a provider first."));
if (!sourceChat.IsLLMProviderAllowed(provider))
return ChatCompressionResult.Failed(TA("The selected provider is not allowed in this chat due to data security or confidence-level requirements. Please select a different provider."));
var window = provider.GetModelProfile().Context;
if (!CanSummarizeWith(window))
return ChatCompressionResult.Failed(TA("The context window of the selected model is too small to summarize this chat. Please select a model which takes longer texts."));
try
{
progress?.Report(new(ChatCompressionStep.READING_FILES, 0, 0));
var texts = await this.ReadMessagesAsync(plan.ToSummarize, token);
//
// The last exchange goes along at its place in the conversation, marked, so that the
// summary knows what follows it. Messages after it, such as an unanswered question, keep
// coming after it, so the AI does not take the answer for a reply to them, see
// ChatCompressionPlan.ToRead:
//
var kept = new Dictionary<ContentBlock, KeptMessage>(ReferenceEqualityComparer.Instance) { [plan.LastAnswer] = KeptMessage.LAST_ANSWER };
if (plan.LastQuestion is not null)
kept[plan.LastQuestion] = KeptMessage.LAST_QUESTION;
var messages = new List<CountedMessage>(plan.ToRead.Count);
var number = 0;
foreach (var block in plan.ToRead)
{
var keptMessage = kept.GetValueOrDefault(block, KeptMessage.NONE);
var text = keptMessage is KeptMessage.NONE ? texts[block] : (block.Content as ContentText)?.Text ?? string.Empty;
messages.Add(await this.CountAsync(provider, ChatCompressionInput.Render(block, ++number, text, keptMessage), token));
}
var tokensForTheSummary = window.IsKnown ? TokensForTheSummary(window.DefaultTokens) : 0;
var tokensForMessagesAndSummary = window.IsKnown ? await this.TokensForMessagesAndSummaryAsync(provider, window.DefaultTokens, tokensForTheSummary, focus, token) : int.MaxValue;
// There has to be room for a part next to a summary so far which takes all the room kept for it, which a long focus may take away:
if (window.IsKnown && tokensForMessagesAndSummary - tokensForTheSummary < MIN_TOKENS_PER_PART)
return ChatCompressionResult.Failed(TA("The context window of the selected model is too small to summarize this chat. Please select a model which takes longer texts."));
// A word takes more than one token in most languages, so half of the tokens is a safe number of words:
var maxWords = tokensForTheSummary / 2;
// Only for showing the progress: every part after the first one shares its room with the summary so far:
var expectedParts = window.IsKnown ? ChatCompressionInput.SplitIntoParts(messages, tokensForMessagesAndSummary - tokensForTheSummary).Count : 1;
var summary = string.Empty;
var summaryTokens = 0;
var position = ChatCompressionPosition.START;
var part = 0;
while (position.Message < messages.Count)
{
token.ThrowIfCancellationRequested();
var tokensPerPart = window.IsKnown ? tokensForMessagesAndSummary - summaryTokens : int.MaxValue;
if (tokensPerPart < MIN_TOKENS_PER_PART)
return ChatCompressionResult.Failed(TA("The summary grew too long for the context window of the selected model. Please select a model which takes longer texts."));
var conversation = ChatCompressionInput.TakePart(messages, position, tokensPerPart);
var isLastPart = conversation.Next.Message >= messages.Count;
part++;
progress?.Report(new(ChatCompressionStep.SUMMARIZING, part, isLastPart ? part : Math.Max(part + 1, expectedParts)));
var request = ChatCompressionPrompts.BuildRequest(conversation.Text, part, isLastPart, summary, focus, maxWords);
summary = await RequestSummaryAsync(sourceChat, provider, request, token);
token.ThrowIfCancellationRequested();
if (string.IsNullOrWhiteSpace(summary))
return ChatCompressionResult.Failed(TA("The AI did not write a summary. Please check the messages about the provider, and try again."));
if (window.IsKnown)
{
summaryTokens = (await this.CountAsync(provider, summary, token)).Tokens;
if (IsTooLong(summaryTokens, tokensForTheSummary))
{
progress?.Report(new(ChatCompressionStep.CONDENSING, part, isLastPart ? part : Math.Max(part + 1, expectedParts)));
var shortened = await RequestSummaryAsync(sourceChat, provider, ChatCompressionPrompts.BuildShorteningRequest(summary, maxWords), token);
token.ThrowIfCancellationRequested();
// An empty answer shortens nothing, so the longer summary is still better than none:
if (!string.IsNullOrWhiteSpace(shortened))
{
summary = shortened;
summaryTokens = (await this.CountAsync(provider, summary, token)).Tokens;
}
}
}
position = conversation.Next;
}
return ChatCompressionResult.Succeeded(summary);
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
return ChatCompressionResult.Failed(TB("Compressing the chat was canceled."));
}
catch (Exception exception)
{
logger.LogError(exception, "Failed to summarize the chat '{ChatId}'.", sourceChat.ChatId);
return ChatCompressionResult.Failed(string.Format(TB("The chat could not be summarized: {0}"), exception.Message));
}
}
/// <summary>
/// How many tokens one request may hold of messages and of the summary so far together.
/// </summary>
/// <remarks>
/// The window of the model holds the system prompt, the words around the messages, what the user
/// wants the summary to pay attention to, the summary so far, and the messages, and it has to
/// leave room for the summary the AI writes.
/// </remarks>
/// <param name="provider">The provider which writes the summary.</param>
/// <param name="windowTokens">The context window of its model.</param>
/// <param name="tokensForTheSummary">The room kept for the summary the AI writes.</param>
/// <param name="focus">What the user wants the summary to pay particular attention to, or an empty string.</param>
/// <param name="token">Ends the counting.</param>
/// <returns>The tokens left for messages and the summary so far, which may be less than nothing for a very small window.</returns>
private async Task<int> TokensForMessagesAndSummaryAsync(AIStudio.Settings.Provider provider, int windowTokens, int tokensForTheSummary, string focus, CancellationToken token)
{
var systemPrompt = (await this.CountAsync(provider, ChatCompressionPrompts.SYSTEM_PROMPT, token)).Tokens;
var focusTokens = string.IsNullOrWhiteSpace(focus) ? 0 : (await this.CountAsync(provider, focus, token)).Tokens;
return windowTokens - systemPrompt - focusTokens - TOKENS_FOR_THE_REQUEST_FRAME - tokensForTheSummary;
}
/// <summary>
/// Counts the tokens of a text with the tokenizer of the provider, or estimates them when it does not answer.
/// </summary>
/// <remarks>
/// The estimate rather errs on the large side, three characters per token, so that a part does
/// not grow beyond the window because a tokenizer was missing.
/// </remarks>
private async Task<CountedMessage> CountAsync(AIStudio.Settings.Provider provider, string text, CancellationToken token)
{
try
{
return new(text, await tokenCounter.CountTextAsync(provider, text, token));
}
catch (OperationCanceledException) when (token.IsCancellationRequested)
{
throw;
}
catch (Exception exception)
{
logger.LogWarning(exception, "Failed to count the tokens of a message to summarize. Estimating them instead.");
return new(text, EstimateTokens(text));
}
}
internal static int EstimateTokens(string text) => (text.Length + 2) / 3;
/// <summary>
/// Reads the messages to summarize as the AI received them in the chat, with the text of their documents and transcripts.
/// </summary>
/// <remarks>
/// The images stay out, see SummarizeAsync; the rendered message names them. Reading works on a
/// copy of each message, so the chat stays untouched. What the filter takes out of the
/// documents is reported once for all of them, as for a request of the chat, see
/// ListContentBlockExtensions.BuildMessagesAsync.<br/><br/>
///
/// An earlier answer of the AI starts with the note of AI Studio about the tools it called and
/// the sources it used, as in every request of the chat, see ToolCallHistoryNote.TextForModel.
/// The results of those calls are not stored, so the note is all the summary can learn about
/// which pages the AI read and which searches it ran.
/// </remarks>
/// <param name="blocks">The messages to summarize.</param>
/// <param name="token">Ends the reading between two messages.</param>
/// <returns>The texts by their blocks.</returns>
private async Task<IReadOnlyDictionary<ContentBlock, string>> ReadMessagesAsync(IReadOnlyList<ContentBlock> blocks, CancellationToken token)
{
var texts = new Dictionary<ContentBlock, string>(ReferenceEqualityComparer.Instance);
await using var reportingScope = blocks.Any(block => block.Content is ContentText text && text.FileAttachments.MayContainDocuments())
? promptInjectionGuard.BeginAction()
: null;
foreach (var block in blocks)
{
token.ThrowIfCancellationRequested();
if (block.Content is not ContentText text)
{
texts[block] = string.Empty;
continue;
}
var withoutImages = new ContentText
{
Text = text.Text,
FileAttachments = [..text.FileAttachments.Where(attachment => !attachment.IsImage)],
};
texts[block] = ToolCallHistoryNote.TextForModel(block, await withoutImages.PrepareTextContentForAI(reportingScope));
}
return texts;
}
/// <summary>
/// Sends one request for a summary.
/// </summary>
/// <remarks>
/// The request is a thread of its own, which nobody sees and nothing stores. It runs without
/// tools and data sources, and it demands of the provider what the chat demands.
/// </remarks>
private static async Task<string> RequestSummaryAsync(ChatThread sourceChat, AIStudio.Settings.Provider provider, string request, CancellationToken token)
{
var time = DateTimeOffset.UtcNow;
var userPrompt = new ContentText
{
Text = request,
};
var answer = new ContentText
{
InitialRemoteWait = true,
};
var thread = new ChatThread
{
WorkspaceId = Guid.Empty,
ChatId = Guid.NewGuid(),
Name = sourceChat.Name,
SystemPrompt = ChatCompressionPrompts.SYSTEM_PROMPT,
SelectedProvider = provider.Id,
// Compressing belongs to the chat, so its requests name the chat:
RuntimeComponent = AIStudio.Tools.Components.CHAT,
Blocks =
[
new()
{
Time = time,
ContentType = ContentType.TEXT,
Role = ChatRole.USER,
Content = userPrompt,
HideFromUser = true,
},
new()
{
Time = time,
ContentType = ContentType.TEXT,
Role = ChatRole.AI,
Content = answer,
HideFromUser = true,
},
],
};
// The request carries everything the chat holds, so it demands what the chat demands:
thread.RequireRestrictionsOf(sourceChat);
await answer.CreateFromProviderAsync(provider.CreateProvider(), provider.Model, userPrompt, thread, token);
return answer.Text.RemoveThinkTags().Trim();
}
}