openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/Custom/Assistants/Internal/Pagination/AsyncRunStepCollectionResult.cs

88lines · modecode

1using System.ClientModel;
2using System.ClientModel.Primitives;
3using System.Collections.Generic;
4using System.Text.Json;
5using System.Threading;
6using System.Threading.Tasks;
7
8#nullable enable
9
10namespace OpenAI.Assistants;
11
12internal class AsyncRunStepCollectionResult : AsyncCollectionResult<RunStep>
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 string _runId;
21 private readonly int? _limit;
22 private readonly string? _order;
23 private readonly string? _after;
24 private readonly string? _before;
25
26 public AsyncRunStepCollectionResult(InternalAssistantRunClient runClient,
27 RequestOptions? options,
28 string threadId, string runId,
29 int? limit, string? order, string? after, string? before)
30 {
31 _runClient = runClient;
32 _options = options;
33 _cancellationToken = _options?.CancellationToken ?? default;
34
35 _threadId = threadId;
36 _runId = runId;
37 _limit = limit;
38 _order = order;
39 _after = after;
40 _before = before;
41 }
42
43 public async override IAsyncEnumerable<ClientResult> GetRawPagesAsync()
44 {
45 ClientResult page = await GetFirstPageAsync().ConfigureAwait(false);
46 yield return page;
47
48 while (HasNextPage(page))
49 {
50 page = await GetNextPageAsync(page);
51 yield return page;
52 }
53 }
54
55 protected override IAsyncEnumerable<RunStep> GetValuesFromPageAsync(ClientResult page)
56 {
57 Argument.AssertNotNull(page, nameof(page));
58
59 PipelineResponse response = page.GetRawResponse();
60 InternalListRunStepsResponse list = ModelReaderWriter.Read<InternalListRunStepsResponse>(response.Content)!;
61 return list.Data.ToAsyncEnumerable(_cancellationToken);
62 }
63
64 public override ContinuationToken? GetContinuationToken(ClientResult page)
65 {
66 Argument.AssertNotNull(page, nameof(page));
67
68 return RunStepCollectionPageToken.FromResponse(page, _threadId, _runId, _limit, _order, _before);
69 }
70
71 public async Task<ClientResult> GetFirstPageAsync()
72 => await _runClient.GetRunStepsAsync(_threadId, _runId, _limit, _order, _after, _before, _options).ConfigureAwait(false);
73
74 public async Task<ClientResult> GetNextPageAsync(ClientResult result)
75 {
76 Argument.AssertNotNull(result, nameof(result));
77
78 PipelineResponse response = result.GetRawResponse();
79
80 using JsonDocument doc = JsonDocument.Parse(response.Content);
81 string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!;
82
83 return await _runClient.GetRunStepsAsync(_threadId, _runId, _limit, _order, lastId, _before, _options).ConfigureAwait(false);
84 }
85
86 public static bool HasNextPage(ClientResult result)
87 => RunStepCollectionResult.HasNextPage(result);
88}
89