1
+ using BotSharp . Abstraction . Options ;
1
2
using BotSharp . Abstraction . Routing ;
2
3
3
4
namespace BotSharp . OpenAPI . Controllers ;
@@ -8,12 +9,16 @@ public class ConversationController : ControllerBase
8
9
{
9
10
private readonly IServiceProvider _services ;
10
11
private readonly IUserIdentity _user ;
12
+ private readonly JsonSerializerOptions _jsonOptions ;
11
13
12
14
public ConversationController ( IServiceProvider services ,
13
- IUserIdentity user )
15
+ IUserIdentity user ,
16
+ BotSharpOptions options )
14
17
{
15
18
_services = services ;
16
19
_user = user ;
20
+ _jsonOptions = InitJsonOptions ( options ) ;
21
+
17
22
}
18
23
19
24
[ HttpPost ( "/conversation/{agentId}" ) ]
@@ -312,10 +317,7 @@ await conv.SendMessage(agentId, inputMsg,
312
317
313
318
private async Task OnChunkReceived ( HttpResponse response , ChatResponseModel message )
314
319
{
315
- var json = JsonSerializer . Serialize ( message , new JsonSerializerOptions
316
- {
317
- PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
318
- } ) ;
320
+ var json = JsonSerializer . Serialize ( message , _jsonOptions ) ;
319
321
320
322
var buffer = Encoding . UTF8 . GetBytes ( $ "data:{ json } \n ") ;
321
323
await response . Body . WriteAsync ( buffer , 0 , buffer . Length ) ;
@@ -333,4 +335,24 @@ private async Task OnEventCompleted(HttpResponse response)
333
335
buffer = Encoding . UTF8 . GetBytes ( "\n " ) ;
334
336
await response . Body . WriteAsync ( buffer , 0 , buffer . Length ) ;
335
337
}
338
+
339
+ private JsonSerializerOptions InitJsonOptions ( BotSharpOptions options )
340
+ {
341
+ var jsonOption = new JsonSerializerOptions
342
+ {
343
+ PropertyNameCaseInsensitive = true ,
344
+ PropertyNamingPolicy = JsonNamingPolicy . CamelCase ,
345
+ AllowTrailingCommas = true
346
+ } ;
347
+
348
+ if ( options ? . JsonSerializerOptions != null )
349
+ {
350
+ foreach ( var option in options . JsonSerializerOptions . Converters )
351
+ {
352
+ jsonOption . Converters . Add ( option ) ;
353
+ }
354
+ }
355
+
356
+ return jsonOption ;
357
+ }
336
358
}
0 commit comments