diff --git a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs index 0eccb301..d98032fe 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Conversations/Models/Conversation.cs @@ -25,7 +25,7 @@ public class Conversation public class DialogElement { - public string MetaData { get; set; } + public DialogMeta MetaData { get; set; } public string Content { get; set; } public DialogElement() @@ -33,9 +33,19 @@ public class DialogElement } - public DialogElement(string meta, string content) + public DialogElement(DialogMeta meta, string content) { MetaData = meta; Content = content; } } + +public class DialogMeta +{ + public string Role { get; set; } + public string AgentId { get; set; } + public string MessageId { get; set; } + public string? FunctionName { get; set; } + public string? SenderId { get; set; } + public DateTime CreateTime { get; set; } +} diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs index aa7372da..1fffa03b 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs @@ -24,8 +24,15 @@ public class ConversationStorage : IConversationStorage if (dialog.Role == AgentRole.Function) { - // var args = dialog.FunctionArgs.RemoveNewLine(); - var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.FunctionName}"; + var meta = new DialogMeta + { + Role = dialog.Role, + AgentId = agentId, + MessageId = dialog.MessageId, + FunctionName = dialog.FunctionName, + CreateTime = dialog.CreatedAt + }; + var content = dialog.Content.RemoveNewLine(); if (string.IsNullOrEmpty(content)) { @@ -35,7 +42,15 @@ public class ConversationStorage : IConversationStorage } else { - var meta = $"{dialog.CreatedAt}|{dialog.Role}|{agentId}|{dialog.MessageId}|{dialog.SenderId}"; + var meta = new DialogMeta + { + Role = dialog.Role, + AgentId = agentId, + MessageId = dialog.MessageId, + SenderId = dialog.SenderId, + CreateTime = dialog.CreatedAt + }; + var content = dialog.Content.RemoveNewLine(); if (string.IsNullOrEmpty(content)) { @@ -58,13 +73,12 @@ public class ConversationStorage : IConversationStorage { var meta = dialog.MetaData; var content = dialog.Content; - var blocks = meta.Split('|'); - var createdAt = DateTime.Parse(blocks[0]); - var role = blocks[1]; - var currentAgentId = blocks[2]; - var messageId = blocks[3]; - var senderId = role == AgentRole.Function ? currentAgentId : blocks[4]; - var function = role == AgentRole.Function ? blocks[4] : null; + var role = meta.Role; + var currentAgentId = meta.AgentId; + var messageId = meta.MessageId; + var function = role == AgentRole.Function ? meta.FunctionName : null; + var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId; + var createdAt = meta.CreateTime; results.Add(new RoleDialogModel(role, content) { diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index dbba2f75..eb323726 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -1037,9 +1037,18 @@ public class FileRepository : IBotSharpRepository { for (int i = 0; i < rawDialogs.Count(); i += 2) { - var meta = rawDialogs[i]; + var blocks = rawDialogs[i].Split("|"); var content = rawDialogs[i + 1]; var trimmed = content.Substring(4); + var meta = new DialogMeta + { + Role = blocks[1], + AgentId = blocks[2], + MessageId = blocks[3], + FunctionName = blocks[1] == AgentRole.Function ? blocks[4] : null, + SenderId = blocks[1] == AgentRole.Function ? null : blocks[4], + CreateTime = DateTime.Parse(blocks[0]) + }; dialogs.Add(new DialogElement(meta, trimmed)); } } @@ -1053,7 +1062,10 @@ public class FileRepository : IBotSharpRepository foreach (var element in dialogs) { - dialogTexts.Add(element.MetaData); + var meta = element.MetaData; + var source = meta.FunctionName ?? meta.SenderId; + var metaStr = $"{meta.CreateTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{source}"; + dialogTexts.Add(metaStr); var content = $" - {element.Content}"; dialogTexts.Add(content); } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs index 86105728..1940d5d3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Models/DialogMongoElement.cs @@ -4,7 +4,7 @@ namespace BotSharp.Plugin.MongoStorage.Models; public class DialogMongoElement { - public string MetaData { get; set; } + public DialogMetaMongoElement MetaData { get; set; } public string Content { get; set; } public DialogMongoElement() @@ -16,7 +16,7 @@ public class DialogMongoElement { return new DialogMongoElement { - MetaData = dialog.MetaData, + MetaData = DialogMetaMongoElement.ToMongoElement(dialog.MetaData), Content = dialog.Content }; } @@ -25,8 +25,49 @@ public class DialogMongoElement { return new DialogElement { - MetaData = dialog.MetaData, + MetaData = DialogMetaMongoElement.ToDomainElement(dialog.MetaData), Content = dialog.Content }; } } + +public class DialogMetaMongoElement +{ + public string Role { get; set; } + public string AgentId { get; set; } + public string MessageId { get; set; } + public string? FunctionName { get; set; } + public string? SenderId { get; set; } + public DateTime CreateTime { get; set; } + + public DialogMetaMongoElement() + { + + } + + public static DialogMeta ToDomainElement(DialogMetaMongoElement meta) + { + return new DialogMeta + { + Role = meta.Role, + AgentId = meta.AgentId, + MessageId = meta.MessageId, + FunctionName = meta.FunctionName, + SenderId = meta.SenderId, + CreateTime = meta.CreateTime, + }; + } + + public static DialogMetaMongoElement ToMongoElement(DialogMeta meta) + { + return new DialogMetaMongoElement + { + Role = meta.Role, + AgentId = meta.AgentId, + MessageId = meta.MessageId, + FunctionName = meta.FunctionName, + SenderId = meta.SenderId, + CreateTime = meta.CreateTime, + }; + } +}