From dbc6b8cf5e4d9a944b15cd47a53e751d7b647d27 Mon Sep 17 00:00:00 2001 From: ccnetcore Date: Sat, 31 Jan 2026 21:22:09 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E6=B6=88=E6=81=AF?= =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=88=9B=E5=BB=BA=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E5=B9=B6=E5=AE=8C=E5=96=84TokenUsage=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 用户消息创建支持传入创建时间,用于统计与回放 - TokenUsage 为空时自动初始化,避免空引用问题 - 网关记录消息开始时间并传递至消息管理器 - 标记并停用旧的发送消息接口 - 前端版本号更新至 3.6 - 移除未使用的 VITE_BUILD_COMPRESS 类型声明 --- .../Services/Chat/AiChatService.cs | 97 ++++++++++--------- .../Entities/Chat/MessageAggregateRoot.cs | 9 ++ .../Managers/AiGateWayManager.cs | 3 +- .../Managers/AiMessageManager.cs | 8 +- Yi.Ai.Vue3/index.html | 2 +- Yi.Ai.Vue3/types/import_meta.d.ts | 1 - 6 files changed, 67 insertions(+), 53 deletions(-) diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/AiChatService.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/AiChatService.cs index 5f3dbe1c..3ce93e5d 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/AiChatService.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Application/Services/Chat/AiChatService.cs @@ -127,54 +127,55 @@ public class AiChatService : ApplicationService } - /// - /// 发送消息 - /// - /// - /// - /// - [HttpPost("ai-chat/send")] - public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId, - CancellationToken cancellationToken) - { - //除了免费模型,其他的模型都要校验 - if (input.Model!=FreeModelId) - { - //有token,需要黑名单校验 - if (CurrentUser.IsAuthenticated) - { - await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId()); - if (!CurrentUser.IsAiVip()) - { - throw new UserFriendlyException("该模型需要VIP用户才能使用,请购买VIP后重新登录重试"); - } - } - else - { - throw new UserFriendlyException("未登录用户,只能使用未加速的DeepSeek-R1,请登录后重试"); - } - } - - //如果是尊享包服务,需要校验是是否尊享包足够 - if (CurrentUser.IsAuthenticated) - { - var isPremium = await _modelManager.IsPremiumModelAsync(input.Model); - - if (isPremium) - { - // 检查尊享token包用量 - var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId()); - if (availableTokens <= 0) - { - throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包"); - } - } - } - - //ai网关代理httpcontext - await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input, - CurrentUser.Id, sessionId, null, CancellationToken.None); - } + // /// + // /// 发送消息 + // /// + // /// + // /// + // /// + // [HttpPost("ai-chat/send")] + // [Obsolete] + // public async Task PostSendAsync([FromBody] ThorChatCompletionsRequest input, [FromQuery] Guid? sessionId, + // CancellationToken cancellationToken) + // { + // //除了免费模型,其他的模型都要校验 + // if (input.Model!=FreeModelId) + // { + // //有token,需要黑名单校验 + // if (CurrentUser.IsAuthenticated) + // { + // await _aiBlacklistManager.VerifiyAiBlacklist(CurrentUser.GetId()); + // if (!CurrentUser.IsAiVip()) + // { + // throw new UserFriendlyException("该模型需要VIP用户才能使用,请购买VIP后重新登录重试"); + // } + // } + // else + // { + // throw new UserFriendlyException("未登录用户,只能使用未加速的DeepSeek-R1,请登录后重试"); + // } + // } + // + // //如果是尊享包服务,需要校验是是否尊享包足够 + // if (CurrentUser.IsAuthenticated) + // { + // var isPremium = await _modelManager.IsPremiumModelAsync(input.Model); + // + // if (isPremium) + // { + // // 检查尊享token包用量 + // var availableTokens = await _premiumPackageManager.GetAvailableTokensAsync(CurrentUser.GetId()); + // if (availableTokens <= 0) + // { + // throw new UserFriendlyException("尊享token包用量不足,请先购买尊享token包"); + // } + // } + // } + // + // //ai网关代理httpcontext + // await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input, + // CurrentUser.Id, sessionId, null, CancellationToken.None); + // } /// /// 发送消息 diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs index a704e924..e390efd4 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Entities/Chat/MessageAggregateRoot.cs @@ -53,6 +53,15 @@ public class MessageAggregateRoot : FullAuditedAggregateRoot TotalTokenCount = tokenUsage.TotalTokens ?? 0 }; } + else + { + this.TokenUsage = new TokenUsageValueObject + { + OutputTokenCount = 0, + InputTokenCount = 0, + TotalTokenCount = 0 + }; + } this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web; } diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs index 90708bbe..0feb8e26 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiGateWayManager.cs @@ -1143,6 +1143,7 @@ public class AiGateWayManager : DomainService Guid? tokenId = null, CancellationToken cancellationToken = default) { + var startTime = DateTime.Now; var response = httpContext.Response; // 设置响应头,声明是 SSE 流 response.ContentType = "text/event-stream;charset=utf-8;"; @@ -1217,7 +1218,7 @@ public class AiGateWayManager : DomainService Content = sessionId is null ? "不予存储" : processResult?.UserContent ?? string.Empty, ModelId = sourceModelId, TokenUsage = processResult?.TokenUsage, - }, tokenId); + }, tokenId,createTime:startTime); await _aiMessageManager.CreateSystemMessageAsync(userId, sessionId, new MessageInputDto diff --git a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs index 95730424..b8310d66 100644 --- a/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs +++ b/Yi.Abp.Net8/module/ai-hub/Yi.Framework.AiHub.Domain/Managers/AiMessageManager.cs @@ -38,11 +38,15 @@ public class AiMessageManager : DomainService /// 会话Id /// 消息输入 /// Token Id(Web端传Guid.Empty) + /// /// - public async Task CreateUserMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null) + public async Task CreateUserMessageAsync( Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null,DateTime? createTime=null) { input.Role = "user"; - var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId); + var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId) + { + CreationTime = createTime??DateTime.Now + }; await _repository.InsertAsync(message); } } \ No newline at end of file diff --git a/Yi.Ai.Vue3/index.html b/Yi.Ai.Vue3/index.html index 70c5ca53..02766bc4 100644 --- a/Yi.Ai.Vue3/index.html +++ b/Yi.Ai.Vue3/index.html @@ -112,7 +112,7 @@
-
意心Ai 3.5
+
意心Ai 3.6
海外地址,仅首次访问预计加载约10秒,无需梯子