From f6726548cdc6766c070aa1f546a3e9198fef767a Mon Sep 17 00:00:00 2001 From: colin Date: Fri, 16 Jan 2026 08:39:33 +0800 Subject: [PATCH] feat: The ConversationId is not mandatory. --- .../LINGYUN/Abp/AI/Agent/AgentFactory.cs | 2 +- .../LINGYUN/Abp/AI/Agent/AgentService.cs | 12 +++-- .../Abp/AI/Messages/IUserMessageStore.cs | 2 +- .../AI/Messages/InMemoryUserMessageStore.cs | 18 ++++--- .../LINGYUN/Abp/AI/Models/UserMessage.cs | 47 ++++++++++++++++--- .../Abp/AI/OpenAIChatClientProvider.cs | 1 - 6 files changed, 63 insertions(+), 19 deletions(-) diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs index 3739fdfd3..95b93584f 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Agent/LINGYUN/Abp/AI/Agent/AgentFactory.cs @@ -80,7 +80,7 @@ public class AgentFactory : IAgentFactory, IScopedDependency Tools = tools, }, Name = workspace?.Name, - Description = description + Description = description, }; var aiAgent = chatClient.CreateAIAgent(clientAgentOptions) 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 ed34c4200..899689c88 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,6 +2,7 @@ using LINGYUN.Abp.AI.Models; using LINGYUN.Abp.AI.Tokens; using Microsoft.Extensions.AI; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -49,11 +50,14 @@ public class AgentService : IAgentService, IScopedDependency { var messages = new List(); - var historyMessages = await _userMessageStore.GetHistoryMessagesAsync(message.ChatId); - - foreach (var chatMessage in historyMessages) + if (!message.ConversationId.IsNullOrWhiteSpace()) { - messages.Add(new ChatMessage(ChatRole.System, chatMessage.Content)); + var historyMessages = await _userMessageStore.GetHistoryMessagesAsync(message.ConversationId); + + foreach (var chatMessage in historyMessages) + { + messages.Add(new ChatMessage(ChatRole.System, chatMessage.Content)); + } } messages.Add(new ChatMessage(ChatRole.User, message.Content)); diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs index f9d8baf54..b56406d5f 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/IUserMessageStore.cs @@ -7,5 +7,5 @@ public interface IUserMessageStore { Task SaveMessageAsync(UserMessage message); - Task> GetHistoryMessagesAsync(string chatId); + Task> GetHistoryMessagesAsync(string conversationId); } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs index 2492a51ae..a12cf1de4 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Messages/InMemoryUserMessageStore.cs @@ -14,9 +14,9 @@ public class InMemoryUserMessageStore : IUserMessageStore { private static readonly ConcurrentDictionary> _userMessageCache = new ConcurrentDictionary>(); - public Task> GetHistoryMessagesAsync(string chatId) + public Task> GetHistoryMessagesAsync(string conversationId) { - if (_userMessageCache.TryGetValue(chatId, out var messages)) + if (_userMessageCache.TryGetValue(conversationId, out var messages)) { return Task.FromResult(messages.Take(5)); } @@ -26,15 +26,21 @@ public class InMemoryUserMessageStore : IUserMessageStore public Task SaveMessageAsync(UserMessage message) { - if (_userMessageCache.ContainsKey(message.ChatId)) + var messageId = message.Id; + if (messageId.IsNullOrWhiteSpace()) { - _userMessageCache[message.ChatId].Add(message); + messageId = Guid.NewGuid().ToString(); + message.WithMessageId(messageId); + } + if (_userMessageCache.ContainsKey(messageId)) + { + _userMessageCache[messageId].Add(message); } else { - _userMessageCache[message.ChatId] = new List() { message }; + _userMessageCache[messageId] = new List() { message }; } - return Task.FromResult(message.Id); + return Task.FromResult(messageId); } } diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs index f902b0c67..1c6a37557 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/Models/UserMessage.cs @@ -3,24 +3,59 @@ namespace LINGYUN.Abp.AI.Models; public class UserMessage { - public string Id { get; } - public string ChatId { get; } + /// + /// 消息内容 + /// public string Content { get; } + /// + /// 工作区 + /// public string Workspace { get; } + /// + /// 消息Id + /// + /// + /// 在持久化设施处更新 + /// + public string? Id { get; private set; } + /// + /// 对话Id + /// + /// + /// 用于从客户端存储中持久化和检索聊天历史的唯一标识符,如果未指定则与AI对话时无上下文关联 + /// + public string? ConversationId { get; private set; } + /// + /// AI回复消息 + /// public string ReplyMessage { get; private set; } + /// + /// 媒体附件 + /// + /// + /// 暂未实现 + /// public MediaMessage[]? Medias { get; private set; } public UserMessage( string workspace, - string id, - string chatId, string content) { Workspace = workspace; - Id = id; - ChatId = chatId; Content = content; } + public UserMessage WithMessageId(string id) + { + Id = id; + return this; + } + + public UserMessage WithConversationId(string conversationId) + { + ConversationId = conversationId; + return this; + } + public UserMessage WithMedia(MediaMessage media) { Medias ??= []; diff --git a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs index d13ac08a1..3501993b1 100644 --- a/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs +++ b/aspnet-core/modules/ai/LINGYUN.Abp.AI.Core/LINGYUN/Abp/AI/OpenAIChatClientProvider.cs @@ -38,7 +38,6 @@ public class OpenAIChatClientProvider : ChatClientProvider .UseOpenTelemetry() .UseFunctionInvocation() .UseDistributedCache() - .UseChatReducer() .Build(ServiceProvider); return Task.FromResult(chatClient);