From 72e9b2005a80d95371f9f4b289ddea7d641a46a1 Mon Sep 17 00:00:00 2001 From: colin Date: Wed, 1 Apr 2026 10:54:35 +0800 Subject: [PATCH] feat(ai): Optimize the configuration of AI tools --- .../LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs | 8 +-- .../Abp/AI/Tools/IWorkspaceAIToolFinder.cs | 5 -- .../Abp/AI/Tools/WorkspaceAIToolFinder.cs | 20 +----- .../Chats/ConversationChangeNameHandler.cs | 70 +++++++++---------- 4 files changed, 38 insertions(+), 65 deletions(-) diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs index 2cbdd7ef0..2da4d9526 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/AbpAIToolsModule.cs @@ -35,12 +35,8 @@ public class AbpAIToolsModule : AbpModule { options.ChatClientBuildActions.Add(async (workspace, sp, builder) => { - IList? workspaceAITools = default!; - var workspaceAIToolFinder = sp.GetService(); - if (workspaceAIToolFinder != null && workspaceAIToolFinder.IsAIToolEnabled()) - { - workspaceAITools = await workspaceAIToolFinder.GetToolsAsync(workspace); - } + var workspaceAIToolFinder = sp.GetRequiredService(); + var workspaceAITools = await workspaceAIToolFinder.GetToolsAsync(workspace); return builder .ConfigureOptions(config => diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs index 2e0437c5e..dbc14b516 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/IWorkspaceAIToolFinder.cs @@ -1,14 +1,9 @@ using LINGYUN.Abp.AI.Workspaces; using Microsoft.Extensions.AI; -using System; using System.Threading.Tasks; namespace LINGYUN.Abp.AI.Tools; public interface IWorkspaceAIToolFinder { - IDisposable DisableAITool(); - Task GetToolsAsync(WorkspaceDefinition workspace); - - bool IsAIToolEnabled(); } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs index 6fd57ef7c..ee587a836 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Tools/LINGYUN/Abp/AI/Tools/WorkspaceAIToolFinder.cs @@ -1,34 +1,22 @@ using LINGYUN.Abp.AI.Workspaces; using Microsoft.Extensions.AI; -using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; -using Volo.Abp.Threading; namespace LINGYUN.Abp.AI.Tools; public class WorkspaceAIToolFinder : IWorkspaceAIToolFinder, ITransientDependency { - private const string AIToolDisabledScopeKey = "Abp.AI.Tools.DisabledAITool"; - private readonly IAIToolFactory _aiToolFactory; private readonly IAIToolDefinitionManager _aiToolDefinitionManager; - private readonly IAmbientScopeProvider _aiToolDisabledState; public WorkspaceAIToolFinder( IAIToolFactory aiToolFactory, - IAIToolDefinitionManager aiToolDefinitionManager, - IAmbientScopeProvider aiToolDisabledState) + IAIToolDefinitionManager aiToolDefinitionManager) { _aiToolFactory = aiToolFactory; _aiToolDefinitionManager = aiToolDefinitionManager; - _aiToolDisabledState = aiToolDisabledState; - } - - public virtual IDisposable DisableAITool() - { - return _aiToolDisabledState.BeginScope(AIToolDisabledScopeKey, new AIToolDisabledState(true)); } public async virtual Task GetToolsAsync(WorkspaceDefinition workspace) @@ -61,10 +49,4 @@ public class WorkspaceAIToolFinder : IWorkspaceAIToolFinder, ITransientDependenc return useAITools.ToArray(); } - - public virtual bool IsAIToolEnabled() - { - var state = _aiToolDisabledState.GetValue(AIToolDisabledScopeKey); - return state == null || !state.IsDisabled; - } } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs index 4c254c319..89f59ccc0 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AIManagement.Domain/LINGYUN/Abp/AIManagement/Chats/ConversationChangeNameHandler.cs @@ -1,5 +1,4 @@ using LINGYUN.Abp.AI; -using LINGYUN.Abp.AI.Tools; using LINGYUN.Abp.AIManagement.Localization; using LINGYUN.Abp.AIManagement.Tokens; using Microsoft.Extensions.AI; @@ -26,7 +25,6 @@ public class ConversationChangeNameHandler : private readonly IGuidGenerator _guidGenerator; private readonly IAbpDistributedLock _distributedLock; private readonly IChatClientFactory _chatClientFactory; - private readonly IWorkspaceAIToolFinder _workspaceAIToolFinder; private readonly IStringLocalizer _stringLocalizer; private readonly ITokenUsageRecordRepository _tokenUsageRecordRepository; private readonly IConversationRecordRepository _conversationRecordRepository; @@ -37,7 +35,6 @@ public class ConversationChangeNameHandler : IGuidGenerator guidGenerator, IAbpDistributedLock distributedLock, IChatClientFactory chatClientFactory, - IWorkspaceAIToolFinder workspaceAIToolFinder, IStringLocalizer stringLocalizer, ITokenUsageRecordRepository tokenUsageRecordRepository, IConversationRecordRepository conversationRecordRepository, @@ -47,7 +44,6 @@ public class ConversationChangeNameHandler : _guidGenerator = guidGenerator; _distributedLock = distributedLock; _chatClientFactory = chatClientFactory; - _workspaceAIToolFinder = workspaceAIToolFinder; _stringLocalizer = stringLocalizer; _tokenUsageRecordRepository = tokenUsageRecordRepository; _conversationRecordRepository = conversationRecordRepository; @@ -101,40 +97,44 @@ public class ConversationChangeNameHandler : var chatClient = await _chatClientFactory.CreateAsync(chatMessage.Workspace); var instructions = _stringLocalizer["DesignConversationNamePrompt", ConversationRecordConsts.MaxNameLength].Value; - // 禁用AI工具 - using (_workspaceAIToolFinder.DisableAITool()) - { - var aiAgent = chatClient - .CreateAIAgent( - instructions: instructions, - services: _serviceProvider); - - var agentRunRes = await aiAgent.RunAsync([ - new ChatMessage(ChatRole.System, instructions), - new ChatMessage(ChatRole.User, chatMessage.Content)]); - - conversation.SetName( - agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength + var aiAgent = chatClient + .AsBuilder() + .ConfigureOptions(options => + { + // 禁用工具 + options.Tools = []; + }) + .BuildAIAgent( + instructions: instructions, + services: _serviceProvider); + + var agentRunRes = await aiAgent.RunAsync([ + new ChatMessage(ChatRole.System, instructions), + new ChatMessage(ChatRole.User, chatMessage.Content)]); + + conversation.SetName( + agentRunRes.Text.Length > ConversationRecordConsts.MaxNameLength + ? chatMessage.Content.Length > ConversationRecordConsts.MaxNameLength ? chatMessage.Content[..ConversationRecordConsts.MaxNameLength] - : agentRunRes.Text); + : chatMessage.Content + : agentRunRes.Text); - await _conversationRecordRepository.UpdateAsync(conversation); + await _conversationRecordRepository.UpdateAsync(conversation); - if (agentRunRes.Usage != null) - { - var tokenUsageRecord = new TokenUsageRecord( - _guidGenerator.Create(), - chatMessage.Id, - conversation.Id, - agentRunRes.Usage.InputTokenCount, - agentRunRes.Usage.OutputTokenCount, - agentRunRes.Usage.TotalTokenCount, - agentRunRes.Usage.CachedInputTokenCount, - agentRunRes.Usage.ReasoningTokenCount, - chatMessage.TenantId); - - await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord); - } + if (agentRunRes.Usage != null) + { + var tokenUsageRecord = new TokenUsageRecord( + _guidGenerator.Create(), + chatMessage.Id, + conversation.Id, + agentRunRes.Usage.InputTokenCount, + agentRunRes.Usage.OutputTokenCount, + agentRunRes.Usage.TotalTokenCount, + agentRunRes.Usage.CachedInputTokenCount, + agentRunRes.Usage.ReasoningTokenCount, + chatMessage.TenantId); + + await _tokenUsageRecordRepository.InsertAsync(tokenUsageRecord); } } }