Skip to content

Commit

Permalink
refactor WIP (doesn't compile yet)
Browse files Browse the repository at this point in the history
  • Loading branch information
kostapetan committed Nov 27, 2024
1 parent a4e6d0d commit e3288b2
Show file tree
Hide file tree
Showing 43 changed files with 121 additions and 116 deletions.
35 changes: 28 additions & 7 deletions dotnet/AutoGen.sln
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Agents", "src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj", "{FD87BD33-4616-460B-AC85-A412BA08BB78}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Abstractions", "src\Microsoft.AutoGen\Abstractions\Microsoft.AutoGen.Abstractions.csproj", "{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Extensions.SemanticKernel", "src\Microsoft.AutoGen\Extensions\SemanticKernel\Microsoft.AutoGen.Extensions.SemanticKernel.csproj", "{952827D4-8D4C-4327-AE4D-E8D25811EF35}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AgentChat", "AgentChat", "{668726B9-77BC-45CF-B576-0F0773BF1615}"
Expand Down Expand Up @@ -132,6 +130,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Extension
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.AutoGen.Agents.Tests", "test\Microsoft.AutoGen.Agents.Tests\Microsoft.AutoGen.Agents.Tests.csproj", "{394FDAF8-74F9-4977-94A5-3371737EB774}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Hello.Shared", "samples\Hello\Hello.Shared\Hello.Shared.csproj", "{F16058BE-1BE9-79AE-119E-A220BC6AADED}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Core", "src\Microsoft.AutoGen\Core\Microsoft.AutoGen.Core.csproj", "{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.Client", "src\Microsoft.AutoGen\Client\Microsoft.AutoGen.Client.csproj", "{03E150AA-3B93-B2AA-3141-1AE4A3481115}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Microsoft.AutoGen.DistributedRuntime", "src\Microsoft.AutoGen\DistributedRuntime\Microsoft.AutoGen.DistributedRuntime.csproj", "{F8726BFF-8D62-BFAB-D000-BFAA8B55649B}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -254,10 +260,6 @@ Global
{FD87BD33-4616-460B-AC85-A412BA08BB78}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FD87BD33-4616-460B-AC85-A412BA08BB78}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FD87BD33-4616-460B-AC85-A412BA08BB78}.Release|Any CPU.Build.0 = Release|Any CPU
{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106}.Release|Any CPU.Build.0 = Release|Any CPU
{952827D4-8D4C-4327-AE4D-E8D25811EF35}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{952827D4-8D4C-4327-AE4D-E8D25811EF35}.Debug|Any CPU.Build.0 = Debug|Any CPU
{952827D4-8D4C-4327-AE4D-E8D25811EF35}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand Down Expand Up @@ -342,6 +344,22 @@ Global
{394FDAF8-74F9-4977-94A5-3371737EB774}.Debug|Any CPU.Build.0 = Debug|Any CPU
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.ActiveCfg = Release|Any CPU
{394FDAF8-74F9-4977-94A5-3371737EB774}.Release|Any CPU.Build.0 = Release|Any CPU
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F16058BE-1BE9-79AE-119E-A220BC6AADED}.Release|Any CPU.Build.0 = Release|Any CPU
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49}.Release|Any CPU.Build.0 = Release|Any CPU
{03E150AA-3B93-B2AA-3141-1AE4A3481115}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{03E150AA-3B93-B2AA-3141-1AE4A3481115}.Debug|Any CPU.Build.0 = Debug|Any CPU
{03E150AA-3B93-B2AA-3141-1AE4A3481115}.Release|Any CPU.ActiveCfg = Release|Any CPU
{03E150AA-3B93-B2AA-3141-1AE4A3481115}.Release|Any CPU.Build.0 = Release|Any CPU
{F8726BFF-8D62-BFAB-D000-BFAA8B55649B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8726BFF-8D62-BFAB-D000-BFAA8B55649B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8726BFF-8D62-BFAB-D000-BFAA8B55649B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8726BFF-8D62-BFAB-D000-BFAA8B55649B}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -377,7 +395,6 @@ Global
{42A8251C-E7B3-47BB-A82E-459952EBE132} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{4BB66E06-37D8-45A0-9B97-DE590AFBA340} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{FD87BD33-4616-460B-AC85-A412BA08BB78} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{E0C991D9-0DB8-471C-ADC9-5FB16E2A0106} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{952827D4-8D4C-4327-AE4D-E8D25811EF35} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{668726B9-77BC-45CF-B576-0F0773BF1615} = {686480D7-8FEC-4ED3-9C5D-CEBE1057A7ED}
{84020C4A-933A-4693-9889-1B99304A7D76} = {668726B9-77BC-45CF-B576-0F0773BF1615}
Expand All @@ -402,6 +419,10 @@ Global
{64EF61E7-00A6-4E5E-9808-62E10993A0E5} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
{65059914-5527-4A00-9308-9FAF23D5E85A} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{394FDAF8-74F9-4977-94A5-3371737EB774} = {F823671B-3ECA-4AE6-86DA-25E920D3FE64}
{F16058BE-1BE9-79AE-119E-A220BC6AADED} = {7EB336C2-7C0A-4BC8-80C6-A3173AB8DC45}
{1222A7E7-F9C7-D89F-4953-EE063A7FDE49} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{03E150AA-3B93-B2AA-3141-1AE4A3481115} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
{F8726BFF-8D62-BFAB-D000-BFAA8B55649B} = {18BF8DD7-0585-48BF-8F97-AD333080CE06}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {93384647-528D-46C8-922C-8DB36A382F0B}
Expand Down
20 changes: 20 additions & 0 deletions dotnet/samples/Hello/Hello.Shared/Hello.Shared.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>


<ItemGroup>
<Protobuf Include=".\Protos\messages.proto" GrpcServices="Client;Server" Link="Protos\messages.proto" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Grpc.AspNetCore" />
<PackageReference Include="Grpc.Net.ClientFactory" />
<PackageReference Include="Grpc.Tools" PrivateAssets="All" />
</ItemGroup>

</Project>
File renamed without changes.
1 change: 0 additions & 1 deletion dotnet/samples/Hello/HelloAIAgents/HelloAIAgents.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Abstractions\Microsoft.AutoGen.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Extensions\MEAI\Microsoft.AutoGen.Extensions.MEAI.csproj" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion dotnet/samples/Hello/HelloAgent/HelloAgent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Abstractions\Microsoft.AutoGen.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Abstractions\Microsoft.AutoGen.Abstractions.csproj" />
<ProjectReference Include="..\..\..\src\Microsoft.AutoGen\Agents\Microsoft.AutoGen.Agents.csproj" />
</ItemGroup>
</Project>
13 changes: 0 additions & 13 deletions dotnet/src/Microsoft.AutoGen/Abstractions/AgentId.cs

This file was deleted.

12 changes: 0 additions & 12 deletions dotnet/src/Microsoft.AutoGen/Abstractions/ChatHistoryItem.cs

This file was deleted.

13 changes: 0 additions & 13 deletions dotnet/src/Microsoft.AutoGen/Abstractions/ChatState.cs

This file was deleted.

11 changes: 0 additions & 11 deletions dotnet/src/Microsoft.AutoGen/Abstractions/ChatUserType.cs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
Expand All @@ -10,7 +10,6 @@


<ItemGroup>
<ProjectReference Include="..\Abstractions\Microsoft.AutoGen.Abstractions.csproj" />
<ProjectReference Include="..\Extensions\Aspire\Microsoft.AutoGen.Extensions.Aspire.csproj" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
using System.Text.Json;
using System.Threading.Channels;
using Google.Protobuf;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents;
namespace Microsoft.AutoGen.Client;

public abstract class AgentBase : IAgentBase, IHandle
{
Expand All @@ -34,7 +34,7 @@ protected AgentBase(
{
_runtime = runtime;
runtime.AgentInstance = this;
this.EventTypes = eventTypes;
EventTypes = eventTypes;
_logger = logger ?? LoggerFactory.Create(builder => { }).CreateLogger<AgentBase>();
var subscriptionRequest = new AddSubscriptionRequest
{
Expand All @@ -43,8 +43,8 @@ protected AgentBase(
{
TypeSubscription = new TypeSubscription
{
AgentType = this.AgentId.Type,
TopicType = this.AgentId.Type + "/" + this.AgentId.Key
AgentType = AgentId.Type,
TopicType = AgentId.Type + "/" + AgentId.Key
}
}
};
Expand Down Expand Up @@ -106,7 +106,7 @@ protected internal async Task HandleRpcMessage(Message msg, CancellationToken ca
{
var activity = this.ExtractActivity(msg.CloudEvent.Type, msg.CloudEvent.Metadata);
await this.InvokeWithActivityAsync(
static ((AgentBase Agent, CloudEvent Item) state, CancellationToken _) => state.Agent.CallHandler(state.Item),
static (state, item) => state.Item1.CallHandler(state.CloudEvent),
(this, msg.CloudEvent),
activity,
msg.CloudEvent.Type, cancellationToken).ConfigureAwait(false);
Expand All @@ -116,7 +116,7 @@ await this.InvokeWithActivityAsync(
{
var activity = this.ExtractActivity(msg.Request.Method, msg.Request.Metadata);
await this.InvokeWithActivityAsync(
static ((AgentBase Agent, RpcRequest Request) state, CancellationToken ct) => state.Agent.OnRequestCoreAsync(state.Request, ct),
static (state, ct) => state.Item1.OnRequestCoreAsync(state.Request, ct),
(this, msg.Request),
activity,
msg.Request.Method, cancellationToken).ConfigureAwait(false);
Expand All @@ -139,7 +139,7 @@ public List<string> Subscribe(string topic)
TypeSubscription = new TypeSubscription
{
TopicType = topic,
AgentType = this.AgentId.Key
AgentType = AgentId.Key
}
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ protected async Task<RpcResponse> RequestAsync(AgentId target, string method, Di
var completion = new TaskCompletionSource<RpcResponse>(TaskCreationOptions.RunContinuationsAsynchronously);
_runtime.Update(request, activity);
await this.InvokeWithActivityAsync(
static async ((AgentBase Agent, RpcRequest Request, TaskCompletionSource<RpcResponse>) state, CancellationToken ct) =>
static async (state, ct) =>
{
var (self, request, completion) = state;

Expand All @@ -219,7 +219,7 @@ static async ((AgentBase Agent, RpcRequest Request, TaskCompletionSource<RpcResp
self._pendingRequests[request.RequestId] = completion;
}

await state.Agent._runtime.SendRequestAsync(state.Agent, state.Request).ConfigureAwait(false);
await state.Item1._runtime.SendRequestAsync(state.Item1, state.request).ConfigureAwait(false);

await completion.Task.ConfigureAwait(false);
},
Expand All @@ -233,7 +233,7 @@ static async ((AgentBase Agent, RpcRequest Request, TaskCompletionSource<RpcResp

public async ValueTask PublishMessageAsync<T>(T message, string? source = null, CancellationToken token = default) where T : IMessage
{
var src = string.IsNullOrWhiteSpace(source) ? this.AgentId.Key : source;
var src = string.IsNullOrWhiteSpace(source) ? AgentId.Key : source;
var evt = message.ToCloudEvent(src);
await PublishEventAsync(evt, token).ConfigureAwait(false);
}
Expand All @@ -246,9 +246,9 @@ public async ValueTask PublishEventAsync(CloudEvent item, CancellationToken canc
// TODO: fix activity
_runtime.Update(item, activity);
await this.InvokeWithActivityAsync(
static async ((AgentBase Agent, CloudEvent Event) state, CancellationToken ct) =>
static async (state, ct) =>
{
await state.Agent._runtime.PublishEventAsync(state.Event).ConfigureAwait(false);
await state.Item1._runtime.PublishEventAsync(state.item).ConfigureAwait(false);
},
(this, item),
activity,
Expand All @@ -271,7 +271,7 @@ public Task CallHandler(CloudEvent item)
try
{
// check that our target actually implements this interface, otherwise call the default static
if (genericInterfaceType.IsAssignableFrom(this.GetType()))
if (genericInterfaceType.IsAssignableFrom(GetType()))
{
methodInfo = genericInterfaceType.GetMethod(nameof(IHandle<object>.Handle), BindingFlags.Public | BindingFlags.Instance)
?? throw new InvalidOperationException($"Method not found on type {genericInterfaceType.FullName}");
Expand Down Expand Up @@ -300,7 +300,7 @@ public Task CallHandler(CloudEvent item)
public virtual Task HandleObject(object item)
{
// get all Handle<T> methods
var handleTMethods = this.GetType().GetMethods().Where(m => m.Name == "Handle" && m.GetParameters().Length == 1).ToList();
var handleTMethods = GetType().GetMethods().Where(m => m.Name == "Handle" && m.GetParameters().Length == 1).ToList();

// get the one that matches the type of the item
var handleTMethod = handleTMethods.FirstOrDefault(m => m.GetParameters()[0].ParameterType == item.GetType());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System.Diagnostics;

namespace Microsoft.AutoGen.Agents;
namespace Microsoft.AutoGen.Client;

/// <summary>
/// Provides extension methods for the <see cref="AgentBase"/> class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// AgentRuntime.cs

using System.Diagnostics;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents;
namespace Microsoft.AutoGen.Client;

internal sealed class AgentRuntime(AgentId agentId, IAgentWorker worker, ILogger<AgentBase> logger, DistributedContextPropagator distributedContextPropagator) : IAgentRuntime
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Threading.Channels;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents;
namespace Microsoft.AutoGen.Client;

public class AgentWorker :
IHostedService,
Expand Down Expand Up @@ -50,7 +50,7 @@ public async ValueTask PublishEventAsync(CloudEvent cloudEvent, CancellationToke
foreach (var (typeName, _) in _agentTypes)
{
if (typeName == nameof(Client)) { continue; }
var agent = GetOrActivateAgent(new AgentId(typeName, cloudEvent.Source));
var agent = GetOrActivateAgent(new AgentId { Type = typeName, Key = cloudEvent.Source });
agent.ReceiveMessage(new Message { CloudEvent = cloudEvent });
}
}
Expand Down Expand Up @@ -104,7 +104,7 @@ public async Task RunMessagePump()

foreach (var (typeName, _) in _agentTypes)
{
var agentToInvoke = GetOrActivateAgent(new AgentId(typeName, item.Source));
var agentToInvoke = GetOrActivateAgent(new AgentId { Type = typeName, Key = item.Source });
agentToInvoke.ReceiveMessage(msg);
}
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
// Client.cs

using System.Diagnostics;
using Microsoft.AutoGen.Abstractions;
using Microsoft.AutoGen.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

namespace Microsoft.AutoGen.Agents;
namespace Microsoft.AutoGen.Client;
public sealed class Client(IAgentWorker runtime, DistributedContextPropagator distributedContextPropagator,
[FromKeyedServices("EventTypes")] EventTypes eventTypes, ILogger<Client> logger)
: AgentBase(new AgentRuntime(new AgentId("client", Guid.NewGuid().ToString()), runtime, logger, distributedContextPropagator), eventTypes)
: AgentBase(new AgentRuntime(new AgentId { Type = "client", Key = Guid.NewGuid().ToString() }, runtime, logger, distributedContextPropagator), eventTypes)
{
}
Loading

0 comments on commit e3288b2

Please sign in to comment.