| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.AI.Abstractions.Models; |
| | | 3 | | using Elsa.AI.Host.Options; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.AI.Host.Services; |
| | | 7 | | |
| | 50 | 8 | | public class AIGroundingResultFormatter(IOptions<AIHostOptions> options) |
| | | 9 | | { |
| | 2 | 10 | | private static readonly string[] SensitiveNameFragments = |
| | 2 | 11 | | [ |
| | 2 | 12 | | "password", |
| | 2 | 13 | | "secret", |
| | 2 | 14 | | "token", |
| | 2 | 15 | | "apikey", |
| | 2 | 16 | | "api_key", |
| | 2 | 17 | | "authorization", |
| | 2 | 18 | | "connectionstring", |
| | 2 | 19 | | "connection_string", |
| | 2 | 20 | | "credential" |
| | 2 | 21 | | ]; |
| | | 22 | | |
| | | 23 | | public AIToolResult CreateResult(string summary, IEnumerable<JsonObject> items, int total, IEnumerable<string>? evid |
| | | 24 | | { |
| | 7 | 25 | | var maxItems = Math.Max(0, options.Value.Grounding.MaxItems); |
| | 7 | 26 | | var itemList = items |
| | 7 | 27 | | .Take(maxItems) |
| | 7 | 28 | | .Select(x => RedactObject((JsonObject)x.DeepClone())) |
| | 7 | 29 | | .ToList(); |
| | 7 | 30 | | var truncated = total > itemList.Count; |
| | 7 | 31 | | var groundingResult = new AIGroundingToolResult |
| | 7 | 32 | | { |
| | 7 | 33 | | Summary = summary, |
| | 7 | 34 | | Items = itemList, |
| | 7 | 35 | | Total = total, |
| | 7 | 36 | | Returned = itemList.Count, |
| | 7 | 37 | | Truncated = truncated, |
| | 5 | 38 | | Evidence = evidence?.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.OrdinalIgnoreCase).To |
| | 0 | 39 | | Warnings = warnings?.Where(x => !string.IsNullOrWhiteSpace(x)).Distinct(StringComparer.OrdinalIgnoreCase).To |
| | 7 | 40 | | }; |
| | | 41 | | |
| | 7 | 42 | | var data = AIGroundingJson.ToJsonObject(groundingResult); |
| | 7 | 43 | | return new AIToolResult |
| | 7 | 44 | | { |
| | 7 | 45 | | Summary = truncated ? $"{summary} Returned {itemList.Count} of {total} results." : summary, |
| | 7 | 46 | | Data = LimitData(data) |
| | 7 | 47 | | }; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | public AIToolResult Unavailable(string sourceName) => |
| | 0 | 51 | | new() |
| | 0 | 52 | | { |
| | 0 | 53 | | Status = AIToolInvocationStatus.Failed, |
| | 0 | 54 | | Summary = $"{sourceName} is not available in this Elsa host.", |
| | 0 | 55 | | Error = $"{sourceName} is not available.", |
| | 0 | 56 | | Data = new JsonObject |
| | 0 | 57 | | { |
| | 0 | 58 | | ["available"] = false, |
| | 0 | 59 | | ["source"] = sourceName |
| | 0 | 60 | | } |
| | 0 | 61 | | }; |
| | | 62 | | |
| | | 63 | | public JsonObject RedactObject(JsonObject source) |
| | | 64 | | { |
| | 18 | 65 | | var redacted = new JsonObject(); |
| | 230 | 66 | | foreach (var (key, value) in source) |
| | 97 | 67 | | redacted[key] = IsSensitiveKey(key) ? "***" : RedactNode(value); |
| | | 68 | | |
| | 18 | 69 | | return redacted; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public JsonObject LimitData(JsonObject data) |
| | | 73 | | { |
| | 7 | 74 | | var maxBytes = Math.Max(0, options.Value.Grounding.MaxResultBytes); |
| | 7 | 75 | | if (maxBytes == 0 || JsonSerializer.SerializeToUtf8Bytes(data).Length <= maxBytes) |
| | 7 | 76 | | return data; |
| | | 77 | | |
| | 0 | 78 | | return new JsonObject |
| | 0 | 79 | | { |
| | 0 | 80 | | ["truncated"] = true, |
| | 0 | 81 | | ["maxBytes"] = maxBytes |
| | 0 | 82 | | }; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | private JsonNode? RedactNode(JsonNode? node) => |
| | 101 | 86 | | node switch |
| | 101 | 87 | | { |
| | 10 | 88 | | JsonObject jsonObject => RedactObject(jsonObject), |
| | 19 | 89 | | JsonArray jsonArray => RedactArray(jsonArray), |
| | 11 | 90 | | null => null, |
| | 61 | 91 | | _ => node.DeepClone() |
| | 101 | 92 | | }; |
| | | 93 | | |
| | | 94 | | private JsonArray RedactArray(JsonArray source) |
| | | 95 | | { |
| | 19 | 96 | | var redacted = new JsonArray(); |
| | 52 | 97 | | foreach (var value in source) |
| | 7 | 98 | | redacted.Add(RedactNode(value)); |
| | | 99 | | |
| | 19 | 100 | | return redacted; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | private static bool IsSensitiveKey(string key) |
| | | 104 | | { |
| | 97 | 105 | | var normalized = key.Replace("-", "", StringComparison.Ordinal).Replace("_", "", StringComparison.Ordinal); |
| | 949 | 106 | | return SensitiveNameFragments.Any(x => normalized.Contains(x, StringComparison.OrdinalIgnoreCase)); |
| | | 107 | | } |
| | | 108 | | } |