Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ To add DevUI to your ASP.NET Core application:
.AddAsAIAgent();
```

`AddAsAIAgent()` includes the workflow's final output in the hosted agent response. This is
required when the workflow is exposed through `MapOpenAIResponses()` or
`MapOpenAIConversations()`; otherwise the output can be visible in streaming workflow events
while the completed response has no output items. If you create an agent directly from a
`Workflow`, opt in with `workflow.AsAIAgent(includeWorkflowOutputsInResponse: true)`.

3. Add OpenAI services and map the endpoints for OpenAI and DevUI:
```csharp
// Register services for OpenAI responses and conversations (also required for DevUI)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public static class HostedWorkflowBuilderExtensions
/// </summary>
/// <param name="builder">The <see cref="IHostedWorkflowBuilder"/> instance to extend.</param>
/// <param name="lifetime">The DI service lifetime for the agent registration. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param>
/// <remarks>Workflow outputs are included in the hosted agent response.</remarks>
/// <returns>An <see cref="IHostedAgentBuilder"/> that can be used to further configure the agent.</returns>
public static IHostedAgentBuilder AddAsAIAgent(this IHostedWorkflowBuilder builder, ServiceLifetime lifetime = ServiceLifetime.Singleton)
=> builder.AddAsAIAgent(name: null, lifetime: lifetime);
Expand All @@ -25,13 +26,16 @@ public static IHostedAgentBuilder AddAsAIAgent(this IHostedWorkflowBuilder build
/// <param name="builder">The <see cref="IHostedWorkflowBuilder"/> instance to extend.</param>
/// <param name="name">The optional name for the AI agent. If not specified, the workflow name is used.</param>
/// <param name="lifetime">The DI service lifetime for the agent registration. Defaults to <see cref="ServiceLifetime.Singleton"/>.</param>
/// <remarks>Workflow outputs are included in the hosted agent response.</remarks>
/// <returns>An <see cref="IHostedAgentBuilder"/> that can be used to further configure the agent.</returns>
public static IHostedAgentBuilder AddAsAIAgent(this IHostedWorkflowBuilder builder, string? name, ServiceLifetime lifetime = ServiceLifetime.Singleton)
{
var workflowName = builder.Name;
var agentName = name ?? workflowName;

return builder.HostApplicationBuilder.AddAIAgent(agentName, (sp, key) =>
sp.GetRequiredKeyedService<Workflow>(workflowName).AsAIAgent(name: key), lifetime);
sp.GetRequiredKeyedService<Workflow>(workflowName).AsAIAgent(
name: key,
includeWorkflowOutputsInResponse: true), lifetime);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Extensions.AI;

namespace Microsoft.Agents.AI.Hosting.UnitTests;

internal static class ChatMessageOutputWorkflow
{
internal static Workflow Build(string name)
{
var output = new OutputExecutor("output");
return new WorkflowBuilder(output)
.WithName(name)
.WithOutputFrom(output)
.Build();
}

private sealed class OutputExecutor(string id) : ChatProtocolExecutor(id)
{
protected override ValueTask TakeTurnAsync(
List<ChatMessage> messages,
IWorkflowContext context,
bool? emitEvents,
CancellationToken cancellationToken = default)
=> context.AddEventAsync(
new WorkflowOutputEvent(new ChatMessage(ChatRole.Assistant, "workflow output"), this.Id),
cancellationToken);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Agents.AI.Workflows;
using Microsoft.Extensions.AI;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Moq;
Expand Down Expand Up @@ -186,6 +188,27 @@ public void AddAsAIAgent_WithoutName_UsesWorkflowName()
Assert.NotNull(agentDescriptor);
}

/// <summary>
/// Verifies that a workflow registered as an AI agent includes its chat-message output in the response.
/// </summary>
[Fact]
public async Task AddAsAIAgent_IncludesWorkflowOutputInResponseAsync()
{
// Arrange
var builder = new HostApplicationBuilder();
const string WorkflowName = "outputWorkflow";
builder.AddWorkflow(WorkflowName, (sp, key) => ChatMessageOutputWorkflow.Build(key))
.AddAsAIAgent();
using var host = builder.Build();
AIAgent agent = host.Services.GetRequiredKeyedService<AIAgent>(WorkflowName);

// Act
AgentResponse response = await agent.RunAsync(new ChatMessage(ChatRole.User, "hello"));

// Assert
Assert.Equal("workflow output", response.Text);
}

/// <summary>
/// Verifies that AddAsAIAgent with a name parameter uses that name instead of the workflow name.
/// </summary>
Expand Down
Loading