| | | 1 | | using System.Collections.Concurrent; |
| | | 2 | | using Elsa.AI.Abstractions.Contracts; |
| | | 3 | | using Elsa.AI.Abstractions.Models; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace Elsa.AI.Host.Services; |
| | | 7 | | |
| | 60 | 8 | | public class AIToolRegistry(IServiceScopeFactory scopeFactory, AIToolEnablementService enablementService) : IAIToolRegis |
| | | 9 | | { |
| | 60 | 10 | | private readonly object _definitionCacheLock = new(); |
| | 60 | 11 | | private readonly ConcurrentDictionary<string, Type> _toolTypes = new(StringComparer.OrdinalIgnoreCase); |
| | 60 | 12 | | private readonly ConcurrentDictionary<string, bool> _uncacheableToolNames = new(StringComparer.OrdinalIgnoreCase); |
| | 60 | 13 | | private readonly ConcurrentDictionary<Type, bool> _uncacheableToolTypes = new(); |
| | | 14 | | private volatile IReadOnlyCollection<AIToolDefinition>? _definitions; |
| | | 15 | | |
| | | 16 | | public ValueTask<IReadOnlyCollection<AIToolDefinition>> ListAsync(AIToolQuery query, CancellationToken cancellationT |
| | | 17 | | { |
| | 57 | 18 | | var definitions = GetCachedDefinitions() |
| | 39 | 19 | | .Where(x => IsVisible(x, query)) |
| | 27 | 20 | | .Select(x => x with { IsEnabled = enablementService.IsEnabled(x) }) |
| | 57 | 21 | | .ToList(); |
| | | 22 | | |
| | 57 | 23 | | return ValueTask.FromResult<IReadOnlyCollection<AIToolDefinition>>(definitions); |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | public ValueTask<IAITool?> FindAsync(string name, AIToolQuery query, CancellationToken cancellationToken = default) |
| | | 27 | | { |
| | 21 | 28 | | var scope = scopeFactory.CreateScope(); |
| | 21 | 29 | | ResolvedTool? resolvedTool = null; |
| | | 30 | | try |
| | | 31 | | { |
| | 21 | 32 | | resolvedTool = ResolveCachedTool(scope.ServiceProvider, name) ?? ResolveAndCacheTool(scope.ServiceProvider, |
| | 21 | 33 | | if (resolvedTool == null || !TryGetDefinition(resolvedTool.Tool, out var definition) || !IsVisible(definitio |
| | | 34 | | { |
| | 4 | 35 | | resolvedTool?.Dispose(); |
| | 4 | 36 | | scope.Dispose(); |
| | 4 | 37 | | return ValueTask.FromResult<IAITool?>(null); |
| | | 38 | | } |
| | | 39 | | |
| | 17 | 40 | | return ValueTask.FromResult<IAITool?>(new ScopedAITool(scope, resolvedTool.Tool, resolvedTool.DisposeTool)); |
| | | 41 | | } |
| | 0 | 42 | | catch |
| | | 43 | | { |
| | 0 | 44 | | resolvedTool?.Dispose(); |
| | 0 | 45 | | scope.Dispose(); |
| | 0 | 46 | | throw; |
| | | 47 | | } |
| | 21 | 48 | | } |
| | | 49 | | |
| | | 50 | | private ResolvedTool? ResolveCachedTool(IServiceProvider serviceProvider, string name) |
| | | 51 | | { |
| | 21 | 52 | | if (!_toolTypes.TryGetValue(name, out var toolType)) |
| | 4 | 53 | | return null; |
| | | 54 | | |
| | | 55 | | ResolvedTool resolvedTool; |
| | | 56 | | try |
| | | 57 | | { |
| | 17 | 58 | | if (serviceProvider.GetService(toolType) is IAITool scopeOwnedTool) |
| | 0 | 59 | | resolvedTool = new ResolvedTool(scopeOwnedTool, DisposeTool: false); |
| | 17 | 60 | | else if (ActivatorUtilities.CreateInstance(serviceProvider, toolType) is IAITool createdTool) |
| | 15 | 61 | | resolvedTool = new ResolvedTool(createdTool, DisposeTool: true); |
| | | 62 | | else |
| | 0 | 63 | | return null; |
| | 15 | 64 | | } |
| | 2 | 65 | | catch (InvalidOperationException) |
| | | 66 | | { |
| | 2 | 67 | | _uncacheableToolNames[name] = true; |
| | 2 | 68 | | _toolTypes.TryRemove(name, out _); |
| | 2 | 69 | | return null; |
| | | 70 | | } |
| | | 71 | | |
| | 15 | 72 | | if (!TryGetDefinition(resolvedTool.Tool, out var definition)) |
| | | 73 | | { |
| | 0 | 74 | | resolvedTool.Dispose(); |
| | 0 | 75 | | _uncacheableToolNames[name] = true; |
| | 0 | 76 | | _toolTypes.TryRemove(name, out _); |
| | 0 | 77 | | return null; |
| | | 78 | | } |
| | | 79 | | |
| | 15 | 80 | | if (string.Equals(definition.Name, name, StringComparison.OrdinalIgnoreCase)) |
| | 15 | 81 | | return resolvedTool; |
| | | 82 | | |
| | 0 | 83 | | resolvedTool.Dispose(); |
| | 0 | 84 | | return null; |
| | 2 | 85 | | } |
| | | 86 | | |
| | | 87 | | private ResolvedTool? ResolveAndCacheTool(IServiceProvider serviceProvider, string name) |
| | | 88 | | { |
| | 6 | 89 | | var tools = serviceProvider.GetServices<IAITool>().ToList(); |
| | 6 | 90 | | var toolInfos = GetToolInfos(tools); |
| | 6 | 91 | | UpdateToolTypeCache(toolInfos); |
| | 11 | 92 | | var tool = toolInfos.FirstOrDefault(x => string.Equals(x.Definition.Name, name, StringComparison.OrdinalIgnoreCa |
| | 6 | 93 | | return tool == null ? null : new ResolvedTool(tool, DisposeTool: false); |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | private IReadOnlyCollection<AIToolDefinition> GetCachedDefinitions() |
| | | 97 | | { |
| | 57 | 98 | | if (_definitions != null) |
| | 5 | 99 | | return _definitions; |
| | | 100 | | |
| | 52 | 101 | | lock (_definitionCacheLock) |
| | | 102 | | { |
| | 52 | 103 | | if (_definitions != null) |
| | 0 | 104 | | return _definitions; |
| | | 105 | | |
| | 52 | 106 | | using var scope = scopeFactory.CreateScope(); |
| | 52 | 107 | | var tools = scope.ServiceProvider.GetServices<IAITool>().ToList(); |
| | 52 | 108 | | var toolInfos = GetToolInfos(tools); |
| | 52 | 109 | | UpdateToolTypeCache(toolInfos); |
| | | 110 | | // Definitions are cached by design. Runtime enablement is refreshed in ListAsync; dynamic Definition proper |
| | 86 | 111 | | _definitions = toolInfos.Select(x => x.Definition).ToList().AsReadOnly(); |
| | 52 | 112 | | return _definitions; |
| | | 113 | | } |
| | 52 | 114 | | } |
| | | 115 | | |
| | | 116 | | private void UpdateToolTypeCache(IReadOnlyCollection<ToolInfo> toolInfos) |
| | | 117 | | { |
| | 58 | 118 | | var namesByType = toolInfos |
| | 39 | 119 | | .Where(x => !_uncacheableToolTypes.ContainsKey(x.ToolType)) |
| | 39 | 120 | | .Where(x => !string.IsNullOrWhiteSpace(x.Definition.Name)) |
| | 39 | 121 | | .GroupBy(x => x.ToolType) |
| | 155 | 122 | | .ToDictionary(x => x.Key, x => x.Select(tool => tool.Definition.Name).Distinct(StringComparer.OrdinalIgnoreC |
| | | 123 | | |
| | 174 | 124 | | foreach (var (toolType, toolNames) in namesByType) |
| | | 125 | | { |
| | 29 | 126 | | if (toolNames.Count != 1) |
| | | 127 | | { |
| | 44 | 128 | | foreach (var toolName in toolNames) |
| | | 129 | | { |
| | 16 | 130 | | _uncacheableToolNames[toolName] = true; |
| | 16 | 131 | | _toolTypes.TryRemove(toolName, out _); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | continue; |
| | | 135 | | } |
| | | 136 | | |
| | 23 | 137 | | var name = toolNames[0]; |
| | 23 | 138 | | if (_uncacheableToolNames.ContainsKey(name)) |
| | | 139 | | continue; |
| | | 140 | | |
| | 21 | 141 | | _toolTypes[name] = toolType; |
| | | 142 | | } |
| | 58 | 143 | | } |
| | | 144 | | |
| | | 145 | | private IReadOnlyCollection<ToolInfo> GetToolInfos(IReadOnlyCollection<IAITool> tools) |
| | | 146 | | { |
| | 58 | 147 | | return tools |
| | 58 | 148 | | .Select(CreateToolInfo) |
| | 42 | 149 | | .Where(x => x != null) |
| | 40 | 150 | | .Select(x => x!) |
| | 40 | 151 | | .Where(x => !string.IsNullOrWhiteSpace(x.Definition.Name)) |
| | 58 | 152 | | .ToList(); |
| | | 153 | | } |
| | | 154 | | |
| | | 155 | | private ToolInfo? CreateToolInfo(IAITool tool) => |
| | 42 | 156 | | TryGetDefinition(tool, out var definition) |
| | 42 | 157 | | ? new ToolInfo(tool.GetType(), tool, definition) |
| | 42 | 158 | | : null; |
| | | 159 | | |
| | | 160 | | private bool TryGetDefinition(IAITool tool, out AIToolDefinition definition) |
| | | 161 | | { |
| | | 162 | | try |
| | | 163 | | { |
| | 76 | 164 | | definition = tool.Definition; |
| | 74 | 165 | | return true; |
| | | 166 | | } |
| | 2 | 167 | | catch (InvalidOperationException) |
| | | 168 | | { |
| | 2 | 169 | | _uncacheableToolTypes[tool.GetType()] = true; |
| | 2 | 170 | | definition = default!; |
| | 2 | 171 | | return false; |
| | | 172 | | } |
| | 76 | 173 | | } |
| | | 174 | | |
| | | 175 | | private static bool IsVisible(AIToolDefinition definition, AIToolQuery query) => |
| | 58 | 176 | | (query.Mutability == null || definition.Mutability == query.Mutability) && |
| | 58 | 177 | | (query.DangerLevel == null || definition.DangerLevel == query.DangerLevel) && |
| | 58 | 178 | | IsVisibleForAgent(definition, query.Agent) && |
| | 58 | 179 | | IsVisibleForTenant(definition, query.TenantId) && |
| | 58 | 180 | | IsVisibleForActor(definition, query.ActorId) && |
| | 58 | 181 | | HasRequiredPermissions(definition, query.UserPermissions); |
| | | 182 | | |
| | | 183 | | private static bool IsVisibleForAgent(AIToolDefinition definition, string? agent) |
| | | 184 | | { |
| | 58 | 185 | | if (definition.AgentScopes.Count == 0) |
| | 47 | 186 | | return true; |
| | | 187 | | |
| | 11 | 188 | | return !string.IsNullOrWhiteSpace(agent) && |
| | 11 | 189 | | definition.AgentScopes.Contains(agent, StringComparer.OrdinalIgnoreCase); |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | private static bool IsVisibleForTenant(AIToolDefinition definition, string? tenantId) |
| | | 193 | | { |
| | 54 | 194 | | if (tenantId != null) |
| | | 195 | | { |
| | 41 | 196 | | if (definition.TenantBehavior == AITenantBehavior.HostScoped) |
| | 2 | 197 | | return false; |
| | | 198 | | |
| | 39 | 199 | | if (definition.TenantBehavior == AITenantBehavior.CrossTenantDenied) |
| | 3 | 200 | | return definition.TenantIds.Contains(tenantId, StringComparer.OrdinalIgnoreCase); |
| | | 201 | | |
| | 36 | 202 | | return definition.TenantIds.Count == 0 || definition.TenantIds.Contains(tenantId, StringComparer.OrdinalIgno |
| | | 203 | | } |
| | | 204 | | |
| | 13 | 205 | | return definition.TenantBehavior != AITenantBehavior.CrossTenantDenied && definition.TenantIds.Count == 0; |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | private static bool IsVisibleForActor(AIToolDefinition definition, string? actorId) |
| | | 209 | | { |
| | 48 | 210 | | return definition.ActorIds.Count == 0 || |
| | 48 | 211 | | (!string.IsNullOrWhiteSpace(actorId) && definition.ActorIds.Contains(actorId, StringComparer.OrdinalIgnor |
| | | 212 | | } |
| | | 213 | | |
| | | 214 | | private static bool HasRequiredPermissions(AIToolDefinition definition, ICollection<string> userPermissions) |
| | | 215 | | { |
| | 47 | 216 | | if (definition.Permissions.Count == 0) |
| | 40 | 217 | | return true; |
| | | 218 | | |
| | 7 | 219 | | var grantedPermissions = userPermissions |
| | 5 | 220 | | .Where(x => !string.IsNullOrWhiteSpace(x)) |
| | 7 | 221 | | .ToHashSet(StringComparer.OrdinalIgnoreCase); |
| | | 222 | | |
| | 7 | 223 | | return grantedPermissions.Contains(PermissionNames.All) || definition.Permissions.All(grantedPermissions.Contain |
| | | 224 | | } |
| | | 225 | | |
| | 17 | 226 | | private class ScopedAITool(IServiceScope scope, IAITool inner, bool disposeInner) : IAITool |
| | | 227 | | { |
| | | 228 | | private bool _disposed; |
| | | 229 | | |
| | 0 | 230 | | public AIToolDefinition Definition => inner.Definition; |
| | | 231 | | |
| | | 232 | | public async ValueTask<AIToolResult> ExecuteAsync(AIToolExecutionContext context, CancellationToken cancellation |
| | | 233 | | { |
| | | 234 | | try |
| | | 235 | | { |
| | 13 | 236 | | return await inner.ExecuteAsync(context, cancellationToken); |
| | | 237 | | } |
| | | 238 | | finally |
| | | 239 | | { |
| | 13 | 240 | | Dispose(); |
| | | 241 | | } |
| | 12 | 242 | | } |
| | | 243 | | |
| | | 244 | | public void Dispose() |
| | | 245 | | { |
| | 30 | 246 | | if (_disposed) |
| | 13 | 247 | | return; |
| | | 248 | | |
| | 17 | 249 | | _disposed = true; |
| | 17 | 250 | | if (disposeInner) |
| | 15 | 251 | | inner.Dispose(); |
| | | 252 | | |
| | 17 | 253 | | scope.Dispose(); |
| | 17 | 254 | | } |
| | | 255 | | } |
| | | 256 | | |
| | 89 | 257 | | private record ResolvedTool(IAITool Tool, bool DisposeTool) |
| | | 258 | | { |
| | | 259 | | public void Dispose() |
| | | 260 | | { |
| | 2 | 261 | | if (DisposeTool) |
| | 0 | 262 | | Tool.Dispose(); |
| | 2 | 263 | | } |
| | | 264 | | } |
| | | 265 | | |
| | 279 | 266 | | private record ToolInfo(Type ToolType, IAITool Tool, AIToolDefinition Definition); |
| | | 267 | | } |