Merge pull request #444 from iceljc/bugfix/fix-log-json

fix json log
This commit is contained in:
C. Oceania 2024-05-08 15:48:39 -05:00 committed by GitHub
commit ceca3d8963
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -16,6 +16,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
{
private readonly ConversationSetting _convSettings;
private readonly BotSharpOptions _options;
private readonly JsonSerializerOptions _localJsonOptions;
private readonly IServiceProvider _services;
private readonly IHubContext<SignalRHub> _chatHub;
private readonly IConversationStateService _state;
@ -41,6 +42,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
_user = user;
_agentService = agentService;
_routingCtx = routingCtx;
_localJsonOptions = InitLocalJsonOptions(options);
}
#region IConversationHook
@ -188,15 +190,7 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
var log = $"{GetMessageContent(message)}";
if (message.RichContent != null || message.SecondaryRichContent != null)
{
var jsonOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
var richContent = JsonSerializer.Serialize(message.SecondaryRichContent ?? message.RichContent, jsonOptions);
var richContent = JsonSerializer.Serialize(message.SecondaryRichContent ?? message.RichContent, _localJsonOptions);
log += $"\r\n```json\r\n{richContent}\r\n```";
}
@ -494,4 +488,26 @@ public class StreamingLogHook : ConversationHookBase, IContentGeneratingHook, IR
{
return !string.IsNullOrEmpty(message.SecondaryContent) ? message.SecondaryContent : message.Content;
}
private JsonSerializerOptions InitLocalJsonOptions(BotSharpOptions options)
{
var localOptions = new JsonSerializerOptions
{
PropertyNameCaseInsensitive = true,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
AllowTrailingCommas = true,
WriteIndented = true,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
};
if (options?.JsonSerializerOptions != null && !options.JsonSerializerOptions.Converters.IsNullOrEmpty())
{
foreach (var converter in options.JsonSerializerOptions.Converters)
{
localOptions.Converters.Add(converter);
}
}
return localOptions;
}
}