-
Notifications
You must be signed in to change notification settings - Fork 681
.NET: fix: WorkflowAsAgent Sample #1787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lokitoth
wants to merge
3
commits into
main
Choose a base branch
from
dev/dotnet_workflow/fix_workflow_as_agent_sample
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−45
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
dotnet/src/Microsoft.Agents.AI.Workflows/ChatForwardingExecutor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| // Copyright (c) Microsoft. All rights reserved. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
| using Microsoft.Extensions.AI; | ||
|
|
||
| namespace Microsoft.Agents.AI.Workflows; | ||
|
|
||
| /// <summary> | ||
| /// Provides configuration options for <see cref="ChatForwardingExecutor"/>. | ||
| /// </summary> | ||
| public class ChatForwardingExecutorOptions | ||
| { | ||
| /// <summary> | ||
| /// Gets or sets the chat role to use when converting string messages to <see cref="ChatMessage"/> instances. | ||
| /// If set, the executor will accept string messages and convert them to chat messages with this role. | ||
| /// </summary> | ||
| public ChatRole? StringMessageChatRole { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// A ChatProtocol executor that forwards all messages it receives. Useful for splitting inputs into parallel | ||
lokitoth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// processing paths. | ||
| /// </summary> | ||
| /// <remarks>This executor is designed to be cross-run shareable and can be reset to its initial state. It handles | ||
| /// multiple chat-related types, enabling flexible message forwarding scenarios. Thread safety and reusability are | ||
| /// ensured by its design.</remarks> | ||
| /// <param name="id">The unique identifier for the executor instance. Used to distinguish this executor within the system.</param> | ||
| /// <param name="options">Optional configuration settings for the executor. If null, default options are used.</param> | ||
| public sealed class ChatForwardingExecutor(string id, ChatForwardingExecutorOptions? options = null) : Executor(id, declareCrossRunShareable: true), IResettableExecutor | ||
| { | ||
| private readonly ChatRole? _stringMessageChatRole = options?.StringMessageChatRole; | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override RouteBuilder ConfigureRoutes(RouteBuilder routeBuilder) | ||
| { | ||
| if (this._stringMessageChatRole.HasValue) | ||
| { | ||
| routeBuilder = routeBuilder.AddHandler<string>( | ||
| (message, context) => context.SendMessageAsync(new ChatMessage(ChatRole.User, message))); | ||
| } | ||
|
|
||
| return routeBuilder.AddHandler<ChatMessage>(ForwardMessageAsync) | ||
| .AddHandler<IEnumerable<ChatMessage>>(ForwardMessagesAsync) | ||
| .AddHandler<ChatMessage[]>(ForwardMessagesAsync) | ||
| .AddHandler<List<ChatMessage>>(ForwardMessagesAsync) | ||
| .AddHandler<TurnToken>(ForwardTurnTokenAsync); | ||
| } | ||
|
|
||
| private static ValueTask ForwardMessageAsync(ChatMessage message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| => context.SendMessageAsync(message, cancellationToken); | ||
|
|
||
| // Note that this can be used to split a turn into multiple parallel turns taken, which will cause streaming ChatMessages | ||
| // to overlap. | ||
| private static ValueTask ForwardTurnTokenAsync(TurnToken message, IWorkflowContext context, CancellationToken cancellationToken) | ||
| => context.SendMessageAsync(message, cancellationToken); | ||
|
|
||
| // TODO: This is not ideal, but until we have a way of guaranteeing correct routing of interfaces across serialization | ||
| // boundaries, we need to do type unification. It behaves better when used as a handler in ChatProtocolExecutor because | ||
| // it is a strictly contravariant use, whereas this forces invariance on the type because it is directly forwarded. | ||
| private static ValueTask ForwardMessagesAsync(IEnumerable<ChatMessage> messages, IWorkflowContext context, CancellationToken cancellationToken) | ||
| => context.SendMessageAsync(messages is List<ChatMessage> messageList ? messageList : messages.ToList(), cancellationToken); | ||
|
|
||
| private static ValueTask ForwardMessagesAsync(ChatMessage[] messages, IWorkflowContext context, CancellationToken cancellationToken) | ||
| => context.SendMessageAsync(messages, cancellationToken); | ||
|
|
||
| /// <inheritdoc/> | ||
| public ValueTask ResetAsync() => default; | ||
| } | ||
20 changes: 0 additions & 20 deletions
20
dotnet/src/Microsoft.Agents.AI.Workflows/Specialized/ChatForwardingExecutor.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.