| | | 1 | | using Elsa.AI.Abstractions.Contracts; |
| | | 2 | | using Elsa.AI.Abstractions.Models; |
| | | 3 | | using Microsoft.Extensions.Logging; |
| | | 4 | | using Microsoft.Extensions.DependencyInjection; |
| | | 5 | | |
| | | 6 | | namespace Elsa.AI.Host.Services; |
| | | 7 | | |
| | 40 | 8 | | public class AIAuditSink(IServiceScopeFactory scopeFactory, ILogger<AIAuditSink> logger) : IAIAuditSink |
| | | 9 | | { |
| | | 10 | | public async ValueTask RecordAsync(AIAuditEvent auditEvent, CancellationToken cancellationToken = default) |
| | | 11 | | { |
| | 73 | 12 | | await RecordManyAsync([auditEvent], cancellationToken); |
| | 72 | 13 | | } |
| | | 14 | | |
| | | 15 | | public async ValueTask RecordManyAsync(IReadOnlyCollection<AIAuditEvent> auditEvents, CancellationToken cancellation |
| | | 16 | | { |
| | 97 | 17 | | if (auditEvents.Count == 0) |
| | 0 | 18 | | return; |
| | | 19 | | |
| | 97 | 20 | | using var scope = scopeFactory.CreateScope(); |
| | 97 | 21 | | var handlers = scope.ServiceProvider.GetServices<IAIAuditEventHandler>(); |
| | | 22 | | |
| | 201 | 23 | | foreach (var handler in handlers) |
| | | 24 | | { |
| | | 25 | | try |
| | | 26 | | { |
| | 4 | 27 | | await handler.RecordManyAsync(auditEvents, cancellationToken); |
| | 2 | 28 | | } |
| | 2 | 29 | | catch (Exception e) when (e is not OperationCanceledException) |
| | | 30 | | { |
| | 1 | 31 | | logger.LogWarning(e, "AI audit handler {HandlerType} failed while recording {AuditEventCount} event(s)." |
| | 1 | 32 | | } |
| | 3 | 33 | | } |
| | 96 | 34 | | } |
| | | 35 | | } |