feat: 完成openapi改造

This commit is contained in:
ccnetcore
2025-07-03 22:31:39 +08:00
parent 0a0e0bca10
commit 15be047371
8 changed files with 238 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;
using Yi.Framework.AiHub.Domain.Entities.ValueObjects;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Enums;
namespace Yi.Framework.AiHub.Domain.Entities.Chat;
@@ -17,7 +18,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
{
}
public MessageAggregateRoot(Guid userId, Guid sessionId, string content, string role, string modelId,
public MessageAggregateRoot(Guid userId, Guid? sessionId, string content, string role, string modelId,
TokenUsage? tokenUsage)
{
UserId = userId;
@@ -27,13 +28,14 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
ModelId = modelId;
if (tokenUsage is not null)
{
this.TokenUsage = tokenUsage.Adapt<TokenUsageValueObject>();
this.TokenUsage = tokenUsage.Adapt<TokenUsageValueObject>();
}
this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web;
}
public Guid UserId { get; set; }
public Guid SessionId { get; set; }
public Guid? SessionId { get; set; }
[SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
public string Content { get; set; }
@@ -42,7 +44,7 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
public string ModelId { get; set; }
public string? Remark { get; set; }
[SugarColumn(IsOwnsOne = true)]
public TokenUsageValueObject TokenUsage { get; set; }= new TokenUsageValueObject();
[SugarColumn(IsOwnsOne = true)] public TokenUsageValueObject TokenUsage { get; set; } = new TokenUsageValueObject();
public MessageTypeEnum MessageType { get; set; }
}

View File

@@ -0,0 +1,52 @@
using System.Text;
using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;
namespace Yi.Framework.AiHub.Domain.Entities.OpenApi;
[SugarTable("Ai_Token")]
public class TokenAggregateRoot : FullAuditedAggregateRoot<Guid>
{
public TokenAggregateRoot()
{
}
public TokenAggregateRoot(Guid userId)
{
this.UserId = userId;
this.Token = GenerateToken();
}
public string Token { get; set; }
public Guid UserId { get; set; }
/// <summary>
/// 重置token
/// </summary>
public void ResetToken()
{
this.Token = GenerateToken();
}
private string GenerateToken(int length = 36)
{
// 定义可能的字符集:大写字母、小写字母和数字
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
// 创建随机数生成器
Random random = new Random();
// 使用StringBuilder高效构建字符串
StringBuilder sb = new StringBuilder(length);
// 生成指定长度的随机字符串
for (int i = 0; i < length; i++)
{
// 从字符集中随机选择一个字符
int index = random.Next(chars.Length);
sb.Append(chars[index]);
}
return "yi-" + sb.ToString();
}
}

View File

@@ -23,7 +23,7 @@ public class AiMessageManager : DomainService
/// <param name="userId"></param>
/// <param name="input"></param>
/// <returns></returns>
public async Task CreateSystemMessageAsync(Guid userId, Guid sessionId, MessageInputDto input)
public async Task CreateSystemMessageAsync(Guid userId, Guid? sessionId, MessageInputDto input)
{
input.Role = "system";
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);
@@ -37,7 +37,7 @@ public class AiMessageManager : DomainService
/// <param name="userId"></param>
/// <param name="input"></param>
/// <returns></returns>
public async Task CreateUserMessageAsync(Guid userId, Guid sessionId, MessageInputDto input)
public async Task CreateUserMessageAsync(Guid userId, Guid? sessionId, MessageInputDto input)
{
input.Role = "user";
var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId,input.TokenUsage);

View File

@@ -0,0 +1,64 @@
using Volo.Abp.Domain.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Domain.Entities.OpenApi;
using Yi.Framework.SqlSugarCore.Abstractions;
namespace Yi.Framework.AiHub.Domain.Managers;
public class TokenManager : DomainService
{
private readonly ISqlSugarRepository<TokenAggregateRoot> _tokenRepository;
public TokenManager(ISqlSugarRepository<TokenAggregateRoot> tokenRepository)
{
_tokenRepository = tokenRepository;
}
public async Task<string?> GetAsync(Guid userId)
{
var entity = await _tokenRepository._DbQueryable.FirstAsync(x => x.UserId == userId);
if (entity is not null)
{
return entity.Token;
}
else
{
return null;
}
}
public async Task CreateAsync(Guid userId)
{
var entity = await _tokenRepository._DbQueryable.FirstAsync(x => x.UserId == userId);
if (entity is not null)
{
entity.ResetToken();
await _tokenRepository.UpdateAsync(entity);
}
else
{
var token = new TokenAggregateRoot(userId);
await _tokenRepository.InsertAsync(token);
}
}
public async Task<Guid> GetUserIdAsync(string? token)
{
if (token is null)
{
throw new UserFriendlyException("当前请求未包含token", "401");
}
if (token.StartsWith("yi-"))
{
var entity = await _tokenRepository._DbQueryable.Where(x => x.Token == token).FirstAsync();
if (entity is null)
{
throw new UserFriendlyException("当前请求token无效", "401");
}
return entity.UserId;
}
throw new UserFriendlyException("当前请求token非法", "401");
}
}