-
-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathAiWebsocketPipelineResponse.cs
114 lines (96 loc) · 3.53 KB
/
AiWebsocketPipelineResponse.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System.ClientModel.Primitives;
using System.Net;
namespace BotSharp.Core.Realtime.Websocket.Chat;
public class AiWebsocketPipelineResponse : PipelineResponse
{
public AiWebsocketPipelineResponse()
{
}
private int _status;
public override int Status => _status;
private string _reasonPhrase;
public override string ReasonPhrase => _reasonPhrase;
private MemoryStream _contentStream = new();
public override Stream? ContentStream
{
get
{
return _contentStream != null ? _contentStream : new MemoryStream();
}
set => throw new NotImplementedException();
}
private BinaryData _content;
public override BinaryData Content
{
get
{
if (_content == null)
{
_content = new(_contentStream.ToArray());
}
return _content;
}
}
protected override PipelineResponseHeaders HeadersCore => throw new NotImplementedException();
public bool IsComplete { get; private set; } = false;
public void CollectReceivedResult(WebSocketReceiveResult receivedResult, BinaryData receivedBytes)
{
if (ContentStream.Length == 0)
{
_status = ConvertWebsocketCloseStatusToHttpStatus(receivedResult.CloseStatus ?? WebSocketCloseStatus.Empty);
_reasonPhrase = receivedResult.CloseStatusDescription?? (receivedResult.CloseStatus ?? WebSocketCloseStatus.Empty).ToString();
}
else if (receivedResult.MessageType != WebSocketMessageType.Text)
{
throw new NotImplementedException($"{nameof(AiWebsocketPipelineResponse)} currently supports only text messages.");
}
var rawBytes = receivedBytes.ToArray();
_contentStream.Position = _contentStream.Length;
_contentStream.Write(rawBytes, 0, rawBytes.Length);
_contentStream.Position = 0;
IsComplete = receivedResult.EndOfMessage;
}
public override BinaryData BufferContent(CancellationToken cancellationToken = default)
{
return Content;
}
public override ValueTask<BinaryData> BufferContentAsync(CancellationToken cancellationToken = default)
{
return new ValueTask<BinaryData>(Task.FromResult(Content));
}
public override void Dispose()
{
ContentStream?.Dispose();
}
private static int ConvertWebsocketCloseStatusToHttpStatus(WebSocketCloseStatus status)
{
int res;
switch (status)
{
case WebSocketCloseStatus.Empty:
case WebSocketCloseStatus.NormalClosure:
res = (int)HttpStatusCode.OK;
break;
case WebSocketCloseStatus.EndpointUnavailable:
case WebSocketCloseStatus.ProtocolError:
case WebSocketCloseStatus.InvalidMessageType:
case WebSocketCloseStatus.InvalidPayloadData:
case WebSocketCloseStatus.PolicyViolation:
res = (int)HttpStatusCode.BadRequest;
break;
case WebSocketCloseStatus.MessageTooBig:
res = (int)HttpStatusCode.RequestEntityTooLarge;
break;
case WebSocketCloseStatus.MandatoryExtension:
res = 418;
break;
case WebSocketCloseStatus.InternalServerError:
res = (int)HttpStatusCode.InternalServerError;
break;
default:
res = (int)HttpStatusCode.InternalServerError;
break;
}
return res;
}
}