BotSharp/src/Infrastructure/BotSharp.Core/Session/LlmRealtimeSession.cs

109 lines
3.2 KiB
C#
Raw Normal View History

2025-04-14 06:25:16 +00:00
using System.ClientModel;
2025-05-07 21:34:39 +00:00
using System.Net.WebSockets;
2025-04-14 15:13:05 +00:00
using System.Runtime.CompilerServices;
2025-04-14 06:25:16 +00:00
2025-05-07 21:34:39 +00:00
namespace BotSharp.Core.Session;
2025-04-14 06:25:16 +00:00
2025-05-01 17:47:30 +00:00
public class LlmRealtimeSession : IDisposable
2025-04-14 06:25:16 +00:00
{
private readonly IServiceProvider _services;
2025-05-01 17:47:30 +00:00
private readonly ChatSessionOptions? _sessionOptions;
2025-04-14 06:25:16 +00:00
private ClientWebSocket _webSocket;
private readonly object _singleReceiveLock = new();
2025-04-14 15:13:05 +00:00
private readonly SemaphoreSlim _clientEventSemaphore = new(initialCount: 1, maxCount: 1);
2025-04-14 06:25:16 +00:00
private AsyncWebsocketDataCollectionResult _receivedCollectionResult;
2025-05-01 17:47:30 +00:00
public LlmRealtimeSession(
2025-04-14 06:25:16 +00:00
IServiceProvider services,
2025-05-01 17:47:30 +00:00
ChatSessionOptions? sessionOptions = null)
2025-04-14 06:25:16 +00:00
{
_services = services;
2025-05-01 17:47:30 +00:00
_sessionOptions = sessionOptions;
2025-04-14 06:25:16 +00:00
}
2025-04-28 21:02:10 +00:00
public async Task ConnectAsync(Uri uri, Dictionary<string, string> headers, CancellationToken cancellationToken = default)
2025-04-14 06:25:16 +00:00
{
_webSocket?.Dispose();
_webSocket = new ClientWebSocket();
2025-04-28 21:02:10 +00:00
foreach (var header in headers)
{
_webSocket.Options.SetRequestHeader(header.Key, header.Value);
}
await _webSocket.ConnectAsync(uri, cancellationToken);
2025-04-14 06:25:16 +00:00
}
2025-04-25 14:58:04 +00:00
public async IAsyncEnumerable<ChatSessionUpdate> ReceiveUpdatesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
2025-04-14 06:25:16 +00:00
{
2025-04-17 21:07:59 +00:00
await foreach (ClientResult result in ReceiveInnerUpdatesAsync(cancellationToken))
2025-04-14 06:25:16 +00:00
{
var update = HandleSessionResult(result);
yield return update;
}
}
2025-05-01 17:47:30 +00:00
private async IAsyncEnumerable<ClientResult> ReceiveInnerUpdatesAsync([EnumeratorCancellation] CancellationToken cancellationToken = default)
2025-04-14 06:25:16 +00:00
{
lock (_singleReceiveLock)
{
2025-05-01 17:47:30 +00:00
_receivedCollectionResult ??= new(_webSocket, _sessionOptions, cancellationToken);
2025-04-14 06:25:16 +00:00
}
await foreach (var result in _receivedCollectionResult)
{
yield return result;
}
}
2025-04-25 14:58:04 +00:00
private ChatSessionUpdate HandleSessionResult(ClientResult result)
2025-04-14 06:25:16 +00:00
{
using var response = result.GetRawResponse();
var bytes = response.Content.ToArray();
var text = Encoding.UTF8.GetString(bytes, 0, bytes.Length);
2025-04-25 14:58:04 +00:00
return new ChatSessionUpdate
2025-04-14 06:25:16 +00:00
{
RawResponse = text
};
}
public async Task SendEventToModel(object message)
{
if (_webSocket.State != WebSocketState.Open)
{
return;
}
2025-04-21 12:05:36 +00:00
await _clientEventSemaphore.WaitAsync();
2025-04-14 06:25:16 +00:00
try
{
if (message is not string data)
{
2025-05-01 17:47:30 +00:00
data = JsonSerializer.Serialize(message, _sessionOptions?.JsonOptions);
2025-04-14 06:25:16 +00:00
}
var buffer = Encoding.UTF8.GetBytes(data);
await _webSocket.SendAsync(new ArraySegment<byte>(buffer), WebSocketMessageType.Text, true, CancellationToken.None);
}
finally
{
2025-04-14 15:13:05 +00:00
_clientEventSemaphore.Release();
2025-04-14 06:25:16 +00:00
}
}
public async Task Disconnect()
{
if (_webSocket.State == WebSocketState.Open)
{
await _webSocket.CloseAsync(WebSocketCloseStatus.Empty, null, CancellationToken.None);
}
}
public void Dispose()
{
_webSocket?.Dispose();
}
}