From d9fd6525dc0eb61185615911a46dc0d62e5f97ff Mon Sep 17 00:00:00 2001 From: Haiping Chen Date: Wed, 22 May 2024 11:02:14 -0500 Subject: [PATCH 1/4] Fix SSE response format. --- .../Controllers/ConversationController.cs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 80374a6d..fd66b225 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -251,6 +251,8 @@ public class ConversationController : ControllerBase await conv.TruncateConversation(conversationId, input.TruncateMessageId, inputMsg.MessageId); } + var state = _services.GetRequiredService(); + var routing = _services.GetRequiredService(); routing.Context.SetMessageId(conversationId, inputMsg.MessageId); @@ -278,6 +280,7 @@ public class ConversationController : ControllerBase response.RichContent = msg.SecondaryRichContent ?? msg.RichContent; response.Instruction = msg.Instruction; response.Data = msg.Data; + response.States = state.GetStates(); await OnChunkReceived(Response, response); }, @@ -290,6 +293,7 @@ public class ConversationController : ControllerBase MessageId = msg.MessageId, Text = msg.Indication, Function = "indicating", + States = new Dictionary() }; await OnChunkReceived(Response, indicator); }, @@ -299,7 +303,6 @@ public class ConversationController : ControllerBase }); - var state = _services.GetRequiredService(); response.States = state.GetStates(); response.MessageId = inputMsg.MessageId; response.ConversationId = conversationId; @@ -309,7 +312,10 @@ public class ConversationController : ControllerBase private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message) { - var json = JsonSerializer.Serialize(message); + var json = JsonSerializer.Serialize(message, new JsonSerializerOptions + { + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + }); var buffer = Encoding.UTF8.GetBytes($"data:{json}\n"); await response.Body.WriteAsync(buffer, 0, buffer.Length); From abf519481022925af6ad499ff394c366dd30d6d2 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 20:05:46 -0500 Subject: [PATCH 2/4] fix api controller json serilizer --- .../Controllers/ConversationController.cs | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index fd66b225..6ed27b32 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -1,3 +1,4 @@ +using BotSharp.Abstraction.Options; using BotSharp.Abstraction.Routing; namespace BotSharp.OpenAPI.Controllers; @@ -8,12 +9,16 @@ public class ConversationController : ControllerBase { private readonly IServiceProvider _services; private readonly IUserIdentity _user; + private readonly JsonSerializerOptions _jsonOptions; public ConversationController(IServiceProvider services, - IUserIdentity user) + IUserIdentity user, + BotSharpOptions options) { _services = services; _user = user; + _jsonOptions = InitJsonOptions(options); + } [HttpPost("/conversation/{agentId}")] @@ -312,10 +317,7 @@ public class ConversationController : ControllerBase private async Task OnChunkReceived(HttpResponse response, ChatResponseModel message) { - var json = JsonSerializer.Serialize(message, new JsonSerializerOptions - { - PropertyNamingPolicy = JsonNamingPolicy.CamelCase, - }); + var json = JsonSerializer.Serialize(message, _jsonOptions); var buffer = Encoding.UTF8.GetBytes($"data:{json}\n"); await response.Body.WriteAsync(buffer, 0, buffer.Length); @@ -333,4 +335,24 @@ public class ConversationController : ControllerBase buffer = Encoding.UTF8.GetBytes("\n"); await response.Body.WriteAsync(buffer, 0, buffer.Length); } + + private JsonSerializerOptions InitJsonOptions(BotSharpOptions options) + { + var jsonOption = new JsonSerializerOptions + { + PropertyNameCaseInsensitive = true, + PropertyNamingPolicy = JsonNamingPolicy.CamelCase, + AllowTrailingCommas = true + }; + + if (options?.JsonSerializerOptions != null) + { + foreach (var option in options.JsonSerializerOptions.Converters) + { + jsonOption.Converters.Add(option); + } + } + + return jsonOption; + } } From f355aa3f319c91741977f85b7e135786b6e7d1de Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 20:08:53 -0500 Subject: [PATCH 3/4] move google api setting --- .../BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs index eabdba84..a1a6dad2 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/BotSharpOpenApiExtensions.cs @@ -11,8 +11,6 @@ using Microsoft.Net.Http.Headers; using Microsoft.OpenApi.Models; using Microsoft.IdentityModel.JsonWebTokens; using BotSharp.OpenAPI.BackgroundServices; -using BotSharp.Abstraction.Settings; -using BotSharp.Abstraction.Google.Settings; namespace BotSharp.OpenAPI; @@ -34,12 +32,6 @@ public static class BotSharpOpenApiExtensions services.AddScoped(); services.AddHostedService(); - services.AddScoped(provider => - { - var settingService = provider.GetRequiredService(); - return settingService.Bind("GoogleApi"); - }); - // Add bearer authentication var schema = "MIXED_SCHEME"; var builder = services.AddAuthentication(options => From c49fcc5dcf2b477d1506a6f1f5dc083b911e1a31 Mon Sep 17 00:00:00 2001 From: Jicheng Lu Date: Sun, 26 May 2024 20:09:18 -0500 Subject: [PATCH 4/4] minor change --- .../BotSharp.Core/Conversations/ConversationPlugin.cs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs index 6887622b..90eb3298 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/ConversationPlugin.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Files; +using BotSharp.Abstraction.Google.Settings; using BotSharp.Abstraction.Instructs; using BotSharp.Abstraction.Messaging; using BotSharp.Abstraction.Plugins.Models; @@ -34,6 +35,12 @@ public class ConversationPlugin : IBotSharpPlugin return settingService.Bind("Conversation"); }); + services.AddScoped(provider => + { + var settingService = provider.GetRequiredService(); + return settingService.Bind("GoogleApi"); + }); + services.AddScoped(); services.AddScoped(); services.AddScoped();