| | |
| | | private readonly IDiagnosticsService _diagnostics; |
| | | private readonly IAiChatClient? _aiChatClient; |
| | | private readonly ConcurrentDictionary<string, CancellationTokenSource> _activeRequests = new(StringComparer.OrdinalIgnoreCase); |
| | | private readonly ConcurrentDictionary<string, string> _requestPlugins = new(StringComparer.OrdinalIgnoreCase); |
| | | |
| | | public LlmGatewayService( |
| | | ILlmModelRegistry registry, |
| | |
| | | var timeoutMs = request.Options.TimeoutMs ?? model.RequestDefaults.TimeoutMs; |
| | | linkedCts.CancelAfter(TimeSpan.FromMilliseconds(timeoutMs)); |
| | | _activeRequests[request.RequestId] = linkedCts; |
| | | _requestPlugins[request.RequestId] = request.PluginId; |
| | | |
| | | var secret = await _credentialStore.ResolveSecretAsync(model.AuthRef, linkedCts.Token).ConfigureAwait(false); |
| | | if (!string.IsNullOrWhiteSpace(model.AuthRef) && string.IsNullOrWhiteSpace(secret)) |
| | |
| | | if (!await enumerator.MoveNextAsync().ConfigureAwait(false)) |
| | | { |
| | | _activeRequests.TryRemove(request.RequestId, out _); |
| | | _requestPlugins.TryRemove(request.RequestId, out _); |
| | | linkedCts.Dispose(); |
| | | yield break; |
| | | } |
| | |
| | | if (nextEvent is LlmCompletedEvent or LlmErrorEvent) |
| | | { |
| | | _activeRequests.TryRemove(request.RequestId, out _); |
| | | _requestPlugins.TryRemove(request.RequestId, out _); |
| | | linkedCts.Dispose(); |
| | | } |
| | | } |
| | |
| | | cancellationToken.ThrowIfCancellationRequested(); |
| | | if (_activeRequests.TryRemove(requestId, out var cts)) |
| | | { |
| | | _requestPlugins.TryRemove(requestId, out _); |
| | | cts.Cancel(); |
| | | cts.Dispose(); |
| | | } |
| | |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | public Task CancelPluginRequestsAsync(string pluginId, CancellationToken cancellationToken = default) |
| | | { |
| | | cancellationToken.ThrowIfCancellationRequested(); |
| | | var matchingIds = _requestPlugins |
| | | .Where(kvp => string.Equals(kvp.Value, pluginId, StringComparison.OrdinalIgnoreCase)) |
| | | .Select(kvp => kvp.Key) |
| | | .ToList(); |
| | | |
| | | foreach (var requestId in matchingIds) |
| | | { |
| | | if (_activeRequests.TryRemove(requestId, out var cts)) |
| | | { |
| | | _requestPlugins.TryRemove(requestId, out _); |
| | | cts.Cancel(); |
| | | cts.Dispose(); |
| | | } |
| | | } |
| | | |
| | | return Task.CompletedTask; |
| | | } |
| | | |
| | | private static bool IsValidRequest(LlmChatRequest request, out string error) |
| | | { |
| | | if (string.IsNullOrWhiteSpace(request.RequestId) || |