namespace AIStudio.Chat.Compression;
///
/// What compressing a chat does with each of its blocks.
///
///
/// Compressing never changes the chat itself. The conversation continues in a new chat, which gets
/// the example conversation of the chat template, a summary of everything in between, and the last
/// exchange word for word: the last message of the user, with its files, and the last answer of the
/// AI. The last exchange stays because the next message of the user mostly refers to it. The summary
/// goes into the last message of the user, ahead of its text, see ChatCompression.TurnIntoContinuation:
/// as a message of its own, it would follow the example conversation and precede the last message,
/// so two messages of the user would meet, which the chat templates of some local models refuse.
///
/// A last message of the user which is hidden, such as the prompt an assistant sent into the chat,
/// is summarized instead: the new chat could show it only together with the summary, which the user
/// has to see. The summary then stands as a message of its own, right before the last answer.
///
/// A message of the user which never got an answer, e.g., because the chat had grown too long for
/// the model, is not summarized as if it had been answered. It goes into the field for the next
/// message of the new chat instead, so the user can send it there. When the user already started
/// another message, that one wins, and the unanswered message is summarized with the rest.
///
/// The example conversation of the chat template, which the new chat starts with unchanged.
/// The messages the summary replaces, in the order of the conversation.
/// The last message of the user before the last answer, which the new chat keeps word for word with its files, or null when it is hidden and therefore summarized.
/// The last answer of the AI, which the new chat keeps word for word.
/// The last message of the user when it has no answer and goes into the field for the next message of the new chat, otherwise null.
public sealed record ChatCompressionPlan(IReadOnlyList Preamble, IReadOnlyList ToSummarize, ContentBlock? LastQuestion, ContentBlock LastAnswer, ContentText? PendingQuestion)
{
///
/// The messages the AI reads for the summary in the order of the conversation: those it summarizes, and the last exchange.
///
///
/// Taken from the conversation itself rather than sorted anew: a message and its answer share
/// their time stamp, so sorting by time alone could put the answer first, and the AI would take
/// the question for one which is still waiting for an answer.
///
public IReadOnlyList ToRead { get; private init; } = [];
///
/// Plans how to compress the given chat.
///
///
/// The order of the conversation is the one of the time stamps, as everywhere the chat is shown
/// or rolled back. Only messages which the message builder sends count, that is, those with a
/// text: a block without one never reached the AI, so there is nothing to summarize in it.
///
/// The example conversation of the chat template is recognized as the hidden blocks the chat
/// starts with, at most as many as the template holds. A chat whose template changed since keeps
/// what it actually starts with; hidden blocks beyond that, such as the prompts an assistant
/// sent into the chat, are part of the conversation and get summarized.
///
/// The chat to compress. It stays unchanged.
/// How many blocks the example conversation of the chat template holds, 0 without a template.
/// Whether the user has not started a message of their own in the chat.
/// The plan, or null when the chat holds no finished answer of the AI, or nothing to summarize besides the last exchange.
public static ChatCompressionPlan? Of(ChatThread chat, int exampleConversationLength, bool draftIsEmpty)
{
var blocks = chat.Blocks.OrderBy(block => block.Time).ToList();
var preambleLength = 0;
while (preambleLength < exampleConversationLength && preambleLength < blocks.Count && blocks[preambleLength].HideFromUser)
preambleLength++;
var lastAnswerIndex = blocks.FindLastIndex(IsFinishedAnswer);
if (lastAnswerIndex < preambleLength)
return null;
var beforeTheAnswer = blocks[preambleLength..lastAnswerIndex];
var lastQuestion = beforeTheAnswer.LastOrDefault(block => block.Role is ChatRole.USER && IsSentText(block));
if (lastQuestion is { HideFromUser: true })
lastQuestion = null;
var afterTheAnswer = blocks[(lastAnswerIndex + 1)..];
var pendingBlock = draftIsEmpty
? afterTheAnswer.LastOrDefault(block => block is { Role: ChatRole.USER, HideFromUser: false } && IsSentText(block))
: null;
var toSummarize = beforeTheAnswer
.Where(block => !ReferenceEquals(block, lastQuestion))
.Concat(afterTheAnswer.Where(block => !ReferenceEquals(block, pendingBlock)))
.Where(IsSentText)
.ToList();
if (toSummarize.Count is 0)
return null;
var toRead = blocks[preambleLength..]
.Where(block => !ReferenceEquals(block, pendingBlock))
.Where(IsSentText)
.ToList();
return new(blocks[..preambleLength], toSummarize, lastQuestion, blocks[lastAnswerIndex], pendingBlock?.Content as ContentText)
{
ToRead = toRead,
};
}
///
/// Whether the chat holds an answer of the AI which compressing could keep.
///
///
/// Cheap enough to be asked with every rendering of the chat, unlike planning, which sorts the
/// whole conversation.
///
/// The chat.
/// True when there is a finished answer of the AI.
public static bool HasFinishedAnswer(ChatThread chat) => chat.Blocks.Any(IsFinishedAnswer);
///
/// Whether a block becomes a message when the request is built, see ListContentBlockExtensions.BuildMessagesAsync.
///
internal static bool IsSentText(ContentBlock block) => block is { ContentType: ContentType.TEXT, Content: ContentText { Text: var text } } && !string.IsNullOrWhiteSpace(text);
///
/// Whether a block is an answer of the AI which the new chat could keep and show.
///
///
/// Hidden answers do not count, such as the example answers of a chat template: the user could
/// not see what the new chat continues from.
///
private static bool IsFinishedAnswer(ContentBlock block) => block is { Role: ChatRole.AI, HideFromUser: false, Content: ContentText { IsStreaming: false } } && IsSentText(block);
}