From a3344c6cafc155ffa30d00e23b4c1020920c8398 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Wed, 19 Feb 2025 12:20:35 -0600 Subject: [PATCH] refine --- .../Agents/IAgentService.cs | 1 + .../BotSharp.Abstraction/Models/IdName.cs | 21 +++++++++++++++++++ .../Agents/Services/AgentService.GetAgents.cs | 10 +++++++++ .../Controllers/AgentController.cs | 6 ++++++ .../Models/DialogMongoElement.cs | 7 ++++--- .../MongoRepository.Conversation.cs | 2 +- 6 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Models/IdName.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs index 2ec4ff92..cf49da42 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/IAgentService.cs @@ -12,6 +12,7 @@ public interface IAgentService Task CreateAgent(Agent agent); Task RefreshAgents(); Task> GetAgents(AgentFilter filter); + Task> GetAgentOptions(); /// /// Load agent configurations and trigger hooks diff --git a/src/Infrastructure/BotSharp.Abstraction/Models/IdName.cs b/src/Infrastructure/BotSharp.Abstraction/Models/IdName.cs new file mode 100644 index 00000000..e5f7d8b5 --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Models/IdName.cs @@ -0,0 +1,21 @@ +namespace BotSharp.Abstraction.Models; + +public class IdName +{ + [JsonPropertyName("id")] + public string Id { get; set; } = default!; + + [JsonPropertyName("name")] + public string Name { get; set; } = default!; + + public IdName(string id, string name) + { + Id = id; + Name = name; + } + + public override string ToString() + { + return $"{Id}: {Name}"; + } +} diff --git a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs index 95018a2d..ec95a51e 100644 --- a/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs +++ b/src/Infrastructure/BotSharp.Core/Agents/Services/AgentService.GetAgents.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Models; using BotSharp.Abstraction.Routing.Settings; namespace BotSharp.Core.Agents.Services; @@ -26,6 +27,15 @@ public partial class AgentService }; } +#if !DEBUG + [SharpCache(10, perInstanceCache: true)] +#endif + public async Task> GetAgentOptions() + { + var agents = _db.GetAgents(AgentFilter.Empty()); + return agents?.Select(x => new IdName(x.Id, x.Name))?.OrderBy(x => x.Name)?.ToList() ?? []; + } + #if !DEBUG [SharpCache(10, perInstanceCache: true)] #endif diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs index 4a05b121..00c82ebe 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentController.cs @@ -149,6 +149,12 @@ public class AgentController : ControllerBase return await _agentService.DeleteAgent(agentId); } + [HttpGet("/agent/options")] + public async Task> GetAgentOptions() + { + return await _agentService.GetAgentOptions(); + } + [HttpGet("/agent/utility/options")] public IEnumerable GetAgentUtilityOptions() { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs index 53c09d4a..87022130 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs @@ -42,6 +42,7 @@ public class DialogMongoElement } } +[BsonIgnoreExtraElements(Inherited = true)] public class DialogMetaDataMongoElement { public string Role { get; set; } = default!; @@ -50,7 +51,7 @@ public class DialogMetaDataMongoElement public string MessageType { get; set; } = default!; public string? FunctionName { get; set; } public string? SenderId { get; set; } - public DateTime CreatedTime { get; set; } + public DateTime CreateTime { get; set; } public static DialogMetaData ToDomainElement(DialogMetaDataMongoElement meta) { @@ -62,7 +63,7 @@ public class DialogMetaDataMongoElement MessageType = meta.MessageType, FunctionName = meta.FunctionName, SenderId = meta.SenderId, - CreatedTime = meta.CreatedTime, + CreatedTime = meta.CreateTime, }; } @@ -76,7 +77,7 @@ public class DialogMetaDataMongoElement MessageType = meta.MessageType, FunctionName = meta.FunctionName, SenderId = meta.SenderId, - CreatedTime = meta.CreatedTime, + CreateTime = meta.CreatedTime, }; } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index 46e378d5..7d45e972 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -534,7 +534,7 @@ public partial class MongoRepository var truncatedDialogs = foundDialog.Dialogs.Where((x, idx) => idx < foundIdx).ToList(); // Handle truncated states - var refTime = foundDialog.Dialogs.ElementAt(foundIdx).MetaData.CreatedTime; + var refTime = foundDialog.Dialogs.ElementAt(foundIdx).MetaData.CreateTime; var stateFilter = Builders.Filter.Eq(x => x.ConversationId, conversationId); var foundStates = _dc.ConversationStates.Find(stateFilter).FirstOrDefault();