feat: 完成agent接口功能

This commit is contained in:
chenchun
2025-12-24 12:18:33 +08:00
parent 62940ae25a
commit eb6ec06157
4 changed files with 123 additions and 38 deletions

View File

@@ -45,14 +45,15 @@ public class AiChatService : ApplicationService
private readonly AiGateWayManager _aiGateWayManager;
private readonly PremiumPackageManager _premiumPackageManager;
private readonly ChatManager _chatManager;
private readonly TokenManager _tokenManager;
private readonly IAccountService _accountService;
public AiChatService(IHttpContextAccessor httpContextAccessor,
AiBlacklistManager aiBlacklistManager,
ISqlSugarRepository<AiModelEntity> aiModelRepository,
ILogger<AiChatService> logger,
AiGateWayManager aiGateWayManager,
PremiumPackageManager premiumPackageManager,
ChatManager chatManager)
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService)
{
_httpContextAccessor = httpContextAccessor;
_aiBlacklistManager = aiBlacklistManager;
@@ -61,6 +62,8 @@ public class AiChatService : ApplicationService
_aiGateWayManager = aiGateWayManager;
_premiumPackageManager = premiumPackageManager;
_chatManager = chatManager;
_tokenManager = tokenManager;
_accountService = accountService;
}
@@ -155,15 +158,42 @@ public class AiChatService : ApplicationService
/// Agent 发送消息
/// </summary>
[HttpPost("ai-chat/agent/send")]
[Authorize]
public async Task PostAgentSendAsync([FromBody] AgentSendInput input, CancellationToken cancellationToken)
{
var tokenValidation = await _tokenManager.ValidateTokenAsync(input.Token, input.ModelId);
await _aiBlacklistManager.VerifiyAiBlacklist(tokenValidation.UserId);
// 验证用户是否为VIP
var userInfo = await _accountService.GetAsync(null, null, tokenValidation.UserId);
if (userInfo == null)
{
throw new UserFriendlyException("用户信息不存在");
}
// 检查是否为VIP使用RoleCodes判断
if (!userInfo.RoleCodes.Contains(AiHubConst.VipRole) && userInfo.User.UserName != "cc")
{
throw new UserFriendlyException("该接口为尊享服务专用需要VIP权限才能使用");
}
//如果是尊享包服务,需要校验是是否尊享包足够
if (PremiumPackageConst.ModeIds.Contains(input.ModelId))
{
// 检查尊享token包用量
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(tokenValidation.UserId);
if (availableTokens <= 0)
{
throw new UserFriendlyException("尊享token包用量不足请先购买尊享token包");
}
}
await _chatManager.AgentCompleteChatStreamAsync(_httpContextAccessor.HttpContext,
input.SessionId,
input.Content,
input.TokenId,
input.Token,
tokenValidation.TokenId,
input.ModelId,
CurrentUser.GetId(),
tokenValidation.UserId,
input.Tools,
cancellationToken);
}