mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-12-05 04:04:35 +00:00
Renamed the settings of AI tasks (#1239)
This commit is contained in:
1 parent
d19d3d431b
commit
a117074972
109 files changed
+3929
-538
No files matched your search
@@ -0,0 +1,142 @@
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
using AIStudio.Tools.PluginSystem.Assistants;
|
||||
using AIStudio.Tools.PluginSystem.Assistants.DataModel;
|
||||
|
||||
using Lua;
|
||||
|
||||
namespace AIStudio.Tests.Tools.PluginSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Checks that assistant plugins keep the former names of props.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Before AI Studio v26.11.2, the web content reader called its preselection of the content cleaner
|
||||
/// <c>PreselectContentCleanerAgent</c>. Plugins keep that name for good, because a plugin which changes
|
||||
/// it needs a new security audit.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class AssistantPropFormerNamesTests
|
||||
{
|
||||
private const string FORMER_NAME = "PreselectContentCleanerAgent";
|
||||
private const string CURRENT_NAME = "PreselectContentCleaner";
|
||||
private const string READER_NAME = "webContent";
|
||||
|
||||
private static PropSpec WebContentReaderSpec => ComponentPropSpecs.SPECS[AssistantComponentType.WEB_CONTENT_READER];
|
||||
|
||||
[TestCase(FORMER_NAME)]
|
||||
[TestCase(CURRENT_NAME)]
|
||||
public async Task BothNamesPreselectTheContentCleaner(string propName)
|
||||
{
|
||||
using var plugin = await LoadAssistantAsync($"""["{propName}"] = true""");
|
||||
var reader = FindWebContentReader(plugin);
|
||||
var state = new AssistantState();
|
||||
reader.InitializeState(state);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(reader.PreselectContentCleaner, Is.True);
|
||||
Assert.That(state.WebContent[READER_NAME].PreselectContentCleaner, Is.True);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TheCurrentNameWinsOverTheFormerName()
|
||||
{
|
||||
using var plugin = await LoadAssistantAsync($"""["{FORMER_NAME}"] = true, ["{CURRENT_NAME}"] = false""");
|
||||
|
||||
Assert.That(FindWebContentReader(plugin).PreselectContentCleaner, Is.False);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnUpdateWithTheFormerNameReachesTheCurrentProp()
|
||||
{
|
||||
var update = new LuaTable { [FORMER_NAME] = true };
|
||||
|
||||
var isApplied = WebContentReaderSpec.TryGetCurrentName(FORMER_NAME, update, out var currentName);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(isApplied, Is.True);
|
||||
Assert.That(currentName, Is.EqualTo(CURRENT_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AnUpdateWithBothNamesTakesTheCurrentOne()
|
||||
{
|
||||
var update = new LuaTable { [FORMER_NAME] = true, [CURRENT_NAME] = false };
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(WebContentReaderSpec.TryGetCurrentName(FORMER_NAME, update, out _), Is.False, "The former name is ignored next to the current one.");
|
||||
Assert.That(WebContentReaderSpec.TryGetCurrentName(CURRENT_NAME, update, out var currentName), Is.True);
|
||||
Assert.That(currentName, Is.EqualTo(CURRENT_NAME));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EveryFormerNameLeadsToAKnownProp()
|
||||
{
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
foreach (var (type, spec) in ComponentPropSpecs.SPECS)
|
||||
{
|
||||
foreach (var (formerName, currentName) in spec.FormerNames)
|
||||
{
|
||||
Assert.That(spec.Required.Concat(spec.Optional), Does.Contain(currentName), $"The former name '{formerName}' of {type} leads to '{currentName}', which is no prop of it.");
|
||||
Assert.That(spec.Required.Concat(spec.Optional), Does.Not.Contain(formerName), $"The former name '{formerName}' is a prop of {type} again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads an assistant plugin whose form holds a web content reader with the given props besides its name.
|
||||
/// </summary>
|
||||
private static async Task<PluginAssistants> LoadAssistantAsync(string readerProps)
|
||||
{
|
||||
var lua = $$"""
|
||||
ID = "a9d4e2b7-61c3-4f85-b0e8-3c7f19d5a246"
|
||||
NAME = "Assistant for the tests"
|
||||
DESCRIPTION = "An assistant plugin with a web content reader."
|
||||
VERSION = "1.0.0"
|
||||
TYPE = "ASSISTANT"
|
||||
AUTHORS = {"MindWork AI"}
|
||||
SUPPORT_CONTACT = "MindWork AI"
|
||||
SOURCE_URL = "https://github.com/MindWorkAI/AI-Studio"
|
||||
CATEGORIES = {"COMMON"}
|
||||
TARGET_GROUPS = {"EVERYONE"}
|
||||
IS_MAINTAINED = true
|
||||
DEPRECATION_MESSAGE = ""
|
||||
|
||||
ASSISTANT = {
|
||||
["Title"] = "Web page summary",
|
||||
["Description"] = "Summarizes a web page.",
|
||||
["SystemPrompt"] = "You summarize the web page the user provides.",
|
||||
["SubmitText"] = "Summarize",
|
||||
["AllowProfiles"] = false,
|
||||
["UI"] = {
|
||||
["Type"] = "FORM",
|
||||
["Children"] = {
|
||||
{
|
||||
["Type"] = "WEB_CONTENT_READER",
|
||||
["Props"] = { ["Name"] = "{{READER_NAME}}", {{readerProps}} }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
""";
|
||||
|
||||
var plugin = await PluginFactory.Load(null, lua);
|
||||
if (plugin is not PluginAssistants { IsValid: true } assistantPlugin)
|
||||
{
|
||||
var issues = string.Join(" ", plugin.Issues);
|
||||
plugin.Dispose();
|
||||
throw new InvalidOperationException($"The code of this test did not produce a valid assistant plugin: {issues}");
|
||||
}
|
||||
|
||||
return assistantPlugin;
|
||||
}
|
||||
|
||||
private static AssistantWebContentReader FindWebContentReader(PluginAssistants plugin) => plugin.RootComponent!.Children.OfType<AssistantWebContentReader>().Single();
|
||||
}
|
||||
@@ -0,0 +1,237 @@
|
||||
using System.Text;
|
||||
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.PluginSystem;
|
||||
using AIStudio.Tools.Services;
|
||||
|
||||
using Lua;
|
||||
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
using SharedTools.Text;
|
||||
|
||||
namespace AIStudio.Tests.Tools.PluginSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Checks that configuration plugins written for AI Studio v26.11.1 keep the seven settings of the AI
|
||||
/// tasks which v26.11.2 renamed.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Compatibility shim, remove after 2027-05-26, see documentation/compatibility-shims/2026-11-ai-task-setting-names.md.
|
||||
/// The tests which run a plugin reach the settings through Program.SERVICE_PROVIDER, and the managed
|
||||
/// state of each setting is static, which is why this fixture does not run alongside others.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
[NonParallelizable]
|
||||
public sealed class FormerSettingNamesTests
|
||||
{
|
||||
private const string FORMER_NAME = "DataAssistantPluginAudit.PreselectedAgentProvider";
|
||||
private const string CURRENT_NAME = "DataAssistantPluginAudit.PreselectedProvider";
|
||||
private const string USER_OVERRIDE_SUFFIX = ".AllowUserOverride";
|
||||
private const string FORMER_PROVIDER_ID = "0b6f3c1e-58a2-4d7b-9e44-2f1c8a7d3b90";
|
||||
private const string CURRENT_PROVIDER_ID = "6d2e9a47-c3b1-4f08-a5d6-91e7b4c20f3a";
|
||||
|
||||
private static readonly Guid PLUGIN_ID = Guid.Parse("3c8e51d7-9f2a-4b66-8d03-e7a1b5f49c28");
|
||||
|
||||
private RustService rustService = null!;
|
||||
private SettingsManager settingsManager = null!;
|
||||
private ServiceProvider serviceProvider = null!;
|
||||
private IServiceProvider previousServiceProvider = null!;
|
||||
|
||||
[SetUp]
|
||||
public void CreateSettings()
|
||||
{
|
||||
// Only builds its HTTP clients. Nothing connects, because the plugins of these tests define no provider:
|
||||
this.rustService = new RustService("1", "unused");
|
||||
|
||||
// Creating the settings data registers every managed setting:
|
||||
this.settingsManager = new SettingsManager(NullLogger<SettingsManager>.Instance, this.rustService);
|
||||
|
||||
this.previousServiceProvider = Program.SERVICE_PROVIDER;
|
||||
this.serviceProvider = new ServiceCollection().AddSingleton(this.settingsManager).AddSingleton(this.rustService).BuildServiceProvider();
|
||||
Program.SERVICE_PROVIDER = this.serviceProvider;
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void RestoreApplicationState()
|
||||
{
|
||||
// The managed state of a setting outlives this fixture, so we release everything the plugin took over:
|
||||
ManagedConfiguration.CleanupLeftOverManagedConfigurations([], new HashSet<Guid>());
|
||||
|
||||
Program.SERVICE_PROVIDER = this.previousServiceProvider;
|
||||
this.serviceProvider.Dispose();
|
||||
this.rustService.Dispose();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void EveryCurrentNameIsARegisteredSetting()
|
||||
{
|
||||
var registeredSettingNames = ManagedConfiguration.RegisteredSettings.Select(setting => setting.SettingName).ToHashSet(StringComparer.Ordinal);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(FormerSettingNames.CURRENT_NAMES, Has.Count.EqualTo(7), "AI Studio v26.11.1 knew seven of the renamed settings.");
|
||||
foreach (var (formerName, currentName) in FormerSettingNames.CURRENT_NAMES)
|
||||
{
|
||||
Assert.That(registeredSettingNames, Does.Contain(currentName), $"The former name '{formerName}' leads to '{currentName}', which is no registered setting.");
|
||||
Assert.That(registeredSettingNames, Does.Not.Contain(formerName), $"The former name '{formerName}' is a registered setting again.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void AFormerNameTakesTheCurrentName()
|
||||
{
|
||||
var settingsTable = new LuaTable
|
||||
{
|
||||
[FORMER_NAME] = FORMER_PROVIDER_ID,
|
||||
[$"{FORMER_NAME}{USER_OVERRIDE_SUFFIX}"] = true,
|
||||
["DataApp.UpdateInterval"] = "ONCE_STARTUP",
|
||||
};
|
||||
|
||||
var resolvedTable = FormerSettingNames.Resolve(settingsTable, "Configuration for the tests", false);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(ReadNames(resolvedTable), Is.EquivalentTo(new[] { CURRENT_NAME, $"{CURRENT_NAME}{USER_OVERRIDE_SUFFIX}", "DataApp.UpdateInterval" }));
|
||||
Assert.That(resolvedTable[CURRENT_NAME].Read<string>(), Is.EqualTo(FORMER_PROVIDER_ID));
|
||||
Assert.That(resolvedTable[$"{CURRENT_NAME}{USER_OVERRIDE_SUFFIX}"].Read<bool>(), Is.True);
|
||||
Assert.That(resolvedTable["DataApp.UpdateInterval"].Read<string>(), Is.EqualTo("ONCE_STARTUP"));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TheCurrentNameWinsOverTheFormerName()
|
||||
{
|
||||
var settingsTable = new LuaTable
|
||||
{
|
||||
[FORMER_NAME] = FORMER_PROVIDER_ID,
|
||||
[CURRENT_NAME] = CURRENT_PROVIDER_ID,
|
||||
[$"{FORMER_NAME}{USER_OVERRIDE_SUFFIX}"] = true,
|
||||
[$"{CURRENT_NAME}{USER_OVERRIDE_SUFFIX}"] = false,
|
||||
};
|
||||
|
||||
var resolvedTable = FormerSettingNames.Resolve(settingsTable, "Configuration for the tests", false);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(ReadNames(resolvedTable), Is.EquivalentTo(new[] { CURRENT_NAME, $"{CURRENT_NAME}{USER_OVERRIDE_SUFFIX}" }));
|
||||
Assert.That(resolvedTable[CURRENT_NAME].Read<string>(), Is.EqualTo(CURRENT_PROVIDER_ID));
|
||||
Assert.That(resolvedTable[$"{CURRENT_NAME}{USER_OVERRIDE_SUFFIX}"].Read<bool>(), Is.False);
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFormerNameLocksTheSetting()
|
||||
{
|
||||
using var plugin = await RunPluginAsync($"""CONFIG["SETTINGS"]["{FORMER_NAME}"] = "{FORMER_PROVIDER_ID}" """);
|
||||
var isRegistered = ManagedConfiguration.TryGet(x => x.AssistantPluginAudit, x => x.PreselectedProvider, out var configMeta);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(isRegistered, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.True);
|
||||
Assert.That(configMeta.LockedByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
Assert.That(this.settingsManager.ConfigurationData.AssistantPluginAudit.PreselectedProvider, Is.EqualTo(FORMER_PROVIDER_ID));
|
||||
Assert.That(plugin.DeclaredSettingsCount, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task AFormerNameWithUserOverrideSetsAnOrganizationDefault()
|
||||
{
|
||||
using var plugin = await RunPluginAsync(
|
||||
$"""CONFIG["SETTINGS"]["{FORMER_NAME}"] = "{FORMER_PROVIDER_ID}" """,
|
||||
$"""CONFIG["SETTINGS"]["{FORMER_NAME}{USER_OVERRIDE_SUFFIX}"] = true""");
|
||||
|
||||
var isRegistered = ManagedConfiguration.TryGet(x => x.AssistantPluginAudit, x => x.PreselectedProvider, out var configMeta);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(isRegistered, Is.True);
|
||||
Assert.That(configMeta.IsLocked, Is.False);
|
||||
Assert.That(configMeta.ManagedMode, Is.EqualTo(ManagedConfigurationMode.EDITABLE_DEFAULT));
|
||||
Assert.That(configMeta.EditableDefaultByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
Assert.That(this.settingsManager.ConfigurationData.AssistantPluginAudit.PreselectedProvider, Is.EqualTo(FORMER_PROVIDER_ID));
|
||||
Assert.That(plugin.DeclaredSettingsCount, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// An organization sets both names while its devices run v26.11.1 and v26.11.2 alike.
|
||||
/// </summary>
|
||||
[Test]
|
||||
public async Task APluginWhichSetsBothNamesCountsTheSettingOnce()
|
||||
{
|
||||
using var plugin = await RunPluginAsync(
|
||||
$"""CONFIG["SETTINGS"]["{FORMER_NAME}"] = "{FORMER_PROVIDER_ID}" """,
|
||||
$"""CONFIG["SETTINGS"]["{CURRENT_NAME}"] = "{CURRENT_PROVIDER_ID}" """);
|
||||
|
||||
var isRegistered = ManagedConfiguration.TryGet(x => x.AssistantPluginAudit, x => x.PreselectedProvider, out var configMeta);
|
||||
|
||||
using (Assert.EnterMultipleScope())
|
||||
{
|
||||
Assert.That(isRegistered, Is.True);
|
||||
Assert.That(configMeta.LockedByConfigPluginId, Is.EqualTo(PLUGIN_ID));
|
||||
Assert.That(this.settingsManager.ConfigurationData.AssistantPluginAudit.PreselectedProvider, Is.EqualTo(CURRENT_PROVIDER_ID));
|
||||
Assert.That(plugin.DeclaredSettingsCount, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a configuration plugin with the given settings and applies it the way AI Studio does when the plugin starts.
|
||||
/// </summary>
|
||||
/// <param name="settingLines">The Lua lines which set the settings.</param>
|
||||
/// <returns>The started configuration plugin.</returns>
|
||||
private static async Task<PluginConfiguration> RunPluginAsync(params string[] settingLines)
|
||||
{
|
||||
var code = new StringBuilder($$"""
|
||||
ID = "{{PLUGIN_ID}}"
|
||||
NAME = "Configuration for the tests"
|
||||
DESCRIPTION = "A configuration plugin which uses the former names of settings."
|
||||
VERSION = "1.0.0"
|
||||
TYPE = "CONFIGURATION"
|
||||
AUTHORS = {"MindWork AI"}
|
||||
SUPPORT_CONTACT = "MindWork AI"
|
||||
SOURCE_URL = "https://github.com/MindWorkAI/AI-Studio"
|
||||
CATEGORIES = {"CORE"}
|
||||
TARGET_GROUPS = {"EVERYONE"}
|
||||
IS_MAINTAINED = true
|
||||
DEPRECATION_MESSAGE = ""
|
||||
CONFIG = {}
|
||||
CONFIG["SETTINGS"] = {}
|
||||
|
||||
""");
|
||||
|
||||
foreach (var settingLine in settingLines)
|
||||
code.AppendLineLF(settingLine);
|
||||
|
||||
var plugin = await PluginFactory.Load(null, code.ToString());
|
||||
if (plugin is not PluginConfiguration { IsValid: true } configurationPlugin)
|
||||
{
|
||||
var issues = string.Join(" ", plugin.Issues);
|
||||
plugin.Dispose();
|
||||
throw new InvalidOperationException($"The code of this test did not produce a valid configuration plugin: {issues}");
|
||||
}
|
||||
|
||||
await configurationPlugin.InitializeAsync(false);
|
||||
return configurationPlugin;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads the names of all settings in a SETTINGS table.
|
||||
/// </summary>
|
||||
private static List<string> ReadNames(LuaTable settingsTable)
|
||||
{
|
||||
var names = new List<string>();
|
||||
var previousKey = LuaValue.Nil;
|
||||
while (settingsTable.TryGetNext(previousKey, out var pair))
|
||||
{
|
||||
previousKey = pair.Key;
|
||||
names.Add(pair.Key.Read<string>());
|
||||
}
|
||||
|
||||
return names;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user