From 4f603429f44d2552021fcb18f2258654468237c0 Mon Sep 17 00:00:00 2001 From: colin Date: Thu, 15 Jan 2026 08:53:48 +0800 Subject: [PATCH] feat(ai): check state of use workspace --- .../Abp/AI/Agent/ChatClientAgentFactory.cs | 50 ++++++++++++++----- .../LINGYUN.Abp.AI.Core.csproj | 5 ++ .../LINGYUN/Abp/AI/ChatClientFactory.cs | 20 +++++++- .../LINGYUN/Abp/AI/KernelFactory.cs | 19 ++++++- 4 files changed, 79 insertions(+), 15 deletions(-) diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/ChatClientAgentFactory.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/ChatClientAgentFactory.cs index 62dd55dca..5f233f4e2 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/ChatClientAgentFactory.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/ChatClientAgentFactory.cs @@ -5,24 +5,29 @@ using Microsoft.Extensions.Localization; using System.Collections.Concurrent; using System.Threading.Tasks; using Volo.Abp.AI; +using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; +using Volo.Abp.SimpleStateChecking; namespace LINGYUN.Abp.AI.Agent; public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDependency { private readonly static ConcurrentDictionary _chatClientAgentCache = new(); - private readonly IChatClientFactory _chatClientFactory; - private readonly IStringLocalizerFactory _stringLocalizerFactory; - private readonly IWorkspaceDefinitionManager _workspaceDefinitionManager; + protected IChatClientFactory ChatClientFactory { get; } + protected IStringLocalizerFactory StringLocalizerFactory { get; } + protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } + protected ISimpleStateCheckerManager StateCheckerManager { get; } public ChatClientAgentFactory( IChatClientFactory chatClientFactory, IStringLocalizerFactory stringLocalizerFactory, - IWorkspaceDefinitionManager workspaceDefinitionManager) + IWorkspaceDefinitionManager workspaceDefinitionManager, + ISimpleStateCheckerManager stateCheckerManager) { - _chatClientFactory = chatClientFactory; - _stringLocalizerFactory = stringLocalizerFactory; - _workspaceDefinitionManager = workspaceDefinitionManager; + ChatClientFactory = chatClientFactory; + StringLocalizerFactory = stringLocalizerFactory; + WorkspaceDefinitionManager = workspaceDefinitionManager; + StateCheckerManager = stateCheckerManager; } public async virtual Task CreateAsync() @@ -33,14 +38,19 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende return chatClientAgent; } - var chatClient = await _chatClientFactory.CreateAsync(); + var chatClient = await ChatClientFactory.CreateAsync(); - var workspaceDefine = await _workspaceDefinitionManager.GetOrNullAsync(workspace); + var workspaceDefine = await WorkspaceDefinitionManager.GetOrNullAsync(workspace); + + if (workspaceDefine != null) + { + await CheckWorkspaceStateAsync(workspaceDefine); + } string? description = null; if (workspaceDefine?.Description != null) { - description = workspaceDefine.Description.Localize(_stringLocalizerFactory); + description = workspaceDefine.Description.Localize(StringLocalizerFactory); } chatClientAgent = chatClient.CreateAIAgent( @@ -59,13 +69,16 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende { return chatClientAgent; } - var workspaceDefine = await _workspaceDefinitionManager.GetAsync(workspace); - var chatClient = await _chatClientFactory.CreateAsync(workspace); + var workspaceDefine = await WorkspaceDefinitionManager.GetAsync(workspace); + + await CheckWorkspaceStateAsync(workspaceDefine); + + var chatClient = await ChatClientFactory.CreateAsync(workspace); string? description = null; if (workspaceDefine.Description != null) { - description = workspaceDefine.Description.Localize(_stringLocalizerFactory); + description = workspaceDefine.Description.Localize(StringLocalizerFactory); } chatClientAgent = chatClient.CreateAIAgent( @@ -77,4 +90,15 @@ public class ChatClientAgentFactory : IChatClientAgentFactory, ISingletonDepende return chatClientAgent; } + + protected async virtual Task CheckWorkspaceStateAsync(WorkspaceDefinition workspace) + { + if (!await StateCheckerManager.IsEnabledAsync(workspace)) + { + throw new AbpAuthorizationException( + $"Workspace is not enabled: {workspace.Name}!", + AbpAIErrorCodes.WorkspaceIsNotEnabled) + .WithData("Workspace", workspace.Name); + } + } } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN.Abp.AI.Core.csproj b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN.Abp.AI.Core.csproj index 1c67a9757..57d089b1c 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN.Abp.AI.Core.csproj +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN.Abp.AI.Core.csproj @@ -14,6 +14,11 @@ + + + + + diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/ChatClientFactory.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/ChatClientFactory.cs index bbdfa9c90..c9dd2e74c 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/ChatClientFactory.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/ChatClientFactory.cs @@ -5,21 +5,26 @@ using System.Collections.Concurrent; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.AI; +using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; +using Volo.Abp.SimpleStateChecking; namespace LINGYUN.Abp.AI; public class ChatClientFactory : IChatClientFactory, ISingletonDependency { private readonly static ConcurrentDictionary _chatClientCache = new(); + protected ISimpleStateCheckerManager StateCheckerManager { get; } protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } protected IChatClientProviderManager ChatClientProviderManager { get; } protected IServiceProvider ServiceProvider { get; } public ChatClientFactory( + ISimpleStateCheckerManager stateCheckerManager, IWorkspaceDefinitionManager workspaceDefinitionManager, IChatClientProviderManager chatClientProviderManager, IServiceProvider serviceProvider) { + StateCheckerManager = stateCheckerManager; WorkspaceDefinitionManager = workspaceDefinitionManager; ChatClientProviderManager = chatClientProviderManager; ServiceProvider = serviceProvider; @@ -58,6 +63,8 @@ public class ChatClientFactory : IChatClientFactory, ISingletonDependency var workspaceDefine = await WorkspaceDefinitionManager.GetAsync(workspace); + await CheckWorkspaceStateAsync(workspaceDefine); + chatClient = await CreateChatClientAsync(workspaceDefine); _chatClientCache.TryAdd(workspace, chatClient); @@ -77,6 +84,17 @@ public class ChatClientFactory : IChatClientFactory, ISingletonDependency return await provider.CreateAsync(workspace); } - throw new AbpException($"The ChatClient provider implementation named {workspace.Provider} was not found"); + throw new AbpException($"The ChatClient provider implementation named {workspace.Provider} was not found!"); + } + + protected async virtual Task CheckWorkspaceStateAsync(WorkspaceDefinition workspace) + { + if (!await StateCheckerManager.IsEnabledAsync(workspace)) + { + throw new AbpAuthorizationException( + $"Workspace is not enabled: {workspace.Name}!", + AbpAIErrorCodes.WorkspaceIsNotEnabled) + .WithData("Workspace", workspace.Name); + } } } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/KernelFactory.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/KernelFactory.cs index 5709d9127..a7067651a 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/KernelFactory.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/KernelFactory.cs @@ -5,21 +5,26 @@ using System.Collections.Concurrent; using System.Threading.Tasks; using Volo.Abp; using Volo.Abp.AI; +using Volo.Abp.Authorization; using Volo.Abp.DependencyInjection; +using Volo.Abp.SimpleStateChecking; namespace LINGYUN.Abp.AI; public class KernelFactory : IKernelFactory, ISingletonDependency { private readonly static ConcurrentDictionary _kernelCache = new(); + protected ISimpleStateCheckerManager StateCheckerManager { get; } protected IWorkspaceDefinitionManager WorkspaceDefinitionManager { get; } protected IKernelProviderManager KernelProviderManager { get; } protected IServiceProvider ServiceProvider { get; } public KernelFactory( + ISimpleStateCheckerManager stateCheckerManager, IWorkspaceDefinitionManager workspaceDefinitionManager, IKernelProviderManager kernelProviderManager, IServiceProvider serviceProvider) { + StateCheckerManager = stateCheckerManager; WorkspaceDefinitionManager = workspaceDefinitionManager; KernelProviderManager = kernelProviderManager; ServiceProvider = serviceProvider; @@ -58,6 +63,8 @@ public class KernelFactory : IKernelFactory, ISingletonDependency var workspaceDefine = await WorkspaceDefinitionManager.GetAsync(workspace); + await CheckWorkspaceStateAsync(workspaceDefine); + kernel = await CreateKernelAsync(workspaceDefine); _kernelCache.TryAdd(workspace, kernel); @@ -77,6 +84,16 @@ public class KernelFactory : IKernelFactory, ISingletonDependency return await provider.CreateAsync(workspace); } - throw new AbpException($"The Kernel provider implementation named {workspace.Provider} was not found"); + throw new AbpException($"The Kernel provider implementation named {workspace.Provider} was not found!"); + } + protected async virtual Task CheckWorkspaceStateAsync(WorkspaceDefinition workspace) + { + if (!await StateCheckerManager.IsEnabledAsync(workspace)) + { + throw new AbpAuthorizationException( + $"Workspace is not enabled: {workspace.Name}!", + AbpAIErrorCodes.WorkspaceIsNotEnabled) + .WithData("Workspace", workspace.Name); + } } }