From 91093f6a04131466c983b5051f3334d3db171bd3 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Tue, 7 May 2024 11:55:44 -0500 Subject: [PATCH] add payload --- .../Conversations/Models/Conversation.cs | 4 ++- .../Conversations/Models/RoleDialogModel.cs | 4 +++ .../Services/ConversationStorage.cs | 20 +++++++++-- .../FileRepository.Conversation.cs | 34 ++++++++++++------- .../Controllers/ConversationController.cs | 3 +- .../Conversations/ChatResponseModel.cs | 4 +++ .../Models/DialogMongoElement.cs | 7 ++-- 7 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs index c99a819b..5613c088 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs @@ -37,6 +37,7 @@ public class DialogElement public string? SecondaryContent { get; set; } public string? RichContent { get; set; } public string? SecondaryRichContent { get; set; } + public string? Payload { get; set; } public DialogElement() { @@ -44,13 +45,14 @@ public class DialogElement } public DialogElement(DialogMetaData meta, string content, string? richContent = null, - string? secondaryContent = null, string? secondaryRichContent = null) + string? secondaryContent = null, string? secondaryRichContent = null, string? payload = null) { MetaData = meta; Content = content; RichContent = richContent; SecondaryContent = secondaryContent; SecondaryRichContent = secondaryRichContent; + Payload = payload; } public override string ToString() diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs index c3a6403a..e019eae4 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/RoleDialogModel.cs @@ -28,6 +28,10 @@ public class RoleDialogModel : ITrackableMessage public string? SecondaryContent { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("payload")] + public string? Payload { get; set; } + /// /// Indicator message used to provide UI feedback for function execution /// diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index 4ab447b8..9bb8f103 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -50,7 +50,13 @@ public class ConversationStorage : IConversationStorage { return; } - dialogElements.Add(new DialogElement(meta, content, dialog.SecondaryContent)); + dialogElements.Add(new DialogElement + { + MetaData = meta, + Content = dialog.Content, + SecondaryContent = dialog.SecondaryContent, + Payload = dialog.Payload + }); } else { @@ -72,7 +78,15 @@ public class ConversationStorage : IConversationStorage var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options.JsonSerializerOptions) : null; var secondaryRichContent = dialog.SecondaryRichContent != null ? JsonSerializer.Serialize(dialog.SecondaryRichContent, _options.JsonSerializerOptions) : null; - dialogElements.Add(new DialogElement(meta, content, richContent, dialog.SecondaryContent, secondaryRichContent)); + dialogElements.Add(new DialogElement + { + MetaData = meta, + Content = dialog.Content, + SecondaryContent = dialog.SecondaryContent, + RichContent = richContent, + SecondaryRichContent = secondaryRichContent, + Payload = dialog.Payload + }); } db.AppendConversationDialogs(conversationId, dialogElements); @@ -90,6 +104,7 @@ public class ConversationStorage : IConversationStorage var meta = dialog.MetaData; var content = dialog.Content; var secondaryContent = dialog.SecondaryContent; + var payload = dialog.Payload; var role = meta.Role; var currentAgentId = meta.AgentId; var messageId = meta.MessageId; @@ -111,6 +126,7 @@ public class ConversationStorage : IConversationStorage RichContent = richContent, SecondaryContent = secondaryContent, SecondaryRichContent = secondaryRichContent, + Payload = payload }; results.Add(record); diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs index d9400e47..6708f49a 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs @@ -529,6 +529,7 @@ namespace BotSharp.Core.Repository var trimmedContent = content.Substring(4); var secondaryContent = rawDialogs[i + 4]; var trimmedSecondaryContent = string.IsNullOrEmpty(secondaryContent) ? null : secondaryContent.Substring(4); + var payload = blocks.Count() > 6 ? blocks[6] : null; var meta = new DialogMetaData { @@ -540,9 +541,17 @@ namespace BotSharp.Core.Repository CreateTime = DateTime.Parse(blocks[0]) }; - var richContent = DecodeRichContent(rawDialogs[i + 1]); - var secondaryRichContent = DecodeRichContent(rawDialogs[i + 3]); - dialogs.Add(new DialogElement(meta, trimmedContent, richContent, trimmedSecondaryContent, secondaryRichContent)); + var richContent = DecodeText(rawDialogs[i + 1]); + var secondaryRichContent = DecodeText(rawDialogs[i + 3]); + dialogs.Add(new DialogElement + { + MetaData = meta, + Content = trimmedContent, + SecondaryContent = trimmedSecondaryContent, + RichContent = richContent, + SecondaryRichContent = secondaryRichContent, + Payload = payload + }); } } return dialogs; @@ -557,9 +566,10 @@ namespace BotSharp.Core.Repository { var meta = element.MetaData; var createTime = meta.CreateTime.ToString("MM/dd/yyyy hh:mm:ss.ffffff tt", CultureInfo.InvariantCulture); - var encodedRichContent = EncodeRichContent(element.RichContent); - var encodedSecondaryRichContent = EncodeRichContent(element.SecondaryRichContent); - var metaStr = $"{createTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{meta.SenderId}|{meta.FunctionName}"; + var encodedRichContent = EncodeText(element.RichContent); + var encodedSecondaryRichContent = EncodeText(element.SecondaryRichContent); + var payload = element.Payload; + var metaStr = $"{createTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{meta.SenderId}|{meta.FunctionName}|{payload}"; dialogTexts.Add(metaStr); dialogTexts.Add(encodedRichContent); @@ -715,20 +725,20 @@ namespace BotSharp.Core.Repository return true; } - private string? EncodeRichContent(string? content) + private string? EncodeText(string? text) { - if (string.IsNullOrEmpty(content)) return content; + if (string.IsNullOrEmpty(text)) return text; - var bytes = Encoding.UTF8.GetBytes(content); + var bytes = Encoding.UTF8.GetBytes(text); var encoded = Convert.ToBase64String(bytes); return encoded; } - private string? DecodeRichContent(string? content) + private string? DecodeText(string? text) { - if (string.IsNullOrEmpty(content)) return content; + if (string.IsNullOrEmpty(text)) return text; - var decoded = Convert.FromBase64String(content); + var decoded = Convert.FromBase64String(text); var origin = Encoding.UTF8.GetString(decoded); return origin; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index c85b8e43..8d91d111 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -89,7 +89,8 @@ public class ConversationController : ControllerBase CreatedAt = message.CreatedAt, Text = !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content, Data = message.Data, - Sender = UserViewModel.FromUser(user) + Sender = UserViewModel.FromUser(user), + Payload = message.Payload }); } else if (message.Role == AgentRole.Assistant) diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ChatResponseModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ChatResponseModel.cs index cf71b8f9..a03f1211 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ChatResponseModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/ChatResponseModel.cs @@ -28,6 +28,10 @@ public class ChatResponseModel : InstructResult [JsonPropertyName("rich_content")] public RichContent? RichContent { get; set; } + [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)] + [JsonPropertyName("payload")] + public string? Payload { get; set; } + [JsonPropertyName("created_at")] public DateTime CreatedAt { get; set; } = DateTime.UtcNow; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs index 6ebc16ff..5c2a1698 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs @@ -9,6 +9,7 @@ public class DialogMongoElement public string? SecondaryContent { get; set; } public string? RichContent { get; set; } public string? SecondaryRichContent { get; set; } + public string? Payload { get; set; } public DialogMongoElement() { @@ -23,7 +24,8 @@ public class DialogMongoElement Content = dialog.Content, SecondaryContent = dialog.SecondaryContent, RichContent = dialog.RichContent, - SecondaryRichContent = dialog.SecondaryRichContent + SecondaryRichContent = dialog.SecondaryRichContent, + Payload = dialog.Payload }; } @@ -35,7 +37,8 @@ public class DialogMongoElement Content = dialog.Content, SecondaryContent = dialog.SecondaryContent, RichContent = dialog.RichContent, - SecondaryRichContent = dialog.SecondaryRichContent + SecondaryRichContent = dialog.SecondaryRichContent, + Payload = dialog.Payload }; } }