11using System . Collections . Concurrent ;
22using System . Net . WebSockets ;
3+ using System . Runtime . ExceptionServices ;
34using System . Text . Json ;
45
56namespace Microsoft . Azure . Agent ;
@@ -26,7 +27,6 @@ private AzureCopilotReceiver(ClientWebSocket webSocket)
2627 }
2728
2829 internal int Watermark { get ; private set ; }
29- internal BlockingCollection < CopilotActivity > ActivityQueue => _activityQueue ;
3030
3131 internal static async Task < AzureCopilotReceiver > CreateAsync ( string streamUrl )
3232 {
@@ -52,6 +52,7 @@ private async Task ProcessActivities()
5252 if ( result . MessageType is WebSocketMessageType . Close )
5353 {
5454 closingMessage = "Close message received" ;
55+ _activityQueue . Add ( new CopilotActivity { Error = new ConnectionDroppedException ( "The server websocket is closing. Connection dropped." ) } ) ;
5556 }
5657 }
5758 catch ( OperationCanceledException )
@@ -65,6 +66,7 @@ private async Task ProcessActivities()
6566 {
6667 // TODO: log the closing request.
6768 await _webSocket . CloseAsync ( WebSocketCloseStatus . NormalClosure , closingMessage , CancellationToken . None ) ;
69+ _activityQueue . CompleteAdding ( ) ;
6870 break ;
6971 }
7072
@@ -98,8 +100,20 @@ private async Task ProcessActivities()
98100 }
99101 }
100102
101- // TODO: log the current state of the web socket
102- // TODO: handle error state, such as 'aborted'
103+ // TODO: log the current state of the web socket.
104+ _activityQueue . Add ( new CopilotActivity { Error = new ConnectionDroppedException ( $ "The websocket got in '{ _webSocket . State } ' state. Connection dropped.") } ) ;
105+ _activityQueue . CompleteAdding ( ) ;
106+ }
107+
108+ internal CopilotActivity Take ( CancellationToken cancellationToken )
109+ {
110+ CopilotActivity activity = _activityQueue . Take ( cancellationToken ) ;
111+ if ( activity . Error is not null )
112+ {
113+ ExceptionDispatchInfo . Capture ( activity . Error ) . Throw ( ) ;
114+ }
115+
116+ return activity ;
103117 }
104118
105119 public void Dispose ( )
0 commit comments