From 596a83655e73ac2642602cbd7eca3a64bf6f2b5c Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Mon, 12 Feb 2024 09:14:33 -0600 Subject: [PATCH] Add TaskId to conversation. --- .../Conversations/Models/Conversation.cs | 5 +++++ .../BotSharp.Abstraction/Models/MessageConfig.cs | 5 +++++ .../Repositories/Filters/ConversationFilter.cs | 5 +++++ .../FileRepository/FileRepository.Conversation.cs | 1 + .../Controllers/ConversationController.cs | 5 +++-- .../ViewModels/Conversations/ConversationViewModel.cs | 8 ++++++++ .../Collections/ConversationDocument.cs | 1 + .../Repository/MongoRepository.Conversation.cs | 3 +++ 8 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs index 1903e913..c8332a3d 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs @@ -7,6 +7,11 @@ public class Conversation public string Id { get; set; } = string.Empty; public string AgentId { get; set; } = string.Empty; public string UserId { get; set; } = string.Empty; + + /// + /// Agent task id + /// + public string? TaskId { get; set; } public string Title { get; set; } = string.Empty; [JsonIgnore] diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs b/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs index 438cc060..17ffeee4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Models/MessageConfig.cs @@ -29,4 +29,9 @@ public class MessageConfig : TruncateMessageRequest /// Conversation states from input /// public List States { get; set; } = new List(); + + /// + /// Agent task id + /// + public string? TaskId { get; set; } } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs index 3d236d9e..33571223 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Filters/ConversationFilter.cs @@ -11,4 +11,9 @@ public class ConversationFilter public string? Status { get; set; } public string? Channel { get; set; } public string? UserId { get; set; } + + /// + /// Agent task id + /// + public string? TaskId { get; set; } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index 36eaa3ed..0b17b492 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -223,6 +223,7 @@ namespace BotSharp.Core.Repository if (filter?.Status != null) matched = matched && record.Status == filter.Status; if (filter?.Channel != null) matched = matched && record.Channel == filter.Channel; if (filter?.UserId != null) matched = matched && record.UserId == filter.UserId; + if (filter?.TaskId != null) matched = matched && record.TaskId == filter.TaskId; if (!matched) continue; records.Add(record); diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 47411167..7443b82d 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -24,7 +24,8 @@ public class ConversationController : ControllerBase { AgentId = agentId, Channel = ConversationChannel.OpenAPI, - UserId = _user.Id + UserId = _user.Id, + TaskId = config.TaskId }; conv = await service.NewConversation(conv); service.SetConversationId(conv.Id, config.States); @@ -50,7 +51,7 @@ public class ConversationController : ControllerBase item.User = UserViewModel.FromUser(user); var agent = await agentService.GetAgent(item.AgentId); - item.AgentName = agent.Name; + item.AgentName = agent?.Name; } return new PagedItems diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs index 0823023d..c50ecc88 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ConversationViewModel.cs @@ -19,6 +19,13 @@ public class ConversationViewModel public string Event { get; set; } public string Channel { get; set; } = ConversationChannel.OpenAPI; + + /// + /// Agent task id + /// + [JsonPropertyName("task_id")] + public string? TaskId { get; set; } + public string Status { get; set; } public Dictionary States { get; set; } @@ -40,6 +47,7 @@ public class ConversationViewModel Title = sess.Title, Channel = sess.Channel, Status = sess.Status, + TaskId = sess.TaskId, CreatedTime = sess.CreatedTime, UpdatedTime = sess.UpdatedTime }; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDocument.cs index 877d1c96..b14ca62f 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationDocument.cs @@ -4,6 +4,7 @@ public class ConversationDocument : MongoBase { public string AgentId { get; set; } public string UserId { get; set; } + public string? TaskId { get; set; } public string Title { get; set; } public string Channel { get; set; } public string Status { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 9f1b2a20..1b113537 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -19,6 +19,7 @@ public partial class MongoRepository UserId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty, Title = conversation.Title, Channel = conversation.Channel, + TaskId = conversation.TaskId, Status = conversation.Status, CreatedTime = DateTime.UtcNow, UpdatedTime = DateTime.UtcNow, @@ -215,6 +216,7 @@ public partial class MongoRepository if (!string.IsNullOrEmpty(filter.Status)) filters.Add(builder.Eq(x => x.Status, filter.Status)); if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel)); if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId)); + if (!string.IsNullOrEmpty(filter.TaskId)) filters.Add(builder.Eq(x => x.TaskId, filter.TaskId)); var filterDef = builder.And(filters); var sortDef = Builders.Sort.Descending(x => x.CreatedTime); @@ -230,6 +232,7 @@ public partial class MongoRepository Id = convId, AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), + TaskId = conv.TaskId, Title = conv.Title, Channel = conv.Channel, Status = conv.Status,