using AIStudio.Settings.DataModel;
namespace AIStudio.Tools.PluginSystem.Assistants;
///
/// Decides which sections the assistants page shows and in which order.
///
///
/// The built-in assistants and the assistant plugins share one set of categories, see
/// AssistantCategoryKey. The page shows the sections of its built-in assistants first, in
/// the order users know: "General", "Business", "Learning & Reflecting", and "Software Development".
/// The categories which only plugins name follow, the ones of AI Studio before the custom ones, each
/// group sorted by its name. "AI Studio Development" comes last, since it concerns the development of
/// AI Studio itself.
///
public static class AssistantPageSections
{
///
/// The groups of sections, in the order of the page.
///
private enum PageGroup
{
LEADING,
OTHER,
CUSTOM,
TRAILING,
}
///
/// A plugin in one of its categories.
///
/// One category of the plugin.
/// The plugin.
/// The type of the plugin.
private sealed record SectionMember(AssistantCategoryKey Category, TPlugin Plugin);
///
/// The categories of the built-in assistants, which lead the page, in their order.
///
public static readonly IReadOnlyList LEADING_CATEGORIES =
[
new(PluginCategory.COMMON),
new(PluginCategory.BUSINESS),
new(PluginCategory.EDUCATION),
new(PluginCategory.SOFTWARE_DEVELOPMENT),
];
///
/// The category of the built-in assistants which ends the page.
///
public static readonly AssistantCategoryKey TRAILING_CATEGORY = new(PluginCategory.AI_STUDIO_DEVELOPMENT);
///
/// Whether the page shows a section of this category for its built-in assistants anyway.
///
/// The category in question.
/// True for the categories of the built-in assistants.
public static bool IsBuiltIn(AssistantCategoryKey category) => LEADING_CATEGORIES.Contains(category) || category == TRAILING_CATEGORY;
///
/// Compares two categories by their place on the assistants page.
///
/// The first category.
/// The second category.
/// Less than zero when the first category comes first, zero when both are the same, otherwise more than zero.
public static int Compare(AssistantCategoryKey left, AssistantCategoryKey right)
{
var byGroup = Group(left).CompareTo(Group(right));
if (byGroup != 0)
return byGroup;
var leftIndex = IndexOfLeading(left);
if (leftIndex >= 0)
return leftIndex.CompareTo(IndexOfLeading(right));
return NameComparer.Compare(left.Name, right.Name);
}
///
/// Selects the categories the settings dialog of the assistants page offers, in the order of the page.
///
///
/// The dialog offers the categories the page shows right now, so that every switch changes something
/// the user sees, and every category the user hid, so that each one can come back. Categories your
/// organization disabled are missing: no switch of the user brings them back.
///
/// The categories which show at least one assistant right now.
/// The app settings which hold the categories your organization disabled.
/// The settings of the assistants page, which hold the categories the user hid.
/// The categories for the dialog, each once.
public static IReadOnlyList SelectForSettings(IEnumerable shownCategories, DataApp app, DataAssistantsPage page)
{
var hiddenCategories = page.HiddenCategories
.Select(category => new AssistantCategoryKey(category))
.Concat(page.HiddenCustomCategories.Select(name => new AssistantCategoryKey(PluginCategory.CUSTOM, name)));
return shownCategories
.Concat(hiddenCategories)
.Distinct()
.Where(category => !app.IsAssistantCategoryDisabled(category))
.Order(Comparer.Create(Compare))
.ToList();
}
///
/// Groups the assistant plugins into the sections of the assistants page.
///
///
/// A plugin appears in each of its categories, but only once per section. The sections come in the
/// order of the page, and their plugins sorted by title.
///
/// The assistant plugins to show.
/// Gets the title under which a plugin appears.
/// The type of the plugins.
/// One section per category any of the plugins names.
public static IReadOnlyList> Build(IEnumerable plugins, Func titleOf) where TPlugin : IPluginMetadata
{
var titleComparer = NameComparer;
return plugins
.SelectMany(plugin => AssistantCategoryKey.AllOf(plugin).Select(category => new SectionMember(category, plugin)))
.GroupBy(member => member.Category)
.Order(Comparer>>.Create((left, right) => Compare(left.Key, right.Key)))
.Select(group => new AssistantPageSection(group.Key, group.Select(member => member.Plugin).OrderBy(titleOf, titleComparer).ToList()))
.ToList();
}
/// The sections of the assistants page.
/// The type of the plugins.
extension(IReadOnlyList> sections)
{
///
/// Gets the assistant plugins of one category.
///
/// The category in question.
/// The plugins of the category, or none when no plugin names it.
public IReadOnlyList PluginsOf(AssistantCategoryKey category) => sections.FirstOrDefault(section => section.Category == category)?.Assistants ?? [];
}
///
/// Compares names the way the user reads them: in the culture of the active language, without regard to case.
///
private static StringComparer NameComparer => StringComparer.Create(I18N.I.Culture, ignoreCase: true);
private static int IndexOfLeading(AssistantCategoryKey category)
{
for (var index = 0; index < LEADING_CATEGORIES.Count; index++)
if (LEADING_CATEGORIES[index] == category)
return index;
return -1;
}
private static PageGroup Group(AssistantCategoryKey category)
{
if (IndexOfLeading(category) >= 0)
return PageGroup.LEADING;
if (category == TRAILING_CATEGORY)
return PageGroup.TRAILING;
return category.Category is PluginCategory.CUSTOM ? PageGroup.CUSTOM : PageGroup.OTHER;
}
}