mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
.NET: Clean up some CancellationToken usage (#996)
* Clean up some CancellationToken usage * Unused usings
This commit is contained in:
committed by
GitHub
Unverified
parent
53eb48968d
commit
4fd61924fc
@@ -44,7 +44,7 @@ internal static partial class Log
|
||||
[LoggerMessage(
|
||||
Level = LogLevel.Warning,
|
||||
Message = "SSE streaming cancelled: RequestId={RequestId}")]
|
||||
public static partial void SseStreamingCancelled(ILogger logger, string requestId);
|
||||
public static partial void SseStreamingCanceled(ILogger logger, string requestId);
|
||||
|
||||
[LoggerMessage(
|
||||
Level = LogLevel.Error,
|
||||
|
||||
+4
-4
@@ -93,14 +93,14 @@ internal sealed class GuessNumberExecutor() : ReflectingExecutor<GuessNumberExec
|
||||
/// Checkpoint the current state of the executor.
|
||||
/// This must be overridden to save any state that is needed to resume the executor.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
context.QueueStateUpdateAsync(StateKey, (this.LowerBound, this.UpperBound));
|
||||
|
||||
/// <summary>
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
(this.LowerBound, this.UpperBound) = await context.ReadStateAsync<(int, int)>(StateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -143,13 +143,13 @@ internal sealed class JudgeExecutor() : ReflectingExecutor<JudgeExecutor>("Judge
|
||||
/// Checkpoint the current state of the executor.
|
||||
/// This must be overridden to save any state that is needed to resume the executor.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
context.QueueStateUpdateAsync(StateKey, this._tries);
|
||||
|
||||
/// <summary>
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
this._tries = await context.ReadStateAsync<int>(StateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
+4
-4
@@ -93,14 +93,14 @@ internal sealed class GuessNumberExecutor() : ReflectingExecutor<GuessNumberExec
|
||||
/// Checkpoint the current state of the executor.
|
||||
/// This must be overridden to save any state that is needed to resume the executor.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
context.QueueStateUpdateAsync(StateKey, (this.LowerBound, this.UpperBound));
|
||||
|
||||
/// <summary>
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
(this.LowerBound, this.UpperBound) = await context.ReadStateAsync<(int, int)>(StateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -143,13 +143,13 @@ internal sealed class JudgeExecutor() : ReflectingExecutor<JudgeExecutor>("Judge
|
||||
/// Checkpoint the current state of the executor.
|
||||
/// This must be overridden to save any state that is needed to resume the executor.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
context.QueueStateUpdateAsync(StateKey, this._tries);
|
||||
|
||||
/// <summary>
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
this._tries = await context.ReadStateAsync<int>(StateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
+2
-2
@@ -93,13 +93,13 @@ internal sealed class JudgeExecutor() : ReflectingExecutor<JudgeExecutor>("Judge
|
||||
/// Checkpoint the current state of the executor.
|
||||
/// This must be overridden to save any state that is needed to resume the executor.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
context.QueueStateUpdateAsync(StateKey, this._tries);
|
||||
|
||||
/// <summary>
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
protected override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
this._tries = await context.ReadStateAsync<int>(StateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ public sealed class A2AHostAgent
|
||||
/// Handle a received message
|
||||
/// </summary>
|
||||
/// <param name="messageSend">The <see cref="MessageSendParams"/> to handle</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to cancel the operation</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
public async Task<Message> OnMessageReceivedAsync(MessageSendParams messageSend, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(messageSend);
|
||||
@@ -92,8 +92,8 @@ public sealed class A2AHostAgent
|
||||
/// Return the <see cref="AgentCard"/> associated with this hosted agent.
|
||||
/// </summary>
|
||||
/// <param name="agentUrl">Current URL for the agent</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to cancel the operation</param>
|
||||
public Task<AgentCard> GetAgentCardAsync(string agentUrl, CancellationToken cancellationToken)
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
public Task<AgentCard> GetAgentCardAsync(string agentUrl, CancellationToken cancellationToken = default)
|
||||
{
|
||||
// Ensure the URL is in the correct format
|
||||
Uri uri = new(agentUrl);
|
||||
|
||||
@@ -38,7 +38,7 @@ public static class A2ACardResolverExtensions
|
||||
/// <param name="resolver">The <see cref="A2ACardResolver" /> to use for the agent creation.</param>
|
||||
/// <param name="httpClient">The <see cref="HttpClient"/> to use for HTTP requests.</param>
|
||||
/// <param name="loggerFactory">The logger factory for enabling logging within the agent.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to use when retrieving the agent card.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>An <see cref="AIAgent"/> instance backed by the A2A agent.</returns>
|
||||
public static async Task<AIAgent> GetAIAgentAsync(this A2ACardResolver resolver, HttpClient? httpClient = null, ILoggerFactory? loggerFactory = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ public abstract class AgentCatalog
|
||||
/// <summary>
|
||||
/// Asynchronously retrieves all registered AI agents from the catalog.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the enumeration operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>
|
||||
/// An asynchronous enumerable of <see cref="AIAgent"/> instances representing all registered agents.
|
||||
/// The enumeration will only include agents that are successfully resolved from the service provider.
|
||||
|
||||
@@ -28,7 +28,7 @@ public abstract class ActorResponseHandle : IDisposable
|
||||
/// <summary>
|
||||
/// Gets the response from the completed request.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to cancel the wait operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task that completes when the request is finished.</returns>
|
||||
public abstract ValueTask<ActorResponse> GetResponseAsync(CancellationToken cancellationToken);
|
||||
|
||||
@@ -41,7 +41,7 @@ public abstract class ActorResponseHandle : IDisposable
|
||||
/// <summary>
|
||||
/// Watches for status and data updates to the request.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to cancel the watch operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>An asynchronous enumerable of request updates.</returns>
|
||||
public abstract IAsyncEnumerable<ActorRequestUpdate> WatchUpdatesAsync(CancellationToken cancellationToken);
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface IActor : IAsyncDisposable
|
||||
/// When the value returned from this method completes, the actor is considered stopped.
|
||||
/// IActor is expected to call IActorContext.WatchMessagesAsync() to receive messages.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to cancel the start operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the start operation.</returns>
|
||||
ValueTask RunAsync(CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ public interface IActorClient
|
||||
/// This method is idempotent: if the request is already in progress, it will return the existing response.
|
||||
/// </summary>
|
||||
/// <param name="request">The request to send to the actor.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the actor response handle.</returns>
|
||||
ValueTask<ActorResponseHandle> SendRequestAsync(ActorRequest request, CancellationToken cancellationToken);
|
||||
|
||||
@@ -24,7 +24,7 @@ public interface IActorClient
|
||||
/// </summary>
|
||||
/// <param name="actorId">The identifier of the actor processing the request.</param>
|
||||
/// <param name="messageId">The unique identifier of the request message.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the actor response handle.</returns>
|
||||
ValueTask<ActorResponseHandle> GetResponseAsync(ActorId actorId, string messageId, CancellationToken cancellationToken);
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ public interface IActorRuntimeContext
|
||||
/// <summary>
|
||||
/// Watches for incoming requests and responses in the actor's inbox and outbox.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A token to cancel the watch operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>An asynchronous enumerable of actor notifications.</returns>
|
||||
IAsyncEnumerable<ActorMessage> WatchMessagesAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -28,7 +28,7 @@ public interface IActorRuntimeContext
|
||||
/// Performs a batch of write operations atomically.
|
||||
/// </summary>
|
||||
/// <param name="operations">The batch of write operations to perform.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the write response.</returns>
|
||||
ValueTask<WriteResponse> WriteAsync(ActorWriteOperationBatch operations, CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -36,7 +36,7 @@ public interface IActorRuntimeContext
|
||||
/// Performs a batch of read operations.
|
||||
/// </summary>
|
||||
/// <param name="operations">The batch of read operations to perform.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the read response.</returns>
|
||||
ValueTask<ReadResponse> ReadAsync(ActorReadOperationBatch operations, CancellationToken cancellationToken = default);
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ public interface IActorStateStorage
|
||||
/// <param name="actorId">The identifier of the actor whose state is being modified.</param>
|
||||
/// <param name="operations">The collection of write operations to perform.</param>
|
||||
/// <param name="etag">The expected ETag for optimistic concurrency control.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the write response with success status and updated ETag.</returns>
|
||||
ValueTask<WriteResponse> WriteStateAsync(ActorId actorId, IReadOnlyCollection<ActorStateWriteOperation> operations, string etag, CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -26,7 +26,7 @@ public interface IActorStateStorage
|
||||
/// </summary>
|
||||
/// <param name="actorId">The identifier of the actor whose state is being read.</param>
|
||||
/// <param name="operations">The collection of read operations to perform.</param>
|
||||
/// <param name="cancellationToken">A token to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A task representing the read response with results and current ETag.</returns>
|
||||
ValueTask<ReadResponse> ReadStateAsync(ActorId actorId, IReadOnlyCollection<ActorStateReadOperation> operations, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -136,7 +136,7 @@ internal sealed class LazyCosmosContainer : IAsyncDisposable
|
||||
HttpStatusCode.RequestTimeout => true, // 408 - Request timeout
|
||||
_ => false
|
||||
},
|
||||
TaskCanceledException or OperationCanceledException or ArgumentException => false,
|
||||
OperationCanceledException or ArgumentException => false,
|
||||
_ => true // Retry other exceptions (network issues, etc.)
|
||||
};
|
||||
|
||||
|
||||
+2
-2
@@ -99,8 +99,8 @@ internal abstract class DeclarativeActionExecutor : Executor<ActionExecutorResul
|
||||
/// Restore the state of the executor from a checkpoint.
|
||||
/// This must be overridden to restore any state that was saved during checkpointing.
|
||||
/// </summary>
|
||||
protected override ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) =>
|
||||
this._state.RestoreAsync(context, cancellation);
|
||||
protected override ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) =>
|
||||
this._state.RestoreAsync(context, cancellationToken);
|
||||
|
||||
protected async ValueTask AssignAsync(PropertyPath? targetPath, FormulaValue result, IWorkflowContext context)
|
||||
{
|
||||
|
||||
@@ -16,14 +16,14 @@ public abstract class WorkflowAgentProvider
|
||||
/// Asynchronously retrieves an AI agent by its unique identifier.
|
||||
/// </summary>
|
||||
/// <param name="agentId">The unique identifier of the AI agent to retrieve. Cannot be null or empty.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The task result contains the <see cref="AIAgent"/> associated.</returns>
|
||||
public abstract Task<AIAgent> GetAgentAsync(string agentId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Asynchronously creates a new conversation and returns its unique identifier.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The conversation identifier</returns>
|
||||
public abstract Task<string> CreateConversationAsync(CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -32,7 +32,7 @@ public abstract class WorkflowAgentProvider
|
||||
/// </summary>
|
||||
/// <param name="conversationId">The identifier of the target conversation.</param>
|
||||
/// <param name="conversationMessage">The message being added.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
public abstract Task CreateMessageAsync(string conversationId, ChatMessage conversationMessage, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
@@ -40,7 +40,7 @@ public abstract class WorkflowAgentProvider
|
||||
/// </summary>
|
||||
/// <param name="conversationId">The identifier of the target conversation.</param>
|
||||
/// <param name="messageId">The identifier of the target message.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The requested message</returns>
|
||||
public abstract Task<ChatMessage> GetMessageAsync(string conversationId, string messageId, CancellationToken cancellationToken = default);
|
||||
|
||||
@@ -52,7 +52,7 @@ public abstract class WorkflowAgentProvider
|
||||
/// <param name="after">A cursor for use in pagination. after is an object ID that defines your place in the list.</param>
|
||||
/// <param name="before">A cursor for use in pagination. before is an object ID that defines your place in the list.</param>
|
||||
/// <param name="newestFirst">Provide records in descending order when true.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The requested messages</returns>
|
||||
public abstract IAsyncEnumerable<ChatMessage> GetMessagesAsync(
|
||||
string conversationId,
|
||||
|
||||
@@ -198,7 +198,7 @@ public static partial class AgentWorkflowBuilder
|
||||
/// </summary>
|
||||
private sealed class OutputMessagesExecutor() : ChatProtocolExecutor("OutputMessages"), IResettableExecutor
|
||||
{
|
||||
protected override ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellation = default)
|
||||
protected override ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellationToken = default)
|
||||
=> context.YieldOutputAsync(messages);
|
||||
|
||||
ValueTask IResettableExecutor.ResetAsync() => this.ResetAsync();
|
||||
@@ -223,7 +223,7 @@ public static partial class AgentWorkflowBuilder
|
||||
/// </summary>
|
||||
private sealed class BatchChatMessagesToListExecutor(string id) : ChatProtocolExecutor(id), IResettableExecutor
|
||||
{
|
||||
protected override ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellation = default)
|
||||
protected override ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellationToken = default)
|
||||
=> context.SendMessageAsync(messages);
|
||||
|
||||
ValueTask IResettableExecutor.ResetAsync() => this.ResetAsync();
|
||||
@@ -623,7 +623,7 @@ public static partial class AgentWorkflowBuilder
|
||||
/// Selects the next agent to participate in the group chat based on the provided chat history and team.
|
||||
/// </summary>
|
||||
/// <param name="history">The chat history to consider.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The next <see cref="AIAgent"/> to speak. This agent must be part of the chat.</returns>
|
||||
protected internal abstract ValueTask<AIAgent> SelectNextAgentAsync(
|
||||
IReadOnlyList<ChatMessage> history,
|
||||
@@ -633,7 +633,7 @@ public static partial class AgentWorkflowBuilder
|
||||
/// Filters the chat history before it's passed to the next agent.
|
||||
/// </summary>
|
||||
/// <param name="history">The chat history to filter.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>The filtered chat history.</returns>
|
||||
protected internal virtual ValueTask<IEnumerable<ChatMessage>> UpdateHistoryAsync(
|
||||
IReadOnlyList<ChatMessage> history,
|
||||
@@ -644,7 +644,7 @@ public static partial class AgentWorkflowBuilder
|
||||
/// Determines whether the group chat should be terminated based on the provided chat history and iteration count.
|
||||
/// </summary>
|
||||
/// <param name="history">The chat history to consider.</param>
|
||||
/// <param name="cancellationToken">A cancellation token that can be used to cancel the operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="bool"/> indicating whether the chat should be terminated.</returns>
|
||||
protected internal virtual ValueTask<bool> ShouldTerminateAsync(
|
||||
IReadOnlyList<ChatMessage> history,
|
||||
|
||||
@@ -35,17 +35,17 @@ public class AggregatingExecutor<TInput, TAggregate>(string id,
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await context.QueueStateUpdateAsync(AggregateStateKey, this._runningAggregate).ConfigureAwait(false);
|
||||
|
||||
await base.OnCheckpointingAsync(context, cancellation).ConfigureAwait(false);
|
||||
await base.OnCheckpointingAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
await base.OnCheckpointRestoredAsync(context, cancellation).ConfigureAwait(false);
|
||||
await base.OnCheckpointRestoredAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
this._runningAggregate = await context.ReadStateAsync<TAggregate>(AggregateStateKey).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
@@ -45,10 +45,10 @@ internal abstract class ChatProtocolExecutor(string id, ChatProtocolExecutorOpti
|
||||
await context.SendMessageAsync(token).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected abstract ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellation = default);
|
||||
protected abstract ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellationToken = default);
|
||||
|
||||
private const string PendingMessagesStateKey = nameof(_pendingMessages);
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Task messagesTask = Task.CompletedTask;
|
||||
if (this._pendingMessages.Count > 0)
|
||||
@@ -60,7 +60,7 @@ internal abstract class ChatProtocolExecutor(string id, ChatProtocolExecutorOpti
|
||||
await messagesTask.ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
JsonElement? messagesValue = await context.ReadStateAsync<JsonElement?>(PendingMessagesStateKey).ConfigureAwait(false);
|
||||
if (messagesValue.HasValue)
|
||||
|
||||
@@ -47,6 +47,6 @@ public class Checkpointed<TRun>
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="ICheckpointingRunner.RestoreCheckpointAsync"/>
|
||||
public ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellation = default)
|
||||
=> this._runner.RestoreCheckpointAsync(checkpointInfo, cancellation);
|
||||
public ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellationToken = default)
|
||||
=> this._runner.RestoreCheckpointAsync(checkpointInfo, cancellationToken);
|
||||
}
|
||||
|
||||
@@ -11,5 +11,5 @@ internal interface ICheckpointingRunner
|
||||
// TODO: Convert this to a multi-timeline (e.g.: Live timeline + forks for orphaned checkpoints due to timetravel)
|
||||
IReadOnlyList<CheckpointInfo> Checkpoints { get; }
|
||||
|
||||
ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellation = default);
|
||||
ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellationToken = default);
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ internal interface ISuperStepRunner
|
||||
|
||||
event EventHandler<WorkflowEvent>? WorkflowEvent;
|
||||
|
||||
ValueTask<bool> RunSuperStepAsync(CancellationToken cancellation);
|
||||
ValueTask<bool> RunSuperStepAsync(CancellationToken cancellationToken);
|
||||
|
||||
ValueTask RequestEndRunAsync();
|
||||
}
|
||||
|
||||
@@ -139,16 +139,16 @@ public abstract class Executor : IIdentified
|
||||
/// </summary>
|
||||
/// <param name="context">The workflow context.</param>
|
||||
/// <returns>A ValueTask representing the asynchronous operation.</returns>
|
||||
/// <param name="cancellation"></param>
|
||||
protected internal virtual ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default) => default;
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
protected internal virtual ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default) => default;
|
||||
|
||||
/// <summary>
|
||||
/// Invoked after a checkpoint is loaded, allowing custom post-load logic in derived classes.
|
||||
/// </summary>
|
||||
/// <param name="context">The workflow context.</param>
|
||||
/// <returns>A ValueTask representing the asynchronous operation.</returns>
|
||||
/// <param name="cancellation"></param>
|
||||
protected internal virtual ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default) => default;
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
protected internal virtual ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default) => default;
|
||||
|
||||
/// <summary>
|
||||
/// A set of <see cref="Type"/>s, representing the messages this executor can handle.
|
||||
|
||||
@@ -21,9 +21,9 @@ public class FunctionExecutor<TInput>(string id,
|
||||
{
|
||||
return RunActionAsync;
|
||||
|
||||
ValueTask RunActionAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellation)
|
||||
ValueTask RunActionAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellationToken)
|
||||
{
|
||||
handlerSync(input, workflowContext, cancellation);
|
||||
handlerSync(input, workflowContext, cancellationToken);
|
||||
return default;
|
||||
}
|
||||
}
|
||||
@@ -57,9 +57,9 @@ public class FunctionExecutor<TInput, TOutput>(string id,
|
||||
{
|
||||
return RunFuncAsync;
|
||||
|
||||
ValueTask<TOutput> RunFuncAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellation)
|
||||
ValueTask<TOutput> RunFuncAsync(TInput input, IWorkflowContext workflowContext, CancellationToken cancellationToken)
|
||||
{
|
||||
TOutput result = handlerSync(input, workflowContext, cancellation);
|
||||
TOutput result = handlerSync(input, workflowContext, cancellationToken);
|
||||
return new ValueTask<TOutput>(result);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
this.WorkflowEvent?.Invoke(this, workflowEvent);
|
||||
}
|
||||
|
||||
public async ValueTask<StreamingRun> ResumeStreamAsync(CheckpointInfo checkpoint, CancellationToken cancellation = default)
|
||||
public async ValueTask<StreamingRun> ResumeStreamAsync(CheckpointInfo checkpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
Throw.IfNull(checkpoint);
|
||||
@@ -117,12 +117,12 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
throw new InvalidOperationException("This runner was not configured with a CheckpointManager, so it cannot restore checkpoints.");
|
||||
}
|
||||
|
||||
await this.RestoreCheckpointAsync(checkpoint, cancellation).ConfigureAwait(false);
|
||||
await this.RestoreCheckpointAsync(checkpoint, cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new StreamingRun(this);
|
||||
}
|
||||
|
||||
public async ValueTask<StreamingRun> StreamAsync(object input, CancellationToken cancellation = default)
|
||||
public async ValueTask<StreamingRun> StreamAsync(object input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
await this.EnqueueMessageAsync(input).ConfigureAwait(false);
|
||||
@@ -130,7 +130,7 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
return new StreamingRun(this);
|
||||
}
|
||||
|
||||
public async ValueTask<StreamingRun> StreamAsync<TInput>(TInput input, CancellationToken cancellation = default)
|
||||
public async ValueTask<StreamingRun> StreamAsync<TInput>(TInput input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
await this.EnqueueMessageAsync(input).ConfigureAwait(false);
|
||||
@@ -138,31 +138,31 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
return new StreamingRun(this);
|
||||
}
|
||||
|
||||
internal async ValueTask<Run> ResumeAsync(CheckpointInfo checkpoint, CancellationToken cancellation = default)
|
||||
internal async ValueTask<Run> ResumeAsync(CheckpointInfo checkpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
StreamingRun streamingRun = await this.ResumeStreamAsync(checkpoint, cancellation).ConfigureAwait(false);
|
||||
cancellation.ThrowIfCancellationRequested();
|
||||
StreamingRun streamingRun = await this.ResumeStreamAsync(checkpoint, cancellationToken).ConfigureAwait(false);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellation).ConfigureAwait(false);
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async ValueTask<Run> RunAsync(object input, CancellationToken cancellation = default)
|
||||
public async ValueTask<Run> RunAsync(object input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
StreamingRun streamingRun = await this.StreamAsync(input, cancellation).ConfigureAwait(false);
|
||||
cancellation.ThrowIfCancellationRequested();
|
||||
StreamingRun streamingRun = await this.StreamAsync(input, cancellationToken).ConfigureAwait(false);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellation).ConfigureAwait(false);
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
public async ValueTask<Run> RunAsync<TInput>(TInput input, CancellationToken cancellation = default)
|
||||
public async ValueTask<Run> RunAsync<TInput>(TInput input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
StreamingRun streamingRun = await this.StreamAsync(input, cancellation).ConfigureAwait(false);
|
||||
cancellation.ThrowIfCancellationRequested();
|
||||
StreamingRun streamingRun = await this.StreamAsync(input, cancellationToken).ConfigureAwait(false);
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellation).ConfigureAwait(false);
|
||||
return await Run.CaptureStreamAsync(streamingRun, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
bool ISuperStepRunner.HasUnservicedRequests => this.RunContext.HasUnservicedRequests;
|
||||
@@ -170,10 +170,10 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
|
||||
public IReadOnlyList<CheckpointInfo> Checkpoints => this._checkpoints;
|
||||
|
||||
async ValueTask<bool> ISuperStepRunner.RunSuperStepAsync(CancellationToken cancellation)
|
||||
async ValueTask<bool> ISuperStepRunner.RunSuperStepAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
if (cancellation.IsCancellationRequested)
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
@@ -239,7 +239,7 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
|
||||
private WorkflowInfo? _workflowInfoCache;
|
||||
private readonly List<CheckpointInfo> _checkpoints = [];
|
||||
internal async ValueTask CheckpointAsync(CancellationToken cancellation = default)
|
||||
internal async ValueTask CheckpointAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
if (this.CheckpointManager is null)
|
||||
@@ -250,7 +250,7 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
}
|
||||
|
||||
// Notify all the executors that they should prepare for checkpointing.
|
||||
Task prepareTask = this.RunContext.PrepareForCheckpointAsync(cancellation);
|
||||
Task prepareTask = this.RunContext.PrepareForCheckpointAsync(cancellationToken);
|
||||
|
||||
// Create a representation of the current workflow if it does not already exist.
|
||||
this._workflowInfoCache ??= this.Workflow.ToWorkflowInfo();
|
||||
@@ -269,7 +269,7 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
this._checkpoints.Add(checkpointInfo);
|
||||
}
|
||||
|
||||
public async ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellation = default)
|
||||
public async ValueTask RestoreCheckpointAsync(CheckpointInfo checkpointInfo, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.RunContext.CheckEnded();
|
||||
Throw.IfNull(checkpointInfo);
|
||||
@@ -291,8 +291,8 @@ internal sealed class InProcessRunner : ISuperStepRunner, ICheckpointingRunner
|
||||
await this.RunContext.StateManager.ImportStateAsync(checkpoint).ConfigureAwait(false);
|
||||
await this.RunContext.ImportStateAsync(checkpoint).ConfigureAwait(false);
|
||||
|
||||
Task executorNotifyTask = this.RunContext.NotifyCheckpointLoadedAsync(cancellation);
|
||||
ValueTask republishRequestsTask = this.RunContext.RepublishUnservicedRequestsAsync(cancellation);
|
||||
Task executorNotifyTask = this.RunContext.NotifyCheckpointLoadedAsync(cancellationToken);
|
||||
ValueTask republishRequestsTask = this.RunContext.RepublishUnservicedRequestsAsync(cancellationToken);
|
||||
|
||||
await this.EdgeMap.ImportStateAsync(checkpoint).ConfigureAwait(false);
|
||||
await Task.WhenAll(executorNotifyTask, republishRequestsTask.AsTask()).ConfigureAwait(false);
|
||||
|
||||
@@ -210,7 +210,7 @@ internal sealed class InProcessRunnerContext : IRunnerContext
|
||||
=> RunnerContext.StateManager.ClearStateAsync(ExecutorId, scopeName);
|
||||
}
|
||||
|
||||
internal Task PrepareForCheckpointAsync(CancellationToken cancellation = default)
|
||||
internal Task PrepareForCheckpointAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.CheckEnded();
|
||||
|
||||
@@ -219,11 +219,11 @@ internal sealed class InProcessRunnerContext : IRunnerContext
|
||||
async Task InvokeCheckpointingAsync(Task<Executor> executorTask)
|
||||
{
|
||||
Executor executor = await executorTask.ConfigureAwait(false);
|
||||
await executor.OnCheckpointingAsync(this.Bind(executor.Id), cancellation).ConfigureAwait(false);
|
||||
await executor.OnCheckpointingAsync(this.Bind(executor.Id), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
internal Task NotifyCheckpointLoadedAsync(CancellationToken cancellation = default)
|
||||
internal Task NotifyCheckpointLoadedAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.CheckEnded();
|
||||
|
||||
@@ -232,7 +232,7 @@ internal sealed class InProcessRunnerContext : IRunnerContext
|
||||
async Task InvokeCheckpointRestoredAsync(Task<Executor> executorTask)
|
||||
{
|
||||
Executor executor = await executorTask.ConfigureAwait(false);
|
||||
await executor.OnCheckpointRestoredAsync(this.Bind(executor.Id), cancellation).ConfigureAwait(false);
|
||||
await executor.OnCheckpointRestoredAsync(this.Bind(executor.Id), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -253,7 +253,7 @@ internal sealed class InProcessRunnerContext : IRunnerContext
|
||||
return new(result);
|
||||
}
|
||||
|
||||
internal async ValueTask RepublishUnservicedRequestsAsync(CancellationToken cancellation = default)
|
||||
internal async ValueTask RepublishUnservicedRequestsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.CheckEnded();
|
||||
|
||||
|
||||
@@ -19,61 +19,61 @@ public static class InProcessExecution
|
||||
where TInput : notnull
|
||||
=> new(checkedWorkflow, checkpointManager, runId, [typeof(TInput)]);
|
||||
|
||||
private static ValueTask<StreamingRun> StreamAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellation = default)
|
||||
=> runner.StreamAsync(input, cancellation);
|
||||
private static ValueTask<StreamingRun> StreamAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellationToken = default)
|
||||
=> runner.StreamAsync(input, cancellationToken);
|
||||
|
||||
private static ValueTask<StreamingRun> StreamAsync(InProcessRunner runner, object input, CancellationToken cancellation = default)
|
||||
=> runner.StreamAsync(input, cancellation);
|
||||
private static ValueTask<StreamingRun> StreamAsync(InProcessRunner runner, object input, CancellationToken cancellationToken = default)
|
||||
=> runner.StreamAsync(input, cancellationToken);
|
||||
|
||||
private static ValueTask<Run> RunAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellation = default)
|
||||
=> runner.RunAsync(input, cancellation);
|
||||
private static ValueTask<Run> RunAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellationToken = default)
|
||||
=> runner.RunAsync(input, cancellationToken);
|
||||
|
||||
private static ValueTask<Run> RunAsync(InProcessRunner runner, object input, CancellationToken cancellation = default)
|
||||
=> runner.RunAsync(input, cancellation);
|
||||
private static ValueTask<Run> RunAsync(InProcessRunner runner, object input, CancellationToken cancellationToken = default)
|
||||
=> runner.RunAsync(input, cancellationToken);
|
||||
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> StreamCheckpointedAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> StreamCheckpointedAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellationToken = default)
|
||||
where TInput : notnull
|
||||
{
|
||||
StreamingRun run = await StreamAsync(runner, input, cancellation).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellation).ConfigureAwait(false);
|
||||
StreamingRun run = await StreamAsync(runner, input, cancellationToken).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> StreamCheckpointedAsync(InProcessRunner runner, object input, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> StreamCheckpointedAsync(InProcessRunner runner, object input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
StreamingRun run = await StreamAsync(runner, input, cancellation).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellation).ConfigureAwait(false);
|
||||
StreamingRun run = await StreamAsync(runner, input, cancellationToken).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
private static async ValueTask<Checkpointed<Run>> RunCheckpointedAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<Run>> RunCheckpointedAsync<TInput>(InProcessRunner runner, TInput input, CancellationToken cancellationToken = default)
|
||||
where TInput : notnull
|
||||
{
|
||||
Run run = await RunAsync(runner, input, cancellation).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellation).ConfigureAwait(false);
|
||||
Run run = await RunAsync(runner, input, cancellationToken).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
private static async ValueTask<Checkpointed<Run>> RunCheckpointedAsync(InProcessRunner runner, object input, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<Run>> RunCheckpointedAsync(InProcessRunner runner, object input, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Run run = await RunAsync(runner, input, cancellation).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellation).ConfigureAwait(false);
|
||||
Run run = await RunAsync(runner, input, cancellationToken).ConfigureAwait(false);
|
||||
await runner.CheckpointAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> ResumeStreamCheckpointedAsync(InProcessRunner runner, CheckpointInfo fromCheckpoint, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<StreamingRun>> ResumeStreamCheckpointedAsync(InProcessRunner runner, CheckpointInfo fromCheckpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
StreamingRun run = await runner.ResumeStreamAsync(fromCheckpoint, cancellation).ConfigureAwait(false);
|
||||
StreamingRun run = await runner.ResumeStreamAsync(fromCheckpoint, cancellationToken).ConfigureAwait(false);
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
private static async ValueTask<Checkpointed<Run>> ResumeRunCheckpointedAsync(InProcessRunner runner, CheckpointInfo fromCheckpoint, CancellationToken cancellation = default)
|
||||
private static async ValueTask<Checkpointed<Run>> ResumeRunCheckpointedAsync(InProcessRunner runner, CheckpointInfo fromCheckpoint, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Run run = await runner.ResumeAsync(fromCheckpoint, cancellation).ConfigureAwait(false);
|
||||
Run run = await runner.ResumeAsync(fromCheckpoint, cancellationToken).ConfigureAwait(false);
|
||||
return new(run, runner);
|
||||
}
|
||||
|
||||
@@ -87,17 +87,17 @@ public static class InProcessExecution
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="input">The input message to be processed as part of the streaming run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{StreamingRun}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="StreamingRun"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<StreamingRun> StreamAsync<TInput>(
|
||||
Workflow workflow,
|
||||
TInput input,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: null, runId);
|
||||
return StreamAsync(runner, (object)input, cancellation);
|
||||
return StreamAsync(runner, (object)input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -110,17 +110,17 @@ public static class InProcessExecution
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="input">The input message to be processed as part of the streaming run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{StreamingRun}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="StreamingRun"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<StreamingRun> StreamAsync<TInput>(
|
||||
Workflow<TInput> workflow,
|
||||
TInput input,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: null, runId);
|
||||
return StreamAsync(runner, input, cancellation);
|
||||
return StreamAsync(runner, input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -134,7 +134,7 @@ public static class InProcessExecution
|
||||
/// <param name="input">The input message to be processed as part of the streaming run.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{StreamingRun}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="StreamingRun"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<StreamingRun>> StreamAsync<TInput>(
|
||||
@@ -142,10 +142,10 @@ public static class InProcessExecution
|
||||
TInput input,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return StreamCheckpointedAsync(runner, (object)input, cancellation);
|
||||
return StreamCheckpointedAsync(runner, (object)input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -159,7 +159,7 @@ public static class InProcessExecution
|
||||
/// <param name="input">The input message to be processed as part of the streaming run.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{StreamingRun}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="StreamingRun"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<StreamingRun>> StreamAsync<TInput>(
|
||||
@@ -167,55 +167,55 @@ public static class InProcessExecution
|
||||
TInput input,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return StreamCheckpointedAsync(runner, input, cancellation);
|
||||
return StreamCheckpointedAsync(runner, input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resumes an asynchronous streaming execution for the specified input from a checkpoint.
|
||||
/// </summary>
|
||||
/// <remarks>If the operation is cancelled via the <paramref name="cancellation"/> token, the streaming execution will
|
||||
/// <remarks>If the operation is cancelled via the <paramref name="cancellationToken"/> token, the streaming execution will
|
||||
/// be terminated.</remarks>
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="fromCheckpoint">The <see cref="CheckpointInfo"/> corresponding to the checkpoint from which to resume.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="StreamingRun"/> that provides access to the results of the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<StreamingRun>> ResumeStreamAsync(
|
||||
Workflow workflow,
|
||||
CheckpointInfo fromCheckpoint,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default)
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return ResumeStreamCheckpointedAsync(runner, fromCheckpoint, cancellation);
|
||||
return ResumeStreamCheckpointedAsync(runner, fromCheckpoint, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resumes an asynchronous streaming execution for the specified input from a checkpoint.
|
||||
/// </summary>
|
||||
/// <remarks>If the operation is cancelled via the <paramref name="cancellation"/> token, the streaming execution will
|
||||
/// <remarks>If the operation is cancelled via the <paramref name="cancellationToken"/> token, the streaming execution will
|
||||
/// be terminated.</remarks>
|
||||
/// <typeparam name="TInput">The type of input accepted by the workflow. Must be non-nullable.</typeparam>
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="fromCheckpoint">The <see cref="CheckpointInfo"/> corresponding to the checkpoint from which to resume.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="StreamingRun"/> that provides access to the results of the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<StreamingRun>> ResumeStreamAsync<TInput>(
|
||||
Workflow<TInput> workflow,
|
||||
CheckpointInfo fromCheckpoint,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return ResumeStreamCheckpointedAsync(runner, fromCheckpoint, cancellation);
|
||||
return ResumeStreamCheckpointedAsync(runner, fromCheckpoint, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -227,17 +227,17 @@ public static class InProcessExecution
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="input">The input message to be processed as part of the run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Run> RunAsync<TInput>(
|
||||
Workflow workflow,
|
||||
TInput input,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: null, runId);
|
||||
return RunAsync(runner, (object)input, cancellation);
|
||||
return RunAsync(runner, (object)input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -249,17 +249,17 @@ public static class InProcessExecution
|
||||
/// <param name="workflow">The workflow to be executed. Must not be <c>null</c>.</param>
|
||||
/// <param name="input">The input message to be processed as part of the run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Run> RunAsync<TInput>(
|
||||
Workflow<TInput> workflow,
|
||||
TInput input,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: null, runId);
|
||||
return RunAsync(runner, input, cancellation);
|
||||
return RunAsync(runner, input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -272,7 +272,7 @@ public static class InProcessExecution
|
||||
/// <param name="input">The input message to be processed as part of the run.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<Run>> RunAsync<TInput>(
|
||||
@@ -280,10 +280,10 @@ public static class InProcessExecution
|
||||
TInput input,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: checkpointManager, runId);
|
||||
return RunCheckpointedAsync(runner, (object)input, cancellation);
|
||||
return RunCheckpointedAsync(runner, (object)input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -296,7 +296,7 @@ public static class InProcessExecution
|
||||
/// <param name="input">The input message to be processed as part of the run.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<Run>> RunAsync<TInput>(
|
||||
@@ -304,10 +304,10 @@ public static class InProcessExecution
|
||||
TInput input,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager: checkpointManager, runId);
|
||||
return RunCheckpointedAsync(runner, input, cancellation);
|
||||
return RunCheckpointedAsync(runner, input, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -319,7 +319,7 @@ public static class InProcessExecution
|
||||
/// <param name="fromCheckpoint">The <see cref="CheckpointInfo"/> corresponding to the checkpoint from which to resume.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<Run>> ResumeAsync(
|
||||
@@ -327,10 +327,10 @@ public static class InProcessExecution
|
||||
CheckpointInfo fromCheckpoint,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default)
|
||||
CancellationToken cancellationToken = default)
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return ResumeRunCheckpointedAsync(runner, fromCheckpoint, cancellation);
|
||||
return ResumeRunCheckpointedAsync(runner, fromCheckpoint, cancellationToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -342,7 +342,7 @@ public static class InProcessExecution
|
||||
/// <param name="fromCheckpoint">The <see cref="CheckpointInfo"/> corresponding to the checkpoint from which to resume.</param>
|
||||
/// <param name="checkpointManager">The <see cref="CheckpointManager"/> to use with this run.</param>
|
||||
/// <param name="runId">An optional unique identifier for the run. If not provided, a new identifier will be generated.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask{Run}"/> that represents the asynchronous operation. The result contains a <see
|
||||
/// cref="Run"/> for managing and interacting with the streaming run.</returns>
|
||||
public static ValueTask<Checkpointed<Run>> ResumeAsync<TInput>(
|
||||
@@ -350,9 +350,9 @@ public static class InProcessExecution
|
||||
CheckpointInfo fromCheckpoint,
|
||||
CheckpointManager checkpointManager,
|
||||
string? runId = null,
|
||||
CancellationToken cancellation = default) where TInput : notnull
|
||||
CancellationToken cancellationToken = default) where TInput : notnull
|
||||
{
|
||||
InProcessRunner runner = CreateRunner(workflow, checkpointManager, runId);
|
||||
return ResumeRunCheckpointedAsync(runner, fromCheckpoint, cancellation);
|
||||
return ResumeRunCheckpointedAsync(runner, fromCheckpoint, cancellationToken);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,10 +40,10 @@ public enum RunStatus
|
||||
/// </summary>
|
||||
public sealed class Run
|
||||
{
|
||||
internal static async ValueTask<Run> CaptureStreamAsync(StreamingRun run, CancellationToken cancellation = default)
|
||||
internal static async ValueTask<Run> CaptureStreamAsync(StreamingRun run, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Run result = new(run);
|
||||
await result.RunToNextHaltAsync(cancellation).ConfigureAwait(false);
|
||||
await result.RunToNextHaltAsync(cancellationToken).ConfigureAwait(false);
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -54,11 +54,11 @@ public sealed class Run
|
||||
this._streamingRun = streamingRun;
|
||||
}
|
||||
|
||||
internal async ValueTask<bool> RunToNextHaltAsync(CancellationToken cancellation = default)
|
||||
internal async ValueTask<bool> RunToNextHaltAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
bool hadEvents = false;
|
||||
this.Status = RunStatus.Running;
|
||||
await foreach (WorkflowEvent evt in this._streamingRun.WatchStreamAsync(blockOnPendingRequest: false, cancellation).ConfigureAwait(false))
|
||||
await foreach (WorkflowEvent evt in this._streamingRun.WatchStreamAsync(blockOnPendingRequest: false, cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
hadEvents = true;
|
||||
this._eventSink.Add(evt);
|
||||
@@ -113,31 +113,31 @@ public sealed class Run
|
||||
/// <summary>
|
||||
/// Resume execution of the workflow with the provided external responses.
|
||||
/// </summary>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the workflow execution.</param>
|
||||
/// <param name="responses">An array of <see cref="ExternalResponse"/> objects to send to the workflow.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns><c>true</c> if the workflow had any output events, <c>false</c> otherwise.</returns>
|
||||
public async ValueTask<bool> ResumeAsync(CancellationToken cancellation = default, params IEnumerable<ExternalResponse> responses)
|
||||
public async ValueTask<bool> ResumeAsync(IEnumerable<ExternalResponse> responses, CancellationToken cancellationToken = default)
|
||||
{
|
||||
foreach (ExternalResponse response in responses)
|
||||
{
|
||||
await this._streamingRun.SendResponseAsync(response).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return await this.RunToNextHaltAsync(cancellation).ConfigureAwait(false);
|
||||
return await this.RunToNextHaltAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resume execution of the workflow with the provided external responses.
|
||||
/// </summary>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the workflow execution.</param>
|
||||
/// <param name="messages">An array of messages to send to the workflow. Messages will only be sent if they are valid
|
||||
/// input types to the starting executor or a <see cref="ExternalResponse"/>.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns><c>true</c> if the workflow had any output events, <c>false</c> otherwise.</returns>
|
||||
public async ValueTask<bool> ResumeAsync<T>(CancellationToken cancellation = default, params IEnumerable<T> messages)
|
||||
public async ValueTask<bool> ResumeAsync<T>(IEnumerable<T> messages, CancellationToken cancellationToken = default)
|
||||
{
|
||||
if (messages is IEnumerable<ExternalResponse> responses)
|
||||
{
|
||||
return await this.ResumeAsync(cancellation, responses).ConfigureAwait(false);
|
||||
return await this.ResumeAsync(responses, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
foreach (T message in messages)
|
||||
@@ -145,7 +145,7 @@ public sealed class Run
|
||||
await this._streamingRun.TrySendMessageAsync(message).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return await this.RunToNextHaltAsync(cancellation).ConfigureAwait(false);
|
||||
return await this.RunToNextHaltAsync(cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="StreamingRun.EndRunAsync"/>
|
||||
|
||||
@@ -24,7 +24,7 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
|
||||
this._thread ??= this._agent.GetNewThread();
|
||||
|
||||
private const string ThreadStateKey = nameof(_thread);
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Task threadTask = Task.CompletedTask;
|
||||
if (this._thread is not null)
|
||||
@@ -33,12 +33,12 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
|
||||
threadTask = context.QueueStateUpdateAsync(ThreadStateKey, threadValue).AsTask();
|
||||
}
|
||||
|
||||
Task baseTask = base.OnCheckpointingAsync(context, cancellation).AsTask();
|
||||
Task baseTask = base.OnCheckpointingAsync(context, cancellationToken).AsTask();
|
||||
|
||||
await Task.WhenAll(threadTask, baseTask).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
JsonElement? threadValue = await context.ReadStateAsync<JsonElement?>(ThreadStateKey).ConfigureAwait(false);
|
||||
if (threadValue.HasValue)
|
||||
@@ -46,13 +46,13 @@ internal sealed class AIAgentHostExecutor : ChatProtocolExecutor
|
||||
this._thread = this._agent.DeserializeThread(threadValue.Value);
|
||||
}
|
||||
|
||||
await base.OnCheckpointRestoredAsync(context, cancellation).ConfigureAwait(false);
|
||||
await base.OnCheckpointRestoredAsync(context, cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
protected override async ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellation = default)
|
||||
protected override async ValueTask TakeTurnAsync(List<ChatMessage> messages, IWorkflowContext context, bool? emitEvents, CancellationToken cancellationToken = default)
|
||||
{
|
||||
emitEvents ??= this._emitEvents;
|
||||
IAsyncEnumerable<AgentRunResponseUpdate> agentStream = this._agent.RunStreamingAsync(messages, this.EnsureThread(context), cancellationToken: cancellation);
|
||||
IAsyncEnumerable<AgentRunResponseUpdate> agentStream = this._agent.RunStreamingAsync(messages, this.EnsureThread(context), cancellationToken: cancellationToken);
|
||||
|
||||
List<AIContent> updates = [];
|
||||
ChatMessage? currentStreamingMessage = null;
|
||||
|
||||
@@ -79,17 +79,17 @@ public sealed class StreamingRun
|
||||
/// <remarks>This method yields <see cref="WorkflowEvent"/> instances in real time as the workflow
|
||||
/// progresses. The stream completes when a <see cref="RequestHaltEvent"/> is encountered. Events are
|
||||
/// delivered in the order they are raised.</remarks>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation. If cancellation is
|
||||
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the streaming operation. If cancellation is
|
||||
/// requested, the stream will end and no further events will be yielded.</param>
|
||||
/// <returns>An asynchronous stream of <see cref="WorkflowEvent"/> objects representing significant workflow state changes.
|
||||
/// The stream ends when the workflow completes or when cancellation is requested.</returns>
|
||||
public IAsyncEnumerable<WorkflowEvent> WatchStreamAsync(
|
||||
CancellationToken cancellation = default)
|
||||
=> this.WatchStreamAsync(blockOnPendingRequest: true, cancellation);
|
||||
CancellationToken cancellationToken = default)
|
||||
=> this.WatchStreamAsync(blockOnPendingRequest: true, cancellationToken);
|
||||
|
||||
internal async IAsyncEnumerable<WorkflowEvent> WatchStreamAsync(
|
||||
bool blockOnPendingRequest,
|
||||
[EnumeratorCancellation] CancellationToken cancellation = default)
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
List<WorkflowEvent> eventSink = [];
|
||||
|
||||
@@ -100,8 +100,8 @@ public sealed class StreamingRun
|
||||
do
|
||||
{
|
||||
// Drain SuperSteps while there are steps to run
|
||||
await this._stepRunner.RunSuperStepAsync(cancellation).ConfigureAwait(false);
|
||||
if (cancellation.IsCancellationRequested)
|
||||
await this._stepRunner.RunSuperStepAsync(cancellationToken).ConfigureAwait(false);
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break; // Exit if cancellation is requested
|
||||
}
|
||||
@@ -109,7 +109,7 @@ public sealed class StreamingRun
|
||||
bool hadCompletionEvent = false;
|
||||
foreach (WorkflowEvent raisedEvent in Interlocked.Exchange(ref eventSink, []))
|
||||
{
|
||||
if (cancellation.IsCancellationRequested)
|
||||
if (cancellationToken.IsCancellationRequested)
|
||||
{
|
||||
yield break; // Exit if cancellation is requested
|
||||
}
|
||||
@@ -140,7 +140,7 @@ public sealed class StreamingRun
|
||||
{
|
||||
this._waitForResponseSource ??= new();
|
||||
|
||||
using CancellationTokenRegistration registration = cancellation.Register(() => this._waitForResponseSource?.SetResult(new()));
|
||||
using CancellationTokenRegistration registration = cancellationToken.Register(() => this._waitForResponseSource?.SetResult(new()));
|
||||
|
||||
await this._waitForResponseSource.Task.ConfigureAwait(false);
|
||||
this._waitForResponseSource = null;
|
||||
@@ -182,14 +182,14 @@ public static class StreamingRunExtensions
|
||||
/// <param name="eventCallback">An optional callback function invoked for each <see cref="WorkflowEvent"/> received from the stream.
|
||||
/// The callback can return a response object to be sent back to the workflow, or <see langword="null"/> if no response
|
||||
/// is required.</param>
|
||||
/// <param name="cancellation">A <see cref="CancellationToken"/> to observe while waiting for events. </param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A <see cref="ValueTask"/> that represents the asynchronous operation. The task completes when the workflow
|
||||
/// execution stream is fully processed.</returns>
|
||||
public static async ValueTask RunToCompletionAsync(this StreamingRun handle, Func<WorkflowEvent, ExternalResponse?>? eventCallback = null, CancellationToken cancellation = default)
|
||||
public static async ValueTask RunToCompletionAsync(this StreamingRun handle, Func<WorkflowEvent, ExternalResponse?>? eventCallback = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
Throw.IfNull(handle);
|
||||
|
||||
await foreach (WorkflowEvent @event in handle.WatchStreamAsync(cancellation).ConfigureAwait(false))
|
||||
await foreach (WorkflowEvent @event in handle.WatchStreamAsync(cancellationToken).ConfigureAwait(false))
|
||||
{
|
||||
ExternalResponse? maybeResponse = eventCallback?.Invoke(@event);
|
||||
if (maybeResponse is not null)
|
||||
|
||||
@@ -53,7 +53,7 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
private async
|
||||
IAsyncEnumerable<AgentRunResponseUpdate> InvokeStageAsync(
|
||||
WorkflowThread conversation,
|
||||
[EnumeratorCancellation] CancellationToken cancellation = default)
|
||||
[EnumeratorCancellation] CancellationToken cancellationToken = default)
|
||||
{
|
||||
string runId = conversation.RunId;
|
||||
List<ChatMessage> messages = conversation.MessageStore.GetFromBookmark().ToList();
|
||||
@@ -64,7 +64,7 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
// in the case of new threads.
|
||||
if (!this._runningWorkflows.TryGetValue(runId, out StreamingRun? run))
|
||||
{
|
||||
run = await InProcessExecution.StreamAsync(this._workflow, messages, cancellation: cancellation)
|
||||
run = await InProcessExecution.StreamAsync(this._workflow, messages, cancellationToken: cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
this._runningWorkflows[runId] = run;
|
||||
}
|
||||
@@ -75,9 +75,9 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
}
|
||||
|
||||
await run.TrySendMessageAsync(new TurnToken(emitEvents: true)).ConfigureAwait(false);
|
||||
await foreach (WorkflowEvent evt in run.WatchStreamAsync(blockOnPendingRequest: false, cancellation)
|
||||
await foreach (WorkflowEvent evt in run.WatchStreamAsync(blockOnPendingRequest: false, cancellationToken)
|
||||
.ConfigureAwait(false)
|
||||
.WithCancellation(cancellation))
|
||||
.WithCancellation(cancellationToken))
|
||||
{
|
||||
switch (evt)
|
||||
{
|
||||
@@ -99,7 +99,7 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
}
|
||||
}
|
||||
|
||||
private async ValueTask<WorkflowThread> UpdateThreadAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, CancellationToken cancellation = default)
|
||||
private async ValueTask<WorkflowThread> UpdateThreadAsync(IEnumerable<ChatMessage> messages, AgentThread? thread = null, CancellationToken cancellationToken = default)
|
||||
{
|
||||
thread ??= this.GetNewThread();
|
||||
|
||||
@@ -108,7 +108,7 @@ internal sealed class WorkflowHostAgent : AIAgent
|
||||
throw new ArgumentException($"Incompatible thread type: {thread.GetType()} (expecting {typeof(WorkflowThread)})", nameof(thread));
|
||||
}
|
||||
|
||||
await workflowThread.MessageStore.AddMessagesAsync(messages, cancellation).ConfigureAwait(false);
|
||||
await workflowThread.MessageStore.AddMessagesAsync(messages, cancellationToken).ConfigureAwait(false);
|
||||
return workflowThread;
|
||||
}
|
||||
|
||||
|
||||
@@ -467,7 +467,7 @@ public sealed class ChatClientAgent : AIAgent
|
||||
/// <param name="thread">The conversation thread to use or create.</param>
|
||||
/// <param name="inputMessages">The input messages to use.</param>
|
||||
/// <param name="runOptions">Optional parameters for agent invocation.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
|
||||
/// <returns>A tuple containing the thread, chat options, and thread messages.</returns>
|
||||
private async Task<(ChatClientAgentThread AgentThread, ChatOptions? ChatOptions, List<ChatMessage> ThreadMessages)> PrepareThreadAndMessagesAsync(
|
||||
AgentThread? thread,
|
||||
|
||||
@@ -68,7 +68,7 @@ public class RepresentationTests
|
||||
testsRun++;
|
||||
}
|
||||
|
||||
async ValueTask MessageHandlerAsync(int message, IWorkflowContext workflowContext, CancellationToken cancellation = default)
|
||||
async ValueTask MessageHandlerAsync(int message, IWorkflowContext workflowContext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -113,12 +113,12 @@ internal sealed class JudgeExecutor : ReflectingExecutor<JudgeExecutor>, IMessag
|
||||
NumberSignal.Above;
|
||||
}
|
||||
|
||||
protected internal override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override ValueTask OnCheckpointingAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return context.QueueStateUpdateAsync("TryCount", this.Tries);
|
||||
}
|
||||
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellation = default)
|
||||
protected internal override async ValueTask OnCheckpointRestoredAsync(IWorkflowContext context, CancellationToken cancellationToken = default)
|
||||
{
|
||||
this.Tries = await context.ReadStateAsync<int?>("TryCount").ConfigureAwait(false) ?? 0;
|
||||
}
|
||||
|
||||
+1
-1
@@ -41,7 +41,7 @@ internal static class Step5EntryPoint
|
||||
{
|
||||
await handle.EndRunAsync().ConfigureAwait(false);
|
||||
|
||||
checkpointed = await InProcessExecution.ResumeStreamAsync(workflow, targetCheckpoint, checkpointManager, runId: handle.RunId, cancellation: CancellationToken.None)
|
||||
checkpointed = await InProcessExecution.ResumeStreamAsync(workflow, targetCheckpoint, checkpointManager, runId: handle.RunId, cancellationToken: CancellationToken.None)
|
||||
.ConfigureAwait(false);
|
||||
handle = checkpointed.Run;
|
||||
}
|
||||
|
||||
@@ -25,12 +25,12 @@ internal abstract class TestingExecutor<TIn, TOut> : Executor, IDisposable
|
||||
this._actions = actions;
|
||||
}
|
||||
|
||||
public void UnlinkCancellation(CancellationToken token) =>
|
||||
this._linkedTokens.Remove(token);
|
||||
public void UnlinkCancellation(CancellationToken cancellationToken) =>
|
||||
this._linkedTokens.Remove(cancellationToken);
|
||||
|
||||
public void LinkCancellation(CancellationToken token)
|
||||
public void LinkCancellation(CancellationToken cancellationToken)
|
||||
{
|
||||
this._linkedTokens.Add(token);
|
||||
this._linkedTokens.Add(cancellationToken);
|
||||
CancellationTokenSource tokenSource = CancellationTokenSource.CreateLinkedTokenSource(this._linkedTokens.ToArray());
|
||||
tokenSource = Interlocked.Exchange(ref this._internalCts, tokenSource);
|
||||
tokenSource.Dispose();
|
||||
|
||||
Reference in New Issue
Block a user