diff --git a/src/Custom/Batch/Internal/Pagination/AsyncBatchCollectionResult.cs b/src/Custom/Batch/Internal/Pagination/AsyncBatchCollectionResult.cs deleted file mode 100644 index 54f51a0bf..000000000 --- a/src/Custom/Batch/Internal/Pagination/AsyncBatchCollectionResult.cs +++ /dev/null @@ -1,75 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using System.Threading.Tasks; - -#nullable enable - -namespace OpenAI.Batch; - -internal class AsyncBatchCollectionResult : AsyncCollectionResult -{ - private readonly BatchClient _batchClient; - private readonly ClientPipeline _pipeline; - private readonly RequestOptions? _options; - - // Initial values - private readonly int? _limit; - private readonly string _after; - - public AsyncBatchCollectionResult(BatchClient batchClient, - ClientPipeline pipeline, RequestOptions? options, - int? limit, string after) - { - _batchClient = batchClient; - _pipeline = pipeline; - _options = options; - - _limit = limit; - _after = after; - } - - public async override IAsyncEnumerable GetRawPagesAsync() - { - ClientResult page = await GetFirstPageAsync().ConfigureAwait(false); - yield return page; - - while (HasNextPage(page)) - { - page = await GetNextPageAsync(page); - yield return page; - } - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return BatchCollectionPageToken.FromResponse(page, _limit); - } - - public async Task GetFirstPageAsync() - => await GetBatchesAsync(_after, _limit, _options).ConfigureAwait(false); - - public async Task GetNextPageAsync(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return await GetBatchesAsync(lastId, _limit, _options).ConfigureAwait(false); - } - - public static bool HasNextPage(ClientResult result) - => BatchCollectionResult.HasNextPage(result); - - internal virtual async Task GetBatchesAsync(string? after, int? limit, RequestOptions? options) - { - using PipelineMessage message = _batchClient.CreateGetBatchesRequest(after, limit, options); - return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); - } -} diff --git a/src/Custom/Batch/Internal/Pagination/BatchCollectionPageToken.cs b/src/Custom/Batch/Internal/Pagination/BatchCollectionPageToken.cs deleted file mode 100644 index 45ed233b0..000000000 --- a/src/Custom/Batch/Internal/Pagination/BatchCollectionPageToken.cs +++ /dev/null @@ -1,94 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Diagnostics; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.Batch; - -internal class BatchCollectionPageToken : ContinuationToken -{ - protected BatchCollectionPageToken(int? limit, string? after) - { - Limit = limit; - After = after; - } - - public int? Limit { get; } - - public string? After { get; } - - public static BatchCollectionPageToken FromToken(ContinuationToken pageToken) - { - if (pageToken is BatchCollectionPageToken token) - { - return token; - } - - BinaryData data = pageToken.ToBytes(); - - if (data.ToMemory().Length == 0) - { - throw new ArgumentException("Failed to create BatchCollectionPageToken from provided pageToken.", nameof(pageToken)); - } - - Utf8JsonReader reader = new(data); - - int? limit = null; - string? after = null; - - reader.Read(); - - Debug.Assert(reader.TokenType == JsonTokenType.StartObject); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - { - break; - } - - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); - - string propertyName = reader.GetString()!; - - switch (propertyName) - { - case "limit": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.Number); - limit = reader.GetInt32(); - break; - case "after": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - after = reader.GetString(); - break; - default: - throw new JsonException($"Unrecognized property '{propertyName}'."); - } - } - - return new(limit, after); - } - - public static BatchCollectionPageToken FromOptions(int? limit, string? after) - => new(limit, after); - - public static BatchCollectionPageToken? FromResponse(ClientResult result, int? limit) - { - PipelineResponse response = result.GetRawResponse(); - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - if (!hasMore || lastId is null) - { - return null; - } - - return new(limit, lastId); - } -} diff --git a/src/Custom/Batch/Internal/Pagination/BatchCollectionResult.cs b/src/Custom/Batch/Internal/Pagination/BatchCollectionResult.cs deleted file mode 100644 index a85682b4f..000000000 --- a/src/Custom/Batch/Internal/Pagination/BatchCollectionResult.cs +++ /dev/null @@ -1,85 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.Batch; - -internal class BatchCollectionResult : CollectionResult -{ - private readonly BatchClient _batchClient; - private readonly ClientPipeline _pipeline; - private readonly RequestOptions? _options; - - // Initial values - private readonly int? _limit; - private readonly string _after; - - public BatchCollectionResult(BatchClient batchClient, - ClientPipeline pipeline, RequestOptions? options, - int? limit, string after) - { - _batchClient = batchClient; - _pipeline = pipeline; - _options = options; - - _limit = limit; - _after = after; - } - - public override IEnumerable GetRawPages() - { - ClientResult page = GetFirstPage(); - yield return page; - - while (HasNextPage(page)) - { - page = GetNextPage(page); - yield return page; - } - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return BatchCollectionPageToken.FromResponse(page, _limit); - } - - public ClientResult GetFirstPage() - => GetBatches(_after, _limit, _options); - - public ClientResult GetNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return GetBatches(lastId, _limit, _options); - } - - public static bool HasNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - return hasMore; - } - - // TODO: Ideally these would come from internal generated client or other - // standardized pattern - internal virtual ClientResult GetBatches(string after, int? limit, RequestOptions? options) - { - using PipelineMessage message = _batchClient.CreateGetBatchesRequest(after, limit, options); - return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options)); - } -} diff --git a/src/Custom/Responses/Pagination/Internal/AsyncResponseItemCollectionResult.cs b/src/Custom/Responses/Pagination/Internal/AsyncResponseItemCollectionResult.cs deleted file mode 100644 index 259fd0433..000000000 --- a/src/Custom/Responses/Pagination/Internal/AsyncResponseItemCollectionResult.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -#nullable enable - -namespace OpenAI.Responses; - -internal class AsyncResponseItemCollectionResult : AsyncCollectionResult -{ - private readonly OpenAIResponseClient _parentClient; - private readonly RequestOptions? _options; - - // Initial values - private readonly string _responseId; - private readonly int? _limit; - private readonly string? _order; - private readonly string? _after; - private readonly string? _before; - - public AsyncResponseItemCollectionResult( - OpenAIResponseClient parentClient, - string responseId, - int? limit, string? order, string? after, string? before, - RequestOptions? options) - { - _parentClient = parentClient; - _responseId = responseId; - _limit = limit; - _order = order; - _after = after; - _before = before; - _options = options; - } - - public async override IAsyncEnumerable GetRawPagesAsync() - { - ClientResult page = await GetFirstPageAsync().ConfigureAwait(false); - yield return page; - - while (HasNextPage(page)) - { - page = await GetNextPageAsync(page); - yield return page; - } - } - - protected override IAsyncEnumerable GetValuesFromPageAsync(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - PipelineResponse response = page.GetRawResponse(); - InternalResponseItemList list = ModelReaderWriter.Read(response.Content, ModelReaderWriterOptions.Json, OpenAIContext.Default)!; - return list.Data.ToAsyncEnumerable(_options?.CancellationToken ?? default); - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return ResponseItemCollectionPageToken.FromResponse(page, _limit, _order, _before); - } - - public async Task GetFirstPageAsync() - => await GetResponsesAsync(_responseId, _limit, _order, _after, _before, _options).ConfigureAwait(false); - - public async Task GetNextPageAsync(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return await GetResponsesAsync(_responseId, _limit, _order, lastId, _before, _options).ConfigureAwait(false); - } - - public static bool HasNextPage(ClientResult result) => ResponseItemCollectionResult.HasNextPage(result); - - internal virtual async Task GetResponsesAsync(string responseId, int? limit, string? order, string? after, string? before, RequestOptions? options) - { - using PipelineMessage message = _parentClient.CreateGetResponseInputItemsRequest(responseId, limit, order, after, before, options); - return ClientResult.FromResponse(await _parentClient.Pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); - } -} diff --git a/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionPageToken.cs b/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionPageToken.cs deleted file mode 100644 index 73d63f65c..000000000 --- a/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionPageToken.cs +++ /dev/null @@ -1,148 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Diagnostics; -using System.IO; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.Responses; - -internal class ResponseItemCollectionPageToken : ContinuationToken -{ - protected ResponseItemCollectionPageToken(int? limit, string? order, string? after, string? before) - { - Limit = limit; - Order = order; - After = after; - Before = before; - } - - public int? Limit { get; } - - public string? Order { get; } - - public string? After { get; } - - public string? Before { get; } - - public override BinaryData ToBytes() - { - using MemoryStream stream = new(); - using Utf8JsonWriter writer = new(stream); - - writer.WriteStartObject(); - - if (Limit.HasValue) - { - writer.WriteNumber("limit", Limit.Value); - } - - if (Order is not null) - { - writer.WriteString("order", Order); - } - - if (After is not null) - { - writer.WriteString("after", After); - } - - if (Before is not null) - { - writer.WriteString("before", Before); - } - - writer.WriteEndObject(); - - writer.Flush(); - stream.Position = 0; - - return BinaryData.FromStream(stream); - } - - public static ResponseItemCollectionPageToken FromToken(ContinuationToken token) - { - if (token is ResponseItemCollectionPageToken pageToken) - { - return pageToken; - } - - BinaryData data = token.ToBytes(); - - if (data.ToMemory().Length == 0) - { - throw new ArgumentException("Failed to create ResponsesItemCollectionPageToken from provided pageToken.", nameof(pageToken)); - } - - Utf8JsonReader reader = new(data); - - int? limit = null; - string? order = null; - string? after = null; - string? before = null; - - reader.Read(); - - Debug.Assert(reader.TokenType == JsonTokenType.StartObject); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - { - break; - } - - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); - - string propertyName = reader.GetString()!; - - switch (propertyName) - { - case "limit": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.Number); - limit = reader.GetInt32(); - break; - case "order": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - order = reader.GetString(); - break; - case "after": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - after = reader.GetString(); - break; - case "before": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - before = reader.GetString(); - break; - default: - throw new JsonException($"Unrecognized property '{propertyName}'."); - } - } - - return new(limit, order, after, before); - } - - public static ResponseItemCollectionPageToken FromOptions(int? limit, string? order, string? after, string? before) - => new ResponseItemCollectionPageToken(limit, order, after, before); - - public static ResponseItemCollectionPageToken? FromResponse(ClientResult result, int? limit, string? order, string? before) - { - PipelineResponse response = result.GetRawResponse(); - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - if (!hasMore || lastId is null) - { - return null; - } - - return new(limit, order, lastId, before); - } -} \ No newline at end of file diff --git a/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionResult.cs b/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionResult.cs deleted file mode 100644 index 98dbbdbbf..000000000 --- a/src/Custom/Responses/Pagination/Internal/ResponseItemCollectionResult.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.Responses; - -internal class ResponseItemCollectionResult : CollectionResult -{ - private readonly OpenAIResponseClient _parentClient; - private readonly RequestOptions? _options; - - // Initial values - private readonly string _responseId; - private readonly int? _limit; - private readonly string? _order; - private readonly string? _after; - private readonly string? _before; - - public ResponseItemCollectionResult( - OpenAIResponseClient parentClient, - string responseId, - int? limit, string? order, string? after, string? before, - RequestOptions? options) - { - _parentClient = parentClient; - _responseId = responseId; - _limit = limit; - _order = order; - _after = after; - _before = before; - _options = options; - } - - public override IEnumerable GetRawPages() - { - ClientResult page = GetFirstPage(); - yield return page; - - while (HasNextPage(page)) - { - page = GetNextPage(page); - yield return page; - } - } - - protected override IEnumerable GetValuesFromPage(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - PipelineResponse response = page.GetRawResponse(); - InternalResponseItemList list = ModelReaderWriter.Read(response.Content, ModelReaderWriterOptions.Json, OpenAIContext.Default)!; - return list.Data; - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return ResponseItemCollectionPageToken.FromResponse(page, _limit, _order, _before); - } - - public ClientResult GetFirstPage() - => GetResponseInputItems(_responseId, _limit, _order, _after, _before, _options); - - public ClientResult GetNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return GetResponseInputItems(_responseId, _limit, _order, lastId, _before, _options); - } - - public static bool HasNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - return hasMore; - } - - internal virtual ClientResult GetResponseInputItems(string responseId, int? limit, string? order, string? after, string? before, RequestOptions? options) - { - using PipelineMessage message = _parentClient.CreateGetResponseInputItemsRequest(responseId, limit, order, after, before, options); - return ClientResult.FromResponse(_parentClient.Pipeline.ProcessMessage(message, options)); - } -} diff --git a/src/Custom/Responses/Pagination/ResponseItemCollectionOptions.cs b/src/Custom/Responses/Pagination/ResponseItemCollectionOptions.cs deleted file mode 100644 index 709f7b5c2..000000000 --- a/src/Custom/Responses/Pagination/ResponseItemCollectionOptions.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace OpenAI.Responses; - -// CUSTOM: Make public and use the correct namespace. -[CodeGenType("ResponseItemCollectionOptions")] public partial class ResponseItemCollectionOptions { } diff --git a/src/Custom/Responses/Pagination/ResponseItemCollectionOrder.cs b/src/Custom/Responses/Pagination/ResponseItemCollectionOrder.cs deleted file mode 100644 index cc0b6d9d3..000000000 --- a/src/Custom/Responses/Pagination/ResponseItemCollectionOrder.cs +++ /dev/null @@ -1,4 +0,0 @@ -namespace OpenAI.Responses; - -// CUSTOM: Make public and use the correct namespace. -[CodeGenType("ResponseItemCollectionOrder")] public readonly partial struct ResponseItemCollectionOrder { } diff --git a/src/Custom/VectorStores/Internal/Pagination/AsyncVectorStoreCollectionResult.cs b/src/Custom/VectorStores/Internal/Pagination/AsyncVectorStoreCollectionResult.cs deleted file mode 100644 index 450c68c96..000000000 --- a/src/Custom/VectorStores/Internal/Pagination/AsyncVectorStoreCollectionResult.cs +++ /dev/null @@ -1,89 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; - -#nullable enable - -namespace OpenAI.VectorStores; - -internal class AsyncVectorStoreCollectionResult : AsyncCollectionResult -{ - private readonly VectorStoreClient _vectorStoreClient; - private readonly ClientPipeline _pipeline; - private readonly RequestOptions? _options; - private readonly CancellationToken _cancellationToken; - - // Initial values - private readonly int? _limit; - private readonly string? _order; - private readonly string? _after; - private readonly string? _before; - - public AsyncVectorStoreCollectionResult(VectorStoreClient vectorStoreClient, - ClientPipeline pipeline, RequestOptions? options, - int? limit, string? order, string? after, string? before) - { - _vectorStoreClient = vectorStoreClient; - _pipeline = pipeline; - _options = options; - _cancellationToken = _options?.CancellationToken ?? default; - - _limit = limit; - _order = order; - _after = after; - _before = before; - } - - public async override IAsyncEnumerable GetRawPagesAsync() - { - ClientResult page = await GetFirstPageAsync().ConfigureAwait(false); - yield return page; - - while (HasNextPage(page)) - { - page = await GetNextPageAsync(page); - yield return page; - } - } - - protected override IAsyncEnumerable GetValuesFromPageAsync(ClientResult page) - { - PipelineResponse response = page.GetRawResponse(); - InternalListVectorStoresResponse list = ModelReaderWriter.Read(response.Content, ModelReaderWriterOptions.Json, OpenAIContext.Default)!; - return list.Data.ToAsyncEnumerable(_cancellationToken); - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return VectorStoreCollectionPageToken.FromResponse(page, _limit, _order, _before); - } - - public async Task GetFirstPageAsync() - => await GetVectorStoresAsync(_limit, _order, _after, _before, _options).ConfigureAwait(false); - - public async Task GetNextPageAsync(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return await GetVectorStoresAsync(_limit, _order, lastId, _before, _options).ConfigureAwait(false); - } - - public static bool HasNextPage(ClientResult result) - => VectorStoreCollectionResult.HasNextPage(result); - - internal virtual async Task GetVectorStoresAsync(int? limit, string? order, string? after, string? before, RequestOptions? options) - { - using PipelineMessage message = _vectorStoreClient.CreateGetVectorStoresRequest(limit, order, after, before, options); - return ClientResult.FromResponse(await _pipeline.ProcessMessageAsync(message, options).ConfigureAwait(false)); - } -} diff --git a/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionPageToken.cs b/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionPageToken.cs deleted file mode 100644 index 4cbe749c3..000000000 --- a/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionPageToken.cs +++ /dev/null @@ -1,148 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Diagnostics; -using System.IO; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.VectorStores; - -internal class VectorStoreCollectionPageToken : ContinuationToken -{ - protected VectorStoreCollectionPageToken(int? limit, string? order, string? after, string? before) - { - Limit = limit; - Order = order; - After = after; - Before = before; - } - - public int? Limit { get; } - - public string? Order { get; } - - public string? After { get; } - - public string? Before { get; } - - public override BinaryData ToBytes() - { - using MemoryStream stream = new(); - using Utf8JsonWriter writer = new(stream); - - writer.WriteStartObject(); - - if (Limit.HasValue) - { - writer.WriteNumber("limit", Limit.Value); - } - - if (Order is not null) - { - writer.WriteString("order", Order); - } - - if (After is not null) - { - writer.WriteString("after", After); - } - - if (Before is not null) - { - writer.WriteString("before", Before); - } - - writer.WriteEndObject(); - - writer.Flush(); - stream.Position = 0; - - return BinaryData.FromStream(stream); - } - - public static VectorStoreCollectionPageToken FromToken(ContinuationToken pageToken) - { - if (pageToken is VectorStoreCollectionPageToken token) - { - return token; - } - - BinaryData data = pageToken.ToBytes(); - - if (data.ToMemory().Length == 0) - { - throw new ArgumentException("Failed to create VectorStoresPageToken from provided pageToken.", nameof(pageToken)); - } - - Utf8JsonReader reader = new(data); - - int? limit = null; - string? order = null; - string? after = null; - string? before = null; - - reader.Read(); - - Debug.Assert(reader.TokenType == JsonTokenType.StartObject); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - { - break; - } - - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); - - string propertyName = reader.GetString()!; - - switch (propertyName) - { - case "limit": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.Number); - limit = reader.GetInt32(); - break; - case "order": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - order = reader.GetString(); - break; - case "after": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - after = reader.GetString(); - break; - case "before": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - before = reader.GetString(); - break; - default: - throw new JsonException($"Unrecognized property '{propertyName}'."); - } - } - - return new(limit, order, after, before); - } - - public static VectorStoreCollectionPageToken FromOptions(int? limit, string? order, string? after, string? before) - => new(limit, order, after, before); - - public static VectorStoreCollectionPageToken? FromResponse(ClientResult result, int? limit, string? order, string? before) - { - PipelineResponse response = result.GetRawResponse(); - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - if (!hasMore || lastId is null) - { - return null; - } - - return new(limit, order, lastId, before); - } -} \ No newline at end of file diff --git a/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionResult.cs b/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionResult.cs deleted file mode 100644 index b15a30d77..000000000 --- a/src/Custom/VectorStores/Internal/Pagination/VectorStoreCollectionResult.cs +++ /dev/null @@ -1,96 +0,0 @@ -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Collections.Generic; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.VectorStores; - -internal class VectorStoreCollectionResult : CollectionResult -{ - private readonly VectorStoreClient _vectorStoreClient; - private readonly ClientPipeline _pipeline; - private readonly RequestOptions? _options; - - // Initial values - private readonly int? _limit; - private readonly string? _order; - private readonly string? _after; - private readonly string? _before; - - public VectorStoreCollectionResult(VectorStoreClient vectorStoreClient, - ClientPipeline pipeline, RequestOptions? options, - int? limit, string? order, string? after, string? before) - { - _vectorStoreClient = vectorStoreClient; - _pipeline = pipeline; - _options = options; - - _limit = limit; - _order = order; - _after = after; - _before = before; - } - - public override IEnumerable GetRawPages() - { - ClientResult page = GetFirstPage(); - yield return page; - - while (HasNextPage(page)) - { - page = GetNextPage(page); - yield return page; - } - } - - protected override IEnumerable GetValuesFromPage(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - PipelineResponse response = page.GetRawResponse(); - InternalListVectorStoresResponse list = ModelReaderWriter.Read(response.Content, ModelReaderWriterOptions.Json, OpenAIContext.Default)!; - return list.Data; - } - - public override ContinuationToken? GetContinuationToken(ClientResult page) - { - Argument.AssertNotNull(page, nameof(page)); - - return VectorStoreCollectionPageToken.FromResponse(page, _limit, _order, _before); - } - - public ClientResult GetFirstPage() - => GetVectorStores(_limit, _order, _after, _before, _options); - - public ClientResult GetNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - - return GetVectorStores(_limit, _order, lastId, _before, _options); - } - - public static bool HasNextPage(ClientResult result) - { - Argument.AssertNotNull(result, nameof(result)); - - PipelineResponse response = result.GetRawResponse(); - - using JsonDocument doc = JsonDocument.Parse(response.Content); - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - return hasMore; - } - - internal virtual ClientResult GetVectorStores(int? limit, string? order, string? after, string? before, RequestOptions? options) - { - using PipelineMessage message = _vectorStoreClient.CreateGetVectorStoresRequest(limit, order, after, before, options); - return ClientResult.FromResponse(_pipeline.ProcessMessage(message, options)); - } -} diff --git a/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileBatchCollectionPageToken.cs b/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileBatchCollectionPageToken.cs deleted file mode 100644 index e71a72464..000000000 --- a/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileBatchCollectionPageToken.cs +++ /dev/null @@ -1,189 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Diagnostics; -using System.IO; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.VectorStores; - -internal class VectorStoreFileBatchCollectionPageToken : ContinuationToken -{ - protected VectorStoreFileBatchCollectionPageToken(string vectorStoreId, string batchId, int? limit, string? order, string? after, string? before, string? filter) - { - VectorStoreId = vectorStoreId; - BatchId = batchId; - - Limit = limit; - Order = order; - After = after; - Before = before; - Filter = filter; - } - - public string VectorStoreId { get; } - - public string BatchId { get; } - - public int? Limit { get; } - - public string? Order { get; } - - public string? After { get; } - - public string? Before { get; } - - public string? Filter { get; } - - public override BinaryData ToBytes() - { - using MemoryStream stream = new(); - using Utf8JsonWriter writer = new(stream); - - writer.WriteStartObject(); - writer.WriteString("vectorStoreId", VectorStoreId); - writer.WriteString("batchId", BatchId); - - if (Limit.HasValue) - { - writer.WriteNumber("limit", Limit.Value); - } - - if (Order is not null) - { - writer.WriteString("order", Order); - } - - if (After is not null) - { - writer.WriteString("after", After); - } - - if (Before is not null) - { - writer.WriteString("before", Before); - } - - if (Filter is not null) - { - writer.WriteString("filter", Filter); - } - - writer.WriteEndObject(); - - writer.Flush(); - stream.Position = 0; - - return BinaryData.FromStream(stream); - } - - public static VectorStoreFileBatchCollectionPageToken FromToken(ContinuationToken pageToken) - { - if (pageToken is VectorStoreFileBatchCollectionPageToken token) - { - return token; - } - - BinaryData data = pageToken.ToBytes(); - - if (data.ToMemory().Length == 0) - { - throw new ArgumentException("Failed to create VectorStoreFileBatchesPageToken from provided pageToken.", nameof(pageToken)); - } - - Utf8JsonReader reader = new(data); - - string vectorStoreId = null!; - string batchId = null!; - int? limit = null; - string? order = null; - string? after = null; - string? before = null; - string? filter = null; - - reader.Read(); - - Debug.Assert(reader.TokenType == JsonTokenType.StartObject); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - { - break; - } - - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); - - string propertyName = reader.GetString()!; - - switch (propertyName) - { - case "vectorStoreId": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - vectorStoreId = reader.GetString()!; - break; - case "batchId": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - batchId = reader.GetString()!; - break; - case "limit": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.Number); - limit = reader.GetInt32(); - break; - case "order": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - order = reader.GetString(); - break; - case "after": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - after = reader.GetString(); - break; - case "before": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - before = reader.GetString(); - break; - case "filter": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - filter = reader.GetString(); - break; - default: - throw new JsonException($"Unrecognized property '{propertyName}'."); - } - } - - if (vectorStoreId is null || - batchId is null) - { - throw new ArgumentException("Failed to create VectorStoreFileBatchesPageToken from provided pageToken.", nameof(pageToken)); - } - - return new(vectorStoreId, batchId, limit, order, after, before, filter); - } - - public static VectorStoreFileBatchCollectionPageToken FromOptions(string vectorStoreId, string batchId, int? limit, string? order, string? after, string? before, string? filter) - => new(vectorStoreId, batchId, limit, order, after, before, filter); - - public static VectorStoreFileBatchCollectionPageToken? FromResponse(ClientResult result, string vectorStoreId, string batchId, int? limit, string? order, string? before, string? filter) - { - PipelineResponse response = result.GetRawResponse(); - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - if (!hasMore || lastId is null) - { - return null; - } - - return new(vectorStoreId, batchId, limit, order, lastId, before, filter); - } -} \ No newline at end of file diff --git a/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileCollectionPageToken.cs b/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileCollectionPageToken.cs deleted file mode 100644 index aef1594e6..000000000 --- a/src/Custom/VectorStores/Internal/Pagination/VectorStoreFileCollectionPageToken.cs +++ /dev/null @@ -1,178 +0,0 @@ -using System; -using System.ClientModel; -using System.ClientModel.Primitives; -using System.Diagnostics; -using System.IO; -using System.Text.Json; - -#nullable enable - -namespace OpenAI.VectorStores; - -internal class VectorStoreFileCollectionPageToken : ContinuationToken -{ - protected VectorStoreFileCollectionPageToken(string vectorStoreId, int? limit, string? order, string? after, string? before, string? filter) - { - VectorStoreId = vectorStoreId; - - Limit = limit; - Order = order; - After = after; - Before = before; - Filter = filter; - } - - public string VectorStoreId { get; } - - public int? Limit { get; } - - public string? Order { get; } - - public string? After { get; } - - public string? Before { get; } - - public string? Filter { get; } - - public override BinaryData ToBytes() - { - using MemoryStream stream = new(); - using Utf8JsonWriter writer = new(stream); - - writer.WriteStartObject(); - writer.WriteString("vectorStoreId", VectorStoreId); - - if (Limit.HasValue) - { - writer.WriteNumber("limit", Limit.Value); - } - - if (Order is not null) - { - writer.WriteString("order", Order); - } - - if (After is not null) - { - writer.WriteString("after", After); - } - - if (Before is not null) - { - writer.WriteString("before", Before); - } - - if (Filter is not null) - { - writer.WriteString("filter", Filter); - } - - writer.WriteEndObject(); - - writer.Flush(); - stream.Position = 0; - - return BinaryData.FromStream(stream); - } - - public static VectorStoreFileCollectionPageToken FromToken(ContinuationToken pageToken) - { - if (pageToken is VectorStoreFileCollectionPageToken token) - { - return token; - } - - BinaryData data = pageToken.ToBytes(); - - if (data.ToMemory().Length == 0) - { - throw new ArgumentException("Failed to create VectorStoreFilesPageToken from provided pageToken.", nameof(pageToken)); - } - - Utf8JsonReader reader = new(data); - - string vectorStoreId = null!; - int? limit = null; - string? order = null; - string? after = null; - string? before = null; - string? filter = null; - - reader.Read(); - - Debug.Assert(reader.TokenType == JsonTokenType.StartObject); - - while (reader.Read()) - { - if (reader.TokenType == JsonTokenType.EndObject) - { - break; - } - - Debug.Assert(reader.TokenType == JsonTokenType.PropertyName); - - string propertyName = reader.GetString()!; - - switch (propertyName) - { - case "vectorStoreId": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - vectorStoreId = reader.GetString()!; - break; - case "limit": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.Number); - limit = reader.GetInt32(); - break; - case "order": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - order = reader.GetString(); - break; - case "after": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - after = reader.GetString(); - break; - case "before": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - before = reader.GetString(); - break; - case "filter": - reader.Read(); - Debug.Assert(reader.TokenType == JsonTokenType.String); - filter = reader.GetString(); - break; - default: - throw new JsonException($"Unrecognized property '{propertyName}'."); - } - } - - if (vectorStoreId is null) - { - throw new ArgumentException("Failed to create VectorStoreFilesPageToken from provided pageToken.", nameof(pageToken)); - } - - return new(vectorStoreId, limit, order, after, before, filter); - } - - public static VectorStoreFileCollectionPageToken FromOptions(string vectorStoreId, int? limit, string? order, string? after, string? before, string? filter) - => new(vectorStoreId, limit, order, after, before, filter); - - public static VectorStoreFileCollectionPageToken? FromResponse(ClientResult result, string vectorStoreId, int? limit, string? order, string? before, string? filter) - { - PipelineResponse response = result.GetRawResponse(); - using JsonDocument doc = JsonDocument.Parse(response.Content); - string lastId = doc.RootElement.GetProperty("last_id"u8).GetString()!; - bool hasMore = doc.RootElement.GetProperty("has_more"u8).GetBoolean(); - - if (!hasMore || lastId is null) - { - return null; - } - - return new(vectorStoreId, limit, order, lastId, before, filter); - } -} \ No newline at end of file