Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 25 additions & 9 deletions src/Plugins/BotSharp.Plugin.ChatHub/Hooks/StreamingLogHook.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,7 @@ public StreamingLogHook(
_user = user;
_agentService = agentService;
_routingCtx = routingCtx;
_localJsonOptions = InitLocalJsonOptions(options);
}

#region IConversationHook
Expand Down Expand Up @@ -188,15 +190,7 @@ public override async Task OnResponseGenerated(RoleDialogModel message)
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```";
}

Expand Down Expand Up @@ -494,4 +488,26 @@ private string GetMessageContent(RoleDialogModel message)
{
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;
}
}