mirror of
https://github.com/microsoft/agent-framework.git
synced 2026-06-16 21:04:09 +08:00
* fix: Add session support for Handoff-hosted Agents In order to better support using `Workflows` hosted as `AIAgents` inside of Handoff workflows, we need to make proper use of AgentSession. This causes potential issues around checkpointing and making sure that we properly compute only the new incoming messages for each agent invocation. * fix: AgentSession checkpointing using AIAgent's Serialize/Deserialize methods We cannot rely on implicit serialization through `HandoffHostState` because we are missing type information. * fix: Thread safety issue in `MultiPartyConversation.AllMessages` * fix: Enable unwrapping of FunctionResultContent when ExternalRequest was wrapped into FunctionCallContent
47 lines
2.1 KiB
C#
47 lines
2.1 KiB
C#
// Copyright (c) Microsoft. All rights reserved.
|
|
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Extensions.AI;
|
|
|
|
namespace Microsoft.Agents.AI.Workflows.Specialized;
|
|
|
|
/// <summary>Executor used at the end of a handoff workflow to raise a final completed event.</summary>
|
|
internal sealed class HandoffEndExecutor(bool returnToPrevious) : Executor(ExecutorId, declareCrossRunShareable: true), IResettableExecutor
|
|
{
|
|
public const string ExecutorId = "HandoffEnd";
|
|
|
|
private readonly StateRef<HandoffSharedState> _sharedStateRef = new(HandoffConstants.HandoffSharedStateKey,
|
|
HandoffConstants.HandoffSharedStateScope);
|
|
|
|
protected override ProtocolBuilder ConfigureProtocol(ProtocolBuilder protocolBuilder) =>
|
|
protocolBuilder.ConfigureRoutes(routeBuilder => routeBuilder.AddHandler<HandoffState>(
|
|
(handoff, context, cancellationToken) => this.HandleAsync(handoff, context, cancellationToken)))
|
|
.YieldsOutput<List<ChatMessage>>();
|
|
|
|
private async ValueTask HandleAsync(HandoffState handoff, IWorkflowContext context, CancellationToken cancellationToken)
|
|
{
|
|
await this._sharedStateRef.InvokeWithStateAsync(
|
|
async (HandoffSharedState? sharedState, IWorkflowContext context, CancellationToken cancellationToken) =>
|
|
{
|
|
if (sharedState == null)
|
|
{
|
|
throw new InvalidOperationException("Handoff Orchestration shared state was not properly initialized.");
|
|
}
|
|
|
|
if (returnToPrevious)
|
|
{
|
|
sharedState.PreviousAgentId = handoff.PreviousAgentId;
|
|
}
|
|
|
|
await context.YieldOutputAsync(sharedState.Conversation.CloneAllMessages(), cancellationToken).ConfigureAwait(false);
|
|
|
|
return sharedState;
|
|
}, context, cancellationToken).ConfigureAwait(false);
|
|
}
|
|
|
|
public ValueTask ResetAsync() => default;
|
|
}
|