Skip to content

Commit 1814304

Browse files
authored
Merge pull request #412 from iceljc/features/fix-file-serialization
fix file serialization
2 parents 1992a70 + 668df3e commit 1814304

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStorage.cs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ namespace BotSharp.Core.Conversations.Services;
88
public class ConversationStorage : IConversationStorage
99
{
1010
private readonly BotSharpDatabaseSettings _dbSettings;
11+
private readonly BotSharpOptions _options;
1112
private readonly IServiceProvider _services;
12-
private readonly JsonSerializerOptions _jsonOptions;
1313

1414
public ConversationStorage(
1515
BotSharpDatabaseSettings dbSettings,
@@ -18,7 +18,7 @@ public ConversationStorage(
1818
{
1919
_dbSettings = dbSettings;
2020
_services = services;
21-
_jsonOptions = InitJsonSerilizerOptions(options);
21+
_options = options;
2222
}
2323

2424
public void Append(string conversationId, RoleDialogModel dialog)
@@ -70,7 +70,7 @@ public void Append(string conversationId, RoleDialogModel dialog)
7070
return;
7171
}
7272

73-
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _jsonOptions) : null;
73+
var richContent = dialog.RichContent != null ? JsonSerializer.Serialize(dialog.RichContent, _options.JsonSerializerOptions) : null;
7474
dialogElements.Add(new DialogElement(meta, content, richContent));
7575
}
7676

@@ -95,7 +95,7 @@ public List<RoleDialogModel> GetDialogs(string conversationId)
9595
var senderId = role == AgentRole.Function ? currentAgentId : meta.SenderId;
9696
var createdAt = meta.CreateTime;
9797
var richContent = !string.IsNullOrEmpty(dialog.RichContent) ?
98-
JsonSerializer.Deserialize<RichContent<IRichMessage>>(dialog.RichContent, _jsonOptions) : null;
98+
JsonSerializer.Deserialize<RichContent<IRichMessage>>(dialog.RichContent, _options.JsonSerializerOptions) : null;
9999

100100
var record = new RoleDialogModel(role, content)
101101
{
@@ -140,21 +140,4 @@ private string GetStorageFile(string conversationId)
140140
}
141141
return Path.Combine(dir, "dialogs.txt");
142142
}
143-
144-
private JsonSerializerOptions InitJsonSerilizerOptions(BotSharpOptions botSharOptions)
145-
{
146-
var options = botSharOptions.JsonSerializerOptions;
147-
var jsonOptions = new JsonSerializerOptions
148-
{
149-
PropertyNameCaseInsensitive = options.PropertyNameCaseInsensitive,
150-
PropertyNamingPolicy = options.PropertyNamingPolicy ?? JsonNamingPolicy.CamelCase,
151-
AllowTrailingCommas = options.AllowTrailingCommas,
152-
};
153-
154-
foreach (var converter in options.Converters)
155-
{
156-
jsonOptions.Converters.Add(converter);
157-
}
158-
return jsonOptions;
159-
}
160143
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,7 +521,7 @@ private List<DialogElement> CollectDialogElements(string dialogDir)
521521
CreateTime = DateTime.Parse(blocks[0])
522522
};
523523

524-
var richContent = blocks.Count() > 6 ? blocks[6] : null;
524+
var richContent = blocks.Count() > 6 ? DecodeRichContent(blocks[6]) : null;
525525
dialogs.Add(new DialogElement(meta, trimmed, richContent));
526526
}
527527
}
@@ -537,7 +537,8 @@ private List<string> ParseDialogElements(List<DialogElement> dialogs)
537537
{
538538
var meta = element.MetaData;
539539
var createTime = meta.CreateTime.ToString("MM/dd/yyyy hh:mm:ss.ffffff tt", CultureInfo.InvariantCulture);
540-
var metaStr = $"{createTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{meta.SenderId}|{meta.FunctionName}|{element.RichContent}";
540+
var encodedRichContent = EncodeRichContent(element.RichContent);
541+
var metaStr = $"{createTime}|{meta.Role}|{meta.AgentId}|{meta.MessageId}|{meta.SenderId}|{meta.FunctionName}|{encodedRichContent}";
541542
dialogTexts.Add(metaStr);
542543
var content = $" - {element.Content}";
543544
dialogTexts.Add(content);
@@ -686,6 +687,24 @@ private bool SaveTruncatedBreakpoints(string breakpointDir, List<ConversationBre
686687
File.WriteAllText(breakpointDir, breakpointStr);
687688
return true;
688689
}
690+
691+
private string? EncodeRichContent(string? content)
692+
{
693+
if (string.IsNullOrEmpty(content)) return content;
694+
695+
var bytes = Encoding.UTF8.GetBytes(content);
696+
var encoded = Convert.ToBase64String(bytes);
697+
return encoded;
698+
}
699+
700+
private string? DecodeRichContent(string? content)
701+
{
702+
if (string.IsNullOrEmpty(content)) return content;
703+
704+
var decoded = Convert.FromBase64String(content);
705+
var origin = Encoding.UTF8.GetString(decoded);
706+
return origin;
707+
}
689708
#endregion
690709
}
691710
}

0 commit comments

Comments
 (0)