openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/Internal/Pagination/AsyncRunCollectionResult.cs
85lines · modecode
| 1 | using System.ClientModel; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | using System.Threading; |
| 6 | using System.Threading.Tasks; |
| 7 | |
| 8 | #nullable enable |
| 9 | |
| 10 | namespace OpenAI.Assistants; |
| 11 | |
| 12 | internal class AsyncRunCollectionResult : AsyncCollectionResult<ThreadRun> |
| 13 | { |
| 14 | private readonly InternalAssistantRunClient _runClient; |
| 15 | private readonly RequestOptions? _options; |
| 16 | private readonly CancellationToken _cancellationToken; |
| 17 | |
| 18 | // Initial values |
| 19 | private readonly string _threadId; |
| 20 | private readonly int? _limit; |
| 21 | private readonly string? _order; |
| 22 | private readonly string? _after; |
| 23 | private readonly string? _before; |
| 24 | |
| 25 | public AsyncRunCollectionResult(InternalAssistantRunClient runClient, |
| 26 | RequestOptions? options, |
| 27 | string threadId, |
| 28 | int? limit, string? order, string? after, string? before) |
| 29 | { |
| 30 | _runClient = runClient; |
| 31 | _options = options; |
| 32 | _cancellationToken = _options?.CancellationToken ?? default; |
| 33 | |
| 34 | _threadId = threadId; |
| 35 | _limit = limit; |
| 36 | _order = order; |
| 37 | _after = after; |
| 38 | _before = before; |
| 39 | } |
| 40 | public async override IAsyncEnumerable<ClientResult> GetRawPagesAsync() |
| 41 | { |
| 42 | ClientResult page = await GetFirstPageAsync().ConfigureAwait(false); |
| 43 | yield return page; |
| 44 | |
| 45 | while (HasNextPage(page)) |
| 46 | { |
| 47 | page = await GetNextPageAsync(page); |
| 48 | yield return page; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | protected override IAsyncEnumerable<ThreadRun> GetValuesFromPageAsync(ClientResult page) |
| 53 | { |
| 54 | Argument.AssertNotNull(page, nameof(page)); |
| 55 | |
| 56 | PipelineResponse response = page.GetRawResponse(); |
| 57 | InternalListRunsResponse list = ModelReaderWriter.Read<InternalListRunsResponse>(response.Content)!; |
| 58 | return list.Data.ToAsyncEnumerable(_cancellationToken); |
| 59 | } |
| 60 | |
| 61 | public override ContinuationToken? GetContinuationToken(ClientResult page) |
| 62 | { |
| 63 | Argument.AssertNotNull(page, nameof(page)); |
| 64 | |
| 65 | return RunCollectionPageToken.FromResponse(page, _threadId, _limit, _order, _before); |
| 66 | } |
| 67 | |
| 68 | public async Task<ClientResult> GetFirstPageAsync() |
| 69 | => await _runClient.GetRunsAsync(_threadId, _limit, _order, _after, _before, _options).ConfigureAwait(false); |
| 70 | |
| 71 | public async Task<ClientResult> GetNextPageAsync(ClientResult result) |
| 72 | { |
| 73 | Argument.AssertNotNull(result, nameof(result)); |
| 74 | |
| 75 | PipelineResponse response = result.GetRawResponse(); |
| 76 | |
| 77 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 78 | string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; |
| 79 | |
| 80 | return await _runClient.GetRunsAsync(_threadId, _limit, _order, lastId, _before, _options).ConfigureAwait(false); |
| 81 | } |
| 82 | |
| 83 | public static bool HasNextPage(ClientResult result) |
| 84 | => RunCollectionResult.HasNextPage(result); |
| 85 | } |
| 86 | |