using AIStudio.AIOperations; using AIStudio.AIOperations.TextContentCleaning; using AIStudio.Components; using AIStudio.Settings; using AIStudio.Tools.Imports; using AIStudio.Tools.Security; namespace AIStudio.Tools.Web; /// /// Loads web content for the web content readers of assistants, in the background. /// /// /// A reader goes away with its assistant when the user leaves it, while loading and above all cleaning a page /// can take a minute. So the work runs here, keyed by the field it fills, see ImportTarget, and its result /// waits until the reader of that field is there again, see TryTakeResult. This follows the transcription of /// media files, see MediaTranscriptionService, with one difference: there is no common queue. Each field has /// its own import, so several assistants, or several readers of an assistant plugin, load at the same time. /// Only the same field cannot start a second import while its first one runs. /// public sealed class WebContentImportService( WebPageRetrievalService webPageRetrievalService, PromptInjectionGuardService promptInjectionGuardService, TextContentCleaningOperation textContentCleaning, AIOperationProviderResolver providerResolver, SettingsManager settingsManager, ILogger logger) { /// /// How long loading one page may take. /// /// /// The user waits for this text, so it is shorter than what the tools allow themselves for the /// pages they fetch. /// private const int TIMEOUT_SECONDS = 60; /// Protects the running imports and the waiting results. private readonly Lock stateLock = new(); /// The state of each running import. private readonly Dictionary runningImports = []; /// Results waiting for the reader of their field. private readonly Dictionary results = []; /// Raised with the owner whose imports changed: one started, went on to its next step, or ended. public event Action? StateChanged; /// /// What the reader adds to the choice of the provider for the content cleaner. /// /// /// Without a model of its own, the cleaner falls back on the model of the assistant, and then on /// the app-wide default. Its minimum confidence applies to both of them: a model somebody picked /// for the assistant has to clear the bar of the cleaner before the cleaner uses it. /// /// The settings, for the app-wide default. /// The model of the assistant around the reader. /// The options for the provider resolver. internal static AIOperationProviderOptions GetCleanerProviderOptions(SettingsManager settingsManager, AIStudio.Settings.Provider assistantProvider) => new() { FallbackProviderIds = [assistantProvider.Id, settingsManager.AppDefaultProviderId], OwnMinimumConfidenceAppliesToFallbacks = true, }; /// Gets whether one of the fields of an owner is loading web content. /// The owner, e.g., an assistant. /// true while an import of the owner runs. public bool IsBusy(ImportOwner owner) { lock (this.stateLock) return this.runningImports.Keys.Any(target => target.Owner == owner); } /// Gets the state of the import which fills a field, while it runs. /// The field. /// A copy of the state, or null when no import of the field runs. public WebContentImportSnapshot? GetSnapshot(ImportTarget target) { lock (this.stateLock) return this.runningImports.GetValueOrDefault(target); } /// Gets copies of the states of all running imports, e.g., for the navigation. public IReadOnlyCollection GetSnapshots() { lock (this.stateLock) return [.. this.runningImports.Values]; } /// /// Starts loading web content into a field, unless an import of that field still runs. /// /// The field to fill. /// What to load. /// true when the import started. public bool TryStart(ImportTarget target, WebContentImportRequest request) => this.Start(target, request, reportStep => this.ImportAsync(request, reportStep)) is not null; /// /// Starts an import into a field, unless an import of that field still runs. /// /// /// The import runs on the thread pool, not on the circuit of the reader which started it: the reader may /// go away any moment. A new import replaces a result which its reader did not take yet. Tests hand in an /// import of their own. /// /// The field to fill. /// What the import loads, for its state and for the result of an import which failed unexpectedly. /// The work, which reports each step it reaches. /// The running import, or null when an import of the field still runs. internal Task? Start(ImportTarget target, WebContentImportRequest request, Func, Task> import) { lock (this.stateLock) { if (!this.runningImports.TryAdd(target, new(target, request.Url, request.CleanContent, ReadWebContentSteps.LOADING))) return null; this.results.Remove(target); } this.NotifyStateChanged(target.Owner); return Task.Run(() => this.RunAsync(target, request.Url, import)); } /// /// Takes the result which waits for a field, so that it is delivered exactly once. /// /// The field. /// The result, or null when none waits. public WebContentImportResult? TryTakeResult(ImportTarget target) { lock (this.stateLock) return this.results.Remove(target, out var result) ? result : null; } /// /// Discards the results which wait for the fields of an owner, e.g., when the user resets the assistant. /// /// /// A running import is left alone, as with media files: the assistant does not reset while one runs. /// /// The owner. public void ClearOwnerState(ImportOwner owner) { lock (this.stateLock) { foreach (var target in this.results.Keys.Where(target => target.Owner == owner).ToList()) this.results.Remove(target); } this.NotifyStateChanged(owner); } private async Task RunAsync(ImportTarget target, string url, Func, Task> import) { WebContentImportResult result; try { result = await import(step => this.ReportStep(target, step)); } catch (Exception exception) { logger.LogWarning(exception, "Could not load the web content from '{ProvidedUrl}'.", url); result = new(url, string.Empty, WebContentImportIssue.LOAD_FAILED, exception.Message); } lock (this.stateLock) { this.runningImports.Remove(target); this.results[target] = result; } this.NotifyStateChanged(target.Owner); } private void ReportStep(ImportTarget target, ReadWebContentSteps step) { lock (this.stateLock) { if (!this.runningImports.TryGetValue(target, out var snapshot)) return; this.runningImports[target] = snapshot with { Step = step }; } this.NotifyStateChanged(target.Owner); } /// /// Loads a page, filters it for prompt injections, and cleans it if asked to. /// /// /// Never throws: the content which arrived before an error goes into the result, together with the error. /// private async Task ImportAsync(WebContentImportRequest request, Action reportStep) { var markdown = string.Empty; try { // // The same retrieval the read web page tool uses, so a page is fetched and read one // way throughout AI Studio. The difference is the target policy: here the user typed // the URL, so their own network is not off limits. // var retrievedPage = await webPageRetrievalService.RetrieveAsync( new Uri(request.Url), new WebPageRetrievalOptions { TimeoutSeconds = TIMEOUT_SECONDS, TargetChosenByUser = true, }); reportStep(ReadWebContentSteps.PARSING); markdown = await promptInjectionGuardService.SanitizeAsync(retrievedPage.ExtractedPage.Markdown, PromptInjectionSource.WebContent(request.Url)); if (!request.CleanContent) return new(request.Url, markdown); var cleaningRequest = new TextContentCleaningRequest(markdown, request.Url); var choice = providerResolver.Resolve(textContentCleaning, cleaningRequest, TextContentCleaningOperation.GetProviderSettings(settingsManager), GetCleanerProviderOptions(settingsManager, request.AssistantProvider)); if (!choice.HasProvider) return new(request.Url, markdown, WebContentImportIssue.NOT_CLEANED_NO_MODEL); reportStep(ReadWebContentSteps.CLEANING); var cleaningResult = await textContentCleaning.RunAsync(cleaningRequest, new(choice.Provider)); return new(request.Url, cleaningResult.Markdown, cleaningResult.IsCleaned ? WebContentImportIssue.NONE : WebContentImportIssue.NOT_CLEANED_NO_ANSWER); } catch (Exception exception) { logger.LogWarning(exception, "Could not load the web content from '{ProvidedUrl}'.", request.Url); return new(request.Url, markdown, WebContentImportIssue.LOAD_FAILED, exception.Message); } } /// /// Tells every handler about a change, so that a handler which throws keeps none of the others from hearing about it. /// private void NotifyStateChanged(ImportOwner owner) { if (this.StateChanged is not { } stateChanged) return; foreach (var @delegate in stateChanged.GetInvocationList()) { try { ((Action)@delegate)(owner); } catch (Exception exception) { logger.LogError(exception, "A handler failed while it was told about a changed web content import."); } } } }