mirror of
https://github.com/microsoft/autogen.git
synced 2025-10-02 19:47:19 +00:00
getting started sample
This commit is contained in:
parent
d48b54fcaf
commit
a4d8d571b0
@ -142,6 +142,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Core.Grpc
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Tests.Shared", "test\Microsoft.AutoGen.Tests.Shared\Microsoft.AutoGen.Tests.Shared.csproj", "{58AD8E1D-83BD-4950-A324-1A20677D78D9}"
|
||||
EndProject
|
||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "samples", "samples", "{CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "getting-started", "samples\getting-started\getting-started.csproj", "{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
CoreOnly|Any CPU = CoreOnly|Any CPU
|
||||
@ -433,6 +437,12 @@ Global
|
||||
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{58AD8E1D-83BD-4950-A324-1A20677D78D9}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.CoreOnly|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.CoreOnly|Any CPU.Build.0 = Debug|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@ -498,6 +508,7 @@ Global
|
||||
{599E1971-1DA9-453F-A7A8-42510BBC95C2} = {05B9C173-6441-4DCA-9AC4-E897EF75F331}
|
||||
{33A28A4B-123B-4416-9631-0F759B8D6172} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
|
||||
{58AD8E1D-83BD-4950-A324-1A20677D78D9} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
|
||||
{70A8D4B5-D0A6-4098-A6F3-6ED274B65E7D} = {CE0AA8D5-12B8-4628-9589-DAD8CB0DDCF6}
|
||||
EndGlobalSection
|
||||
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
|
||||
|
34
dotnet/samples/getting-started/Checker.cs
Normal file
34
dotnet/samples/getting-started/Checker.cs
Normal file
@ -0,0 +1,34 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Checker.cs
|
||||
|
||||
using Microsoft.AutoGen.Contracts;
|
||||
using Microsoft.AutoGen.Core;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using TerminationF = System.Func<int, bool>;
|
||||
|
||||
namespace Samples;
|
||||
|
||||
[TypeSubscription("default")]
|
||||
public class Checker(
|
||||
AgentId id,
|
||||
IAgentRuntime runtime,
|
||||
IHostApplicationLifetime hostApplicationLifetime,
|
||||
TerminationF runUntilFunc
|
||||
) :
|
||||
BaseAgent(id, runtime, "Modifier", null),
|
||||
IHandle<CountUpdate>
|
||||
{
|
||||
public async ValueTask HandleAsync(CountUpdate item, MessageContext messageContext)
|
||||
{
|
||||
if (!runUntilFunc(item.NewCount))
|
||||
{
|
||||
Console.WriteLine($"\nChecker:\n{item.NewCount} passed the check, continue.");
|
||||
await this.PublishMessageAsync(new CountMessage { Content = item.NewCount }, new TopicId("default"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine($"\nChecker:\n{item.NewCount} failed the check, stopping.");
|
||||
hostApplicationLifetime.StopApplication();
|
||||
}
|
||||
}
|
||||
}
|
9
dotnet/samples/getting-started/CountMessage.cs
Normal file
9
dotnet/samples/getting-started/CountMessage.cs
Normal file
@ -0,0 +1,9 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// CountMessage.cs
|
||||
|
||||
namespace Samples;
|
||||
|
||||
public class CountMessage
|
||||
{
|
||||
public int Content { get; set; }
|
||||
}
|
9
dotnet/samples/getting-started/CountUpdate.cs
Normal file
9
dotnet/samples/getting-started/CountUpdate.cs
Normal file
@ -0,0 +1,9 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// CountUpdate.cs
|
||||
|
||||
namespace Samples;
|
||||
|
||||
public class CountUpdate
|
||||
{
|
||||
public int NewCount { get; set; }
|
||||
}
|
29
dotnet/samples/getting-started/Modifier.cs
Normal file
29
dotnet/samples/getting-started/Modifier.cs
Normal file
@ -0,0 +1,29 @@
|
||||
// Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
// Modifier.cs
|
||||
|
||||
using Microsoft.AutoGen.Contracts;
|
||||
using Microsoft.AutoGen.Core;
|
||||
|
||||
using ModifyF = System.Func<int, int>;
|
||||
|
||||
namespace Samples;
|
||||
|
||||
[TypeSubscription("default")]
|
||||
public class Modifier(
|
||||
AgentId id,
|
||||
IAgentRuntime runtime,
|
||||
ModifyF modifyFunc
|
||||
) :
|
||||
BaseAgent(id, runtime, "Modifier", null),
|
||||
IHandle<CountMessage>
|
||||
{
|
||||
|
||||
public async ValueTask HandleAsync(CountMessage item, MessageContext messageContext)
|
||||
{
|
||||
int newValue = modifyFunc(item.Content);
|
||||
Console.WriteLine($"\nModifier:\nModified {item.Content} to {newValue}");
|
||||
|
||||
CountUpdate updateMessage = new CountUpdate { NewCount = newValue };
|
||||
await this.PublishMessageAsync(updateMessage, topic: new TopicId("default"));
|
||||
}
|
||||
}
|
34
dotnet/samples/getting-started/Program.cs
Normal file
34
dotnet/samples/getting-started/Program.cs
Normal file
@ -0,0 +1,34 @@
|
||||
// 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;
|
||||
|
||||
ModifyF modifyFunc = (int x) => x - 1;
|
||||
TerminationF runUntilFunc = (int x) =>
|
||||
{
|
||||
return x <= 1;
|
||||
};
|
||||
|
||||
HostApplicationBuilder builder = new HostApplicationBuilder();
|
||||
builder.Services.TryAddSingleton(modifyFunc);
|
||||
builder.Services.TryAddSingleton(runUntilFunc);
|
||||
|
||||
AgentsAppBuilder agentApp = new(builder);
|
||||
agentApp.AddAgent<Checker>("Checker");
|
||||
agentApp.AddAgent<Modifier>("Modifier");
|
||||
var app = await agentApp.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
|
||||
{
|
||||
Content = 10
|
||||
}, new TopicId("default"));
|
||||
|
||||
// Run until application shutdown
|
||||
await app.WaitForShutdownAsync();
|
15
dotnet/samples/getting-started/getting-started.csproj
Normal file
15
dotnet/samples/getting-started/getting-started.csproj
Normal file
@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>getting_started</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\Microsoft.AutoGen\Contracts\Microsoft.AutoGen.Contracts.csproj" />
|
||||
<ProjectReference Include="..\..\src\Microsoft.AutoGen\Core\Microsoft.AutoGen.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -83,10 +83,6 @@ public class AgentsApp
|
||||
|
||||
public async ValueTask ShutdownAsync() => await this.Host.StopAsync();
|
||||
|
||||
public ValueTask PublishMessageAsync<TMessage>(TMessage message, string topic, string? messageId = null, CancellationToken? cancellationToken = default)
|
||||
where TMessage : notnull
|
||||
=> this.AgentRuntime.PublishMessageAsync(message, TopicId.FromStr(topic), messageId: messageId, cancellationToken: cancellationToken);
|
||||
|
||||
public ValueTask PublishMessageAsync<TMessage>(TMessage message, TopicId topic, string? messageId = null, CancellationToken? cancellationToken = default)
|
||||
where TMessage : notnull
|
||||
{
|
||||
|
3
dotnet/src/Microsoft.AutoGen/readme.md
Normal file
3
dotnet/src/Microsoft.AutoGen/readme.md
Normal file
@ -0,0 +1,3 @@
|
||||
# Microsoft.AutoGen
|
||||
|
||||
- [Getting started sample](../../samples/getting-started/)
|
Loading…
x
Reference in New Issue
Block a user