using System.Globalization; using AIStudio.Tools.PluginSystem; using AIStudio.Tools.Web; namespace AIStudio.Chat.Compression; /// /// Makes the chat a compressed chat continues in. /// /// /// The new chat starts as a copy of the compressed one, see WorkspaceBehaviour.CopyChatAsync, so it /// keeps everything which is not a message: the workspace, the profile, the chat template, the /// tools, the data sources, the system prompt, and above all the restrictions, which say what the /// conversation has seen and which providers and tools may still see it. Only the messages are /// replaced, before the copy is stored for the first time. An embedded chat of an assistant has no /// workspace to keep the original in, so its copy replaces it, see ChatCompressionTarget.SAME_CHAT. /// public static class ChatCompression { private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(ChatCompression).Namespace, nameof(ChatCompression)); private static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(ChatCompression).Namespace, nameof(ChatCompression)); /// /// Stands for the name of the chat in the formats of a continued name. /// private const string NAME_PLACEHOLDER = "{0}"; /// /// Stands for the number of the continuation in the formats of a continued name. /// private const string NUMBER_PLACEHOLDER = "{1}"; /// /// Replaces the messages of a copy of the compressed chat with the summary and the last exchange. /// /// /// The plan is made anew on the copy, which is the compressed chat as it was serialized, so the /// copy keeps blocks and attachments of its own and shares none with the original. The new chat /// then holds the example conversation of its chat template, the last message of the user with /// the summary ahead of its text, and the last answer of the AI. The last message keeps its time /// and its files, which the AI thus receives in full again, and the roles keep alternating, see /// ChatCompressionPlan. Answering that message anew in the new chat answers it with the summary /// at hand.

/// /// When the last message of the user was hidden and therefore summarized, the summary becomes a /// message of its own, right before the last answer.

/// /// A message of the user which had no answer becomes the draft of the new chat, so the user finds /// it in the field for the next message, see ChatCompressionPlan. Within the same chat, the user /// still has the message being written in that field, so such a message is summarized instead.

/// /// What described the last request of the compressed chat is reset, as after a rollback: the data /// retrieved for it and the data sources an AI operation picked. The restrictions only tighten, by those /// the summary brings along, see AssistantLaunchService.CreateTextPayloadAsync.

/// /// The last answer keeps what AI Studio noted about its tool calls and sources. The web addresses /// the tools returned for the summarized answers move to it, as far as the summary names them, see /// WebAddressesFromToolsNamedIn. ///
/// The copy of the compressed chat, not stored yet. /// How many blocks the example conversation of the chat template holds, 0 without a template. /// Where the conversation goes on. /// The text of the message which holds the summary, see SummaryMessage. /// What the filtered summary demands, which the new chat demands as well. /// The provider the new chat continues with: the one which wrote the summary. /// The copy holds nothing to compress, which only happens when it is not a copy of a chat which had something. public static void TurnIntoContinuation(ChatThread continuation, int exampleConversationLength, ChatCompressionTarget target, string summaryMessage, ChatRestrictions summaryRestrictions, string providerId) { var draftIsEmpty = target is ChatCompressionTarget.NEW_CHAT && continuation.Draft.IsEmpty; var plan = ChatCompressionPlan.Of(continuation, exampleConversationLength, draftIsEmpty) ?? throw new InvalidOperationException("The chat holds nothing to compress."); ContentBlock summary; if (plan.LastQuestion is { Content: ContentText lastQuestion } lastQuestionBlock) { lastQuestion.Text = WithLastMessage(target, summaryMessage, lastQuestion.Text); summary = lastQuestionBlock; } else { summary = new ContentBlock { Time = plan.LastAnswer.Time.AddTicks(-1), ContentType = ContentType.TEXT, Role = ChatRole.USER, Content = new ContentText { Text = summaryMessage, }, }; } if (plan.LastAnswer.Content is ContentText lastAnswer) { // What the provider reported about the last answer was for the request of the compressed chat, see ChatThread.ReportedHistoryFor: lastAnswer.ReportedUsage = null; lastAnswer.WebAddressesFromTools.UnionWith(WebAddressesFromToolsNamedIn(plan.ToSummarize, summaryMessage)); } continuation.Blocks = [..plan.Preamble, summary, plan.LastAnswer]; if (plan.PendingQuestion is { } pendingQuestion) { continuation.Draft.SetText(pendingQuestion.Text); continuation.Draft.AddAttachments(pendingQuestion.FileAttachments); } continuation.AugmentedData = string.Empty; continuation.AISelectedDataSources = []; continuation.RequireRestrictions(summaryRestrictions); continuation.SelectedProvider = providerId; } /// /// The web addresses the tools returned for the summarized answers which the summary still names, as request keys. /// /// /// Each answer keeps the addresses its tools returned and a later message still shows the model, /// so that the model may read such a page again while its free address choice is off, see /// ContentText.WebAddressesFromTools. The summarized answers are gone from the new chat, and their /// addresses with them. Those the summary names are still shown to the model, so the rule which /// kept them holds for them in the new chat as well. Any other address in the summary stays one a /// model wrote, see ChatThread.WebAddressesWrittenByModels. /// /// The blocks the summary replaces. /// The text of the message which holds the summary, see SummaryMessage. /// The request keys, see WebAddresses.CreateRequestKey. internal static IEnumerable WebAddressesFromToolsNamedIn(IEnumerable summarized, string summaryMessage) { var named = new HashSet(StringComparer.Ordinal); foreach (var address in WebAddresses.Find(summaryMessage)) { if (WebAddresses.TryCreateRequestKey(address, out var requestKey)) named.Add(requestKey); } return summarized .Where(block => block.Role is ChatRole.AI) .Select(block => block.Content) .OfType() .SelectMany(answer => answer.WebAddressesFromTools) .Where(named.Contains) .Distinct(StringComparer.Ordinal); } /// /// The text of the message which holds the summary in the chat the conversation continues in. /// /// /// The user reads it, so it says where the conversation comes from and that the summary only /// describes the files attached there. The AI reads it as well, which does no harm: it learns /// the same. /// /// Where the conversation goes on. /// The name of the compressed chat; only a new chat names it. /// The summary, filtered for prompt injections. /// The text of the message. public static string SummaryMessage(ChatCompressionTarget target, string sourceName, string summary) { if (target is ChatCompressionTarget.SAME_CHAT) { var heading = TB("Summary of the earlier conversation"); var note = TA("The summary takes the place of the earlier messages of this conversation and describes the files attached there. The files of your last message stay attached to it; attach any other file again when the AI needs its full content."); return $"**{heading}**\n\n{note}\n\n---\n\n{summary.Trim()}"; } var chatHeading = string.Format(TB("Summary of the chat '{0}'"), sourceName); var chatNote = TA("This chat continues the conversation of that chat, which stays as it was. The summary describes the files attached there. The files of your last message stay attached to it; attach any other file again when the AI needs its full content."); return $"**{chatHeading}**\n\n{chatNote}\n\n---\n\n{summary.Trim()}"; } /// /// The text of the last message of the user after compressing: the summary, followed by the message as it was. /// /// Where the conversation goes on. /// The text of the message which holds the summary, see SummaryMessage. /// The text of the last message of the user. /// The text of the message. internal static string WithLastMessage(ChatCompressionTarget target, string summaryMessage, string lastMessage) { var heading = target is ChatCompressionTarget.SAME_CHAT ? TA("Your last message") : TA("Your last message in that chat"); return $"{summaryMessage}\n\n---\n\n**{heading}**\n\n{lastMessage}"; } /// /// The name of a chat as the summary and the suggested name of its continuation call it. /// /// The name of the chat, which may be empty. /// The name, or a placeholder for a chat without one. public static string NameOf(string chatName) => string.IsNullOrWhiteSpace(chatName) ? TB("Unnamed chat") : chatName; /// /// The name suggested for the new chat. /// /// The name of the compressed chat, which may be empty. /// The suggested name, e.g., "Plan (continued)", or "Plan (continued 2)" for a chat which continues one already. public static string NameOfContinuation(string sourceName) => ContinuedName(NameOf(sourceName), TB("{0} (continued)"), TB("{0} (continued {1})")); /// /// Names the continuation of a chat, counting up when the chat continues one already. /// /// /// Kept apart from the translations so that a test can ask it with formats of its own. A chat /// compressed several times thus becomes "Plan (continued 3)" rather than "Plan (continued) /// (continued) (continued)". /// /// The name of the compressed chat. /// The format of a first continuation, with {0} for the name. /// The format of a further continuation, with {0} for the name and {1} for the number. /// The name of the continuation. internal static string ContinuedName(string name, string onceFormat, string countedFormat) { if (TryReadName(name, countedFormat, out var countedName, out var number) && number < int.MaxValue) return string.Format(CultureInfo.InvariantCulture, countedFormat, countedName, number + 1); if (TryReadName(name, onceFormat, out var onceName, out _)) return string.Format(CultureInfo.InvariantCulture, countedFormat, onceName, 2); return string.Format(CultureInfo.InvariantCulture, onceFormat, name); } /// /// Reads the name of the compressed chat, and the number where the format has one, back out of a name written with the given format. /// /// /// The format comes from the translation, so it can only be known while the app runs: a regular /// expression could not be generated while compiling, and none is needed. The text around the /// placeholders is compared as it stands, with {0} for a name of at least one character and {1} /// for digits, in whichever order the translation puts them. When the name holds the text between /// the placeholders more than once, the name of the chat takes the most, as in "Plan (continued 2) /// (continued 3)", whose chat is "Plan (continued 2)". /// /// The name to read. /// The format, with {0} for the name of the chat and, optionally, {1} for the number. /// The name of the chat, when the name was written with the format; otherwise empty. /// The number, when the format has one and the name was written with it; otherwise 0. /// True when the name was written with the format. private static bool TryReadName(string name, string format, out string chatName, out int number) { chatName = string.Empty; number = 0; var namePosition = format.IndexOf(NAME_PLACEHOLDER, StringComparison.Ordinal); if (namePosition < 0) return false; // Both placeholders are equally long, so the length of the one steps over the other as well: var numberPosition = format.IndexOf(NUMBER_PLACEHOLDER, StringComparison.Ordinal); var firstPosition = numberPosition < 0 ? namePosition : Math.Min(namePosition, numberPosition); var lastPosition = numberPosition < 0 ? namePosition : Math.Max(namePosition, numberPosition); var prefix = format[..firstPosition]; var suffix = format[(lastPosition + NAME_PLACEHOLDER.Length)..]; if (name.Length < prefix.Length + suffix.Length || !name.StartsWith(prefix, StringComparison.Ordinal) || !name.EndsWith(suffix, StringComparison.Ordinal)) return false; var inner = name[prefix.Length..^suffix.Length]; if (numberPosition < 0) { chatName = inner; return chatName.Length > 0; } var separator = format[(firstPosition + NAME_PLACEHOLDER.Length)..lastPosition]; string digits; if (numberPosition < namePosition) { var digitCount = 0; while (digitCount < inner.Length && char.IsAsciiDigit(inner[digitCount])) digitCount++; var rest = inner[digitCount..]; if (digitCount is 0 || !rest.StartsWith(separator, StringComparison.Ordinal)) return false; digits = inner[..digitCount]; chatName = rest[separator.Length..]; } else { var digitCount = 0; while (digitCount < inner.Length && char.IsAsciiDigit(inner[^(digitCount + 1)])) digitCount++; var rest = inner[..^digitCount]; if (digitCount is 0 || !rest.EndsWith(separator, StringComparison.Ordinal)) return false; digits = inner[^digitCount..]; chatName = rest[..^separator.Length]; } return chatName.Length > 0 && int.TryParse(digits, NumberStyles.None, CultureInfo.InvariantCulture, out number); } }