From e3b0a7f075ca486d06dd1b99067bb96635e6be73 Mon Sep 17 00:00:00 2001
From: Jicheng Lu <103353@smsassist.com>
Date: Sun, 26 Nov 2023 21:04:48 -0600
Subject: [PATCH] get channel from conversation
---
.../Conversations/IConversationService.cs | 4 +-
.../ConversationService.SendMessage.cs | 10 ++-
.../Evaluations/EvaluatingService.cs | 4 +-
.../Repository/FileRepository.cs | 2 +-
.../BotSharp.Core/Routing/RoutingService.cs | 7 +-
.../Controllers/ConversationController.cs | 13 +--
.../Conversations/NewMessageModel.cs | 3 +-
.../Instructs/InstructMessageModel.cs | 3 +-
.../ChatbotUiController.cs | 10 +--
.../ViewModels/OpenAiMessageInput.cs | 3 +-
.../Services/MessageHandleService.cs | 82 +++++++++----------
.../Controllers/TwilioVoiceController.cs | 21 +++--
12 files changed, 88 insertions(+), 74 deletions(-)
diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
index 38e017d6..44e90d32 100644
--- a/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
+++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/IConversationService.cs
@@ -16,13 +16,15 @@ public interface IConversationService
/// Send message to LLM
///
///
+ ///
///
///
///
/// This delegate is useful when you want to report progress on UI
/// This delegate is useful when you want to report progress on UI
///
- Task SendMessage(string agentId,
+ Task SendMessage(string agentId,
+ string channel,
RoleDialogModel lastDalog,
Func onMessageReceived,
Func onFunctionExecuting,
diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
index 613ab4fb..b393edd8 100644
--- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
+++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.SendMessage.cs
@@ -9,13 +9,14 @@ namespace BotSharp.Core.Conversations.Services;
public partial class ConversationService
{
- public async Task SendMessage(string agentId,
+ public async Task SendMessage(string agentId,
+ string channel,
RoleDialogModel message,
Func onMessageReceived,
Func onFunctionExecuting,
Func onFunctionExecuted)
{
- var conversation = await GetConversationRecord(agentId);
+ var conversation = await GetConversationRecord(agentId, channel);
var agentService = _services.GetRequiredService();
Agent agent = await agentService.LoadAgent(agentId);
@@ -69,16 +70,17 @@ public partial class ConversationService
return true;
}
- private async Task GetConversationRecord(string agentId)
+ private async Task GetConversationRecord(string agentId, string channel)
{
var converation = await GetConversation(_conversationId);
- // Create conversation if this conversation not exists
+ // Create conversation if this conversation does not exist
if (converation == null)
{
var sess = new Conversation
{
Id = _conversationId,
+ Channel = channel,
AgentId = agentId
};
converation = await NewConversation(sess);
diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/EvaluatingService.cs b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluatingService.cs
index 1cb826b7..d8824c7a 100644
--- a/src/Infrastructure/BotSharp.Core/Evaluations/EvaluatingService.cs
+++ b/src/Infrastructure/BotSharp.Core/Evaluations/EvaluatingService.cs
@@ -1,3 +1,4 @@
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Evaluations;
using BotSharp.Abstraction.Evaluations.Models;
using BotSharp.Abstraction.Evaluations.Settings;
@@ -92,7 +93,8 @@ public class EvaluatingService : IEvaluatingService
RoleDialogModel response = default;
await conv.SendMessage(agentId,
- new RoleDialogModel("user", text),
+ ConversationChannel.OpenAPI,
+ new RoleDialogModel(AgentRole.User, text),
async msg => response = msg,
fnExecuting => Task.CompletedTask,
fnExecuted => Task.CompletedTask);
diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
index e32d0877..6716f9f5 100644
--- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
+++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs
@@ -712,7 +712,7 @@ public class FileRepository : IBotSharpRepository
public Conversation GetConversation(string conversationId)
{
var convDir = FindConversationDirectory(conversationId);
- if (!string.IsNullOrEmpty(convDir)) return null;
+ if (string.IsNullOrEmpty(convDir)) return null;
var convFile = Path.Combine(convDir, "conversation.json");
var content = File.ReadAllText(convFile);
diff --git a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
index 72ab4aa9..1516f867 100644
--- a/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
+++ b/src/Infrastructure/BotSharp.Core/Routing/RoutingService.cs
@@ -143,11 +143,12 @@ public partial class RoutingService : IRoutingService
// Filter agents by profile
var state = _services.GetRequiredService();
- var name = state.GetState("channel");
- var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(name));
+ var conversation = db.GetConversation(state.GetConversationId());
+ var channel = conversation?.Channel;
+ var specifiedProfile = agents.FirstOrDefault(x => x.Profiles.Contains(channel));
if (specifiedProfile != null)
{
- records = records.Where(x => specifiedProfile.Profiles.Contains(name)).ToArray();
+ records = records.Where(x => specifiedProfile.Profiles.Contains(channel)).ToArray();
}
return records;
diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
index de82fbf1..b4d8a8e2 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs
@@ -1,4 +1,5 @@
using BotSharp.Abstraction.ApiAdapters;
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Models;
using BotSharp.OpenAPI.ViewModels.Conversations;
@@ -30,6 +31,7 @@ public class ConversationController : ControllerBase, IApiAdapter
var conv = new Conversation
{
AgentId = agentId,
+ Channel = ConversationChannel.OpenAPI,
UserId = _user.Id
};
conv = await service.NewConversation(conv);
@@ -97,15 +99,14 @@ public class ConversationController : ControllerBase, IApiAdapter
{
var conv = _services.GetRequiredService();
conv.SetConversationId(conversationId, input.States);
- conv.States.SetState("channel", input.Channel)
- .SetState("provider", input.Provider)
- .SetState("model", input.Model)
- .SetState("temperature", input.Temperature)
- .SetState("sampling_factor", input.SamplingFactor);
+ conv.States.SetState("provider", input.Provider)
+ .SetState("model", input.Model)
+ .SetState("temperature", input.Temperature)
+ .SetState("sampling_factor", input.SamplingFactor);
var response = new ChatResponseModel();
var inputMsg = new RoleDialogModel("user", input.Text);
- await conv.SendMessage(agentId, inputMsg,
+ await conv.SendMessage(agentId, input.Channel, inputMsg,
async msg =>
{
response.Text = msg.Content;
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
index 4b84f8f5..1750a303 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/NewMessageModel.cs
@@ -1,8 +1,9 @@
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.OpenAPI.ViewModels.Conversations;
public class NewMessageModel : IncomingMessageModel
{
- public override string Channel { get; set; } = "openapi";
+ public override string Channel { get; set; } = ConversationChannel.OpenAPI;
}
diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs
index 6da68895..33e5e168 100644
--- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs
+++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Instructs/InstructMessageModel.cs
@@ -1,8 +1,9 @@
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
namespace BotSharp.OpenAPI.ViewModels.Instructs;
public class InstructMessageModel : IncomingMessageModel
{
- public override string Channel { get; set; } = "openapi";
+ public override string Channel { get; set; } = ConversationChannel.OpenAPI;
public string? Template { get; set; }
}
diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
index 0fa194de..892d2894 100644
--- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
+++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ChatbotUiController.cs
@@ -77,12 +77,12 @@ public class ChatbotUiController : ControllerBase, IApiAdapter
var conv = _services.GetRequiredService();
conv.SetConversationId(input.ConversationId, input.States);
conv.States.SetState("provider", input.Provider)
- .SetState("model", input.Model)
- .SetState("channel", input.Channel)
- .SetState("temperature", input.Temperature)
- .SetState("sampling_factor", input.SamplingFactor);
+ .SetState("model", input.Model)
+ .SetState("temperature", input.Temperature)
+ .SetState("sampling_factor", input.SamplingFactor);
- var result = await conv.SendMessage(input.AgentId,
+ var result = await conv.SendMessage(input.AgentId,
+ input.Channel,
message,
async msg =>
await OnChunkReceived(outputStream, msg),
diff --git a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs
index 65dfd491..4ffdadbe 100644
--- a/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs
+++ b/src/Plugins/BotSharp.Plugin.ChatbotUI/ViewModels/OpenAiMessageInput.cs
@@ -1,3 +1,4 @@
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
using System.Collections.Generic;
using System.Linq;
@@ -9,7 +10,7 @@ public class OpenAiMessageInput : IncomingMessageModel
{
public string AgentId { get; set; } = string.Empty;
public string ConversationId { get; set; } = string.Empty;
- public override string Channel { get; set; } = "webchat";
+ public override string Channel { get; set; } = ConversationChannel.WebChat;
public List Messages { get; set; } = new List();
diff --git a/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs b/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs
index 465eca07..6e84ec49 100644
--- a/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs
+++ b/src/Plugins/BotSharp.Plugin.MetaMessenger/Services/MessageHandleService.cs
@@ -1,3 +1,5 @@
+using BotSharp.Abstraction.Agents.Enums;
+using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Messaging.Models.RichContent;
using BotSharp.Abstraction.Utilities;
@@ -46,60 +48,58 @@ public class MessageHandleService
// Go to LLM
var conv = _services.GetRequiredService();
- conv.SetConversationId(sender, new List
- {
- "channel=messenger"
- });
+ conv.SetConversationId(sender, new List());
var replies = new List();
- var result = await conv.SendMessage(agentId, new RoleDialogModel("user", message), async msg =>
- {
- if (msg.RichContent != null)
+ var result = await conv.SendMessage(agentId, ConversationChannel.Messenger,
+ new RoleDialogModel(AgentRole.User, message), async msg =>
{
- // Official API doesn't support to show extra content above the products
- if (!string.IsNullOrEmpty(msg.RichContent.Message.Text) &&
- // avoid duplicated text
- msg.RichContent.Message is not QuickReplyMessage)
+ if (msg.RichContent != null)
{
- replies.Add(new TextMessage(msg.RichContent.Message.Text));
- }
+ // Official API doesn't support to show extra content above the products
+ if (!string.IsNullOrEmpty(msg.RichContent.Message.Text) &&
+ // avoid duplicated text
+ msg.RichContent.Message is not QuickReplyMessage)
+ {
+ replies.Add(new TextMessage(msg.RichContent.Message.Text));
+ }
- if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
- {
- replies.Add(new AttachmentMessage
+ if (msg.RichContent.Message is GenericTemplateMessage genericTemplate)
{
- Attachment = new AttachmentBody
+ replies.Add(new AttachmentMessage
{
- Payload = genericTemplate
- }
- });
- }
- else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
- {
- replies.Add(new AttachmentMessage
+ Attachment = new AttachmentBody
+ {
+ Payload = genericTemplate
+ }
+ });
+ }
+ else if (msg.RichContent.Message is CouponTemplateMessage couponTemplate)
{
- Attachment = new AttachmentBody
+ replies.Add(new AttachmentMessage
{
- Payload = couponTemplate
- }
- });
- }
- else if (msg.RichContent.Message is QuickReplyMessage quickReplyMessage)
- {
- replies.Add(quickReplyMessage);
+ Attachment = new AttachmentBody
+ {
+ Payload = couponTemplate
+ }
+ });
+ }
+ else if (msg.RichContent.Message is QuickReplyMessage quickReplyMessage)
+ {
+ replies.Add(quickReplyMessage);
+ }
+ else
+ {
+ replies.Add(msg.RichContent.Message);
+ }
}
else
{
- replies.Add(msg.RichContent.Message);
+ replies.Add(new TextMessage(msg.Content));
}
- }
- else
- {
- replies.Add(new TextMessage(msg.Content));
- }
- },
- _ => Task.CompletedTask,
- _ => Task.CompletedTask);
+ },
+ _ => Task.CompletedTask,
+ _ => Task.CompletedTask);
// Response to user
foreach(var reply in replies)
diff --git a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs
index 7913c056..85b75834 100644
--- a/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs
+++ b/src/Plugins/BotSharp.Plugin.Twilio/Controllers/TwilioVoiceController.cs
@@ -15,6 +15,7 @@ using Twilio.Http;
using Twilio.TwiML.Messaging;
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.Conversations.Models;
+using BotSharp.Abstraction.Conversations.Enums;
namespace BotSharp.Plugin.Twilio.Controllers;
@@ -46,20 +47,22 @@ public class TwilioVoiceController : TwilioController
var conv = _services.GetRequiredService();
conv.SetConversationId(sessionId, new List
{
- "channel=phone",
$"calling_phone={input.DialCallSid}"
});
VoiceResponse response = default;
- var result = await conv.SendMessage(agentId, new RoleDialogModel(AgentRole.User, input.SpeechResult), async msg =>
- {
- response = HangUp(msg.Content);
- }, async functionExecuting =>
- {
- }, async functionExecuted =>
- {
- });
+ var result = await conv.SendMessage(agentId,
+ ConversationChannel.Phone,
+ new RoleDialogModel(AgentRole.User, input.SpeechResult),
+ async msg =>
+ {
+ response = HangUp(msg.Content);
+ }, async functionExecuting =>
+ {
+ }, async functionExecuted =>
+ {
+ });
return TwiML(response);
}