openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Assistants/Internal/Pagination/MessageCollectionResult.cs
91lines · modecode
| 1 | using System.ClientModel; |
| 2 | using System.ClientModel.Primitives; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Text.Json; |
| 5 | |
| 6 | #nullable enable |
| 7 | |
| 8 | namespace OpenAI.Assistants; |
| 9 | |
| 10 | // Internal subclient that handles paginated requests |
| 11 | internal class MessageCollectionResult : CollectionResult<ThreadMessage> |
| 12 | { |
| 13 | private readonly InternalAssistantMessageClient _messageClient; |
| 14 | private readonly RequestOptions? _options; |
| 15 | |
| 16 | // Initial values |
| 17 | private readonly string _threadId; |
| 18 | private readonly int? _limit; |
| 19 | private readonly string? _order; |
| 20 | private readonly string? _after; |
| 21 | private readonly string? _before; |
| 22 | |
| 23 | public MessageCollectionResult(InternalAssistantMessageClient messageClient, |
| 24 | RequestOptions? options, |
| 25 | string threadId, int? limit, string? order, string? after, string? before) |
| 26 | { |
| 27 | _messageClient = messageClient; |
| 28 | _options = options; |
| 29 | |
| 30 | _threadId = threadId; |
| 31 | _limit = limit; |
| 32 | _order = order; |
| 33 | _after = after; |
| 34 | _before = before; |
| 35 | } |
| 36 | |
| 37 | public override IEnumerable<ClientResult> GetRawPages() |
| 38 | { |
| 39 | ClientResult page = GetFirstPage(); |
| 40 | yield return page; |
| 41 | |
| 42 | while (HasNextPage(page)) |
| 43 | { |
| 44 | page = GetNextPage(page); |
| 45 | yield return page; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | protected override IEnumerable<ThreadMessage> GetValuesFromPage(ClientResult page) |
| 50 | { |
| 51 | Argument.AssertNotNull(page, nameof(page)); |
| 52 | |
| 53 | PipelineResponse response = page.GetRawResponse(); |
| 54 | InternalListMessagesResponse list = ModelReaderWriter.Read<InternalListMessagesResponse>(response.Content)!; |
| 55 | return list.Data; |
| 56 | } |
| 57 | |
| 58 | public override ContinuationToken? GetContinuationToken(ClientResult page) |
| 59 | { |
| 60 | Argument.AssertNotNull(page, nameof(page)); |
| 61 | |
| 62 | return MessageCollectionPageToken.FromResponse(page, _threadId, _limit, _order, _before); |
| 63 | } |
| 64 | |
| 65 | public ClientResult GetFirstPage() |
| 66 | => _messageClient.GetMessages(_threadId, _limit, _order, _after, _before, _options); |
| 67 | |
| 68 | public ClientResult GetNextPage(ClientResult result) |
| 69 | { |
| 70 | Argument.AssertNotNull(result, nameof(result)); |
| 71 | |
| 72 | PipelineResponse response = result.GetRawResponse(); |
| 73 | |
| 74 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 75 | string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; |
| 76 | |
| 77 | return _messageClient.GetMessages(_threadId, _limit, _order, lastId, _before, _options); |
| 78 | } |
| 79 | |
| 80 | public static bool HasNextPage(ClientResult result) |
| 81 | { |
| 82 | Argument.AssertNotNull(result, nameof(result)); |
| 83 | |
| 84 | PipelineResponse response = result.GetRawResponse(); |
| 85 | |
| 86 | using JsonDocument doc = JsonDocument.Parse(response.Content); |
| 87 | bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); |
| 88 | |
| 89 | return hasMore; |
| 90 | } |
| 91 | } |
| 92 | |