using AIStudio.Provider; using AIStudio.Tools.ToolCallingSystem; using Microsoft.AspNetCore.Components; namespace AIStudio.Components; /// /// Says when tools an assistant was told to use cannot reach the selected provider. /// /// /// Whoever named these tools — a document analysis policy, an assistant plugin — did so without /// knowing which provider the user would pick. The user cannot switch a blocked tool on either, /// because there is no selection to switch. Saying nothing would let the run quietly proceed /// without them, which is why this belongs next to the provider selection: choosing another /// provider is what resolves it. /// public partial class ManagedToolsWarning : MSGComponentBase { [Parameter] public AIStudio.Tools.Components Component { get; set; } = AIStudio.Tools.Components.CHAT; /// /// The tools of this run, as named by the assistant's own rules. /// [Parameter] public IReadOnlySet ToolIds { get; set; } = new HashSet(); [Parameter] public AIStudio.Settings.Provider ProviderSettings { get; set; } = AIStudio.Settings.Provider.NONE; [Parameter] public string Class { get; set; } = "mb-3"; [Inject] private ToolRegistry ToolRegistry { get; init; } = null!; private IReadOnlyList availableTools = []; /// /// Whether this run expects tools while the selected provider cannot call any. /// private bool NeedsToolCallingProvider => this.ToolIds.Count > 0 && this.SettingsManager.AreToolsEnabled() && !this.ProviderSettings.GetToolCallingAvailability().IsAvailable; /// /// The tools of this run whose settings are incomplete, so they cannot run at all. /// /// /// Unlike the confidence case, no provider resolves this: the tool itself is missing something, /// such as the web search without a server address. Tools an organization switched off are left /// out, because completing their settings would not bring them back either. /// private IReadOnlyList ToolsNeedingConfiguration { get { if (this.ToolIds.Count is 0 || !this.SettingsManager.AreToolsEnabled()) return []; return this.availableTools .Where(x => this.ToolIds.Contains(x.Definition.Id) && x.IsActive && !x.ConfigurationState.IsConfigured) .Select(x => x.Implementation.GetDisplayName()) .ToList(); } } /// /// The tools of this run which the selected provider is not trusted enough to receive. /// /// /// Tools switched off in the settings are not counted: choosing another provider would not /// bring them back, so naming them here would send the user after the wrong fix. /// private IReadOnlyList ToolsBeyondProviderConfidence { get { if (this.ToolIds.Count is 0 || !this.SettingsManager.AreToolsEnabled()) return []; var providerConfidence = this.ProviderSettings == AIStudio.Settings.Provider.NONE ? ConfidenceLevel.NONE : this.ProviderSettings.UsedLLMProvider.GetConfidence(this.SettingsManager).Level; return this.availableTools .Where(x => this.ToolIds.Contains(x.Definition.Id) && x.IsActive) .Where(x => !ToolSelectionRules.IsProviderConfidenceAllowed(providerConfidence, x.MinimumProviderConfidence)) .Select(x => x.Implementation.GetDisplayName()) .ToList(); } } protected override async Task OnInitializedAsync() { this.availableTools = await this.ToolRegistry.GetCatalogAsync(this.Component); this.ApplyFilters([], [ Event.CONFIGURATION_CHANGED ]); await base.OnInitializedAsync(); } protected override async Task ProcessIncomingMessage(ComponentBase? sendingComponent, Event triggeredEvent, T? data) where T : default { switch (triggeredEvent) { case Event.CONFIGURATION_CHANGED: this.availableTools = await this.ToolRegistry.GetCatalogAsync(this.Component); await this.InvokeAsync(this.StateHasChanged); break; } } }