mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 04:53:48 +00:00
Added a shared method for keeping assistant plugin audits (#1216)
This commit is contained in:
1 parent
c50a93cd17
commit
808b95b15d
8 files changed
+102
-64
No files matched your search
@@ -750,7 +750,7 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
|
||||
return;
|
||||
}
|
||||
|
||||
this.UpsertAudit(this.pluginAudit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, this.pluginAudit);
|
||||
await this.SettingsManager.StoreSettings();
|
||||
await this.MessageBus.SendSuccess(new(
|
||||
this.pluginAudit.Level.Icon,
|
||||
@@ -848,16 +848,6 @@ public partial class AssistantBuilder : AssistantBaseCore<SettingsDialogAssistan
|
||||
|
||||
private static PluginAssistants? ResolveAssistantPlugin(Guid pluginId) => PluginFactory.RunningPlugins.OfType<PluginAssistants>().FirstOrDefault(plugin => plugin.Id == pluginId);
|
||||
|
||||
private void UpsertAudit(PluginAssistantAudit audit)
|
||||
{
|
||||
var audits = this.SettingsManager.ConfigurationData.AssistantPluginAudits;
|
||||
var existingIndex = audits.FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
private void FailInstallStep(BuilderInstallStep installStep, string issue)
|
||||
{
|
||||
this.failedInstallStep = installStep;
|
||||
|
||||
@@ -70,7 +70,7 @@ public partial class AssistantPluginSecurityCard : MSGComponentBase
|
||||
return;
|
||||
|
||||
if (auditResult.Audit is not null)
|
||||
UpsertAudit(this.SettingsManager.ConfigurationData.AssistantPluginAudits, auditResult.Audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, auditResult.Audit);
|
||||
|
||||
if (auditResult.ActivatePlugin && !this.SettingsManager.ConfigurationData.EnabledPlugins.Contains(this.Plugin.Id))
|
||||
this.SettingsManager.ConfigurationData.EnabledPlugins.Add(this.Plugin.Id);
|
||||
@@ -94,15 +94,6 @@ public partial class AssistantPluginSecurityCard : MSGComponentBase
|
||||
private void ToggleDetails() => this.showDetails = !this.showDetails;
|
||||
|
||||
private void ToggleMetadata() => this.showMetadata = !this.showMetadata;
|
||||
|
||||
private static void UpsertAudit(List<PluginAssistantAudit> audits, PluginAssistantAudit audit)
|
||||
{
|
||||
var existingIndex = audits.FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
private string FormatFileTimestamp(DateTime timestamp) => CommonTools.FormatTimestampToGeneral(timestamp, this.currentCultureInfo);
|
||||
|
||||
|
||||
@@ -207,7 +207,7 @@ public partial class AssistantPluginRevisionDialog : MSGComponentBase
|
||||
if (audit.Level is PluginAuditLevel.UNKNOWN)
|
||||
return audit;
|
||||
|
||||
UpsertAudit(this.SettingsManager.ConfigurationData.AssistantPluginAudits, audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, audit);
|
||||
await this.SettingsManager.StoreSettings();
|
||||
return audit;
|
||||
}
|
||||
@@ -232,15 +232,6 @@ public partial class AssistantPluginRevisionDialog : MSGComponentBase
|
||||
!string.IsNullOrWhiteSpace(availablePlugin.LocalPath) &&
|
||||
assistantPlugin is { IsInternal: false, IsManagedByConfigServer: false };
|
||||
|
||||
private static void UpsertAudit(IList<PluginAssistantAudit> audits, PluginAssistantAudit audit)
|
||||
{
|
||||
var existingIndex = audits.ToList().FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
private static bool AreSamePath(string left, string right)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(left) || string.IsNullOrWhiteSpace(right))
|
||||
|
||||
@@ -234,7 +234,7 @@ public partial class DirectChatLauncherSettingsDialog : MSGComponentBase
|
||||
if (audit.Level is PluginAuditLevel.UNKNOWN)
|
||||
return audit;
|
||||
|
||||
UpsertAudit(this.SettingsManager.ConfigurationData.AssistantPluginAudits, audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, audit);
|
||||
await this.SettingsManager.StoreSettings();
|
||||
return audit;
|
||||
}
|
||||
@@ -254,15 +254,6 @@ public partial class DirectChatLauncherSettingsDialog : MSGComponentBase
|
||||
|
||||
private static Guid? ParseOptionalGuid(string value) => Guid.TryParse(value, out var parsed) ? parsed : null;
|
||||
|
||||
private static void UpsertAudit(IList<PluginAssistantAudit> audits, PluginAssistantAudit audit)
|
||||
{
|
||||
var existingIndex = audits.ToList().FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
private static bool AreSamePath(string left, string right)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(left) || string.IsNullOrWhiteSpace(right))
|
||||
|
||||
@@ -100,7 +100,7 @@ public partial class Assistants : MSGComponentBase
|
||||
return;
|
||||
|
||||
foreach (var audit in audits)
|
||||
this.UpsertAuditCard(audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, audit);
|
||||
|
||||
await this.SettingsManager.StoreSettings();
|
||||
await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
|
||||
@@ -112,16 +112,6 @@ public partial class Assistants : MSGComponentBase
|
||||
}
|
||||
}
|
||||
|
||||
private void UpsertAuditCard(PluginAssistantAudit audit)
|
||||
{
|
||||
var audits = this.SettingsManager.ConfigurationData.AssistantPluginAudits;
|
||||
var existingIndex = audits.FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
|
||||
{
|
||||
if (triggeredEvent is Event.PLUGINS_RELOADED)
|
||||
|
||||
@@ -143,7 +143,7 @@ public partial class Plugins : MSGComponentBase
|
||||
return;
|
||||
|
||||
if (auditResult.Audit is not null)
|
||||
this.UpsertAuditCard(auditResult.Audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, auditResult.Audit);
|
||||
|
||||
if (auditResult.ActivatePlugin)
|
||||
this.SettingsManager.ConfigurationData.EnabledPlugins.Add(pluginId);
|
||||
@@ -575,7 +575,7 @@ public partial class Plugins : MSGComponentBase
|
||||
return;
|
||||
|
||||
foreach (var audit in audits)
|
||||
this.UpsertAuditCard(audit);
|
||||
AssistantPluginAuditService.UpsertAudit(this.SettingsManager, audit);
|
||||
|
||||
await this.SettingsManager.StoreSettings();
|
||||
await this.MessageBus.SendMessage<bool>(this, Event.CONFIGURATION_CHANGED);
|
||||
@@ -587,16 +587,6 @@ public partial class Plugins : MSGComponentBase
|
||||
}
|
||||
}
|
||||
|
||||
private void UpsertAuditCard(PluginAssistantAudit audit)
|
||||
{
|
||||
var audits = this.SettingsManager.ConfigurationData.AssistantPluginAudits;
|
||||
var existingIndex = audits.FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
#region Overrides of MSGComponentBase
|
||||
|
||||
protected override async Task ProcessIncomingMessage<T>(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default
|
||||
|
||||
@@ -104,6 +104,26 @@ public sealed class AssistantPluginAuditService(PluginAuditOperation auditOperat
|
||||
return audits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Keeps an audit in the settings: it replaces the audit stored for the same plugin, in its place, or joins the others.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The settings keep one audit per plugin, whatever version of the plugin it checked: a newer audit replaces
|
||||
/// the older one, also when the plugin changed meanwhile. The settings are not written; the caller stores
|
||||
/// them once it changed everything it wanted to.
|
||||
/// </remarks>
|
||||
/// <param name="settingsManager">The settings which keep the audits.</param>
|
||||
/// <param name="audit">The audit to keep.</param>
|
||||
public static void UpsertAudit(SettingsManager settingsManager, PluginAssistantAudit audit)
|
||||
{
|
||||
var audits = settingsManager.ConfigurationData.AssistantPluginAudits;
|
||||
var existingIndex = audits.FindIndex(x => x.PluginId == audit.PluginId);
|
||||
if (existingIndex >= 0)
|
||||
audits[existingIndex] = audit;
|
||||
else
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Which provider an audit would run with when nobody has a provider at hand, as in the background.
|
||||
/// </summary>
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
using AIStudio.Settings;
|
||||
using AIStudio.Tools.PluginSystem.Assistants;
|
||||
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace AIStudio.Tests.Tools.PluginSystem;
|
||||
|
||||
/// <summary>
|
||||
/// Checks how the settings keep the audits of assistant plugins.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The settings keep one audit per plugin. A newer audit takes the place of the older one, also when it
|
||||
/// checked another version of the plugin, and the audits of the other plugins keep their places. Every place
|
||||
/// which stores an audit goes through AssistantPluginAuditService.UpsertAudit.
|
||||
/// </remarks>
|
||||
[TestFixture]
|
||||
public sealed class AssistantPluginAuditUpsertTests
|
||||
{
|
||||
private static readonly Guid FIRST_PLUGIN_ID = Guid.Parse("6d2f8a41-3c7e-4b9a-9e15-0a8c4f27d613");
|
||||
|
||||
private static readonly Guid SECOND_PLUGIN_ID = Guid.Parse("b84e1c90-5f2a-4d63-8a7b-2e9d0c4f6a18");
|
||||
|
||||
private static readonly Guid THIRD_PLUGIN_ID = Guid.Parse("1fa7c3d5-8e64-4b02-b9c1-7d5e2a0f8b34");
|
||||
|
||||
[Test]
|
||||
public void AnAuditForANewPluginJoinsTheOthers()
|
||||
{
|
||||
var settingsManager = CreateSettingsManager();
|
||||
var firstAudit = CreateAudit(FIRST_PLUGIN_ID, "hash-of-the-first-plugin");
|
||||
var secondAudit = CreateAudit(SECOND_PLUGIN_ID, "hash-of-the-second-plugin");
|
||||
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, firstAudit);
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, secondAudit);
|
||||
|
||||
Assert.That(settingsManager.ConfigurationData.AssistantPluginAudits, Is.EqualTo(new[] { firstAudit, secondAudit }), "An audit of a plugin without one joins the audits at their end.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ANewerAuditReplacesTheOldOneInItsPlace()
|
||||
{
|
||||
var settingsManager = CreateSettingsManager();
|
||||
var firstAudit = CreateAudit(FIRST_PLUGIN_ID, "hash-of-the-first-plugin");
|
||||
var olderSecondAudit = CreateAudit(SECOND_PLUGIN_ID, "hash-of-the-second-plugin");
|
||||
var thirdAudit = CreateAudit(THIRD_PLUGIN_ID, "hash-of-the-third-plugin");
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, firstAudit);
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, olderSecondAudit);
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, thirdAudit);
|
||||
|
||||
var newerSecondAudit = CreateAudit(SECOND_PLUGIN_ID, "hash-of-the-second-plugin");
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, newerSecondAudit);
|
||||
|
||||
Assert.That(settingsManager.ConfigurationData.AssistantPluginAudits, Is.EqualTo(new[] { firstAudit, newerSecondAudit, thirdAudit }), "A newer audit takes the place of the older one, so the order of the audits stays as it was.");
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ANewerAuditReplacesTheOldOneOfAnotherVersion()
|
||||
{
|
||||
var settingsManager = CreateSettingsManager();
|
||||
var auditOfTheFirstVersion = CreateAudit(FIRST_PLUGIN_ID, "hash-of-the-first-version");
|
||||
var auditOfTheSecondVersion = CreateAudit(FIRST_PLUGIN_ID, "hash-of-the-second-version");
|
||||
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, auditOfTheFirstVersion);
|
||||
AssistantPluginAuditService.UpsertAudit(settingsManager, auditOfTheSecondVersion);
|
||||
|
||||
Assert.That(settingsManager.ConfigurationData.AssistantPluginAudits, Is.EqualTo(new[] { auditOfTheSecondVersion }), "The settings keep one audit per plugin, not one per version of it.");
|
||||
}
|
||||
|
||||
private static SettingsManager CreateSettingsManager() => new(NullLogger<SettingsManager>.Instance, null!);
|
||||
|
||||
private static PluginAssistantAudit CreateAudit(Guid pluginId, string pluginHash) => new()
|
||||
{
|
||||
PluginId = pluginId,
|
||||
PluginHash = pluginHash,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user