From 29af7cdea023515156c87cf1db498e4d3b1fb65a Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 9 Sep 2024 12:26:26 -0500 Subject: [PATCH 1/2] add exclude agent ids --- .../Conversations/IConversationService.cs | 2 +- .../Conversations/Settings/ConversationSetting.cs | 6 +++--- .../Repositories/IBotSharpRepository.cs | 2 +- .../Conversations/Services/ConversationService.cs | 4 ++-- .../BotSharp.Core/Repository/BotSharpDbContext.cs | 2 +- .../FileRepository/FileRepository.Conversation.cs | 4 ++-- .../BackgroundServices/ConversationTimeoutService.cs | 2 +- .../Repository/MongoRepository.Conversation.cs | 4 ++-- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs index 3cc2c5fd..6b29a72b 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs @@ -13,7 +13,7 @@ public interface IConversationService Task> GetConversations(ConversationFilter filter); Task UpdateConversationTitle(string id, string title); Task> GetLastConversations(); - Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours); + Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds); Task DeleteConversations(IEnumerable ids); /// diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs index 9df16428..bf29f521 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Settings/ConversationSetting.cs @@ -12,8 +12,8 @@ public class ConversationSetting public bool EnableContentLog { get; set; } public bool EnableStateLog { get; set; } public bool EnableTranslationMemory { get; set; } - public CleanConversationSetting CleanSetting { get; set; } = new CleanConversationSetting(); - public RateLimitSetting RateLimit { get; set; } = new RateLimitSetting(); + public CleanConversationSetting CleanSetting { get; set; } = new(); + public RateLimitSetting RateLimit { get; set; } = new(); } public class CleanConversationSetting @@ -22,5 +22,5 @@ public class CleanConversationSetting public int BatchSize { get; set; } public int MessageLimit { get; set; } public int BufferHours { get; set; } - + public IEnumerable ExcludeAgentIds { get; set; } = new List(); } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 0af44080..ed5d2dab 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -67,7 +67,7 @@ public interface IBotSharpRepository void UpdateConversationBreakpoint(string conversationId, ConversationBreakpoint breakpoint); ConversationBreakpoint? GetConversationBreakpoint(string conversationId); List GetLastConversations(); - List GetIdleConversations(int batchSize, int messageLimit, int bufferHours); + List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds); IEnumerable TruncateConversation(string conversationId, string messageId, bool cleanLog = false); #endregion diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index f2a93b3f..6e0f97d1 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -70,10 +70,10 @@ public partial class ConversationService : IConversationService return db.GetLastConversations(); } - public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public async Task> GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var db = _services.GetRequiredService(); - return db.GetIdleConversations(batchSize, messageLimit, bufferHours); + return db.GetIdleConversations(batchSize, messageLimit, bufferHours, excludeAgentIds ?? new List()); } public async Task NewConversation(Conversation sess) diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 5ce76901..ebaa8ee1 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -145,7 +145,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository public List GetLastConversations() => throw new NotImplementedException(); - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) => throw new NotImplementedException(); public List GetConversationDialogs(string conversationId) diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index e4bb5098..6f078b61 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -378,7 +378,7 @@ namespace BotSharp.Core.Repository .ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var ids = new List(); var batchLimit = 100; @@ -423,7 +423,7 @@ namespace BotSharp.Core.Repository continue; } - if (conv.UpdatedTime > utcNow.AddHours(-bufferHours)) + if (excludeAgentIds.Contains(conv.AgentId) || conv.UpdatedTime > utcNow.AddHours(-bufferHours)) { continue; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs b/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs index c1222a4e..a409ea14 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/BackgroundServices/ConversationTimeoutService.cs @@ -62,7 +62,7 @@ namespace BotSharp.OpenAPI.BackgroundServices if (cleanSetting == null || !cleanSetting.Enable) return; var conversationService = scope.ServiceProvider.GetRequiredService(); - var conversationIds = await conversationService.GetIdleConversations(cleanSetting.BatchSize, cleanSetting.MessageLimit, cleanSetting.BufferHours); + var conversationIds = await conversationService.GetIdleConversations(cleanSetting.BatchSize, cleanSetting.MessageLimit, cleanSetting.BufferHours, cleanSetting.ExcludeAgentIds); if (!conversationIds.IsNullOrEmpty()) { diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs index e3f95f27..9aed7265 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs @@ -344,7 +344,7 @@ public partial class MongoRepository }).ToList(); } - public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours) + public List GetIdleConversations(int batchSize, int messageLimit, int bufferHours, IEnumerable excludeAgentIds) { var page = 1; var batchLimit = 100; @@ -370,7 +370,7 @@ public partial class MongoRepository { var skip = (page - 1) * batchSize; var candidates = _dc.Conversations.AsQueryable() - .Where(x => x.DialogCount <= messageLimit && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) + .Where(x => !excludeAgentIds.Contains(x.AgentId) && x.DialogCount <= messageLimit && x.UpdatedTime <= utcNow.AddHours(-bufferHours)) .Skip(skip) .Take(batchSize) .Select(x => x.Id) From 79dbfe9e21f5c945541a8851a308dd4d86f81e95 Mon Sep 17 00:00:00 2001 From: Bo Yin Date: Mon, 9 Sep 2024 15:27:09 -0500 Subject: [PATCH 2/2] support reply hints --- .../Controllers/TwilioVoiceController.cs | 2 +- .../Models/AssistantMessage.cs | 1 + .../Services/TwilioMessageQueueService.cs | 21 ++++++++++++++++++- .../Services/TwilioService.cs | 4 ++-- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs index 4e37ac78..f9c05a06 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs @@ -184,7 +184,7 @@ public class TwilioVoiceController : TwilioController response = twilio.ReturnInstructions(new List { $"twilio/voice/speeches/{conversationId}/{reply.SpeechFileName}" - }, $"twilio/voice/{conversationId}/receive/{nextSeqNum}?states={states}", true); + }, $"twilio/voice/{conversationId}/receive/{nextSeqNum}?states={states}", true, hints:reply.Hints); } } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Models/AssistantMessage.cs b/src/Plugins/BotSharp.Plugin.Twilio/Models/AssistantMessage.cs index 521dba43..2587fed9 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Models/AssistantMessage.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Models/AssistantMessage.cs @@ -7,5 +7,6 @@ namespace BotSharp.Plugin.Twilio.Models public string Content { get; set; } public string MessageId { get; set; } public string SpeechFileName { get; set; } + public string Hints { get; set; } } } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs index 4679c653..cd5643f1 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioMessageQueueService.cs @@ -106,8 +106,27 @@ namespace BotSharp.Plugin.Twilio.Services var data = await completion.GenerateAudioFromTextAsync(reply.Content); var fileName = $"reply_{reply.MessageId}.mp3"; fileStorage.SaveSpeechFile(message.ConversationId, fileName, data); - reply.SpeechFileName = fileName; + var phrases = reply.Content.Split(',', StringSplitOptions.RemoveEmptyEntries); + int capcity = 100; + var hints = new List(capcity); + for (int i = phrases.Length - 1; i >= 0; i--) + { + var words = phrases[i].Split(' ', StringSplitOptions.RemoveEmptyEntries); + for (int j = words.Length - 1; j >= 0; j--) + { + hints.Add(words[j]); + if (hints.Count >= capcity) + { + break; + } + } + if (hints.Count >= capcity) + { + break; + } + } + reply.Hints = string.Join(',', hints); reply.Content = null; await sessionManager.SetAssistantReplyAsync(message.ConversationId, message.SeqNumber, reply); } diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs index d72f549b..0955e65d 100644 --- a/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs +++ b/src/Plugins/BotSharp.Plugin.Twilio/Services/TwilioService.cs @@ -66,7 +66,7 @@ public class TwilioService return response; } - public VoiceResponse ReturnInstructions(List speechPaths, string callbackPath, bool actionOnEmptyResult, int timeout = 3) + public VoiceResponse ReturnInstructions(List speechPaths, string callbackPath, bool actionOnEmptyResult, int timeout = 3, string hints = null) { var response = new VoiceResponse(); var gather = new Gather() @@ -81,7 +81,7 @@ public class TwilioService SpeechTimeout = "auto", // timeout > 0 ? timeout.ToString() : "3", Timeout = timeout > 0 ? timeout : 3, ActionOnEmptyResult = actionOnEmptyResult, - Hints = "Yes, No, Correct" + Hints = hints }; if (!speechPaths.IsNullOrEmpty())