refactor(llm):SSE服务

This commit is contained in:
PZ688 2025-06-18 02:06:35 +08:00
parent e38b575e8f
commit aacda18a3c
4 changed files with 82 additions and 86 deletions

View File

@ -0,0 +1,26 @@
// 1. 定义接口
using System.Threading.Channels;
namespace Admin.NET.Core.Ai.Interface;
/// <summary>
/// SSE通道管理接口
/// </summary>
public interface ISseChannelManager
{
/// <summary>
/// 注册
/// </summary>
ChannelReader<string> Register(long userId);
/// <summary>
/// 注销
/// </summary>
/// <param name="userId"></param>
void Unregister(long userId);
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
Task SendMessageAsync(long userId, string message, CancellationToken cancellationToken = default);
}

View File

@ -0,0 +1,52 @@
using System.Threading.Channels;
namespace Admin.NET.Core.Ai.Services.SSE;
/// <summary>
/// SSE通道管理
/// </summary>
public class BaseSseChannelManager : ISseChannelManager
{
private readonly ConcurrentDictionary<long, Channel<string>> _userChannels = new();
/// <summary>
/// 注册
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public ChannelReader<string> Register(long userId)
{
var channel = Channel.CreateBounded<string>(new BoundedChannelOptions(100)
{
FullMode = BoundedChannelFullMode.Wait
});
_userChannels[userId] = channel;
return channel.Reader;
}
/// <summary>
/// 注销
/// </summary>
/// <param name="userId"></param>
public void Unregister(long userId)
{
if (_userChannels.TryRemove(userId, out var channel))
{
channel.Writer.TryComplete(); // 结束读端
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
public async Task SendMessageAsync(long userId, string message, CancellationToken cancellationToken = default)
{
if (_userChannels.TryGetValue(userId, out var channel))
{
await channel.Writer.WriteAsync(message, cancellationToken);
}
}
}

View File

@ -3,49 +3,8 @@ using System.Threading.Channels;
namespace Admin.NET.Core.Ai.Services.SSE;
/// <summary>
/// SSE通道管理
/// 聊天通道管理
/// </summary>
public class SseChannelManager: ISingleton
public class SseChannelManager: BaseSseChannelManager,ISingleton
{
private readonly ConcurrentDictionary<long, Channel<string>> _userChannels = new();
/// <summary>
/// 注册
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public ChannelReader<string> Register(long userId)
{
var channel = Channel.CreateBounded<string>(new BoundedChannelOptions(100){
FullMode = BoundedChannelFullMode.Wait
});
_userChannels[userId] = channel;
return channel.Reader;
}
/// <summary>
/// 注销
/// </summary>
/// <param name="userId"></param>
public void Unregister(long userId)
{
if (_userChannels.TryRemove(userId, out var channel))
{
channel.Writer.TryComplete(); // 结束读端
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
public async Task SendMessageAsync(long userId, string message, CancellationToken cancellationToken = default)
{
if (_userChannels.TryGetValue(userId, out var channel))
{
await channel.Writer.WriteAsync(message,cancellationToken);
}
}
}

View File

@ -3,49 +3,8 @@ using System.Threading.Channels;
namespace Admin.NET.Core.Ai.Services.SSE;
/// <summary>
/// SSE通道管理
/// 深度思考通道管理
/// </summary>
public class SseDeepThinkingChannelManager: ISingleton
public class SseDeepThinkingChannelManager: BaseSseChannelManager,ISingleton
{
private readonly ConcurrentDictionary<long, Channel<string>> _userChannels = new();
/// <summary>
/// 注册
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
public ChannelReader<string> Register(long userId)
{
var channel = Channel.CreateBounded<string>(new BoundedChannelOptions(100){
FullMode = BoundedChannelFullMode.Wait
});
_userChannels[userId] = channel;
return channel.Reader;
}
/// <summary>
/// 注销
/// </summary>
/// <param name="userId"></param>
public void Unregister(long userId)
{
if (_userChannels.TryRemove(userId, out var channel))
{
channel.Writer.TryComplete(); // 结束读端
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="userId"></param>
/// <param name="message"></param>
/// <param name="cancellationToken"></param>
public async Task SendMessageAsync(long userId, string message, CancellationToken cancellationToken = default)
{
if (_userChannels.TryGetValue(userId, out var channel))
{
await channel.Writer.WriteAsync(message,cancellationToken);
}
}
}