Skip to content

A programming framework for agentic AI

feat: Enable Services mapping without passing in HostApplicationBuilder

Jacob Alber235a3bfab38a
diff --git a/dotnet/samples/getting-started/Program.cs b/dotnet/samples/getting-started/Program.cs
index efbb0a14f..8ad2a24fb 100644
--- a/dotnet/samples/getting-started/Program.cs
+++ b/dotnet/samples/getting-started/Program.cs
@@ -1,13 +1,14 @@
-// Copyright (c) Microsoft Corporation. All rights reserved.
+// Copyright (c) Microsoft Corporation. All rights reserved.
// Program.cs
-using TerminationF = System.Func<int, bool>;
-using ModifyF = System.Func<int, int>;
using Microsoft.AutoGen.Core;
-using Microsoft.Extensions.Hosting;
-using Samples;
-using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AutoGen.Contracts;
+using Microsoft.Extensions.DependencyInjection.Extensions;
+
+using Samples;
+
+using TerminationF = System.Func<int, bool>;
+using ModifyF = System.Func<int, int>;
ModifyF modifyFunc = (int x) => x - 1;
TerminationF runUntilFunc = (int x) =>
@@ -15,14 +16,13 @@ TerminationF runUntilFunc = (int x) =>
return x <= 1;
};
-HostApplicationBuilder builder = new HostApplicationBuilder();
-builder.Services.TryAddSingleton(modifyFunc);
-builder.Services.TryAddSingleton(runUntilFunc);
+AgentsAppBuilder appBuilder = new AgentsAppBuilder();
+appBuilder.Services.TryAddSingleton(modifyFunc);
+appBuilder.Services.TryAddSingleton(runUntilFunc);
-AgentsAppBuilder agentApp = new(builder);
-agentApp.AddAgent<Checker>("Checker");
-agentApp.AddAgent<Modifier>("Modifier");
-var app = await agentApp.BuildAsync();
+appBuilder.AddAgent<Checker>("Checker");
+appBuilder.AddAgent<Modifier>("Modifier");
+var app = await appBuilder.BuildAsync();
// Send the initial count to the agents app, running on the `local` runtime, and pass through the registered services via the application `builder`
await app.PublishMessageAsync(new CountMessage
@@ -31,4 +31,4 @@ await app.PublishMessageAsync(new CountMessage
}, new TopicId("default"));
// Run until application shutdown
-await app.WaitForShutdownAsync();
\ No newline at end of file
+await app.WaitForShutdownAsync();
diff --git a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
index 540492e24..e30b5e2ef 100644
--- a/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
+++ b/dotnet/src/Microsoft.AutoGen/Core/AgentsApp.cs
@@ -21,6 +21,8 @@ public class AgentsAppBuilder
this.builder.Services.AddSingleton<IAgentRuntime, InProcessRuntime>();
}
+ public IServiceCollection Services => this.builder.Services;
+
public void AddAgentsFromAssemblies()
{
this.AddAgentsFromAssemblies(AppDomain.CurrentDomain.GetAssemblies());