From 409154366bbc1fe23aecda41d63f5908fc4942ac Mon Sep 17 00:00:00 2001 From: colin Date: Sat, 14 Mar 2026 14:43:09 +0800 Subject: [PATCH] feat: Adding the API key without proper verification --- .../LINGYUN/Abp/AI/Agent/AgentService.cs | 40 ++++++++++++++++--- ...AsyncEnumerableErrorHandlingExtenssions.cs | 35 ++++++++++++++++ .../LINGYUN/Abp/AI/AbpAIErrorCodes.cs | 4 ++ .../Abp/AI/Localization/Resources/en.json | 1 + .../AI/Localization/Resources/zh-Hans.json | 1 + 5 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs index 63b090f4c..20508d83e 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentService.cs @@ -2,10 +2,14 @@ using LINGYUN.Abp.AI.Localization; using LINGYUN.Abp.AI.Models; using LINGYUN.Abp.AI.Tokens; +using LINGYUN.Abp.AI.Workspaces; using Microsoft.Agents.AI; using Microsoft.Extensions.AI; using Microsoft.Extensions.Localization; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using System; +using System.ClientModel; using System.Collections.Generic; using System.Linq; using System.Text; @@ -19,6 +23,8 @@ using AIChatMessage = Microsoft.Extensions.AI.ChatMessage; namespace LINGYUN.Abp.AI.Agent; public class AgentService : IAgentService, IScopedDependency { + public ILogger Logger { protected get; set; } + private readonly IClock _clock; private readonly IGuidGenerator _guidGenerator; private readonly IAgentFactory _agentFactory; @@ -42,6 +48,8 @@ public class AgentService : IAgentService, IScopedDependency _chatMessageStore = chatMessageStore; _conversationStore = conversationStore; _localizerResource = localizerResource; + + Logger = NullLogger.Instance; } public async virtual IAsyncEnumerable SendMessageAsync(Models.ChatMessage message) @@ -52,9 +60,29 @@ public class AgentService : IAgentService, IScopedDependency var messages = await BuildChatMessages(message); - var agent = await _agentFactory.CreateAsync(message.Workspace); + AIAgent agent = default!; + + try + { + agent = await _agentFactory.CreateAsync(message.Workspace); + } + catch (ArgumentException ae) + { + if (ae.ParamName == nameof(WorkspaceDefinition.ApiKey)) + { + throw new BusinessException(AbpAIErrorCodes.MissingOrInvalidApiKey, innerException: ae); + } + throw; + } - var agentRunRes = agent.RunStreamingAsync(messages); + var agentRunRes = agent.RunStreamingAsync(messages) + .WithErrorHandling((ex) => + { + if (ex is ClientResultException cre && cre.Status == 401) + { + throw new BusinessException(AbpAIErrorCodes.MissingOrInvalidApiKey, innerException: ex); + } + }); var tokenUsageInfo = new TokenUsageInfo(message.Workspace, conversationId); var agentMessageBuilder = new StringBuilder(); @@ -70,10 +98,10 @@ public class AgentService : IAgentService, IScopedDependency tokenUsageInfo.WithMessageId(messageId); -#if DEBUG - Console.WriteLine(); - Console.WriteLine(tokenUsageInfo); -#endif + if (Logger.IsEnabled(LogLevel.Debug)) + { + Logger.LogDebug("TokenUsageInfo: {TokenUsageInfo}", tokenUsageInfo); + } await StoreTokenUsageInfo(tokenUsageInfo); } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs new file mode 100644 index 000000000..145445fd0 --- /dev/null +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/System/Collections/Generic/IAsyncEnumerableErrorHandlingExtenssions.cs @@ -0,0 +1,35 @@ +using System.Runtime.CompilerServices; +using System.Threading; + +namespace System.Collections.Generic; + +internal static class IAsyncEnumerableErrorHandlingExtenssions +{ + public static async IAsyncEnumerable WithErrorHandling( + this IAsyncEnumerable source, + Action onError, + [EnumeratorCancellation] CancellationToken cancellationToken = default) + { + var enumerator = source.GetAsyncEnumerator(cancellationToken); + await using (enumerator) + { + while (true) + { + try + { + if (!await enumerator.MoveNextAsync()) + { + break; + } + } + catch (Exception ex) + { + onError(ex); + yield break; + } + + yield return enumerator.Current; + } + } + } +} diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs index e5538e47e..90c7e6084 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/AbpAIErrorCodes.cs @@ -7,6 +7,10 @@ public static class AbpAIErrorCodes /// public const string WorkspaceIsNotEnabled = Namespace + ":110001"; /// + /// ApiKey缺失或无效! + /// + public const string MissingOrInvalidApiKey = Namespace + ":110002"; + /// /// 对话已过期, 请重新创建会话! /// public const string ConversationHasExpired = Namespace + ":110101"; diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json index 5e631f34c..e8f71a734 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/en.json @@ -2,6 +2,7 @@ "culture": "en", "texts": { "Abp.AI:110001": "Workspace is not enabled: {Workspace}!", + "Abp.AI:110002": "Missing or invalid API key!", "Abp.AI:110101": "The conversation has expired. Please create a new one!", "NewConversation": "New Conversation" } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json index 8681eedaf..4346e798a 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Localization/Resources/zh-Hans.json @@ -2,6 +2,7 @@ "culture": "zh-Hans", "texts": { "Abp.AI:110001": "工作区不可用: {Workspace}!", + "Abp.AI:110002": "ApiKey缺失或无效!", "Abp.AI:110101": "对话已过期, 请重新创建会话!", "NewConversation": "新对话" }