|
| 1 | +using System.ClientModel; |
| 2 | +using System.ClientModel.Primitives; |
| 3 | +using System.Diagnostics.CodeAnalysis; |
| 4 | +using System.Text; |
| 5 | +using Google.Protobuf; |
| 6 | +using Grpc.Core; |
| 7 | +using System.Buffers; |
| 8 | +using System.Buffers.Binary; |
| 9 | + |
| 10 | +namespace Devlooped.Extensions.AI.Grok; |
| 11 | + |
| 12 | +class ClientPipelineCallInvoker(ClientPipeline pipeline, Uri endpoint) : CallInvoker |
| 13 | +{ |
| 14 | + public override TResponse BlockingUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 15 | + { |
| 16 | + using var message = CreateMessage(method, options, request); |
| 17 | + pipeline.Send(message); |
| 18 | + |
| 19 | + var response = message.Response; |
| 20 | + EnsureSuccess(response); |
| 21 | + |
| 22 | + using var stream = response.ContentStream; |
| 23 | + if (stream == null) throw new RpcException(new Status(StatusCode.Internal, "Response content stream is null")); |
| 24 | + return ReadSingleResponse<TResponse>(stream); |
| 25 | + } |
| 26 | + |
| 27 | + public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 28 | + { |
| 29 | + var task = SendAsync(method, options, request); |
| 30 | + return new AsyncUnaryCall<TResponse>(task, Task.FromResult(new Metadata()), static () => Status.DefaultSuccess, static () => [], static () => { }); |
| 31 | + } |
| 32 | + |
| 33 | + async Task<TResponse> SendAsync<TRequest, TResponse>(Method<TRequest, TResponse> method, CallOptions options, TRequest request) |
| 34 | + where TRequest : class |
| 35 | + where TResponse : class |
| 36 | + { |
| 37 | + using var message = CreateMessage(method, options, request); |
| 38 | + await pipeline.SendAsync(message).ConfigureAwait(false); |
| 39 | + |
| 40 | + var response = message.Response; |
| 41 | + EnsureSuccess(response); |
| 42 | + |
| 43 | + using var stream = response.ContentStream; |
| 44 | + if (stream == null) throw new RpcException(new Status(StatusCode.Internal, "Response content stream is null")); |
| 45 | + return await ReadSingleResponseAsync<TResponse>(stream).ConfigureAwait(false); |
| 46 | + } |
| 47 | + |
| 48 | + public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options, TRequest request) |
| 49 | + { |
| 50 | + var responseStream = new ClientPipelineResponseStream<TResponse>(pipeline, CreateMessage(method, options, request)); |
| 51 | + // We need to start the request. |
| 52 | + |
| 53 | + return new AsyncServerStreamingCall<TResponse>( |
| 54 | + responseStream, |
| 55 | + Task.FromResult(new Metadata()), |
| 56 | + () => Status.DefaultSuccess, |
| 57 | + () => [], |
| 58 | + () => { } |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + PipelineMessage CreateMessage<TRequest, TResponse>(Method<TRequest, TResponse> method, CallOptions options, TRequest request) |
| 63 | + where TRequest : class |
| 64 | + where TResponse : class |
| 65 | + { |
| 66 | + var message = pipeline.CreateMessage(); |
| 67 | + message.ResponseClassifier = PipelineMessageClassifier.Create([200]); |
| 68 | + var req = message.Request; |
| 69 | + req.Method = "POST"; |
| 70 | + req.Uri = new Uri(endpoint, method.FullName); |
| 71 | + req.Headers.Add("Content-Type", "application/grpc"); |
| 72 | + req.Headers.Add("TE", "trailers"); |
| 73 | + |
| 74 | + if (request is IMessage msg) |
| 75 | + { |
| 76 | + var length = msg.CalculateSize(); |
| 77 | + var frame = new byte[length + 5]; |
| 78 | + frame[0] = 0; // No compression |
| 79 | + BinaryPrimitives.WriteUInt32BigEndian(frame.AsSpan(1), (uint)length); |
| 80 | + msg.WriteTo(frame.AsSpan(5)); |
| 81 | + req.Content = BinaryContent.Create(BinaryData.FromBytes(frame)); |
| 82 | + } |
| 83 | + |
| 84 | + if (options.CancellationToken != default) |
| 85 | + { |
| 86 | + message.Apply(new RequestOptions { CancellationToken = options.CancellationToken }); |
| 87 | + } |
| 88 | + |
| 89 | + return message; |
| 90 | + } |
| 91 | + |
| 92 | + static void EnsureSuccess([NotNull] PipelineResponse? response) |
| 93 | + { |
| 94 | + if (response == null || response.IsError) |
| 95 | + { |
| 96 | + throw new RpcException(new Status(StatusCode.Internal, response?.ReasonPhrase ?? "Unknown error")); |
| 97 | + } |
| 98 | + if (response.Headers.TryGetValue("grpc-status", out var statusStr) && int.TryParse(statusStr, out var status) && status != 0) |
| 99 | + { |
| 100 | + response.Headers.TryGetValue("grpc-message", out var grpcMessage); |
| 101 | + throw new RpcException(new Status((StatusCode)status, grpcMessage ?? "Unknown gRPC error")); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + static TResponse ReadSingleResponse<TResponse>(Stream stream) |
| 106 | + { |
| 107 | + Span<byte> header = stackalloc byte[5]; |
| 108 | + var read = 0; |
| 109 | + while (read < 5) |
| 110 | + { |
| 111 | + var r = stream.Read(header.Slice(read)); |
| 112 | + if (r == 0) throw new IOException("Unexpected end of stream reading gRPC header"); |
| 113 | + read += r; |
| 114 | + } |
| 115 | + |
| 116 | + var length = BinaryPrimitives.ReadUInt32BigEndian(header.Slice(1)); |
| 117 | + var data = ArrayPool<byte>.Shared.Rent((int)length); |
| 118 | + try |
| 119 | + { |
| 120 | + read = 0; |
| 121 | + while (read < length) |
| 122 | + { |
| 123 | + var r = stream.Read(data, read, (int)(length - read)); |
| 124 | + if (r == 0) throw new IOException("Unexpected end of stream reading gRPC payload"); |
| 125 | + read += r; |
| 126 | + } |
| 127 | + |
| 128 | + var instance = Activator.CreateInstance<TResponse>(); |
| 129 | + if (instance is IMessage message) |
| 130 | + { |
| 131 | + message.MergeFrom(data.AsSpan(0, (int)length)); |
| 132 | + return (TResponse)message; |
| 133 | + } |
| 134 | + throw new InvalidOperationException($"Type {typeof(TResponse)} is not a Protobuf Message."); |
| 135 | + } |
| 136 | + finally |
| 137 | + { |
| 138 | + ArrayPool<byte>.Shared.Return(data); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + static async Task<TResponse> ReadSingleResponseAsync<TResponse>(Stream stream) |
| 143 | + { |
| 144 | + var header = new byte[5]; |
| 145 | + var read = 0; |
| 146 | + while (read < 5) |
| 147 | + { |
| 148 | + var r = await stream.ReadAsync(header.AsMemory(read, 5 - read)).ConfigureAwait(false); |
| 149 | + if (r == 0) throw new IOException("Unexpected end of stream reading gRPC header"); |
| 150 | + read += r; |
| 151 | + } |
| 152 | + |
| 153 | + var length = BinaryPrimitives.ReadUInt32BigEndian(header.AsSpan(1)); |
| 154 | + var data = ArrayPool<byte>.Shared.Rent((int)length); |
| 155 | + try |
| 156 | + { |
| 157 | + read = 0; |
| 158 | + while (read < length) |
| 159 | + { |
| 160 | + var r = await stream.ReadAsync(data.AsMemory(read, (int)(length - read))).ConfigureAwait(false); |
| 161 | + if (r == 0) throw new IOException("Unexpected end of stream reading gRPC payload"); |
| 162 | + read += r; |
| 163 | + } |
| 164 | + |
| 165 | + var instance = Activator.CreateInstance<TResponse>(); |
| 166 | + if (instance is IMessage message) |
| 167 | + { |
| 168 | + message.MergeFrom(data.AsSpan(0, (int)length)); |
| 169 | + return (TResponse)message; |
| 170 | + } |
| 171 | + throw new InvalidOperationException($"Type {typeof(TResponse)} is not a Protobuf Message."); |
| 172 | + } |
| 173 | + finally |
| 174 | + { |
| 175 | + ArrayPool<byte>.Shared.Return(data); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options) |
| 180 | + { |
| 181 | + throw new NotSupportedException("Client streaming is not supported over this adapter."); |
| 182 | + } |
| 183 | + |
| 184 | + public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>(Method<TRequest, TResponse> method, string? host, CallOptions options) |
| 185 | + { |
| 186 | + throw new NotSupportedException("Duplex streaming is not supported over this adapter."); |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +class ClientPipelineResponseStream<TResponse>(ClientPipeline pipeline, PipelineMessage message) : IAsyncStreamReader<TResponse> where TResponse : class |
| 191 | +{ |
| 192 | + IAsyncEnumerator<TResponse>? enumerator; |
| 193 | + TResponse? current; |
| 194 | + |
| 195 | + public Task StartAsync() => Task.CompletedTask; |
| 196 | + |
| 197 | + public TResponse Current => current ?? throw new InvalidOperationException("No current element"); |
| 198 | + |
| 199 | + public async Task<bool> MoveNext(CancellationToken cancellationToken) |
| 200 | + { |
| 201 | + if (enumerator == null) |
| 202 | + { |
| 203 | + await pipeline.SendAsync(message).ConfigureAwait(false); |
| 204 | + var response = message.Response; |
| 205 | + |
| 206 | + if (response == null || response.IsError) |
| 207 | + { |
| 208 | + throw new RpcException(new Status(StatusCode.Internal, response?.ReasonPhrase ?? "Unknown error")); |
| 209 | + } |
| 210 | + if (response.Headers.TryGetValue("grpc-status", out var statusStr) && int.TryParse(statusStr, out var status) && status != 0) |
| 211 | + { |
| 212 | + response.Headers.TryGetValue("grpc-message", out var grpcMessage); |
| 213 | + throw new RpcException(new Status((StatusCode)status, grpcMessage ?? "Unknown gRPC error")); |
| 214 | + } |
| 215 | + |
| 216 | + enumerator = ReadStream(response.ContentStream!, cancellationToken).GetAsyncEnumerator(cancellationToken); |
| 217 | + } |
| 218 | + |
| 219 | + if (await enumerator.MoveNextAsync().ConfigureAwait(false)) |
| 220 | + { |
| 221 | + current = enumerator.Current; |
| 222 | + return true; |
| 223 | + } |
| 224 | + |
| 225 | + return false; |
| 226 | + } |
| 227 | + |
| 228 | + async IAsyncEnumerable<TResponse> ReadStream(Stream stream, [System.Runtime.CompilerServices.EnumeratorCancellation] CancellationToken cancellationToken) |
| 229 | + { |
| 230 | + var header = new byte[5]; |
| 231 | + while (true) |
| 232 | + { |
| 233 | + var read = 0; |
| 234 | + while (read < 5) |
| 235 | + { |
| 236 | + var r = await stream.ReadAsync(header, read, 5 - read, cancellationToken).ConfigureAwait(false); |
| 237 | + if (r == 0) |
| 238 | + { |
| 239 | + if (read == 0) yield break; // End of stream |
| 240 | + throw new IOException("Unexpected end of stream reading gRPC header"); |
| 241 | + } |
| 242 | + read += r; |
| 243 | + } |
| 244 | + |
| 245 | + var length = BinaryPrimitives.ReadUInt32BigEndian(header.AsSpan(1)); |
| 246 | + var data = ArrayPool<byte>.Shared.Rent((int)length); |
| 247 | + try |
| 248 | + { |
| 249 | + read = 0; |
| 250 | + while (read < length) |
| 251 | + { |
| 252 | + var r = await stream.ReadAsync(data, read, (int)(length - read), cancellationToken).ConfigureAwait(false); |
| 253 | + if (r == 0) throw new IOException("Unexpected end of stream reading gRPC payload"); |
| 254 | + read += r; |
| 255 | + } |
| 256 | + |
| 257 | + var instance = Activator.CreateInstance<TResponse>(); |
| 258 | + if (instance is IMessage message) |
| 259 | + { |
| 260 | + message.MergeFrom(data.AsSpan(0, (int)length)); |
| 261 | + yield return (TResponse)message; |
| 262 | + } |
| 263 | + } |
| 264 | + finally |
| 265 | + { |
| 266 | + ArrayPool<byte>.Shared.Return(data); |
| 267 | + } |
| 268 | + } |
| 269 | + } |
| 270 | +} |
0 commit comments