openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Common/CancellationTokenExtensions.cs
22lines · modecode
| 1 | using System.ClientModel.Primitives; |
| 2 | using System.Threading; |
| 3 | |
| 4 | internal static class CancellationTokenExtensions |
| 5 | { |
| 6 | public static RequestOptions ToRequestOptions(this CancellationToken cancellationToken, bool streaming = false) |
| 7 | { |
| 8 | if (cancellationToken == default) |
| 9 | { |
| 10 | if (!streaming) return null; |
| 11 | return StreamRequestOptions; |
| 12 | } |
| 13 | |
| 14 | return new RequestOptions() { |
| 15 | CancellationToken = cancellationToken, |
| 16 | BufferResponse = !streaming, |
| 17 | }; |
| 18 | } |
| 19 | |
| 20 | private static RequestOptions StreamRequestOptions => _streamRequestOptions ??= new() { BufferResponse = false }; |
| 21 | private static RequestOptions _streamRequestOptions; |
| 22 | } |
| 23 | |