Improved the assistants page with categories for assistant plugins (#1211)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-20 11:47:59 +01:00
1 parent ef059ffd15
commit 1db0f85898
74 files changed
+3392 -380

No files matched your search

@@ -1,5 +1,6 @@
@attribute [Route(Routes.ASSISTANT_META_ASSISTANT)]
@using AIStudio.Agents.AssistantAudit
@using AIStudio.Tools.PluginSystem
@using AIStudio.Tools.PluginSystem.Assistants.DataModel
@inherits AssistantBaseCore<AIStudio.Dialogs.Settings.SettingsDialogAssistantBuilder>
@@ -50,7 +51,7 @@
{
<MudTextField T="string" @bind-Text="@this.assistantName" AdornmentIcon="@AppIcons.NAME" Adornment="Adornment.Start" IconSize="Size.Small" Label="@T("Display Name (Optional)")" Placeholder="@T("Meeting Task Extractor")" Variant="Variant.Outlined" Margin="Margin.Dense" Class="mb-3" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
}
<EnumSelection T="AssistantCategory" NameFunc="@(category => category.NameSelecting)" @bind-Value="@this.selectedCategory" ValidateSelection="@this.ValidatingCategory" Icon="@AppIcons.CATEGORY" IconSize="Size.Small" Label="@T("Category (Optional)")" AllowOther="@true" OtherValue="AssistantCategory.OTHER" @bind-OtherInput="@this.customCategory" ValidateOther="@this.ValidateCustomCategory" LabelOther="@T("Custom assistant category")" />
<EnumSelection T="PluginCategory" Values="@this.OfferedCategories" NameFunc="@this.NameOfCategory" @bind-Value="@this.selectedCategory" Icon="@AppIcons.CATEGORY" IconSize="Size.Small" Label="@T("Category (Optional)")" AllowOther="@true" OtherValue="PluginCategory.CUSTOM" @bind-OtherInput="@this.customCategory" ValidateOther="@this.ValidateCustomCategory" LabelOther="@T("Custom assistant category")" />
@if (!this.createChatLauncher)
{
<MudTextField T="string" @bind-Text="@this.typicalInput" AdornmentIcon="@AppIcons.INPUT" Adornment="Adornment.Start" IconSize="Size.Small" Label="@T("Typical input (Optional)")" Placeholder="@T("What users provide, e.g. text, notes, files, or a URL")" Variant="Variant.Outlined" Margin="Margin.Dense" Lines="3" Sizing="InputSizing.Auto" MaxLines="8" Class="mb-3" UserAttributes="@USER_INPUT_ATTRIBUTES"/>
@@ -97,7 +97,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
private bool isAuditingPlugin;
private bool isEnablingPlugin;
private string assistantDescription = string.Empty;
private AssistantCategory selectedCategory;
private PluginCategory selectedCategory;
private string customCategory = string.Empty;
private string assistantName = string.Empty;
private string typicalInput = string.Empty;
@@ -135,7 +135,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
private static readonly AssistantSessionStateKey<bool> IS_AUDITING_PLUGIN_STATE_KEY = new(nameof(isAuditingPlugin));
private static readonly AssistantSessionStateKey<bool> IS_ENABLING_PLUGIN_STATE_KEY = new(nameof(isEnablingPlugin));
private static readonly AssistantSessionStateKey<string> ASSISTANT_DESCRIPTION_STATE_KEY = new(nameof(assistantDescription));
private static readonly AssistantSessionStateKey<AssistantCategory> SELECTED_CATEGORY_STATE_KEY = new(nameof(selectedCategory));
private static readonly AssistantSessionStateKey<PluginCategory> SELECTED_CATEGORY_STATE_KEY = new(nameof(selectedCategory));
private static readonly AssistantSessionStateKey<string> CUSTOM_CATEGORY_STATE_KEY = new(nameof(customCategory));
private static readonly AssistantSessionStateKey<string> ASSISTANT_NAME_STATE_KEY = new(nameof(assistantName));
private static readonly AssistantSessionStateKey<string> TYPICAL_INPUT_STATE_KEY = new(nameof(typicalInput));
@@ -241,7 +241,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
this.pluginId = Guid.NewGuid();
this.step = BuilderStep.DESCRIBE;
this.assistantDescription = string.Empty;
this.selectedCategory = AssistantCategory.AS_IS;
this.selectedCategory = PluginCategory.NONE;
this.customCategory = string.Empty;
this.assistantName = string.Empty;
this.typicalInput = string.Empty;
@@ -334,7 +334,8 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
state.Restore(IS_AUDITING_PLUGIN_STATE_KEY, value => this.isAuditingPlugin = value);
state.Restore(IS_ENABLING_PLUGIN_STATE_KEY, value => this.isEnablingPlugin = value);
state.Restore(ASSISTANT_DESCRIPTION_STATE_KEY, value => this.assistantDescription = value);
state.Restore(SELECTED_CATEGORY_STATE_KEY, value => this.selectedCategory = value);
// A category your organization disabled meanwhile goes back to the choice of the model:
state.Restore(SELECTED_CATEGORY_STATE_KEY, value => this.selectedCategory = this.OfferedCategories.Contains(value) ? value : PluginCategory.NONE);
state.Restore(CUSTOM_CATEGORY_STATE_KEY, value => this.customCategory = value);
state.Restore(ASSISTANT_NAME_STATE_KEY, value => this.assistantName = value);
state.Restore(TYPICAL_INPUT_STATE_KEY, value => this.typicalInput = value);
@@ -374,16 +375,41 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
return null;
}
private string? ValidatingCategory(AssistantCategory category)
/// <summary>
/// The choices of the category field: leaving it to the model, the categories of AI Studio in the
/// order of the assistants page, and a custom category last. Categories your organization disabled
/// are missing.
/// </summary>
private IReadOnlyList<PluginCategory> OfferedCategories
{
return null;
get
{
var app = this.SettingsManager.ConfigurationData.App;
return AssistantBuilderCategories.IsCustomOfferedUnder(app)
? [PluginCategory.NONE, ..AssistantBuilderCategories.OfferedUnder(app), PluginCategory.CUSTOM]
: [PluginCategory.NONE, ..AssistantBuilderCategories.OfferedUnder(app)];
}
}
private string NameOfCategory(PluginCategory category) => category switch
{
PluginCategory.NONE => T("Let the model choose"),
PluginCategory.CUSTOM => T("Other"),
_ => category.Name,
};
private string? ValidateCustomCategory(string category)
{
if(this.selectedCategory is AssistantCategory.OTHER && string.IsNullOrWhiteSpace(category))
if (this.selectedCategory is not PluginCategory.CUSTOM)
return null;
if(string.IsNullOrWhiteSpace(category))
return TA("Please provide a custom category.");
if (this.SettingsManager.ConfigurationData.App.IsAssistantCategoryDisabled(new AssistantCategoryKey(PluginCategory.CUSTOM, category)))
return TA("Your organization disabled this category. Please choose another one.");
return null;
}
@@ -407,7 +433,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
var draft = await this.AssistantPluginGenerationService.GenerateAssistantDraftAsync(
new(
this.assistantDescription,
this.GetSelectedCategoryName(),
this.GetSelectedCategoryForDraft(),
this.assistantName,
this.createChatLauncher ? string.Empty : this.typicalInput,
this.createChatLauncher ? string.Empty : this.expectedOutput,
@@ -522,12 +548,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
this.step = BuilderStep.REVIEW_SPEC;
}
private string GetSelectedCategoryName() => this.selectedCategory switch
{
AssistantCategory.AS_IS => string.Empty,
AssistantCategory.OTHER => this.customCategory,
_ => this.selectedCategory.Name,
};
private string GetSelectedCategoryForDraft() => AssistantBuilderCategories.FormatForDraft(this.selectedCategory, this.customCategory);
private string GetSelectedOutputLanguageName() => this.selectedOutputLanguage switch
{
@@ -0,0 +1,143 @@
using AIStudio.Settings;
using AIStudio.Settings.DataModel;
using AIStudio.Tools.PluginSystem;
using AIStudio.Tools.PluginSystem.Assistants;
namespace AIStudio.Assistants.Builder;
/// <summary>
/// The categories the Assistant Builder offers and how its draft names them.
/// </summary>
/// <remarks>
/// The draft is Markdown which the user may edit, and the step which writes the plugin reads the category
/// from it. The section therefore names the category in a fixed form: the key of a category of AI Studio,
/// such as <c>BUSINESS</c>, or <c>CUSTOM: </c> followed by the name of a category of the user's own.
/// CORE belongs to configuration and language plugins, so the builder never offers it.
/// </remarks>
public static class AssistantBuilderCategories
{
/// <summary>
/// The prefix of a custom category in the draft.
/// </summary>
public const string CUSTOM_PREFIX = "CUSTOM:";
/// <summary>
/// Gets the categories of AI Studio the builder offers, in the order of the assistants page.
/// </summary>
public static IReadOnlyList<PluginCategory> Offered => Enum.GetValues<PluginCategory>()
.Where(category => category is not (PluginCategory.NONE or PluginCategory.CORE or PluginCategory.CUSTOM))
.Order(Comparer<PluginCategory>.Create((left, right) => AssistantPageSections.Compare(new(left), new(right))))
.ToList();
/// <summary>
/// Gets the categories of AI Studio the builder offers under the rules of your organization, in the
/// order of the assistants page.
/// </summary>
/// <param name="app">The app settings which hold the categories your organization disabled.</param>
/// <returns>The offered categories without the ones your organization disabled.</returns>
public static IReadOnlyList<PluginCategory> OfferedUnder(DataApp app) => Offered
.Where(category => !app.IsAssistantCategoryDisabled(new AssistantCategoryKey(category)))
.ToList();
/// <summary>
/// Whether the builder offers custom categories under the rules of your organization.
/// </summary>
/// <param name="app">The app settings which hold the categories your organization disabled.</param>
/// <returns>False when your organization disabled CUSTOM, i.e., every custom category.</returns>
public static bool IsCustomOfferedUnder(DataApp app) => !app.DisabledAssistantCategories.Contains(PluginCategory.CUSTOM);
/// <summary>
/// Finds the categories of a new or revised assistant which your organization disabled.
/// </summary>
/// <remarks>
/// The builder creates no assistant with a disabled category, even when another category would
/// keep it available on the assistants page: the user would build something the organization
/// does not want on its devices.
/// </remarks>
/// <param name="app">The app settings which hold the categories your organization disabled.</param>
/// <param name="categories">The categories of the assistant.</param>
/// <param name="customCategories">The names of its custom categories.</param>
/// <returns>The disabled categories, each once, or an empty list.</returns>
public static IReadOnlyList<AssistantCategoryKey> FindDisabled(DataApp app, IEnumerable<PluginCategory> categories, IEnumerable<string> customCategories) =>
AssistantCategoryKey.AllOf(categories, customCategories)
.Where(category => app.IsAssistantCategoryDisabled(category))
.ToList();
/// <summary>
/// Writes a category the way the draft names it.
/// </summary>
/// <param name="category">The selected category, or NONE when the user left the choice to the model.</param>
/// <param name="customName">The name of the custom category, when the category is CUSTOM.</param>
/// <returns>The category in the form of the draft, or an empty string for NONE.</returns>
public static string FormatForDraft(PluginCategory category, string customName) => category switch
{
PluginCategory.NONE => string.Empty,
PluginCategory.CUSTOM => $"{CUSTOM_PREFIX} {customName.Trim()}",
_ => Enum.GetName(category)!,
};
/// <summary>
/// Lists the categories a model may choose from: one line per key, with the name users read.
/// </summary>
/// <param name="app">The app settings which hold the categories your organization disabled.</param>
/// <returns>The list of the offered categories as Markdown, without the ones your organization disabled.</returns>
public static string ListForPrompt(DataApp app) => string.Join('\n', OfferedUnder(app).Select(category => $"- {Enum.GetName(category)}: {category.Name}"));
/// <summary>
/// Reads the categories a model names for a plugin, and refuses anything else than what the plugin
/// loader accepts.
/// </summary>
/// <remarks>
/// The loader reads CATEGORIES by the names of the categories and drops what it does not know; versions
/// before v26.11.2 also drop a name in another case. A model which writes "1", or "business", would thus
/// leave a plugin without its category, at least in an older version. We refuse such an answer instead
/// of guessing what the model meant, and ask for the exact spelling. CORE stays valid, so that
/// a revision of a plugin written before COMMON existed keeps working. The names of custom categories
/// belong to CUSTOM, in both directions, as in the loader.
/// </remarks>
/// <param name="categoryNames">The keys of the categories, as the model wrote them.</param>
/// <param name="customNames">The names of the custom categories, as the model wrote them.</param>
/// <param name="categories">The categories, each once, in the order of the model.</param>
/// <param name="customCategories">The trimmed names of the custom categories, each once.</param>
/// <returns>True when the model named at least one category, and only valid ones.</returns>
public static bool TryRead(IReadOnlyList<string> categoryNames, IReadOnlyList<string> customNames, out PluginCategory[] categories, out string[] customCategories)
{
categories = [];
customCategories = [];
if (categoryNames.Count == 0)
return false;
var readCategories = new List<PluginCategory>();
foreach (var categoryName in categoryNames)
{
if (!PluginCategory.TryParseName(categoryName, out var category) || category is PluginCategory.NONE || !string.Equals(Enum.GetName(category), categoryName, StringComparison.Ordinal))
return false;
if (!readCategories.Contains(category))
readCategories.Add(category);
}
if (customNames.Any(string.IsNullOrWhiteSpace))
return false;
var readCustomNames = customNames.Select(name => name.Trim()).Distinct(StringComparer.OrdinalIgnoreCase).ToArray();
if (readCategories.Contains(PluginCategory.CUSTOM) != readCustomNames.Length > 0)
return false;
categories = readCategories.ToArray();
customCategories = readCustomNames;
return true;
}
/// <summary>
/// Whether a loaded plugin states the same categories as the model answer which came with it.
/// </summary>
/// <param name="plugin">The plugin as AI Studio loaded it.</param>
/// <param name="categories">The categories the answer names.</param>
/// <param name="customCategories">The custom categories the answer names.</param>
/// <returns>True when both name the same categories, in any order.</returns>
public static bool MatchPlugin(IPluginMetadata plugin, IReadOnlyCollection<PluginCategory> categories, IReadOnlyCollection<string> customCategories) =>
plugin.Categories.ToHashSet().SetEquals(categories) &&
plugin.CustomCategories.ToHashSet(StringComparer.OrdinalIgnoreCase).SetEquals(customCategories);
}
@@ -12,7 +12,7 @@
],
"properties": {
"schema_version": {
"const": "assistant_builder_lua_response_v2"
"const": "assistant_builder_lua_response_v3"
},
"plugin": {
"type": "object",
@@ -34,6 +34,44 @@
"categories": {
"type": "array",
"minItems": 1,
"uniqueItems": true,
"items": {
"enum": [
"CORE",
"BUSINESS",
"INDUSTRY",
"UTILITY",
"SOFTWARE_DEVELOPMENT",
"GAMING",
"EDUCATION",
"ENTERTAINMENT",
"SOCIAL",
"SHOPPING",
"TRAVEL",
"HEALTH",
"FITNESS",
"FOOD",
"PARTY",
"SPORTS",
"NEWS",
"WEATHER",
"MUSIC",
"POLITICAL",
"SCIENCE",
"TECHNOLOGY",
"ART",
"FICTION",
"WRITING",
"CONTENT_CREATION",
"COMMON",
"AI_STUDIO_DEVELOPMENT",
"CUSTOM"
]
}
},
"custom_categories": {
"description": "The names of the custom categories. Required with CUSTOM in categories, and empty or absent otherwise.",
"type": "array",
"items": {
"type": "string",
"minLength": 1
@@ -5,4 +5,9 @@ internal sealed class AssistantBuilderPluginMetadata
public string Name { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
public string[] Categories { get; set; } = [];
/// <summary>
/// The names of the custom categories, when the categories contain CUSTOM.
/// </summary>
public string[] CustomCategories { get; set; } = [];
}
@@ -12,7 +12,7 @@ namespace AIStudio.Assistants.Builder;
/// </remarks>
internal sealed class LauncherTextsResponse
{
public const string SCHEMA_VERSION_VALUE = "assistant_builder_launcher_texts_v1";
public const string SCHEMA_VERSION_VALUE = "assistant_builder_launcher_texts_v2";
/// <summary>
/// How the answer of the model is read.
@@ -48,6 +48,16 @@ internal sealed class LauncherTextsResponse
/// </summary>
public string Description { get; set; } = string.Empty;
/// <summary>
/// The keys of the categories, taken from the approved draft.
/// </summary>
public string[] Categories { get; set; } = [];
/// <summary>
/// The names of the custom categories, when the categories contain CUSTOM.
/// </summary>
public string[] CustomCategories { get; set; } = [];
public static bool TryParse(string modelResponse, out LauncherTextsResponse response, out LuaResponseParseError error, out string technicalDetails)
{
response = new();
@@ -93,6 +103,12 @@ internal sealed class LauncherTextsResponse
return false;
}
if (!AssistantBuilderCategories.TryRead(parsed.Categories, parsed.CustomCategories, out _, out _))
{
error = LuaResponseParseError.INVALID_CATEGORIES;
return false;
}
response = parsed;
return true;
}
@@ -90,6 +90,12 @@ internal sealed partial class LuaResponse
return false;
}
if (!AssistantBuilderCategories.TryRead(this.Plugin.Categories, this.Plugin.CustomCategories, out _, out _))
{
error = LuaResponseParseError.INVALID_CATEGORIES;
return false;
}
if (string.IsNullOrWhiteSpace(this.Assistant.Title) ||
string.IsNullOrWhiteSpace(this.Assistant.Description) ||
!IsValidAssistantMetadata(this.Assistant))
@@ -2,7 +2,7 @@ namespace AIStudio.Assistants.Builder;
internal sealed partial class LuaResponse
{
public const string SCHEMA_VERSION_VALUE = "assistant_builder_lua_response_v2";
public const string SCHEMA_VERSION_VALUE = "assistant_builder_lua_response_v3";
public string SchemaVersion { get; set; } = string.Empty;
public AssistantBuilderPluginMetadata? Plugin { get; set; }
public AssistantBuilderAssistantMetadata? Assistant { get; set; }
@@ -10,6 +10,7 @@ public enum LuaResponseParseError
MISSING_PLUGIN_METADATA,
MISSING_ASSISTANT_METADATA,
INCOMPLETE_PLUGIN_METADATA,
INVALID_CATEGORIES,
INCOMPLETE_ASSISTANT_METADATA,
MISSING_LUA,
LUA_MISSING_ID,
@@ -17,6 +17,7 @@ public static class LuaResponseParseErrorExtension
LuaResponseParseError.MISSING_PLUGIN_METADATA => TB("The model's answer is missing the plugin metadata."),
LuaResponseParseError.MISSING_ASSISTANT_METADATA => TB("The model's answer is missing the assistant metadata."),
LuaResponseParseError.INCOMPLETE_PLUGIN_METADATA => TB("The model's answer contains incomplete plugin metadata."),
LuaResponseParseError.INVALID_CATEGORIES => TB("The model's answer names a category AI Studio does not know, or a custom category without its name."),
LuaResponseParseError.INCOMPLETE_ASSISTANT_METADATA => TB("The model's answer contains incomplete assistant metadata."),
LuaResponseParseError.MISSING_LUA => TB("The model response does not contain the generated Lua plugin code."),
LuaResponseParseError.LUA_MISSING_ID => TB("The generated Lua plugin code does not contain a readable plugin ID."),