Skip to content
Merged
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 @@ -90,7 +90,7 @@ static string GetCurrentTime()
{
var agents = new List<IHostedAgentBuilder>() { assistantBuilder, reviewerBuilder }.Select(ab => sp.GetRequiredKeyedService<AIAgent>(ab.Name));
return AgentWorkflowBuilder.BuildSequential(workflowName: key, agents: agents);
}).AddAsAIAgent();
}).AddAsAIAgent(includeWorkflowOutputsInResponse: true);

builder.Services.AddOpenAIResponses();
builder.Services.AddOpenAIConversations();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,16 @@ To add DevUI to your ASP.NET Core application:
var agent1Builder = builder.AddAIAgent("workflow-agent1", "You are agent 1.");
var agent2Builder = builder.AddAIAgent("workflow-agent2", "You are agent 2.");
builder.AddSequentialWorkflow("my-workflow", [agent1Builder, agent2Builder])
.AddAsAIAgent();
.AddAsAIAgent(includeWorkflowOutputsInResponse: true);
```

Set `includeWorkflowOutputsInResponse` to `true` to include 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,23 +15,34 @@ 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>
/// <param name="includeWorkflowOutputsInResponse">If <see langword="true"/>, workflow outputs are included in the agent response.</param>
/// <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);
public static IHostedAgentBuilder AddAsAIAgent(
this IHostedWorkflowBuilder builder,
ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool includeWorkflowOutputsInResponse = false)
=> builder.AddAsAIAgent(name: null, lifetime: lifetime, includeWorkflowOutputsInResponse: includeWorkflowOutputsInResponse);

/// <summary>
/// Registers the workflow as an AI agent in the dependency injection container.
/// </summary>
/// <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>
/// <param name="includeWorkflowOutputsInResponse">If <see langword="true"/>, workflow outputs are included in the agent response.</param>
/// <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)
public static IHostedAgentBuilder AddAsAIAgent(
this IHostedWorkflowBuilder builder,
string? name,
ServiceLifetime lifetime = ServiceLifetime.Singleton,
bool includeWorkflowOutputsInResponse = false)
{
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: includeWorkflowOutputsInResponse), 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,48 @@ 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(includeWorkflowOutputsInResponse: true);
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 a workflow registered as an AI agent excludes its output from the response by default.
/// </summary>
[Fact]
public async Task AddAsAIAgent_DefaultExcludesWorkflowOutputFromResponseAsync()
{
// 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.Empty(response.Messages);
}

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