openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

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

95lines · 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 AsyncAssistantCollectionResult : AsyncCollectionResult<Assistant>
13{
14 private readonly AssistantClient _assistantClient;
15 private readonly ClientPipeline _pipeline;
16 private readonly RequestOptions? _options;
17 private readonly CancellationToken _cancellationToken;
18
19 // Initial values
20 private readonly int? _limit;
21 private readonly string? _order;
22 private readonly string? _after;
23 private readonly string? _before;
24
25 public AsyncAssistantCollectionResult(AssistantClient assistantClient,
26 ClientPipeline pipeline, RequestOptions options,
27 int? limit, string? order, string? after, string? before)
28 {
29 _assistantClient = assistantClient;
30 _pipeline = pipeline;
31 _options = options;
32 _cancellationToken = _options?.CancellationToken ?? default;
33
34 _limit = limit;
35 _order = order;
36 _after = after;
37 _before = before;
38 }
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<Assistant> GetValuesFromPageAsync(ClientResult page)
53 {
54 Argument.AssertNotNull(page, nameof(page));
55
56 PipelineResponse response = page.GetRawResponse();
57 InternalListAssistantsResponse list = ModelReaderWriter.Read<InternalListAssistantsResponse>(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 AssistantCollectionPageToken.FromResponse(page, _limit, _order, _before);
66 }
67
68 public async Task<ClientResult> GetFirstPageAsync()
69 => await GetAssistantsAsync(_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 GetAssistantsAsync(_limit, _order, lastId, _before, _options).ConfigureAwait(false);
81 }
82
83 public static bool HasNextPage(ClientResult result)
84 {
85 Argument.AssertNotNull(result, nameof(result));
86
87 return AssistantCollectionResult.HasNextPage(result);
88 }
89
90 internal virtual async Task<ClientResult> GetAssistantsAsync(int? limit, string? order, string? after, string? before, RequestOptions? options)
91 {
92 using PipelineMessage message = _assistantClient.CreateListAssistantsRequest(limit, order, after, before, options);
93 return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false));
94 }
95}