diff --git a/.dotnet/OpenAI.sln b/.dotnet/OpenAI.sln index d6350d85f..b69815bd1 100644 --- a/.dotnet/OpenAI.sln +++ b/.dotnet/OpenAI.sln @@ -6,7 +6,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI", "src\OpenAI.csproj EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI.Examples", "examples\OpenAI.Examples.csproj", "{1F1CD1D4-9932-4B73-99D8-C252A67D4B46}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenAI.Tests", "tests\OpenAI.Tests.csproj", "{6F156401-2544-41D7-B204-3148C51C1D09}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenAI.Tests", "tests\OpenAI.Tests.csproj", "{6F156401-2544-41D7-B204-3148C51C1D09}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.ClientModel", "..\..\azure-sdk-for-net\sdk\core\System.ClientModel\src\System.ClientModel.csproj", "{C427A4AD-EEC8-4149-8F9F-29F963F2D81A}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -26,6 +28,10 @@ Global {6F156401-2544-41D7-B204-3148C51C1D09}.Debug|Any CPU.Build.0 = Debug|Any CPU {6F156401-2544-41D7-B204-3148C51C1D09}.Release|Any CPU.ActiveCfg = Release|Any CPU {6F156401-2544-41D7-B204-3148C51C1D09}.Release|Any CPU.Build.0 = Release|Any CPU + {C427A4AD-EEC8-4149-8F9F-29F963F2D81A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C427A4AD-EEC8-4149-8F9F-29F963F2D81A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C427A4AD-EEC8-4149-8F9F-29F963F2D81A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C427A4AD-EEC8-4149-8F9F-29F963F2D81A}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -33,4 +39,4 @@ Global GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {A97F4B90-2591-4689-B1F8-5F21FE6D6CAE} EndGlobalSection -EndGlobal \ No newline at end of file +EndGlobal diff --git a/.dotnet/api/OpenAI.netstandard2.0.cs b/.dotnet/api/OpenAI.netstandard2.0.cs index 33e6bbf34..8e89484e7 100644 --- a/.dotnet/api/OpenAI.netstandard2.0.cs +++ b/.dotnet/api/OpenAI.netstandard2.0.cs @@ -1153,7 +1153,6 @@ public class BatchClient { } public class CreateBatchOperation : OperationResult { public string BatchId { get; } - public override bool IsCompleted { get; protected set; } public override ContinuationToken? RehydrationToken { get; protected set; } public virtual ClientResult CancelBatch(RequestOptions? options); public virtual Task CancelBatchAsync(RequestOptions? options); @@ -1161,8 +1160,8 @@ public class CreateBatchOperation : OperationResult { public virtual Task GetBatchAsync(RequestOptions? options); public static CreateBatchOperation Rehydrate(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); public static Task RehydrateAsync(BatchClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); - public override void WaitForCompletion(CancellationToken cancellationToken = default); - public override Task WaitForCompletionAsync(CancellationToken cancellationToken = default); + public override ClientResult UpdateStatus(RequestOptions? options = null); + public override ValueTask UpdateStatusAsync(RequestOptions? options = null); } } namespace OpenAI.Chat { @@ -1729,7 +1728,6 @@ public class OpenAIFileInfoCollection : ObjectModel.ReadOnlyCollection GetJobEventsAsync(string after, int? limit, RequestOptions? options); public static CreateJobOperation Rehydrate(FineTuningClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); public static Task RehydrateAsync(FineTuningClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); - public override void WaitForCompletion(CancellationToken cancellationToken = default); - public override Task WaitForCompletionAsync(CancellationToken cancellationToken = default); + public override ClientResult UpdateStatus(RequestOptions? options = null); + public override ValueTask UpdateStatusAsync(RequestOptions? options = null); } public class FineTuningClient { protected FineTuningClient(); @@ -2005,9 +2003,7 @@ public class ModerationResult : IJsonModel, IPersistableModel< } namespace OpenAI.VectorStores { public class AddFileToVectorStoreOperation : OperationResult { - public AddFileToVectorStoreOperation(ClientPipeline pipeline, Uri endpoint, ClientResult result) : base(default!); public string FileId { get; } - public override bool IsCompleted { get; protected set; } public override ContinuationToken? RehydrationToken { get; protected set; } public VectorStoreFileAssociationStatus? Status { get; } public VectorStoreFileAssociation? Value { get; } @@ -2020,12 +2016,11 @@ public class AddFileToVectorStoreOperation : OperationResult { public virtual Task> GetFileAssociationAsync(CancellationToken cancellationToken = default); public static AddFileToVectorStoreOperation Rehydrate(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); public static Task RehydrateAsync(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); - public override void WaitForCompletion(CancellationToken cancellationToken = default); - public override Task WaitForCompletionAsync(CancellationToken cancellationToken = default); + public override ClientResult UpdateStatus(RequestOptions? options = null); + public override ValueTask UpdateStatusAsync(RequestOptions? options = null); } public class CreateBatchFileJobOperation : OperationResult { public string BatchId { get; } - public override bool IsCompleted { get; protected set; } public override ContinuationToken? RehydrationToken { get; protected set; } public VectorStoreBatchFileJobStatus? Status { get; } public VectorStoreBatchFileJob? Value { get; } @@ -2052,12 +2047,10 @@ public class CreateBatchFileJobOperation : OperationResult { public virtual IAsyncEnumerable GetFilesInBatchAsync(int? limit, string? order, string? after, string? before, string? filter, RequestOptions? options); public static CreateBatchFileJobOperation Rehydrate(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); public static Task RehydrateAsync(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); - public override void WaitForCompletion(CancellationToken cancellationToken = default); - public override Task WaitForCompletionAsync(CancellationToken cancellationToken = default); + public override ClientResult UpdateStatus(RequestOptions? options = null); + public override ValueTask UpdateStatusAsync(RequestOptions? options = null); } public class CreateVectorStoreOperation : OperationResult { - public CreateVectorStoreOperation(ClientPipeline pipeline, Uri endpoint, ClientResult result) : base(default!); - public override bool IsCompleted { get; protected set; } public override ContinuationToken? RehydrationToken { get; protected set; } public VectorStoreStatus? Status { get; } public VectorStore? Value { get; } @@ -2070,8 +2063,8 @@ public class CreateVectorStoreOperation : OperationResult { public virtual Task> GetVectorStoreAsync(CancellationToken cancellationToken = default); public static CreateVectorStoreOperation Rehydrate(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); public static Task RehydrateAsync(VectorStoreClient client, ContinuationToken rehydrationToken, CancellationToken cancellationToken = default); - public override void WaitForCompletion(CancellationToken cancellationToken = default); - public override Task WaitForCompletionAsync(CancellationToken cancellationToken = default); + public override ClientResult UpdateStatus(RequestOptions? options = null); + public override ValueTask UpdateStatusAsync(RequestOptions? options = null); } public abstract class FileChunkingStrategy : IJsonModel, IPersistableModel { public static FileChunkingStrategy Auto { get; } diff --git a/.dotnet/src/Custom/Batch/CreateBatchOperation.Protocol.cs b/.dotnet/src/Custom/Batch/CreateBatchOperation.Protocol.cs index 4f2374293..b80a2f7cf 100644 --- a/.dotnet/src/Custom/Batch/CreateBatchOperation.Protocol.cs +++ b/.dotnet/src/Custom/Batch/CreateBatchOperation.Protocol.cs @@ -20,8 +20,6 @@ public partial class CreateBatchOperation : OperationResult private readonly string _batchId; - private PollingInterval? _pollingInterval; - internal CreateBatchOperation( ClientPipeline pipeline, Uri endpoint, @@ -34,7 +32,7 @@ internal CreateBatchOperation( _endpoint = endpoint; _batchId = batchId; - IsCompleted = GetIsCompleted(status); + HasCompleted = GetHasCompleted(status); RehydrationToken = new CreateBatchOperationToken(batchId); } @@ -43,9 +41,6 @@ internal CreateBatchOperation( /// public override ContinuationToken? RehydrationToken { get; protected set; } - /// - public override bool IsCompleted { get; protected set; } - /// /// Recreates a from a rehydration token. /// @@ -101,37 +96,23 @@ public static CreateBatchOperation Rehydrate(BatchClient client, ContinuationTok } /// - public override async Task WaitForCompletionAsync(CancellationToken cancellationToken = default) + public override async ValueTask UpdateStatusAsync(RequestOptions? options = null) { - _pollingInterval ??= new(); - - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); + ClientResult result = await GetBatchAsync(options).ConfigureAwait(false); - await _pollingInterval.WaitAsync(response, cancellationToken); + ApplyUpdate(result); - ClientResult result = await GetBatchAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); - - ApplyUpdate(result); - } + return result; } /// - public override void WaitForCompletion(CancellationToken cancellationToken = default) + public override ClientResult UpdateStatus(RequestOptions? options = null) { - _pollingInterval ??= new(); - - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - _pollingInterval.Wait(response, cancellationToken); + ClientResult result = GetBatch(options); - ClientResult result = GetBatch(cancellationToken.ToRequestOptions()); + ApplyUpdate(result); - ApplyUpdate(result); - } + return result; } internal async Task WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) @@ -155,11 +136,11 @@ private void ApplyUpdate(ClientResult result) using JsonDocument doc = JsonDocument.Parse(response.Content); string? status = doc.RootElement.GetProperty("status"u8).GetString(); - IsCompleted = GetIsCompleted(status); + HasCompleted = GetHasCompleted(status); SetRawResponse(response); } - private static bool GetIsCompleted(string? status) + private static bool GetHasCompleted(string? status) { return status == InternalBatchStatus.Completed || status == InternalBatchStatus.Cancelled || diff --git a/.dotnet/src/Custom/FineTuning/CreateJobOperation.Protocol.cs b/.dotnet/src/Custom/FineTuning/CreateJobOperation.Protocol.cs index e9601dd0e..1e4c592c4 100644 --- a/.dotnet/src/Custom/FineTuning/CreateJobOperation.Protocol.cs +++ b/.dotnet/src/Custom/FineTuning/CreateJobOperation.Protocol.cs @@ -19,8 +19,6 @@ public partial class CreateJobOperation : OperationResult private readonly string _jobId; - private PollingInterval? _pollingInterval; - internal CreateJobOperation( ClientPipeline pipeline, Uri endpoint, @@ -32,7 +30,7 @@ internal CreateJobOperation( _endpoint = endpoint; _jobId = jobId; - IsCompleted = GetIsCompleted(status); + HasCompleted = GetHasCompleted(status); RehydrationToken = new CreateJobOperationToken(jobId); } @@ -41,9 +39,6 @@ internal CreateJobOperation( /// public override ContinuationToken? RehydrationToken { get; protected set; } - /// - public override bool IsCompleted { get; protected set; } - /// /// Recreates a from a rehydration token. /// @@ -99,37 +94,23 @@ public static CreateJobOperation Rehydrate(FineTuningClient client, Continuation } /// - public override async Task WaitForCompletionAsync(CancellationToken cancellationToken = default) + public override async ValueTask UpdateStatusAsync(RequestOptions? options = null) { - _pollingInterval ??= new(); - - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - await _pollingInterval.WaitAsync(response, cancellationToken); + ClientResult result = await GetJobAsync(options).ConfigureAwait(false); - ClientResult result = await GetJobAsync(cancellationToken.ToRequestOptions()).ConfigureAwait(false); + ApplyUpdate(result); - ApplyUpdate(result); - } + return result; } /// - public override void WaitForCompletion(CancellationToken cancellationToken = default) + public override ClientResult UpdateStatus(RequestOptions? options = null) { - _pollingInterval ??= new(); - - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); + ClientResult result = GetJob(options); - _pollingInterval.Wait(response, cancellationToken); + ApplyUpdate(result); - ClientResult result = GetJob(cancellationToken.ToRequestOptions()); - - ApplyUpdate(result); - } + return result; } internal async Task WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) @@ -153,11 +134,11 @@ private void ApplyUpdate(ClientResult result) using JsonDocument doc = JsonDocument.Parse(response.Content); string? status = doc.RootElement.GetProperty("status"u8).GetString(); - IsCompleted = GetIsCompleted(status); + HasCompleted = GetHasCompleted(status); SetRawResponse(response); } - private static bool GetIsCompleted(string? status) + private static bool GetHasCompleted(string? status) { return status == InternalFineTuningJobStatus.Succeeded || status == InternalFineTuningJobStatus.Failed || diff --git a/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.Protocol.cs b/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.Protocol.cs index b5711c9ab..def381069 100644 --- a/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.Protocol.cs +++ b/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.Protocol.cs @@ -16,14 +16,9 @@ public partial class AddFileToVectorStoreOperation : OperationResult private readonly string _vectorStoreId; private readonly string _fileId; - private PollingInterval? _pollingInterval; - /// public override ContinuationToken? RehydrationToken { get; protected set; } - /// - public override bool IsCompleted { get; protected set; } - /// /// [Protocol Method] Retrieves a vector store file. /// diff --git a/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.cs b/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.cs index de4fc796b..e891d8d39 100644 --- a/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.cs +++ b/.dotnet/src/Custom/VectorStores/AddFileToVectorStoreOperation.cs @@ -12,7 +12,7 @@ namespace OpenAI.VectorStores; [Experimental("OPENAI001")] public partial class AddFileToVectorStoreOperation : OperationResult { - public AddFileToVectorStoreOperation( + internal AddFileToVectorStoreOperation( ClientPipeline pipeline, Uri endpoint, ClientResult result) @@ -27,7 +27,7 @@ public AddFileToVectorStoreOperation( _vectorStoreId = Value.VectorStoreId; _fileId = Value.FileId; - IsCompleted = GetIsCompleted(Value.Status); + HasCompleted = GetHasCompleted(Value.Status); RehydrationToken = new AddFileToVectorStoreOperationToken(VectorStoreId, FileId); } @@ -102,36 +102,29 @@ public static AddFileToVectorStoreOperation Rehydrate(VectorStoreClient client, } /// - public override async Task WaitForCompletionAsync(CancellationToken cancellationToken = default) + public override async ValueTask UpdateStatusAsync(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = await GetFileAssociationAsync(options).ConfigureAwait(false); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - await _pollingInterval.WaitAsync(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStoreFileAssociation value = VectorStoreFileAssociation.FromResponse(response); - ClientResult result = await GetFileAssociationAsync(cancellationToken).ConfigureAwait(false); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } - public override void WaitForCompletion(CancellationToken cancellationToken = default) + /// + public override ClientResult UpdateStatus(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = GetFileAssociation(options); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - _pollingInterval.Wait(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStoreFileAssociation value = VectorStoreFileAssociation.FromResponse(response); - ClientResult result = GetFileAssociation(cancellationToken); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } internal async Task WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) @@ -148,16 +141,16 @@ internal AddFileToVectorStoreOperation WaitUntil(bool waitUntilCompleted, Reques return this; } - private void ApplyUpdate(ClientResult update) + private void ApplyUpdate(PipelineResponse response, VectorStoreFileAssociation value) { - Value = update; - Status = Value.Status; + Value = value; + Status = value.Status; - IsCompleted = GetIsCompleted(Value.Status); - SetRawResponse(update.GetRawResponse()); + HasCompleted = GetHasCompleted(value.Status); + SetRawResponse(response); } - private static bool GetIsCompleted(VectorStoreFileAssociationStatus status) + private static bool GetHasCompleted(VectorStoreFileAssociationStatus status) { return status == VectorStoreFileAssociationStatus.Completed || status == VectorStoreFileAssociationStatus.Cancelled || diff --git a/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.Protocol.cs b/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.Protocol.cs index 44c604e81..3105b17f2 100644 --- a/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.Protocol.cs +++ b/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.Protocol.cs @@ -20,14 +20,9 @@ public partial class CreateBatchFileJobOperation : OperationResult private readonly string _vectorStoreId; private readonly string _batchId; - private PollingInterval? _pollingInterval; - /// public override ContinuationToken? RehydrationToken { get; protected set; } - /// - public override bool IsCompleted { get; protected set; } - // Generated protocol methods /// diff --git a/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.cs b/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.cs index a34cbae52..4daadf820 100644 --- a/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.cs +++ b/.dotnet/src/Custom/VectorStores/CreateBatchFileJobOperation.cs @@ -30,7 +30,7 @@ internal CreateBatchFileJobOperation( _vectorStoreId = Value.VectorStoreId; _batchId = Value.BatchId; - IsCompleted = GetIsCompleted(Value.Status); + HasCompleted = GetHasCompleted(Value.Status); RehydrationToken = new CreateBatchFileJobOperationToken(VectorStoreId, BatchId); } @@ -105,37 +105,29 @@ public static CreateBatchFileJobOperation Rehydrate(VectorStoreClient client, Co } /// - public override async Task WaitForCompletionAsync(CancellationToken cancellationToken = default) + public override async ValueTask UpdateStatusAsync(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = await GetFileBatchAsync(options).ConfigureAwait(false); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - await _pollingInterval.WaitAsync(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); - ClientResult result = await GetFileBatchAsync(cancellationToken).ConfigureAwait(false); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } /// - public override void WaitForCompletion(CancellationToken cancellationToken = default) + public override ClientResult UpdateStatus(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = GetFileBatch(options); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - _pollingInterval.Wait(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStoreBatchFileJob value = VectorStoreBatchFileJob.FromResponse(response); - ClientResult result = GetFileBatch(cancellationToken); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } internal async Task WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) @@ -152,16 +144,16 @@ internal CreateBatchFileJobOperation WaitUntil(bool waitUntilCompleted, RequestO return this; } - private void ApplyUpdate(ClientResult update) + private void ApplyUpdate(PipelineResponse response, VectorStoreBatchFileJob value) { - Value = update; - Status = Value.Status; + Value = value; + Status = value.Status; - IsCompleted = GetIsCompleted(Value.Status); - SetRawResponse(update.GetRawResponse()); + HasCompleted = GetHasCompleted(value.Status); + SetRawResponse(response); } - private static bool GetIsCompleted(VectorStoreBatchFileJobStatus status) + private static bool GetHasCompleted(VectorStoreBatchFileJobStatus status) { return status == VectorStoreBatchFileJobStatus.Completed || status == VectorStoreBatchFileJobStatus.Cancelled || diff --git a/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.Protocol.cs b/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.Protocol.cs index 626694167..0068dfc3d 100644 --- a/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.Protocol.cs +++ b/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.Protocol.cs @@ -15,14 +15,9 @@ public partial class CreateVectorStoreOperation : OperationResult private readonly string _vectorStoreId; - private PollingInterval? _pollingInterval; - /// public override ContinuationToken? RehydrationToken { get; protected set; } - /// - public override bool IsCompleted { get; protected set; } - /// /// [Protocol Method] Retrieves a vector store. /// diff --git a/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.cs b/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.cs index 47aac950d..16e1d334f 100644 --- a/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.cs +++ b/.dotnet/src/Custom/VectorStores/CreateVectorStoreOperation.cs @@ -12,7 +12,7 @@ namespace OpenAI.VectorStores; [Experimental("OPENAI001")] public partial class CreateVectorStoreOperation : OperationResult { - public CreateVectorStoreOperation( + internal CreateVectorStoreOperation( ClientPipeline pipeline, Uri endpoint, ClientResult result) @@ -26,7 +26,7 @@ public CreateVectorStoreOperation( _vectorStoreId = Value.Id; - IsCompleted = GetIsCompleted(Value.Status); + HasCompleted = GetHasCompleted(Value.Status); RehydrationToken = new CreateVectorStoreOperationToken(VectorStoreId); } @@ -97,36 +97,29 @@ public static CreateVectorStoreOperation Rehydrate(VectorStoreClient client, Con } /// - public override async Task WaitForCompletionAsync(CancellationToken cancellationToken = default) + public override async ValueTask UpdateStatusAsync(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = await GetVectorStoreAsync(options).ConfigureAwait(false); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - await _pollingInterval.WaitAsync(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStore value = VectorStore.FromResponse(response); - ClientResult result = await GetVectorStoreAsync(cancellationToken).ConfigureAwait(false); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } - public override void WaitForCompletion(CancellationToken cancellationToken = default) + /// + public override ClientResult UpdateStatus(RequestOptions? options = null) { - _pollingInterval ??= new(); + ClientResult result = GetVectorStore(options); - while (!IsCompleted) - { - PipelineResponse response = GetRawResponse(); - - _pollingInterval.Wait(response, cancellationToken); + PipelineResponse response = result.GetRawResponse(); + VectorStore value = VectorStore.FromResponse(response); - ClientResult result = GetVectorStore(cancellationToken); + ApplyUpdate(response, value); - ApplyUpdate(result); - } + return result; } internal async Task WaitUntilAsync(bool waitUntilCompleted, RequestOptions? options) @@ -143,16 +136,16 @@ internal CreateVectorStoreOperation WaitUntil(bool waitUntilCompleted, RequestOp return this; } - private void ApplyUpdate(ClientResult update) + private void ApplyUpdate(PipelineResponse response, VectorStore value) { - Value = update; - Status = Value.Status; + Value = value; + Status = value.Status; - IsCompleted = GetIsCompleted(Value.Status); - SetRawResponse(update.GetRawResponse()); + HasCompleted = GetHasCompleted(value.Status); + SetRawResponse(response); } - private static bool GetIsCompleted(VectorStoreStatus status) + private static bool GetHasCompleted(VectorStoreStatus status) { return status == VectorStoreStatus.Completed || status == VectorStoreStatus.Expired; diff --git a/.dotnet/src/OpenAI.csproj b/.dotnet/src/OpenAI.csproj index 5318c8fc4..8f5022aed 100644 --- a/.dotnet/src/OpenAI.csproj +++ b/.dotnet/src/OpenAI.csproj @@ -72,6 +72,9 @@ - + + + + diff --git a/.dotnet/src/Utility/PollingInterval.cs b/.dotnet/src/Utility/PollingInterval.cs deleted file mode 100644 index 7f5937367..000000000 --- a/.dotnet/src/Utility/PollingInterval.cs +++ /dev/null @@ -1,70 +0,0 @@ -using System; -using System.ClientModel.Primitives; -using System.Threading; -using System.Threading.Tasks; - -#nullable enable - -namespace OpenAI; - -internal class PollingInterval -{ - private const string RetryAfterHeaderName = "Retry-After"; - private static readonly TimeSpan DefaultDelay = TimeSpan.FromSeconds(0.8); - - private readonly TimeSpan _interval; - - public PollingInterval(TimeSpan? interval = default) - { - _interval = interval ?? DefaultDelay; - } - - public async Task WaitAsync(PipelineResponse response, CancellationToken cancellationToken) - { - TimeSpan delay = GetDelay(response); - - await Task.Delay(delay, cancellationToken); - } - - public void Wait(PipelineResponse response, CancellationToken cancellationToken) - { - TimeSpan delay = GetDelay(response); - - if (!cancellationToken.CanBeCanceled) - { - Thread.Sleep(delay); - return; - } - - if (cancellationToken.WaitHandle.WaitOne(delay)) - { - cancellationToken.ThrowIfCancellationRequested(); - } - } - - private TimeSpan GetDelay(PipelineResponse response) - => TryGetRetryAfter(response, out TimeSpan retryAfter) && retryAfter > _interval ? - retryAfter : _interval; - - private static bool TryGetRetryAfter(PipelineResponse response, out TimeSpan value) - { - // See: https://www.rfc-editor.org/rfc/rfc7231#section-7.1.3 - if (response.Headers.TryGetValue(RetryAfterHeaderName, out string? retryAfter)) - { - if (int.TryParse(retryAfter, out var delaySeconds)) - { - value = TimeSpan.FromSeconds(delaySeconds); - return true; - } - - if (DateTimeOffset.TryParse(retryAfter, out DateTimeOffset retryAfterDate)) - { - value = retryAfterDate - DateTimeOffset.Now; - return true; - } - } - - value = default; - return false; - } -} diff --git a/.dotnet/tests/Assistants/VectorStoreTests.cs b/.dotnet/tests/Assistants/VectorStoreTests.cs index f345a7b0c..774f6aec4 100644 --- a/.dotnet/tests/Assistants/VectorStoreTests.cs +++ b/.dotnet/tests/Assistants/VectorStoreTests.cs @@ -345,8 +345,8 @@ public void CanRehydrateBatchFileJob() Task.Run(() => batchOperation.WaitForCompletion()), Task.Run(() => rehydratedOperation.WaitForCompletion())); - Assert.IsTrue(batchOperation.IsCompleted); - Assert.IsTrue(rehydratedOperation.IsCompleted); + Assert.IsTrue(batchOperation.HasCompleted); + Assert.IsTrue(rehydratedOperation.HasCompleted); Assert.AreEqual(batchOperation.Status, rehydratedOperation.Status); } diff --git a/.dotnet/tests/Batch/BatchTests.cs b/.dotnet/tests/Batch/BatchTests.cs index de8b8c2af..0976da816 100644 --- a/.dotnet/tests/Batch/BatchTests.cs +++ b/.dotnet/tests/Batch/BatchTests.cs @@ -194,11 +194,11 @@ static bool Validate(CreateBatchOperation operation) Assert.IsTrue(Validate(rehydratedOperation)); Task.WaitAll( - IsAsync ? batchOperation.WaitForCompletionAsync() : Task.Run(() => batchOperation.WaitForCompletion()), - IsAsync ? rehydratedOperation.WaitForCompletionAsync() : Task.Run(() => rehydratedOperation.WaitForCompletion())); + IsAsync ? batchOperation.WaitForCompletionAsync().AsTask() : Task.Run(() => batchOperation.WaitForCompletion()), + IsAsync ? rehydratedOperation.WaitForCompletionAsync().AsTask() : Task.Run(() => rehydratedOperation.WaitForCompletion())); - Assert.IsTrue(batchOperation.IsCompleted); - Assert.IsTrue(rehydratedOperation.IsCompleted); + Assert.IsTrue(batchOperation.HasCompleted); + Assert.IsTrue(rehydratedOperation.HasCompleted); } private static BatchClient GetTestClient() => GetTestClient(TestScenario.Batch);