Added context compression (#1198)

Co-authored-by: Thorsten Sommer <SommerEngineering@users.noreply.github.com>
This commit is contained in:
j-erlerandThorsten Sommer authored and GitHub committed 2026-11-21 14:16:04 +01:00
1 parent 28b88580a9
commit 8a4a26503b
54 files changed
+4582 -56

No files matched your search

@@ -0,0 +1,22 @@
namespace AIStudio.Chat.Embedded;
/// <summary>
/// What an embedded chat tells the user before it sends a message, see EmbeddedChatTokenBudget.NoticeBeforeSending.
/// </summary>
public enum EmbeddedChatSendNotice
{
/// <summary>
/// Nothing: the message fits, or nobody knows the context window of the model.
/// </summary>
NONE,
/// <summary>
/// The message nearly fills the context window; the user may send it anyway.
/// </summary>
NEARLY_FULL,
/// <summary>
/// With the message, the conversation leaves no room for the next answer; the chat does not send it.
/// </summary>
FULL,
}
@@ -249,6 +249,38 @@ public sealed class EmbeddedChatService(SettingsManager settingsManager, Assista
return true;
}
/// <summary>
/// Replaces the conversation of a chat, e.g., with its compressed form, see ChatCompression.TurnIntoContinuation.
/// </summary>
/// <remarks>
/// The new conversation keeps the ID of the chat. Unlike ResetAsync, it also keeps what the chat knows about the
/// files sent in it, see ChatProvenance: the messages which stay may refer to them. It is stored before the chat
/// shows it, so that the chat never shows a conversation which is not on the disk.
/// </remarks>
/// <param name="chatId">The ID of a chat which GetAsync loaded before.</param>
/// <param name="thread">The new conversation; it gets the ID of the chat.</param>
/// <returns>False while the model answers in the chat, or when it was not loaded.</returns>
/// <exception cref="IOException">The new conversation could not be stored; the chat keeps the old one.</exception>
/// <exception cref="UnauthorizedAccessException">The new conversation could not be stored; the chat keeps the old one.</exception>
public async Task<bool> ReplaceAsync(Guid chatId, ChatThread thread)
{
if (!this.conversations.TryGetValue(chatId, out var conversation))
return false;
lock (this.runningLock)
{
if (conversation.IsRunning)
return false;
}
thread.ChatId = chatId;
await conversation.Store.SaveThreadAsync(thread);
conversation.Thread = thread;
await this.NotifyAsync(chatId);
return true;
}
/// <summary>
/// Stops the answer of a chat and forgets the chat, e.g., because the user deleted what it belonged to.
/// </summary>
@@ -4,9 +4,10 @@ namespace AIStudio.Chat.Embedded;
/// Decides how much room an embedded chat has left in the context of its model, see EmbeddedChat.ShowsContextWindow.
/// </summary>
/// <remarks>
/// Meant for assistants whose requests carry long instructions, such as the guide of the BeyondFacts Lab: their
/// conversations fill the context faster than an ordinary chat, and a model which loses the beginning of its context
/// loses its instructions first. Therefore, the chat stops before.
/// Assistants whose requests carry long instructions, such as the guide of the BeyondFacts Lab, fill the context faster
/// than an ordinary chat, and a model which loses the beginning of its context loses its instructions first. Therefore,
/// the chat stops before. It does so as well where it hides the context window, e.g., in the Model Comparison: there,
/// the provider would refuse the request or cut the conversation instead.
/// </remarks>
public static class EmbeddedChatTokenBudget
{
@@ -36,4 +37,41 @@ public static class EmbeddedChatTokenBudget
return tokens.Tokens >= window * WARNING_SHARE ? EmbeddedChatTokenBudgetState.WARNING : EmbeddedChatTokenBudgetState.OK;
}
/// <summary>
/// Decides how much of the room left a chat shows.
/// </summary>
/// <remarks>
/// A chat which hides the context window, see EmbeddedChat.ShowsContextWindow, shows only that the conversation is
/// full: a warning before would tell apart models the user must not know, while a conversation which does not fit
/// anymore has to stop anyway, with a word why.
/// </remarks>
/// <param name="tokens">What the conversation costs, with the system prompt, and the context of the model.</param>
/// <param name="showsContextWindow">Whether the chat shows the context window.</param>
/// <returns>The state of the budget the chat shows.</returns>
public static EmbeddedChatTokenBudgetState Shown(ConversationTokens tokens, bool showsContextWindow)
{
var state = Of(tokens);
return showsContextWindow || state is EmbeddedChatTokenBudgetState.FULL ? state : EmbeddedChatTokenBudgetState.UNKNOWN;
}
/// <summary>
/// Decides what the chat tells the user before it sends a message.
/// </summary>
/// <remarks>
/// The chat refuses a message after which the conversation would be full, see RESERVE_TOKENS; before that, it asks
/// from the share on from which the chat page asks as well, see ConversationTokens.SHARE_TO_ASK_BEFORE_SENDING. In
/// a small window, the reserve comes first; in a large one, the share does. A chat which hides the context window
/// does not ask, since its question would name the window; it only refuses, see Shown.
/// </remarks>
/// <param name="tokensWithMessage">What the conversation costs together with the message, and the context of the model.</param>
/// <param name="showsContextWindow">Whether the chat shows the context window.</param>
/// <returns>What to tell the user.</returns>
public static EmbeddedChatSendNotice NoticeBeforeSending(ConversationTokens tokensWithMessage, bool showsContextWindow)
{
if (Of(tokensWithMessage) is EmbeddedChatTokenBudgetState.FULL)
return EmbeddedChatSendNotice.FULL;
return showsContextWindow && tokensWithMessage.AsksBeforeSending ? EmbeddedChatSendNotice.NEARLY_FULL : EmbeddedChatSendNotice.NONE;
}
}