mirror of
https://github.com/MindWorkAI/AI-Studio.git
synced 2026-11-23 08:33:49 +00:00
Moved the background agents to AI operations (#1215)
This commit is contained in:
1 parent
8a4a26503b
commit
c50a93cd17
139 files changed
+4069
-2340
No files matched your search
@@ -24,7 +24,7 @@ and whoever runs it: loading, starting, checking an import, and the assistant bu
|
||||
## Assistant plugins
|
||||
|
||||
Every prompt text an assistant plugin brings, its `SystemPrompt` as well as its `CustomChatSystemPrompt`, goes
|
||||
through `PluginAssistants.BuildSecureSystemPrompt` and into the security audit of `AssistantAuditAgent`. A new
|
||||
through `PluginAssistants.BuildSecureSystemPrompt` and into the security audit of `PluginAuditOperation`. A new
|
||||
field which reaches a model as a prompt needs both, and its documentation in `Plugins/assistants/README.md` and in
|
||||
the template `Plugins/assistants/plugin.lua`: the Assistant Builder reads both files as its source of truth.
|
||||
|
||||
|
||||
+146
-8
@@ -1,21 +1,47 @@
|
||||
using AIStudio.Agents.AssistantAudit;
|
||||
using AIStudio.AIOperations;
|
||||
using AIStudio.AIOperations.PluginAudit;
|
||||
using AIStudio.Provider;
|
||||
using AIStudio.Settings;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem.Assistants;
|
||||
|
||||
/// <summary>
|
||||
/// Runs an assistant security audit and maps the agent result to the persisted audit model.
|
||||
/// Runs the security audit of an assistant plugin and maps its result to the persisted audit model.
|
||||
/// </summary>
|
||||
public sealed class AssistantPluginAuditService(AssistantAuditAgent auditAgent)
|
||||
public sealed class AssistantPluginAuditService(PluginAuditOperation auditOperation, AIOperationProviderResolver providerResolver, SettingsManager settingsManager)
|
||||
{
|
||||
private static string TB(string fallbackEN) => I18N.I.T(fallbackEN, typeof(AssistantPluginAuditService).Namespace, nameof(AssistantPluginAuditService));
|
||||
|
||||
private static string TA(string fallbackEN) => I18N.I.TA(fallbackEN, typeof(AssistantPluginAuditService).Namespace, nameof(AssistantPluginAuditService));
|
||||
|
||||
/// <summary>
|
||||
/// Runs an assistant plugin audit, optionally falling back to the supplied provider when no audit provider is configured.
|
||||
/// </summary>
|
||||
public async Task<PluginAssistantAudit> RunAuditAsync(PluginAssistants plugin, Settings.Provider? fallbackProvider = null, CancellationToken token = default)
|
||||
/// <param name="plugin">The plugin to audit.</param>
|
||||
/// <param name="providerAtHand">A provider somebody picked for something else, e.g., the assistant they are building; the audit falls back on it last.</param>
|
||||
/// <param name="token">The cancellation token.</param>
|
||||
/// <returns>The audit, which names the provider it ran with.</returns>
|
||||
public async Task<PluginAssistantAudit> RunAuditAsync(PluginAssistants plugin, Settings.Provider? providerAtHand = null, CancellationToken token = default)
|
||||
{
|
||||
var result = await auditAgent.AuditAsync(plugin, fallbackProvider, token);
|
||||
var provider = auditAgent.ProviderSettings;
|
||||
var promptPreview = await plugin.BuildAuditPromptPreviewAsync(token);
|
||||
var request = new PluginAuditRequest(plugin);
|
||||
var choice = providerResolver.Resolve(auditOperation, request, PluginAuditOperation.GetProviderSettings(settingsManager), GetProviderOptions(settingsManager, providerAtHand));
|
||||
|
||||
PluginAuditResult result;
|
||||
if (choice.HasProvider)
|
||||
result = await auditOperation.RunAsync(request, new(choice.Provider), token);
|
||||
else
|
||||
{
|
||||
var issue = GetRelevantIssue(choice);
|
||||
await MessageBus.INSTANCE.SendError(new (AppIcons.AI_OPERATION, GetMissingProviderMessage(issue)));
|
||||
result = new PluginAuditResult
|
||||
{
|
||||
Level = nameof(PluginAuditLevel.UNKNOWN),
|
||||
Summary = GetMissingProviderSummary(issue),
|
||||
};
|
||||
}
|
||||
|
||||
var provider = choice.Provider;
|
||||
var promptPreview = await plugin.BuildAuditPromptPreviewAsync(token);
|
||||
return new PluginAssistantAudit
|
||||
{
|
||||
PluginId = plugin.Id,
|
||||
@@ -25,11 +51,123 @@ public sealed class AssistantPluginAuditService(AssistantAuditAgent auditAgent)
|
||||
AuditProviderName = provider == Settings.Provider.NONE
|
||||
? string.Empty
|
||||
: provider.InstanceName,
|
||||
Level = AssistantAuditLevel.ParseOrUnknown(result.Level),
|
||||
Level = PluginAuditLevel.ParseOrUnknown(result.Level),
|
||||
Summary = result.Summary,
|
||||
Confidence = result.Confidence,
|
||||
PromptPreview = promptPreview,
|
||||
Findings = result.Findings,
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Audits every running assistant plugin which needs an audit, as long as automatic audits are switched on.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// In the background, there is no provider at hand. Should no provider be left for a plugin, its audit would
|
||||
/// fail without a request, and the user would get two messages for each plugin, again with every visit of the
|
||||
/// assistants or plugins page. Such a plugin is therefore skipped, and a single message says why.
|
||||
/// </remarks>
|
||||
/// <param name="token">The cancellation token.</param>
|
||||
/// <returns>The audits which succeeded, for the caller to store.</returns>
|
||||
public async Task<IReadOnlyList<PluginAssistantAudit>> RunAutomaticAuditsAsync(CancellationToken token = default)
|
||||
{
|
||||
if (!settingsManager.ConfigurationData.AssistantPluginAudit.AutomaticallyAuditAssistants)
|
||||
return [];
|
||||
|
||||
var audits = new List<PluginAssistantAudit>();
|
||||
AIOperationProviderIssue? issueOfSkippedPlugins = null;
|
||||
foreach (var plugin in PluginFactory.RunningPlugins.OfType<PluginAssistants>().ToList())
|
||||
{
|
||||
if (!PluginAssistantSecurityResolver.Resolve(settingsManager, plugin).RequiresAudit)
|
||||
continue;
|
||||
|
||||
var choice = this.PreviewProvider(new PluginAuditRequest(plugin));
|
||||
if (!choice.HasProvider)
|
||||
{
|
||||
issueOfSkippedPlugins ??= GetRelevantIssue(choice);
|
||||
continue;
|
||||
}
|
||||
|
||||
var audit = await this.RunAuditAsync(plugin, token: token);
|
||||
if (audit.Level is PluginAuditLevel.UNKNOWN)
|
||||
{
|
||||
await MessageBus.INSTANCE.SendError(new (AppIcons.AI_OPERATION, string.Format(TA("The automatic security audit for the assistant plugin '{0}' failed. Please run it manually on the plugins page."), plugin.Name)));
|
||||
continue;
|
||||
}
|
||||
|
||||
audits.Add(audit);
|
||||
}
|
||||
|
||||
if (issueOfSkippedPlugins is { } issue)
|
||||
await MessageBus.INSTANCE.SendError(new (AppIcons.AI_OPERATION, GetAutomaticAuditMessage(issue)));
|
||||
|
||||
return audits;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Which provider an audit would run with when nobody has a provider at hand, as in the background.
|
||||
/// </summary>
|
||||
/// <param name="request">The audit, for the confidence its content requires; null for the least any audit requires.</param>
|
||||
/// <returns>The choice, which logs nothing.</returns>
|
||||
public AIOperationProviderChoice PreviewProvider(PluginAuditRequest? request = null)
|
||||
{
|
||||
var requiredConfidence = request is null
|
||||
? ConfidenceLevel.NONE
|
||||
: auditOperation.GetRequiredConfidence(request);
|
||||
|
||||
return providerResolver.Preview(PluginAuditOperation.GetProviderSettings(settingsManager), requiredConfidence, GetProviderOptions(settingsManager, null));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// What an audit adds to the choice of its provider.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Without a provider of its own, an audit falls back on the app-wide default, and then on the provider at
|
||||
/// hand. A provider somebody picked for something else -- the assistant they were building, the revision
|
||||
/// they asked for, the check they are standing in front of -- may read the source of a plugin only when it
|
||||
/// clears the bar of the audit. Otherwise a provider an organization ruled out for audits would see the very
|
||||
/// thing it was ruled out for.
|
||||
/// </remarks>
|
||||
/// <param name="settingsManager">The settings, for the app-wide default.</param>
|
||||
/// <param name="providerAtHand">The provider at hand, or null.</param>
|
||||
/// <returns>The options for the provider resolver.</returns>
|
||||
internal static AIOperationProviderOptions GetProviderOptions(SettingsManager settingsManager, Settings.Provider? providerAtHand) => new()
|
||||
{
|
||||
FallbackProviderIds = [settingsManager.AppDefaultProviderId, providerAtHand?.Id],
|
||||
OwnMinimumConfidenceAppliesToFallbacks = true,
|
||||
};
|
||||
|
||||
/// <summary>
|
||||
/// The issue which explains best why no provider can audit: the one of the provider somebody named.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// The first named fallback is the app-wide default, or the provider at hand when there is no default.
|
||||
/// When no fallback was named at all, the preferred provider of the audit is what failed.
|
||||
/// </remarks>
|
||||
/// <param name="choice">The choice without a provider.</param>
|
||||
/// <returns>The issue to explain; NOT_SET when nobody named any provider.</returns>
|
||||
internal static AIOperationProviderIssue GetRelevantIssue(AIOperationProviderChoice choice) => choice.FallbackIssue is AIOperationProviderIssue.NOT_SET
|
||||
? choice.PreferredIssue
|
||||
: choice.FallbackIssue;
|
||||
|
||||
private static string GetMissingProviderMessage(AIOperationProviderIssue issue) => issue switch
|
||||
{
|
||||
AIOperationProviderIssue.NOT_SET or AIOperationProviderIssue.MISSING => TB("No provider is configured for security checks."),
|
||||
AIOperationProviderIssue.PROHIBITED => TA("Your organization does not allow the provider for security checks. Pick another one, or choose a dedicated provider for security checks in the app settings."),
|
||||
_ => TA("The selected provider is not trusted enough for security checks. Pick one which meets the confidence required here, or choose a dedicated provider for security checks in the app settings."),
|
||||
};
|
||||
|
||||
private static string GetAutomaticAuditMessage(AIOperationProviderIssue issue) => issue switch
|
||||
{
|
||||
AIOperationProviderIssue.NOT_SET or AIOperationProviderIssue.MISSING => TA("AI Studio did not audit new or updated assistant plugins automatically, because no model is set for security checks, and there is no app-wide default either. Choose a model for security checks in the settings."),
|
||||
AIOperationProviderIssue.PROHIBITED => TA("AI Studio did not audit new or updated assistant plugins automatically, because your organization does not allow the model for security checks. Choose another one in the settings."),
|
||||
_ => TA("AI Studio did not audit new or updated assistant plugins automatically, because the model for security checks is not trusted enough. Choose another one in the settings."),
|
||||
};
|
||||
|
||||
private static string GetMissingProviderSummary(AIOperationProviderIssue issue) => issue switch
|
||||
{
|
||||
AIOperationProviderIssue.NOT_SET or AIOperationProviderIssue.MISSING => TB("No audit provider is configured."),
|
||||
AIOperationProviderIssue.PROHIBITED => TB("The organization does not allow the provider for security checks."),
|
||||
_ => TB("The provider is not trusted enough for security checks."),
|
||||
};
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
using AIStudio.Agents.AssistantAudit;
|
||||
using AIStudio.AIOperations.PluginAudit;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem.Assistants;
|
||||
|
||||
@@ -9,9 +9,9 @@ public sealed class PluginAssistantAudit
|
||||
public DateTimeOffset AuditedAtUtc { get; set; }
|
||||
public string AuditProviderId { get; set; } = string.Empty;
|
||||
public string AuditProviderName { get; set; } = string.Empty;
|
||||
public AssistantAuditLevel Level { get; set; } = AssistantAuditLevel.UNKNOWN;
|
||||
public PluginAuditLevel Level { get; set; } = PluginAuditLevel.UNKNOWN;
|
||||
public string Summary { get; set; } = string.Empty;
|
||||
public float Confidence { get; set; }
|
||||
public string PromptPreview { get; set; } = string.Empty;
|
||||
public List<AssistantAuditFinding> Findings { get; set; } = [];
|
||||
public List<PluginAuditFinding> Findings { get; set; } = [];
|
||||
}
|
||||
+8
-8
@@ -1,4 +1,4 @@
|
||||
using AIStudio.Agents.AssistantAudit;
|
||||
using AIStudio.AIOperations.PluginAudit;
|
||||
using AIStudio.Settings;
|
||||
|
||||
namespace AIStudio.Tools.PluginSystem.Assistants;
|
||||
@@ -111,8 +111,8 @@ public static class PluginAssistantSecurityResolver
|
||||
CanActivatePlugin = true,
|
||||
CanStartAssistant = true,
|
||||
AuditLabel = TB("Safe"),
|
||||
AuditColor = AssistantAuditLevel.SAFE.Color,
|
||||
AuditIcon = AssistantAuditLevel.SAFE.Icon,
|
||||
AuditColor = PluginAuditLevel.SAFE.Color,
|
||||
AuditIcon = PluginAuditLevel.SAFE.Icon,
|
||||
AvailabilityLabel = GetAvailabilityLabel(requiresAudit: false, hasAudit: true, hasHashMismatch: false, isBlocked: false, canOverride: false),
|
||||
AvailabilityColor = GetAvailabilityColor(requiresAudit: false, hasAudit: true, hasHashMismatch: false, isBlocked: false, canOverride: false),
|
||||
AvailabilityIcon = GetAvailabilityIcon(requiresAudit: false, hasAudit: true, hasHashMismatch: false, isBlocked: false, canOverride: false),
|
||||
@@ -130,7 +130,7 @@ public static class PluginAssistantSecurityResolver
|
||||
}
|
||||
|
||||
var audit = settingsManager.ConfigurationData.AssistantPluginAudits.FirstOrDefault(x => x.PluginId == plugin.Id);
|
||||
var hasAudit = audit is not null && audit.Level is not AssistantAuditLevel.UNKNOWN;
|
||||
var hasAudit = audit is not null && audit.Level is not PluginAuditLevel.UNKNOWN;
|
||||
var hashMatches = hasAudit && string.Equals(NormalizeHash(audit!.PluginHash), currentHash, StringComparison.Ordinal);
|
||||
var hasHashMismatch = hasAudit && !hashMatches;
|
||||
var isBelowMinimum = hashMatches && audit is not null && audit.Level < auditSettings.MinimumLevel;
|
||||
@@ -159,8 +159,8 @@ public static class PluginAssistantSecurityResolver
|
||||
CanActivatePlugin = !isBlocked,
|
||||
CanStartAssistant = !isBlocked,
|
||||
AuditLabel = TB("Unknown"),
|
||||
AuditColor = AssistantAuditLevel.UNKNOWN.Color,
|
||||
AuditIcon = AssistantAuditLevel.UNKNOWN.Icon,
|
||||
AuditColor = PluginAuditLevel.UNKNOWN.Color,
|
||||
AuditIcon = PluginAuditLevel.UNKNOWN.Icon,
|
||||
AvailabilityLabel = GetAvailabilityLabel(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
AvailabilityColor = GetAvailabilityColor(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
AvailabilityIcon = GetAvailabilityIcon(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
@@ -198,8 +198,8 @@ public static class PluginAssistantSecurityResolver
|
||||
CanActivatePlugin = !isBlocked,
|
||||
CanStartAssistant = !isBlocked,
|
||||
AuditLabel = TB("Unknown"),
|
||||
AuditColor = AssistantAuditLevel.UNKNOWN.Color,
|
||||
AuditIcon = AssistantAuditLevel.UNKNOWN.Icon,
|
||||
AuditColor = PluginAuditLevel.UNKNOWN.Color,
|
||||
AuditIcon = PluginAuditLevel.UNKNOWN.Icon,
|
||||
AvailabilityLabel = GetAvailabilityLabel(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
AvailabilityColor = GetAvailabilityColor(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
AvailabilityIcon = GetAvailabilityIcon(requiresAudit, hasAudit, hasHashMismatch, isBlocked, canOverride: false),
|
||||
|
||||
@@ -408,19 +408,27 @@ public sealed class PluginConfiguration(bool isInternal, LuaState state, PluginT
|
||||
&& maximumPauseSeconds < minimumPauseSeconds)
|
||||
LOG.LogWarning("The upper bound of the pause between indexing steps configured by plugin {ConfigPluginId} is below its lower bound. The lower bound of {MinimumPauseSeconds} seconds wins.", this.Id, minimumPauseSeconds);
|
||||
|
||||
// Config: data source selection agent settings
|
||||
// Config: text content cleaner settings
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.TextContentCleaner, x => x.PreselectAgentOptions, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.TextContentCleaner, x => x.PreselectedAgentProvider, Guid.Empty, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.TextContentCleaner, x => x.MinimumProviderConfidence, this.Id, settingsTable, dryRun);
|
||||
|
||||
// Config: data source selection settings
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentDataSourceSelection, x => x.PreselectAgentOptions, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentDataSourceSelection, x => x.PreselectedAgentProvider, Guid.Empty, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentDataSourceSelection, x => x.MinimumProviderConfidence, this.Id, settingsTable, dryRun);
|
||||
|
||||
// Config: retrieval context validation agent settings
|
||||
// Config: retrieval context validation settings
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentRetrievalContextValidation, x => x.EnableRetrievalContextValidation, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentRetrievalContextValidation, x => x.PreselectAgentOptions, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentRetrievalContextValidation, x => x.PreselectedAgentProvider, Guid.Empty, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentRetrievalContextValidation, x => x.MinimumProviderConfidence, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AgentRetrievalContextValidation, x => x.NumParallelValidations, this.Id, settingsTable, dryRun);
|
||||
|
||||
// Config: assistant plugin audit settings
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.RequireAuditBeforeActivation, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.PreselectedAgentProvider, Guid.Empty, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.MinimumProviderConfidence, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.MinimumLevel, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.BlockActivationBelowMinimum, this.Id, settingsTable, dryRun);
|
||||
ManagedConfiguration.TryProcessConfiguration(x => x.AssistantPluginAudit, x => x.AutomaticallyAuditAssistants, this.Id, settingsTable, dryRun);
|
||||
|
||||
Reference in new issue
Block a user