using AIStudio.Settings.DataModel; namespace AIStudio.Chat.Compression; /// /// Decides what happens with a message which would fill the context window of the model, as the settings say. /// /// /// Pure functions, shared by the chat, the embedded chat of an assistant, and the assistants, so that all of them /// follow the same rules, see DataChatCompression.ContextWindowFullBehavior. /// public static class ContextWindowFullHandling { /// /// Decides whether a chat asks, sends, or compresses before it sends a message which would fill the context window. /// /// /// /// Sending without asking needs a place which may send the message at all: the embedded chat of an assistant /// sends no message which no longer fits, so it asks there. /// /// /// Compressing without asking needs a compressing which helps, see ChatCompressionService.OfferBeforeSendingAsync. /// Otherwise, the chat asks, and the dialog tells why compressing would not help. An assistant compresses /// nothing, so it asks as well. After compressing, the chat decides once more without compressing again, so /// that a summary which leaves the chat too full ends in a question instead of a loop. /// /// /// What the settings say, read once for this decision. /// Whether compressing helps, or why not; NONE where nothing compresses. /// Whether the place may send the message although it fills the window. /// False when the chat was compressed for this message already. /// What the chat does with the message. public static ContextWindowFullAction Decide(ContextWindowFullBehavior behavior, ChatCompressionOffer offer, bool canSendAnyway, bool mayCompressAutomatically) => behavior switch { ContextWindowFullBehavior.SEND_WITHOUT_ASKING when canSendAnyway => ContextWindowFullAction.SEND, ContextWindowFullBehavior.COMPRESS_INTO_NEW_CHAT or ContextWindowFullBehavior.COMPRESS_AND_REPLACE when mayCompressAutomatically && offer is ChatCompressionOffer.OFFERED => ContextWindowFullAction.COMPRESS, _ => ContextWindowFullAction.ASK, }; /// /// Decides where the conversation of the chat page goes on once it is compressed before sending. /// /// /// Without workspaces, the chat is never stored, so there is no new chat to switch to, and the summary takes the /// place of the earlier messages. Otherwise, the settings decide; asking continues in a new chat, as the button /// of the chat does. The embedded chat of an assistant always compresses in place, see EmbeddedChat. /// /// What the settings say, read once for this decision. /// Whether the user switched off the workspaces. /// Where the conversation goes on. public static ChatCompressionTarget TargetFor(ContextWindowFullBehavior behavior, bool workspacesDisabled) => workspacesDisabled || behavior is ContextWindowFullBehavior.COMPRESS_AND_REPLACE ? ChatCompressionTarget.SAME_CHAT : ChatCompressionTarget.NEW_CHAT; }