@@ -131,6 +131,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs(
131131 Data = message . Data ,
132132 Sender = UserDto . FromUser ( user ) ,
133133 Payload = message . Payload ,
134+ MetaData = message . MetaData ,
134135 HasMessageFiles = files . Any ( x => x . MessageId . IsEqualTo ( message . MessageId ) && x . FileSource == FileSource . User )
135136 } ) ;
136137 }
@@ -146,6 +147,7 @@ public async Task<IEnumerable<ChatResponseModel>> GetDialogs(
146147 Text = ! string . IsNullOrEmpty ( message . SecondaryContent ) ? message . SecondaryContent : message . Content ,
147148 Function = message . FunctionName ,
148149 Data = message . Data ,
150+ MetaData = message . MetaData ,
149151 Sender = new ( )
150152 {
151153 FirstName = agent ? . Name ?? "Unkown" ,
@@ -397,18 +399,36 @@ public async Task<ChatResponseModel> SendMessage(
397399 await conv . SetConversationId ( conversationId , input . States ) ;
398400 SetStates ( conv , input ) ;
399401
402+ IConversationCancellationService ? convCancellation = null ;
403+ if ( input . IsStreamingMessage )
404+ {
405+ convCancellation = _services . GetRequiredService < IConversationCancellationService > ( ) ;
406+ convCancellation . RegisterConversation ( conversationId ) ;
407+ }
408+
400409 var response = new ChatResponseModel ( ) ;
401- await conv . SendMessage ( agentId , inputMsg ,
402- replyMessage : input . Postback ,
403- async msg =>
404- {
405- response . Text = ! string . IsNullOrEmpty ( msg . SecondaryContent ) ? msg . SecondaryContent : msg . Content ;
406- response . Function = msg . FunctionName ;
407- response . MessageLabel = msg . MessageLabel ;
408- response . RichContent = msg . SecondaryRichContent ?? msg . RichContent ;
409- response . Instruction = msg . Instruction ;
410- response . Data = msg . Data ;
411- } ) ;
410+ try
411+ {
412+ await conv . SendMessage ( agentId , inputMsg ,
413+ replyMessage : input . Postback ,
414+ async msg =>
415+ {
416+ response . Text = ! string . IsNullOrEmpty ( msg . SecondaryContent ) ? msg . SecondaryContent : msg . Content ;
417+ response . Function = msg . FunctionName ;
418+ response . MessageLabel = msg . MessageLabel ;
419+ response . RichContent = msg . SecondaryRichContent ?? msg . RichContent ;
420+ response . Instruction = msg . Instruction ;
421+ response . Data = msg . Data ;
422+ } ) ;
423+ }
424+ catch ( OperationCanceledException ) when ( input . IsStreamingMessage )
425+ {
426+ response . Text = string . Empty ;
427+ }
428+ finally
429+ {
430+ convCancellation ? . UnregisterConversation ( conversationId ) ;
431+ }
412432
413433 var state = _services . GetRequiredService < IConversationStateService > ( ) ;
414434 response . States = state . GetStates ( ) ;
@@ -455,20 +475,20 @@ public async Task SendMessageSse([FromRoute] string agentId, [FromRoute] string
455475 Response . Headers . Append ( Microsoft . Net . Http . Headers . HeaderNames . Connection , "keep-alive" ) ;
456476
457477 await conv . SendMessage ( agentId , inputMsg ,
458- replyMessage : input . Postback ,
459- // responsed generated
460- async msg =>
461- {
462- response . Text = ! string . IsNullOrEmpty ( msg . SecondaryContent ) ? msg . SecondaryContent : msg . Content ;
463- response . MessageLabel = msg . MessageLabel ;
464- response . Function = msg . FunctionName ;
465- response . RichContent = msg . SecondaryRichContent ?? msg . RichContent ;
466- response . Instruction = msg . Instruction ;
467- response . Data = msg . Data ;
468- response . States = state . GetStates ( ) ;
469-
470- await OnChunkReceived ( Response , response ) ;
471- } ) ;
478+ replyMessage : input . Postback ,
479+ // responsed generated
480+ async msg =>
481+ {
482+ response . Text = ! string . IsNullOrEmpty ( msg . SecondaryContent ) ? msg . SecondaryContent : msg . Content ;
483+ response . MessageLabel = msg . MessageLabel ;
484+ response . Function = msg . FunctionName ;
485+ response . RichContent = msg . SecondaryRichContent ?? msg . RichContent ;
486+ response . Instruction = msg . Instruction ;
487+ response . Data = msg . Data ;
488+ response . States = state . GetStates ( ) ;
489+
490+ await OnChunkReceived ( Response , response ) ;
491+ } ) ;
472492
473493 response . States = state . GetStates ( ) ;
474494 response . MessageId = inputMsg . MessageId ;
@@ -477,18 +497,13 @@ await conv.SendMessage(agentId, inputMsg,
477497 // await OnEventCompleted(Response);
478498 }
479499
480- private async Task OnReceiveToolCallIndication ( string conversationId , RoleDialogModel msg )
500+ [ HttpPost ( "/conversation/{conversationId}/stop-streaming" ) ]
501+ public ConverstionCancellationResponse StopStreaming ( [ FromRoute ] string conversationId )
481502 {
482- var indicator = new ChatResponseModel
483- {
484- ConversationId = conversationId ,
485- MessageId = msg . MessageId ,
486- Text = msg . Indication ,
487- Function = "indicating" ,
488- Instruction = msg . Instruction ,
489- States = new Dictionary < string , string > ( )
490- } ;
491- await OnChunkReceived ( Response , indicator ) ;
503+ var streamingCancellation = _services . GetRequiredService < IConversationCancellationService > ( ) ;
504+ var cancelled = streamingCancellation . CancelStreaming ( conversationId ) ;
505+
506+ return new ConverstionCancellationResponse { Success = cancelled } ;
492507 }
493508 #endregion
494509
@@ -515,6 +530,8 @@ private void SetStates(IConversationService conv, NewMessageModel input)
515530 {
516531 conv . States . SetState ( "sampling_factor" , input . SamplingFactor , source : StateSource . External ) ;
517532 }
533+
534+ conv . States . SetState ( "use_stream_message" , input . IsStreamingMessage , source : StateSource . Application ) ;
518535 }
519536
520537 private FileContentResult BuildFileResult ( string file )
@@ -567,5 +584,19 @@ private JsonSerializerOptions InitJsonOptions(BotSharpOptions options)
567584
568585 return jsonOption ;
569586 }
587+
588+ private async Task OnReceiveToolCallIndication ( string conversationId , RoleDialogModel msg )
589+ {
590+ var indicator = new ChatResponseModel
591+ {
592+ ConversationId = conversationId ,
593+ MessageId = msg . MessageId ,
594+ Text = msg . Indication ,
595+ Function = "indicating" ,
596+ Instruction = msg . Instruction ,
597+ States = [ ]
598+ } ;
599+ await OnChunkReceived ( Response , indicator ) ;
600+ }
570601 #endregion
571602}
0 commit comments