2025-06-25 00:30:01 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2025-12-22 00:17:10 +08:00
|
|
|
|
using System.Reflection;
|
2025-06-25 00:30:01 +08:00
|
|
|
|
using System.Text;
|
2025-12-22 00:17:10 +08:00
|
|
|
|
using System.Text.Encodings.Web;
|
|
|
|
|
|
using Dm.util;
|
2025-06-21 21:40:51 +08:00
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2025-06-21 21:40:51 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2025-07-02 00:28:44 +08:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
using Microsoft.Extensions.Options;
|
2025-12-22 00:17:10 +08:00
|
|
|
|
using ModelContextProtocol;
|
|
|
|
|
|
using ModelContextProtocol.Server;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
|
|
|
|
|
using OpenAI.Chat;
|
2025-06-19 21:24:13 +08:00
|
|
|
|
using Volo.Abp.Application.Services;
|
2025-06-21 13:02:38 +08:00
|
|
|
|
using Volo.Abp.Users;
|
2025-06-19 21:24:13 +08:00
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.Dtos;
|
2025-12-24 00:22:46 +08:00
|
|
|
|
using Yi.Framework.AiHub.Application.Contracts.Dtos.Chat;
|
2025-12-22 00:17:10 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain;
|
2025-06-25 17:12:09 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities;
|
2025-12-24 14:17:32 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.Chat;
|
2025-06-27 22:13:26 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Entities.Model;
|
2025-07-05 15:11:56 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Extensions;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Managers;
|
2025-10-14 22:17:21 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Consts;
|
2025-06-27 22:13:26 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Dtos;
|
2025-08-11 15:31:11 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Dtos.OpenAi;
|
2025-08-03 23:23:32 +08:00
|
|
|
|
using Yi.Framework.AiHub.Domain.Shared.Enums;
|
2025-06-21 21:40:51 +08:00
|
|
|
|
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
|
|
|
|
|
using Yi.Framework.Rbac.Domain.Shared.Dtos;
|
2025-06-25 17:12:09 +08:00
|
|
|
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
2025-06-19 21:24:13 +08:00
|
|
|
|
|
|
|
|
|
|
namespace Yi.Framework.AiHub.Application.Services;
|
|
|
|
|
|
|
2025-06-21 13:02:38 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// ai服务
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public class AiChatService : ApplicationService
|
2025-06-19 21:24:13 +08:00
|
|
|
|
{
|
2025-06-21 01:08:14 +08:00
|
|
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
2025-06-25 17:12:09 +08:00
|
|
|
|
private readonly AiBlacklistManager _aiBlacklistManager;
|
2025-07-02 00:28:44 +08:00
|
|
|
|
private readonly ILogger<AiChatService> _logger;
|
2025-07-05 15:11:56 +08:00
|
|
|
|
private readonly AiGateWayManager _aiGateWayManager;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
private readonly ModelManager _modelManager;
|
2025-10-14 22:17:21 +08:00
|
|
|
|
private readonly PremiumPackageManager _premiumPackageManager;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
private readonly ChatManager _chatManager;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
private readonly TokenManager _tokenManager;
|
|
|
|
|
|
private readonly IAccountService _accountService;
|
2025-12-24 14:17:32 +08:00
|
|
|
|
private readonly ISqlSugarRepository<AgentStoreAggregateRoot> _agentStoreRepository;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;
|
2025-12-24 14:17:32 +08:00
|
|
|
|
|
2025-06-25 17:12:09 +08:00
|
|
|
|
public AiChatService(IHttpContextAccessor httpContextAccessor,
|
2025-07-05 15:11:56 +08:00
|
|
|
|
AiBlacklistManager aiBlacklistManager,
|
2025-12-23 00:49:17 +08:00
|
|
|
|
ILogger<AiChatService> logger,
|
|
|
|
|
|
AiGateWayManager aiGateWayManager,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
ModelManager modelManager,
|
2025-12-23 00:49:17 +08:00
|
|
|
|
PremiumPackageManager premiumPackageManager,
|
2025-12-24 14:17:32 +08:00
|
|
|
|
ChatManager chatManager, TokenManager tokenManager, IAccountService accountService,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
ISqlSugarRepository<AgentStoreAggregateRoot> agentStoreRepository, ISqlSugarRepository<AiModelEntity> aiModelRepository)
|
2025-06-21 01:08:14 +08:00
|
|
|
|
{
|
2025-07-05 15:11:56 +08:00
|
|
|
|
_httpContextAccessor = httpContextAccessor;
|
2025-06-25 17:12:09 +08:00
|
|
|
|
_aiBlacklistManager = aiBlacklistManager;
|
2025-07-02 00:28:44 +08:00
|
|
|
|
_logger = logger;
|
2025-07-05 15:11:56 +08:00
|
|
|
|
_aiGateWayManager = aiGateWayManager;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
_modelManager = modelManager;
|
2025-10-14 22:17:21 +08:00
|
|
|
|
_premiumPackageManager = premiumPackageManager;
|
2025-12-23 00:49:17 +08:00
|
|
|
|
_chatManager = chatManager;
|
2025-12-24 12:18:33 +08:00
|
|
|
|
_tokenManager = tokenManager;
|
|
|
|
|
|
_accountService = accountService;
|
2025-12-24 14:17:32 +08:00
|
|
|
|
_agentStoreRepository = agentStoreRepository;
|
2026-01-01 00:44:02 +08:00
|
|
|
|
_aiModelRepository = aiModelRepository;
|
2025-06-21 01:08:14 +08:00
|
|
|
|
}
|
2025-06-19 21:24:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-21 21:40:51 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 查询已登录的账户信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[Route("ai-chat/account")]
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
public async Task<UserRoleMenuDto> GetAsync()
|
|
|
|
|
|
{
|
|
|
|
|
|
var accountService = LazyServiceProvider.GetRequiredService<IAccountService>();
|
|
|
|
|
|
var output = await accountService.GetAsync();
|
|
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-19 21:24:13 +08:00
|
|
|
|
/// <summary>
|
2026-01-02 19:26:09 +08:00
|
|
|
|
/// 获取对话模型列表
|
2025-06-19 21:24:13 +08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public async Task<List<ModelGetListOutput>> GetModelAsync()
|
|
|
|
|
|
{
|
2025-06-25 22:45:57 +08:00
|
|
|
|
var output = await _aiModelRepository._DbQueryable
|
2026-01-05 22:19:23 +08:00
|
|
|
|
.Where(x=>x.IsEnabled==true)
|
2025-08-03 23:23:32 +08:00
|
|
|
|
.Where(x => x.ModelType == ModelTypeEnum.Chat)
|
2025-12-11 21:12:29 +08:00
|
|
|
|
.Where(x => x.ModelApiType == ModelApiTypeEnum.OpenAi)
|
2025-06-27 22:13:26 +08:00
|
|
|
|
.OrderByDescending(x => x.OrderNum)
|
2025-06-25 22:45:57 +08:00
|
|
|
|
.Select(x => new ModelGetListOutput
|
2025-06-27 22:13:26 +08:00
|
|
|
|
{
|
|
|
|
|
|
Id = x.Id,
|
2025-07-05 15:11:56 +08:00
|
|
|
|
ModelId = x.ModelId,
|
2025-06-27 22:13:26 +08:00
|
|
|
|
ModelName = x.Name,
|
|
|
|
|
|
ModelDescribe = x.Description,
|
2025-11-25 09:59:31 +08:00
|
|
|
|
Remark = x.Description,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
IsPremiumPackage = x.IsPremium
|
2025-06-27 22:13:26 +08:00
|
|
|
|
}).ToListAsync();
|
2025-06-21 01:08:14 +08:00
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
2025-07-17 23:52:00 +08:00
|
|
|
|
/// <param name="sessionId"></param>
|
2025-06-21 01:41:05 +08:00
|
|
|
|
/// <param name="cancellationToken"></param>
|
2025-08-03 21:47:22 +08:00
|
|
|
|
[HttpPost("ai-chat/send")]
|
|
|
|
|
|
public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId,
|
2025-07-17 23:10:26 +08:00
|
|
|
|
CancellationToken cancellationToken)
|
2025-06-21 01:08:14 +08:00
|
|
|
|
{
|
2025-06-25 17:12:09 +08:00
|
|
|
|
//除了免费模型,其他的模型都要校验
|
2025-06-30 22:07:42 +08:00
|
|
|
|
if (!input.Model.Contains("DeepSeek-R1"))
|
2025-06-29 15:41:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
//有token,需要黑名单校验
|
|
|
|
|
|
if (CurrentUser.IsAuthenticated)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
|
2025-07-05 15:11:56 +08:00
|
|
|
|
if (!CurrentUser.IsAiVip())
|
2025-06-29 15:41:49 +08:00
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("该模型需要VIP用户才能使用,请购买VIP后重新登录重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("未登录用户,只能使用未加速的DeepSeek-R1,请登录后重试");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-08-03 23:23:32 +08:00
|
|
|
|
|
2025-10-14 22:17:21 +08:00
|
|
|
|
//如果是尊享包服务,需要校验是是否尊享包足够
|
2026-01-01 00:44:02 +08:00
|
|
|
|
if (CurrentUser.IsAuthenticated)
|
2025-10-14 22:17:21 +08:00
|
|
|
|
{
|
2026-01-01 00:44:02 +08:00
|
|
|
|
var isPremium = await _modelManager.IsPremiumModelAsync(input.Model);
|
|
|
|
|
|
|
|
|
|
|
|
if (isPremium)
|
2025-10-14 22:17:21 +08:00
|
|
|
|
{
|
2026-01-01 00:44:02 +08:00
|
|
|
|
// 检查尊享token包用量
|
|
|
|
|
|
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId());
|
|
|
|
|
|
if (availableTokens <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
|
|
|
|
|
}
|
2025-10-14 22:17:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
//ai网关代理httpcontext
|
|
|
|
|
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
CurrentUser.Id, sessionId, null, CancellationToken.None);
|
2025-12-23 17:08:42 +08:00
|
|
|
|
}
|
2025-12-24 00:22:46 +08:00
|
|
|
|
|
2025-12-24 14:17:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 发送消息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="input"></param>
|
|
|
|
|
|
/// <param name="cancellationToken"></param>
|
|
|
|
|
|
[HttpPost("ai-chat/FileMaster/send")]
|
|
|
|
|
|
public async Task PostFileMasterSendAsync([FromBody] ThorChatCompletionsRequest input,
|
|
|
|
|
|
CancellationToken cancellationToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(input.Model))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BusinessException("当前接口不支持第三方使用");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (CurrentUser.IsAuthenticated)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId());
|
|
|
|
|
|
if (CurrentUser.IsAiVip())
|
|
|
|
|
|
{
|
|
|
|
|
|
input.Model = "gpt-5-chat";
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
input.Model = "gpt-4.1-mini";
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
input.Model = "DeepSeek-R1-0528";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//ai网关代理httpcontext
|
|
|
|
|
|
await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
CurrentUser.Id, null, null, CancellationToken.None);
|
2025-12-24 14:17:32 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-12-23 17:29:07 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Agent 发送消息
|
|
|
|
|
|
/// </summary>
|
2025-12-23 17:08:42 +08:00
|
|
|
|
[HttpPost("ai-chat/agent/send")]
|
2025-12-24 00:22:46 +08:00
|
|
|
|
public async Task PostAgentSendAsync([FromBody] AgentSendInput input, CancellationToken cancellationToken)
|
2025-12-23 17:08:42 +08:00
|
|
|
|
{
|
2026-01-02 19:26:09 +08:00
|
|
|
|
var tokenValidation = await _tokenManager.ValidateTokenAsync(input.TokenId, input.ModelId);
|
2025-12-24 12:18:33 +08:00
|
|
|
|
|
|
|
|
|
|
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权限才能使用");
|
|
|
|
|
|
}
|
2025-12-24 14:17:32 +08:00
|
|
|
|
|
2025-12-24 12:18:33 +08:00
|
|
|
|
//如果是尊享包服务,需要校验是是否尊享包足够
|
2026-01-01 00:44:02 +08:00
|
|
|
|
var isPremium = await _modelManager.IsPremiumModelAsync(input.ModelId);
|
|
|
|
|
|
|
|
|
|
|
|
if (isPremium)
|
2025-12-24 12:18:33 +08:00
|
|
|
|
{
|
|
|
|
|
|
// 检查尊享token包用量
|
|
|
|
|
|
var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(tokenValidation.UserId);
|
|
|
|
|
|
if (availableTokens <= 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-24 14:17:32 +08:00
|
|
|
|
|
2025-12-24 00:22:46 +08:00
|
|
|
|
await _chatManager.AgentCompleteChatStreamAsync(_httpContextAccessor.HttpContext,
|
|
|
|
|
|
input.SessionId,
|
|
|
|
|
|
input.Content,
|
2026-01-02 19:26:09 +08:00
|
|
|
|
tokenValidation.Token,
|
2025-12-24 12:18:33 +08:00
|
|
|
|
tokenValidation.TokenId,
|
2025-12-24 00:22:46 +08:00
|
|
|
|
input.ModelId,
|
2025-12-24 12:18:33 +08:00
|
|
|
|
tokenValidation.UserId,
|
2025-12-24 00:22:46 +08:00
|
|
|
|
input.Tools,
|
2026-01-01 00:44:02 +08:00
|
|
|
|
CancellationToken.None);
|
2025-06-19 21:24:13 +08:00
|
|
|
|
}
|
2025-08-23 13:15:28 +08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-12-24 14:17:32 +08:00
|
|
|
|
/// 获取 Agent 工具
|
2025-08-23 13:15:28 +08:00
|
|
|
|
/// </summary>
|
2025-12-24 14:17:32 +08:00
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost("ai-chat/agent/tool")]
|
|
|
|
|
|
public List<AgentToolOutput> GetAgentToolAsync()
|
2025-08-23 13:15:28 +08:00
|
|
|
|
{
|
2025-12-24 14:17:32 +08:00
|
|
|
|
var agentTools = _chatManager.GetTools().Select(x => new AgentToolOutput
|
2025-08-23 13:15:28 +08:00
|
|
|
|
{
|
2025-12-24 14:17:32 +08:00
|
|
|
|
Code = x.Code,
|
|
|
|
|
|
Name = x.Name
|
|
|
|
|
|
}).ToList();
|
|
|
|
|
|
return agentTools;
|
2025-08-23 13:15:28 +08:00
|
|
|
|
}
|
2025-12-22 00:17:10 +08:00
|
|
|
|
|
2025-12-24 14:17:32 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 获取 Agent 上下文
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
[HttpPost("ai-chat/agent/context/{sessionId}")]
|
|
|
|
|
|
[Authorize]
|
|
|
|
|
|
public async Task<string?> GetAgentContextAsync([FromRoute] Guid sessionId)
|
2025-12-22 00:17:10 +08:00
|
|
|
|
{
|
2025-12-24 14:17:32 +08:00
|
|
|
|
var data = await _agentStoreRepository.GetFirstAsync(x => x.SessionId == sessionId);
|
|
|
|
|
|
return data?.Store;
|
2025-12-22 00:17:10 +08:00
|
|
|
|
}
|
2025-06-19 21:24:13 +08:00
|
|
|
|
}
|