| | | 1 | | using System.Text.RegularExpressions; |
| | | 2 | | using Elsa.AI.Abstractions.Contracts; |
| | | 3 | | using Elsa.AI.Abstractions.Models; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace Elsa.AI.Host.Context; |
| | | 8 | | |
| | 47 | 9 | | public class AIContextResolver(IServiceScopeFactory scopeFactory, ILogger<AIContextResolver> logger) |
| | | 10 | | { |
| | 2 | 11 | | private static readonly string[] SensitiveKeyFragments = ["secret", "token", "password", "apikey", "api_key", "api-k |
| | 2 | 12 | | private static readonly Regex[] SensitiveValuePatterns = |
| | 2 | 13 | | [ |
| | 2 | 14 | | new(@"\bBearer\s+[A-Za-z0-9._~+/\-=]+", RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.C |
| | 2 | 15 | | new(@"\b(?:api[_-]?key|token|secret|password)\s*[:=]\s*['""]?[A-Za-z0-9._~+/\-=]{8,}['""]?", RegexOptions.Ignore |
| | 2 | 16 | | ]; |
| | | 17 | | private const string Redacted = "[redacted]"; |
| | | 18 | | |
| | | 19 | | public async ValueTask<IReadOnlyCollection<AIResolvedContext>> ResolveAsync(AIChatRequest request, CancellationToken |
| | | 20 | | { |
| | 44 | 21 | | using var scope = scopeFactory.CreateScope(); |
| | 44 | 22 | | var providers = BuildProviders(scope.ServiceProvider.GetServices<IAIContextProvider>()); |
| | 44 | 23 | | var resolved = new List<AIResolvedContext>(); |
| | 44 | 24 | | var attachmentsWithProviders = request.Attachments |
| | 15 | 25 | | .Select(attachment => new |
| | 15 | 26 | | { |
| | 15 | 27 | | Attachment = attachment, |
| | 15 | 28 | | Provider = providers.GetValueOrDefault(attachment.Kind) |
| | 15 | 29 | | }) |
| | 59 | 30 | | .Where(x => x.Provider != null); |
| | | 31 | | |
| | 117 | 32 | | foreach (var item in attachmentsWithProviders) |
| | | 33 | | { |
| | 15 | 34 | | if (item.Provider is IPlaceholderAIContextProvider) |
| | 1 | 35 | | logger.LogWarning( |
| | 1 | 36 | | "AI context kind {ContextKind} is using placeholder provider {ProviderType}. Replace it before produ |
| | 1 | 37 | | item.Attachment.Kind, |
| | 1 | 38 | | item.Provider.GetType().Name); |
| | | 39 | | |
| | 15 | 40 | | var context = await item.Provider!.ResolveAsync(new AIContextResolutionRequest |
| | 15 | 41 | | { |
| | 15 | 42 | | Attachment = item.Attachment, |
| | 15 | 43 | | TenantId = request.TenantId, |
| | 15 | 44 | | UserId = request.UserId |
| | 15 | 45 | | }, cancellationToken); |
| | | 46 | | |
| | 14 | 47 | | resolved.Add(Redact(context)); |
| | | 48 | | } |
| | | 49 | | |
| | 43 | 50 | | return resolved; |
| | 43 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static AIResolvedContext Redact(AIResolvedContext context) => |
| | 14 | 54 | | context with |
| | 14 | 55 | | { |
| | 14 | 56 | | Summary = RedactText(context.Summary), |
| | 14 | 57 | | Data = RedactObject(context.Data), |
| | 14 | 58 | | Metadata = RedactObject(context.Metadata) |
| | 14 | 59 | | }; |
| | | 60 | | |
| | | 61 | | private static JsonObject RedactObject(JsonObject source) |
| | | 62 | | { |
| | 28 | 63 | | var redacted = new JsonObject(); |
| | | 64 | | |
| | 82 | 65 | | foreach (var property in source) |
| | 13 | 66 | | redacted[property.Key] = IsSensitiveKey(property.Key) ? Redacted : RedactNode(property.Value); |
| | | 67 | | |
| | 28 | 68 | | return redacted; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | private static JsonNode? RedactNode(JsonNode? node) => |
| | 11 | 72 | | node switch |
| | 11 | 73 | | { |
| | 0 | 74 | | JsonObject jsonObject => RedactObject(jsonObject), |
| | 0 | 75 | | JsonArray jsonArray => new JsonArray(jsonArray.Select(RedactNode).ToArray()), |
| | 20 | 76 | | JsonValue jsonValue when jsonValue.TryGetValue<string>(out var value) => JsonValue.Create(RedactText(value)) |
| | 0 | 77 | | JsonValue jsonValue => jsonValue.DeepClone(), |
| | 1 | 78 | | _ => node?.DeepClone() |
| | 11 | 79 | | }; |
| | | 80 | | |
| | | 81 | | private static string RedactText(string text) |
| | | 82 | | { |
| | 24 | 83 | | if (string.IsNullOrWhiteSpace(text)) |
| | 0 | 84 | | return text; |
| | | 85 | | |
| | 72 | 86 | | return SensitiveValuePatterns.Aggregate(text, (current, pattern) => pattern.Replace(current, Redacted)); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | private static bool IsSensitiveKey(string key) => |
| | 118 | 90 | | SensitiveKeyFragments.Any(fragment => key.Contains(fragment, StringComparison.OrdinalIgnoreCase)); |
| | | 91 | | |
| | | 92 | | private static Dictionary<string, IAIContextProvider> BuildProviders(IEnumerable<IAIContextProvider> providers) |
| | | 93 | | { |
| | 44 | 94 | | return providers |
| | 92 | 95 | | .GroupBy(x => x.Kind, StringComparer.OrdinalIgnoreCase) |
| | 134 | 96 | | .ToDictionary(x => x.Key, SelectProvider, StringComparer.OrdinalIgnoreCase); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | private static IAIContextProvider SelectProvider(IEnumerable<IAIContextProvider> providers) |
| | | 100 | | { |
| | 90 | 101 | | var providerList = providers.ToList(); |
| | 181 | 102 | | return providerList.LastOrDefault(x => x is not IPlaceholderAIContextProvider) ?? providerList.Last(); |
| | | 103 | | } |
| | | 104 | | } |